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
+11 -24
View File
@@ -393,19 +393,13 @@ type midTurn struct {
func (m *Manager) deliverMidTurn(ctx context.Context, l *live) midTurn {
if l.owner != "" {
inputs, held := m.takeLeadQueuedMidTurn(ctx, l)
parts := []string{}
if len(inputs) > 0 {
msgs := []queued{}
for _, in := range inputs {
msgs = append(msgs, queued{actor: in.Actor, text: in.Text, sendID: in.SendID, client: in.Client})
}
parts = append(parts, m.recordQueued(ctx, l, msgs))
msgs := []queued{}
for _, in := range inputs {
msgs = append(msgs, queued{actor: in.Actor, text: in.Text, sendID: in.SendID, client: in.Client})
}
mt := midTurn{leadInputs: inputs}
if hint := m.imagesHint(l, held); hint != "" {
parts = append(parts, hint)
mt.hinted = held.id
}
parts, hinted := m.queuedParts(ctx, l, msgs, held)
mt.hinted = hinted
mt.text = strings.Join(parts, "\n\n")
return mt
}
@@ -425,21 +419,14 @@ func (m *Manager) deliverMidTurn(ctx context.Context, l *live) midTurn {
return midTurn{}
}
mt := midTurn{}
parts := []string{}
inputs, held := m.takeQueuedMidTurn(ctx, l)
mt.taskInputs = inputs
if len(mt.taskInputs) > 0 {
msgs := []queued{}
for _, in := range mt.taskInputs {
msgs = append(msgs, queued{actor: in.Actor, text: in.Text, sendID: in.SendID, client: in.Client})
}
parts = append(parts, m.recordQueued(ctx, l, msgs))
}
if hint := m.imagesHint(l, held); hint != "" {
parts = append(parts, hint)
mt.hinted = held.id
msgs := []queued{}
for _, in := range inputs {
msgs = append(msgs, queued{actor: in.Actor, text: in.Text, sendID: in.SendID, client: in.Client})
}
mt := midTurn{taskInputs: inputs}
parts, hinted := m.queuedParts(ctx, l, msgs, held)
mt.hinted = hinted
responses, ids := m.responsesMidTurn(ctx, l)
if responses != "" {
parts = append(parts, responses)
+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 ""
-1
View File
@@ -81,7 +81,6 @@ type bundle struct {
type gen struct {
defs map[string]*schema
order []string
emitted map[string]bool
kinds map[string]string
out bytes.Buffer