winx_code_agent/utils/
orchestration.rs1use std::fmt::Write as _;
2
3const DEFAULT_INSTRUCTIONS: &str = concat!(
4 "Initialize Winx once for the intended workspace. Treat structuredContent.status as ",
5 "authoritative. Never repeat a failed tool call unchanged: execute next_action and every ",
6 "required_read first. Keep the exact thread_id/workspace_root pair for the user's current ",
7 "project; never borrow one from another chat. workspace_root identifies the session, not a ",
8 "path sandbox: allowed absolute targets outside it do not require rebinding. After a SEARCH ",
9 "conflict, only ReadFiles refreshes the edit guard: execute its exact next_action, then use the ",
10 "returned revision and visible lines in one corrected EditFiles line_patch. Never fall back to ",
11 "shell, sed, or Python for an ordinary source edit. If ",
12 "edit_applied=true or status=completed_with_issues, never repeat the original edit: diagnose ",
13 "verification and make only corrective changes. If Initialize ",
14 "reports initialize_workspace_already_bound or workspace_change_requires_new_session, do not ",
15 "call Initialize again in that conversation. If a reset or mode change follows adapter state ",
16 "loss, accept the recovered first_call result and continue with its returned pair. Request ",
17 "reset_shell only after BashCommand returns a shell-runtime failure; if Winx reports ",
18 "reset_skipped_healthy, continue with the preserved PTY instead of retrying. Use CodeMap ",
19 "with a concise query to locate code, ",
20 "then batch ReadFiles for exact text. Read existing targets before editing; use one EditFiles ",
21 "call for a single change or a validated cross-file batch (each file is replaced atomically; a ",
22 "commit-time failure reports which files were already written). For an existing file read by ReadFiles, ",
23 "default to revision-bound line_patch; reserve search_replace for intentional text anchoring ",
24 "copied exactly from the current read, replace for new files or intentional whole-file rewrites, ",
25 "and undo only with the returned undo_id. Combine related ",
26 "finite checks with && or EditFiles verify_command. For ",
27 "BashCommand, follow next_action only while status is running and never auto-approve a prompt ",
28 "that changes permissions, data, or system state. If command_already_running, the new command ",
29 "did not start; status_check observes the existing execution. Run independent work with ",
30 "is_background=true and an explicit working directory; the new shell does not inherit foreground ",
31 "variables or functions. Never interrupt or reset an unrelated execution. Use ReadFiles for unsupported CodeMap ",
32 "languages; never transform source solely to make CodeMap parse it. Put useful derived helpers ",
33 "only in the temporary_artifact_dir returned by Initialize (exported as WINX_TEMP_DIR). Keep ",
34 "a small stable set: overwrite or reuse the same descriptive helper per purpose, retain ",
35 "source-path/line provenance, remove obsolete files, and treat helpers as non-canonical. Never turn ",
36 "TTL-expired helpers into durable state: Winx may reclaim them near the session quota. Never ",
37 "convert command, lint, test, or search output into a parseable carrier merely to call CodeMap. CodeMap ",
38 "on helpers accepts only an existing single file and has a smaller aggregate session budget; ",
39 "canonical source maps remain available. Never encode payload in names or create .winx-* or ",
40 ".winx_tmp artifacts at the project root."
41);
42
43const DISALLOW_INSTRUCTION: &str = "As soon as you encounter \"The user has chosen to disallow the tool call.\", immediately stop doing everything and ask the user for the reason.";
44
45pub fn server_instructions() -> String {
49 let mut instructions = format!("{DEFAULT_INSTRUCTIONS}\n\n{DISALLOW_INSTRUCTION}");
50 if let Some(extra) = crate::config::env_text("WINX_SERVER_INSTRUCTIONS") {
51 let _ = write!(instructions, "\n\nOperator instructions:\n{extra}");
52 }
53 instructions
54}
55
56pub fn append_initialize_instructions(response: &mut String) {
59 let _ = write!(response, "\n# Winx orchestration contract\n{}\n", server_instructions());
60}
61
62#[cfg(test)]
63mod tests {
64 use super::server_instructions;
65
66 #[test]
67 fn default_contract_names_structured_recovery_before_optional_operator_text() {
68 let instructions = server_instructions();
69 let prefix = instructions.chars().take(512).collect::<String>();
70 assert!(prefix.contains("structuredContent.status"));
71 assert!(prefix.contains("Never repeat a failed tool call unchanged"));
72 assert!(prefix.contains("required_read"));
73 assert!(prefix.contains("thread_id/workspace_root pair"));
74 assert!(prefix.contains("user's current project"));
75 assert!(prefix.contains("not a path sandbox"));
76 }
77
78 #[test]
79 fn default_contract_keeps_agent_artifacts_coherent() {
80 let instructions = server_instructions();
81 assert!(instructions.contains("never transform source solely"));
82 assert!(instructions.contains("Use ReadFiles"));
83 assert!(instructions.contains("temporary_artifact_dir returned by Initialize"));
84 assert!(instructions.contains("exported as WINX_TEMP_DIR"));
85 assert!(instructions.contains("helpers as non-canonical"));
86 assert!(instructions.contains("overwrite or reuse the same descriptive helper"));
87 assert!(instructions.contains("merely to call CodeMap"));
88 assert!(instructions.contains("existing single file"));
89 assert!(instructions.contains("Never encode payload in names"));
90 assert!(instructions.contains(".winx-* or .winx_tmp artifacts"));
91 assert!(instructions.contains("Winx may reclaim them near the session quota"));
92 }
93
94 #[test]
95 fn default_contract_stops_terminal_initialize_retries() {
96 let instructions = server_instructions();
97 assert!(instructions.contains("initialize_workspace_already_bound"));
98 assert!(instructions.contains("workspace_change_requires_new_session"));
99 assert!(instructions.contains("do not call Initialize again"));
100 assert!(instructions.contains("allowed absolute targets outside it"));
101 assert!(instructions.contains("reset_shell only after BashCommand"));
102 assert!(instructions.contains("reset_skipped_healthy"));
103 }
104
105 #[test]
106 fn default_contract_distinguishes_edit_conflicts_from_verification_follow_up() {
107 let instructions = server_instructions();
108 assert!(instructions.contains("only ReadFiles refreshes the edit guard"));
109 assert!(instructions.contains("one corrected EditFiles line_patch"));
110 assert!(instructions.contains("Never fall back to shell, sed, or Python"));
111 assert!(instructions.contains("default to revision-bound line_patch"));
112 assert!(instructions.contains("edit_applied=true"));
113 assert!(instructions.contains("status=completed_with_issues"));
114 assert!(instructions.contains("never repeat the original edit"));
115 }
116}