Skip to content

feat(plan): add a plan-mode scratchpad and restrict writes to it - #1577

Merged
edenreich merged 4 commits into
mainfrom
fix/issue-1576
Oct 20, 2026
Merged

edenreich merged 4 commits into
mainfrom
fix/issue-1576

Conversation

@inference-gateway-maintainer

@inference-gateway-maintainer inference-gateway-maintainer Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #1576
Docs follow-up: inference-gateway/docs#1020

Summary

Plan mode gains a scratchpad: the per-project runtime directory now includes a scratchpad/ subdir, and plan mode is the one mode allowed to write there. Write, Edit, MultiEdit, Delete and Bash stay advertised to the model but execute in plan mode under a single chokepoint in sandbox.ValidateWrite, which reads the agent mode from the tool-loop context and allows a write only when the path sits inside the scratchpad, before grants and the configured allowed list. The working tree stays read-only until the plan is approved, and the tool definition JSON sent to the provider is unchanged.

Changes

  • config: new ProjectScratchpadDir() -> <ProjectTmpDir>/scratchpad (~/.infer/projects/<slug>/tmp/scratchpad), already a writable runtime carve-out, outside git, wiped by /reset.
  • sandbox: ValidateWrite(ctx, cfg, path) gates on agentdomain.AgentModeFromContext; in plan mode the gate runs before Granted and the allowed entries (a prior always-grant cannot widen plan mode), is re-applied to the symlink-resolved target (traversal and link escapes rejected), and denied entries plus the policy/custom-tool pins still win inside the scratchpad.
  • sandbox: plan rejections are plain errors naming the scratchpad path; see the deviation note below.
  • manifests: modes: [standard, auto, auto-with-judge, plan] on Write, Edit, MultiEdit, Delete and Bash - only the execution gate changes; readonly mode and the other modes keep their behaviour.
  • Bash: plan-mode commands run with their working directory pinned to the scratchpad (created lazily on the first command); the tools.bash.mode.plan.allow bucket stays empty by default over the shared baseline. The allow-listed path check carries the agent mode too: the baseline auto-approves mkdir and ln -s, so in plan mode those follow the scratchpad gate (relative paths anchored to the scratchpad, absolute paths outside it fall through to approval) instead of creating directories in the working tree unprompted.
  • guidance: the built-in plan mode-change guidance names the scratchpad ({scratchpad} is substituted at render time from the live project), tells the model to copy files in rather than edit originals, run verification there, report what it verified under ## Verification, and keep the scratchpad out of ## Files to Modify.
  • tests: scratchpad gate cases in internal/sandbox (in-pad, outside, ../ traversal, symlink escape, granted-but-plan-mode, standard/readonly unchanged), plan-mode allow-list cases for mkdir/ln -s (in-pad relative and absolute allowed, project-absolute and ../ rejected), Bash work-dir test, plan-mode tool availability and approval-policy rows updated, golden tool definitions regenerated (per-mode lists only - the definition JSON is untouched).
  • docs: docs/plan-mode.md and docs/tools-reference.md gain the scratchpad documentation; the remaining "read-only" plan-mode wording in the mode-guard error, the AgentModePlan comment, docs/commands-reference.md, docs/configuration-reference.md and examples/tools/README.md is updated to match.

Deviations from the issue text

  • The issue asked for a DeniedError on plan-mode rejections. I used a plain error instead: a DeniedError makes the tool loop offer the user a sandbox grant approval that can never unlock plan mode (the gate runs before grants), which would look broken in the chat UI. Plain errors match the existing house pattern for non-grantable rejections (excluded for security, read-only library dirs). Tests assert the message names the scratchpad path, satisfying the acceptance criterion.
  • The issue kept the shell path check mode-unaware. It is mode-aware here, because the baseline allow-list auto-approves mkdir and ln -s and a plan-mode mkdir <absolute project path> would otherwise have run with no approval at all. Commands whose arguments the check cannot predict still fall through to approval as in every other mode; the file tools remain the hard boundary.

Follow-ups (not in this PR)

  • Baseline git status/git diff/git log run in the scratchpad cwd in plan mode and fail with "not a git repository". Not a regression (Bash was fully disabled in plan mode before), worth a guidance line or a -C allowance.
  • Scratchpad writes prompt for approval under the standard rules. Auto-approving writes that resolve inside the scratchpad in plan mode would remove the friction without widening the boundary.

Plan mode is executable again for the file tools and Bash: sandbox.ValidateWrite reads the agent mode from the tool-loop context and in plan mode allows writes only inside the per-project scratchpad before grants and the configured allowed list, rejecting traversal and symlink escapes like any other path outside it. Write, Edit, MultiEdit, Delete and Bash advertise plan in their modes, Bash pins its working directory to the scratchpad which is created lazily, and the built-in plan guidance names the scratchpad and tells the model to verify there and report it in the Verification section of the plan. The tool definitions sent to the provider are unchanged.
The plan-mode guide gets a scratchpad section covering its location, what may write there, the bash allow-list and the traversal and symlink rules, and the tools reference points the write tools and Bash at it, including the scratchpad-pinned working directory in plan mode.
@inference-gateway-maintainer
inference-gateway-maintainer Bot marked this pull request as ready for review October 20, 2026 11:51
@inference-gateway-maintainer
inference-gateway-maintainer Bot requested a review from a team as a code owner October 20, 2026 11:51
@edenreich

