cflx 0.6.327

Conflux – a spec-driven parallel coding orchestrator that runs AI agents on git worktrees
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
//! Configuration templates for different AI agents

use crate::cli::Template;
use crate::config::OrchestratorConfig;

/// Claude Code agent configuration template
pub const CLAUDE_TEMPLATE: &str = r#"{
  // Conflux Configuration
  // Template: Claude Code

  // Command to analyze dependencies and select next change
  "analyze_command": "claude --dangerously-skip-permissions --verbose --output-format stream-json -p {prompt}",

  // Command to apply a change (supports {change_id} and {prompt} placeholders)
  "apply_command": "claude --dangerously-skip-permissions --verbose --output-format stream-json -p '{prompt}'",

  // Command to archive a completed change (supports {change_id} and {prompt} placeholders)
  "archive_command": "claude --dangerously-skip-permissions --verbose --output-format stream-json -p '{prompt}'",

  // Command to run acceptance tests after apply (supports {change_id} and {prompt} placeholders)
  "acceptance_command": "claude --dangerously-skip-permissions --verbose --output-format stream-json -p '{prompt}'",

  // Operation skill for acceptance prompts (defaults to cflx-accept).
  // Opt into SPECA-style property review without changing acceptance_command:
  // "accept_skill": "cflx-accept-with-speca",

  // Command to resolve conflicts (supports {prompt} placeholder)
  "resolve_command": "claude --dangerously-skip-permissions --verbose --output-format stream-json -p {prompt}",

  // Operation skill preludes loaded into generated prompts (all optional; shown with defaults)
  // Example: set accept_skill to "cflx-accept-with-speca" to customize acceptance guidance only.
  "analyze_skill": "cflx-analyze",
  "apply_skill": "cflx-apply",
  "rejecting_skill": "cflx-rejecting",
  "cleanup_review_skill": "cflx-cleanup-review",
  "accept_skill": "cflx-accept",
  // "accept_skill": "cflx-accept-with-speca",
  "archive_skill": "cflx-archive",
  "resolve_skill": "cflx-resolve",

  // System prompt for apply command (user-customizable, injected into {prompt} placeholder)
  // Note: A hardcoded system prompt is always appended after this value
  //"apply_prompt": "",

  // System prompt for archive command (injected into {prompt} placeholder)
  //"archive_prompt": "",

  // System prompt for acceptance command (injected into {prompt} placeholder)
  // Note: A hardcoded acceptance prompt is always prepended before this value
  //"acceptance_prompt": "",

  // Optional raw append prompts. These are added after the final generated prompt and are inactive by default.
  //"apply_append_prompt": "",
  //"acceptance_append_prompt": "",
  //"archive_append_prompt": "",
  //"analyze_append_prompt": "",
  //"resolve_append_prompt": "",

   // Worktree command for TUI (+ key)
  // Supports {workspace_dir} and {repo_root} placeholders
  "worktree_command": "tmux new-window -n wt ",
  // "worktree_command": "codex '/openspec:proposal --worktree {workspace_dir}'",

  // Storage roots (both optional, both absolute paths, empty string = unset).
  // workspace_base_dir holds managed Git worktrees; state_base_dir holds
  // Conflux-owned persistent state, i.e. logs at
  // <state_base_dir>/cflx/logs/<project_slug>/<YYYY-MM-DD>.log.
  // state_base_dir takes precedence over XDG_STATE_HOME for Conflux paths only:
  // child commands keep the XDG_STATE_HOME they inherited. An invalid or
  // unwritable root fails startup instead of falling back to the internal disk.
  // Existing worktrees and logs are never migrated or cleaned up for you.
  // "workspace_base_dir": "/Volumes/BigDisk/cflx/worktrees",
  // "state_base_dir": "/Volumes/BigDisk/cflx/state",

  // Maximum iterations for the orchestration loop (default: 50, 0 = no limit)
  // "max_iterations": 50,

  // Absolute runtime limit for one AI command invocation (seconds).
  // Default: 10800 (3 hours). Measured from child spawn and never extended by
  // output; 0 disables it.
  // "command_max_runtime_secs": 10800,

  // Absolute runtime limit for one acceptance invocation (seconds).
  // Default: 1800 (30 minutes). Acceptance reviews an implementation rather than
  // producing one, so it carries its own shorter deadline. Measured from child
  // spawn and never extended by output. Accepts 300 through 10800; 0 is rejected
  // because acceptance cannot disable its wall-clock guard. When
  // command_max_runtime_secs is a positive value the shorter of the two applies.
  // "acceptance_max_runtime_secs": 1800,

  // Optional purpose-scoped judge command (non-authoritative shadow observer).
  // Omitting judge_commands is the normal case: no judge process starts and
  // dependency analysis behaves exactly as before. `mode` accepts only
  // "shadow". `evaluation` is separately opt-in durable evidence, written to
  // <state_base_dir>/cflx/evaluations — a sibling of logs, never visible to
  // `cflx logs`, and always safe to delete. Read it with
  // `cflx judge-eval summary --purpose parallel_dependency`.
  // "judge_commands": {
  //   "parallel_dependency": {
  //     "command": ["jev", "run", "-"],
  //     "model": "jev-1.13.0",
  //     "yes_threshold": 0.85,
  //     "mode": "shadow",
  //     "evaluation": {
  //       "enabled": false,
  //       "retention_days": 30,
  //       "max_total_bytes": 104857600
  //     }
  //   }
  // },

  // Logging configuration for TUI debug output
  // "logging": {
  //   "suppress_repetitive_debug": true,
  //   "summary_interval_secs": 60
  // },

  // Lifecycle hooks (optional)
  // Available hooks:
  //   Run lifecycle: on_start, on_finish, on_error
  //   Change lifecycle: on_change_start, pre_apply, post_apply, on_change_complete, pre_archive, post_archive, on_change_end, on_merged
  //   Operator interaction (any frontend): on_queue_add, on_queue_remove
  // Available placeholders: {change_id}, {changes_processed}, {total_changes}, {remaining_changes}, {apply_count}, {completed_tasks}, {total_tasks}
  "hooks": {
    // Run lifecycle
    // "on_start": "echo '[on_start] changes_processed={changes_processed} total={total_changes} remaining={remaining_changes}'",
    // "on_finish": "echo '[on_finish] status={status} processed={changes_processed}/{total_changes}'",
    // "on_error": "echo '[on_error] change={change_id} error={error}'",

    // Change lifecycle
    // "on_change_start": "echo '[on_change_start] change={change_id} tasks={completed_tasks}/{total_tasks} progress={changes_processed}/{total_changes}'",
    // "pre_apply": "echo '[pre_apply] change={change_id} apply_count={apply_count} tasks={completed_tasks}/{total_tasks}'",
    // "post_apply": "echo '[post_apply] change={change_id} apply_count={apply_count} tasks={completed_tasks}/{total_tasks}'",
    // "on_change_complete": "echo '[on_change_complete] change={change_id} tasks={total_tasks}/{total_tasks}'",
    // "pre_archive": "echo '[pre_archive] change={change_id} progress={changes_processed}/{total_changes}'",
    // "post_archive": "echo '[post_archive] change={change_id} progress={changes_processed}/{total_changes}'",
    // "on_change_end": "echo '[on_change_end] change={change_id} progress={changes_processed}/{total_changes} remaining={remaining_changes}'",
    // "on_merged": "echo '[on_merged] change={change_id} tasks={completed_tasks}/{total_tasks} progress={changes_processed}/{total_changes}'",

    // TUI interaction
    // "on_queue_add": "echo '[on_queue_add] change={change_id} tasks={completed_tasks}/{total_tasks}'",
    // "on_queue_remove": "echo '[on_queue_remove] change={change_id} tasks={completed_tasks}/{total_tasks}'"
  }
}
"#;

/// OpenCode agent configuration template
pub const OPENCODE_TEMPLATE: &str = r#"{
  // Conflux Configuration
  // Template: OpenCode

  // Command to analyze dependencies and select next change
  "analyze_command": "opencode run --format json {prompt}",

  // Command to apply a change (supports {change_id} and {prompt} placeholders)
  "apply_command": "opencode run '{prompt}'",

  // Command to archive a completed change (supports {change_id} and {prompt} placeholders)
  "archive_command": "opencode run '{prompt}'",

  // Command to run acceptance tests after apply (supports {change_id} and {prompt} placeholders)
  "acceptance_command": "opencode run '{prompt}'",

  // Operation skill for acceptance prompts (defaults to cflx-accept).
  // Opt into SPECA-style property review without changing acceptance_command:
  // "accept_skill": "cflx-accept-with-speca",

  // Command to resolve conflicts (supports {prompt} placeholder)
  "resolve_command": "opencode run {prompt}",

  // Operation skill preludes loaded into generated prompts (all optional; shown with defaults)
  // Example: set accept_skill to "cflx-accept-with-speca" to customize acceptance guidance only.
  "analyze_skill": "cflx-analyze",
  "apply_skill": "cflx-apply",
  "rejecting_skill": "cflx-rejecting",
  "cleanup_review_skill": "cflx-cleanup-review",
  "accept_skill": "cflx-accept",
  // "accept_skill": "cflx-accept-with-speca",
  "archive_skill": "cflx-archive",
  "resolve_skill": "cflx-resolve",

  // System prompt for apply command (user-customizable, injected into {prompt} placeholder)
  // Note: A hardcoded system prompt is always appended after this value
  //"apply_prompt": "",

  // System prompt for archive command (injected into {prompt} placeholder)
  //"archive_prompt": "",

  // System prompt for acceptance command (injected into {prompt} placeholder)
  // Note: A hardcoded acceptance prompt is always prepended before this value
  //"acceptance_prompt": "",

  // Optional raw append prompts. These are added after the final generated prompt and are inactive by default.
  //"apply_append_prompt": "",
  //"acceptance_append_prompt": "",
  //"archive_append_prompt": "",
  //"analyze_append_prompt": "",
  //"resolve_append_prompt": "",

  // Worktree command for TUI (+ key)
  // Supports {workspace_dir} and {repo_root} placeholders
  "worktree_command": "tmux new-window -n wt ",
  // "worktree_command": "claude run '/openspec:proposal --worktree {workspace_dir}'",

  // Storage roots (both optional, both absolute paths, empty string = unset).
  // workspace_base_dir holds managed Git worktrees; state_base_dir holds
  // Conflux-owned persistent state, i.e. logs at
  // <state_base_dir>/cflx/logs/<project_slug>/<YYYY-MM-DD>.log.
  // state_base_dir takes precedence over XDG_STATE_HOME for Conflux paths only:
  // child commands keep the XDG_STATE_HOME they inherited. An invalid or
  // unwritable root fails startup instead of falling back to the internal disk.
  // Existing worktrees and logs are never migrated or cleaned up for you.
  // "workspace_base_dir": "/Volumes/BigDisk/cflx/worktrees",
  // "state_base_dir": "/Volumes/BigDisk/cflx/state",

  // Maximum iterations for the orchestration loop (default: 50, 0 = no limit)
  // "max_iterations": 50,

  // Absolute runtime limit for one AI command invocation (seconds).
  // Default: 10800 (3 hours). Measured from child spawn and never extended by
  // output; 0 disables it.
  // "command_max_runtime_secs": 10800,

  // Absolute runtime limit for one acceptance invocation (seconds).
  // Default: 1800 (30 minutes). Acceptance reviews an implementation rather than
  // producing one, so it carries its own shorter deadline. Measured from child
  // spawn and never extended by output. Accepts 300 through 10800; 0 is rejected
  // because acceptance cannot disable its wall-clock guard. When
  // command_max_runtime_secs is a positive value the shorter of the two applies.
  // "acceptance_max_runtime_secs": 1800,

  // Optional purpose-scoped judge command (non-authoritative shadow observer).
  // Omitting judge_commands is the normal case: no judge process starts and
  // dependency analysis behaves exactly as before. `mode` accepts only
  // "shadow". `evaluation` is separately opt-in durable evidence, written to
  // <state_base_dir>/cflx/evaluations — a sibling of logs, never visible to
  // `cflx logs`, and always safe to delete. Read it with
  // `cflx judge-eval summary --purpose parallel_dependency`.
  // "judge_commands": {
  //   "parallel_dependency": {
  //     "command": ["jev", "run", "-"],
  //     "model": "jev-1.13.0",
  //     "yes_threshold": 0.85,
  //     "mode": "shadow",
  //     "evaluation": {
  //       "enabled": false,
  //       "retention_days": 30,
  //       "max_total_bytes": 104857600
  //     }
  //   }
  // },

  // Logging configuration for TUI debug output
  // "logging": {
  //   "suppress_repetitive_debug": true,
  //   "summary_interval_secs": 60
  // },

  // Lifecycle hooks (optional)
  // Available hooks:
  //   Run lifecycle: on_start, on_finish, on_error
  //   Change lifecycle: on_change_start, pre_apply, post_apply, on_change_complete, pre_archive, post_archive, on_change_end, on_merged
  //   Operator interaction (any frontend): on_queue_add, on_queue_remove
  // Available placeholders: {change_id}, {changes_processed}, {total_changes}, {remaining_changes}, {apply_count}, {completed_tasks}, {total_tasks}
  "hooks": {
    // Run lifecycle
    // "on_start": "echo '[on_start] changes_processed={changes_processed} total={total_changes} remaining={remaining_changes}'",
    // "on_finish": "echo '[on_finish] status={status} processed={changes_processed}/{total_changes}'",
    // "on_error": "echo '[on_error] change={change_id} error={error}'",

    // Change lifecycle
    // "on_change_start": "echo '[on_change_start] change={change_id} tasks={completed_tasks}/{total_tasks} progress={changes_processed}/{total_changes}'",
    // "pre_apply": "echo '[pre_apply] change={change_id} apply_count={apply_count} tasks={completed_tasks}/{total_tasks}'",
    // "post_apply": "echo '[post_apply] change={change_id} apply_count={apply_count} tasks={completed_tasks}/{total_tasks}'",
    // "on_change_complete": "echo '[on_change_complete] change={change_id} tasks={total_tasks}/{total_tasks}'",
    // "pre_archive": "echo '[pre_archive] change={change_id} progress={changes_processed}/{total_changes}'",
    // "post_archive": "echo '[post_archive] change={change_id} progress={changes_processed}/{total_changes}'",
    // "on_change_end": "echo '[on_change_end] change={change_id} progress={changes_processed}/{total_changes} remaining={remaining_changes}'",
    // "on_merged": "echo '[on_merged] change={change_id} tasks={completed_tasks}/{total_tasks} progress={changes_processed}/{total_changes}'",

    // TUI interaction
    // "on_queue_add": "echo '[on_queue_add] change={change_id} tasks={completed_tasks}/{total_tasks}'",
    // "on_queue_remove": "echo '[on_queue_remove] change={change_id} tasks={completed_tasks}/{total_tasks}'"
  }
}
"#;

/// Codex agent configuration template
pub const CODEX_TEMPLATE: &str = r#"{
  // Conflux Configuration
  // Template: Codex

  // Command to analyze dependencies and select next change
  "analyze_command": "codex -a never exec --json --sandbox workspace-write '{prompt}'",

  // Command to apply a change (supports {change_id} and {prompt} placeholders)
  "apply_command": "codex -a never exec --sandbox workspace-write '{prompt}'",

  // Command to archive a completed change (supports {change_id} and {prompt} placeholders)
  "archive_command": "codex -a never exec --sandbox workspace-write '{prompt}'",

  // Command to run acceptance tests after apply (supports {change_id} and {prompt} placeholders)
  "acceptance_command": "codex -a never exec --sandbox workspace-write '{prompt}'",

  // Operation skill for acceptance prompts (defaults to cflx-accept).
  // Opt into SPECA-style property review without changing acceptance_command:
  // "accept_skill": "cflx-accept-with-speca",

  // Command to resolve conflicts (supports {prompt} placeholder)
  "resolve_command": "codex -a never exec --sandbox workspace-write '{prompt}'",

  // Operation skill preludes loaded into generated prompts (all optional; shown with defaults)
  // Example: set accept_skill to "cflx-accept-with-speca" to customize acceptance guidance only.
  "analyze_skill": "cflx-analyze",
  "apply_skill": "cflx-apply",
  "rejecting_skill": "cflx-rejecting",
  "cleanup_review_skill": "cflx-cleanup-review",
  "accept_skill": "cflx-accept",
  // "accept_skill": "cflx-accept-with-speca",
  "archive_skill": "cflx-archive",
  "resolve_skill": "cflx-resolve",

  // System prompt for apply command (user-customizable, injected into {prompt} placeholder)
  // Note: A hardcoded system prompt is always appended after this value
  //"apply_prompt": "",

  // System prompt for archive command (injected into {prompt} placeholder)
  //"archive_prompt": "",

  // System prompt for acceptance command (injected into {prompt} placeholder)
  // Note: A hardcoded acceptance prompt is always prepended before this value
  //"acceptance_prompt": "",

  // Optional raw append prompts. These are added after the final generated prompt and are inactive by default.
  //"apply_append_prompt": "",
  //"acceptance_append_prompt": "",
  //"archive_append_prompt": "",
  //"analyze_append_prompt": "",
  //"resolve_append_prompt": "",

  // Worktree command for TUI (+ key)
  // Supports {workspace_dir} and {repo_root} placeholders
  "worktree_command": "tmux new-window -n wt ",
  // "worktree_command": "opencode run '/openspec:proposal --worktree {workspace_dir}'",

  // Storage roots (both optional, both absolute paths, empty string = unset).
  // workspace_base_dir holds managed Git worktrees; state_base_dir holds
  // Conflux-owned persistent state, i.e. logs at
  // <state_base_dir>/cflx/logs/<project_slug>/<YYYY-MM-DD>.log.
  // state_base_dir takes precedence over XDG_STATE_HOME for Conflux paths only:
  // child commands keep the XDG_STATE_HOME they inherited. An invalid or
  // unwritable root fails startup instead of falling back to the internal disk.
  // Existing worktrees and logs are never migrated or cleaned up for you.
  // "workspace_base_dir": "/Volumes/BigDisk/cflx/worktrees",
  // "state_base_dir": "/Volumes/BigDisk/cflx/state",

  // Maximum iterations for the orchestration loop (default: 50, 0 = no limit)
  // "max_iterations": 50,

  // Absolute runtime limit for one AI command invocation (seconds).
  // Default: 10800 (3 hours). Measured from child spawn and never extended by
  // output; 0 disables it.
  // "command_max_runtime_secs": 10800,

  // Absolute runtime limit for one acceptance invocation (seconds).
  // Default: 1800 (30 minutes). Acceptance reviews an implementation rather than
  // producing one, so it carries its own shorter deadline. Measured from child
  // spawn and never extended by output. Accepts 300 through 10800; 0 is rejected
  // because acceptance cannot disable its wall-clock guard. When
  // command_max_runtime_secs is a positive value the shorter of the two applies.
  // "acceptance_max_runtime_secs": 1800,

  // Optional purpose-scoped judge command (non-authoritative shadow observer).
  // Omitting judge_commands is the normal case: no judge process starts and
  // dependency analysis behaves exactly as before. `mode` accepts only
  // "shadow". `evaluation` is separately opt-in durable evidence, written to
  // <state_base_dir>/cflx/evaluations — a sibling of logs, never visible to
  // `cflx logs`, and always safe to delete. Read it with
  // `cflx judge-eval summary --purpose parallel_dependency`.
  // "judge_commands": {
  //   "parallel_dependency": {
  //     "command": ["jev", "run", "-"],
  //     "model": "jev-1.13.0",
  //     "yes_threshold": 0.85,
  //     "mode": "shadow",
  //     "evaluation": {
  //       "enabled": false,
  //       "retention_days": 30,
  //       "max_total_bytes": 104857600
  //     }
  //   }
  // },

  // Logging configuration for TUI debug output
  // "logging": {
  //   "suppress_repetitive_debug": true,
  //   "summary_interval_secs": 60
  // },

  // Lifecycle hooks (optional)
  // Available hooks:
  //   Run lifecycle: on_start, on_finish, on_error
  //   Change lifecycle: on_change_start, pre_apply, post_apply, on_change_complete, pre_archive, post_archive, on_change_end, on_merged
  //   Operator interaction (any frontend): on_queue_add, on_queue_remove
  // Available placeholders: {change_id}, {changes_processed}, {total_changes}, {remaining_changes}, {apply_count}, {completed_tasks}, {total_tasks}
  "hooks": {
    // Run lifecycle
    // "on_start": "echo '[on_start] changes_processed={changes_processed} total={total_changes} remaining={remaining_changes}'",
    // "on_finish": "echo '[on_finish] status={status} processed={changes_processed}/{total_changes}'",
    // "on_error": "echo '[on_error] change={change_id} error={error}'",

    // Change lifecycle
    // "on_change_start": "echo '[on_change_start] change={change_id} tasks={completed_tasks}/{total_tasks} progress={changes_processed}/{total_changes}'",
    // "pre_apply": "echo '[pre_apply] change={change_id} apply_count={apply_count} tasks={completed_tasks}/{total_tasks}'",
    // "post_apply": "echo '[post_apply] change={change_id} apply_count={apply_count} tasks={completed_tasks}/{total_tasks}'",
    // "on_change_complete": "echo '[on_change_complete] change={change_id} tasks={total_tasks}/{total_tasks}'",
    // "pre_archive": "echo '[pre_archive] change={change_id} progress={changes_processed}/{total_changes}'",
    // "post_archive": "echo '[post_archive] change={change_id} progress={changes_processed}/{total_changes}'",
    // "on_change_end": "echo '[on_change_end] change={change_id} progress={changes_processed}/{total_changes} remaining={remaining_changes}'",
    // "on_merged": "echo '[on_merged] change={change_id} tasks={completed_tasks}/{total_tasks} progress={changes_processed}/{total_changes}'",

    // TUI interaction
    // "on_queue_add": "echo '[on_queue_add] change={change_id} tasks={completed_tasks}/{total_tasks}'",
    // "on_queue_remove": "echo '[on_queue_remove] change={change_id} tasks={completed_tasks}/{total_tasks}'"
  }
}
"#;

/// Get the configuration template content for the specified template type
pub fn get_template_content(template: Template) -> &'static str {
    match template {
        Template::Claude => CLAUDE_TEMPLATE,
        Template::Opencode => OPENCODE_TEMPLATE,
        Template::Codex => CODEX_TEMPLATE,
    }
}

/// Build a project template containing only required commands absent from the
/// merged global configuration. An empty result means no project config is needed.
pub fn get_missing_required_template(
    template: Template,
    global: &OrchestratorConfig,
) -> crate::error::Result<Option<String>> {
    let selected = OrchestratorConfig::parse_jsonc(get_template_content(template))?;
    let mut fields = serde_json::Map::new();

    for (name, global_value, template_value) in [
        (
            "analyze_command",
            &global.analyze_command,
            &selected.analyze_command,
        ),
        (
            "apply_command",
            &global.apply_command,
            &selected.apply_command,
        ),
        (
            "archive_command",
            &global.archive_command,
            &selected.archive_command,
        ),
        (
            "acceptance_command",
            &global.acceptance_command,
            &selected.acceptance_command,
        ),
        (
            "resolve_command",
            &global.resolve_command,
            &selected.resolve_command,
        ),
    ] {
        if global_value.is_none() {
            fields.insert(
                name.to_string(),
                serde_json::Value::String(
                    template_value
                        .clone()
                        .expect("built-in templates define every required command"),
                ),
            );
        }
    }

    if fields.is_empty() {
        return Ok(None);
    }

    Ok(Some(
        serde_json::to_string_pretty(&serde_json::Value::Object(fields))
            .expect("required command template must serialize")
            + "\n",
    ))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::OrchestratorConfig;

    #[test]
    fn test_claude_template_is_valid_jsonc() {
        // JSONC allows comments and trailing commas, but we should at least
        // verify the template contains expected fields
        assert!(CLAUDE_TEMPLATE.contains("apply_command"));
        assert!(CLAUDE_TEMPLATE.contains("archive_command"));
        assert!(CLAUDE_TEMPLATE.contains("analyze_command"));
        assert!(CLAUDE_TEMPLATE.contains("claude --dangerously-skip-permissions"));
        assert!(CLAUDE_TEMPLATE.contains("--verbose --output-format stream-json"));
        // Should NOT contain nested agent structure
        assert!(!CLAUDE_TEMPLATE.contains("\"agent\":"));
    }

    #[test]
    fn test_opencode_template_is_valid_jsonc() {
        assert!(OPENCODE_TEMPLATE.contains("apply_command"));
        assert!(OPENCODE_TEMPLATE.contains("archive_command"));
        assert!(OPENCODE_TEMPLATE.contains("analyze_command"));
        assert!(OPENCODE_TEMPLATE.contains("opencode run"));
        // Should NOT contain nested agent structure
        assert!(!OPENCODE_TEMPLATE.contains("\"agent\":"));
    }

    #[test]
    fn test_codex_template_is_valid_jsonc() {
        assert!(CODEX_TEMPLATE.contains("apply_command"));
        assert!(CODEX_TEMPLATE.contains("archive_command"));
        assert!(CODEX_TEMPLATE.contains("analyze_command"));
        assert!(CODEX_TEMPLATE.contains("codex"));
        // Should NOT contain nested agent structure
        assert!(!CODEX_TEMPLATE.contains("\"agent\":"));
    }

    #[test]
    fn test_templates_document_operation_skill_keys_and_speca_example() {
        for template in [CLAUDE_TEMPLATE, OPENCODE_TEMPLATE, CODEX_TEMPLATE] {
            assert!(template.contains("\"analyze_skill\": \"cflx-analyze\""));
            assert!(template.contains("\"apply_skill\": \"cflx-apply\""));
            assert!(template.contains("\"rejecting_skill\": \"cflx-rejecting\""));
            assert!(template.contains("\"cleanup_review_skill\": \"cflx-cleanup-review\""));
            assert!(template.contains("\"accept_skill\": \"cflx-accept\""));
            assert!(template.contains("\"archive_skill\": \"cflx-archive\""));
            assert!(template.contains("\"resolve_skill\": \"cflx-resolve\""));
            assert!(template.contains("\"accept_skill\": \"cflx-accept-with-speca\""));
        }
    }

    #[test]
    fn test_templates_document_inactive_append_prompt_examples() {
        for template in [CLAUDE_TEMPLATE, OPENCODE_TEMPLATE, CODEX_TEMPLATE] {
            for field in [
                "apply_append_prompt",
                "acceptance_append_prompt",
                "archive_append_prompt",
                "analyze_append_prompt",
                "resolve_append_prompt",
            ] {
                assert!(template.contains(&format!("//\"{}\": \"\"", field)));
                assert!(!template.contains(&format!("\n  \"{}\"", field)));
            }

            let config = OrchestratorConfig::parse_jsonc(template).unwrap();
            assert_eq!(config.get_apply_append_prompt(), None);
            assert_eq!(config.get_acceptance_append_prompt(), None);
            assert_eq!(config.get_archive_append_prompt(), None);
            assert_eq!(config.get_analyze_append_prompt(), None);
            assert_eq!(config.get_resolve_append_prompt(), None);
        }
    }

    #[test]
    fn test_get_template_content() {
        assert_eq!(get_template_content(Template::Claude), CLAUDE_TEMPLATE);
        assert_eq!(get_template_content(Template::Opencode), OPENCODE_TEMPLATE);
        assert_eq!(get_template_content(Template::Codex), CODEX_TEMPLATE);
    }

    #[test]
    fn test_claude_template_matches_sample_style() {
        assert!(CLAUDE_TEMPLATE.contains("\"apply_command\": \"claude --dangerously-skip-permissions --verbose --output-format stream-json -p '{prompt}'\""));
        assert!(CLAUDE_TEMPLATE.contains("\"archive_command\": \"claude --dangerously-skip-permissions --verbose --output-format stream-json -p '{prompt}'\""));
        assert!(CLAUDE_TEMPLATE.contains("\"acceptance_command\": \"claude --dangerously-skip-permissions --verbose --output-format stream-json -p '{prompt}'\""));
        assert!(CLAUDE_TEMPLATE.contains("\"worktree_command\": \"tmux new-window -n wt \""));
        assert!(!CLAUDE_TEMPLATE.contains("\"acceptance_prompt_mode\""));
        assert!(CLAUDE_TEMPLATE.contains("\"accept_skill\": \"cflx-accept-with-speca\""));
    }

    #[test]
    fn test_opencode_template_matches_sample_style() {
        assert!(OPENCODE_TEMPLATE.contains("\"apply_command\": \"opencode run '{prompt}'\""));
        assert!(OPENCODE_TEMPLATE.contains("\"archive_command\": \"opencode run '{prompt}'\""));
        assert!(OPENCODE_TEMPLATE.contains("\"acceptance_command\": \"opencode run '{prompt}'\""));
        assert!(OPENCODE_TEMPLATE.contains("\"worktree_command\": \"tmux new-window -n wt \""));
        assert!(!OPENCODE_TEMPLATE.contains("\"acceptance_prompt_mode\""));
        assert!(OPENCODE_TEMPLATE.contains("\"accept_skill\": \"cflx-accept-with-speca\""));
    }

    #[test]
    fn test_codex_template_matches_sample_style() {
        assert!(CODEX_TEMPLATE.contains(
            "\"analyze_command\": \"codex -a never exec --json --sandbox workspace-write '{prompt}'\""
        ));
        assert!(CODEX_TEMPLATE.contains(
            "\"apply_command\": \"codex -a never exec --sandbox workspace-write '{prompt}'\""
        ));
        assert!(CODEX_TEMPLATE.contains(
            "\"archive_command\": \"codex -a never exec --sandbox workspace-write '{prompt}'\""
        ));
        assert!(CODEX_TEMPLATE.contains(
            "\"acceptance_command\": \"codex -a never exec --sandbox workspace-write '{prompt}'\""
        ));
        assert!(CODEX_TEMPLATE.contains("\"worktree_command\": \"tmux new-window -n wt \""));
        assert!(!CODEX_TEMPLATE.contains("\"acceptance_prompt_mode\""));
        assert!(CODEX_TEMPLATE.contains("\"accept_skill\": \"cflx-accept-with-speca\""));
    }

    #[test]
    fn test_claude_template_parseable_by_config() {
        // Template should be parseable by OrchestratorConfig
        let config = OrchestratorConfig::parse_jsonc(CLAUDE_TEMPLATE)
            .expect("CLAUDE_TEMPLATE should be valid JSONC");
        assert!(config.apply_command.is_some());
        assert!(config.archive_command.is_some());
        assert!(config.analyze_command.is_some());
        assert_eq!(config.get_accept_skill(), "cflx-accept");
        assert_eq!(config.get_resolve_skill(), "cflx-resolve");
        assert!(config.apply_command.unwrap().contains("claude"));
    }

    #[test]
    fn test_opencode_template_parseable_by_config() {
        // Template should be parseable by OrchestratorConfig
        let config = OrchestratorConfig::parse_jsonc(OPENCODE_TEMPLATE)
            .expect("OPENCODE_TEMPLATE should be valid JSONC");
        assert!(config.apply_command.is_some());
        assert!(config.archive_command.is_some());
        assert!(config.analyze_command.is_some());
        assert_eq!(config.get_accept_skill(), "cflx-accept");
        assert_eq!(config.get_resolve_skill(), "cflx-resolve");
        assert!(config.apply_command.unwrap().contains("opencode"));
    }

    #[test]
    fn test_codex_template_parseable_by_config() {
        // Template should be parseable by OrchestratorConfig
        let config = OrchestratorConfig::parse_jsonc(CODEX_TEMPLATE)
            .expect("CODEX_TEMPLATE should be valid JSONC");
        assert!(config.apply_command.is_some());
        assert!(config.archive_command.is_some());
        assert!(config.analyze_command.is_some());
        assert_eq!(config.get_accept_skill(), "cflx-accept");
        assert_eq!(config.get_resolve_skill(), "cflx-resolve");
        assert!(config.apply_command.unwrap().contains("codex"));
    }
}