atomwrite 0.1.38

Atomic file operations CLI for LLM agents — read, write, edit, search, replace with NDJSON output
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
// SPDX-License-Identifier: MIT OR Apache-2.0
// Closed schemas for terminal events (summary verbs, `diff`, `config`).
// Included from schema_dispatch.inc.rs.

/// Schema names whose terminal event is a closed contract.
///
/// Every key such an event can carry is declared, `additionalProperties` is
/// `false`, and `required` lists the keys that are always present, so the
/// schema rejects an object the verb could never emit instead of validating
/// anything.
const CLOSED_TERMINAL_SCHEMAS: &[&str] = &[
    "search-terminal",
    "replace-terminal",
    "count",
    "count-by-ext",
    "count-by-size",
    "backup-terminal",
    "list-terminal",
    "config",
    "batch-terminal",
    "doctor",
    "extract",
];

/// Keys a shared summary type declares but the verb never emits.
fn never_emitted(name: &str) -> &'static [&'static str] {
    match name {
        "search-terminal" => &[
            "files_modified",
            "total_replacements",
            "files_read",
            "total_bytes",
            "binary_rejects",
        ],
        "replace-terminal" => &["total_matches", "files_read", "offset_skipped", "total_bytes"],
        _ => &[],
    }
}

/// Keys the verb always emits although the shared summary type makes them optional.
fn always_emitted(name: &str) -> &'static [&'static str] {
    match name {
        "search-terminal" => &["total_matches", "offset_skipped"],
        "replace-terminal" => &[
            "files_skipped",
            "total_replacements",
            "files_modified",
            "binary_rejects",
        ],
        "list-terminal" => &["total_bytes"],
        "count-by-ext" | "count-by-size" => &["items_omitted"],
        _ => &[],
    }
}

/// Keys a failed terminal event must carry.
///
/// Only the two verdict keys are required: `message`, `error_class` and
/// `retryable` are dropped from the default envelope (`-v` restores them), so
/// demanding them would reject every failed run the product actually writes.
/// `suggestion_lang` is not listed either: it is written only when the message
/// is not in English, so an English run would fail a schema that demanded it.
const FAILURE_REQUIRED: &[&str] = &["code", "exit"];

/// Whether the verb ends a failed run with its own summary (`publish_failure`).
///
/// Such a summary says `ok: false` only when the run failed, so a failed
/// summary must carry the whole failure detail.
fn ends_failure_with_summary(name: &str) -> bool {
    matches!(
        name,
        "search-terminal" | "replace-terminal" | "batch-terminal" | "backup-terminal" | "prune-backups"
    )
}

/// Whether the terminal event can carry the failure detail `publish_failure` stamps.
///
/// `doctor` is the exception to the rule above: its report says `ok: false`
/// for a failing check without failing the run, and carries the failure
/// detail only under `--strict`, so the detail is optional there.
fn can_carry_failure_detail(name: &str) -> bool {
    ends_failure_with_summary(name) || name == "doctor"
}

/// Add `key` to the schema's `required` list when it is not there yet.
fn require_key(root: &mut serde_json::Map<String, serde_json::Value>, key: &str) {
    let required = root
        .entry("required")
        .or_insert_with(|| serde_json::json!([]));
    if let Some(list) = required.as_array_mut()
        && !list.iter().any(|k| k == key)
    {
        list.push(serde_json::Value::String(key.to_string()));
    }
}

/// Failure-detail properties and their definitions, read off the error envelope type.
fn failure_detail_schema() -> (
    serde_json::Map<String, serde_json::Value>,
    serde_json::Map<String, serde_json::Value>,
) {
    let schema = schemars::schema_for!(crate::error::ErrorJson);
    let root = schema.as_object();
    let pick = |key: &str| {
        root.and_then(|r| r.get(key))
            .and_then(serde_json::Value::as_object)
            .cloned()
            .unwrap_or_default()
    };
    let mut properties = pick("properties");
    for stamped in ["ok", "code", "exit"] {
        properties.remove(stamped);
    }
    (properties, pick("$defs"))
}

/// Declare the failure detail `publish_failure` stamps (and the definitions it
/// refers to) on an object schema, without overwriting a key it already has.
fn add_failure_detail(root: &mut serde_json::Map<String, serde_json::Value>) {
    let (detail, defs) = failure_detail_schema();
    if let Some(properties) = root
        .get_mut("properties")
        .and_then(serde_json::Value::as_object_mut)
    {
        for (key, definition) in detail {
            properties.entry(key).or_insert(definition);
        }
    }
    let known = root.entry("$defs").or_insert_with(|| serde_json::json!({}));
    if let Some(known) = known.as_object_mut() {
        for (key, definition) in defs {
            known.entry(key).or_insert(definition);
        }
    }
}

/// Schema of everything `extract` writes.
///
/// `extract` is a stream of records, and which record it writes depends on the
/// input line and on the field flags:
///
/// - an NDJSON object with fields selected is narrowed to those keys (the
///   keys are the caller's, so the object stays open);
/// - an NDJSON object with no field selected is passed through whole (also
///   open: the input decides the keys);
/// - a text line with no field selected becomes `{type: "text", fields: [...]}`;
/// - a text line with fields selected becomes `{type: "text", values: [...]}`.
///
/// The union is the contract. Serving one branch alone (the schema used to
/// serve `calc`'s) described an event `extract` never writes.
fn extract_output_schema() -> Option<schemars::Schema> {
    let fields = serde_json::Value::from(schemars::schema_for!(
        ndjson_types::TextFieldsOutput<'static>
    ));
    let values = serde_json::Value::from(schemars::schema_for!(
        ndjson_types::TextValuesOutput<'static>
    ));
    // The two passthrough records leave every key to the input line, which is
    // exactly what `{"type": "object"}` says; the description names the case so
    // the schema is readable on its own.
    let json_narrowed = serde_json::json!({
        "type": "object",
        "description": "An NDJSON input object narrowed to the selected fields.",
        "additionalProperties": true,
    });
    let json_whole = serde_json::json!({
        "type": "object",
        "description": "An NDJSON input object passed through whole (no field selected).",
        "additionalProperties": true,
    });
    union_of(
        "ExtractOutput",
        "Records of `atomwrite extract`: one per input line, narrowed or whole for NDJSON, one of two shapes for text.",
        vec![json_narrowed, json_whole, fields, values],
    )
}

/// Schema of everything `scope` writes: one `result` record per rewritten
/// file, then the `summary`.
///
/// The summary is the terminal event, so it carries the keys the writer
/// stamps and, when the run failed, the failure detail. Without the `ast`
/// feature the verb fails closed and only the per-file record is described.
fn scope_output_schema() -> Option<schemars::Schema> {
    #[cfg(feature = "ast")]
    {
        union_of(
            "ScopeOutput",
            "Records of `atomwrite scope`: one `result` per rewritten file, then the `summary`.",
            vec![
                serde_json::Value::from(schemars::schema_for!(ndjson_types::ScopeResult)),
                terminal_record(
                    schemars::schema_for!(crate::commands::scope::ScopeSummary),
                    &[],
                ),
            ],
        )
    }
    #[cfg(not(feature = "ast"))]
    {
        Some(schemars::schema_for!(ndjson_types::ScopeResult))
    }
}

/// Schema of everything a list verb writes: one record per item, then the
/// terminal summary.
///
/// `hash`, `verify` and `copy` publish a record per file and always close with a
/// summary, so serving the record alone described a stream a consumer could not
/// read the verdict off. The union is the contract, the same shape `extract` and
/// `scope` already use.
fn record_with_summary_schema(
    record: schemars::Schema,
    title: &str,
    description: &str,
) -> Option<schemars::Schema> {
    record_with_own_summary(
        record,
        schemars::schema_for!(ndjson_types::FailureSummary),
        title,
        description,
    )
}

/// Same union for a verb whose summary is its own type (`backup`,
/// `prune-backups`), which still writes a record per item before the terminal.
fn record_with_own_summary(
    record: schemars::Schema,
    summary: schemars::Schema,
    title: &str,
    description: &str,
) -> Option<schemars::Schema> {
    union_of(
        title,
        description,
        vec![
            serde_json::Value::from(record),
            terminal_record(summary, &[]),
        ],
    )
}

/// The `step` tag `recipe` adds to every record it reemits from a child.
fn step_property() -> serde_json::Value {
    serde_json::json!({
        "type": "integer",
        "minimum": 1,
        "description": "1-based recipe step that emitted this record",
    })
}

/// The `rule_id` tag `codemod` adds to every record it reemits from a child.
fn rule_property() -> serde_json::Value {
    serde_json::json!({
        "type": "string",
        "description": "Rule the record was written for",
    })
}

/// One record shape of a union, with the tags the parent verb adds to it.
fn tagged_record(
    schema: schemars::Schema,
    tags: &[(&str, serde_json::Value)],
) -> serde_json::Value {
    let mut value = serde_json::Value::from(schema);
    if let Some(obj) = value.as_object_mut()
        && let Some(properties) = obj
            .get_mut("properties")
            .and_then(serde_json::Value::as_object_mut)
    {
        for (key, definition) in tags {
            properties
                .entry((*key).to_string())
                .or_insert_with(|| definition.clone());
        }
    }
    value
}

/// The terminal record of a union: the type's own keys plus every key the
/// writer stamps on the last line, and the failure detail when the run failed.
fn terminal_record(
    schema: schemars::Schema,
    tags: &[(&str, serde_json::Value)],
) -> serde_json::Value {
    let mut value = tagged_record(schema, tags);
    if let Some(obj) = value.as_object_mut() {
        if let Some(properties) = obj
            .get_mut("properties")
            .and_then(serde_json::Value::as_object_mut)
        {
            for (key, definition) in writer_stamped_properties() {
                properties.entry(key).or_insert(definition);
            }
        }
        add_failure_detail(obj);
    }
    value
}

/// One terminal branch closed on its own, with the verdict keys required.
///
/// The writer stamps the verdict and the resolved target on the last line, and
/// the union root no longer carries them, so the branch adds them before it
/// closes, exactly as the root used to.
fn closed_branch(name: &str, schema: schemars::Schema) -> serde_json::Value {
    serde_json::Value::from(close_terminal_schema(name, with_envelope_keys(schema)))
}

/// The failure envelope, which any verb may publish in place of its stream.
fn error_branch() -> serde_json::Value {
    let mut value = serde_json::Value::from(error_output_schema());
    if let Some(obj) = value.as_object_mut() {
        obj.remove("$schema");
    }
    value
}

/// Build the union of every record a verb writes.
///
/// One name describes one verb, so the records before the terminal, the
/// terminal and the failure envelope live in the same document; a consumer
/// validating the stream needs one schema, not one per record shape.
fn union_of(
    title: &str,
    description: &str,
    mut branches: Vec<serde_json::Value>,
) -> Option<schemars::Schema> {
    branches.push(error_branch());
    let mut defs = serde_json::Map::new();
    for branch in &mut branches {
        let Some(obj) = branch.as_object_mut() else {
            continue;
        };
        obj.remove("$schema");
        if let Some(serde_json::Value::Object(found)) = obj.remove("$defs") {
            defs.extend(found);
        }
    }
    let union = serde_json::json!({
        "$schema": "https://json-schema.org/draft/2020-12/schema",
        "title": title,
        "description": description,
        "type": "object",
        "anyOf": branches,
        "$defs": defs,
    });
    schemars::Schema::try_from(union).ok()
}

/// Everything `recipe` writes: the listing row, every record of each child step
/// tagged with `step`, the step records and the terminal result.
fn recipe_output_schema() -> Option<schemars::Schema> {
    let step = [("step", step_property())];
    union_of(
        "RecipeOutput",
        "Records of `atomwrite recipe`: the recipe listing, every record of each step tagged with `step`, and the terminal result.",
        vec![
            tagged_record(schemars::schema_for!(ndjson_types::SearchBegin), &step),
            tagged_record(schemars::schema_for!(ndjson_types::SearchMatch), &step),
            tagged_record(schemars::schema_for!(ndjson_types::SearchEnd), &step),
            tagged_record(schemars::schema_for!(ndjson_types::DryRunPlan), &step),
            tagged_record(schemars::schema_for!(ndjson_types::ReplaceResult), &step),
            tagged_record(schemars::schema_for!(ndjson_types::ProgressEvent), &step),
            tagged_record(schemars::schema_for!(ndjson_types::HashOutput), &step),
            tagged_record(schemars::schema_for!(ndjson_types::CancelledEvent), &step),
            serde_json::Value::from(schemars::schema_for!(ndjson_types::RecipeListEvent)),
            serde_json::Value::from(schemars::schema_for!(ndjson_types::RecipeStepEvent)),
            terminal_record(
                schemars::schema_for!(crate::commands::recipe::RecipeResult),
                &[],
            ),
        ],
    )
}

/// Everything `codemod` writes: the start event, every record of each rule
/// tagged with `rule_id`, and the terminal summary the run really ends with.
fn codemod_output_schema() -> Option<schemars::Schema> {
    let rule = [("rule_id", rule_property())];
    union_of(
        "CodemodOutput",
        "Records of `atomwrite codemod`: the start event, every record of each rule tagged with `rule_id`, and the terminal summary.",
        vec![
            serde_json::Value::from(schemars::schema_for!(ndjson_types::CodemodStartEvent)),
            tagged_record(schemars::schema_for!(ndjson_types::TransformRuleBegin), &rule),
            tagged_record(schemars::schema_for!(ndjson_types::TransformRuleError), &rule),
            tagged_record(schemars::schema_for!(ndjson_types::TransformResult), &rule),
            tagged_record(schemars::schema_for!(ndjson_types::CancelledEvent), &rule),
            terminal_record(
                schemars::schema_for!(ndjson_types::CodemodSummaryEvent),
                &[],
            ),
            terminal_record(schemars::schema_for!(ndjson_types::Summary), &rule),
        ],
    )
}

/// The JSON Schema document `batch --input-schema` prints instead of a stream.
fn manifest_schema_branch() -> serde_json::Value {
    serde_json::json!({
        "type": "object",
        "description": "The JSON Schema document `batch --input-schema` prints instead of a stream.",
        "required": ["$schema", "title", "properties"],
        "properties": {
            "$schema": {"type": "string"},
            "title": {"type": "string"},
            "type": {"type": "string"},
            "description": {"type": "string"},
            "properties": {"type": "object"},
            "required": {"type": "array"},
            "additionalProperties": {"type": "boolean"},
        },
    })
}

/// Everything `batch` writes, plus the manifest schema `--input-schema` answers.
fn batch_output_schema() -> Option<schemars::Schema> {
    union_of(
        "BatchOutput",
        "Records of `atomwrite batch`: one result per operation, progress, the rollback event and the terminal summary; `--input-schema` answers with the manifest schema instead.",
        vec![
            serde_json::Value::from(schemars::schema_for!(ndjson_types::BatchOpResult<'static>)),
            serde_json::Value::from(schemars::schema_for!(ndjson_types::ProgressEvent)),
            terminal_record(
                schemars::schema_for!(crate::commands::batch::RollbackEvent),
                &[],
            ),
            closed_branch(
                "batch-terminal",
                schemars::schema_for!(ndjson_types::BatchSummary),
            ),
            manifest_schema_branch(),
        ],
    )
}

/// Everything `apply` writes: the plan of `--dry-run`, one `applied` record
/// per patched file, and the rollback record of a run that put its files back.
///
/// The plan record was never described by a published schema, and the rollback
/// record had no schema at all, so a consumer validating the stream against
/// `apply-result.schema.json` rejected two of the three records the verb can
/// write. The union is the contract, the same shape `batch` and `recipe` use.
fn apply_output_schema() -> Option<schemars::Schema> {
    union_of(
        "ApplyOutput",
        "Records of `atomwrite apply`: the plan of `--dry-run`, one `applied` record per patched file, and the rollback record when a failed run put the files it had already written back.",
        vec![
            serde_json::Value::from(schemars::schema_for!(ndjson_types::ApplyPlan)),
            serde_json::Value::from(schemars::schema_for!(ndjson_types::ApplyResult)),
            terminal_record(
                schemars::schema_for!(ndjson_types::ApplyRollbackEvent),
                &[],
            ),
        ],
    )
}

/// Everything `search` writes: begin, context, end, file and match records,
/// then the terminal summary.
fn search_output_schema() -> Option<schemars::Schema> {
    union_of(
        "SearchOutput",
        "Records of `atomwrite search`: `begin`, `context`, `file`, `match` and `end` records, then the terminal summary.",
        vec![
            serde_json::Value::from(schemars::schema_for!(ndjson_types::SearchBegin)),
            serde_json::Value::from(schemars::schema_for!(ndjson_types::SearchContext)),
            serde_json::Value::from(schemars::schema_for!(ndjson_types::SearchEnd)),
            serde_json::Value::from(schemars::schema_for!(ndjson_types::SearchFile)),
            serde_json::Value::from(schemars::schema_for!(ndjson_types::SearchMatch)),
            closed_branch(
                "search-terminal",
                schemars::schema_for!(ndjson_types::FailureSummary),
            ),
        ],
    )
}

/// Everything `replace` writes: the dry-run plan or the per-file result, then
/// the terminal summary.
fn replace_output_schema() -> Option<schemars::Schema> {
    union_of(
        "ReplaceOutput",
        "Records of `atomwrite replace`: a `plan` record in `--dry-run`, one `replace` record per file, then the terminal summary.",
        vec![
            serde_json::Value::from(schemars::schema_for!(ndjson_types::DryRunPlan)),
            serde_json::Value::from(schemars::schema_for!(ndjson_types::ReplaceResult)),
            closed_branch(
                "replace-terminal",
                schemars::schema_for!(ndjson_types::FailureSummary),
            ),
        ],
    )
}

/// Everything `list` writes: one entry per item, then the terminal summary.
fn list_output_schema() -> Option<schemars::Schema> {
    union_of(
        "ListOutput",
        "Records of `atomwrite list`: one `entry` per item, then the terminal summary.",
        vec![
            serde_json::Value::from(schemars::schema_for!(ndjson_types::ListEntry)),
            closed_branch(
                "list-terminal",
                schemars::schema_for!(ndjson_types::ListSummary),
            ),
        ],
    )
}

/// Everything `backup` writes: one record per source, then the terminal
/// summary.
fn backup_output_schema() -> Option<schemars::Schema> {
    union_of(
        "BackupOutput",
        "Records of `atomwrite backup`: one `backup` or `plan` record per source, then the terminal summary.",
        vec![
            serde_json::Value::from(schemars::schema_for!(ndjson_types::BackupResult)),
            closed_branch(
                "backup-terminal",
                schemars::schema_for!(ndjson_types::BackupSummary),
            ),
        ],
    )
}

/// Everything `semantic-search` writes: one match per hit, then the summary.
fn semantic_search_output_schema() -> Option<schemars::Schema> {
    union_of(
        "SemanticSearchOutput",
        "Records of `atomwrite semantic-search`: one `semantic_match` per hit, then the terminal summary.",
        vec![
            serde_json::Value::from(schemars::schema_for!(ndjson_types::SemanticMatchEvent)),
            terminal_record(
                schemars::schema_for!(ndjson_types::SemanticSummaryEvent),
                &[],
            ),
        ],
    )
}

/// Everything `sparse` writes: the entry listing, the read records, then the
/// summary.
fn sparse_output_schema() -> Option<schemars::Schema> {
    union_of(
        "SparseOutput",
        "Records of `atomwrite sparse`: `sparse_entry` records on the listing, `sparse_read` records on a read, then the terminal summary.",
        vec![
            serde_json::Value::from(schemars::schema_for!(crate::commands::sparse::SparseEntry)),
            serde_json::Value::from(schemars::schema_for!(ndjson_types::SparseReadEvent)),
            terminal_record(
                schemars::schema_for!(crate::commands::sparse::SparseSummary),
                &[],
            ),
        ],
    )
}

/// Everything `semantic-merge` writes: the dry-run plan, then the result.
fn semantic_merge_output_schema() -> Option<schemars::Schema> {
    union_of(
        "SemanticMergeOutput",
        "Records of `atomwrite semantic-merge`: the `plan` record in `--dry-run`, then the terminal result.",
        vec![
            serde_json::Value::from(schemars::schema_for!(ndjson_types::DryRunPlan)),
            terminal_record(
                schemars::schema_for!(crate::commands::semantic_merge::SemanticMergeResult),
                &[],
            ),
        ],
    )
}

/// Everything `case` writes: one `case` or `case_preview` record per file,
/// then the summary.
fn case_output_schema() -> Option<schemars::Schema> {
    union_of(
        "CaseOutput",
        "Records of `atomwrite case`: one `case` or `case_preview` record per file, then the terminal summary.",
        vec![
            serde_json::Value::from(schemars::schema_for!(crate::commands::case::CaseResult)),
            terminal_record(
                schemars::schema_for!(crate::commands::case::CaseSummary),
                &[],
            ),
        ],
    )
}

/// Everything `watch` writes: one event per change, then the summary.
fn watch_output_schema() -> Option<schemars::Schema> {
    union_of(
        "WatchOutput",
        "Records of `atomwrite watch`: one `watch` event per change, then the terminal summary.",
        vec![
            serde_json::Value::from(schemars::schema_for!(ndjson_types::WatchEvent)),
            terminal_record(schemars::schema_for!(ndjson_types::WatchSummary), &[]),
        ],
    )
}

/// Terminal event of `completions --install`.
///
/// Without `--install` the command writes the shell script as plain text
/// (`stdout_kind: shell-script` on the agent surface) and emits no NDJSON, so
/// the installed-path event is the only record this schema can describe.
#[derive(serde::Serialize, schemars::JsonSchema)]
pub struct CompletionsInstalled {
    /// Event type discriminator, always `installed`.
    pub r#type: &'static str,
    /// Absolute path of the completion file that was written.
    pub path: String,
}

/// Close the schema of a terminal event; other schemas pass through untouched.
fn close_terminal_schema(name: &str, mut schema: schemars::Schema) -> schemars::Schema {
    if name == "diff" {
        return close_diff_schema(schema);
    }
    // `extract` writes a stream of records, not a terminal event: the caller's
    // own keys decide the shape of a passthrough line, so the envelope keys
    // would describe keys the verb never writes and a closed root would reject
    // every line of a plain object.
    if name == "extract" {
        return schema;
    }
    // A union root carries no `properties` of its own, so closing it would put
    // `additionalProperties: false` beside the `anyOf` and reject every record.
    // Each union closes its own terminal branch instead.
    if schema
        .as_object()
        .is_some_and(|root| root.contains_key("anyOf") || root.contains_key("oneOf"))
    {
        return schema;
    }
    if !CLOSED_TERMINAL_SCHEMAS.contains(&name) {
        return schema;
    }
    let Some(root) = schema.as_object_mut() else {
        return schema;
    };
    if let Some(properties) = root
        .get_mut("properties")
        .and_then(serde_json::Value::as_object_mut)
    {
        for key in never_emitted(name) {
            properties.remove(*key);
        }
    }
    // The two verbs share one summary type; each schema carries its own name.
    match name {
        "search-terminal" => {
            root.insert("title".to_string(), "SearchSummary".into());
        }
        "replace-terminal" => {
            root.insert("title".to_string(), "ReplaceSummary".into());
        }
        _ => {}
    }
    for key in always_emitted(name) {
        require_key(root, key);
    }
    require_key(root, crate::output::RESULT_KEY);
    if can_carry_failure_detail(name) {
        add_failure_detail(root);
    }
    if ends_failure_with_summary(name) {
        root.insert(
            "if".to_string(),
            serde_json::json!({"properties": {"ok": {"const": false}}, "required": ["ok"]}),
        );
        root.insert(
            "then".to_string(),
            serde_json::json!({"required": FAILURE_REQUIRED}),
        );
    }
    root.insert(
        "additionalProperties".to_string(),
        serde_json::Value::Bool(false),
    );
    schema
}

/// Close each record shape of `diff`.
///
/// The schema is a union of five inlined shapes: the four records the verb
/// writes and the failure envelope, which any run may publish instead. The two
/// stream-level shapes
/// that end the run (`summary`, `diff`) carry the verdict stamped by the
/// writer; the `change` record does not.
fn close_diff_schema(mut schema: schemars::Schema) -> schemars::Schema {
    let stamped = writer_stamped_properties();
    let Some(branches) = schema
        .as_object_mut()
        .and_then(|root| root.get_mut("anyOf"))
        .and_then(serde_json::Value::as_array_mut)
    else {
        return schema;
    };
    for branch in branches {
        let Some(obj) = branch.as_object_mut() else {
            continue;
        };
        let terminal = obj
            .get("properties")
            .and_then(serde_json::Value::as_object)
            .is_some_and(|p| p.contains_key("elapsed_ms"));
        if terminal {
            if let Some(properties) = obj
                .get_mut("properties")
                .and_then(serde_json::Value::as_object_mut)
            {
                for (key, definition) in &stamped {
                    properties
                        .entry((*key).to_string())
                        .or_insert(definition.clone());
                }
            }
            require_key(obj, crate::output::RESULT_KEY);
        }
        obj.insert(
            "additionalProperties".to_string(),
            serde_json::Value::Bool(false),
        );
    }
    // Every record matches exactly one shape (the shapes differ by `type` and by
    // closed key set), so the union is a `oneOf` under an object root.
    if let Some(root) = schema.as_object_mut()
        && let Some(serde_json::Value::Array(mut branches)) = root.remove("anyOf")
    {
        let mut error = error_branch();
        if let Some(obj) = error.as_object_mut()
            && let Some(serde_json::Value::Object(found)) = obj.remove("$defs")
        {
            let defs = root
                .entry("$defs")
                .or_insert_with(|| serde_json::json!({}));
            if let Some(map) = defs.as_object_mut() {
                map.extend(found);
            }
        }
        branches.push(error);
        root.insert("type".to_string(), "object".into());
        root.insert("oneOf".to_string(), serde_json::Value::Array(branches));
    }
    schema
}