Simplify codebase per audit of 4e20fe8

This commit is contained in:
Ian Gulliver
2026-10-23 11:05:45 -07:00
parent 4e20fe8a12
commit 1a7ff950e9
3 changed files with 46 additions and 47 deletions
+35 -22
View File
@@ -181,6 +181,20 @@ type heldImages struct {
actor string
}
func takeBeforeImages[T any](inputs []T, ref func(T) (int64, string, []string)) ([]T, []int64, heldImages) {
out := []T{}
ids := []int64{}
for _, in := range inputs {
id, actor, images := ref(in)
if len(images) > 0 {
return out, ids, heldImages{id, actor}
}
out = append(out, in)
ids = append(ids, id)
}
return out, ids, heldImages{}
}
func (m *Manager) takeQueuedMidTurn(ctx context.Context, l *live) ([]store.TaskInput, heldImages) {
m.mu.Lock()
compacting := l.compacting
@@ -197,17 +211,9 @@ func (m *Manager) takeQueuedMidTurn(ctx context.Context, l *live) ([]store.TaskI
slog.Error("list queued messages", "session", l.id, "err", err)
return nil, heldImages{}
}
out := []store.TaskInput{}
ids := []int64{}
held := heldImages{}
for _, in := range inputs {
if len(in.Images) > 0 {
held = heldImages{in.ID, in.Actor}
break
}
out = append(out, in)
ids = append(ids, in.ID)
}
out, ids, held := takeBeforeImages(inputs, func(in store.TaskInput) (int64, string, []string) {
return in.ID, in.Actor, in.Images
})
if len(ids) == 0 {
return nil, held
}
@@ -235,20 +241,15 @@ func (m *Manager) takeLeadQueuedMidTurn(ctx context.Context, l *live) ([]store.L
slog.Error("list held lead messages", "session", l.id, "err", err)
return nil, heldImages{}
}
out := []store.LeadInput{}
ids := []int64{}
held := heldImages{}
messages := []store.LeadInput{}
for _, in := range inputs {
if in.Kind != store.LeadMessage {
continue
if in.Kind == store.LeadMessage {
messages = append(messages, in)
}
if len(in.Images) > 0 {
held = heldImages{in.ID, in.Actor}
break
}
out = append(out, in)
ids = append(ids, in.ID)
}
out, ids, held := takeBeforeImages(messages, func(in store.LeadInput) (int64, string, []string) {
return in.ID, in.Actor, in.Images
})
if len(ids) == 0 {
return nil, held
}
@@ -260,6 +261,18 @@ func (m *Manager) takeLeadQueuedMidTurn(ctx context.Context, l *live) ([]store.L
return out, held
}
func (m *Manager) queuedParts(ctx context.Context, l *live, msgs []queued, held heldImages) ([]string, int64) {
parts := []string{}
if len(msgs) > 0 {
parts = append(parts, m.recordQueued(ctx, l, msgs))
}
hint := m.imagesHint(l, held)
if hint == "" {
return parts, 0
}
return append(parts, hint), held.id
}
func (m *Manager) imagesHint(l *live, held heldImages) string {
if held.id == 0 {
return ""