@criogaid/pi-hashline-edit 0.13.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. package/README.md +18 -12
  2. package/README.zh-CN.md +18 -12
  3. package/package.json +8 -1
  4. package/src/core/AGENTS.md +3 -1
  5. package/src/core/apply.ts +110 -117
  6. package/src/core/character-set.ts +2 -3
  7. package/src/core/copy-span.ts +15 -0
  8. package/src/core/errors.ts +24 -9
  9. package/src/core/lines.ts +71 -17
  10. package/src/core/ranges.ts +5 -4
  11. package/src/core/replace-schema.ts +3 -1
  12. package/src/core/replace.ts +50 -29
  13. package/src/core/search-analysis.ts +21 -22
  14. package/src/core/search.ts +15 -32
  15. package/src/core/types.ts +3 -0
  16. package/src/index.ts +47 -39
  17. package/src/pi/AGENTS.md +30 -19
  18. package/src/pi/action-fusion.ts +284 -211
  19. package/src/pi/argument-diagnostics.ts +50 -37
  20. package/src/pi/argument-error.ts +2 -2
  21. package/src/pi/argument-validation.ts +163 -66
  22. package/src/pi/budgets.ts +12 -0
  23. package/src/pi/diagnostic-buffer.ts +12 -11
  24. package/src/pi/edit-tool.ts +138 -115
  25. package/src/pi/error-text.ts +10 -2
  26. package/src/pi/failure-context.ts +24 -37
  27. package/src/pi/file-commit.ts +131 -164
  28. package/src/pi/file-read.ts +122 -31
  29. package/src/pi/fusion-arguments.ts +214 -0
  30. package/src/pi/fusion-card.ts +102 -60
  31. package/src/pi/grep-candidates.ts +35 -22
  32. package/src/pi/grep-output.ts +197 -190
  33. package/src/pi/grep-render.ts +22 -13
  34. package/src/pi/grep-reread.ts +7 -6
  35. package/src/pi/grep-scope.ts +31 -16
  36. package/src/pi/grep-search.ts +56 -47
  37. package/src/pi/grep-tool.ts +35 -33
  38. package/src/pi/grep-worker.ts +3 -4
  39. package/src/pi/mutation-diff.ts +238 -0
  40. package/src/pi/mutation-result.ts +102 -79
  41. package/src/pi/mutation-runner.ts +107 -84
  42. package/src/pi/native-bash.ts +41 -0
  43. package/src/pi/path.ts +59 -6
  44. package/src/pi/read-tool.ts +173 -138
  45. package/src/pi/regex-worker.ts +14 -8
  46. package/src/pi/render.ts +101 -54
  47. package/src/pi/replace-regex.ts +1 -1
  48. package/src/pi/replace-tool.ts +14 -7
  49. package/src/pi/report-cause.ts +34 -17
  50. package/src/pi/report-schema.ts +184 -85
  51. package/src/pi/report.ts +133 -64
  52. package/src/pi/rg-process.ts +68 -48
  53. package/src/pi/schema.ts +4 -0
  54. package/src/pi/text-stream.ts +81 -87
  55. package/src/pi/workspace-cwd.js +2 -0
  56. package/src/pi/workspace.ts +46 -0
  57. package/src/pi/write-tool.ts +19 -13
  58. package/src/core/apply.test.ts +0 -644
  59. package/src/core/case-fold.test.ts +0 -35
  60. package/src/core/character-set.test.ts +0 -97
  61. package/src/core/errors.test.ts +0 -39
  62. package/src/core/hash.test.ts +0 -64
  63. package/src/core/lines.test.ts +0 -63
  64. package/src/core/ranges.test.ts +0 -123
  65. package/src/core/replace.test.ts +0 -187
  66. package/src/core/search-analysis.test.ts +0 -210
  67. package/src/core/search-matrix.test.ts +0 -64
  68. package/src/core/search-matrix.testing.ts +0 -171
  69. package/src/core/search.test.ts +0 -132
  70. package/src/core/text.test.ts +0 -52
  71. package/src/integration/config-registration.test.ts +0 -291
  72. package/src/integration/fusion-dispatch.test.ts +0 -320
  73. package/src/integration/grep-errors.test.ts +0 -81
  74. package/src/integration/grep-flags.test.ts +0 -76
  75. package/src/integration/grep-js-completeness.test.ts +0 -278
  76. package/src/integration/grep-line-candidates.test.ts +0 -201
  77. package/src/integration/grep-matrix.test.ts +0 -118
  78. package/src/integration/grep-order.test.ts +0 -78
  79. package/src/integration/grep-path-matrix.test.ts +0 -128
  80. package/src/integration/grep-prefilter-coverage.test.ts +0 -204
  81. package/src/integration/grep-process-limits.test.ts +0 -122
  82. package/src/integration/grep-regex.test.ts +0 -412
  83. package/src/integration/grep-rg.test.ts +0 -1659
  84. package/src/integration/grep-scan-budget.test.ts +0 -111
  85. package/src/integration/grep-translation.test.ts +0 -199
  86. package/src/integration/large-text.test.ts +0 -70
  87. package/src/integration/model-budget-lifecycle.test.ts +0 -186
  88. package/src/integration/mutation-file-types.test.ts +0 -30
  89. package/src/integration/mutation-file-types.testing.ts +0 -33
  90. package/src/integration/npm-package.test.ts +0 -19
  91. package/src/integration/npm-package.testing.ts +0 -243
  92. package/src/integration/publication-read-changes.test.ts +0 -454
  93. package/src/integration/read-changes.test.ts +0 -98
  94. package/src/integration/replace-package.test.ts +0 -22
  95. package/src/integration/replace-package.testing.ts +0 -36
  96. package/src/integration/replace-worker-boundary.test.ts +0 -127
  97. package/src/integration/report-replay.test.ts +0 -91
  98. package/src/integration/utf8-errors.test.ts +0 -46
  99. package/src/pi/action-fusion.test.ts +0 -659
  100. package/src/pi/action-fusion.testing.ts +0 -38
  101. package/src/pi/anchor-format.test.ts +0 -124
  102. package/src/pi/argument-error.testing.ts +0 -46
  103. package/src/pi/argument-format.test.ts +0 -196
  104. package/src/pi/argument-validation.test.ts +0 -1007
  105. package/src/pi/codemode.test.ts +0 -101
  106. package/src/pi/config.test.ts +0 -204
  107. package/src/pi/diagnostic-buffer.test.ts +0 -115
  108. package/src/pi/directory-sync.test.ts +0 -57
  109. package/src/pi/execute.test.ts +0 -1257
  110. package/src/pi/extension-loading.test.ts +0 -14
  111. package/src/pi/extension-loading.testing.ts +0 -3
  112. package/src/pi/failure-context.test.ts +0 -370
  113. package/src/pi/failure-path.test.ts +0 -289
  114. package/src/pi/file-commit.test.ts +0 -628
  115. package/src/pi/file-read.test.ts +0 -29
  116. package/src/pi/file-snapshot.test.ts +0 -22
  117. package/src/pi/fusion-card-interaction.test.ts +0 -143
  118. package/src/pi/fusion-card.test.ts +0 -353
  119. package/src/pi/grep-candidates.test.ts +0 -26
  120. package/src/pi/grep-matrix.test.ts +0 -68
  121. package/src/pi/grep-order.test.ts +0 -447
  122. package/src/pi/grep-output.testing.ts +0 -24
  123. package/src/pi/grep-rg.testing.ts +0 -73
  124. package/src/pi/grep.test.ts +0 -1670
  125. package/src/pi/model-budgets.test.ts +0 -96
  126. package/src/pi/mutation-outcome.testing.ts +0 -20
  127. package/src/pi/mutation-result.test.ts +0 -96
  128. package/src/pi/output-budgets.test.ts +0 -221
  129. package/src/pi/pi.test.ts +0 -158
  130. package/src/pi/read-delegation.test.ts +0 -119
  131. package/src/pi/read.test.ts +0 -735
  132. package/src/pi/regex-worker.test.ts +0 -42
  133. package/src/pi/registration.test.ts +0 -549
  134. package/src/pi/render.test.ts +0 -841
  135. package/src/pi/replace.test.ts +0 -634
  136. package/src/pi/report-failure.testing.ts +0 -27
  137. package/src/pi/report.test.ts +0 -533
  138. package/src/pi/result-safety.test.ts +0 -597
  139. package/src/pi/rg-process.test.ts +0 -133
  140. package/src/pi/rg-regex.test.ts +0 -40
  141. package/src/pi/text-stream.test.ts +0 -281
  142. package/src/pi/tool-call.testing.ts +0 -61
  143. package/src/pi/tool-factories.testing.ts +0 -59
  144. package/src/pi/write.test.ts +0 -256
  145. package/src/testing/extension-loader.testing.ts +0 -24
  146. package/src/testing/installation.testing.ts +0 -77
  147. package/src/testing/process.testing.ts +0 -33
  148. package/src/testing/regex-host.testing.ts +0 -20
  149. package/src/testing/regex-translation-corpus.ts +0 -267
  150. package/src/testing/rg-line-listing.fixture.mjs +0 -7
  151. package/src/testing/run-tests.ts +0 -47
  152. package/src/testing/session.testing.ts +0 -127