Copy link
Copy Markdown
Contributor

@infer please follow the YAML instructions from the AGENTS.md - fix the styling.

@inference-gateway-maintainer

inference-gateway-maintainer Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Model: ollama_cloud/glm-5.3-flash · Action: v0.57.0 · CLI: v0.231.1 · View Job

Todos

  • Convert modes entries to block style in the 5 tool manifests
  • Verify no other flow-style YAML in the PR diff
  • Run repo checks (tests + precommit)
  • Commit and push

Pull Request

#1577

✅ Infer Result: Success

All checks passed, the worktree is clean, and the branch is in sync with origin.

Done - the YAML styling is fixed per the AGENTS.md rule (block style only, never flow style).

What changed

The plan-mode modes: entries added by this PR in the five tool manifests used flow style (modes: [standard, auto, auto-with-judge, plan]). They are now block sequences, matching the style the manifests already use for required: and enum::

modes:
  - standard
  - auto
  - auto-with-judge
  - plan

Applied to: internal/tools/Bash.yaml, Delete.yaml, Edit.yaml, MultiEdit.yaml, Write.yaml. I verified against the full PR diff that these were the only YAML files touched - docs changes are prose with no YAML examples, and the golden tool_definitions.golden.json is JSON (its arrays are not governed by the YAML rule).

Validation

  • go test ./internal/tools/... ./internal/agent/loop/... - all pass, including the golden tool-definitions test, confirming block style parses to identical tool definitions (the JSON sent to providers is unchanged).
  • task precommit:run - clean (mod tidy, fmt, lint:imports, golangci-lint 0 issues, markdownlint).
  • Committed as 35cc1558 style(tools): use block-style YAML sequences for the manifest modes and pushed to fix/issue-1576. Worktree clean, branch in sync with origin.

Note: the pre-existing allow: [] snippet in the unchanged docs configuration example also uses flow style, but it predates this PR and is outside the comment's scope, so I left it alone. Risk: none - block and flow YAML are equivalent to the parser, which the untouched golden test confirms.

Model: ollama_cloud/glm-5.3-flash · Action: v0.57.0 · CLI: v0.231.1 · Exit Code: 0 · Duration: 4m 5s

Tokens: 505,988 in · 7,676 out · 513,664 total (14 requests)

Tool calls: 27 total · 100% success rate

Traces
Session: 1792498288-0562837e

