Skip to content

feat(plan): compact context and start a fresh session when a plan is approved - #705

Merged
edenreich merged 3 commits into
mainfrom
feat/677-compact-on-plan-approval
Jul 1, 2026
Merged

edenreich merged 3 commits into
mainfrom
feat/677-compact-on-plan-approval

Conversation

@edenreich

Copy link
Copy Markdown
Contributor

Summary

Closes #677.

When a plan is approved in Plan Mode, the agent now compacts the (usually exploration-heavy) planning conversation into a summary, starts a fresh session, and re-reads the approved plan from disk to execute — so execution begins with an optimized, much smaller context window.

Planning is read-heavy (Read/Grep/Tree all over the codebase), and none of that exploration is needed once the plan is accepted. This reuses the existing /compact machinery at plan-approval time.

Behaviour

compact.enabled Auto-compact at auto_at (80%) Compact on plan approval
true (default) ✅ ✅
false ❌ ✅ (always)

Compaction on plan approval runs regardless of compact.enabled — that flag only governs the automatic mid-conversation compaction and the headless session rollover. It also fails open: if summarization errors or times out, execution still proceeds on the un-compacted conversation.

Note: the approved plan is never lost — it lives on disk under <configDir>/plans/… and is captured in the summary; the continuation prompt points the agent back at that file to execute step by step.

Changes

  • Plan-path plumbing — the saved plan path from the RequestPlanApproval tool result flows through PlanApprovalRequestedEvent → PlanApprovalUIState, so the post-approval continuation prompt can reference it (internal/agent/agent.go, internal/domain/{events,state,interfaces}.go, internal/services/state_manager.go, internal/services/approvalcoord/coordinator.go).
  • Orchestration — HandlePlanApprovalResponseEvent captures the path and, on accept, runs a new compactThenExecutePlanCmd: async optimize under a 70s timeout → reseed a new session → add a plan-path-aware continue message → resume the agent (internal/handlers/chat_handler.go, new internal/handlers/chat_compaction.go).
  • Shared helpers — extracted optimizeWithTimeout / reseedConversationWithMessages / nonHiddenMessages; the /compact shortcut now reuses them (behaviour unchanged).
  • Optimizer always constructed — internal/container/container.go builds the ConversationOptimizer unconditionally so approval compaction works even with compact.enabled: false (force=true bypasses the enabled check).
  • Docs — new "Context compaction on approval" section in docs/plan-mode.md; fixed the stale compact.enabled default (it is true, not false) in docs/configuration-reference.md.

Testing

  • task build, full go test ./..., golangci-lint (0 issues), markdownlint — all green.
  • New unit tests: extractPlanPath (edge cases), checkToolResultsStatus path extraction, planExecutionContinuePrompt, and compactPlanningContext (reseed / no-reduce / absent optimizer / no model), plus plan-path forwarding in the coordinator test.

Manual check to run before merge

Interactive TUI + live gateway (not runnable in CI): Shift+Tab into Plan Mode, ask for a multi-file change so the model reads several files, Accept the plan, and confirm the history collapses to a --- Context Summary ---, the agent re-Reads the plan file, and /context shows a smaller token count. Repeat with Accept & Auto-Approve, and with compact.enabled: false.

Docs follow-up

A matching [DOCS] issue should be filed for inference-gateway/docs (Plan Mode / compaction pages) per the docs follow-up rule.

…approved

Approving a plan in Plan Mode now summarizes the (usually exploration-heavy)
planning conversation, starts a fresh session seeded with that summary, and
points the agent back at the saved plan file to execute - so execution begins
with an optimized, much smaller context window.

- Thread the saved plan file path from the RequestPlanApproval tool result
  through PlanApprovalRequestedEvent and the plan-approval UI state so the
  post-approval continuation prompt can reference it.
- Add compactThenExecutePlanCmd on the chat handler: force-optimize the
  conversation under a timeout, reseed a new session, add a plan-path-aware
  continue message, then resume. Fails open when compaction errors or times out.
- Extract shared compaction helpers (optimize-with-timeout, reseed) reused by
  the /compact shortcut.