@@ -17,6 +17,7 @@
17
17
 
18
18
  import { Type, type Static, type TObject, type TProperties, type TSchema } from "typebox";
19
19
  import { literalUnion } from "./schema.ts";
20
+ import { BASH_OUTPUT_SCHEMA } from "./native-bash.ts";
20
21
 
21
22
  const count = (description: string) => Type.Integer({ minimum: 1, description });
22
23
  const opt = Type.Optional;
@@ -53,16 +54,21 @@ const FRESHNESS_SCHEMA = Type.Union([
53
54
  ]);
54
55
  export type Freshness = Static<typeof FRESHNESS_SCHEMA>;
55
56
 
57
+ const EXIT_STATUS_SCHEMA = Type.Union([Type.Literal("succeeded"), Type.Literal("failed")]);
56
58
  /** A then_run command's final status; `skipped` never started, `cancelled` may have. */
57
59
  const COMMAND_STATUS_SCHEMA = Type.Union([
58
- Type.Literal("succeeded"),
59
- Type.Literal("failed"),
60
+ ...EXIT_STATUS_SCHEMA.anyOf,
60
61
  Type.Literal("timeout"),
61
62
  Type.Literal("cancelled"),
62
63
  Type.Literal("skipped"),
63
64
  ]);
64
65
  export type CommandStatus = Static<typeof COMMAND_STATUS_SCHEMA>;
65
66
 
67
+ export const COMMAND_IMAGE_SCHEMA = Type.Object(
68
+ { type: Type.Literal("image"), data: Type.String(), mimeType: Type.String() },
69
+ { additionalProperties: false },
70
+ );
71
+
66
72
  /** A then_run command's session tool result, as the TUI renders it. */
67
73
  const COMMAND_RESULT_SCHEMA = Type.Object(
68
74
  {
@@ -76,10 +82,7 @@ const COMMAND_RESULT_SCHEMA = Type.Object(
76
82
  },
77
83
  { additionalProperties: false },
78
84
  ),
79
- Type.Object(
80
- { type: Type.Literal("image"), data: Type.String(), mimeType: Type.String() },
81
- { additionalProperties: false },
82
- ),
85
+ COMMAND_IMAGE_SCHEMA,
83
86
  ]),
84
87
  ),
85
88
  details: opt(Type.Unknown()),
@@ -111,27 +114,37 @@ export const ACTION_FUSION_PROGRESS_SCHEMA = Type.Object(
111
114
  );
112
115
  export type ActionFusionProgress = Static<typeof ACTION_FUSION_PROGRESS_SCHEMA>;
113
116
 
114
- const ANCHOR_FAILURE_SCHEMA = Type.Object(
115
- {
116
- field: Type.String(),
117
- action: Type.String(),
118
- cited: Type.String(),
119
- result: Type.Union([
120
- Type.Literal("shifted"),
121
- Type.Literal("ambiguous"),
122
- Type.Literal("unresolved"),
123
- ]),
124
- candidate: opt(Type.String()),
125
- content: opt(Type.String()),
126
- candidates: opt(Type.Array(Type.String())),
117
+ /** One cited anchor's failure with the facts its result carries. */
118
+ function anchorFailureSchema<Result extends string, Facts extends TProperties>(
119
+ result: Result,
120
+ facts: Facts,
121
+ ) {
122
+ return Type.Object(
123
+ {
124
+ field: Type.String(),
125
+ action: Type.String(),
126
+ cited: Type.String(),
127
+ result: Type.Literal(result),
128
+ ...facts,
129
+ },
130
+ { additionalProperties: false },
131
+ );
132
+ }
133
+
134
+ const NOT_SHOWN_SCHEMA = Type.String({
135
+ description: "Why content (shifted) or observed (unresolved) is absent.",
136
+ });
137
+
138
+ const ANCHOR_FAILURE_SCHEMA = Type.Union([
139
+ anchorFailureSchema("shifted", { candidate: Type.String(), content: Type.String() }),
140
+ anchorFailureSchema("shifted", { candidate: Type.String(), notShown: NOT_SHOWN_SCHEMA }),
141
+ anchorFailureSchema("ambiguous", {
142
+ candidates: Type.Array(Type.String(), { minItems: 2 }),
127
143
  omittedCandidates: opt(Type.Integer({ minimum: 1 })),
128
- observed: opt(Type.String()),
129
- notShown: opt(
130
- Type.String({ description: "Why content (shifted) or observed (unresolved) is absent." }),
131
- ),
132
- },
133
- { additionalProperties: false },
134
- );
144
+ }),
145
+ anchorFailureSchema("unresolved", { observed: Type.String() }),
146
+ anchorFailureSchema("unresolved", { notShown: NOT_SHOWN_SCHEMA }),
147
+ ]);
135
148
  export type AnchorFailureFact = Static<typeof ANCHOR_FAILURE_SCHEMA>;