session (headless, success)                                                                        3m57s
├── chat ollama_cloud/glm-5.3-flash                                                                 9.7s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                       9.7s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]     9.7s
│           ╰── POST [inference-gateway, server]                                                    9.7s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                 9.7s
├── execute_tool Bash call_836rothn                                                                  3ms
├── execute_tool Bash call_d6iz50qh                                                                  6ms
├── execute_tool Read call_2vy4al8m                                                                397µs
├── chat ollama_cloud/glm-5.3-flash                                                                 6.7s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                       6.4s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]     6.4s
│           ╰── POST [inference-gateway, server]                                                    6.4s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                 6.4s
├── execute_tool TodoWrite call_s96cnlyo                                                            70µs
├── chat ollama_cloud/glm-5.3-flash                                                                 1.5s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                       1.5s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]     1.5s
│           ╰── POST [inference-gateway, server]                                                    1.5s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                 1.5s
├── execute_tool Read call_sg2cr9pd                                                                422µs
├── execute_tool Read call_t4nki1qo                                                                466µs
├── chat ollama_cloud/glm-5.3-flash                                                                 4.5s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                       4.5s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]     4.4s
│           ╰── POST [inference-gateway, server]                                                    4.4s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                 4.4s
├── execute_tool Read call_917hyeu5                                                                740µs
├── execute_tool Read call_rlvrgeuy                                                                693µs
├── execute_tool Read call_ruy9mzc0                                                                554µs
├── chat ollama_cloud/glm-5.3-flash                                                                 5.9s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                       5.9s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]     5.9s
│           ╰── POST [inference-gateway, server]                                                    5.9s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                 5.9s
├── execute_tool Edit call_bvzurkm4                                                                783µs
├── execute_tool Edit call_eotiwsnt                                                                788µs
├── execute_tool Edit call_thfh2ee3                                                                745µs
├── execute_tool Edit call_fj152e95                                                                805µs
├── execute_tool Edit call_810lmo1r                                                                754µs
├── execute_tool Bash call_nfah5ne5                                                                  4ms
├── chat ollama_cloud/glm-5.3-flash                                                                 6.3s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                       6.3s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]     6.3s
│           ╰── POST [inference-gateway, server]                                                    6.3s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                 6.3s
├── execute_tool TodoWrite call_bvuivk4m                                                            65µs
├── execute_tool Bash call_514xmolb                                                                  7ms
├── chat ollama_cloud/glm-5.3-flash                                                                 6.3s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                       6.3s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]     6.3s
│           ╰── POST [inference-gateway, server]                                                    6.3s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                 6.3s
├── execute_tool TodoWrite call_k0uo4po3                                                            67µs
├── execute_tool Bash call_wxuhblso                                                                1m15s
├── chat ollama_cloud/glm-5.3-flash                                                                 2.1s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                       2.1s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]     2.1s
│           ╰── POST /proxy/{provider}/{path...} [inference-gateway, server]                        2.1s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                 2.1s
├── execute_tool Memory call_jirq3ek0                                                              131µs
├── execute_tool Bash call_ihhr4x97                                                                 12ms
├── chat ollama_cloud/glm-5.3-flash                                                                 1.1s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                       1.1s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]     1.1s
│           ╰── POST /proxy/{provider}/{path...} [inference-gateway, server]                        1.1s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                 1.1s
├── execute_tool Bash call_s0dzgj9m                                                                1m40s
├── chat ollama_cloud/glm-5.3-flash                                                                 1.7s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                       1.7s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]     1.7s
│           ╰── POST [inference-gateway, server]                                                    1.7s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                 1.7s
├── execute_tool Bash call_ad4r1kb2                                                                 4.4s
├── chat ollama_cloud/glm-5.3-flash                                                                884ms
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                      872ms
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]    865ms
│           ╰── POST [inference-gateway, server]                                                   865ms
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                865ms
├── execute_tool Bash call_etf9g5b6                                                                 1.8s
├── chat ollama_cloud/glm-5.3-flash                                                                 1.0s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                       1.0s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]     1.0s
│           ╰── POST [inference-gateway, server]                                                    1.0s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                 1.0s
├── execute_tool Bash call_2y1y1llz                                                                  8ms
├── execute_tool Bash call_s8im631s                                                                  4ms
├── chat ollama_cloud/glm-5.3-flash                                                                 2.2s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                       2.2s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]     2.2s
│           ╰── POST [inference-gateway, server]                                                    2.2s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                 2.2s
├── execute_tool TodoWrite call_h5ejyjic                                                            63µs
╰── chat ollama_cloud/glm-5.3-flash                                                                 2.7s
    ╰── POST /v1/chat/completions [inference-gateway, server]                                       2.7s
        ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]     2.7s
            ╰── POST [inference-gateway, server]                                                    2.7s
                ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                 2.7s
Stats
Tool Calls

╭───────────┬───────┬───────┬─────────╮
│ Tool      │ Calls │ Fail% │ Avg     │
├───────────┼───────┼───────┼─────────┤
│ Bash      │ 11    │ 0%    │ 16491ms │
│ Read      │ 6     │ 0%    │ 477µs   │
│ Edit      │ 5     │ 0%    │ 780µs   │
│ TodoWrite │ 4     │ 0%    │ 61µs    │
│ Memory    │ 1     │ 0%    │ 131µs   │
╰───────────┴───────┴───────┴─────────╯

Token Usage

╭────────────────────────────┬────────┬────────┬────────────┬────────┬──────╮
│ Model                      │ Prompt │ Cached │ Completion │ Total  │ Cost │
├────────────────────────────┼────────┼────────┼────────────┼────────┼──────┤
│ ollama_cloud/glm-5.3-flash │ 505988 │ 309120 │ 7676       │ 513664 │ -    │
╰────────────────────────────┴────────┴────────┴────────────┴────────┴──────╯

Sessions

╭───────────┬──────────┬──────────╮
│ Execution │ Mode     │ Sessions │
├───────────┼──────────┼──────────┤
│ headless  │ headless │ 1        │
╰───────────┴──────────┴──────────╯

Triggered by edenreich · Infer Action · OSS

@edenreich edenreich moved this from Todo to QA in 2026 Oct 20, 2026
…ll writes

The baseline bash allow-list auto-approves mkdir and ln -s in every mode, and
the shell path check validated writes without an agent mode, so a plan-mode
mkdir with an absolute path could create directories in the working tree with
no approval. The check now carries the mode, anchors relative paths to the
scratchpad where plan-mode commands run, and rejects creating paths outside it.

Also drop the remaining read-only wording for plan mode in the mode-guard
error, the AgentModePlan comment and the docs, and inline the single-use
scratchpad directory constant.
@edenreich
edenreich merged commit e01b4cd into main Oct 20, 2026
1 check passed
@edenreich
edenreich deleted the fix/issue-1576 branch October 20, 2026 13:53
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.233.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 Oct 20, 2026
@edenreich edenreich moved this from QA to Done in 2026 Oct 20, 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

Status: Done

Development

Successfully merging this pull request may close these issues.

[FEATURE] Add a scratchpad in the project dir and restrict write operations to this directory when in plan mode

1 participant