Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): bound Codex subagent progress events #5682

ClosedAndrey170170(GitHub profile, opens in a new tab)wants to merge 4 commits intomain fromrefs/pull/5682/headopened View on GitHub

Closes #5681

What Changed

  • Coalesce normal Codex child item and cumulative token-usage progress at the provider runtime boundary.
  • Preserve independent latest-item and latest-usage lanes per child thread.
  • Throttle normal progress through the shared keyed worker, while allowing a terminal child lifecycle event to flush that child's latest progress immediately.
  • Add priority-flush and scope-ownership semantics to KeyedCoalescingWorker without changing existing drainKey behavior.
  • Bind the progress worker to the Codex runtime scope so explicit close cancels pending best-effort progress before event queues shut down.
  • Add focused coverage for burst coalescing, latest-value preservation, progress-before-terminal ordering, cross-key priority flush, and scope-close interruption.

Why

Native Codex subagent observability currently emits every child item/started, item/completed, and cumulative token-usage snapshot as a separate provider event. The adapter maps each event to task.progress, and provider ingestion persists every update as a durable activity append.

Stable task activity IDs keep projected row count small, but do not reduce event-store or command-processing work. A busy subagent fleet can therefore fill the serial ingestion path with redundant progress snapshots and delay unrelated threads.

Coalescing belongs at the provider boundary because these events are latest-state snapshots: the UI needs the newest item summary and cumulative usage, not every intermediate value. Terminal lifecycle events still flush the latest pending values first, so task state cannot overtake its final progress.

Normal progress is throttled per Codex session. Terminal flushes intentionally bypass the debounce and may emit up to the latest item and usage snapshots immediately; this avoids cross-child head-of-line blocking without claiming an absolute rate cap over lifecycle flushes.

Validation

  • node_modules/vite-plus/bin/vp test run packages/shared/src/KeyedCoalescingWorker.test.ts apps/server/src/provider/Layers/CodexCollabRuntime.integration.test.ts
    • 2 test files passed
    • 6 tests passed
  • Targeted vp lint --type-aware --type-check --report-unused-disable-directives on all four changed files passed.
  • Targeted vp fmt --check on all four changed files passed.
  • git diff --check passed.

UI Changes

None.

Scope

This PR does not change raw assistant streaming, global orchestration scheduling, projectors, SQLite cleanup, or UI behavior. It addresses only Codex child progress amplification and the worker semantics required for correct terminal flushing.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I added focused regression coverage
  • No UI screenshots or interaction video are needed
  • Proposed issue: #5681
  • Native subagent observability: PR #5219
  • Related assistant-stream persistence amplification: issue #5110
  • Regression-introducing commit: a2ca89aa

Prepared with Codex CLI (GPT-5.6-sol).


Note

Medium Risk Touches provider event ordering and ingestion volume for collab children; terminal flush ordering is correctness-sensitive but scoped to Codex runtime and covered by integration tests.

Overview Codex collab child progress (collabAgent/item, collabAgent/tokenUsage) is no longer emitted on every wire notification. Updates are merged per child thread into separate latest-item and latest-usage lanes, debounced (~250ms) through a session-scoped KeyedCoalescingWorker, then emitted as provider events.

Terminal lifecycle (turn/completed, non-active thread/status/changed, thread/closed, terminal child error) calls flushKey so the newest pending progress is delivered before idle/complete/close events, without waiting behind other children. Normal flushes skip the debounce sleep.

KeyedCoalescingWorker gains flushKey (priority processing for one key, context.flush on process) and documents that drainKey behavior is unchanged. Closing the worker’s scope interrupts in-flight work.

Session close now Scope.close(runtimeScope) before session/closed and queue shutdown so coalesced progress cannot enqueue after teardown.

