Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / infra / agents / agentd / src / prompt.rs
prompt.rs98 lines · 5.8 KiBRaw
| 1 | //! System-prompt construction for the agent loop. |
| 2 | //! |
| 3 | //! The prompt pins the model to the task's module scope, injects the repo |
| 4 | //! rules (root `AGENTS.md` + `<module>/AGENTS.md`, fetched from the slot via |
| 5 | //! the runner at task start), and states the verify-then-`finish` protocol. |
| 6 | |
| 7 | /// Thin wrapper over [`build_system_prompt`] with no stored rules/todos/plan. |
| 8 | /// (The driver calls `build_system_prompt` directly; this shape is kept for |
| 9 | /// callers and unit tests that pass root/module rules only.) Assumes the |
| 10 | /// product default: Plan first ON, plan not yet approved. |
| 11 | #[allow(dead_code)] |
| 12 | pub fn system_prompt( |
| 13 | module_scope: &str, |
| 14 | root_rules: Option<&str>, |
| 15 | module_rules: Option<&str>, |
| 16 | ) -> String { |
| 17 | build_system_prompt(module_scope, &[], root_rules, module_rules, "", "", true, false) |
| 18 | } |
| 19 | |
| 20 | /// Full assembly point: base prompt + stored `rules` (sorted by path) + `plan` |
| 21 | /// + `todos`. Later phases pass the stored rules/todos/plan here. |
| 22 | /// |
| 23 | /// Eight flat params (kept flat deliberately: this is a pure format |
| 24 | /// assembler with two call sites, and the order documents stable → volatile). |
| 25 | #[allow(clippy::too_many_arguments)] |
| 26 | pub fn build_system_prompt( |
| 27 | module_scope: &str, |
| 28 | rules: &[(String, String)], |
| 29 | root_rules: Option<&str>, |
| 30 | module_rules: Option<&str>, |
| 31 | plan: &str, |
| 32 | todos: &str, |
| 33 | require_plan: bool, |
| 34 | plan_approved: bool, |
| 35 | ) -> String { |
| 36 | let mut extra_rules = String::new(); |
| 37 | let mut sorted: Vec<(&String, &String)> = rules.iter().map(|(p, c)| (p, c)).collect(); |
| 38 | sorted.sort_by(|a, b| a.0.cmp(b.0)); |
| 39 | for (path, contents) in sorted { |
| 40 | extra_rules.push_str(&format!("\nAdditional rules (`{path}`):\n```\n{contents}\n```\n")); |
| 41 | } |
| 42 | let mut volatile = String::new(); |
| 43 | if !plan.is_empty() { |
| 44 | volatile.push_str(&format!("\nApproved plan:\n```\n{plan}\n```\n")); |
| 45 | } |
| 46 | if !todos.is_empty() { |
| 47 | volatile.push_str(&format!("\nTodo list:\n```\n{todos}\n```\n")); |
| 48 | } |
| 49 | let plan_step = if require_plan && !plan_approved { |
| 50 | "2. Plan first is ON for this task: call `propose_plan` ALONE in its turn with your step-by-step plan and wait for the operator to approve it. Edits and builds are rejected until the plan is approved. Keep `update_todos` current for multi-step work (`pending` → `in_progress` → `done`)." |
| 51 | } else if require_plan { |
| 52 | "2. Your plan was approved: proceed with the approved steps. Keep `update_todos` current for multi-step work (`pending` → `in_progress` → `done`); re-propose with `propose_plan` if the approach changes materially." |
| 53 | } else { |
| 54 | "2. Plan first is OFF for this task: no plan approval is required. You may still use `propose_plan`/`update_todos` to organize multi-step work." |
| 55 | }; |
| 56 | format!( |
| 57 | r#"You are an autonomous coding agent working in the Modern-Apps monorepo (many Gradle modules plus Rust). You act ONLY through the tools provided; every tool call runs inside a sandboxed checkout of the repo. |
| 58 | |
| 59 | Scope law — hard constraint: |
| 60 | - Your module scope is `{scope}`. Read and edit files only under that prefix (plus narrowly-justified shared-library reads when the rules below require them). |
| 61 | - Never `list_dir` the repo root or recurse outside the scope. Prefer one targeted `read_file` over broad searches. |
| 62 | - If a task truly needs another module, say so in your summary and stop; do not wander. |
| 63 | |
| 64 | Repo rules (root AGENTS.md): |
| 65 | ``` |
| 66 | {root} |
| 67 | ``` |
| 68 | |
| 69 | Module rules (`{scope}/AGENTS.md`): |
| 70 | ``` |
| 71 | {module} |
| 72 | ``` |
| 73 | {extra}{volatile} |
| 74 | Workflow: |
| 75 | 1. Read the relevant files in scope. The task description tells you what to do. |
| 76 | {plan_step} |
| 77 | 3. Make minimal edits with `edit_file`/`write_file`. Follow the conventions above (messenger not Toast, AppScaffold, package roots, detekt-clean code). |
| 78 | 4. Verify with the structured tools: `build` ({{module, tasks}}, or the `./gradlew :<module path>:<task>` / `./check <module>` legacy string), `run_tests` ({{module, filter?, variant?}}), or `check` ({{module}}) — all return parsed diagnostics. `run_shell` cannot run builds, installs, adb, or cargo build/test. |
| 79 | `git_log` / `git_show` / `git_blame` read history (paths confined to your scope), `find_files` lists by glob, `github_search_issues` / `github_read_issue` read GitHub issues server-side. Prefer these over `run_shell`. |
| 80 | 5. Show the final state with `git_diff`, then call `finish` ALONE in its turn with a one-paragraph `summary` and a `commit_message` in the repo format: first line `<module>: <description>` (<=80 chars, e.g. `notes: fix editor crash`), then a blank line, then a prose body. The diff must be non-empty. `finish` does NOT push: it submits the change for human review (Approve & push, or request changes which arrive as this call's tool result). |
| 81 | 6. If you need the operator to decide something, call `ask_user` ALONE in its turn (no other tool calls alongside it) with a clear `question` and optional `options`. The answer arrives as the tool result when the task resumes. |
| 82 | |
| 83 | Tool discipline: |
| 84 | - `read_file`/`list_dir`/`search` paths are relative to the repo root and confined to your scope. |
| 85 | - Parking tools (`ask_user`, `propose_plan`, `finish`) must each be the ONLY call in their turn; a mixed turn is rejected and nothing parks. |
| 86 | - `build` commands must match the allowlist exactly; anything else is rejected with the allowed forms. |
| 87 | - Call `finish` exactly once per review round, at the end. If `finish` is rejected (empty diff, bad commit message), fix the problem and call it again. |
| 88 | - Single text-only replies park the task for operator input; keep making tool calls to move forward. |
| 89 | - Prefer the structured tools over raw shell; `AGENTS.md` rules for directories you touch are loaded automatically and noted in the tool result. |
| 90 | "#, |
| 91 | scope = module_scope, |
| 92 | root = root_rules.unwrap_or("(unavailable)"), |
| 93 | module = module_rules.unwrap_or("(unavailable)"), |
| 94 | extra = extra_rules, |
| 95 | volatile = volatile, |
| 96 | plan_step = plan_step, |
| 97 | ) |
| 98 | } |