Skip to content

fix(deferred): persist activated tools across session resume and durable runs - #5125

Open
iemafzalhassan wants to merge 2 commits into
docker:mainfrom
iemafzalhassan:fix/persist-deferred-activated-tools
Open

iemafzalhassan wants to merge 2 commits into
docker:mainfrom
iemafzalhassan:fix/persist-deferred-activated-tools

Conversation

@iemafzalhassan

Copy link
Copy Markdown

Closes #5056

Persist deferred.ToolSet activations across session resumes and DurableRuntime checkpoints without adding dedicated fields or accessors to session.Session.

Summary of changes

  • Add a tools.Stateful capability interface (StateKey, SnapshotState, RestoreState) in pkg/tools/stateful.go.
  • Implement tools.Stateful on deferred.ToolSet in pkg/tools/builtin/deferred/deferred.go, encoding sorted activated tool names as JSON and keeping not-yet-listed names in pendingRestore until delayed sources finish starting and list their tools.
  • Save and restore per-agent stateful toolset snapshots through session.Session.Attributes (docker-agent.toolset.<agent>.<key>) in pkg/runtime/toolset_state.go, wired into runStreamLoop, EmitStartupInfo, and ACP reconnectSession.
  • Carry Session.Attributes in DurableRuntime checkpoints (pkg/runtime/durable.go) and flush stateful toolset snapshots during CallFinished, so ContinueStream restores mid-batch and between-turn activations cleanly.
  • Add unit and integration tests covering snapshot/restore, slow deferred sources, SQLite session resume, sequential session isolation, and DurableRuntime suspend/resume (including mid-batch interruption).

Verification

  • go test -v -race ./pkg/tools ./pkg/tools/builtin/deferred
  • go test -v -race ./pkg/runtime -run "TestLocalRuntime_DeferredTools|TestLocalRuntime_Stateful|TestDurableRuntime_DeferredTools"
  • go test ./pkg/session/... ./pkg/runtime/... ./pkg/acp ./pkg/teamloader/... ./pkg/app/... ./pkg/server/...
  • go run ./lint . (3197 file(s) inspected, no offenses detected)
  • go vet ./...
  • gofmt -s -l clean across all modified and added files

@iemafzalhassan
iemafzalhassan requested a review from a team as a code owner December 4, 2026 13:19
Copilot AI balanced review requested due to automatic review settings December 4, 2026 13:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 insufficient remaining quota. Try requesting a review at a lower effort level.

@aheritier aheritier added area/agent For work that has to do with the general agent loop/agentic features of the app area/runtime Runtime engine, agent loop execution, tool dispatch, loop detection area/tools For features/issues/fixes related to the usage of built-in and MCP tools kind/fix PR fixes a bug (maps to fix:). Use on PRs only. status/needs-signed-commits Some commits in the PR are signed with a valid SSH/GPG key labels Dec 4, 2026
@aheritier

Copy link
Copy Markdown
Collaborator

👋 Some commits in this PR are not signed and verified by GitHub. Please sign your commits with a GPG or SSH key registered in your GitHub account, then force-push.

Commits that are not verified: d9d5b6c

See GitHub’s guide on signing commits for setup instructions. I’ve added status/needs-signed-commits; it will be removed automatically once every commit in this PR carries a valid GitHub-verified signature.

…ble runs

Introduce a tools.Stateful capability backed by existing Session.Attributes and DurableRuntime checkpoints so add_tool activations survive process restarts and mid-run continuation without adding dedicated accessors to session.Session.

Closes docker#5056

Signed-off-by: iemafzalhassan <iemafzalhassan@gmail.com>

@aheritier aheritier left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

7 blocking issues inline: durable runs can lose activations or session metadata, and restore can mix state across sessions, start from the wrong branch state, select a different tool, or stall a resumed turn. Thanks for adding the SQLite and suspend/resume tests; the inline notes call out the cases they still need to cover.

Comment thread pkg/runtime/loop.go Resolved
Comment thread pkg/runtime/durable.go Resolved
Comment thread pkg/runtime/durable.go Resolved
Comment thread pkg/runtime/toolset_state.go Resolved
Comment thread pkg/runtime/toolset_state.go Resolved
Comment thread pkg/tools/builtin/deferred/deferred.go Resolved
Comment thread pkg/tools/builtin/deferred/deferred.go Resolved
@aheritier
aheritier marked this pull request as draft December 4, 2026 15:12
… checkpoints

- Scope Stateful snapshots and deferred activations per session via tools.StateScope
- Persist source identities in deferred snapshots and keep delayed original sources pending
- Avoid eager source listing in RestoreState and bound relevant deferred discovery
- Checkpoint toolset state at CallRecorded and persist final state before EndRun in settle
- Leave unrelated session attributes intact on pre-change v1 checkpoints
- Reconcile toolset state attributes at branch/fork points in session.BranchSession and ForkSession
- Fix golangci-lint embeddedstructfieldcheck and inamedparam issues

Signed-off-by: iemafzalhassan <iemafzalhassan@gmail.com>
Copilot AI balanced review requested due to automatic review settings December 4, 2026 15:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 insufficient remaining quota. Try requesting a review at a lower effort level.

@aheritier aheritier added status/needs-signed-commits Some commits in the PR are signed with a valid SSH/GPG key and removed status/needs-signed-commits Some commits in the PR are signed with a valid SSH/GPG key labels Dec 4, 2026
@iemafzalhassan

Copy link
Copy Markdown
Author

@aheritier Thanks for catching those. I pushed 259035b to address all seven inline items and the two lint failures. Toolset state is now scoped per session via tools.StateScope, saved at CallRecorded and in endRun, reconciled across BranchSession and ForkSession, pinned to source identities, and probed lazily under a timeout while leaving unrelated v1 checkpoint attributes intact.

I also added the requested regression tests in toolset_state_test.go and deferred_test.go, and verified locally with golangci-lint and go test -race. Could you kick off CI again when you get a chance?

@iemafzalhassan
iemafzalhassan marked this pull request as ready for review December 4, 2026 18:25
@iemafzalhassan
iemafzalhassan marked this pull request as draft December 4, 2026 18:27
@iemafzalhassan
iemafzalhassan marked this pull request as ready for review December 4, 2026 18:30

This branch has not been deployed

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

Labels

area/agent For work that has to do with the general agent loop/agentic features of the app area/runtime Runtime engine, agent loop execution, tool dispatch, loop detection area/tools For features/issues/fixes related to the usage of built-in and MCP tools kind/fix PR fixes a bug (maps to fix:). Use on PRs only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tools activated with add_tool are lost when the session is resumed

3 participants