Integration and unit tests cover burst coalescing, latest-value retention, progress-before-terminal ordering, cross-key flush, and scope-close interruption.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit c74ae215156452ba8de454ad2f179949c4ff6be8. Configure [here](https://www.cursor.com/dashboard/bugbot).

Note

Bound Codex subagent progress events with coalescing and flush-before-terminal semantics

  • Collab child progress notifications (collabAgent/item and collabAgent/tokenUsage) are now coalesced via a keyed worker with a 250ms cooldown, emitting only the latest item and token usage per child within each window.
  • Before emitting terminal child lifecycle events (turn completed, status changed to non-active, thread closed, terminal errors), the latest coalesced progress is flushed synchronously to preserve ordering.
  • Extends KeyedCoalescingWorker in KeyedCoalescingWorker.ts with a flushKey method that bypasses the queue and cooldown for a specific key, passing a flush-context flag to the process callback.
  • Session close now shuts down the runtime scope before emitting session/closed, cancelling any pending best-effort progress emissions.
  • Behavioral Change: child progress events may be delayed up to ~250ms and burst updates are collapsed to a single emit; consumers will see fewer, coalesced events rather than every individual update.
Macroscope summarized d765754.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Important

      Review skipped

      Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

      ⚙️ Run configuration

      Configuration used: Repository UI

      Review profile: CHILL

      Plan: Pro Plus

      Run ID: 5cbe578b-4cd0-4c2a-9d64-5d3be26673c7

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    3. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟠 High

      https://github.com/pingdotgg/t3code/blob/c74ae215156452ba8de454ad2f179949c4ff6be8/packages/shared/src/KeyedCoalescingWorker.ts#L102

      flushKey processes a key directly while the same key may still be sitting in the background TxQueue. If a new value is enqueued for that key during the flush, the background worker dequeues the stale entry, removes the pending value from latestByKey, and starts a second processKey concurrently for the same key. This breaks per-key serialization and can reorder or concurrently persist values that should follow the flushed one.

      The worker dequeue path at line 102 does not check activeKeys before promoting a key, so it does not skip keys already being processed by flushKey. Consider either draining the stale queue entry for the key inside flushKey, or making the dequeue path skip keys already in activeKeys.

      ✅ Resolved in 5e1a02bf69ed761bbcd6636a3ebcae5882606a52

    4. 💡 Codex Review

      Here are some automated review suggestions for this pull request.

      Reviewed commit: c74ae21515

      ℹ️ About Codex in GitHub

      Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

      • Open a pull request for review
      • Mark a draft as ready
      • Comment "@codex review".

      If Codex has suggestions, it will comment; otherwise it will react with 👍.

      Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

      @@ -47,9 +60,18 @@
      60+ const flushRequestedKeys = new Set(state.flushRequestedKeys);
      61+ flushRequestedKeys.delete(key);
      62+ return [null, { ...state, activeKeys, flushRequestedKeys }] as const;
      4763 }
      4864
      4965 const latestByKey = new Map(state.latestByKey);
      5066 latestByKey.delete(key);
      51- return [nextValue, { ...state, latestByKey }] as const;
      67+ return [
      68+ {
      69+ value: nextValue,
      70+ flush: state.flushRequestedKeys.has(key),
      71+ },
      72+ { ...state, latestByKey },
      73+ ] as const;
      5274 }).pipe(Effect.tx),
      5375 ),
      54- Effect.flatMap((nextValue) =>
      55- nextValue === null ? Effect.void : processKey(key, nextValue),
      76+ Effect.flatMap((next) =>
      77+ next === null ? Effect.void : processKey(key, next.value, next.flush),

      P2 Badge Yield hot keys before processing their next value

      When one Codex child produces progress at least once per 250 ms debounce window, latestByKey remains populated and this recursive call immediately processes that same key again instead of returning it to the queue. Normal progress for every other queued child can therefore remain stale indefinitely until the hot child pauses or a terminal event invokes flushKey; requeue the next value so the worker can service keys round-robin.

      AGENTS.md reference: AGENTS.md:L142-L142

      Useful? React with 👍 / 👎.

    5. Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.

      Fix All in Cursor ❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the [Cursor dashboard](https://www.cursor.com/dashboard/bugbot).

      Want fixes drafted automatically? Bugbot Autofix can create code changes for findings. A team admin can enable Autofix in the Cursor dashboard.

      Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit c74ae215156452ba8de454ad2f179949c4ff6be8. Configure [here](https://www.cursor.com/dashboard/bugbot).
      @@ -141,0 +185,21 @@
      185+ activeKeys.add(key);
      186+ return [
      187+ { value },
      188+ {
      189+ ...state,
      190+ latestByKey,
      191+ queuedKeys,
      192+ activeKeys,
      193+ flushRequestedKeys,
      194+ },
      195+ ] as const;
      196+ }).pipe(
      197+ Effect.tx,
      198+ Effect.flatMap((item) =>
      199+ item === null
      200+ ? Effect.void
      201+ : processKey(key, item.value, true).pipe(
      202+ Effect.catchCause(() => cleanupFailedKey(key)),
      203+ ),
      204+ ),
      205+ Effect.andThen(drainKey(key)),

      Flush swallows interrupt on close

      High Severity

      flushKey wraps processKey in Effect.catchCause that recovers from all causes, including fiber interrupts. Codex runs flushKey on the notification consumer fiber and now closes runtimeScope before queue shutdown, so a close that lands mid-flush can be swallowed. The consumer then keeps waiting on serverNotifications while Scope.close waits for that fiber, deadlocking session teardown.

      Additional Locations (2)
      Fix in Cursor Fix in Web
      Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit c74ae215156452ba8de454ad2f179949c4ff6be8. Configure [here](https://www.cursor.com/dashboard/bugbot).
      @@ -92,4 +116,4 @@
      92116 activeKeys.add(key);
      93117
      94118 return [
      95- { key, value } as const,
      119+ { key, value, flush: state.flushRequestedKeys.has(key) } as const,

      Flush races with stale queue take

      Medium Severity

      flushKey can steal a queued key and run processKey on the caller fiber while leaving a stale TxQueue entry. The queue consumer only checks whether latestByKey still has a value, not whether the key is already in activeKeys. A concurrent enqueue during that flush can repopulate latestByKey, so the stale take starts a second processor for the same key and can clear activeKeys early, letting drainKey finish before progress emission completes.

      Additional Locations (1)
      Fix in Cursor Fix in Web
      Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit c74ae215156452ba8de454ad2f179949c4ff6be8. Configure [here](https://www.cursor.com/dashboard/bugbot).
    6. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Needs human review

      1 blocking correctness issue found. This PR introduces a new concurrent KeyedCoalescingWorker pattern to debounce subagent progress events with flush/drain semantics, and modifies session lifecycle timing. Multiple unresolved review comments flag potential deadlocks and race conditions in the new concurrency logic. The substantial new runtime behavior and open correctness concerns warrant human review.

      You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    7. Note

      🤖 GPT-5.6 Sol responding on behalf of Theo

      We're closing this PR as we clean up the T3 Code backlog. Thank you for taking the time to put this together.

      We are keeping the Codex child-progress fix in the open #6004, which coalesces progress inside the Codex adapter before ingestion. The separate item and usage lanes, terminal flush behavior, and ordering tests here remain useful input for that review.

      If you believe we closed this in error, please reopen the PR and leave a comment explaining what we missed.

Sign in to join this conversation.