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
+6 -19
View File
@@ -393,19 +393,13 @@ type midTurn struct {
func (m *Manager) deliverMidTurn(ctx context.Context, l *live) midTurn { func (m *Manager) deliverMidTurn(ctx context.Context, l *live) midTurn {
if l.owner != "" { if l.owner != "" {
inputs, held := m.takeLeadQueuedMidTurn(ctx, l) inputs, held := m.takeLeadQueuedMidTurn(ctx, l)
parts := []string{}
if len(inputs) > 0 {
msgs := []queued{} msgs := []queued{}
for _, in := range inputs { for _, in := range inputs {
msgs = append(msgs, queued{actor: in.Actor, text: in.Text, sendID: in.SendID, client: in.Client}) msgs = append(msgs, queued{actor: in.Actor, text: in.Text, sendID: in.SendID, client: in.Client})
} }
parts = append(parts, m.recordQueued(ctx, l, msgs))
}
mt := midTurn{leadInputs: inputs} mt := midTurn{leadInputs: inputs}
if hint := m.imagesHint(l, held); hint != "" { parts, hinted := m.queuedParts(ctx, l, msgs, held)
parts = append(parts, hint) mt.hinted = hinted
mt.hinted = held.id
}
mt.text = strings.Join(parts, "\n\n") mt.text = strings.Join(parts, "\n\n")
return mt return mt
} }
@@ -425,21 +419,14 @@ func (m *Manager) deliverMidTurn(ctx context.Context, l *live) midTurn {
return midTurn{} return midTurn{}
} }
mt := midTurn{}
parts := []string{}
inputs, held := m.takeQueuedMidTurn(ctx, l) inputs, held := m.takeQueuedMidTurn(ctx, l)
mt.taskInputs = inputs
if len(mt.taskInputs) > 0 {
msgs := []queued{} msgs := []queued{}
for _, in := range mt.taskInputs { for _, in := range inputs {
msgs = append(msgs, queued{actor: in.Actor, text: in.Text, sendID: in.SendID, client: in.Client}) msgs = append(msgs, queued{actor: in.Actor, text: in.Text, sendID: in.SendID, client: in.Client})
} }
parts = append(parts, m.recordQueued(ctx, l, msgs)) mt := midTurn{taskInputs: inputs}
} parts, hinted := m.queuedParts(ctx, l, msgs, held)
if hint := m.imagesHint(l, held); hint != "" { mt.hinted = hinted
parts = append(parts, hint)
mt.hinted = held.id
}
responses, ids := m.responsesMidTurn(ctx, l) responses, ids := m.responsesMidTurn(ctx, l)
if responses != "" { if responses != "" {
parts = append(parts, responses) parts = append(parts, responses)
+35 -22
View File
@@ -181,6 +181,20 @@ type heldImages struct {
actor string 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) { func (m *Manager) takeQueuedMidTurn(ctx context.Context, l *live) ([]store.TaskInput, heldImages) {
m.mu.Lock() m.mu.Lock()
compacting := l.compacting 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) slog.Error("list queued messages", "session", l.id, "err", err)
return nil, heldImages{} return nil, heldImages{}
} }
out := []store.TaskInput{} out, ids, held := takeBeforeImages(inputs, func(in store.TaskInput) (int64, string, []string) {
ids := []int64{} return in.ID, in.Actor, in.Images
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)
}
if len(ids) == 0 { if len(ids) == 0 {
return nil, held 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) slog.Error("list held lead messages", "session", l.id, "err", err)
return nil, heldImages{} return nil, heldImages{}
} }
out := []store.LeadInput{} messages := []store.LeadInput{}
ids := []int64{}
held := heldImages{}
for _, in := range inputs { for _, in := range inputs {
if in.Kind != store.LeadMessage { if in.Kind == store.LeadMessage {
continue 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 { if len(ids) == 0 {
return nil, held return nil, held
} }
@@ -260,6 +261,18 @@ func (m *Manager) takeLeadQueuedMidTurn(ctx context.Context, l *live) ([]store.L
return out, held 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 { func (m *Manager) imagesHint(l *live, held heldImages) string {
if held.id == 0 { if held.id == 0 {
return "" return ""
-1
View File
@@ -81,7 +81,6 @@ type bundle struct {
type gen struct { type gen struct {
defs map[string]*schema defs map[string]*schema
order []string
emitted map[string]bool emitted map[string]bool
kinds map[string]string kinds map[string]string
out bytes.Buffer out bytes.Buffer