136
149
 
137
150
  const ARGUMENT_ISSUE_SCHEMA = Type.Object(
@@ -195,6 +208,10 @@ const FACT_FIELDS = {
195
208
  }),
196
209
  "Correct the missing paths and search them again, or use create mode for a new file.",
197
210
  ),
211
+ omittedMissing: modelFact(
212
+ count("Missing paths omitted from the byte budget."),
213
+ "Know that the missing list is incomplete.",
214
+ ),
198
215
 
199
216
  // Anchor verification (failure-context.ts).
200
217
  failures: modelFact(
@@ -232,6 +249,10 @@ const FACT_FIELDS = {
232
249
  Type.Array(Type.String(), { description: "Rules that match nothing." }),
233
250
  "Correct the target rules using read or grep before retrying.",
234
251
  ),
252
+ omittedUnmatched: modelFact(
253
+ count("Unmatched rules omitted from the byte budget."),
254
+ "Know that the unmatched list is incomplete.",
255
+ ),
235
256
  line: modelFact(
236
257
  Type.Integer({ minimum: 1, description: "Line where the conflicting match starts." }),
237
258
  "Locate the first conflicting match when correcting the request.",
@@ -244,6 +265,12 @@ const FACT_FIELDS = {
244
265
  ),
245
266
  exitCode: tuiFact(Type.Union([Type.Integer(), Type.Null()])),
246
267
 
268
+ // read window (read-tool.ts).
269
+ totalLines: modelFact(
270
+ Type.Integer({ minimum: 0, description: "Lines in the file." }),
271
+ "Set offset to a line the file has.",
272
+ ),
273
+
247
274
  // read outcome; outputLimit below is shared with grep.
248
275
  nextOffset: modelFact(Type.Integer({ minimum: 1 }), "Use nextOffset to continue reading."),
249
276
  lineTooLong: modelFact(
@@ -306,18 +333,25 @@ const FACT_FIELDS = {
306
333
  "Use the freshness recovery instruction when the post-commit observation failed.",
307
334
  ),
308
335
 
309
- // Action Fusion.
336
+ // The model and TUI report only the follow-up status. Bash owns the command payloads.
310
337
  then_run: modelFact(
311
- Type.Object(
312
- { status: COMMAND_STATUS_SCHEMA, output: opt(Type.String()) },
313
- { additionalProperties: false },
314
- ),
338
+ Type.Object({ status: COMMAND_STATUS_SCHEMA }, { additionalProperties: false }),
315
339
  "Act on the failure the output shows, and run the command separately when it did not run.",
316
340
  ),
317
341
  progressError: tuiFact(CAUSE_SCHEMA),
318
342
  } as const satisfies Record<string, FactDefinition>;
319
343
 
320
344
  type FactName = keyof typeof FACT_FIELDS;
345
+
346
+ /**
347
+ * List facts the report layer cuts to whole leading entries within the diagnostic block, each
348
+ * with the fact that counts the entries it omitted.
349
+ */
350
+ export const BOUNDED_LISTS = [
351
+ ["missing", "omittedMissing"],
352
+ ["unmatched", "omittedUnmatched"],
353
+ ["failures", "omittedFailures"],
354
+ ] as const satisfies readonly (readonly [FactName, FactName])[];
321
355
  type FactSchema<Name extends FactName> = (typeof FACT_FIELDS)[Name]["schema"];
322
356
  type TuiOnlyFactName = {
323
357
  [Name in FactName]: (typeof FACT_FIELDS)[Name]["audience"] extends "tui" ? Name : never;
@@ -362,7 +396,7 @@ export const ERROR_DEFINITIONS = {
362
396
  INVALID_ARGUMENTS: { facts: facts(["issues"], ["omittedIssues", "schemaLimited"]) },
363
397
  OPERATION_ABORTED: { facts: NONE },
364
398
  PATH_NOT_FOUND: {
365
- facts: facts([], ["missing"]),
399
+ facts: facts([], ["missing", "omittedMissing"]),
366
400
  next: {
367
401
  default: "Check the path.",
368
402
  useCreate: 'Use mode "create" to create a new file.',
@@ -394,7 +428,7 @@ export const ERROR_DEFINITIONS = {
394
428
  },
395
429
  },
396
430
  NO_MATCH: {
397
- facts: facts(["unmatched"]),
431
+ facts: facts(["unmatched"], ["omittedUnmatched"]),
398
432
  next: {
399
433
  default:
400
434
  "Verify the target text with read or grep; check case sensitivity or regex flags if applicable.",
@@ -416,6 +450,13 @@ export const ERROR_DEFINITIONS = {
416
450
  'Check the path and read the existing file. Use mode "overwrite" only if replacing that file is intended.',
417
451
  },
418
452
  },
453
+ OFFSET_PAST_END: {
454
+ facts: facts(["totalLines"]),
455
+ next: {
456
+ default: "Set offset to at most totalLines, or omit it.",
457
+ emptyFile: "Omit offset to read from the start.",
458
+ },
459
+ },
419
460
  NOT_REGULAR_FILE: {
420
461
  facts: NONE,
421
462
  next: { default: "Use the path of a regular file." },
@@ -424,7 +465,9 @@ export const ERROR_DEFINITIONS = {
424
465
  SYMLINK_UNRESOLVED: { facts: NONE },
425
466
  PUBLISH_FAILED: { facts: NONE },
426
467
  POST_PROCESS_FAILED: { facts: NONE },
427
- SEARCH_INCOMPLETE: { facts: facts(["diagnostics"], ["scanLimit", "missing"]) },
468
+ SEARCH_INCOMPLETE: {
469
+ facts: facts(["diagnostics"], ["scanLimit", "missing", "omittedMissing"]),
470
+ },
428
471
  RIPGREP_FAILED: { facts: NONE },
429
472
  UNSUPPORTED_PATH: { facts: NONE },
430
473
  UNCLASSIFIED: { facts: NONE },
@@ -485,11 +528,11 @@ type OutcomeRule =
485
528
  | readonly [fact: "publication", next: string, except?: PublicationStatus];
486
529
 
487
530
  /**
488
- * Recovery after a successful result, by the first fact present in this order.
489
- * A fact absent from this list, or holding the value its rule excepts, needs no recovery beyond itself.
531
+ * Recovery after a successful result. The first listed fact present decides; its excepted value
532
+ * yields no next. A fact absent from this list needs no recovery beyond itself.
490
533
  */
491
534
  export const OUTCOME_NEXT: readonly OutcomeRule[] = [
492
- // A file that no longer exists has no anchors to refresh.
535
+ // A file that no longer exists has no anchors to refresh, and its earlier content is gone.
493
536
  ["freshness", REREAD, "missing"],
494
537
  [
495
538
  "publication",
@@ -504,7 +547,7 @@ export const OUTCOME_NEXT: readonly OutcomeRule[] = [
504
547
  // Ahead of grep's limits: repeating the corrected search states them again.
505
548
  [
506
549
  "missing",
507
- "Only the existing paths were searched. Correct the paths in missing and repeat the search.",
550
+ "Only the existing paths were searched. Correct the missing paths and repeat the search.",
508
551
  ],
509
552
  // Ahead of matchLimit: once the byte budget cuts the output, raising limit shows nothing more.
510
553
  ["outputLimit", OUTPUT_LIMIT_NEXT],
@@ -538,38 +581,76 @@ export const REPORT_SCHEMA = Type.Object({
538
581
  });
539
582
  type ReportFields = Static<typeof REPORT_SCHEMA>;
540
583
 
541
- const SUCCESS_DEFINITIONS = {
542
- read: facts([], ["nextOffset", "outputLimit", "lineTooLong"]),
543
- grep: facts(
544
- [],
545
- [
546
- "matches",
547
- "files",
548
- "missing",
549
- "matchLimit",
550
- "scanLimit",
551
- "outputLimit",
552
- "linePreviewLimit",
553
- "invalidUtf8",
554
- "diagnostics",
555
- "literalPipe",
556
- ],
584
+ const TEXT_OUTPUT = { text: Type.String() };
585
+ const FILE_PUBLICATION = { publication: PUBLICATION_SCHEMA };
586
+ const MUTATION_OUTPUT = {
587
+ ...facts([], ["freshness", "freshnessError"]).properties,
588
+ then_run: opt(
589
+ Type.Union([
590
+ FACT_FIELDS.then_run.schema,
591
+ Type.Object(
592
+ {
593
+ status: EXIT_STATUS_SCHEMA,
594
+ ...BASH_OUTPUT_SCHEMA.properties,
595
+ },
596
+ { additionalProperties: false },
597
+ ),
598
+ ]),
557
599
  ),
558
- edit: facts([]),
559
- replace: facts([], ["matches"]),
560
- write: facts([]),
561
- } as const;
562
- export type ReportTool = keyof typeof SUCCESS_DEFINITIONS;
600
+ };
601
+
602
+ function scriptOutput<P extends TProperties>(properties: P) {
603
+ const fields = { ...properties, next: REPORT_SCHEMA.properties.next };
604
+ const order = Object.keys(REPORT_SCHEMA.properties);
605
+ // The payload's text sorts first. Reordering preserves the exact property schemas.
606
+ const ordered = Object.fromEntries(
607
+ Object.entries(fields).sort(([a], [b]) => order.indexOf(a) - order.indexOf(b)),
608
+ ) as typeof fields;
609
+ return Type.Object(ordered, { additionalProperties: false });
610
+ }
611
+
612
+ /** The full facts of each success, in script output order. Reports may omit facts already shown. */
613
+ export const SCRIPT_OUTPUT_SCHEMAS = {
614
+ read: scriptOutput({
615
+ ...TEXT_OUTPUT,
616
+ ...facts([], ["nextOffset", "lineTooLong", "outputLimit"]).properties,
617
+ }),
618
+ grep: scriptOutput({
619
+ ...TEXT_OUTPUT,
620
+ ...facts(
621
+ ["matches", "files"],
622
+ [
623
+ "missing",
624
+ "omittedMissing",
625
+ "matchLimit",
626
+ "scanLimit",
627
+ "outputLimit",
628
+ "linePreviewLimit",
629
+ "invalidUtf8",
630
+ "diagnostics",
631
+ "literalPipe",
632
+ ],
633
+ ).properties,
634
+ }),
635
+ edit: scriptOutput({ ...FILE_PUBLICATION, ...MUTATION_OUTPUT }),
636
+ replace: scriptOutput({
637
+ ...FILE_PUBLICATION,
638
+ ...facts(["matches"]).properties,
639
+ ...MUTATION_OUTPUT,
640
+ }),
641
+ write: scriptOutput({ ...FILE_PUBLICATION, ...MUTATION_OUTPUT }),
642
+ };
643
+ export type ReportTool = keyof typeof SCRIPT_OUTPUT_SCHEMAS;
563
644
  /** Tools whose reports carry publication, freshness, and the then_run outcome. */
564
645
  const MUTATION_TOOLS = ["edit", "replace", "write"] as const satisfies readonly ReportTool[];
565
646
  export type MutationToolName = (typeof MUTATION_TOOLS)[number];
566
647
  /** Argument rejections may omit message; Pi supplies none for them. */
567
648
  const MESSAGE_OPTIONAL = "INVALID_ARGUMENTS" satisfies ErrorCode;
568
649
 
569
- const NON_MUTATION_TOOLS = (Object.keys(SUCCESS_DEFINITIONS) as ReportTool[]).filter(
570
- (tool): tool is Exclude<ReportTool, MutationToolName> =>
571
- !(MUTATION_TOOLS as readonly ReportTool[]).includes(tool),
572
- );
650
+ const REPORT_TOOLS = Object.keys(SCRIPT_OUTPUT_SCHEMAS) as ReportTool[];
651
+ const isMutationTool = (tool: ReportTool): tool is MutationToolName =>
652
+ (MUTATION_TOOLS as readonly ReportTool[]).includes(tool);
653
+ const NON_MUTATION_TOOLS = REPORT_TOOLS.filter((tool) => !isMutationTool(tool));
573
654
  const COMMON_WIRE_FIELDS = {
574
655
  path: REPORT_SCHEMA.properties.path,
575
656
  next: REPORT_SCHEMA.properties.next,
@@ -579,11 +660,7 @@ const MUTATION_WIRE_FIELDS = {
579
660
  then_run: REPORT_SCHEMA.properties.then_run,
580
661
  progressError: REPORT_SCHEMA.properties.progressError,
581
662
  };
582
- const MUTATION_SUCCESS_WIRE_FIELDS = {
583
- ...MUTATION_WIRE_FIELDS,
584
- freshness: REPORT_SCHEMA.properties.freshness,
585
- freshnessError: REPORT_SCHEMA.properties.freshnessError,
586
- };
663
+ const MUTATION_PRESENTATION = { progressError: REPORT_SCHEMA.properties.progressError };
587
664
  const FAILURE_SCHEMAS = [
588
665
  { tool: literalUnion(NON_MUTATION_TOOLS) },
589
666
  { tool: literalUnion(MUTATION_TOOLS), ...MUTATION_WIRE_FIELDS },
@@ -602,13 +679,13 @@ const FAILURE_SCHEMAS = [
602
679
  ),
603
680
  ),
604
681
  );
605
- const SUCCESS_SCHEMAS = Object.entries(SUCCESS_DEFINITIONS).map(([tool, schema]) =>
682
+ const SUCCESS_SCHEMAS = REPORT_TOOLS.map((tool) =>
606
683
  Type.Object(
607
684
  {
608
685
  ...COMMON_WIRE_FIELDS,
686
+ ...Type.Pick(REPORT_SCHEMA, Object.keys(SCRIPT_OUTPUT_SCHEMAS[tool].properties)).properties,
687
+ ...(isMutationTool(tool) ? MUTATION_PRESENTATION : {}),
609
688
  tool: Type.Literal(tool),
610
- ...((MUTATION_TOOLS as readonly string[]).includes(tool) ? MUTATION_SUCCESS_WIRE_FIELDS : {}),
611
- ...schema.properties,
612
689
  },
613
690
  { additionalProperties: false },
614
691
  ),
@@ -617,15 +694,15 @@ const SUCCESS_SCHEMAS = Object.entries(SUCCESS_DEFINITIONS).map(([tool, schema])
617
694
  export const WIRE_REPORT_SCHEMA = Type.Union([...FAILURE_SCHEMAS, ...SUCCESS_SCHEMAS]);
618
695
 
619
696
  type SuccessFactNames = {
620
- [Tool in ReportTool]: keyof Static<(typeof SUCCESS_DEFINITIONS)[Tool]>;
621
- }[ReportTool] &
622
- string;
697
+ [Tool in ReportTool]: keyof SuccessFacts<Tool>;
698
+ }[ReportTool];
623
699
  type ErrorFactNames = {
624
700
  [Code in ErrorCode]: keyof Static<(typeof ERROR_DEFINITIONS)[Code]["facts"]>;
625
- }[ErrorCode] &
626
- string;
627
- type MutationWireFactNames = keyof Fields<typeof MUTATION_SUCCESS_WIRE_FIELDS> & string;
628
- type UnusedFactNames = Exclude<FactName, SuccessFactNames | ErrorFactNames | MutationWireFactNames>;
701
+ }[ErrorCode];
702
+ type UnusedFactNames = Exclude<
703
+ FactName,
704
+ SuccessFactNames | ErrorFactNames | keyof typeof MUTATION_PRESENTATION
705
+ >;
629
706
  // Compilation fails here, naming the fact, when no tool, code, or mutation field carries it.
630
707
  ({}) satisfies Record<UnusedFactNames, never>;
631
708
 
@@ -636,21 +713,43 @@ type FailureToolFields =
636
713
  | ({ tool: MutationToolName } & Fields<typeof MUTATION_WIRE_FIELDS>)
637
714
  | { tool: (typeof NON_MUTATION_TOOLS)[number] };
638
715
 
639
- /** Facts owned by one successful tool, derived from its wire schema. */
640
- export type SuccessFacts<T extends ReportTool> = Static<(typeof SUCCESS_DEFINITIONS)[T]>;
716
+ /** Success facts before projecting them into the model, script, and TUI results. */
717
+ export type SuccessFacts<T extends ReportTool> = Omit<
718
+ Static<(typeof SCRIPT_OUTPUT_SCHEMAS)[T]>,
719
+ "text" | "next"
720
+ >;
721
+
722
+ /** The tool's own part of a mutation outcome. Publication comes directly from the commit. */
723
+ export type MutationFacts<T extends MutationToolName> = {
724
+ [K in T]: ReportVariant<
725
+ { tool: K } & Omit<
726
+ SuccessFacts<K>,
727
+ keyof typeof FILE_PUBLICATION | keyof typeof MUTATION_OUTPUT
728
+ >
729
+ >;
730
+ }[T];
641
731
 
642
732
  // Absent fields stay readable as undefined without admitting another variant's facts.
643
733
  type ReportVariant<T> = T extends unknown
644
734
  ? T & { [K in Exclude<keyof ReportFields, keyof T>]?: never }
645
735
  : never;
646
736
 
647
- /** A success report a tool may produce: its own facts, and mutation facts only for mutation tools. */
737
+ type SuccessEnvelope<T extends ReportTool> = { tool: T } & Pick<Envelope, "path"> &
738
+ (T extends MutationToolName ? Fields<typeof MUTATION_PRESENTATION> : unknown);
739
+
740
+ /** Producer facts are complete. The visibility flags only control what the report repeats. */
741
+ export type SuccessOutcome = {
742
+ [T in ReportTool]: ReportVariant<SuccessEnvelope<T> & SuccessFacts<T>> & {
743
+ callShowsNextOffset?: T extends "read" ? true : never;
744
+ payloadShowsCounts?: T extends "grep" ? true : never;
745
+ };
746
+ }[ReportTool];
747
+
748
+ /** A success report may leave out facts the payload or originating call already shows. */
648
749
  export type SuccessReport = {
649
750
  [T in ReportTool]: ReportVariant<
650
- Envelope & { tool: T } & (T extends MutationToolName
651
- ? Fields<typeof MUTATION_SUCCESS_WIRE_FIELDS>
652
- : unknown) &
653
- SuccessFacts<T>
751
+ SuccessEnvelope<T> &
752
+ Pick<ReportFields, Extract<keyof SuccessFacts<T>, keyof ReportFields> | "next">
654
753
  >;
655
754
  }[ReportTool];
656
755