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. Use ReadFiles for unsupported CodeMap ",
29 "languages; never transform source solely to make CodeMap parse it. Put useful derived helpers ",
30 "only in the temporary_artifact_dir returned by Initialize (exported as WINX_TEMP_DIR). Keep ",
31 "a small stable set: overwrite or reuse the same descriptive helper per purpose, retain ",
32 "source-path/line provenance, remove obsolete files, and treat helpers as non-canonical. Never turn ",
33 "TTL-expired helpers into durable state: Winx may reclaim them near the session quota. Never ",
34 "convert command, lint, test, or search output into a parseable carrier merely to call CodeMap. CodeMap ",
35 "on helpers accepts only an existing single file and has a smaller aggregate session budget; ",
36 "canonical source maps remain available. Never encode payload in names or create .winx-* or ",
37 ".winx_tmp artifacts at the project root."
38);
39
40const 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.";
41
42pub fn server_instructions() -> String {
46 let mut instructions = format!("{DEFAULT_INSTRUCTIONS}\n\n{DISALLOW_INSTRUCTION}");
47 if let Some(extra) = crate::config::env_text("WINX_SERVER_INSTRUCTIONS") {
48 let _ = write!(instructions, "\n\nOperator instructions:\n{extra}");
49 }
50 instructions
51}
52
53pub fn append_initialize_instructions(response: &mut String) {
56 let _ = write!(response, "\n# Winx orchestration contract\n{}\n", server_instructions());
57}
58
59#[cfg(test)]
60mod tests {
61 use super::server_instructions;
62
63 #[test]
64 fn default_contract_names_structured_recovery_before_optional_operator_text() {
65 let instructions = server_instructions();
66 let prefix = instructions.chars().take(512).collect::<String>();
67 assert!(prefix.contains("structuredContent.status"));
68 assert!(prefix.contains("Never repeat a failed tool call unchanged"));
69 assert!(prefix.contains("required_read"));
70 assert!(prefix.contains("thread_id/workspace_root pair"));
71 assert!(prefix.contains("user's current project"));
72 assert!(prefix.contains("not a path sandbox"));
73 }
74
75 #[test]
76 fn default_contract_keeps_agent_artifacts_coherent() {
77 let instructions = server_instructions();
78 assert!(instructions.contains("never transform source solely"));
79 assert!(instructions.contains("Use ReadFiles"));
80 assert!(instructions.contains("temporary_artifact_dir returned by Initialize"));
81 assert!(instructions.contains("exported as WINX_TEMP_DIR"));
82 assert!(instructions.contains("helpers as non-canonical"));
83 assert!(instructions.contains("overwrite or reuse the same descriptive helper"));
84 assert!(instructions.contains("merely to call CodeMap"));
85 assert!(instructions.contains("existing single file"));
86 assert!(instructions.contains("Never encode payload in names"));
87 assert!(instructions.contains(".winx-* or .winx_tmp artifacts"));
88 assert!(instructions.contains("Winx may reclaim them near the session quota"));
89 }
90
91 #[test]
92 fn default_contract_stops_terminal_initialize_retries() {
93 let instructions = server_instructions();
94 assert!(instructions.contains("initialize_workspace_already_bound"));
95 assert!(instructions.contains("workspace_change_requires_new_session"));
96 assert!(instructions.contains("do not call Initialize again"));
97 assert!(instructions.contains("allowed absolute targets outside it"));
98 assert!(instructions.contains("reset_shell only after BashCommand"));
99 assert!(instructions.contains("reset_skipped_healthy"));
100 }
101
102 #[test]
103 fn default_contract_distinguishes_edit_conflicts_from_verification_follow_up() {
104 let instructions = server_instructions();
105 assert!(instructions.contains("only ReadFiles refreshes the edit guard"));
106 assert!(instructions.contains("one corrected EditFiles line_patch"));
107 assert!(instructions.contains("Never fall back to shell, sed, or Python"));
108 assert!(instructions.contains("default to revision-bound line_patch"));
109 assert!(instructions.contains("edit_applied=true"));
110 assert!(instructions.contains("status=completed_with_issues"));
111 assert!(instructions.contains("never repeat the original edit"));
112 }
113}