diff --git a/docs/adr/0348-persistent-python-repl-and-unharnessed-mode.md b/docs/adr/0348-persistent-python-repl-and-unharnessed-mode.md new file mode 100644 index 00000000..deba417f --- /dev/null +++ b/docs/adr/0348-persistent-python-repl-and-unharnessed-mode.md @@ -0,0 +1,21 @@ +# 0348. Persistent Stateful Python REPL (`python_repl`) and Opt-In `--unharnessed` Mode + +Status: accepted 2026-11-16 + +## Context + +One-shot `shell("python3 ...")` calls lose in-memory variables, parsed datasets, compiled helpers, and working directory state across turns, forcing models to re-read files and serialize intermediate results back to disk on every step. Meanwhile, `rlm` (ADR 0022) intentionally provides a constrained call-only script surface (`read_file`/`codedb`/`bash`/`subagent`) rather than full Python execution. At the same time, keeping the full native editing/search catalog by default preserves strict path/edit safety and minimum reliability on general software engineering tasks. + +## Decision + +1. **Persistent per-owner Python 3 worker (`src/python_repl.zig`, `src/python_repl_worker.py`):** Each owner (`root` or subagent worker ID) lazily spawns one persistent Python 3 child speaking newline-delimited JSON over dedicated non-inheritable (`CLOEXEC`) duplicated file descriptors (`proto_in`/`proto_out`). Before executing code, stdio fds `1`/`2` and `sys.stdout`/`sys.stderr` redirect into an unlinked per-call temporary capture file so subprocess and C-level fd writes capture without corrupting JSONL framing. The last expression statement evaluates via `ast.Expression`, binds `_`, and returns `repr()`, while POSIX `SIGALRM` + Zig `Io.Select` watchdogs bound hangs without dropping prior REPL globals on recoverable errors or `SystemExit`. Workspace switches automatically update `os.chdir` and `sys.path[0]`. +2. **Dual exposure — folded native `python_repl` + opt-in `--unharnessed` 1-tool mode:** + - **`python_repl` in normal sessions:** Appended via `python_repl.catalogExtras()` and registered in `native_fold.folded`, keeping turn-1 tool schemas and the 64-cell `tool_catalog.json` cube unchanged until loaded (`load_tool_schemas`) or called. + - **`--unharnessed` / `/unharnessed` (`src/unharnessed_mode.zig`):** Opt-in mode (`CLI > GRAFF_UNHARNESSED > .harness/settings.json`, default `false`) exposing ONLY the single `python` tool and a compact single-tool system prompt, bypassing `orchestrator_turn` handoff, with live toggle parity across CLI, REPL (`/unharnessed`), `--json` (`set_unharnessed`), and ACP (`configOptions` `id: "unharnessed"`). +3. **Uniform safety & lifecycle invariants:** Both `python_repl` and `python` are blocked by `--no-local-tools`, refused in `plan_mode` and read-only subagents, checked against `.harness/settings.json` hard policies (`tool.use`), serialized in mixed tool batches (`agent_tool_batch.isSequential`), and reset on `/clear`, `/new`, ACP `session/load`, or `reset: true`, with workers reaped on exit in `session_globals.release`. + +## Consequences + +- Normal `graff` sessions keep the existing turn-1 schema budget while unlocking persistent cross-turn Python state on demand via `python_repl`. +- Sessions launched with `--unharnessed` (or toggled via `/unharnessed on` / ACP) expose a clean 1-tool `python` loop with full auth, transport, approval, and trace parity. +- `--no-local-tools` continues to take precedence over unharnessed settings so host execution cannot be bypassed in embedder deployments. diff --git a/docs/adr/README.md b/docs/adr/README.md index 797e9c05..02fed288 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -372,6 +372,7 @@ record only when you need the evidence or the edge cases. | [0345](0345-claude-dual-frontier-separate-quota-and-plan-ladders.md) | `anthropic` and `claude-agent` declare a 3-rung ladder with dual frontier (`opus-5-5` normal, `fable-5-2` at `xhigh`/`max`), `billing.quotaPoolFor` marks Fable as a separate plan pool across REPL (`plan*`), ACP (`separateQuota`), and quota recovery (`stepDownSeparateQuotaPool`), `chatgpt-new` mirrors Codex tiers, `xai` stays strictly `grok-4.7` or nothing, `liveLadderModel` adapts to rotated live catalogs, and cross-provider sub routing requires `billing.costFor == .plan`. | | [0346](0346-acp-accounts-plan-windows-and-live-rate-limits.md) | `plan_usage_live` caches live HTTP/WebSocket/`claude-agent` rate limits and drives `isModelBlocked` Fable stepdown; `plan_accounts` and `plan_accounts_probe` collect redacted identity and usage windows across `/usage` and ACP `graff/accounts`, `_codegraff/usage.planWindows` streams active limits, and `initialize` advertises `_meta["codegraff/accounts"]` plus all 7 terminal `authMethods`. | | [0347](0347-same-chat-plan-execute-baton-handoff.md) | Orchestrator Mode (`off` \| `auto` \| `on`, default `auto`) runs Phase 1 read-only planning on the root frontier seat (`exit_plan_mode`) and hands off in-place inside the same KV-cached conversation to a same-provider, same-wire executor 1 rung down (`enter_plan_mode` capped at 2 replans), integrated with `orchestration_policy` `.baton`, `.harness/settings.json`, REPL `/orchestrator`, and ACP `orchestrator` / `graff/models`. | +| [0348](0348-persistent-python-repl-and-unharnessed-mode.md) | Persistent per-owner Python 3 worker (`python_repl`, folded in `native_fold`) gives sessions cross-turn globals, `_` expression results, fd 1/2 capture, and timeout resilience without turn-1 schema overhead; opt-in `--unharnessed` / `/unharnessed` (`CLI > env > .harness/settings.json`, ACP/JSON parity) exposes only `python` with a single-tool prompt, while `--no-local-tools`, `plan_mode`, and hard policies block both. | ## When to write one diff --git a/evals/harness_behavior.jsonl b/evals/harness_behavior.jsonl index 085a298e..ef40706d 100644 --- a/evals/harness_behavior.jsonl +++ b/evals/harness_behavior.jsonl @@ -181,3 +181,5 @@ {"id": "hard-policy-restricts-yolo-write", "why": "Operator hard policy is an independent restrictive gate even when ordinary approvals are bypassed.", "workspace_seed": {".harness/settings.json": "{\"policies\": [{\"action\": \"tool.use\", \"resource\": \"write_file\", \"effect\": \"deny\"}]}", "note.txt": "unchanged"}, "prompt": "Try writing to note.txt, then explain the result.", "script": [{"tool": "write_file", "arguments": {"path": "note.txt", "content": "changed"}}, {"text": "The write was blocked by hard policy."}], "assert": [{"file_equals": {"path": "note.txt", "content": "unchanged"}}, {"request_contains": {"index": 1, "text": "hard policy denied"}}, {"final_text_contains": "blocked by hard policy"}]} {"id": "builtin-formatter-preserves-symlink-argv0", "why": "Built-in formatters resolved from PATH must keep the symlink leaf basename in argv[0] so multicall wrappers such as rustfmt -> rustup dispatch properly.", "env": {"PATH": "bin:/usr/bin:/bin", "GRAFF_NO_CODEDB_GUARD": "1"}, "workspace_seed": {".harness/settings.json": "{\"formatters\": true}", "pkg/Cargo.toml": "[package]\nname = \"pkg\"\nversion = \"0.1.0\"\n"}, "prompt": "Set up the multicall rustfmt shim and write pkg/src/lib.rs.", "script": [{"tool": "bash", "arguments": {"command": "mkdir -p bin pkg/src && printf '#!/bin/sh\\nif [ \"$(basename \"$0\")\" != \"rustfmt\" ]; then exit 2; fi\\nif [ \"$RUSTUP_AUTO_INSTALL\" != \"0\" ]; then exit 3; fi\\nfor last; do :; done\\ntr -s \" \" < \"$last\" > \"$last.tmp\" && mv \"$last.tmp\" \"$last\"\\n' > multicall && chmod +x multicall && ln -s ../multicall bin/rustfmt && echo shim-ready"}}, {"tool": "write_file", "arguments": {"path": "pkg/src/lib.rs", "content": "pub fn add() {}\n"}}, {"text": "Formatted through multicall symlink."}], "assert": [{"event": {"type": "tool_result", "name": "bash", "text": "shim-ready\n"}}, {"event": {"type": "tool_result", "name": "write_file", "is_error": false}}, {"request_contains": {"index": -1, "text": "formatter rustfmt: formatted"}}, {"file_equals": {"path": "pkg/src/lib.rs", "content": "pub fn add() {}\n"}}, {"final_text_contains": "Formatted through multicall symlink."}]} {"id": "orchestrator-mode-json-control-and-single-seat-fallback", "scripted_only": true, "why": "set_orchestrator persists to .harness/settings.json and emits an orchestrator ack; single-seat or custom routes keep executor execution inline without exposing planning baton tools.", "controls_before_prompt": [{"type": "set_orchestrator", "mode": "on"}], "prompt": "Reply with hello.", "script": [{"text": "hello from mock"}], "assert": [{"event": {"type": "orchestrator", "ok": true, "mode": "on", "supported": false}}, {"request_count": 1}, {"request_lacks": {"index": 0, "text": "exit_plan_mode"}}, {"file_equals": {"path": ".harness/settings.json", "content": "{\"orchestrator\":\"on\"}\n"}}, {"final_text_contains": "hello from mock"}]} +{"id": "python-repl-persistent-session-state", "scripted_only": true, "why": "ADR 0348: python_repl unfolds on demand via load_tool_schemas and preserves variables and trailing _ across calls.", "prompt": "Load python_repl and compute total into answer.txt across two calls.", "script": [{"tool": "load_tool_schemas", "arguments": {"tools": ["python_repl"]}}, {"tool": "python_repl", "arguments": {"code": "nums = [10, 20, 30]\nsum(nums)"}}, {"tool": "python_repl", "arguments": {"code": "open('answer.txt', 'w').write(f'total={sum(nums)},prev={_}')\n'saved'"}}, {"text": "Saved persistent computation to answer.txt."}], "assert": [{"event": {"type": "tool_call", "name": "load_tool_schemas"}}, {"event": {"type": "tool_result", "name": "python_repl", "is_error": false}}, {"request_lacks": {"index": 0, "text": "\"name\": \"python_repl\""}}, {"request_contains": {"index": 1, "text": "\"name\": \"python_repl\""}}, {"request_contains": {"index": -1, "text": "'saved'"}}, {"file_equals": {"path": "answer.txt", "content": "total=60,prev=60"}}, {"final_text_contains": "answer.txt"}]} +{"id": "unharnessed-mode-single-tool-and-json-control", "scripted_only": true, "why": "ADR 0348: set_unharnessed persists to .harness/settings.json, switches to single-tool python catalog without file/shell tools, and preserves persistent REPL state.", "controls_before_prompt": [{"type": "set_unharnessed", "on": true}], "prompt": "Compute 7 * 9 across two persistent python calls and write out.txt.", "script": [{"tool": "python", "arguments": {"code": "val = 7 * 9\nval"}}, {"tool": "python", "arguments": {"code": "open('out.txt', 'w').write(f'{val}:{_}')\n'done'"}}, {"text": "Completed in single-tool python mode."}], "assert": [{"event": {"type": "unharnessed", "ok": true, "on": true}}, {"request_contains": {"index": 0, "text": "\"name\": \"python\""}}, {"request_lacks": {"index": 0, "text": "\"name\": \"read_file\""}}, {"request_lacks": {"index": 0, "text": "\"name\": \"edit_file\""}}, {"request_lacks": {"index": 0, "text": "\"name\": \"shell\""}}, {"file_equals": {"path": ".harness/settings.json", "content": "{\"unharnessed\":true}\n"}}, {"file_equals": {"path": "out.txt", "content": "63:63"}}, {"final_text_contains": "Completed in single-tool python mode."}]} diff --git a/examples/edge-worker/src/remote.ts b/examples/edge-worker/src/remote.ts index f5964505..c1f64a58 100644 --- a/examples/edge-worker/src/remote.ts +++ b/examples/edge-worker/src/remote.ts @@ -10,7 +10,7 @@ export const HARNESS_VERSION = "0.13"; export type ModelName = "MiniMax-M2.5" | "MiniMax-M2.7" | "MiniMax-M3" | "accounts/fireworks/models/deepseek-v4-flash" | "accounts/fireworks/models/deepseek-v4-pro" | "accounts/fireworks/models/glm-5p2" | "accounts/fireworks/models/gpt-oss-120b" | "accounts/fireworks/models/kimi-k2p6" | "accounts/fireworks/models/kimi-k2p7-code" | "accounts/fireworks/models/minimax-m3" | "accounts/fireworks/models/qwen3p7-plus" | "alibaba/qwen3.8-27b" | "anthropic/claude-sonnet-4.6" | "claude-fable-5" | "claude-fable-5-1" | "claude-fable-5-2" | "claude-haiku-5-5" | "claude-opus-4-5" | "claude-opus-4-6" | "claude-opus-4-7" | "claude-opus-4-8" | "claude-opus-4.8" | "claude-opus-5" | "claude-opus-5-5" | "claude-sonnet-4-5" | "claude-sonnet-4-6" | "claude-sonnet-4.6" | "claude-sonnet-5" | "claude-sonnet-5-5" | "deepseek-chat" | "deepseek-reasoner" | "deepseek-v4-flash" | "deepseek-v4-pro" | "flint" | "fugu" | "fugu-ultra" | "fugu-ultra-20260615" | "gemini-3-flash-preview" | "gemini-3.1-flash-lite" | "gemini-3.1-pro-preview" | "gemini-3.5-flash" | "gemini-3.5-flash-lite" | "gemini-3.6-flash" | "gemini-3.7-flash" | "gemini-3.8-flash" | "gemini-4-argon" | "gemma-4-26b-a4b-it" | "gemma-4-31b" | "gemma-4-31b-it" | "glm-4.5" | "glm-4.7" | "glm-5" | "glm-5-turbo" | "glm-5.2" | "glm-5.3" | "glm-5.3-flash" | "glm-5v-turbo" | "gpt-5-codex" | "gpt-5.2" | "gpt-5.3-codex-spark" | "gpt-5.4" | "gpt-5.4-mini" | "gpt-5.4-pro" | "gpt-5.5" | "gpt-5.6" | "gpt-5.6-luna" | "gpt-5.6-sol" | "gpt-5.6-terra" | "gpt-6-astra" | "gpt-6-luna" | "gpt-6-sol" | "gpt-6.1-sol" | "gpt-oss-120b" | "grok-4.3" | "grok-4.6" | "grok-4.7" | "grok-build" | "k3" | "k3-256k" | "k3.1-pro" | "k3.1-pro-highspeed" | "kilo-auto/small" | "kimi-for-coding" | "kimi-for-coding-highspeed" | "kimi-k2.6" | "kimi-k2.7-code" | "kimi-k3" | "kimi-k3.1-pro" | "kimi-k3.1-pro-highspeed" | "kimi-latest" | "lmstudio" | "mimo-v2-flash" | "mimo-v2.5" | "mimo-v2.5-pro" | "mimo-v2.5-pro-ultraspeed" | "mimo-v2.6-flash" | "mimo-v2.6-pro" | "mimo-v2.6-pro-ultraspeed" | "minimax-m3" | "mistral-medium-latest" | "mlx-community/Qwen3.6-27B-OptiQ-4bit" | "muse-spark-1.2" | "muse-spark-1.2-contributor" | "omlx" | "openai/gpt-oss-120b" | (string & {}); -export type ToolName = "shell" | "read_file" | "edit_file" | "write_file" | "webfetch" | "skill" | "codedb" | "read_tool_result" | "render_html" | "todo_write" | "todo_read" | "eval" | "note_constraint" | "ask_user" | "attempt_completion" | "load_tool_schemas" | "mcp_search_tools" | "mcp_select_tool" | "clock_sleep" | "subagent" | "workflow" | "agent_output" | "agent_message" | "subagent_resume" | "learn_candidate" | "peer_message" | "workspace" | "yield_turn" | "imagegen" | "lsp" | "rlm"; +export type ToolName = "shell" | "read_file" | "edit_file" | "write_file" | "webfetch" | "skill" | "codedb" | "read_tool_result" | "render_html" | "todo_write" | "todo_read" | "eval" | "note_constraint" | "ask_user" | "attempt_completion" | "load_tool_schemas" | "mcp_search_tools" | "mcp_select_tool" | "clock_sleep" | "subagent" | "workflow" | "agent_output" | "agent_message" | "subagent_resume" | "learn_candidate" | "peer_message" | "workspace" | "yield_turn" | "imagegen" | "lsp" | "python_repl" | "rlm"; export type ProviderId = "anthropic" | "codegraff" | "deepseek" | "openai" | "google" | "minimax" | "xiaomi" | "kilo" | "groq" | "cerebras" | "mistral" | "kimi" | "moonshot" | "xai" | "zai" | "meta" | "vercel" | "openrouter" | "fugu" | "fireworks" | "mlx" | "lmstudio" | "omlx" | "chatgpt-new" | "codex" | "claude-agent"; /** Events streamed by the bridge (same `--json` contract as the stdio SDK). @@ -529,7 +529,7 @@ export async function* runAgentRemote(opts: RunAgentRemoteOptions): AsyncGenerat } export const MODELS: ModelName[] = ["MiniMax-M2.5", "MiniMax-M2.7", "MiniMax-M3", "accounts/fireworks/models/deepseek-v4-flash", "accounts/fireworks/models/deepseek-v4-pro", "accounts/fireworks/models/glm-5p2", "accounts/fireworks/models/gpt-oss-120b", "accounts/fireworks/models/kimi-k2p6", "accounts/fireworks/models/kimi-k2p7-code", "accounts/fireworks/models/minimax-m3", "accounts/fireworks/models/qwen3p7-plus", "alibaba/qwen3.8-27b", "anthropic/claude-sonnet-4.6", "claude-fable-5", "claude-fable-5-1", "claude-fable-5-2", "claude-haiku-5-5", "claude-opus-4-5", "claude-opus-4-6", "claude-opus-4-7", "claude-opus-4-8", "claude-opus-4.8", "claude-opus-5", "claude-opus-5-5", "claude-sonnet-4-5", "claude-sonnet-4-6", "claude-sonnet-4.6", "claude-sonnet-5", "claude-sonnet-5-5", "deepseek-chat", "deepseek-reasoner", "deepseek-v4-flash", "deepseek-v4-pro", "flint", "fugu", "fugu-ultra", "fugu-ultra-20260615", "gemini-3-flash-preview", "gemini-3.1-flash-lite", "gemini-3.1-pro-preview", "gemini-3.5-flash", "gemini-3.5-flash-lite", "gemini-3.6-flash", "gemini-3.7-flash", "gemini-3.8-flash", "gemini-4-argon", "gemma-4-26b-a4b-it", "gemma-4-31b", "gemma-4-31b-it", "glm-4.5", "glm-4.7", "glm-5", "glm-5-turbo", "glm-5.2", "glm-5.3", "glm-5.3-flash", "glm-5v-turbo", "gpt-5-codex", "gpt-5.2", "gpt-5.3-codex-spark", "gpt-5.4", "gpt-5.4-mini", "gpt-5.4-pro", "gpt-5.5", "gpt-5.6", "gpt-5.6-luna", "gpt-5.6-sol", "gpt-5.6-terra", "gpt-6-astra", "gpt-6-luna", "gpt-6-sol", "gpt-6.1-sol", "gpt-oss-120b", "grok-4.3", "grok-4.6", "grok-4.7", "grok-build", "k3", "k3-256k", "k3.1-pro", "k3.1-pro-highspeed", "kilo-auto/small", "kimi-for-coding", "kimi-for-coding-highspeed", "kimi-k2.6", "kimi-k2.7-code", "kimi-k3", "kimi-k3.1-pro", "kimi-k3.1-pro-highspeed", "kimi-latest", "lmstudio", "mimo-v2-flash", "mimo-v2.5", "mimo-v2.5-pro", "mimo-v2.5-pro-ultraspeed", "mimo-v2.6-flash", "mimo-v2.6-pro", "mimo-v2.6-pro-ultraspeed", "minimax-m3", "mistral-medium-latest", "mlx-community/Qwen3.6-27B-OptiQ-4bit", "muse-spark-1.2", "muse-spark-1.2-contributor", "omlx", "openai/gpt-oss-120b"]; -export const TOOLS: ToolName[] = ["shell", "read_file", "edit_file", "write_file", "webfetch", "skill", "codedb", "read_tool_result", "render_html", "todo_write", "todo_read", "eval", "note_constraint", "ask_user", "attempt_completion", "load_tool_schemas", "mcp_search_tools", "mcp_select_tool", "clock_sleep", "subagent", "workflow", "agent_output", "agent_message", "subagent_resume", "learn_candidate", "peer_message", "workspace", "yield_turn", "imagegen", "lsp", "rlm"]; +export const TOOLS: ToolName[] = ["shell", "read_file", "edit_file", "write_file", "webfetch", "skill", "codedb", "read_tool_result", "render_html", "todo_write", "todo_read", "eval", "note_constraint", "ask_user", "attempt_completion", "load_tool_schemas", "mcp_search_tools", "mcp_select_tool", "clock_sleep", "subagent", "workflow", "agent_output", "agent_message", "subagent_resume", "learn_candidate", "peer_message", "workspace", "yield_turn", "imagegen", "lsp", "python_repl", "rlm"]; // ── Serverless / In-Memory Agent Runtime (Edge & Worker safe) ───────────── diff --git a/scripts/test-acp-effort-config.py b/scripts/test-acp-effort-config.py index 06a618ff..38a7d6bf 100644 --- a/scripts/test-acp-effort-config.py +++ b/scripts/test-acp-effort-config.py @@ -15,7 +15,7 @@ def option(response): options = response["result"]["configOptions"] - assert len(options) == 1, options + assert [o["id"] for o in options] == ["thought_level", "unharnessed"], options selected = options[0] assert selected["id"] == selected["category"] == "thought_level", selected assert selected["type"] == "select", selected diff --git a/scripts/test-acp-http2.py b/scripts/test-acp-http2.py index 86894da4..2ab390cc 100644 --- a/scripts/test-acp-http2.py +++ b/scripts/test-acp-http2.py @@ -173,12 +173,12 @@ def expected_text(number): created, _ = call('session/new', {'cwd': str(case), 'mcpServers': []}, 2) sid = created['result']['sessionId'] initial = created['result']['configOptions'] - assert len(initial) == 1 and initial[0]['category'] == 'thought_level', initial + assert [o['id'] for o in initial] == ['thought_level', 'unharnessed'] and initial[0]['category'] == 'thought_level', initial assert initial[0]['currentValue'] == 'medium', initial selected, _ = call('session/set_config_option', { 'sessionId': sid, 'configId': 'thought_level', 'value': 'high'}, 8) options = selected['result']['configOptions'] - assert len(options) == 1 and options[0]['currentValue'] == 'high', selected + assert [o['id'] for o in options] == ['thought_level', 'unharnessed'] and options[0]['currentValue'] == 'high', selected assert not [r for r in (json.loads(line) for line in log.read_text().splitlines()) if r['event'] == 'request'], 'Effort selection made a network request' for number in range(1, 3 if success else 2): diff --git a/scripts/test-acp-session-load.py b/scripts/test-acp-session-load.py index 1dc6d89b..d5897809 100644 --- a/scripts/test-acp-session-load.py +++ b/scripts/test-acp-session-load.py @@ -351,7 +351,7 @@ def run(binary): assert selected["model"] == "alibaba/qwen3.8-27b", selected created_config = created["configOptions"] loaded_config = loaded["result"]["configOptions"] - assert len(created_config) == len(loaded_config) == 1, (created, loaded) + assert [o["id"] for o in created_config] == [o["id"] for o in loaded_config] == ["thought_level", "unharnessed"], (created, loaded) assert created_config[0]["category"] == loaded_config[0]["category"] == "thought_level" assert created_config[0]["currentValue"] == "high", created_config assert loaded_config[0]["currentValue"] == "low", loaded_config diff --git a/sdk/py/harness_sdk.py b/sdk/py/harness_sdk.py index 0324fcc6..5f3fb661 100644 --- a/sdk/py/harness_sdk.py +++ b/sdk/py/harness_sdk.py @@ -14,7 +14,7 @@ from typing import Iterator, List, Literal, Optional, TypedDict, Union MODELS = ["MiniMax-M2.5", "MiniMax-M2.7", "MiniMax-M3", "accounts/fireworks/models/deepseek-v4-flash", "accounts/fireworks/models/deepseek-v4-pro", "accounts/fireworks/models/glm-5p2", "accounts/fireworks/models/gpt-oss-120b", "accounts/fireworks/models/kimi-k2p6", "accounts/fireworks/models/kimi-k2p7-code", "accounts/fireworks/models/minimax-m3", "accounts/fireworks/models/qwen3p7-plus", "alibaba/qwen3.8-27b", "anthropic/claude-sonnet-4.6", "claude-fable-5", "claude-fable-5-1", "claude-fable-5-2", "claude-haiku-5-5", "claude-opus-4-5", "claude-opus-4-6", "claude-opus-4-7", "claude-opus-4-8", "claude-opus-4.8", "claude-opus-5", "claude-opus-5-5", "claude-sonnet-4-5", "claude-sonnet-4-6", "claude-sonnet-4.6", "claude-sonnet-5", "claude-sonnet-5-5", "deepseek-chat", "deepseek-reasoner", "deepseek-v4-flash", "deepseek-v4-pro", "flint", "fugu", "fugu-ultra", "fugu-ultra-20260615", "gemini-3-flash-preview", "gemini-3.1-flash-lite", "gemini-3.1-pro-preview", "gemini-3.5-flash", "gemini-3.5-flash-lite", "gemini-3.6-flash", "gemini-3.7-flash", "gemini-3.8-flash", "gemini-4-argon", "gemma-4-26b-a4b-it", "gemma-4-31b", "gemma-4-31b-it", "glm-4.5", "glm-4.7", "glm-5", "glm-5-turbo", "glm-5.2", "glm-5.3", "glm-5.3-flash", "glm-5v-turbo", "gpt-5-codex", "gpt-5.2", "gpt-5.3-codex-spark", "gpt-5.4", "gpt-5.4-mini", "gpt-5.4-pro", "gpt-5.5", "gpt-5.6", "gpt-5.6-luna", "gpt-5.6-sol", "gpt-5.6-terra", "gpt-6-astra", "gpt-6-luna", "gpt-6-sol", "gpt-6.1-sol", "gpt-oss-120b", "grok-4.3", "grok-4.6", "grok-4.7", "grok-build", "k3", "k3-256k", "k3.1-pro", "k3.1-pro-highspeed", "kilo-auto/small", "kimi-for-coding", "kimi-for-coding-highspeed", "kimi-k2.6", "kimi-k2.7-code", "kimi-k3", "kimi-k3.1-pro", "kimi-k3.1-pro-highspeed", "kimi-latest", "lmstudio", "mimo-v2-flash", "mimo-v2.5", "mimo-v2.5-pro", "mimo-v2.5-pro-ultraspeed", "mimo-v2.6-flash", "mimo-v2.6-pro", "mimo-v2.6-pro-ultraspeed", "minimax-m3", "mistral-medium-latest", "mlx-community/Qwen3.6-27B-OptiQ-4bit", "muse-spark-1.2", "muse-spark-1.2-contributor", "omlx", "openai/gpt-oss-120b"] -TOOLS = ["shell", "read_file", "edit_file", "write_file", "webfetch", "skill", "codedb", "read_tool_result", "render_html", "todo_write", "todo_read", "eval", "note_constraint", "ask_user", "attempt_completion", "load_tool_schemas", "mcp_search_tools", "mcp_select_tool", "clock_sleep", "subagent", "workflow", "agent_output", "agent_message", "subagent_resume", "learn_candidate", "peer_message", "workspace", "yield_turn", "imagegen", "lsp", "rlm"] +TOOLS = ["shell", "read_file", "edit_file", "write_file", "webfetch", "skill", "codedb", "read_tool_result", "render_html", "todo_write", "todo_read", "eval", "note_constraint", "ask_user", "attempt_completion", "load_tool_schemas", "mcp_search_tools", "mcp_select_tool", "clock_sleep", "subagent", "workflow", "agent_output", "agent_message", "subagent_resume", "learn_candidate", "peer_message", "workspace", "yield_turn", "imagegen", "lsp", "python_repl", "rlm"] PROVIDERS = ["anthropic", "codegraff", "deepseek", "openai", "google", "minimax", "xiaomi", "kilo", "groq", "cerebras", "mistral", "kimi", "moonshot", "xai", "zai", "meta", "vercel", "openrouter", "fugu", "fireworks", "mlx", "lmstudio", "omlx", "chatgpt-new", "codex", "claude-agent"] diff --git a/sdk/ts/harness.ts b/sdk/ts/harness.ts index f61eaf29..e2a38635 100644 --- a/sdk/ts/harness.ts +++ b/sdk/ts/harness.ts @@ -12,7 +12,7 @@ import type { Readable, Writable } from "node:stream"; export const HARNESS_VERSION = "0.13"; export type ModelName = "MiniMax-M2.5" | "MiniMax-M2.7" | "MiniMax-M3" | "accounts/fireworks/models/deepseek-v4-flash" | "accounts/fireworks/models/deepseek-v4-pro" | "accounts/fireworks/models/glm-5p2" | "accounts/fireworks/models/gpt-oss-120b" | "accounts/fireworks/models/kimi-k2p6" | "accounts/fireworks/models/kimi-k2p7-code" | "accounts/fireworks/models/minimax-m3" | "accounts/fireworks/models/qwen3p7-plus" | "alibaba/qwen3.8-27b" | "anthropic/claude-sonnet-4.6" | "claude-fable-5" | "claude-fable-5-1" | "claude-fable-5-2" | "claude-haiku-5-5" | "claude-opus-4-5" | "claude-opus-4-6" | "claude-opus-4-7" | "claude-opus-4-8" | "claude-opus-4.8" | "claude-opus-5" | "claude-opus-5-5" | "claude-sonnet-4-5" | "claude-sonnet-4-6" | "claude-sonnet-4.6" | "claude-sonnet-5" | "claude-sonnet-5-5" | "deepseek-chat" | "deepseek-reasoner" | "deepseek-v4-flash" | "deepseek-v4-pro" | "flint" | "fugu" | "fugu-ultra" | "fugu-ultra-20260615" | "gemini-3-flash-preview" | "gemini-3.1-flash-lite" | "gemini-3.1-pro-preview" | "gemini-3.5-flash" | "gemini-3.5-flash-lite" | "gemini-3.6-flash" | "gemini-3.7-flash" | "gemini-3.8-flash" | "gemini-4-argon" | "gemma-4-26b-a4b-it" | "gemma-4-31b" | "gemma-4-31b-it" | "glm-4.5" | "glm-4.7" | "glm-5" | "glm-5-turbo" | "glm-5.2" | "glm-5.3" | "glm-5.3-flash" | "glm-5v-turbo" | "gpt-5-codex" | "gpt-5.2" | "gpt-5.3-codex-spark" | "gpt-5.4" | "gpt-5.4-mini" | "gpt-5.4-pro" | "gpt-5.5" | "gpt-5.6" | "gpt-5.6-luna" | "gpt-5.6-sol" | "gpt-5.6-terra" | "gpt-6-astra" | "gpt-6-luna" | "gpt-6-sol" | "gpt-6.1-sol" | "gpt-oss-120b" | "grok-4.3" | "grok-4.6" | "grok-4.7" | "grok-build" | "k3" | "k3-256k" | "k3.1-pro" | "k3.1-pro-highspeed" | "kilo-auto/small" | "kimi-for-coding" | "kimi-for-coding-highspeed" | "kimi-k2.6" | "kimi-k2.7-code" | "kimi-k3" | "kimi-k3.1-pro" | "kimi-k3.1-pro-highspeed" | "kimi-latest" | "lmstudio" | "mimo-v2-flash" | "mimo-v2.5" | "mimo-v2.5-pro" | "mimo-v2.5-pro-ultraspeed" | "mimo-v2.6-flash" | "mimo-v2.6-pro" | "mimo-v2.6-pro-ultraspeed" | "minimax-m3" | "mistral-medium-latest" | "mlx-community/Qwen3.6-27B-OptiQ-4bit" | "muse-spark-1.2" | "muse-spark-1.2-contributor" | "omlx" | "openai/gpt-oss-120b" | (string & {}); -export type ToolName = "shell" | "read_file" | "edit_file" | "write_file" | "webfetch" | "skill" | "codedb" | "read_tool_result" | "render_html" | "todo_write" | "todo_read" | "eval" | "note_constraint" | "ask_user" | "attempt_completion" | "load_tool_schemas" | "mcp_search_tools" | "mcp_select_tool" | "clock_sleep" | "subagent" | "workflow" | "agent_output" | "agent_message" | "subagent_resume" | "learn_candidate" | "peer_message" | "workspace" | "yield_turn" | "imagegen" | "lsp" | "rlm"; +export type ToolName = "shell" | "read_file" | "edit_file" | "write_file" | "webfetch" | "skill" | "codedb" | "read_tool_result" | "render_html" | "todo_write" | "todo_read" | "eval" | "note_constraint" | "ask_user" | "attempt_completion" | "load_tool_schemas" | "mcp_search_tools" | "mcp_select_tool" | "clock_sleep" | "subagent" | "workflow" | "agent_output" | "agent_message" | "subagent_resume" | "learn_candidate" | "peer_message" | "workspace" | "yield_turn" | "imagegen" | "lsp" | "python_repl" | "rlm"; export type ProviderId = "anthropic" | "codegraff" | "deepseek" | "openai" | "google" | "minimax" | "xiaomi" | "kilo" | "groq" | "cerebras" | "mistral" | "kimi" | "moonshot" | "xai" | "zai" | "meta" | "vercel" | "openrouter" | "fugu" | "fireworks" | "mlx" | "lmstudio" | "omlx" | "chatgpt-new" | "codex" | "claude-agent"; /** Events streamed by `harness --json` (one JSON object per stdout line), each @@ -685,7 +685,7 @@ export async function* runAgent(opts: RunAgentOptions): AsyncGenerator { } export const MODELS: ModelName[] = ["MiniMax-M2.5", "MiniMax-M2.7", "MiniMax-M3", "accounts/fireworks/models/deepseek-v4-flash", "accounts/fireworks/models/deepseek-v4-pro", "accounts/fireworks/models/glm-5p2", "accounts/fireworks/models/gpt-oss-120b", "accounts/fireworks/models/kimi-k2p6", "accounts/fireworks/models/kimi-k2p7-code", "accounts/fireworks/models/minimax-m3", "accounts/fireworks/models/qwen3p7-plus", "alibaba/qwen3.8-27b", "anthropic/claude-sonnet-4.6", "claude-fable-5", "claude-fable-5-1", "claude-fable-5-2", "claude-haiku-5-5", "claude-opus-4-5", "claude-opus-4-6", "claude-opus-4-7", "claude-opus-4-8", "claude-opus-4.8", "claude-opus-5", "claude-opus-5-5", "claude-sonnet-4-5", "claude-sonnet-4-6", "claude-sonnet-4.6", "claude-sonnet-5", "claude-sonnet-5-5", "deepseek-chat", "deepseek-reasoner", "deepseek-v4-flash", "deepseek-v4-pro", "flint", "fugu", "fugu-ultra", "fugu-ultra-20260615", "gemini-3-flash-preview", "gemini-3.1-flash-lite", "gemini-3.1-pro-preview", "gemini-3.5-flash", "gemini-3.5-flash-lite", "gemini-3.6-flash", "gemini-3.7-flash", "gemini-3.8-flash", "gemini-4-argon", "gemma-4-26b-a4b-it", "gemma-4-31b", "gemma-4-31b-it", "glm-4.5", "glm-4.7", "glm-5", "glm-5-turbo", "glm-5.2", "glm-5.3", "glm-5.3-flash", "glm-5v-turbo", "gpt-5-codex", "gpt-5.2", "gpt-5.3-codex-spark", "gpt-5.4", "gpt-5.4-mini", "gpt-5.4-pro", "gpt-5.5", "gpt-5.6", "gpt-5.6-luna", "gpt-5.6-sol", "gpt-5.6-terra", "gpt-6-astra", "gpt-6-luna", "gpt-6-sol", "gpt-6.1-sol", "gpt-oss-120b", "grok-4.3", "grok-4.6", "grok-4.7", "grok-build", "k3", "k3-256k", "k3.1-pro", "k3.1-pro-highspeed", "kilo-auto/small", "kimi-for-coding", "kimi-for-coding-highspeed", "kimi-k2.6", "kimi-k2.7-code", "kimi-k3", "kimi-k3.1-pro", "kimi-k3.1-pro-highspeed", "kimi-latest", "lmstudio", "mimo-v2-flash", "mimo-v2.5", "mimo-v2.5-pro", "mimo-v2.5-pro-ultraspeed", "mimo-v2.6-flash", "mimo-v2.6-pro", "mimo-v2.6-pro-ultraspeed", "minimax-m3", "mistral-medium-latest", "mlx-community/Qwen3.6-27B-OptiQ-4bit", "muse-spark-1.2", "muse-spark-1.2-contributor", "omlx", "openai/gpt-oss-120b"]; -export const TOOLS: ToolName[] = ["shell", "read_file", "edit_file", "write_file", "webfetch", "skill", "codedb", "read_tool_result", "render_html", "todo_write", "todo_read", "eval", "note_constraint", "ask_user", "attempt_completion", "load_tool_schemas", "mcp_search_tools", "mcp_select_tool", "clock_sleep", "subagent", "workflow", "agent_output", "agent_message", "subagent_resume", "learn_candidate", "peer_message", "workspace", "yield_turn", "imagegen", "lsp", "rlm"]; +export const TOOLS: ToolName[] = ["shell", "read_file", "edit_file", "write_file", "webfetch", "skill", "codedb", "read_tool_result", "render_html", "todo_write", "todo_read", "eval", "note_constraint", "ask_user", "attempt_completion", "load_tool_schemas", "mcp_search_tools", "mcp_select_tool", "clock_sleep", "subagent", "workflow", "agent_output", "agent_message", "subagent_resume", "learn_candidate", "peer_message", "workspace", "yield_turn", "imagegen", "lsp", "python_repl", "rlm"]; // ── Serverless / In-Memory Agent Runtime (Edge & Worker safe) ───────────── diff --git a/sdk/ts/remote.ts b/sdk/ts/remote.ts index f5964505..c1f64a58 100644 --- a/sdk/ts/remote.ts +++ b/sdk/ts/remote.ts @@ -10,7 +10,7 @@ export const HARNESS_VERSION = "0.13"; export type ModelName = "MiniMax-M2.5" | "MiniMax-M2.7" | "MiniMax-M3" | "accounts/fireworks/models/deepseek-v4-flash" | "accounts/fireworks/models/deepseek-v4-pro" | "accounts/fireworks/models/glm-5p2" | "accounts/fireworks/models/gpt-oss-120b" | "accounts/fireworks/models/kimi-k2p6" | "accounts/fireworks/models/kimi-k2p7-code" | "accounts/fireworks/models/minimax-m3" | "accounts/fireworks/models/qwen3p7-plus" | "alibaba/qwen3.8-27b" | "anthropic/claude-sonnet-4.6" | "claude-fable-5" | "claude-fable-5-1" | "claude-fable-5-2" | "claude-haiku-5-5" | "claude-opus-4-5" | "claude-opus-4-6" | "claude-opus-4-7" | "claude-opus-4-8" | "claude-opus-4.8" | "claude-opus-5" | "claude-opus-5-5" | "claude-sonnet-4-5" | "claude-sonnet-4-6" | "claude-sonnet-4.6" | "claude-sonnet-5" | "claude-sonnet-5-5" | "deepseek-chat" | "deepseek-reasoner" | "deepseek-v4-flash" | "deepseek-v4-pro" | "flint" | "fugu" | "fugu-ultra" | "fugu-ultra-20260615" | "gemini-3-flash-preview" | "gemini-3.1-flash-lite" | "gemini-3.1-pro-preview" | "gemini-3.5-flash" | "gemini-3.5-flash-lite" | "gemini-3.6-flash" | "gemini-3.7-flash" | "gemini-3.8-flash" | "gemini-4-argon" | "gemma-4-26b-a4b-it" | "gemma-4-31b" | "gemma-4-31b-it" | "glm-4.5" | "glm-4.7" | "glm-5" | "glm-5-turbo" | "glm-5.2" | "glm-5.3" | "glm-5.3-flash" | "glm-5v-turbo" | "gpt-5-codex" | "gpt-5.2" | "gpt-5.3-codex-spark" | "gpt-5.4" | "gpt-5.4-mini" | "gpt-5.4-pro" | "gpt-5.5" | "gpt-5.6" | "gpt-5.6-luna" | "gpt-5.6-sol" | "gpt-5.6-terra" | "gpt-6-astra" | "gpt-6-luna" | "gpt-6-sol" | "gpt-6.1-sol" | "gpt-oss-120b" | "grok-4.3" | "grok-4.6" | "grok-4.7" | "grok-build" | "k3" | "k3-256k" | "k3.1-pro" | "k3.1-pro-highspeed" | "kilo-auto/small" | "kimi-for-coding" | "kimi-for-coding-highspeed" | "kimi-k2.6" | "kimi-k2.7-code" | "kimi-k3" | "kimi-k3.1-pro" | "kimi-k3.1-pro-highspeed" | "kimi-latest" | "lmstudio" | "mimo-v2-flash" | "mimo-v2.5" | "mimo-v2.5-pro" | "mimo-v2.5-pro-ultraspeed" | "mimo-v2.6-flash" | "mimo-v2.6-pro" | "mimo-v2.6-pro-ultraspeed" | "minimax-m3" | "mistral-medium-latest" | "mlx-community/Qwen3.6-27B-OptiQ-4bit" | "muse-spark-1.2" | "muse-spark-1.2-contributor" | "omlx" | "openai/gpt-oss-120b" | (string & {}); -export type ToolName = "shell" | "read_file" | "edit_file" | "write_file" | "webfetch" | "skill" | "codedb" | "read_tool_result" | "render_html" | "todo_write" | "todo_read" | "eval" | "note_constraint" | "ask_user" | "attempt_completion" | "load_tool_schemas" | "mcp_search_tools" | "mcp_select_tool" | "clock_sleep" | "subagent" | "workflow" | "agent_output" | "agent_message" | "subagent_resume" | "learn_candidate" | "peer_message" | "workspace" | "yield_turn" | "imagegen" | "lsp" | "rlm"; +export type ToolName = "shell" | "read_file" | "edit_file" | "write_file" | "webfetch" | "skill" | "codedb" | "read_tool_result" | "render_html" | "todo_write" | "todo_read" | "eval" | "note_constraint" | "ask_user" | "attempt_completion" | "load_tool_schemas" | "mcp_search_tools" | "mcp_select_tool" | "clock_sleep" | "subagent" | "workflow" | "agent_output" | "agent_message" | "subagent_resume" | "learn_candidate" | "peer_message" | "workspace" | "yield_turn" | "imagegen" | "lsp" | "python_repl" | "rlm"; export type ProviderId = "anthropic" | "codegraff" | "deepseek" | "openai" | "google" | "minimax" | "xiaomi" | "kilo" | "groq" | "cerebras" | "mistral" | "kimi" | "moonshot" | "xai" | "zai" | "meta" | "vercel" | "openrouter" | "fugu" | "fireworks" | "mlx" | "lmstudio" | "omlx" | "chatgpt-new" | "codex" | "claude-agent"; /** Events streamed by the bridge (same `--json` contract as the stdio SDK). @@ -529,7 +529,7 @@ export async function* runAgentRemote(opts: RunAgentRemoteOptions): AsyncGenerat } export const MODELS: ModelName[] = ["MiniMax-M2.5", "MiniMax-M2.7", "MiniMax-M3", "accounts/fireworks/models/deepseek-v4-flash", "accounts/fireworks/models/deepseek-v4-pro", "accounts/fireworks/models/glm-5p2", "accounts/fireworks/models/gpt-oss-120b", "accounts/fireworks/models/kimi-k2p6", "accounts/fireworks/models/kimi-k2p7-code", "accounts/fireworks/models/minimax-m3", "accounts/fireworks/models/qwen3p7-plus", "alibaba/qwen3.8-27b", "anthropic/claude-sonnet-4.6", "claude-fable-5", "claude-fable-5-1", "claude-fable-5-2", "claude-haiku-5-5", "claude-opus-4-5", "claude-opus-4-6", "claude-opus-4-7", "claude-opus-4-8", "claude-opus-4.8", "claude-opus-5", "claude-opus-5-5", "claude-sonnet-4-5", "claude-sonnet-4-6", "claude-sonnet-4.6", "claude-sonnet-5", "claude-sonnet-5-5", "deepseek-chat", "deepseek-reasoner", "deepseek-v4-flash", "deepseek-v4-pro", "flint", "fugu", "fugu-ultra", "fugu-ultra-20260615", "gemini-3-flash-preview", "gemini-3.1-flash-lite", "gemini-3.1-pro-preview", "gemini-3.5-flash", "gemini-3.5-flash-lite", "gemini-3.6-flash", "gemini-3.7-flash", "gemini-3.8-flash", "gemini-4-argon", "gemma-4-26b-a4b-it", "gemma-4-31b", "gemma-4-31b-it", "glm-4.5", "glm-4.7", "glm-5", "glm-5-turbo", "glm-5.2", "glm-5.3", "glm-5.3-flash", "glm-5v-turbo", "gpt-5-codex", "gpt-5.2", "gpt-5.3-codex-spark", "gpt-5.4", "gpt-5.4-mini", "gpt-5.4-pro", "gpt-5.5", "gpt-5.6", "gpt-5.6-luna", "gpt-5.6-sol", "gpt-5.6-terra", "gpt-6-astra", "gpt-6-luna", "gpt-6-sol", "gpt-6.1-sol", "gpt-oss-120b", "grok-4.3", "grok-4.6", "grok-4.7", "grok-build", "k3", "k3-256k", "k3.1-pro", "k3.1-pro-highspeed", "kilo-auto/small", "kimi-for-coding", "kimi-for-coding-highspeed", "kimi-k2.6", "kimi-k2.7-code", "kimi-k3", "kimi-k3.1-pro", "kimi-k3.1-pro-highspeed", "kimi-latest", "lmstudio", "mimo-v2-flash", "mimo-v2.5", "mimo-v2.5-pro", "mimo-v2.5-pro-ultraspeed", "mimo-v2.6-flash", "mimo-v2.6-pro", "mimo-v2.6-pro-ultraspeed", "minimax-m3", "mistral-medium-latest", "mlx-community/Qwen3.6-27B-OptiQ-4bit", "muse-spark-1.2", "muse-spark-1.2-contributor", "omlx", "openai/gpt-oss-120b"]; -export const TOOLS: ToolName[] = ["shell", "read_file", "edit_file", "write_file", "webfetch", "skill", "codedb", "read_tool_result", "render_html", "todo_write", "todo_read", "eval", "note_constraint", "ask_user", "attempt_completion", "load_tool_schemas", "mcp_search_tools", "mcp_select_tool", "clock_sleep", "subagent", "workflow", "agent_output", "agent_message", "subagent_resume", "learn_candidate", "peer_message", "workspace", "yield_turn", "imagegen", "lsp", "rlm"]; +export const TOOLS: ToolName[] = ["shell", "read_file", "edit_file", "write_file", "webfetch", "skill", "codedb", "read_tool_result", "render_html", "todo_write", "todo_read", "eval", "note_constraint", "ask_user", "attempt_completion", "load_tool_schemas", "mcp_search_tools", "mcp_select_tool", "clock_sleep", "subagent", "workflow", "agent_output", "agent_message", "subagent_resume", "learn_candidate", "peer_message", "workspace", "yield_turn", "imagegen", "lsp", "python_repl", "rlm"]; // ── Serverless / In-Memory Agent Runtime (Edge & Worker safe) ───────────── diff --git a/src/acp_engine.zig b/src/acp_engine.zig index ee28077e..2cbaa880 100644 --- a/src/acp_engine.zig +++ b/src/acp_engine.zig @@ -90,6 +90,8 @@ pub const Dispatch = struct { set_model: ?SetConfigFn = null, orchestrator_config: ?ConfigFn = null, set_orchestrator: ?SetConfigFn = null, + unharnessed_config: ?ConfigFn = null, + set_unharnessed: ?SetConfigFn = null, mcp_servers: ?McpServersFn = null, error_message: ?*const fn (ctx: *anyopaque, err: anyerror) []const u8 = null, cancel_info: ?*const fn (ctx: *anyopaque) ?Cancellation = null, @@ -132,11 +134,12 @@ pub fn configOptions(d: *Dispatch, arena: Allocator) ![]const ConfigOption { if (d.model_config) |model| if (try model(d.ctx, arena)) |option| try items.append(arena, option); if (d.config) |config| if (try config(d.ctx, arena)) |option| try items.append(arena, option); if (d.orchestrator_config) |orch_cfg| if (try orch_cfg(d.ctx, arena)) |option| try items.append(arena, option); + if (d.unharnessed_config) |unh_cfg| if (try unh_cfg(d.ctx, arena)) |option| try items.append(arena, option); return items.items; } pub fn emitConfigChange(d: *Dispatch, arena: Allocator, w: *Io.Writer, sid: []const u8, before: []const ConfigOption) !void { - if (d.config == null and d.model_config == null and d.orchestrator_config == null) return; + if (d.config == null and d.model_config == null and d.orchestrator_config == null and d.unharnessed_config == null) return; const after = try configOptions(d, arena); if (@import("acp_model_option.zig").same(before, after)) return; try proto.writeNotification(w, "session/update", .{ .sessionId = sid, .update = .{ @@ -323,7 +326,7 @@ pub fn handleLine(d: *Dispatch, arena: Allocator, w: *Io.Writer, line: []const u if (d.mcp_servers) |attach| attach(d.ctx, arena, req.params) catch {}; d.created += 1; d.session_id = d.durable_session_id orelse (std.fmt.bufPrint(&d.fallback_id_buf, "acp-{x}-{d}", .{ d.seed, d.created }) catch "acp-0"); - if (d.config != null or d.model_config != null or d.orchestrator_config != null) { + if (d.config != null or d.model_config != null or d.orchestrator_config != null or d.unharnessed_config != null) { const options = configOptions(d, arena) catch |err| return respondError(w, req, err_internal, @errorName(err)); if (d.workspace) |env| try respond(w, req, .{ .sessionId = d.session_id.?, .configOptions = options, ._meta = acp_workspace.meta(env, arena) }) @@ -337,7 +340,7 @@ pub fn handleLine(d: *Dispatch, arena: Allocator, w: *Io.Writer, line: []const u return; } if ((d.load_session != null and (std.mem.eql(u8, req.method, "session/prompt") or std.mem.eql(u8, req.method, "session/cancel") or std.mem.eql(u8, req.method, "session/close") or std.mem.eql(u8, req.method, "_session/steering"))) or - ((d.set_config != null or d.set_orchestrator != null) and std.mem.eql(u8, req.method, "session/set_config_option"))) + ((d.set_config != null or d.set_orchestrator != null or d.set_unharnessed != null) and std.mem.eql(u8, req.method, "session/set_config_option"))) { const params = req.params orelse return respondError(w, req, -32602, "Invalid session ID"); if (params != .object) return respondError(w, req, -32602, "Invalid session ID"); @@ -358,7 +361,7 @@ pub fn handleLine(d: *Dispatch, arena: Allocator, w: *Io.Writer, line: []const u return load(d.ctx, arena, w, req); } } - if (std.mem.eql(u8, req.method, "session/set_config_option") and (d.set_config != null or d.set_orchestrator != null)) { + if (std.mem.eql(u8, req.method, "session/set_config_option") and (d.set_config != null or d.set_orchestrator != null or d.set_unharnessed != null)) { const params = req.params orelse return respondError(w, req, -32602, "Invalid configuration option"); if (params != .object) return respondError(w, req, -32602, "Invalid configuration option"); const id = util.strFieldObj(params.object, "configId") orelse return respondError(w, req, -32602, "Invalid configuration option"); @@ -369,6 +372,8 @@ pub fn handleLine(d: *Dispatch, arena: Allocator, w: *Io.Writer, line: []const u (d.set_model orelse return respondError(w, req, -32602, "Invalid configuration value")) else if (std.mem.eql(u8, id, "orchestrator")) (d.set_orchestrator orelse return respondError(w, req, -32602, "Invalid configuration value")) + else if (std.mem.eql(u8, id, "unharnessed")) + (d.set_unharnessed orelse return respondError(w, req, -32602, "Invalid configuration value")) else return respondError(w, req, -32602, "Invalid configuration value"); const accepted = setter(d.ctx, value) catch |err| return respondError(w, req, err_internal, @errorName(err)); diff --git a/src/acp_session_load.zig b/src/acp_session_load.zig index b8a8fc1d..f30cf32d 100644 --- a/src/acp_session_load.zig +++ b/src/acp_session_load.zig @@ -26,6 +26,8 @@ pub fn configure(d: *engine.Dispatch, live: *LiveTurn) void { d.set_model = @import("acp_model_option.zig").set; d.orchestrator_config = @import("acp_orchestrator.zig").option; d.set_orchestrator = @import("acp_orchestrator.zig").set; + d.unharnessed_config = @import("acp_unharnessed.zig").option; + d.set_unharnessed = @import("acp_unharnessed.zig").set; d.durable_session_id = if (session.validSessionName(live.root.session_name)) live.root.session_name else null; d.durable_name = durableName; live.dispatch = d; @@ -90,6 +92,7 @@ pub fn load(ctx: *anyopaque, arena: Allocator, w: *Io.Writer, req: proto.Request session.loadSession(live.root, live.keys, live.root.arena, sid) catch |err| { return proto.writeError(w, req.id, invalid_params, loadErrorText(arena, err, provider)); }; + @import("python_repl.zig").resetSession(live.root.io); // Same precedence as CLI resume: the host respawns with --model to switch. if (live.model_override) |p| @import("session_branch.zig").applyModelOverride(live.root, live.root.arena, p); live.root.session_name = try live.root.arena.dupe(u8, sid); diff --git a/src/acp_unharnessed.zig b/src/acp_unharnessed.zig new file mode 100644 index 00000000..2c91362f --- /dev/null +++ b/src/acp_unharnessed.zig @@ -0,0 +1,37 @@ +//! Live ACP `unharnessed` selector (`off` | `on`). + +const std = @import("std"); +const Allocator = std.mem.Allocator; + +const engine = @import("acp_engine.zig"); +const LiveTurn = @import("acp_live_turn.zig").LiveTurn; +const no_local_tools = @import("no_local_tools.zig"); +const unharnessed = @import("unharnessed_mode.zig"); + +const choices = [_]engine.ConfigValue{ + .{ .value = "off", .name = "Off" }, + .{ .value = "on", .name = "On" }, +}; + +pub fn option(ctx: *anyopaque, _: Allocator) anyerror!?engine.ConfigOption { + _ = ctx; + if (no_local_tools.enabled) return null; + return .{ + .id = "unharnessed", + .name = "Unharnessed", + .category = "mode", + .currentValue = unharnessed.label(), + .options = &choices, + }; +} + +pub fn set(ctx: *anyopaque, value: []const u8) anyerror!bool { + if (no_local_tools.enabled) return false; + const on = unharnessed.parse(value) orelse return false; + const live: *LiveTurn = @ptrCast(@alignCast(ctx)); + const root = live.root; + var arena_state = std.heap.ArenaAllocator.init(root.gpa); + defer arena_state.deinit(); + _ = try unharnessed.applyLive(root, arena_state.allocator(), on, true); + return true; +} diff --git a/src/additional_tests.zig b/src/additional_tests.zig index 8dfc50a6..19d9fd30 100644 --- a/src/additional_tests.zig +++ b/src/additional_tests.zig @@ -96,5 +96,10 @@ test { @import("plan_accounts_probe.zig"), @import("plan_accounts.zig"), @import("acp_accounts.zig"), + @import("python_repl.zig"), + @import("unharnessed_mode.zig"), + @import("commands_unharnessed.zig"), + @import("acp_unharnessed.zig"), + @import("python_repl_tests.zig"), }; } diff --git a/src/additional_tools.zig b/src/additional_tools.zig index 17667a6a..3521d0ea 100644 --- a/src/additional_tools.zig +++ b/src/additional_tools.zig @@ -140,7 +140,7 @@ fn announced(arena: Allocator, messages: []const Value, name: []const u8) bool { /// itself carries the loaded entry (#492), so rebuild whenever any loaded /// tool is missing from the root's cached catalog. pub fn staleCatalog(self: *Agent) bool { - if (self.sub) return false; + if (self.sub or @import("unharnessed_mode.zig").enabled()) return false; const arena = self.scratchAlloc(); const catalog = self.toolsJson(); for (tailEntries(self, arena) catch return false) |t| { @@ -159,7 +159,7 @@ fn inTail(tail: []const Value, name: []const u8) bool { /// On any other route, remove the announcements instead. Other wires do not /// know them, and there the catalog tail carries the loaded tools. pub fn sync(self: *Agent) void { - if (self.sub) return; + if (self.sub or @import("unharnessed_mode.zig").enabled()) return; if (!active(self.provider)) return dropItems(&self.messages); const arena = self.messageMutationAlloc(); const tail = tailEntries(self, arena) catch return; diff --git a/src/agent_catalog.zig b/src/agent_catalog.zig index c4488633..f93be677 100644 --- a/src/agent_catalog.zig +++ b/src/agent_catalog.zig @@ -18,13 +18,15 @@ fn withExtras(arena: Allocator, base: []const schema.ToolSpec, provider: Provide const b = schedule.catalogExtras(arena); const c = jev_tool.catalogExtras(provider); const d = @import("lsp_catalog.zig").extras(lsp); - if (a.len == 0 and b.len == 0 and c.len == 0 and d.len == 0) return base; - const out = try arena.alloc(schema.ToolSpec, base.len + a.len + b.len + c.len + d.len); + const e = @import("python_repl.zig").catalogExtras(); + if (a.len == 0 and b.len == 0 and c.len == 0 and d.len == 0 and e.len == 0) return base; + const out = try arena.alloc(schema.ToolSpec, base.len + a.len + b.len + c.len + d.len + e.len); @memcpy(out[0..base.len], base); @memcpy(out[base.len..][0..a.len], a); @memcpy(out[base.len + a.len ..][0..b.len], b); @memcpy(out[base.len + a.len + b.len ..][0..c.len], c); - @memcpy(out[base.len + a.len + b.len + c.len ..], d); + @memcpy(out[base.len + a.len + b.len + c.len ..][0..d.len], d); + @memcpy(out[base.len + a.len + b.len + c.len + d.len ..], e); return out; } @@ -85,6 +87,11 @@ pub fn ensureRootTools(self: *Agent, kind: Provider.Kind) !void { .interactions => &self.tools_interactions, }; if (dest.*.len != 0) return; + if (!self.sub and @import("unharnessed_mode.zig").enabled()) { + dest.* = try schema.renderRootTools(self.arena, kind, &@import("unharnessed_mode.zig").specs, &.{}); + self.tools_epoch = @import("native_fold.zig").catalogEpoch(); + return; + } const specs = if (self.sub) try surface.filterSpecs(@TypeOf(schema.base_specs[0]), self.arena, schema.base_specs[0..]) else blk: { diff --git a/src/agent_empty_completion.zig b/src/agent_empty_completion.zig index 0c1002e6..d63d9c5a 100644 --- a/src/agent_empty_completion.zig +++ b/src/agent_empty_completion.zig @@ -195,7 +195,11 @@ fn handleWithWait(self: *Agent, final_text: []const u8, hist_len: usize, comptim if (!PendingWork.canRequest(self)) return false; if (!shouldBounce(main_mod.unattended, no_local_tools.lean, self.text_only, self.review_mode, self.sub, self.tool_calls_this_turn, self.model_calls_this_turn, final_text)) return false; - try self.messages.append(try messages.userNote(self.arena, self.provider.kind, bounce_note)); + const note = if (@import("unharnessed_mode.zig").enabled()) + "If the user requested a change, a description alone is not completion: inspect, update, and verify the workspace using `python` instead of claiming it is already updated. If the request is informational, answer cleanly without making unrequested changes." + else + bounce_note; + try self.messages.append(try messages.userNote(self.arena, self.provider.kind, note)); try self.say("[described a change with no tool call — asking once more]\n", .{}); if (self.tracer) |tr| tr.note("fake_done", "lean -p text-only; bounced"); return true; diff --git a/src/agent_eval_control.zig b/src/agent_eval_control.zig index 1daeb64a..70ecd6b0 100644 --- a/src/agent_eval_control.zig +++ b/src/agent_eval_control.zig @@ -57,6 +57,7 @@ pub fn batchBlocksCompletionFor(calls: []const ToolCall, writes_complete: bool) for (calls) |call| { if ((!writes_complete and (std.mem.eql(u8, call.name, "edit_file") or std.mem.eql(u8, call.name, "write_file"))) or + @import("python_repl.zig").isTool(call.name) or std.mem.eql(u8, call.name, "rlm") or std.mem.eql(u8, call.name, "subagent")) return true; if (std.mem.startsWith(u8, call.name, "mcp__") and !companionReadOnly(call.name, call.input)) return true; diff --git a/src/agent_tool_batch.zig b/src/agent_tool_batch.zig index b68ee535..68c4332c 100644 --- a/src/agent_tool_batch.zig +++ b/src/agent_tool_batch.zig @@ -30,6 +30,7 @@ pub const skipped_text = "tool execution skipped because the operation was abort /// serializes, because write/edit do. Shell-only batches stay parallel /// (#266); the parent joins all results before converting/freeing them (#1166). pub fn isSequential(name: []const u8) bool { + if (@import("python_repl.zig").isTool(name)) return true; if (std.mem.eql(u8, name, @import("jev_tool.zig").name)) return true; if (std.mem.eql(u8, name, "write_file")) return true; if (std.mem.eql(u8, name, "edit_file")) return true; diff --git a/src/agent_tool_gate.zig b/src/agent_tool_gate.zig index a5202167..c8dde52d 100644 --- a/src/agent_tool_gate.zig +++ b/src/agent_tool_gate.zig @@ -150,7 +150,7 @@ pub fn gateHarnessPolicyWrite(self: *Agent, call: ToolCall) !?ExecResult { /// is a read when it is a companion read or its server declares it read-only /// (ADR 0228). fn readOnlyChildAllows(registry: ?*mcp.Registry, call: ToolCall) bool { - for ([_][]const u8{ "write_file", "edit_file", "imagegen", "peer_message", "learn_candidate" }) |name| + for ([_][]const u8{ "write_file", "edit_file", "imagegen", "peer_message", "learn_candidate", "python_repl", "python" }) |name| if (std.mem.eql(u8, call.name, name)) return false; if (mcp.Registry.isMcp(call.name)) return companionReadOnly(call.name, call.input) or (if (registry) |reg| reg.declaresReadOnly(call.name) else false); @@ -223,6 +223,7 @@ pub fn gateTool(self: *Agent, call: ToolCall) !?ExecResult { if (main_mod.plan_mode) { if (std.mem.eql(u8, call.name, "learn_candidate") or std.mem.eql(u8, call.name, "write_file") or std.mem.eql(u8, call.name, "edit_file") or + @import("python_repl.zig").isTool(call.name) or std.mem.eql(u8, call.name, "imagegen") or // #352: writes a file and spawns a workspace-write child std.mem.eql(u8, call.name, "peer_message") or // #469: appends to a peer's inbox — a side effect (mcp.Registry.isMcp(call.name) and !companionReadOnly(call.name, call.input))) @@ -323,6 +324,20 @@ pub fn gateTool(self: *Agent, call: ToolCall) !?ExecResult { if (approvals.allowedExact(self.io, call.name)) return null; key = call.name; prompt_line = std.fmt.bufPrint(&line_buf, "{s} {s}", .{ call.name, path }) catch call.name; + } else if (@import("python_repl.zig").isTool(call.name)) { + if (presence.gateCheck(self.io, self.arena)) |text| return .{ .text = text, .is_error = true }; + if (approvals.allowedExact(self.io, call.name)) return null; + key = call.name; + const code = if (call.input == .object) + (if (call.input.object.get("code")) |c| (if (c == .string) c.string else "") else "") + else + ""; + const first_nl = std.mem.indexOfScalar(u8, code, '\n') orelse code.len; + const snippet = std.mem.trim(u8, code[0..@min(first_nl, 80)], " \t\r"); + prompt_line = if (snippet.len > 0) + std.fmt.bufPrint(&line_buf, "run python: {s}", .{snippet}) catch call.name + else + "run python REPL"; } else if (std.mem.eql(u8, call.name, "learn_candidate")) { if (!learning_privacy.allowsAggregate()) { const in = self.in orelse return .{ @@ -398,7 +413,7 @@ pub fn gateTool(self: *Agent, call: ToolCall) !?ExecResult { 'y', 'Y' => return null, 'a', 'A' => { try approvals.approve(self.io, self.gpa, key); - if (shell_tool.runsCommand(call.name) and Approvals.isInterpreter(key)) { + if ((shell_tool.runsCommand(call.name) and Approvals.isInterpreter(key)) or @import("python_repl.zig").isTool(key)) { try w.print(" note: \"{s}\" can execute arbitrary code (e.g. {s} -c '…'); approving it is effectively unrestricted.\n", .{ key, key }); try w.flush(); } diff --git a/src/agent_tool_label.zig b/src/agent_tool_label.zig index d36697df..9847230f 100644 --- a/src/agent_tool_label.zig +++ b/src/agent_tool_label.zig @@ -52,6 +52,7 @@ fn namedDetailField(name: []const u8, input: std.json.Value) ?[]const u8 { std.mem.eql(u8, name, "write_file") or std.mem.eql(u8, name, "edit_file")) return "path"; if (std.mem.eql(u8, name, "webfetch")) return "url"; + if (@import("python_repl.zig").isTool(name)) return "code"; if (std.mem.eql(u8, name, "subagent")) return "description"; if (std.mem.eql(u8, name, "imagegen")) return "prompt"; if (std.mem.eql(u8, name, "peer_message")) { @@ -169,6 +170,7 @@ pub fn verb(name: []const u8, input: std.json.Value, buf: []u8) []const u8 { if (std.mem.eql(u8, name, "edit_file")) return "edit"; if (std.mem.eql(u8, name, "write_file")) return "write"; if (std.mem.eql(u8, name, "read_file")) return "read"; + if (@import("python_repl.zig").isTool(name)) return "python"; if (std.mem.eql(u8, name, "webfetch")) return "fetch"; if (std.mem.eql(u8, name, "codedb")) { const cmd = stringField(input, "command") orelse return "search"; diff --git a/src/agent_unfinished_bounce.zig b/src/agent_unfinished_bounce.zig index cc43fda5..7db9639b 100644 --- a/src/agent_unfinished_bounce.zig +++ b/src/agent_unfinished_bounce.zig @@ -156,7 +156,8 @@ fn hasWorkspaceMutation(self: *const Agent) bool { if (e.err) continue; if (std.mem.eql(u8, e.name, "edit_file") or std.mem.eql(u8, e.name, "write_file") or - std.mem.eql(u8, e.name, "apply_patch")) return true; + std.mem.eql(u8, e.name, "apply_patch") or + @import("python_repl.zig").isTool(e.name)) return true; } return false; } @@ -177,7 +178,11 @@ pub fn maybeBounceUnfinishedStop(self: *Agent, final_text: []const u8, bounced_o if (!@import("agent_empty_completion.zig").PendingWork.canRequest(self)) return false; bounced_once.* = true; - try self.messages.append(try messages.userNote(self.arena, self.provider.kind, unfinished_action_note)); + const note = if (@import("unharnessed_mode.zig").enabled()) + "Unfinished-action check: your reply ended the turn in plain text while stating that work or edits remain pending. If changes remain, apply and verify them using `python` before concluding; otherwise state any concrete blocker explicitly." + else + unfinished_action_note; + try self.messages.append(try messages.userNote(self.arena, self.provider.kind, note)); try self.say("[stopped with unfinished-work prose and zero edits — asking once more]\n", .{}); if (self.tracer) |tr| tr.note("unfinished_stop", "unattended action turn stopped without edits; bounced once (#1879)"); return true; diff --git a/src/args.zig b/src/args.zig index a65d64c4..12ccbade 100644 --- a/src/args.zig +++ b/src/args.zig @@ -209,6 +209,15 @@ pub fn parse(init: std.process.Init) !Flags { const mode = @import("orchestrator_mode.zig").Mode.parse(val) orelse std.process.fatal("--orchestrator needs off, auto, or on, got '{s}'", .{val}); @import("orchestrator_mode.zig").setFromCli(mode); + } else if (std.mem.eql(u8, arg, "--unharnessed")) { + @import("unharnessed_mode.zig").setFromCli(true); + } else if (std.mem.eql(u8, arg, "--no-unharnessed")) { + @import("unharnessed_mode.zig").setFromCli(false); + } else if (std.mem.startsWith(u8, arg, "--unharnessed=")) { + const val = arg["--unharnessed=".len..]; + const on = @import("unharnessed_mode.zig").parse(val) orelse + std.process.fatal("--unharnessed needs on or off, got '{s}'", .{val}); + @import("unharnessed_mode.zig").setFromCli(on); } else if (std.mem.eql(u8, arg, "--no-autocommit")) { main_mod.g_worktree_autocommit = false; } else if (std.mem.eql(u8, arg, "--schema")) { diff --git a/src/cli_text_source.zig b/src/cli_text_source.zig index 95e80ce1..fe33027b 100644 --- a/src/cli_text_source.zig +++ b/src/cli_text_source.zig @@ -536,6 +536,8 @@ pub const usage_text = \\ --old, --no-rlm restore the pre-rlm structured-only catalog (GRAFF_OLD=1 or GRAFF_RLM=0) \\ --orchestrator[=off|auto|on] same-chat read-only planning -> cheaper executor baton handoff (GRAFF_ORCHESTRATOR) \\ --no-orchestrator disable orchestrator baton handoff (same as --orchestrator=off) + \\ --unharnessed[=on|off] expose ONLY a persistent stateful python REPL tool (GRAFF_UNHARNESSED) + \\ --no-unharnessed disable unharnessed mode (same as --unharnessed=off) \\ --lean slim tool surface (8 core tools) + MCP schemas folded behind load_tool_schemas — the DEFAULT for -p one-shots (GRAFF_LEAN=1). `.mcp.json` still connects. \\ --no-lean opt a one-shot out of the implied --lean: full tool surface + eager MCP schemas, the pre-default -p behavior \\ --no-local-tools embedder mode: hard-disable the built-in bash/bash_output/bash_kill/read_file/edit_file/write_file/codedb tools for the whole process (subagents included), so graff can run outside the sandbox and get its coding tools from an MCP server instead; webfetch, orchestration and MCP tools still work (GRAFF_NO_LOCAL_TOOLS=1) diff --git a/src/command_catalog.zig b/src/command_catalog.zig index 54eaf551..22e21971 100644 --- a/src/command_catalog.zig +++ b/src/command_catalog.zig @@ -78,6 +78,7 @@ pub const commands = [_]Item{ .{ .name = "/routes", .usage = "/routes [|add ]", .desc = "your own priced model lanes across providers — view the set and which seat wins each lane now" }, .{ .name = "/plan", .desc = "toggle plan mode: read-only explore + propose; writes/edits denied" }, .{ .name = "/orchestrator", .usage = "/orchestrator [off|auto|on|status]", .desc = "same-chat planner -> executor baton handoff mode (off|auto|on; persists)" }, + .{ .name = "/unharnessed", .usage = "/unharnessed [on|off|reset|status]", .desc = "single-tool persistent Python REPL mode (on|off|reset; persists)" }, .{ .name = "/ultracode", .desc = "toggle persistent workflow mode; bare opens an on/off picker, or /ultracode on|off" }, .{ .name = "/fallback", .usage = "/fallback [allow|remove|off]", .desc = "opt-in cross-provider fallback for this workspace (same-provider rollout stays on)" }, .{ .name = "/key", .usage = "/key [provider secret]", .desc = "show API-key status; /key adds one live (+ Keychain)" }, diff --git a/src/commands_model.zig b/src/commands_model.zig index 594c07bb..c735f308 100644 --- a/src/commands_model.zig +++ b/src/commands_model.zig @@ -295,6 +295,7 @@ pub fn tryHandle(root: *Agent, keys: *Keys, arena: Allocator, line: []const u8, if (try @import("commands_effort.zig").handle(root, arena, line, out)) return true; if (try @import("compact_at.zig").handle(root, line, out)) return true; if (try @import("commands_orchestrator.zig").handle(root, arena, line, out)) return true; + if (try @import("commands_unharnessed.zig").handle(root, arena, line, out)) return true; if (std.mem.startsWith(u8, line, "/keepcontext")) { const arg = std.mem.trim(u8, line["/keepcontext".len..], " \t"); if (std.mem.eql(u8, arg, "on")) { diff --git a/src/commands_session.zig b/src/commands_session.zig index b0800721..31d8ca12 100644 --- a/src/commands_session.zig +++ b/src/commands_session.zig @@ -80,6 +80,7 @@ pub fn resetConversationSteering(root: *Agent) void { // attempt_completion ended their steering - #318 through the /clear door. if (root.goal_flag) |g| root.goal = goal_flow.standingGoalFromFlag(g, null, root.todos.items, 0); @import("rlm_spec.zig").resetBindsSession(root.io); + @import("python_repl.zig").resetSession(root.io); @import("native_fold.zig").resetSession(); root.invalidateRootTools(); @import("subagent_ledger.zig").reset(root.gpa); // #753: handles belong to the conversation diff --git a/src/commands_unharnessed.zig b/src/commands_unharnessed.zig new file mode 100644 index 00000000..10708c11 --- /dev/null +++ b/src/commands_unharnessed.zig @@ -0,0 +1,73 @@ +//! REPL `/unharnessed` and `--json` `set_unharnessed` handlers. + +const std = @import("std"); +const Io = std.Io; +const Allocator = std.mem.Allocator; + +const Agent = @import("agent.zig").Agent; +const unharnessed = @import("unharnessed_mode.zig"); +const python_repl = @import("python_repl.zig"); +const no_local_tools = @import("no_local_tools.zig"); + +pub fn handle(root: *Agent, arena: Allocator, line: []const u8, out: *Io.Writer) !bool { + if (!std.mem.eql(u8, line, "/unharnessed") and !std.mem.startsWith(u8, line, "/unharnessed ")) return false; + const arg = std.mem.trim(u8, line["/unharnessed".len..], " \t\r\n"); + if (arg.len == 0 or std.ascii.eqlIgnoreCase(arg, "status")) { + if (no_local_tools.enabled) { + try out.writeAll("unharnessed: off (disabled by --no-local-tools)\n"); + } else { + try out.print("unharnessed: {s} (use /unharnessed on|off|reset)\n", .{unharnessed.label()}); + } + try out.flush(); + return true; + } + if (std.ascii.eqlIgnoreCase(arg, "reset")) { + python_repl.resetSession(root.io); + try out.writeAll("python_repl: namespace reset\n"); + try out.flush(); + return true; + } + const on = unharnessed.parse(arg) orelse { + try out.writeAll("usage: /unharnessed [on|off|reset|status]\n"); + try out.flush(); + return true; + }; + if (on and no_local_tools.enabled) { + try out.writeAll("unharnessed: unavailable in --no-local-tools mode\n"); + try out.flush(); + return true; + } + const saved = try unharnessed.applyLive(root, arena, on, true); + const suffix: []const u8 = if (saved) "" else " (not persisted)"; + try out.print("unharnessed: {s}{s}\n", .{ unharnessed.label(), suffix }); + try out.flush(); + return true; +} + +pub fn handleJson(root: *Agent, arena: Allocator, obj: std.json.ObjectMap) void { + const mode_val = obj.get("mode"); + if (mode_val) |mv| if (mv == .string and std.ascii.eqlIgnoreCase(std.mem.trim(u8, mv.string, " \t\r\n"), "reset")) { + python_repl.resetSession(root.io); + root.emit(.{ .type = "unharnessed", .ok = true, .on = unharnessed.enabled(), .reset = true }); + return; + }; + const on: ?bool = if (obj.get("on")) |v| + (if (v == .bool) v.bool else if (v == .string) unharnessed.parse(v.string) else null) + else if (mode_val) |mv| + (if (mv == .string) unharnessed.parse(mv.string) else null) + else + null; + const next = on orelse { + root.emit(.{ .type = "error", .message = "set_unharnessed needs {\"on\": true|false} or {\"mode\": \"on\"|\"off\"|\"reset\"}" }); + return; + }; + if (next and no_local_tools.enabled) { + root.emit(.{ .type = "error", .message = "unharnessed mode is disabled by --no-local-tools" }); + return; + } + _ = unharnessed.applyLive(root, arena, next, true) catch |err| { + root.emit(.{ .type = "error", .message = @errorName(err) }); + return; + }; + root.emit(.{ .type = "unharnessed", .ok = true, .on = unharnessed.enabled() }); +} diff --git a/src/exec.zig b/src/exec.zig index 57a757d7..5edf41c0 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -117,7 +117,7 @@ pub fn execTool(ctx: ToolCtx, call: ToolCall) ToolOutput { // read-only streaks skew, append the edge-triggered nudge cleanly while // freeing the superseded gpa-owned slice (`ToolOutput.text` is `[]u8`, // `&.{}` when empty). - const allow_write_nudge = ctx.tools_used != null and !main_mod.plan_mode and !ctx.read_only_shell and !ctx.synthesis and !no_local_tools.enabled; + const allow_write_nudge = ctx.tools_used != null and !main_mod.plan_mode and !ctx.read_only_shell and !ctx.synthesis and !no_local_tools.enabled and !@import("unharnessed_mode.zig").enabled(); if (tool_balance.recordWithMode(ctx.gpa, call, out.is_error, allow_write_nudge)) |nudge| { defer ctx.gpa.free(nudge); if (std.fmt.allocPrint(ctx.gpa, "{s}\n\n[{s}]", .{ out.text, nudge })) |combined| { @@ -149,7 +149,7 @@ fn execToolInner(ctx: ToolCtx, call: ToolCall) !ToolOutput { // Plan mode backstop: the root gate already denies these with a nicer // message; this catches subagents (which skip the gate entirely). if (main_mod.plan_mode) { - if (local_tools.isLocal(call.name) or std.mem.eql(u8, call.name, "learn_candidate") or std.mem.eql(u8, call.name, "write_file") or std.mem.eql(u8, call.name, "edit_file") or std.mem.eql(u8, call.name, imagegen.tool_name) or mcp.Registry.isMcp(call.name)) return .{ + if (local_tools.isLocal(call.name) or @import("python_repl.zig").isTool(call.name) or std.mem.eql(u8, call.name, "learn_candidate") or std.mem.eql(u8, call.name, "write_file") or std.mem.eql(u8, call.name, "edit_file") or std.mem.eql(u8, call.name, imagegen.tool_name) or mcp.Registry.isMcp(call.name)) return .{ .text = try gpa.dupe(u8, "plan mode is on — read-only; describe the change instead of making it"), .is_error = true, }; @@ -353,6 +353,14 @@ fn execToolInner(ctx: ToolCtx, call: ToolCall) !ToolOutput { } if (std.mem.eql(u8, call.name, "write_file")) return write_file.exec(ctx, input); // ADR 0231 if (std.mem.eql(u8, call.name, "lsp")) return @import("lsp.zig").execute(ctx, input); + if (@import("python_repl.zig").isTool(call.name)) { + const unharnessed = @import("unharnessed_mode.zig").enabled(); + if (std.mem.eql(u8, call.name, "python") and !unharnessed) + return .{ .text = try gpa.dupe(u8, "`python` requires --unharnessed mode; load `python_repl` or use `shell` instead"), .is_error = true }; + if (std.mem.eql(u8, call.name, "python_repl") and unharnessed) + return .{ .text = try gpa.dupe(u8, "`python_repl` is unavailable in --unharnessed mode; use `python` instead"), .is_error = true }; + return @import("python_repl.zig").execute(ctx, call.name, input); + } // Loads one SKILL.md body (or lists them). Rescans on every call, so a // skill written this session is loadable without a restart. if ((comptime @import("build_options").extras) and std.mem.eql(u8, call.name, html_view.tool_name)) return html_view.exec(ctx, input); diff --git a/src/help.zig b/src/help.zig index 7c31a210..b039822a 100644 --- a/src/help.zig +++ b/src/help.zig @@ -30,7 +30,7 @@ const peers_blurb = pub const sections = [_]Section{ .{ .title = "getting around", .names = &.{ "/new", "/clear", "/resume", "/fork", "/save", "/sessions", "/workspace", "/experiment", "/rename", "/rewind", "/edit", "/snapshot", "/teleport" } }, .{ .title = "the model", .names = &.{ "/model", "/models", "/effort", "/reasoning", "/fast", "/thinking", "/followups", "/keepcontext", "/fallback", "/routes" } }, - .{ .title = "working autonomously", .names = &.{ "/goal", "/goals", "/schedule", "/review", "/issue", "/plan", "/orchestrator", "/todo", "/jobs", "/workers", "/ultracode", "/strict", "/yolo", "/never" } }, + .{ .title = "working autonomously", .names = &.{ "/goal", "/goals", "/schedule", "/review", "/issue", "/plan", "/orchestrator", "/unharnessed", "/todo", "/jobs", "/workers", "/ultracode", "/strict", "/yolo", "/never" } }, .{ .title = "talking to other graffs", .names = &.{ "/tell", "/daddy", "/peek", "/adapter" }, .blurb = peers_blurb }, .{ .title = "your setup", .names = &.{ "/login", "/key", "/cost", "/usage", "/version", "/update", "/privacy", "/mcp", "/computer", "/import-claude", "/skills", "/plugins", "/agents", "/hooks", "/tools", "/fleet" } }, .{ .title = "context & history", .names = &.{ "/compact", "/compact-at", "/btw", "/doctor", "/debug", "/cache", "/trace", "/trajectory" } }, diff --git a/src/mainloop.zig b/src/mainloop.zig index e6ad4002..47934f09 100644 --- a/src/mainloop.zig +++ b/src/mainloop.zig @@ -261,6 +261,10 @@ pub fn run(ctx: *Ctx) !void { @import("commands_orchestrator.zig").handleJson(ctx.root, ctx.arena, parsed.object); continue; } + if (std.mem.eql(u8, rtype, "set_unharnessed")) { + @import("commands_unharnessed.zig").handleJson(ctx.root, ctx.arena, parsed.object); + continue; + } if (std.mem.eql(u8, rtype, "score")) { mainloop_score.handle(ctx.root, parsed.object); continue; diff --git a/src/native_fold.zig b/src/native_fold.zig index 186f6075..f745d8dc 100644 --- a/src/native_fold.zig +++ b/src/native_fold.zig @@ -65,6 +65,7 @@ pub const folded = [_][]const u8{ "subagent_resume", "schedule_task", "lsp", + "python_repl", // ADR 0022 / 0140: isFolded is false under --old so the structured-only // catalog stays byte-stable. While available, rlm is on the catalog // from turn one — no batch-size or compactAt gate. diff --git a/src/native_fold_load.zig b/src/native_fold_load.zig index 95910d77..a5061513 100644 --- a/src/native_fold_load.zig +++ b/src/native_fold_load.zig @@ -36,6 +36,7 @@ fn bareNativeName(raw: []const u8) ?[]const u8 { if (std.mem.eql(u8, raw, schedule.tool_name)) return schedule.tool_name; if (std.mem.eql(u8, raw, jev_tool.name)) return jev_tool.name; if (std.mem.eql(u8, raw, "lsp")) return "lsp"; + if (std.mem.eql(u8, raw, "python_repl")) return "python_repl"; return null; } @@ -79,6 +80,9 @@ pub fn findSessionSpec(arena: Allocator, name: []const u8, maybe_provider: ?Prov for (@import("lsp_catalog.zig").rootExtras()) |spec| { if (std.mem.eql(u8, spec.name, name)) return spec; } + for (@import("python_repl.zig").catalogExtras()) |spec| { + if (std.mem.eql(u8, spec.name, name)) return spec; + } if (maybe_provider) |p| { for (jev_tool.catalogExtras(p)) |spec| { if (std.mem.eql(u8, spec.name, name)) return spec; @@ -113,6 +117,10 @@ pub fn canLoadFoldedSpec(arena: Allocator, name: []const u8) !?schema.ToolSpec { if (!@import("lsp_config.zig").root_enabled) return null; break :blk @import("lsp.zig").spec; } + if (std.mem.eql(u8, name, "python_repl")) { + if (@import("unharnessed_mode.zig").enabled()) return null; + break :blk @import("python_repl.zig").spec; + } return null; }; const compacted = try no_local_tools.compactLeanSpecs(schema.ToolSpec, arena, &.{raw_spec}); @@ -155,6 +163,8 @@ pub fn unavailableReason(name: []const u8) []const u8 { return "not enabled for the active provider or model."; if (std.mem.eql(u8, name, "lsp") and !@import("lsp_config.zig").root_enabled) return "no lsp.servers configured in .harness/settings.json."; + if (std.mem.eql(u8, name, "python_repl") and @import("unharnessed_mode.zig").enabled()) + return "unharnessed mode is active (exposed as `python` instead of `python_repl`)."; if (tool_surface.hideBuiltin(name)) return "superseded by the connected companion MCP surface in this session."; if (no_local_tools.lean and !no_local_tools.leanKeeps(name)) @@ -264,6 +274,12 @@ pub fn fallbackNativeQuery(agent: anytype, query: []const u8) !?ExecResult { try active_aw.writer.print("{s}: {s}\n", .{ s.name, s.schema }); active_count += 1; } + for (@import("python_repl.zig").catalogExtras()) |s| { + if (native_fold.blocked(s.name) or scoreSpec(s.name, s.desc, query) == 0) continue; + if (!agentCatalogHasTool(agent, s.name)) continue; + try active_aw.writer.print("{s}: {s}\n", .{ s.name, s.schema }); + active_count += 1; + } if (active_count > 0) { const head = try std.fmt.allocPrint( agent.arena, @@ -279,7 +295,7 @@ pub fn fallbackNativeQuery(agent: anytype, query: []const u8) !?ExecResult { if (std.mem.eql(u8, s.name, name)) break s.desc; } else for (schema.optional_specs) |s| { if (std.mem.eql(u8, s.name, name)) break s.desc; - } else if (std.mem.eql(u8, name, "lsp")) @import("lsp.zig").spec.desc else ""; + } else if (std.mem.eql(u8, name, "lsp")) @import("lsp.zig").spec.desc else if (std.mem.eql(u8, name, "python_repl")) @import("python_repl.zig").spec.desc else ""; if (scoreSpec(name, desc, query) == 0) continue; const rest = (try listDeferredNative(agent.arena)) orelse ""; const sep = if (rest.len > 0) "\n" else ""; diff --git a/src/no_local_tools.zig b/src/no_local_tools.zig index 3d0f5cc5..6d43d36a 100644 --- a/src/no_local_tools.zig +++ b/src/no_local_tools.zig @@ -56,11 +56,14 @@ pub const gated_tools = [_][]const u8{ "render_html", }; -/// Pre-unification names. Still dispatched, still blocked by --no-local-tools. +/// Pre-unification aliases and runtime-extra / mode-specific local tools +/// (`python_repl`, `python`). Blocked by --no-local-tools. pub const gated_aliases = [_][]const u8{ "bash", "bash_output", "bash_kill", + "python_repl", + "python", }; /// A name test only, independent of whether the gate is on, so the comptime @@ -225,6 +228,7 @@ test "#330: the gated set is exactly the host-touching built-ins; webfetch, meta for (gated_tools) |tool| try std.testing.expect(isLocalTool(tool)); try std.testing.expectEqual(@as(usize, 9), gated_tools.len); try std.testing.expect(isLocalTool("bash") and isLocalTool("bash_output") and isLocalTool("bash_kill")); + try std.testing.expect(isLocalTool("python_repl") and isLocalTool("python")); try std.testing.expect(isLocalTool("imagegen")); // #352: optional, but still a local spawn+write try std.testing.expect(isLocalTool("render_html")); // writes the snapshot under the host's own $HOME for ([_][]const u8{ "webfetch", "skill", "subagent", "workflow", "todo_write", "eval", "mcp__sandbox__exec", "mcp__sandbox__bash" }) |tool| @@ -236,6 +240,7 @@ test "#330: the gated set is exactly the host-touching built-ins; webfetch, meta enabled = true; for (gated_tools) |tool| try std.testing.expect(blocks(tool)); try std.testing.expect(blocks("bash") and blocks("bash_output") and blocks("bash_kill")); + try std.testing.expect(blocks("python_repl") and blocks("python")); try std.testing.expect(!blocks("webfetch")); try std.testing.expect(!blocks("mcp__sandbox__bash")); } diff --git a/src/orchestrator_turn.zig b/src/orchestrator_turn.zig index c22a8816..df3c3faa 100644 --- a/src/orchestrator_turn.zig +++ b/src/orchestrator_turn.zig @@ -92,7 +92,7 @@ fn isAutonomousOrNoticeTurn(items: []const std.json.Value, raw_user_text: []cons pub fn run(root: *Agent, keys: *Keys, arena: Allocator, out: ?*Io.Writer, singlePhase: SinglePhaseFn) anyerror![]const u8 { if (root.fallback_blocked or root.sub or main_mod.plan_mode or root.text_only or root.output_schema != null or root.review_mode or orchestrator.g_mode == .off or - activeAutonomousGoal(root)) + activeAutonomousGoal(root) or @import("unharnessed_mode.zig").enabled()) { return singlePhase(root, keys, arena, out); } diff --git a/src/prompts.zig b/src/prompts.zig index e1e5588d..a28b6f59 100644 --- a/src/prompts.zig +++ b/src/prompts.zig @@ -335,6 +335,14 @@ pub fn ultracodeActive(agent: *const Agent) bool { /// by setRootSystemPrompts below) so this stays a pure string funnel for the /// tests and for every non-root agent. pub fn setSystemPrompts(agent: *Agent, base: []const u8, arena: Allocator) !void { + if (@import("unharnessed_mode.zig").enabled() and !agent.sub) { + agent.sys_base = base; + agent.sys_normal = base; + agent.sys_strict = base; + agent.sys_ultra = base; + agent.sys_ultra_strict = base; + return; + } // #1701: custom/learned root personas and later replacements retain the // same publication policy as workers. Keep sys_base verbatim for refreshes. const protected = if (playbook.g_root_inject) text.withAuthority(arena, base) else base; diff --git a/src/python_repl.zig b/src/python_repl.zig new file mode 100644 index 00000000..8c854cc3 --- /dev/null +++ b/src/python_repl.zig @@ -0,0 +1,326 @@ +//! Persistent stateful Python 3 REPL tool (`python_repl` / `python`) backed by +//! a long-lived worker per owner (`root` or subagent id) speaking JSONL over +//! stdio pipes. + +const std = @import("std"); +const builtin = @import("builtin"); +const Io = std.Io; +const Value = std.json.Value; +const Allocator = std.mem.Allocator; + +const tools = @import("tools.zig"); +const ToolCtx = tools.ToolCtx; +const ToolOutput = tools.ToolOutput; +const schema = @import("schema.zig"); +const http = @import("http.zig"); +const codedbpro_paths = @import("codedbpro_paths.zig"); +const mcp_stdio = @import("mcp_stdio.zig"); +const no_local_tools = @import("no_local_tools.zig"); +const native_fold = @import("native_fold.zig"); + +pub const tool_name = "python_repl"; +pub const unharnessed_tool_name = "python"; + +pub const default_timeout_ms: u64 = 75_000; +pub const min_timeout_ms: u64 = 1_000; +pub const max_timeout_ms: u64 = 300_000; +const watchdog_grace_ms: u64 = 2_500; +const in_buf_len: usize = 64 << 10; +const out_buf_len: usize = 256 << 10; + +pub const input_schema = + \\{"type":"object","properties":{"code":{"type":"string","description":"Python 3 source code to run in the persistent session REPL."},"timeout_ms":{"type":["integer","null"],"description":"Optional per-call timeout in milliseconds (1000..300000, default 75000)."},"reset":{"type":["boolean","null"],"description":"Clear all REPL globals before running code (code may be empty when reset is true)."}},"required":["code"],"additionalProperties":false} +; + +pub const desc = "Execute Python 3 code in a persistent stateful session REPL rooted at the workspace directory. Globals, imports, helper functions, in-memory datasets or models, and working-directory changes persist across calls until reset or session end; stdout/stderr are captured, and a trailing expression's repr() is returned and bound to `_`."; + +pub const spec: schema.ToolSpec = .{ + .name = tool_name, + .desc = desc, + .schema = input_schema, +}; + +pub fn isTool(name: []const u8) bool { + return std.mem.eql(u8, name, tool_name) or std.mem.eql(u8, name, unharnessed_tool_name); +} + +pub fn catalogExtras() []const schema.ToolSpec { + if (no_local_tools.enabled or @import("unharnessed_mode.zig").enabled()) return &.{}; + if (no_local_tools.lean and !native_fold.isLoaded(tool_name)) return &.{}; + return &.{spec}; +} + +const worker_script = @embedFile("python_repl_worker.py"); + +const macos_dev_pythons = [_][]const u8{ + "/Library/Developer/CommandLineTools/usr/bin/python3", + "/Applications/Xcode.app/Contents/Developer/usr/bin/python3", +}; + +fn canUseCandidate(io: Io, path: []const u8) bool { + var real_buf: [std.fs.max_path_bytes]u8 = undefined; + const n = Io.Dir.cwd().realPathFile(io, path, &real_buf) catch return false; + const real = real_buf[0..n]; + if (builtin.os.tag == .macos and std.mem.eql(u8, real, "/usr/bin/python3")) { + for (macos_dev_pythons) |dev| { + if (Io.Dir.cwd().access(io, dev, .{})) |_| return true else |_| {} + } + return false; + } + return true; +} + +fn pathEnv() ?[]const u8 { + if (@import("tool_env.zig").get()) |m| { + if (m.get("PATH")) |p| return p; + } + const g = @import("main.zig").g_path_env; + if (g.len > 0) return g; + if (builtin.os.tag != .windows and builtin.os.tag != .wasi) { + if (std.c.getenv("PATH")) |p| return std.mem.span(p); + } + return null; +} + +/// Resolve a runnable Python 3 interpreter without triggering macOS's +/// developer-tools GUI install stub on `/usr/bin/python3`. +pub fn findPython(io: Io, arena: Allocator) ?[]const u8 { + if (pathEnv()) |paths| { + const sep: u8 = if (builtin.os.tag == .windows) ';' else ':'; + const bins = if (builtin.os.tag == .windows) + [_][]const u8{ "python3.exe", "python3", "python.exe", "python" } + else + [_][]const u8{ "python3", "python" }; + for (bins) |bin| { + var it = std.mem.tokenizeScalar(u8, paths, sep); + while (it.next()) |dir| { + const cand = std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, bin }) catch continue; + if (canUseCandidate(io, cand)) return cand; + } + } + } + if (builtin.os.tag == .windows) return null; + for ([_][]const u8{ "/opt/homebrew/bin/python3", "/usr/local/bin/python3", "/usr/bin/python3" }) |abs| { + if (canUseCandidate(io, abs)) return abs; + } + return null; +} + +const Worker = struct { + gpa: Allocator, + io: Io, + key: []u8, + child: std.process.Child, + in_buf: []u8, + out_buf: []u8, + writer: Io.File.Writer, + reader: Io.File.Reader, + + fn start(gpa: Allocator, io: Io, owner: []const u8, prog: []const u8, cwd: []const u8) !*Worker { + const self = try gpa.create(Worker); + errdefer gpa.destroy(self); + const key = try gpa.dupe(u8, owner); + errdefer gpa.free(key); + const in_buf = try gpa.alloc(u8, in_buf_len); + errdefer gpa.free(in_buf); + const out_buf = try gpa.alloc(u8, out_buf_len); + errdefer gpa.free(out_buf); + + var child = try std.process.spawn(io, .{ + .argv = &.{ prog, "-u", "-c", worker_script }, + .stdin = .pipe, + .stdout = .pipe, + .stderr = .ignore, + .environ_map = @import("tool_env.zig").get(), + .cwd = .{ .path = cwd }, + }); + errdefer mcp_stdio.stopChild(io, &child); + + self.* = .{ + .gpa = gpa, + .io = io, + .key = key, + .child = child, + .in_buf = in_buf, + .out_buf = out_buf, + .writer = undefined, + .reader = undefined, + }; + self.writer = self.child.stdin.?.writerStreaming(io, self.in_buf); + self.reader = self.child.stdout.?.readerStreaming(io, self.out_buf); + return self; + } + + fn send(self: *Worker, line: []const u8) !void { + const w = &self.writer.interface; + w.writeAll(line) catch return error.PythonWorkerExited; + w.writeByte('\n') catch return error.PythonWorkerExited; + w.flush() catch return error.PythonWorkerExited; + } + + fn readLine(self: *Worker, budget_ms: u64) ![]u8 { + const Done = union(enum) { line: anyerror![]u8, stall: http.WatchdogFired }; + var buf: [2]Done = undefined; + var sel: Io.Select(Done) = .init(self.io, &buf); + sel.concurrent(.line, readLineTask, .{&self.reader.interface}) catch return error.StreamStalled; + sel.concurrent(.stall, http.deadlineStallTask, .{ self.io, false, budget_ms }) catch { + const only = sel.await() catch |e| { + sel.cancelDiscard(); + return e; + }; + sel.cancelDiscard(); + return only.line; + }; + const first = sel.await() catch |e| { + sel.cancelDiscard(); + return e; + }; + sel.cancelDiscard(); + return switch (first) { + .line => |l| l, + .stall => |w| http.watchdogError(w, error.StreamStalled), + }; + } + + fn stop(self: *Worker) void { + const gpa = self.gpa; + mcp_stdio.stopChild(self.io, &self.child); + gpa.free(self.key); + gpa.free(self.in_buf); + gpa.free(self.out_buf); + gpa.destroy(self); + } +}; + +fn readLineTask(r: *Io.Reader) anyerror![]u8 { + const line = r.takeDelimiter('\n') catch |err| switch (err) { + error.StreamTooLong => { + _ = r.discardDelimiterInclusive('\n') catch return error.PythonWorkerExited; + return error.PythonResponseTooLarge; + }, + error.ReadFailed => return error.PythonWorkerExited, + }; + return line orelse error.PythonWorkerExited; +} + +var mutex: Io.Mutex = .init; +var workers: std.StringHashMapUnmanaged(*Worker) = .empty; +var map_gpa: ?Allocator = null; + +fn takeWorker(io: Io, key: []const u8) ?*Worker { + mutex.lockUncancelable(io); + defer mutex.unlock(io); + const kv = workers.fetchRemove(key) orelse return null; + return kv.value; +} + +fn putWorker(io: Io, w: *Worker) void { + var displaced: ?*Worker = null; + var stop_new = false; + { + mutex.lockUncancelable(io); + defer mutex.unlock(io); + const gpa = map_gpa orelse blk: { + map_gpa = w.gpa; + break :blk w.gpa; + }; + if (workers.fetchRemove(w.key)) |old| displaced = old.value; + workers.put(gpa, w.key, w) catch { + stop_new = true; + }; + } + if (displaced) |old| old.stop(); + if (stop_new) w.stop(); +} + +pub fn resetOwner(io: Io, key: []const u8) void { + if (takeWorker(io, key)) |w| w.stop(); +} + +pub fn resetSession(io: Io) void { + resetOwner(io, "root"); +} + +pub fn stopAll(io: Io) void { + var all: std.ArrayList(*Worker) = .empty; + mutex.lockUncancelable(io); + const gpa = map_gpa; + if (gpa) |g| { + var it = workers.valueIterator(); + while (it.next()) |w| all.append(g, w.*) catch {}; + workers.deinit(g); + workers = .empty; + map_gpa = null; + } + mutex.unlock(io); + for (all.items) |w| w.stop(); + if (gpa) |g| all.deinit(g); +} + +fn fail(gpa: Allocator, msg: []const u8) !ToolOutput { + return .{ .text = try gpa.dupe(u8, msg), .is_error = true }; +} + +pub fn execute(ctx: ToolCtx, call_name: []const u8, input: Value) !ToolOutput { + if (no_local_tools.enabled) return fail(ctx.gpa, no_local_tools.refusal_text); + var arena_state = std.heap.ArenaAllocator.init(ctx.gpa); + defer arena_state.deinit(); + const a = arena_state.allocator(); + + if (@import("hard_policy_context.zig").check(ctx.io, a, ctx.agent_cwd, ctx.hard_policy, "tool.use", call_name)) |denied| { + return fail(ctx.gpa, denied); + } + if (input != .object) return fail(ctx.gpa, "python REPL requires a JSON object"); + + const reset = tools.json_args.flag(input, "reset"); + const code_raw = tools.strField(input, "code"); + if (code_raw == null and !reset) return tools.missingArg(ctx.gpa, "code"); + const code = code_raw orelse ""; + + const timeout_ms: u64 = if (tools.intField(input, "timeout_ms")) |raw_t| + @intCast(std.math.clamp(raw_t, @as(i64, min_timeout_ms), @as(i64, max_timeout_ms))) + else + default_timeout_ms; + + const root_abs = try codedbpro_paths.sessionAbs(a, ctx.io, ctx.agent_cwd, "."); + const owner: []const u8 = if (ctx.from_sub) (ctx.worker_id orelse "sub") else "root"; + const req = try std.json.Stringify.valueAlloc(a, .{ + .code = code, + .workspace_root = root_abs, + .timeout_ms = timeout_ms, + .reset = reset, + }, .{}); + + const worker = takeWorker(ctx.io, owner) orelse blk: { + const prog = findPython(ctx.io, a) orelse return fail(ctx.gpa, "python3 interpreter not found on PATH"); + break :blk Worker.start(ctx.gpa, ctx.io, owner, prog, root_abs) catch |err| { + return .{ .text = try std.fmt.allocPrint(ctx.gpa, "failed to start python REPL worker: {t}", .{err}), .is_error = true }; + }; + }; + var returned = false; + defer if (!returned) worker.stop(); + + worker.send(req) catch { + return fail(ctx.gpa, "python REPL worker exited before receiving input; namespace reset for the next call"); + }; + const raw_line = worker.readLine(timeout_ms + watchdog_grace_ms) catch |err| { + return switch (err) { + error.Canceled, error.Interrupted => .{ .text = try ctx.gpa.dupe(u8, "Python REPL execution cancelled"), .is_error = true, .cancelled = true }, + error.StreamStalled => fail(ctx.gpa, "python REPL worker hung past deadline; worker terminated and namespace reset"), + error.PythonResponseTooLarge => fail(ctx.gpa, "python REPL response exceeded framing buffer; worker reset"), + else => fail(ctx.gpa, "python REPL worker exited unexpectedly; namespace reset for the next call"), + }; + }; + const parsed = std.json.parseFromSliceLeaky(Value, a, raw_line, .{}) catch { + return fail(ctx.gpa, "invalid JSON response from python REPL worker"); + }; + if (parsed != .object) { + return fail(ctx.gpa, "malformed response from python REPL worker"); + } + const ok = if (parsed.object.get("ok")) |v| v == .bool and v.bool else false; + const text = if (parsed.object.get("output")) |v| (if (v == .string) v.string else "") else ""; + const out_copy = try ctx.gpa.dupe(u8, text); + returned = true; + putWorker(ctx.io, worker); + return .{ .text = out_copy, .is_error = !ok }; +} diff --git a/src/python_repl_tests.zig b/src/python_repl_tests.zig new file mode 100644 index 00000000..86c4c2c8 --- /dev/null +++ b/src/python_repl_tests.zig @@ -0,0 +1,355 @@ +//! Unit tests for persistent `python_repl`, safety gates, and `--unharnessed` mode. + +const std = @import("std"); +const builtin = @import("builtin"); +const Io = std.Io; +const Value = std.json.Value; +const Allocator = std.mem.Allocator; + +const main_mod = @import("main.zig"); +const agent_mod = @import("agent.zig"); +const Agent = agent_mod.Agent; +const Provider = @import("provider.zig").Provider; +const Keys = @import("provider.zig").Keys; +const tools = @import("tools.zig"); +const schema = @import("schema.zig"); +const no_local_tools = @import("no_local_tools.zig"); +const native_fold = @import("native_fold.zig"); +const mcp_schema_gate = @import("mcp_schema_gate.zig"); +const mcp_select = @import("mcp_select.zig"); +const additional_tools = @import("additional_tools.zig"); +const exec = @import("exec.zig"); +const agent_tool_gate = @import("agent_tool_gate.zig"); +const agent_tool_batch = @import("agent_tool_batch.zig"); +const agent_eval_control = @import("agent_eval_control.zig"); +const tool_balance = @import("tool_balance.zig"); +const python_repl = @import("python_repl.zig"); +const unharnessed = @import("unharnessed_mode.zig"); +const commands_unharnessed = @import("commands_unharnessed.zig"); +const acp_unharnessed = @import("acp_unharnessed.zig"); +const LiveTurn = @import("acp_live_turn.zig").LiveTurn; +const testAgentFor = @import("agent_request_body_responses.zig").testAgentFor; + +fn makeCtx(gpa: Allocator, io: Io, pending: *@import("jev_effort_state.zig").Pending, cwd: ?[]const u8) tools.ToolCtx { + return .{ + .gpa = gpa, + .io = io, + .client = undefined, + .provider = .{ .id = "codex", .kind = .responses, .auth = .bearer, .url = "", .api_key = "k", .model = "gpt-6-sol", .context = 200_000 }, + .jev_effort_pending = pending, + .registry = null, + .from_sub = false, + .approvals = null, + .tracer = null, + .agent_cwd = cwd, + }; +} + +fn requirePython(io: Io, a: Allocator) !void { + if (builtin.os.tag == .wasi) return error.SkipZigTest; + if (python_repl.findPython(io, a) == null) return error.SkipZigTest; +} + +fn runPy(ctx: tools.ToolCtx, a: Allocator, name: []const u8, payload: []const u8) !tools.ToolOutput { + const val = try std.json.parseFromSliceLeaky(Value, a, payload, .{}); + return python_repl.execute(ctx, name, val); +} + +test "python_repl: persists globals, trailing expression _, resets, and survives errors/sys.exit/SIGALRM" { + const gpa = std.testing.allocator; + const io = std.testing.io; + var arena_state = std.heap.ArenaAllocator.init(gpa); + defer arena_state.deinit(); + const a = arena_state.allocator(); + try requirePython(io, a); + defer python_repl.stopAll(io); + + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var root_buf: [std.fs.max_path_bytes]u8 = undefined; + const root_abs = root_buf[0..try tmp.dir.realPath(io, &root_buf)]; + var pending: @import("jev_effort_state.zig").Pending = .{}; + const ctx = makeCtx(gpa, io, &pending, root_abs); + + // 1. State + trailing expression repr + `_` binding persist across calls. + const r1 = try runPy(ctx, a, "python_repl", "{\"code\":\"items = [2, 4, 6]\\nsum(items)\"}"); + defer gpa.free(r1.text); + try std.testing.expect(!r1.is_error); + try std.testing.expectEqualStrings("12\n", r1.text); + + const r2 = try runPy(ctx, a, "python_repl", "{\"code\":\"prev_val = _\\ndef doubled():\\n return prev_val + items[-1]\\ndoubled()\"}"); + defer gpa.free(r2.text); + try std.testing.expect(!r2.is_error); + try std.testing.expectEqualStrings("18\n", r2.text); + + // 2. OS-level fd 1/2 output from subprocess without capture_output=True is captured without corrupting framing. + const r_fd = try runPy(ctx, a, "python_repl", "{\"code\":\"import subprocess, sys\\nsubprocess.run([sys.executable, '-c', 'print(\\\"fd1-ok\\\")'], check=True)\\n'after-fd1'\"}"); + defer gpa.free(r_fd.text); + try std.testing.expect(!r_fd.is_error); + try std.testing.expect(std.mem.indexOf(u8, r_fd.text, "fd1-ok\n") != null); + try std.testing.expect(std.mem.indexOf(u8, r_fd.text, "'after-fd1'") != null); + + // 3. Exceptions and SystemExit report is_error while keeping prior globals intact. + const r_err = try runPy(ctx, a, "python_repl", "{\"code\":\"1 / 0\"}"); + defer gpa.free(r_err.text); + try std.testing.expect(r_err.is_error); + try std.testing.expect(std.mem.indexOf(u8, r_err.text, "ZeroDivisionError") != null); + + const r_exit = try runPy(ctx, a, "python_repl", "{\"code\":\"import sys; sys.exit(2)\"}"); + defer gpa.free(r_exit.text); + try std.testing.expect(r_exit.is_error); + try std.testing.expect(std.mem.indexOf(u8, r_exit.text, "SystemExit") != null); + + const r_keep = try runPy(ctx, a, "python_repl", "{\"code\":\"items\"}"); + defer gpa.free(r_keep.text); + try std.testing.expect(!r_keep.is_error); + try std.testing.expectEqualStrings("[2, 4, 6]\n", r_keep.text); + + // 4. POSIX SIGALRM timeout preserves prior globals. + if (builtin.os.tag != .windows) { + const r_to = try runPy(ctx, a, "python_repl", "{\"code\":\"import time; time.sleep(3)\",\"timeout_ms\":1000}"); + defer gpa.free(r_to.text); + try std.testing.expect(r_to.is_error); + try std.testing.expect(std.mem.indexOf(u8, r_to.text, "TimeoutError") != null); + + const r_after_to = try runPy(ctx, a, "python_repl", "{\"code\":\"doubled()\"}"); + defer gpa.free(r_after_to.text); + try std.testing.expect(!r_after_to.is_error); + try std.testing.expectEqualStrings("18\n", r_after_to.text); + } + + // 5. Explicit reset clears globals while supporting empty code. + const r_rst = try runPy(ctx, a, "python_repl", "{\"code\":\"\",\"reset\":true}"); + defer gpa.free(r_rst.text); + try std.testing.expect(!r_rst.is_error); + try std.testing.expect(std.mem.indexOf(u8, r_rst.text, "reset") != null); + + const r_gone = try runPy(ctx, a, "python_repl", "{\"code\":\"items\"}"); + defer gpa.free(r_gone.text); + try std.testing.expect(r_gone.is_error); + try std.testing.expect(std.mem.indexOf(u8, r_gone.text, "NameError") != null); + + // 6. resetSession stops root worker cleanly. + const r_reseed = try runPy(ctx, a, "python_repl", "{\"code\":\"x = 42\"}"); + defer gpa.free(r_reseed.text); + python_repl.resetSession(io); + const r_recheck = try runPy(ctx, a, "python_repl", "{\"code\":\"x\"}"); + defer gpa.free(r_recheck.text); + try std.testing.expect(r_recheck.is_error); +} + +test "python_repl: workspace switches update cwd while intra-workspace chdir persists" { + const gpa = std.testing.allocator; + const io = std.testing.io; + var arena_state = std.heap.ArenaAllocator.init(gpa); + defer arena_state.deinit(); + const a = arena_state.allocator(); + try requirePython(io, a); + defer python_repl.stopAll(io); + + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.createDirPath(io, "ws_a/sub"); + try tmp.dir.createDirPath(io, "ws_b"); + var buf_a: [std.fs.max_path_bytes]u8 = undefined; + var buf_b: [std.fs.max_path_bytes]u8 = undefined; + const ws_a = buf_a[0..try tmp.dir.realPathFile(io, "ws_a", &buf_a)]; + const ws_b = buf_b[0..try tmp.dir.realPathFile(io, "ws_b", &buf_b)]; + const sub_a = try std.fs.path.join(a, &.{ ws_a, "sub" }); + + var pending: @import("jev_effort_state.zig").Pending = .{}; + const ctx_a = makeCtx(gpa, io, &pending, ws_a); + const r1 = try runPy(ctx_a, a, "python_repl", "{\"code\":\"import os; os.chdir('sub'); os.getcwd()\"}"); + defer gpa.free(r1.text); + try std.testing.expect(!r1.is_error); + try std.testing.expect(std.mem.indexOf(u8, r1.text, sub_a) != null); + + // Same workspace_root preserves user's os.chdir('sub'). + const r2 = try runPy(ctx_a, a, "python_repl", "{\"code\":\"os.getcwd()\"}"); + defer gpa.free(r2.text); + try std.testing.expect(!r2.is_error); + try std.testing.expect(std.mem.indexOf(u8, r2.text, sub_a) != null); + + // Switching workspace_root updates os.getcwd() automatically. + const ctx_b = makeCtx(gpa, io, &pending, ws_b); + const r3 = try runPy(ctx_b, a, "python_repl", "{\"code\":\"os.getcwd()\"}"); + defer gpa.free(r3.text); + try std.testing.expect(!r3.is_error); + try std.testing.expect(std.mem.indexOf(u8, r3.text, ws_b) != null); +} + +test "python_repl: folded native lifecycle, --no-local-tools, plan_mode, and batch/eval policies" { + const saved_unh = unharnessed.SavedState.save(); + defer saved_unh.restore(); + const saved_local = no_local_tools.enabled; + const saved_lean = no_local_tools.lean; + const saved_fold = native_fold.enabled; + const saved_plan = main_mod.plan_mode; + defer { + no_local_tools.enabled = saved_local; + no_local_tools.lean = saved_lean; + native_fold.enabled = saved_fold; + main_mod.plan_mode = saved_plan; + native_fold.clearLoadedSession(); + mcp_schema_gate.reset(); + } + unharnessed.resetForTest(); + no_local_tools.enabled = false; + no_local_tools.lean = false; + native_fold.enabled = true; + main_mod.plan_mode = false; + native_fold.clearLoadedSession(); + mcp_schema_gate.reset(); + + var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena_state.deinit(); + const a = arena_state.allocator(); + + try std.testing.expect(native_fold.isFolded("python_repl")); + try std.testing.expect(native_fold.blocked("python_repl")); + try std.testing.expect(agent_tool_batch.isSequential("python_repl")); + try std.testing.expect(agent_tool_batch.isSequential("python")); + + var agent = try testAgentFor(a, "anthropic", .anthropic, "claude-sonnet-5"); + agent.registry = null; + agent.invalidateRootTools(); + try agent.ensureRootTools(.anthropic); + try std.testing.expect(std.mem.indexOf(u8, agent.toolsJson(), "\"name\":\"python_repl\"") == null); + + // Deferred listing advertises python_repl and load_tool_schemas loads it cleanly. + const desc = try mcp_schema_gate.descWithListing(a, &.{}); + try std.testing.expect(std.mem.indexOf(u8, desc, "python_repl") != null); + + const load_in = try std.json.parseFromSliceLeaky(Value, a, "{\"tools\":[\"python_repl\"]}", .{}); + const load_res = mcp_select.loadBoth(&agent, .{ .id = "l1", .name = mcp_schema_gate.tool_name, .input = load_in }); + try std.testing.expect(!load_res.is_error); + try std.testing.expect(native_fold.isLoaded("python_repl")); + try std.testing.expect(std.mem.indexOf(u8, agent.toolsJson(), "\"name\":\"python_repl\"") != null); + + const call_py: tools.ToolCall = .{ .id = "c1", .name = "python_repl", .input = try std.json.parseFromSliceLeaky(Value, a, "{\"code\":\"1+1\"}", .{}) }; + try std.testing.expect(agent_eval_control.toolInvalidatesEval(call_py)); + try std.testing.expect(agent_eval_control.batchBlocksCompletion(&.{call_py})); + + // --no-local-tools hides python_repl from folded listing and blocks dispatch for both names. + no_local_tools.enabled = true; + unharnessed.g_enabled = true; + try std.testing.expect(!unharnessed.enabled()); + const remote_desc = try mcp_schema_gate.descWithListing(a, &.{}); + try std.testing.expect(std.mem.indexOf(u8, remote_desc, "python_repl") == null); + + var pending: @import("jev_effort_state.zig").Pending = .{}; + const ctx = makeCtx(std.testing.allocator, std.testing.io, &pending, null); + for ([_][]const u8{ "python_repl", "python" }) |nm| { + const out = exec.execTool(ctx, .{ .id = "d1", .name = nm, .input = call_py.input }); + defer if (out.text.len > 0) std.testing.allocator.free(out.text); + try std.testing.expect(out.is_error); + try std.testing.expect(std.mem.indexOf(u8, out.text, "--no-local-tools") != null); + } + + // plan_mode blocks both python_repl and python at gateTool. + no_local_tools.enabled = false; + main_mod.plan_mode = true; + var ap: agent_mod.Approvals = .{ .yolo = true }; + agent.approvals = ≈ + for ([_][]const u8{ "python_repl", "python" }) |nm| { + const blocked = (try agent_tool_gate.gateTool(&agent, .{ .id = "p1", .name = nm, .input = call_py.input })).?; + try std.testing.expect(blocked.is_error); + try std.testing.expect(std.mem.indexOf(u8, blocked.text, "plan mode") != null); + } + _ = tool_balance.reset; +} + +test "unharnessed_mode: precedence, single-tool catalog isolation, live toggle, REPL, JSON, and ACP" { + const saved_unh = unharnessed.SavedState.save(); + defer saved_unh.restore(); + const saved_local = no_local_tools.enabled; + const saved_stable = mcp_schema_gate.g_stable_catalog; + defer { + no_local_tools.enabled = saved_local; + mcp_schema_gate.g_stable_catalog = saved_stable; + native_fold.clearLoadedSession(); + mcp_schema_gate.reset(); + python_repl.stopAll(std.testing.io); + } + unharnessed.resetForTest(); + no_local_tools.enabled = false; + mcp_schema_gate.g_stable_catalog = true; + native_fold.clearLoadedSession(); + mcp_schema_gate.reset(); + + var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena_state.deinit(); + const a = arena_state.allocator(); + + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.createDirPath(std.testing.io, ".harness"); + try tmp.dir.writeFile(std.testing.io, .{ + .sub_path = unharnessed.settings_path, + .data = "{\"orchestrator\":\"auto\"}\n", + }); + unharnessed.test_settings_dir = tmp.dir; + + try unharnessed.saveSettingsAt(std.testing.io, tmp.dir, a, true); + try std.testing.expect(unharnessed.enabled()); + const raw_settings = try tmp.dir.readFileAlloc(std.testing.io, unharnessed.settings_path, a, .limited(64 * 1024)); + try std.testing.expect(std.mem.indexOf(u8, raw_settings, "\"orchestrator\":\"auto\"") != null); + try std.testing.expect(std.mem.indexOf(u8, raw_settings, "\"unharnessed\":true") != null); + + // Precedence: CLI > env > settings. + unharnessed.g_enabled = false; + unharnessed.loadSettingsFrom(std.testing.io, tmp.dir, a); + try std.testing.expect(unharnessed.enabled()); + unharnessed.applyEnv("off"); + unharnessed.loadSettingsFrom(std.testing.io, tmp.dir, a); + try std.testing.expect(!unharnessed.enabled()); + unharnessed.setFromCli(true); + unharnessed.applyEnv("off"); + try std.testing.expect(unharnessed.enabled()); + + // Single-tool `python` catalog omits all other native/folded/MCP tools and skips additional_tools sync. + unharnessed.resetForTest(); + unharnessed.test_settings_dir = tmp.dir; + native_fold.markLoaded("workflow"); + var agent = try testAgentFor(a, "codex", .responses, "gpt-6.1-sol"); + agent.registry = null; + const normal_prompt = try unharnessed.applyStartupPrompt(std.testing.io, a, "NORMAL-BASE", null, null); + try std.testing.expectEqualStrings("NORMAL-BASE", normal_prompt); + + try std.testing.expect(try unharnessed.applyLive(&agent, a, true, true)); + try std.testing.expect(std.mem.indexOf(u8, agent.sys_normal, "single-tool Python environment") != null); + try std.testing.expect(std.mem.indexOf(u8, agent.sys_strict, "STRICT MODE") == null); + try std.testing.expect(std.mem.indexOf(u8, agent.toolsJson(), "\"name\":\"python\"") != null); + try std.testing.expect(std.mem.indexOf(u8, agent.toolsJson(), "\"name\":\"read_file\"") == null); + try std.testing.expect(std.mem.indexOf(u8, agent.toolsJson(), "\"name\":\"workflow\"") == null); + const parsed_tools = try std.json.parseFromSliceLeaky(Value, a, agent.toolsJson(), .{}); + try std.testing.expectEqual(@as(usize, 1), parsed_tools.array.items.len); + const pre_msgs = agent.messages.items.len; + additional_tools.sync(&agent); + try std.testing.expectEqual(pre_msgs, agent.messages.items.len); + + // REPL `/unharnessed` and JSON `set_unharnessed` toggle back to normal catalog + prompt. + var repl_out: Io.Writer.Allocating = .init(a); + try std.testing.expect(try commands_unharnessed.handle(&agent, a, "/unharnessed off", &repl_out.writer)); + try std.testing.expect(!unharnessed.enabled()); + try std.testing.expect(std.mem.indexOf(u8, repl_out.writer.buffered(), "unharnessed: off") != null); + try std.testing.expect(std.mem.indexOf(u8, agent.sys_normal, "NORMAL-BASE") != null); + try std.testing.expect(std.mem.indexOf(u8, agent.toolsJson(), "\"name\":\"read_file\"") != null); + + var json_aw: Io.Writer.Allocating = .init(a); + agent.out = &json_aw.writer; + main_mod.json_mode = true; + defer main_mod.json_mode = false; + const set_obj = (try std.json.parseFromSliceLeaky(Value, a, "{\"type\":\"set_unharnessed\",\"on\":true}", .{})).object; + commands_unharnessed.handleJson(&agent, a, set_obj); + try std.testing.expect(unharnessed.enabled()); + + // ACP option & set parity. + var keys: Keys = .{ .values = @splat(null) }; + var live: LiveTurn = .{ .root = &agent, .keys = &keys, .out = undefined }; + const opt = (try acp_unharnessed.option(&live, a)).?; + try std.testing.expectEqualStrings("unharnessed", opt.id); + try std.testing.expectEqualStrings("on", opt.currentValue); + try std.testing.expect(try acp_unharnessed.set(&live, "off")); + try std.testing.expect(!unharnessed.enabled()); +} diff --git a/src/python_repl_worker.py b/src/python_repl_worker.py new file mode 100644 index 00000000..bc0d5b39 --- /dev/null +++ b/src/python_repl_worker.py @@ -0,0 +1,190 @@ +"""Persistent stateful Python 3 REPL worker communicating over stdio JSONL.""" + +import ast +import io +import json +import os +import signal +import sys +import tempfile +import traceback + +_MAX_CAPTURE_BYTES = 512 * 1024 +_MAX_OUTPUT_CHARS = 24_000 +_HEAD_TAIL_CHARS = 12_000 + + +def _read_capture(cap): + cap.flush() + size = cap.seek(0, os.SEEK_END) + if size <= _MAX_CAPTURE_BYTES: + cap.seek(0) + return cap.read().decode("utf-8", errors="replace") + half = _MAX_CAPTURE_BYTES // 2 + cap.seek(0) + head = cap.read(half).decode("utf-8", errors="replace") + cap.seek(size - half) + tail = cap.read(half).decode("utf-8", errors="replace") + omitted = size - (2 * half) + return f"{head}\n...[truncated {omitted} raw bytes]...\n{tail}" + + +def _format_output(text, ok): + if len(text) > _MAX_OUTPUT_CHARS: + omitted = len(text) - (2 * _HEAD_TAIL_CHARS) + text = ( + text[:_HEAD_TAIL_CHARS] + + f"\n...[truncated {omitted} chars; keep large objects in Python variables and inspect slices/summaries]...\n" + + text[-_HEAD_TAIL_CHARS:] + ) + if not text: + return "[ok - no output]" if ok else "[error with no output]" + return text + + +def _run_request(req, ns, state, devnull_fd): + if req.get("reset"): + ns.clear() + ns["__name__"] = "__main__" + ns["__builtins__"] = __builtins__ + state["workspace_root"] = None + + ws = req.get("workspace_root") or "" + if ws and ws != state["workspace_root"]: + os.chdir(ws) + if ws not in sys.path: + sys.path.insert(0, ws) + state["workspace_root"] = ws + + code = req.get("code") or "" + if req.get("reset") and not code.strip(): + return True, "[python_repl namespace reset]" + + timeout_ms = req.get("timeout_ms", 75_000) + if isinstance(timeout_ms, bool) or not isinstance(timeout_ms, (int, float)) or timeout_ms <= 0: + timeout_ms = 75_000 + timeout_s = max(1_000, min(int(timeout_ms), 300_000)) / 1000.0 + + cap = tempfile.TemporaryFile(mode="w+b") + cap_fd = cap.fileno() + os.set_inheritable(cap_fd, False) + old_stdout, old_stderr = sys.stdout, sys.stderr + os.dup2(cap_fd, 1) + os.dup2(cap_fd, 2) + out_wrap = io.TextIOWrapper( + os.fdopen(1, "wb", buffering=0, closefd=False), + encoding="utf-8", + errors="replace", + write_through=True, + ) + err_wrap = io.TextIOWrapper( + os.fdopen(2, "wb", buffering=0, closefd=False), + encoding="utf-8", + errors="replace", + write_through=True, + ) + sys.stdout = out_wrap + sys.stderr = err_wrap + + use_alarm = hasattr(signal, "SIGALRM") and hasattr(signal, "setitimer") + old_handler = None + ok = True + try: + if use_alarm: + def _on_timeout(signum, frame): + raise TimeoutError( + f"Execution timed out after {timeout_s:g}s (prior REPL state is preserved)" + ) + + old_handler = signal.signal(signal.SIGALRM, _on_timeout) + signal.setitimer(signal.ITIMER_REAL, timeout_s) + try: + tree = ast.parse(code, "", "exec") + if tree.body and isinstance(tree.body[-1], ast.Expr): + if len(tree.body) > 1: + prefix = ast.Module(body=tree.body[:-1], type_ignores=[]) + exec(compile(prefix, "", "exec"), ns, ns) + expr = ast.Expression(body=tree.body[-1].value) + val = eval(compile(expr, "", "eval"), ns, ns) + if val is not None: + ns["_"] = val + print(repr(val)) + else: + exec(compile(tree, "", "exec"), ns, ns) + except BaseException: + ok = False + traceback.print_exc(file=err_wrap) + finally: + if use_alarm: + signal.setitimer(signal.ITIMER_REAL, 0) + signal.signal(signal.SIGALRM, old_handler) + raw_text = _read_capture(cap) + finally: + for stream in (sys.stdout, sys.stderr, out_wrap, err_wrap): + try: + stream.flush() + except Exception: + pass + sys.stdout = old_stdout + sys.stderr = old_stderr + for wrap in (out_wrap, err_wrap): + try: + wrap.close() + except Exception: + pass + os.dup2(devnull_fd, 1) + os.dup2(devnull_fd, 2) + cap.close() + + return ok, _format_output(raw_text, ok) + + +def main(): + # Isolate protocol pipes before user code executes so prints or child processes + # writing to OS fds 1/2 cannot corrupt JSONL responses or hold pipes open. + proto_in_fd = os.dup(0) + proto_out_fd = os.dup(1) + os.set_inheritable(proto_in_fd, False) + os.set_inheritable(proto_out_fd, False) + proto_in = os.fdopen(proto_in_fd, "rb") + proto_out = os.fdopen(proto_out_fd, "wb", buffering=0) + + devnull_fd = os.open(os.devnull, os.O_RDWR) + os.set_inheritable(devnull_fd, False) + os.dup2(devnull_fd, 0) + os.dup2(devnull_fd, 1) + os.dup2(devnull_fd, 2) + sys.stdin = open(os.devnull, "r", encoding="utf-8", errors="replace") + + ns = {"__name__": "__main__", "__builtins__": __builtins__} + state = {"workspace_root": None} + + try: + while True: + raw = proto_in.readline() + if not raw: + break + line = raw.strip() + if not line: + continue + try: + req = json.loads(line.decode("utf-8")) + ok, output = _run_request(req, ns, state, devnull_fd) + except BaseException as exc: + ok, output = False, f"Worker protocol error: {exc}" + payload = json.dumps({"ok": ok, "output": output}, ensure_ascii=False) + "\n" + proto_out.write(payload.encode("utf-8")) + finally: + for handle in (proto_in, proto_out): + try: + handle.close() + except Exception: + pass + try: + os.close(devnull_fd) + except Exception: + pass + + +if __name__ == "__main__": + main() diff --git a/src/schema.zig b/src/schema.zig index c72cfcb4..2291584a 100644 --- a/src/schema.zig +++ b/src/schema.zig @@ -265,7 +265,7 @@ pub fn renderRootTools( // GRAFF_STABLE_CATALOG (#476): loaded tools append in LOAD ORDER after the // stable head, so loads change only tail bytes (see renderLoadedTail for // why that alone does not keep an OpenAI cache hit). - if (mcp_schema_gate.g_stable_catalog) try native_fold.renderLoadedTail(&s, kind, out, mcp_tools); + if (mcp_schema_gate.g_stable_catalog and !@import("unharnessed_mode.zig").enabled()) try native_fold.renderLoadedTail(&s, kind, out, mcp_tools); try s.endArray(); return aw.toOwnedSlice(); } @@ -276,6 +276,7 @@ pub fn renderRootTools( /// optional tool whose backing capability startup found. MCP tools are /// appended afterwards by renderRootTools and stay untouched. pub fn effectiveRootSpecs(arena: Allocator) ![]const ToolSpec { + if (@import("unharnessed_mode.zig").enabled()) return &@import("unharnessed_mode.zig").specs; const chosen: []const ToolSpec = if (root.g_clock_sleep) (if (learn_store.active_agent_loaded) &root_specs else &root_specs_without_learning) else @@ -472,7 +473,7 @@ pub fn emitSchema(w: *Io.Writer) !void { try s.objectField("tools"); try s.beginArray(); // Optionals + rlm: SDK dump is unconditional (#352, #868). - for (root_specs ++ optional_specs ++ [_]ToolSpec{ @import("lsp.zig").spec, .{ .name = rlm.tool_name, .desc = rlm.tool_desc, .schema = rlm.tool_schema } }) |t| { + for (root_specs ++ optional_specs ++ [_]ToolSpec{ @import("lsp.zig").spec, @import("python_repl.zig").spec, .{ .name = rlm.tool_name, .desc = rlm.tool_desc, .schema = rlm.tool_schema } }) |t| { try s.beginObject(); try s.objectField("name"); try s.write(t.name); diff --git a/src/schema_protocol.zig b/src/schema_protocol.zig index 110d2d5a..4ddd34ce 100644 --- a/src/schema_protocol.zig +++ b/src/schema_protocol.zig @@ -19,6 +19,8 @@ pub const flags = \\ {"flag": "--old", "arg": null, "description": "restore the pre-rlm structured-only catalog. --no-rlm is an alias. GRAFF_OLD=1 or GRAFF_RLM=0"}, \\ {"flag": "--orchestrator", "arg": "off|auto|on", "description": "enable same-chat planner -> executor baton handoff (bare flag = on; --orchestrator=off|auto|on). GRAFF_ORCHESTRATOR is the env form"}, \\ {"flag": "--no-orchestrator", "arg": null, "description": "disable orchestrator baton handoff (--orchestrator=off)"}, + \\ {"flag": "--unharnessed", "arg": "on|off", "description": "expose only the persistent stateful python REPL tool (bare flag = on; --unharnessed=on|off). GRAFF_UNHARNESSED is the env form"}, + \\ {"flag": "--no-unharnessed", "arg": null, "description": "disable unharnessed mode (--unharnessed=off)"}, \\ {"flag": "--lean", "arg": null, "description": "slim the tool surface to 8 core tools and connect MCP servers DEFERRED behind the load_tool_schemas meta tool (names + one-line descriptions up front, full schemas on demand) instead of paying every server's schemas in every model turn. DEFAULT for -p one-shots; GRAFF_LEAN=1 forces it on anywhere"}, \\ {"flag": "--no-lean", "arg": null, "description": "opt a one-shot out of the implied --lean: full tool surface and eager MCP schemas, the pre-default -p behavior"}, \\ {"flag": "--add-dir", "arg": "path", "description": "extra workspace root file tools may touch (repeatable, max 16). Complementary to workspace switch; extra roots do not contribute skills or sessions"}, @@ -39,7 +41,7 @@ pub const flags = pub const json = \\{ \\ "transport": "newline-delimited JSON over stdin/stdout (--json)", - \\ "request": "one JSON object per line: {\"type\":\"user\",\"text\":\"...\",\"images\":[{\"type\":\"image_url\",\"url\":\"https://...\"},{\"type\":\"image_base64\",\"media_type\":\"image/png\",\"data\":\"...\"}]} sends a native multimodal user turn (images optional, max 16). Only user input may set this field; {\"type\":\"review\",\"text\":\"target or instructions\"} runs one isolated read-only review turn with fresh model-visible history and no edits, delegation, workflows, or web access; {\"type\":\"answer\",\"text\":\"...\",\"cancelled\":false,\"call_id\":\"optional\"} answers an active ask_user event; {\"type\":\"cancel\"} interrupts an active turn; {\"type\":\"set_system_prompt\",\"text\":\"...\",\"append\":false} replaces (or with append=true extends) the system prompt between turns and acks with a system_prompt event; {\"type\":\"set_model\",\"model\":\"gpt-6-sol\",\"provider\":\"codex\"}, {\"type\":\"compact\"}, {\"type\":\"set_mode\",\"mode\":\"plan|normal\"}, {\"type\":\"set_orchestrator\",\"mode\":\"off|auto|on\"}, and {\"type\":\"set_agent\",\"id\":\"reviewer\"}, {\"type\":\"set_effort\",\"level\":\"low|medium|high|xhigh|max|ultra\"}, {\"type\":\"set_fast\",\"on\":true}, and {\"type\":\"set_ultracode\",\"on\":true} are live control requests acked by model/compact/mode/orchestrator/agent/effort/fast/ultracode events. For compatibility, set_model also accepts legacy {\"name\":\"provider model|model\"}. NOTE: the system prompt heads the KV-cached prefix, so any mutation invalidates the cache for the whole conversation (per Manus context-engineering lessons) — set it at spawn when possible and mutate only at task boundaries", + \\ "request": "one JSON object per line: {\"type\":\"user\",\"text\":\"...\",\"images\":[{\"type\":\"image_url\",\"url\":\"https://...\"},{\"type\":\"image_base64\",\"media_type\":\"image/png\",\"data\":\"...\"}]} sends a native multimodal user turn (images optional, max 16). Only user input may set this field; {\"type\":\"review\",\"text\":\"target or instructions\"} runs one isolated read-only review turn with fresh model-visible history and no edits, delegation, workflows, or web access; {\"type\":\"answer\",\"text\":\"...\",\"cancelled\":false,\"call_id\":\"optional\"} answers an active ask_user event; {\"type\":\"cancel\"} interrupts an active turn; {\"type\":\"set_system_prompt\",\"text\":\"...\",\"append\":false} replaces (or with append=true extends) the system prompt between turns and acks with a system_prompt event; {\"type\":\"set_model\",\"model\":\"gpt-6-sol\",\"provider\":\"codex\"}, {\"type\":\"compact\"}, {\"type\":\"set_mode\",\"mode\":\"plan|normal\"}, {\"type\":\"set_orchestrator\",\"mode\":\"off|auto|on\"}, {\"type\":\"set_unharnessed\",\"on\":true}, and {\"type\":\"set_agent\",\"id\":\"reviewer\"}, {\"type\":\"set_effort\",\"level\":\"low|medium|high|xhigh|max|ultra\"}, {\"type\":\"set_fast\",\"on\":true}, and {\"type\":\"set_ultracode\",\"on\":true} are live control requests acked by model/compact/mode/orchestrator/unharnessed/agent/effort/fast/ultracode events. For compatibility, set_model also accepts legacy {\"name\":\"provider model|model\"}. NOTE: the system prompt heads the KV-cached prefix, so any mutation invalidates the cache for the whole conversation (per Manus context-engineering lessons) — set it at spawn when possible and mutate only at task boundaries", \\ "score_request": "{\"type\":\"score\",\"prompt_sha\":\"<16 hex>\",\"score\":0.7,\"notes\":\"...\",\"parent_sha\":\"<16 hex, optional>\"} appends an evaluation record for an agent/prompt variant to this run's file under .graff/trajectories (the aggregate append-only DGM-style archive; prompt_sha = first 8 bytes of SHA-256 of the system prompt, hex; parent_sha records which prompt this variant was mutated from — the lineage edge DGM parent selection counts children with) and acks with a score event", \\ "sequencing": "every event object below carries a monotonic per-session \"seq\" as its FIRST field: 1-based, gap-free, one id per emitted line. It survives a resume - the counter is persisted with the conversation as event_seq and restored by --resume, so a REPLACEMENT process continues the numbering instead of reissuing ids a consumer already saw. `graff serve` forwards the child's seq unchanged and uses it for ?from=N replay", \\ "events": [ @@ -63,6 +65,7 @@ pub const json = \\ {"type": "compact", "ok": true, "chars": 0}, \\ {"type": "mode", "ok": true, "mode": "plan"}, \\ {"type": "orchestrator", "ok": true, "mode": "auto", "supported": true, "executor_model": "gpt-6-sol"}, + \\ {"type": "unharnessed", "ok": true, "on": true}, \\ {"type": "agent", "ok": true, "id": "reviewer", "chars": 0}, \\ {"type": "effort", "ok": true, "level": "medium", "applies": true}, \\ {"type": "fast", "ok": true, "on": true, "applies": true}, diff --git a/src/session_globals.zig b/src/session_globals.zig index 48f82fa9..9f3111ba 100644 --- a/src/session_globals.zig +++ b/src/session_globals.zig @@ -17,4 +17,5 @@ pub fn release(gpa: Allocator, io: Io) void { @import("mcp_shapes.zig").shutdown(io); // ADR 0309: held `claude` children end with the run. @import("claude_agent_session.zig").stopAll(io); + @import("python_repl.zig").stopAll(io); } diff --git a/src/session_settings.zig b/src/session_settings.zig index 8c35ca09..45624086 100644 --- a/src/session_settings.zig +++ b/src/session_settings.zig @@ -299,6 +299,7 @@ pub fn applyEnvKnobs(arena: Allocator, environ_map: anytype) !void { @import("turn_chrome.zig").max_turn_model_calls = @import("turn_chrome.zig").parseMaxTurnCalls(v); } @import("orchestrator_mode.zig").applyEnv(environ_map.get("GRAFF_ORCHESTRATOR")); + @import("unharnessed_mode.zig").applyEnv(environ_map.get("GRAFF_UNHARNESSED")); } fn codegraffLoginKey(io: Io, arena: Allocator, environ_map: anytype) ?[]const u8 { @@ -319,6 +320,7 @@ pub fn setupSkillsAndTheme(io: Io, arena: Allocator, environ_map: anytype, out: anim.loadThemeSetting(io, arena); // {"theme": ""} → opt-in terminal color theme @import("followups.zig").load(io, arena); // {"followups": "queue"|"immediate"} → Codex/ChatGPT delivery @import("orchestrator_mode.zig").loadSettings(io, arena); // {"orchestrator": "off"|"auto"|"on"} → same-chat baton handoff + @import("unharnessed_mode.zig").loadSettings(io, arena); // {"unharnessed": true|false} → 1-tool python REPL mode const theme_on = anim.g_theme != null and use_color and !json_mode; if (theme_on) { out.writeAll(anim.themes[anim.g_theme.?].seq) catch {}; diff --git a/src/spec_catalog_conformance.zig b/src/spec_catalog_conformance.zig index 5e0d5b53..35ecf152 100644 --- a/src/spec_catalog_conformance.zig +++ b/src/spec_catalog_conformance.zig @@ -55,6 +55,7 @@ test "spec/tool_catalog: implementation matches executable semantics" { const rlm = @import("rlm.zig"); const orch_mode = @import("orchestrator_mode.zig"); + const unharnessed = @import("unharnessed_mode.zig"); const saved_local = no_local_tools.enabled; const saved_lean = no_local_tools.lean; const saved_img = imagegen.available; @@ -62,6 +63,7 @@ test "spec/tool_catalog: implementation matches executable semantics" { const saved_learn = learn_store.active_agent_loaded; const saved_rlm = rlm.available; const saved_orch = orch_mode.handoff_active; + const saved_unharnessed = unharnessed.g_enabled; defer { no_local_tools.enabled = saved_local; no_local_tools.lean = saved_lean; @@ -71,12 +73,14 @@ test "spec/tool_catalog: implementation matches executable semantics" { rlm.available = saved_rlm; rlm.sync(); orch_mode.handoff_active = saved_orch; + unharnessed.g_enabled = saved_unharnessed; } // The 64-cell kernel is the structured catalog (`--old`). rlm is a // session overlay (ADR 0022) and must not grow this cube. rlm.available = false; rlm.sync(); orch_mode.handoff_active = false; + unharnessed.g_enabled = false; const cases = parsed.value.object.get("cases").?.array.items; try std.testing.expect(cases.len == 64); diff --git a/src/startup.zig b/src/startup.zig index 02fe6a4b..81e596f3 100644 --- a/src/startup.zig +++ b/src/startup.zig @@ -191,7 +191,7 @@ pub fn buildSystemPrompt( // sys_base so a later playbook refresh cannot drop it. if (if (comptime build_options.learn) @import("experiment_pool.zig").directive() else null) |d| sys_normal = try std.fmt.allocPrint(arena, "{s}\n\n{s}", .{ sys_normal, d }); - return sys_normal; + return @import("unharnessed_mode.zig").applyStartupPrompt(io, arena, sys_normal, system_prompt_flag, append_system_flag); } const args = @import("args.zig"); diff --git a/src/tool_balance.zig b/src/tool_balance.zig index fe58cd28..c11524ff 100644 --- a/src/tool_balance.zig +++ b/src/tool_balance.zig @@ -212,7 +212,8 @@ fn classifyActivity(call: tools.ToolCall, class: Class) ToolActivity { const name = call.name; if (std.mem.eql(u8, name, "edit_file") or std.mem.eql(u8, name, "write_file") or - std.mem.eql(u8, name, "apply_patch")) return .write_or_exec; + std.mem.eql(u8, name, "apply_patch") or + @import("python_repl.zig").isTool(name)) return .write_or_exec; if (std.mem.eql(u8, name, "bash") or std.mem.eql(u8, name, "shell")) { const cmd = tools.strField(call.input, "command") orelse return .neutral; return classifyShellCommand(cmd); diff --git a/src/unharnessed_mode.zig b/src/unharnessed_mode.zig new file mode 100644 index 00000000..540aefd9 --- /dev/null +++ b/src/unharnessed_mode.zig @@ -0,0 +1,210 @@ +//! Opt-in single-tool Python REPL mode (`--unharnessed`, `GRAFF_UNHARNESSED`, +//! `.harness/settings.json` `"unharnessed"`, `/unharnessed`, ACP config). +//! Exposes ONLY the persistent stateful `python` tool alongside a compact +//! single-tool system prompt while reusing graff's provider transports, auth, +//! approvals, hard policies, and trace/session machinery. + +const std = @import("std"); +const Io = std.Io; +const Allocator = std.mem.Allocator; + +const Agent = @import("agent.zig").Agent; +const schema = @import("schema.zig"); +const no_local_tools = @import("no_local_tools.zig"); +const python_repl = @import("python_repl.zig"); + +pub const settings_path = ".harness/settings.json"; +pub const settings_field = "unharnessed"; + +pub var g_enabled: bool = false; +pub var cli_set: bool = false; +pub var env_set: bool = false; +pub var g_saved_normal_base: []const u8 = ""; +pub var g_saved_system_flag: ?[]const u8 = null; +pub var g_saved_append_flag: ?[]const u8 = null; +pub var test_settings_dir: ?Io.Dir = null; + +pub const specs = [_]schema.ToolSpec{ + .{ + .name = python_repl.unharnessed_tool_name, + .desc = "Execute Python 3 code in a persistent stateful REPL rooted at the workspace directory. All globals, imports, helper functions, classes, in-memory data structures, and working-directory changes persist across calls for the entire session. stdout/stderr are returned, and if the final statement is an expression, its repr() is returned and bound to `_`.", + .schema = python_repl.input_schema, + }, +}; + +pub const system_prompt = + \\You are an autonomous problem-solving agent operating in a single-tool Python environment. + \\Your ONLY tool is `python`, a persistent stateful Python 3 REPL running directly in the workspace root. + \\ + \\Key properties of your persistent Python REPL: + \\- Variables, imports, functions, classes, parsed datasets/ASTs, and `os.chdir` persist across every `python` call in this session. + \\- If the last statement in a call is an expression, its `repr()` is displayed automatically and bound to `_`. + \\- Use standard Python (`pathlib`, `json`, `csv`, `sqlite3`, `ast`, `re`, `numpy`, `scipy`, `pandas`, `subprocess`, etc.) directly to inspect files, search, edit or generate files, run computations, and execute tests or build tools (`subprocess.run(..., capture_output=True, text=True)`). + \\- Inspect slices, schemas, summaries, and targeted sections rather than printing entire huge files or arrays to stdout; keep full objects in Python variables across turns. + \\- When solving coding tasks, do not edit existing locked test files unless explicitly asked. + \\- Always verify your changes and generated outputs before finishing, then respond directly in plain text with no tool calls summarizing what you verified. +; + +pub fn enabled() bool { + return g_enabled and !no_local_tools.enabled; +} + +pub fn label() []const u8 { + return if (enabled()) "on" else "off"; +} + +pub fn parse(raw: []const u8) ?bool { + const s = std.mem.trim(u8, raw, " \t\r\n"); + if (std.ascii.eqlIgnoreCase(s, "on") or std.mem.eql(u8, s, "1") or + std.ascii.eqlIgnoreCase(s, "true") or std.ascii.eqlIgnoreCase(s, "yes")) return true; + if (std.ascii.eqlIgnoreCase(s, "off") or std.mem.eql(u8, s, "0") or + std.ascii.eqlIgnoreCase(s, "false") or std.ascii.eqlIgnoreCase(s, "no")) return false; + return null; +} + +pub fn setFromCli(on: bool) void { + g_enabled = on; + cli_set = true; +} + +pub fn applyEnv(raw: ?[]const u8) void { + if (cli_set) return; + const v = raw orelse return; + if (parse(v)) |on| { + g_enabled = on; + env_set = true; + } +} + +pub fn loadSettings(io: Io, arena: Allocator) void { + loadSettingsFrom(io, test_settings_dir orelse Io.Dir.cwd(), arena); +} + +pub fn loadSettingsFrom(io: Io, dir: Io.Dir, arena: Allocator) void { + if (cli_set or env_set) return; + const text = dir.readFileAlloc(io, settings_path, arena, .limited(1024 * 1024)) catch return; + const v = std.json.parseFromSliceLeaky(std.json.Value, arena, text, .{}) catch return; + if (v != .object) return; + const raw = v.object.get(settings_field) orelse return; + switch (raw) { + .bool => |b| g_enabled = b, + .string => |s| if (parse(s)) |b| { + g_enabled = b; + }, + else => {}, + } +} + +pub fn saveSettings(io: Io, arena: Allocator, on: bool) !void { + try saveSettingsAt(io, test_settings_dir orelse Io.Dir.cwd(), arena, on); +} + +pub fn saveSettingsAt(io: Io, dir: Io.Dir, arena: Allocator, on: bool) !void { + const existing = dir.readFileAlloc(io, settings_path, arena, .limited(1024 * 1024)) catch "{}"; + var obj: std.json.ObjectMap = .empty; + if (std.json.parseFromSliceLeaky(std.json.Value, arena, existing, .{})) |v| { + if (v == .object) obj = v.object; + } else |_| {} + try obj.put(arena, settings_field, .{ .bool = on }); + var aw: Io.Writer.Allocating = .init(arena); + var s: std.json.Stringify = .{ .writer = &aw.writer }; + try s.write(std.json.Value{ .object = obj }); + aw.writer.writeByte('\n') catch {}; + if (std.fs.path.dirname(settings_path)) |sub| dir.createDirPath(io, sub) catch {}; + try @import("credential_store.zig").replaceFile(io, dir, settings_path, aw.writer.buffered(), .default_file); + g_enabled = on; +} + +pub fn buildPrompt(io: Io, arena: Allocator, system_prompt_flag: ?[]const u8, append_flag: ?[]const u8) ![]const u8 { + var out_prompt: []const u8 = system_prompt_flag orelse system_prompt; + for ([_][]const u8{ "AGENTS.md", "HARNESS.md", "CLAUDE.md" }) |fname| { + const body = Io.Dir.cwd().readFileAlloc(io, fname, arena, .limited(64 * 1024)) catch continue; + const trimmed = std.mem.trim(u8, body, " \t\r\n"); + if (trimmed.len == 0) continue; + const capped = @import("context_limits.zig").applyFileAlloc(arena, fname, trimmed, @import("context_limits.zig").agents_md_bytes); + out_prompt = try std.fmt.allocPrint(arena, "{s}\n\n# Project instructions (from {s})\n{s}", .{ out_prompt, fname, capped }); + @import("hot_context.zig").noteBaked(fname, capped); + break; + } + if (append_flag) |extra| { + if (extra.len > 0) out_prompt = try std.fmt.allocPrint(arena, "{s}\n\n{s}", .{ out_prompt, extra }); + } + return out_prompt; +} + +pub fn applyStartupPrompt(io: Io, arena: Allocator, normal_prompt: []const u8, system_prompt_flag: ?[]const u8, append_flag: ?[]const u8) ![]const u8 { + g_saved_normal_base = normal_prompt; + g_saved_system_flag = system_prompt_flag; + g_saved_append_flag = append_flag; + if (!enabled()) return normal_prompt; + return buildPrompt(io, arena, system_prompt_flag, append_flag); +} + +pub fn applyLive(root: *Agent, arena: Allocator, on: bool, persist: bool) !bool { + if (on and no_local_tools.enabled) return error.LocalToolsDisabled; + const saved = if (persist) blk: { + saveSettings(root.io, arena, on) catch { + g_enabled = on; + break :blk false; + }; + break :blk true; + } else blk: { + g_enabled = on; + break :blk false; + }; + const next_base = if (enabled()) + try buildPrompt(root.io, root.arena, g_saved_system_flag, g_saved_append_flag) + else if (g_saved_normal_base.len > 0) + g_saved_normal_base + else if (g_saved_system_flag) |sp| + try root.arena.dupe(u8, sp) + else + try @import("prompts.zig").baseForSession(root.arena); + @import("prompt_cache_hud.zig").noteBust(.mode); + try @import("prompts.zig").setSystemPrompts(root, next_base, root.arena); + root.invalidateRootTools(); + try root.ensureRootTools(root.provider.kind); + return saved; +} + +pub fn resetForTest() void { + g_enabled = false; + cli_set = false; + env_set = false; + g_saved_normal_base = ""; + g_saved_system_flag = null; + g_saved_append_flag = null; + test_settings_dir = null; +} + +pub const SavedState = struct { + enabled: bool, + cli_set: bool, + env_set: bool, + normal_base: []const u8, + system_flag: ?[]const u8, + append_flag: ?[]const u8, + settings_dir: ?Io.Dir, + + pub fn save() SavedState { + return .{ + .enabled = g_enabled, + .cli_set = cli_set, + .env_set = env_set, + .normal_base = g_saved_normal_base, + .system_flag = g_saved_system_flag, + .append_flag = g_saved_append_flag, + .settings_dir = test_settings_dir, + }; + } + + pub fn restore(self: SavedState) void { + g_enabled = self.enabled; + cli_set = self.cli_set; + env_set = self.env_set; + g_saved_normal_base = self.normal_base; + g_saved_system_flag = self.system_flag; + g_saved_append_flag = self.append_flag; + test_settings_dir = self.settings_dir; + } +};