Skip to content

fix(agent-runtime): restore on-demand tool activations still in context - #231

Merged
vastsa merged 1 commit into
vastsa:mainfrom
L4XB:fix/restore-deferred-tools-from-context
Sep 11, 2026
Merged

fix(agent-runtime): restore on-demand tool activations still in context#231
vastsa merged 1 commit into
vastsa:mainfrom
L4XB:fix/restore-deferred-tools-from-context

Conversation

@L4XB

@L4XB L4XB commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Fixes #225

Problem

resetDeferredToolsForPrompt() (called from prompt() and executeApprovedPlan()) and setMode() cleared activeDeferredToolNames outright. The session context, however, still contained the successful ToolSearch results ("Activated on-demand tools: Skill.") and the results those tools produced, so the model reasonably believed the tools were still in the schema, called them, and got a failure or re-searched.

Change

packages/agent-runtime/src/runtime.ts

  • New restoreDeferredToolsFromContext(): walks buildSessionContext(this.entriesWithCompaction()).messages and re-activates
    • addedToolNames of successful ToolSearch results, and
    • the tool itself for a successful result of a deferred tool,
      keeping only names still in deferredToolNames (which rebuildToolCatalog already limits to tools allowed in the current mode).
  • Rows with isError and rows whose only content is the [no tool result recorded] placeholder are ignored (the placeholder string is now a shared constant so both sites agree). Assistant/user prose is not parsed.
  • resetDeferredToolsForPrompt() clears, restores, then sets agent.state.tools; setMode() restores after rebuildToolCatalog() so a mode round trip does not drop activations either.

Tests

packages/agent-runtime/src/runtime.test.ts, new describe "deferred tool restore (#225)":

  • a persisted successful ToolSearch row keeps BrowserPreview active across resetDeferredToolsForPrompt();
  • a failed ToolSearch row and a BrowserPreview row without a recorded result restore nothing, while a successful BrowserPreview result does;
  • the activation survives setMode("plan")setMode("agent").

The existing "resets deferred capabilities at the beginning of a new prompt" test still passes: its activation was made through a direct execute() call and never entered the context, so there is nothing to restore.

Verified locally: vitest run src/runtime.test.ts -t deferred (11 passed), tsc -p tsconfig.json --noEmit in packages/agent-runtime.

Every new prompt and mode switch cleared the active deferred-tool set,
but the transcript kept the ToolSearch results that announced those
activations and the results the tools produced. The model therefore
kept calling tools that were missing from the schema (vastsa#225).

Rebuild the active set from the effective session context instead:
successful ToolSearch rows contribute their addedToolNames, a successful
result of a deferred tool contributes that tool, and only names still in
the deferred catalog (already limited to the current mode) are kept.
Failed rows and rows without a recorded result are ignored.
Copilot AI lite review requested due to automatic review settings September 11, 2026 08:01
@vercel

vercel Bot commented Sep 11, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
pi-desktop-docs Skipped Skipped Sep 11, 2026 8:01am UTC

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@vastsa
vastsa merged commit 9012b59 into vastsa:main Sep 11, 2026
4 checks passed
@vastsa

vastsa commented Sep 11, 2026

Copy link
Copy Markdown
Owner

Reviewed and merged. The PR addresses the verified #225 regression: successful deferred-tool activations retained in the effective session context are restored after per-prompt reset, while failed/interrupted/missing results and mode-ineligible tools are excluded. Validation on the PR head: 11 deferred-runtime tests passed, agent-runtime typecheck passed, and protocol E2E passed 18/18 (2 live-model scenarios skipped because PI_DESKTOP_TEST_API_KEY was not set). Follow-up spec/ADR/E2E documentation sync landed locally after the merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants