Skip to content

feat: propose_plan client tool and NEMAR plan-first prompt (E2.1, E2.2) - #688

Merged
neuromechanist merged 10 commits into
developfrom
feature/issue-660-propose-plan
Nov 11, 2026
Merged

neuromechanist merged 10 commits into
developfrom
feature/issue-660-propose-plan

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Summary

Backend only. The analysis model can now call propose_plan {findings, steps: [{title, detail}], est_peak_mb}. Like execute_code it parks the graph, /chat/resume returns the reader's decision, and the NEMAR prompt tells the analysis model to inspect, propose 3 to 6 steps, then run one execute_code per step (board A2, .context/designs/nemar-ai-ux/WidgetPlan.dc.html).

The widget is untouched. ClientToolController.declared still does not declare propose_plan, so the tool and the prompt text are inert in production until the plan card (E2.3) lands. Tests declare it explicitly. est_peak_mb is only a validated field; the memory budget is E5.1.

Commit 1, the tool (E2.1):

  • PROPOSE_PLAN_TOOL_NAME is reserved (RESERVED_CLIENT_TOOL_NAMES), which lowers the derived MAX_CONFIGURED_CLIENT_TOOLS by one.
  • PlanStep and ProposePlanArgs (findings at most 2000, 1 to 8 steps, title at most 120, detail at most 600, est_peak_mb int 0 to 8192). Bound by build_client_tools only with a python tool bound, the name declared and tier == "analysis", with requires_permission=False; never on quick, with no tier, or under the kill switch.
  • PlanDecision (extra forbid) and build_plan_tool_message: deterministic text with the decision, run mode, numbered steps, whether they differ from the proposal (computed on the server from the parked call), feedback, and for changes "revise the plan and call propose_plan again; run no code".
  • PendingClientCall.runs_code is False for plans and answering a plan does not spend MAX_BROWSER_RUNS_PER_REPLY (runs_after_answer).
  • ResumeRequest.result is ClientToolResult | PlanDecision. chat_resume checks, before claim_pending_call, that the answer kind matches the parked call's tool; a mismatch is a 409 and the call stays parked.

Commit 2, the prompt (E2.2):

  • CommunityConfig.plan_first_instructions, valid only with quick_model and analysis_model, not blank.
  • src/agents/plan_first.py plan_first_section(config, tier, declared), appended to the merged instructions in create_community_assistant beside QUICK_MODE_INSTRUCTIONS.
  • NEMAR's text in src/assistants/nemar/config.yaml; docs/community-widget.md documents the key and the tool; one CHANGELOG.md line.

Closes #660
Closes #661

Tests

  • tests/test_tools/test_client_tools.py: binding conditions (declared, python tool, analysis tier; absent on quick, no tier, kill switch), reserved name, schema bounds.
  • tests/test_agents/test_client_tools_graph.py: a real graph parks a propose_plan call with requires_permission false; invalid args get an error ToolMessage.
  • tests/test_api/test_propose_plan.py (new; real router, real graph, StreamingScriptedChatModel, the test_model_routing.py harness): tool_request for the plan, resume with edited steps and run_mode reaching the model and the session history, second resume 409, changes_requested leading to a second plan, code result for a plan call 409 and decision for a code call 409 (both stay parked), 20 plans leave the browser-run budget intact (fails if runs_after_answer counts plans), undeclared means no tool bound, and the prompt section present or absent per tier, declaration and kill switch.
  • tests/test_agents/test_plan_first.py, tests/test_api/test_tool_results.py, tests/test_core/test_config/test_community.py: the section, PlanDecision, the message text and the config validation. No LLM-marked test (E2.5).

Commands run

  • uv run pytest tests/test_tools/test_client_tools.py tests/test_agents tests/test_api/test_propose_plan.py tests/test_api/test_chat_resume_endpoint.py tests/test_api/test_model_routing.py tests/test_api/test_community_router.py tests/test_core/test_config tests/test_assistants tests/test_frontend -q: 1613 passed, 8 skipped.
  • uv run pytest --cov -q: 5625 passed, 91 skipped, 3 failed, total coverage 85%. The 3 failures are live-network tests unrelated to this change: tests/test_tools/test_hed_suggest.py::test_real_program_suggests_a_schema_tag and two tests/test_tools/test_mcp_client.py::TestAgainstProductionNemar tests (the production NEMAR server answered with a D1 error).
  • uv run ruff check . && uv run ruff format --check .: all checks passed, 291 files formatted.

Add the derived propose_plan client tool (analysis tier, python tool bound,
declared by the caller), PlanDecision and build_plan_tool_message for
/chat/resume, a 409 when the answer kind does not match the parked call, and
keep plan answers out of the browser-run budget.

Tested: tool binding, schema bounds, real graph parking, and the real app
through /chat and /chat/resume (edit, approve, changes, mismatch, budget).
Add CommunityConfig.plan_first_instructions (valid only with quick_model and
analysis_model), plan_first_section, and append it to an analysis turn that
declares propose_plan. NEMAR sets the text; docs and changelog updated.
The widget still does not declare propose_plan, so this is inert in production.

Tested: config validation, section present only for analysis plus declared
plus configured, absent on quick, undeclared and kill switch, and through
the real app.
…lan test bounds from the MAX_PLAN constants (tested)
… tie NEMAR plan tool names to the prompt ladder; note runsCode rule for #662 (tested)
@neuromechanist

Copy link
Copy Markdown
Member Author

Review round 1: outcome for each finding. Fix commits are on this branch (4c9b073 through 4681df8). Tests: the full suite passes (5637) except test_hed_suggest.py::test_real_program_suggests_a_schema_tag, which calls an external HED service and fails the same way without this change; test_llm_service.py does not collect on Python 3.14 locally and was left out. ruff check and ruff format --check are clean.

  • runs_code docstring versus the widget's runsCode rule (tool_results.py): fixed. The docstring now states that the widget's current rule differs for propose_plan, that this is inert only while the widget never declares the tool, and that the plan-card change (Widget: editable plan card with run mode #662) must exclude the plan tool from runsCode. Added test_a_decided_plan_is_not_code_either in test_cut_off_replies.py: a reply that only answered a plan and then wrote nothing is an error. I did not comment on Widget: editable plan card with run mode #662 itself, since that is outside this PR; the docstring carries the requirement.
  • plan_first_instructions missing from .context/yaml_registry.md: fixed, row added with its validity rules and pointers.
  • PlanStep.title accepts blank text and newlines: fixed. A validator folds every whitespace run to one space and rejects a blank title; PlanDecision.steps reuses PlanStep, so proposed and edited steps stay comparable. Added rejection cases to test_client_tools.py and test_tool_results.py, plus a test that a newline in a title cannot add a Plan decision: line.
  • Decision message built twice (community.py): fixed, built once and used for both the live and stored lists.
  • Plan refused as "already run code" when out of browser runs (base.py): fixed by giving the refusal a plan-specific text rather than exempting plans. A plan answered after exhaustion could not be followed, since every step would be refused, so refusing stays right; the comment says so. Graph tests cover both wordings.
  • Unanswered plan abandoned with "Browser execution did not complete": fixed. build_unanswered_tool_message takes the tool name, and abandon_pending_call passes it; for propose_plan the text says the reader did not decide, the plan was not approved, run no code, and propose again or ask. Unit tests plus an endpoint test (TestALeftPlan).
  • No handover-path test: fixed. A quick session handing over to a new analysis session is tested for propose_plan bound and PLAN_TEXT in the new session's prompt (and absent from the quick summary turn). Confirmed it fails when declared_client_tools is dropped from run_on.
  • Hardcoded tool names and bounds in new tests: fixed. Bounds are derived from MAX_PLAN_* and PROPOSE_PLAN_TOOL_NAME is imported in the modules that hardcoded it. NEMAR's tools come from a live MCP server, so there is no static listing to resolve against; the test now extracts every nemar_* name from the plan-first text and requires each to be in the system prompt's tool ladder, which test_nemar_mcp_wiring.py ties to the live server.

@neuromechanist

Copy link
Copy Markdown
Member Author

Review round 2 outcomes:

  • Plan step detail can add lines to the tool message (src/api/tool_results.py): fixed. build_plan_tool_message now renders each step on one line (" ".join(detail.split())); the comparison with the proposal still uses the raw values. New test test_a_detail_cannot_add_a_line_to_the_message covers forged Plan decision:, Run mode: and Reader feedback: lines.
  • abandon_pending_call reason says "before it was run" for a plan (src/api/routers/community.py): fixed. The left-call reason is now "the conversation moved on before it was answered"; TestALeftPlan asserts the new wording. The "replaced by a newer browser call" reason at the other call site is already call-agnostic and is unchanged.

Tested: tests/test_api and tests/test_integration/test_client_tool_round_trip.py (1557 passed, 6 skipped); ruff check and ruff format --check clean.

@neuromechanist

Copy link
Copy Markdown
Member Author

PM verdict: ready to merge. Acceptance criteria are covered by tests in test_client_tools.py, test_client_tools_graph.py, test_propose_plan.py (new) and test_plan_first.py (new). Both review rounds have a recorded outcome for every finding. All checks pass (Lint, Test 3.11 and 3.12, Frontend Tests, Docker, notebook site, sync-cors), and the branch is up to date with develop. Squash merging.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant