Skip to content

Move stateful message handling into AgentMessageHandler (step 4b)#193

Merged
shellicar merged 3 commits intomainfrom
feature/agent-message-handler-stateful
Apr 6, 2026
Merged

Move stateful message handling into AgentMessageHandler (step 4b)#193
shellicar merged 3 commits intomainfrom
feature/agent-message-handler-stateful

Conversation

@shellicar
Copy link
Copy Markdown
Owner

Completes the AgentMessageHandler extraction by moving the stateful cases in.

What moves in:

  • Helper functions fmtBytes, primaryArg, formatRefSummary, formatToolSummary (were module-level in runAgent.ts)
  • Private fields: #lastUsage, #usageBeforeTools
  • message_usage case: delta token calculation, marginal cost annotation via appendToLastSealed
  • tool_approval_request case: snapshots usageBeforeTools, fires #toolApprovalRequest
  • tool_error case
  • Private #toolApprovalRequest async method (permission check, approval prompt, respond callback)

Constructor change: adds opts: AgentMessageHandlerOptionsmodel, cacheTtl, cwd, store, tools, respond callback. The respond callback abstracts port.postMessage so the handler doesn't hold a reference to port.

runAgent.ts after this: 89 lines. Sets up tools, calls agent.runAgent(), constructs respond + handler, wires port.on('message', handler.handle). No message logic lives there anymore.

Restores: the message_compaction context-usage annotation ([compacted at X/Y (Z%)]) that was temporarily omitted in 4a because it needed lastUsage.

Tests: 27 total (16 unchanged + 11 new). New tests cover tool_error, message_usage with/without tool batch, delta annotation values, reset-timing sequence (two batches compute independently), and message_compaction annotation presence.

@shellicar shellicar added this to the 1.0 milestone Apr 6, 2026
@shellicar shellicar added the enhancement New feature or request label Apr 6, 2026
@shellicar shellicar self-assigned this Apr 6, 2026
@shellicar shellicar added the enhancement New feature or request label Apr 6, 2026
@shellicar shellicar requested a review from bananabot9000 April 6, 2026 11:49
@shellicar shellicar enabled auto-merge (squash) April 6, 2026 11:49
@shellicar shellicar disabled auto-merge April 6, 2026 12:10
Copy link
Copy Markdown
Collaborator

@bananabot9000 bananabot9000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stateful extraction complete, regression fixed 🍌

State properly encapsulated: #lastUsage and #usageBeforeTools are private fields on the handler. Three stateful cases moved in: tool_approval_request, tool_error, message_usage. Four helper functions relocated. Constructor takes typed AgentMessageHandlerOptions with respond callback abstracting the message port.

4a regression fixed: message_compaction now reads this.#lastUsage for the [compacted at X/Y (Z%)] annotation. Both present and absent paths tested.

runAgent.ts is now 89 lines. No message logic remains. Just tool setup, agent.runAgent(), handler instantiation, and port.on('message', handler.handle). A thin wiring function.

27 tests (16 existing + 11 new). The delta reset-timing tests are the highlight -- proving two independent tool batches compute deltas independently, snapshot resets between batches. This was the risky part and it's well covered.

Observations (not blocking):

  • void this.#toolApprovalRequest(msg) -- fire-and-forget async, matches original pattern, void makes intent explicit. Good.
  • removePendingTool duplicated in try + catch -- could be finally, but explicit is fine.
  • tool_approval_request not directly tested (async + mock complexity). Acceptable scope boundary for this step.
  • Unicode escapes replacing literals (\u00b7, \u2190, \u2013) -- functionally identical, likely editor normalization.

Ship it 🚢🍌

@shellicar shellicar merged commit d6afe37 into main Apr 6, 2026
4 checks passed
@shellicar shellicar deleted the feature/agent-message-handler-stateful branch April 6, 2026 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants