Skip to main content

winx_code_agent/utils/
orchestration.rs

1use 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 an edit ",
9    "conflict, only ReadFiles refreshes the edit guard; rebuild SEARCH from its returned text. If ",
10    "edit_applied=true or status=completed_with_issues, never repeat the original edit: diagnose ",
11    "verification and make only corrective changes. If Initialize ",
12    "reports initialize_workspace_already_bound or workspace_change_requires_new_session, do not ",
13    "call Initialize again in that conversation. If a reset or mode change follows adapter state ",
14    "loss, accept the recovered first_call result and continue with its returned pair. Use CodeMap ",
15    "with a concise query to locate code, ",
16    "then batch ReadFiles for exact text. Read targets before editing; use MultiFileEdit for atomic ",
17    "cross-file changes. Combine related finite checks with && or edit verify_command. For ",
18    "BashCommand, follow next_action only while status is running and never auto-approve a prompt ",
19    "that changes permissions, data, or system state. Use ReadFiles for unsupported CodeMap ",
20    "languages; never transform source solely to make CodeMap parse it. Put useful derived helpers ",
21    "only in the temporary_artifact_dir returned by Initialize (exported as WINX_TEMP_DIR). Keep ",
22    "a small stable set: overwrite or reuse the same descriptive helper per purpose, retain ",
23    "source-path/line provenance, remove obsolete files, and treat helpers as non-canonical. Never turn ",
24    "command, lint, test, or search output into a parseable carrier merely to call CodeMap. CodeMap ",
25    "on helpers accepts only an existing single file and has a smaller aggregate session budget; ",
26    "canonical source maps remain available. Never encode payload in names or create .winx-* or ",
27    ".winx_tmp artifacts at the project root."
28);
29
30const 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.";
31
32/// Instructions advertised in the MCP handshake. The stable defaults come
33/// first so clients that truncate this field still receive the orchestration
34/// contract; operator-provided rules are appended without requiring a rebuild.
35pub fn server_instructions() -> String {
36    let mut instructions = format!("{DEFAULT_INSTRUCTIONS}\n\n{DISALLOW_INSTRUCTION}");
37    if let Some(extra) = crate::config::env_text("WINX_SERVER_INSTRUCTIONS") {
38        let _ = write!(instructions, "\n\nOperator instructions:\n{extra}");
39    }
40    instructions
41}
42
43/// Mirror the handshake contract in Initialize output for older clients that
44/// do not surface ServerInfo.instructions to the model.
45pub fn append_initialize_instructions(response: &mut String) {
46    let _ = write!(response, "\n# Winx orchestration contract\n{}\n", server_instructions());
47}
48
49#[cfg(test)]
50mod tests {
51    use super::server_instructions;
52
53    #[test]
54    fn default_contract_names_structured_recovery_before_optional_operator_text() {
55        let instructions = server_instructions();
56        let prefix = instructions.chars().take(512).collect::<String>();
57        assert!(prefix.contains("structuredContent.status"));
58        assert!(prefix.contains("Never repeat a failed tool call unchanged"));
59        assert!(prefix.contains("required_read"));
60        assert!(prefix.contains("thread_id/workspace_root pair"));
61        assert!(prefix.contains("user's current project"));
62        assert!(prefix.contains("not a path sandbox"));
63    }
64
65    #[test]
66    fn default_contract_keeps_agent_artifacts_coherent() {
67        let instructions = server_instructions();
68        assert!(instructions.contains("never transform source solely"));
69        assert!(instructions.contains("Use ReadFiles"));
70        assert!(instructions.contains("temporary_artifact_dir returned by Initialize"));
71        assert!(instructions.contains("exported as WINX_TEMP_DIR"));
72        assert!(instructions.contains("helpers as non-canonical"));
73        assert!(instructions.contains("overwrite or reuse the same descriptive helper"));
74        assert!(instructions.contains("merely to call CodeMap"));
75        assert!(instructions.contains("existing single file"));
76        assert!(instructions.contains("Never encode payload in names"));
77        assert!(instructions.contains(".winx-* or .winx_tmp artifacts"));
78    }
79
80    #[test]
81    fn default_contract_stops_terminal_initialize_retries() {
82        let instructions = server_instructions();
83        assert!(instructions.contains("initialize_workspace_already_bound"));
84        assert!(instructions.contains("workspace_change_requires_new_session"));
85        assert!(instructions.contains("do not call Initialize again"));
86        assert!(instructions.contains("allowed absolute targets outside it"));
87    }
88
89    #[test]
90    fn default_contract_distinguishes_edit_conflicts_from_verification_follow_up() {
91        let instructions = server_instructions();
92        assert!(instructions.contains("only ReadFiles refreshes the edit guard"));
93        assert!(instructions.contains("edit_applied=true"));
94        assert!(instructions.contains("status=completed_with_issues"));
95        assert!(instructions.contains("never repeat the original edit"));
96    }
97}