Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(codex): stream agent text while the answer is generated (#10100)
pseudoyu611b428eaaab
diff --git a/server/pkg/agent/codex.go b/server/pkg/agent/codex.goindex 9ac4c06970..db7998c275 100644--- a/server/pkg/agent/codex.go+++ b/server/pkg/agent/codex.go@@ -81,15 +81,18 @@ const (codexVersionDiagnosticTimeout = 2 * time.Second// Keep a raw agent-message tail small enough that a slow daemon does not// make the stdout reader retain token-sized channel entries indefinitely.- // At one sixty-fourth of the scanner's maximum completed-snapshot line, any- // reconcilable item occupies at most 65 of the 256 message slots (the leading- // delta plus 64 aggregates), reserving the rest for status and tool events.- // Since this threshold is 512 KiB, a normal message will not reach it: only- // its leading delta is usually handed off during generation, while the- // remaining deltas stay pending until item/completed reconciles the item.- // A single provider event may be larger, in which case that event is flushed- // as one chunk rather than split at an arbitrary byte boundary.+ // At one sixty-fourth of the scanner's maximum completed-snapshot line, a+ // burst of deltas costs at most one channel slot per 512 KiB, never one per+ // token. A normal message will not reach this size; its tail is handed off+ // on codexAgentMessageFlushInterval instead. A single+ // provider event may be larger, in which case that event is flushed as one+ // chunk rather than split at an arbitrary byte boundary.codexAgentMessageAggregateBytes = agentStreamMaxLineBytes / 64+ // codexAgentMessageFlushInterval is how long pending agent text may wait+ // before the next delta hands it off. It matches the daemon's 500 ms report+ // batch, so a long answer streams at the rate the server can show it while+ // a token burst still costs at most one channel slot per interval.+ codexAgentMessageFlushInterval = 500 * time.Millisecond// codexGracefulShutdownTimeout bounds how long the lifecycle goroutine// waits for codex to exit on its own after stdin is closed, before forcing// a context-cancel kill. A clean exit lets codex run its shutdown path and@@ -2457,6 +2460,8 @@ type codexClient struct {// advanced only after onAgentMessageChunk confirms daemon handoff.agentMessageStreams map[string]*codexAgentMessageStreamagentMessageOrder []string+ // now is the clock for agent-text flush pacing; nil means time.Now.+ now func() time.TimenotificationProtocol string // "unknown", "legacy", "raw"turnCompleted bool@@ -2476,6 +2481,8 @@ type codexClient struct {type codexAgentMessageStream struct {delivered strings.Builderpending strings.Builder+ // lastFlush is when pending text was last offered to the daemon channel.+ lastFlush time.Time}// codexTurnNotificationGate keeps resume-time history replay from mutating the@@ -3650,9 +3657,11 @@ func (c *codexClient) handleAgentMessageDelta(itemID, delta string) {stream := c.agentMessageStream(itemID)// The leading delta is the latency-critical event. Deliver it immediately;- // coalesce the rest so a burst of token notifications cannot fill the- // daemon channel before its consumer is scheduled.+ // coalesce the rest into one chunk per flush interval so a burst of token+ // notifications cannot fill the daemon channel before its consumer is+ // scheduled, while a long answer still streams as it is generated.if stream.delivered.Len() == 0 && stream.pending.Len() == 0 {+ stream.lastFlush = c.clock()if c.emitAgentMessageChunk(delta) {stream.delivered.WriteString(delta)return@@ -3660,11 +3669,20 @@ func (c *codexClient) handleAgentMessageDelta(itemID, delta string) {}stream.pending.WriteString(delta)- if stream.pending.Len() >= codexAgentMessageAggregateBytes {+ if stream.pending.Len() >= codexAgentMessageAggregateBytes ||+ c.clock().Sub(stream.lastFlush) >= codexAgentMessageFlushInterval {+ stream.lastFlush = c.clock()c.flushAgentMessageStream(stream)}}+func (c *codexClient) clock() time.Time {+ if c.now != nil {+ return c.now()+ }+ return time.Now()+}+func (c *codexClient) flushAgentMessageStream(stream *codexAgentMessageStream) bool {if stream == nil || stream.pending.Len() == 0 {return truediff --git a/server/pkg/agent/codex_test.go b/server/pkg/agent/codex_test.goindex fd29fb7930..738ba1466f 100644--- a/server/pkg/agent/codex_test.go+++ b/server/pkg/agent/codex_test.go@@ -12,6 +12,7 @@ import ("path/filepath""reflect""runtime"+ "slices""strings""sync""testing"@@ -1621,6 +1622,43 @@ func TestCodexRawAgentMessageBelowAggregationThresholdDefersTailUntilCompleted(t}}+// A long answer must keep streaming while it is generated, not show its first+// token and then nothing until item/completed (Telegram, Slack and the web chat+// all rendered the reply as stuck on its first word).+func TestCodexRawAgentMessageFlushesPendingTailOnInterval(t *testing.T) {+ t.Parallel()++ c, _, _ := newTestCodexClient(t)+ c.notificationProtocol = "raw"+ clock := time.Unix(1_700_000_000, 0)+ c.now = func() time.Time { return clock }+ var chunks []string+ c.onAgentMessageChunk = func(text string) bool {+ chunks = append(chunks, text)+ return true+ }+ delta := func(after time.Duration, text string) {+ clock = clock.Add(after)+ c.handleLine(fmt.Sprintf(`{"jsonrpc":"2.0","method":"item/agentMessage/delta","params":{"threadId":"thr-1","turnId":"turn-1","itemId":"msg-1","delta":%q}}`, text))+ }++ delta(0, "The ")+ delta(codexAgentMessageFlushInterval/5, "answer ")+ if want := []string{"The "}; !slices.Equal(chunks, want) {+ t.Fatalf("chunks inside the interval = %q, want %q", chunks, want)+ }+ delta(codexAgentMessageFlushInterval, "is ")+ if want := []string{"The ", "answer is "}; !slices.Equal(chunks, want) {+ t.Fatalf("chunks after the interval = %q, want %q", chunks, want)+ }+ delta(codexAgentMessageFlushInterval/5, "42.")++ c.handleLine(`{"jsonrpc":"2.0","method":"item/completed","params":{"threadId":"thr-1","turnId":"turn-1","item":{"type":"agentMessage","id":"msg-1","text":"The answer is 42."}}}`)+ if want := []string{"The ", "answer is ", "42."}; !slices.Equal(chunks, want) {+ t.Fatalf("chunks after item/completed = %q, want %q", chunks, want)+ }+}+func TestCodexRawAgentMessageMismatchRetriesRejectedPendingAtTerminal(t *testing.T) {t.Parallel()