- Construct the ConversationOptimizer unconditionally so compaction on plan
  approval runs regardless of compact.enabled; that flag now only gates the
  automatic mid-conversation compaction and the headless session rollover.
- Document the behaviour in plan-mode.md and fix the stale compact.enabled
  default (true, not false) in configuration-reference.md.

Closes #677
@edenreich
edenreich requested a review from a team as a code owner July 1, 2026 10:20
Comment thread internal/container/container.go Resolved
Comment thread internal/handlers/chat_handler.go Resolved
Comment thread internal/handlers/chat_handler.go Resolved
@edenreich
edenreich merged commit 28ddba7 into main Jul 1, 2026
1 check passed
@edenreich
edenreich deleted the feat/677-compact-on-plan-approval branch July 1, 2026 11:27
edenreich added a commit that referenced this pull request Jul 2, 2026
…714)

## Summary

Accepting a plan dropped the agent into standard mode, forcing
per-action approval during execution — the friction reported in #710.
The default **Accept** now switches to **auto-accept mode**, so an
already-reviewed plan executes without re-approving every step.

## Behavior

The plan-approval prompt now offers:

| Option | Key | Result |
| --- | --- | --- |
| **Accept** (default) | `Enter` / `y` | auto-accept mode (no per-action
approval) |
| **Approve Each Step** | `s` | standard mode (approve every action) |
| **Reject** | `n` | ends the session (unchanged) |

The cautious per-step path is preserved as an explicit choice, so no
capability is lost. The mode is set on the `StateManager` rather than
the conversation, so it survives the post-approval context compaction +
fresh session from #705.

> Note: the secondary keybinding changed from `a` to `s`.

## Changes

- `applyPlanDecision` (approval coordinator): `Accept` → `AutoAccept`;
new `AcceptStandard` → `Standard`.
- Renamed the enum `PlanApprovalAcceptAndAutoApprove` →
`PlanApprovalAcceptStandard`.
- UI: relabeled the third button to "Approve Each Step", updated the
plan-approval hint text, and rekeyed `a` → `s`.
- Updated the `RequestPlanApproval` tool result + prompt copy to
describe the new options.
- Updated the coordinator tests for the swapped semantics.

## Testing

- `task test` — full suite passes
- `task lint` — 0 issues (golangci-lint + markdownlint)

Closes #710
inference-gateway-releaser Bot added a commit that referenced this pull request Jul 2, 2026
## [0.127.0](v0.126.0...v0.127.0) (2026-07-02)

### 🚀 Features

* **plan:** compact context and start a fresh session when a plan is approved ([#705](#705)) ([28ddba7](28ddba7))
* **memory:** configurable git sync backend ([#707](#707)) ([6a68c3d](6a68c3d)), closes [#670](#670) [#670](#670)
* **history:** each subagent gets its own history file ([#711](#711)) ([16c8fa1](16c8fa1)), closes [#699](#699)

### 🐛 Bug Fixes

* **plan:** accepting a plan switches to auto-accept mode by default ([#714](#714)) ([7b0befc](7b0befc)), closes [#710](#710)
* **tools:** guard concurrent gitignore and screenshot caches ([#713](#713)) ([0ff4276](0ff4276)), closes [#712](#712)
* **tools:** guard Registry.tools with RWMutex against concurrent MCP registration ([#709](#709)) ([8a70871](8a70871)), closes [#708](#708)
* **ui:** update dim color from [#565f89](https://github.com/inference-gateway/cli/issues/565f89) to [#7a7f9a](https://github.com/inference-gateway/cli/issues/7a7f9a) for improved contrast ([#706](#706)) ([b2ee67e](b2ee67e))

### 🔧 Build System

* **deps:** bump github.com/go-telegram/bot from 1.21.0 to 1.22.0 in the gomod group ([#703](#703)) ([3de5136](3de5136))
* **deps:** bump the github-actions group with 5 updates ([#704](#704)) ([80221f7](80221f7))
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.127.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@inference-gateway-releaser inference-gateway-releaser Bot added the released Shipped in a release label Jul 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] When a plan is approved the session should get compacted and start on a new session

1 participant