kibi-cli 2.22.0 → 2.23.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 (142) hide show
  1. package/dist/cli-register-foundation.d.ts.map +1 -1
  2. package/dist/cli-register-foundation.js +1 -0
  3. package/dist/commands/check.d.ts.map +1 -1
  4. package/dist/commands/check.js +51 -3
  5. package/dist/commands/doctor.d.ts.map +1 -1
  6. package/dist/commands/doctor.js +40 -0
  7. package/dist/commands/init-helpers.d.ts +2 -1
  8. package/dist/commands/init-helpers.d.ts.map +1 -1
  9. package/dist/commands/init-helpers.js +3 -0
  10. package/dist/commands/migrate.d.ts +47 -0
  11. package/dist/commands/migrate.d.ts.map +1 -1
  12. package/dist/commands/migrate.js +82 -15
  13. package/dist/commands/sync.d.ts.map +1 -1
  14. package/dist/commands/sync.js +15 -1
  15. package/dist/extractors/markdown.d.ts.map +1 -1
  16. package/dist/extractors/markdown.js +37 -35
  17. package/dist/intent-search.d.ts.map +1 -1
  18. package/dist/intent-search.js +32 -4
  19. package/dist/operations/bootstrap/activation.d.ts +7 -1
  20. package/dist/operations/bootstrap/activation.d.ts.map +1 -1
  21. package/dist/operations/bootstrap/activation.js +27 -2
  22. package/dist/operations/bootstrap/generate.d.ts.map +1 -1
  23. package/dist/operations/bootstrap/generate.js +56 -16
  24. package/dist/operations/bootstrap/guidance-actions.d.ts.map +1 -1
  25. package/dist/operations/bootstrap/guidance-actions.js +14 -2
  26. package/dist/operations/bootstrap/guidance.d.ts.map +1 -1
  27. package/dist/operations/bootstrap/guidance.js +6 -2
  28. package/dist/operations/bootstrap/presentation.d.ts.map +1 -1
  29. package/dist/operations/bootstrap/presentation.js +75 -9
  30. package/dist/operations/bootstrap/types.d.ts +6 -0
  31. package/dist/operations/bootstrap/types.d.ts.map +1 -1
  32. package/dist/operations/migration/kb-sources.d.ts +8 -5
  33. package/dist/operations/migration/kb-sources.d.ts.map +1 -1
  34. package/dist/operations/migration/kb-sources.js +50 -14
  35. package/dist/operations/migration/schema6.d.ts.map +1 -1
  36. package/dist/operations/migration/schema6.js +11 -2
  37. package/dist/operations/migration/source-paths.d.ts +18 -1
  38. package/dist/operations/migration/source-paths.d.ts.map +1 -1
  39. package/dist/operations/migration/source-paths.js +111 -16
  40. package/dist/operations/modeling/suggest-predicates.d.ts.map +1 -1
  41. package/dist/operations/modeling/suggest-predicates.js +22 -7
  42. package/dist/operations/mutation/requirement-semantics.d.ts +16 -0
  43. package/dist/operations/mutation/requirement-semantics.d.ts.map +1 -1
  44. package/dist/operations/mutation/requirement-semantics.js +50 -0
  45. package/dist/operations/mutation/strict-fact.js +1 -1
  46. package/dist/operations/mutation/upsert.d.ts.map +1 -1
  47. package/dist/operations/mutation/upsert.js +2 -1
  48. package/dist/operations/mutation/validation.d.ts.map +1 -1
  49. package/dist/operations/mutation/validation.js +43 -1
  50. package/dist/operations/planning/apply-plan.d.ts.map +1 -1
  51. package/dist/operations/planning/apply-plan.js +38 -9
  52. package/dist/operations/semantic-advisor/analyze-prose.d.ts.map +1 -1
  53. package/dist/operations/semantic-advisor/analyze-prose.js +49 -17
  54. package/dist/operations/semantic-advisor/ingestion-boundary.d.ts.map +1 -1
  55. package/dist/operations/semantic-advisor/ingestion-boundary.js +25 -1
  56. package/dist/operations/semantic-advisor/stored-ledger-drift.d.ts +19 -0
  57. package/dist/operations/semantic-advisor/stored-ledger-drift.d.ts.map +1 -0
  58. package/dist/operations/semantic-advisor/stored-ledger-drift.js +78 -0
  59. package/dist/public/impact/analyzer.d.ts.map +1 -1
  60. package/dist/public/impact/analyzer.js +7 -1
  61. package/dist/public/impact/diagnostics.d.ts.map +1 -1
  62. package/dist/public/impact/diagnostics.js +16 -3
  63. package/dist/public/impact/types.d.ts +2 -0
  64. package/dist/public/impact/types.d.ts.map +1 -1
  65. package/dist/public/operations/catalog.d.ts +7 -2
  66. package/dist/public/operations/catalog.d.ts.map +1 -1
  67. package/dist/public/operations/check-executor.d.ts.map +1 -1
  68. package/dist/public/operations/check-executor.js +10 -3
  69. package/dist/public/operations/check-helpers.d.ts +10 -0
  70. package/dist/public/operations/check-helpers.d.ts.map +1 -1
  71. package/dist/public/operations/check-helpers.js +54 -0
  72. package/dist/public/operations/contracts.d.ts.map +1 -1
  73. package/dist/public/operations/contracts.js +1 -0
  74. package/dist/public/operations/decision-firmness.d.ts +3 -1
  75. package/dist/public/operations/decision-firmness.d.ts.map +1 -1
  76. package/dist/public/operations/decision-firmness.js +13 -3
  77. package/dist/public/operations/discovery-executors.d.ts +18 -0
  78. package/dist/public/operations/discovery-executors.d.ts.map +1 -1
  79. package/dist/public/operations/discovery-executors.js +77 -3
  80. package/dist/public/operations/migration-plan.d.ts +13 -0
  81. package/dist/public/operations/migration-plan.d.ts.map +1 -1
  82. package/dist/public/operations/migration-plan.js +29 -1
  83. package/dist/public/operations/schema6-check-actions.d.ts +4 -0
  84. package/dist/public/operations/schema6-check-actions.d.ts.map +1 -1
  85. package/dist/public/operations/schema6-check-actions.js +39 -2
  86. package/dist/public/operations/source-path-dangling.d.ts +1 -1
  87. package/dist/public/operations/source-path-dangling.d.ts.map +1 -1
  88. package/dist/public/operations/source-path-dangling.js +11 -2
  89. package/dist/public/operations/specs/composite.d.ts +1 -1
  90. package/dist/public/operations/specs/discovery.d.ts +5 -0
  91. package/dist/public/operations/specs/discovery.d.ts.map +1 -1
  92. package/dist/public/operations/specs/discovery.js +5 -0
  93. package/dist/public/operations/specs/planning.d.ts +1 -1
  94. package/dist/public/operations/specs/planning.js +1 -1
  95. package/dist/public/operations/specs/reporting.d.ts.map +1 -1
  96. package/dist/public/operations/specs/reporting.js +6 -4
  97. package/dist/public/operations/specs/semantic.d.ts +1 -1
  98. package/dist/public/operations/specs/semantic.d.ts.map +1 -1
  99. package/dist/public/operations/specs/semantic.js +30 -3
  100. package/dist/public/skills/kibi-bootstrap/SKILL.md +3 -2
  101. package/dist/public/skills/kibi-usage/SKILL.md +13 -5
  102. package/dist/public/skills/kibi-usage/resources/workflows.md +3 -1
  103. package/dist/search-answer.d.ts +14 -0
  104. package/dist/search-answer.d.ts.map +1 -1
  105. package/dist/search-answer.js +42 -4
  106. package/dist/traceability/git-change-snapshot.d.ts +46 -0
  107. package/dist/traceability/git-change-snapshot.d.ts.map +1 -1
  108. package/dist/traceability/git-change-snapshot.js +187 -3
  109. package/dist/traceability/staged-diagnostics.d.ts.map +1 -1
  110. package/dist/traceability/staged-diagnostics.js +5 -6
  111. package/dist/traceability/staged-inventory-drift.d.ts +24 -0
  112. package/dist/traceability/staged-inventory-drift.d.ts.map +1 -0
  113. package/dist/traceability/staged-inventory-drift.js +84 -0
  114. package/dist/traceability/staged-symbols-manifest.d.ts.map +1 -1
  115. package/dist/traceability/staged-symbols-manifest.js +1 -2
  116. package/dist/utils/content-freshness.d.ts +9 -0
  117. package/dist/utils/content-freshness.d.ts.map +1 -1
  118. package/dist/utils/content-freshness.js +33 -0
  119. package/dist/utils/merge-driver-setup.d.ts +10 -0
  120. package/dist/utils/merge-driver-setup.d.ts.map +1 -0
  121. package/dist/utils/merge-driver-setup.js +69 -0
  122. package/dist/utils/rule-registry.generated.d.ts +1 -1
  123. package/dist/utils/rule-registry.generated.js +1 -1
  124. package/package.json +2 -2
  125. package/src/public/impact/analyzer.ts +13 -1
  126. package/src/public/impact/diagnostics.ts +20 -3
  127. package/src/public/impact/types.ts +2 -0
  128. package/src/public/operations/check-executor.ts +16 -4
  129. package/src/public/operations/check-helpers.ts +65 -0
  130. package/src/public/operations/contracts.ts +1 -0
  131. package/src/public/operations/decision-firmness.ts +18 -2
  132. package/src/public/operations/discovery-executors.ts +113 -3
  133. package/src/public/operations/migration-plan.ts +47 -1
  134. package/src/public/operations/schema6-check-actions.ts +43 -2
  135. package/src/public/operations/source-path-dangling.ts +12 -2
  136. package/src/public/operations/specs/discovery.ts +6 -0
  137. package/src/public/operations/specs/planning.ts +1 -1
  138. package/src/public/operations/specs/reporting.ts +6 -4
  139. package/src/public/operations/specs/semantic.ts +41 -4
  140. package/src/public/skills/kibi-bootstrap/SKILL.md +3 -2
  141. package/src/public/skills/kibi-usage/SKILL.md +13 -5
  142. package/src/public/skills/kibi-usage/resources/workflows.md +3 -1
@@ -81,6 +81,12 @@ function getGranularSymbolsForSourceFile(
81
81
  };
82
82
  }
83
83
 
84
+ function hasTraceabilityLinks(symbol: ExtractedSymbol): boolean {
85
+ return (symbol.relationships ?? []).some((relationship) =>
86
+ isTraceabilityRelationshipType(relationship.type),
87
+ );
88
+ }
89
+
84
90
  function formatRelationshipTargets(symbol: ExtractedSymbol): string {
85
91
  const targets = [
86
92
  ...new Set(
@@ -183,15 +189,26 @@ export function createSemanticReviewDiagnostics(
183
189
  if (!isChangedBehavioralReviewCandidate(symbol)) continue;
184
190
 
185
191
  const linkedTargets = formatRelationshipTargets(symbol);
192
+ const file = symbol.location.file;
193
+ // In a file no requirement owns at all, the changed behavior has no
194
+ // recorded intent, so the review is "record it", not "compare it with
195
+ // its links". Elsewhere a coarser symbol of the file may still own it.
196
+ const unlinked =
197
+ !hasTraceabilityLinks(symbol) &&
198
+ !(options.linkedSourceFiles?.has(file) ?? false);
186
199
  diagnostics.push({
187
200
  id: "symbol_semantic_review_needed",
188
201
  severity: "warning",
189
202
  blocking: false,
190
203
  category: "symbol",
191
- files: [symbol.location.file],
204
+ files: [file],
192
205
  docs: ["docs/symbol-traceability-taxonomy.md"],
193
- message: `Changed behavioral symbol ${symbol.name} needs semantic review against linked Kibi coverage: ${linkedTargets}`,
194
- suggestion: `Use kb_search and kb_query for ${symbol.location.file}, then verify that ${linkedTargets} actually describe and test the changed behavior or UI copy. Update Kibi links or requirements/tests while the edit context is fresh.`,
206
+ message: unlinked
207
+ ? `Changed behavioral symbol ${symbol.name} is in a file no requirement owns, so no recorded intent covers the behavior it has after this change`
208
+ : `Changed behavioral symbol ${symbol.name} needs semantic review against linked Kibi coverage: ${linkedTargets}`,
209
+ suggestion: unlinked
210
+ ? `If the change adds or alters behavior, record it with the change: kb_search({query:"<the behavior>", sourceLocations:[{path:"${file}"}]}) for a requirement that already describes it and link ${symbol.name} to it; if none does, kb_upsert a requirement with a scenario and link the symbol with implements. If behavior is unchanged (a refactor, renaming), say so in the final report instead.`
211
+ : `Use kb_search and kb_query for ${file}, then verify that ${linkedTargets} actually describe and test the changed behavior or UI copy. Update Kibi links or requirements/tests while the edit context is fresh.`,
195
212
  });
196
213
  }
197
214
  }
@@ -49,6 +49,8 @@ export type SymbolGranularityDiagnosticsOptions = {
49
49
 
50
50
  export type SemanticReviewDiagnosticsOptions = {
51
51
  readonly symbolsByFile: SymbolsByFile;
52
+ /** Source files where some manifest symbol carries a traceability link. */
53
+ readonly linkedSourceFiles?: ReadonlySet<string>;
52
54
  };
53
55
 
54
56
  export type SymbolQualityDiagnosticsOptions = {
@@ -32,12 +32,14 @@ import {
32
32
  buildSummary,
33
33
  } from "./check-format-shared.js";
34
34
  import {
35
+ SCOPED_CHECK_OMITTED_ID,
35
36
  analyzeKbCheckImpact,
36
37
  collectQueryPlanSafetyViolations,
37
38
  getEffectiveRules,
38
39
  partitionCheckFindings,
39
40
  qualityDiagnosticsFromImpact,
40
41
  resolveCheckRules,
42
+ scopeQualityDiagnosticsToImpact,
41
43
  stagedEntityIdStyleDiagnostics,
42
44
  } from "./check-helpers.js";
43
45
  import { collectCheckPolicyViolations } from "./check-policy-rules.js";
@@ -430,8 +432,17 @@ export async function executeCheck(
430
432
  now: context.clock(),
431
433
  ...maxDiagnosticsOption,
432
434
  });
435
+ // A check scoped to sourceFiles answers about those files; the KB-wide
436
+ // advisory scan only lists what concerns them.
437
+ const advisoryQualityDiagnostics =
438
+ impactResult && args.sourceFiles !== undefined && !hasExplicitRules
439
+ ? scopeQualityDiagnosticsToImpact(
440
+ partitioned.qualityDiagnostics,
441
+ impactResult,
442
+ )
443
+ : partitioned.qualityDiagnostics;
433
444
  const qualityDiagnostics = [
434
- ...partitioned.qualityDiagnostics,
445
+ ...advisoryQualityDiagnostics,
435
446
  ...extraQualityDiagnostics,
436
447
  ];
437
448
 
@@ -439,9 +450,10 @@ export async function executeCheck(
439
450
  const migrationPlan = await migrationPlanForCheck(
440
451
  context,
441
452
  violations,
442
- qualityDiagnostics as unknown as readonly Readonly<
443
- Record<string, unknown>
444
- >[],
453
+ // The omitted-findings count is a pointer, not a repair to plan.
454
+ qualityDiagnostics.filter(
455
+ (diagnostic) => diagnostic.id !== SCOPED_CHECK_OMITTED_ID,
456
+ ) as unknown as readonly Readonly<Record<string, unknown>>[],
445
457
  statusPlan,
446
458
  );
447
459
  return {
@@ -80,6 +80,71 @@ export function partitionCheckFindings(findings: readonly Violation[]): {
80
80
  return { violations, qualityDiagnostics };
81
81
  }
82
82
 
83
+ export const SCOPED_CHECK_OMITTED_ID = "scoped_check_omitted_quality";
84
+ const OMITTED_RULES_SHOWN = 5;
85
+
86
+ /**
87
+ * Advisory rule findings of a check scoped to `sourceFiles` that concern
88
+ * those files: findings on an entity a changed file's symbols link to (or on
89
+ * those symbols) and findings whose source is one of the files. The KB-wide rest (unapproved requirements elsewhere,
90
+ * unrelated scenarios) is reduced to one info diagnostic with per-rule counts,
91
+ * so a check after a small edit answers about that edit; kb_check without
92
+ * sourceFiles still lists every finding.
93
+ */
94
+ // implements REQ-cli-check
95
+ export function scopeQualityDiagnosticsToImpact(
96
+ diagnostics: readonly QualityDiagnostic[],
97
+ impactResult: ChangedFileImpactResult,
98
+ ): QualityDiagnostic[] {
99
+ const normalizePath = (value: string) =>
100
+ value.replace(/\\/g, "/").replace(/^\.\//, "");
101
+ const files = new Set(impactResult.sourceFiles.map(normalizePath));
102
+ const entityIds = new Set<string>();
103
+ for (const linked of impactResult.linkedEntities) {
104
+ entityIds.add(linked.id);
105
+ entityIds.add(linked.sourceSymbolId);
106
+ }
107
+ const inScope = (diagnostic: QualityDiagnostic) =>
108
+ (diagnostic.entityId !== undefined && entityIds.has(diagnostic.entityId)) ||
109
+ (diagnostic.source !== undefined &&
110
+ files.has(normalizePath(diagnostic.source))) ||
111
+ (diagnostic.files ?? []).some((file) => files.has(normalizePath(file)));
112
+
113
+ const kept: QualityDiagnostic[] = [];
114
+ const omittedByRule = new Map<string, number>();
115
+ for (const diagnostic of diagnostics) {
116
+ if (inScope(diagnostic)) {
117
+ kept.push(diagnostic);
118
+ continue;
119
+ }
120
+ const rule = diagnostic.id.replace(/^rule\./, "");
121
+ omittedByRule.set(rule, (omittedByRule.get(rule) ?? 0) + 1);
122
+ }
123
+ if (omittedByRule.size === 0) return kept;
124
+
125
+ const omitted = [...omittedByRule.values()].reduce((a, b) => a + b, 0);
126
+ const rules = [...omittedByRule.entries()].sort(
127
+ (a, b) => b[1] - a[1] || a[0].localeCompare(b[0]),
128
+ );
129
+ const shown = rules
130
+ .slice(0, OMITTED_RULES_SHOWN)
131
+ .map(([rule, count]) => `${rule} ×${count}`);
132
+ if (rules.length > OMITTED_RULES_SHOWN) {
133
+ shown.push(`${rules.length - OMITTED_RULES_SHOWN} more rules`);
134
+ }
135
+ kept.push({
136
+ id: SCOPED_CHECK_OMITTED_ID,
137
+ severity: "info",
138
+ blocking: false,
139
+ category: "integrity",
140
+ message: `This check is scoped to ${files.size} source ${files.size === 1 ? "file" : "files"}; ${omitted} advisory ${omitted === 1 ? "finding" : "findings"} elsewhere in the KB ${omitted === 1 ? "is" : "are"} not listed (${shown.join(", ")}). None of them concerns the checked files.`,
141
+ suggestion:
142
+ "Run kb_check without sourceFiles for the full KB audit when it matters to the task.",
143
+ evidence: { omitted, byRule: Object.fromEntries(rules) },
144
+ });
145
+ return kept;
146
+ }
147
+
83
148
  function resolveChecksPlPath(): string {
84
149
  const override = getKbPlPathOverride();
85
150
  return path.join(path.dirname(override ?? resolveKbPlPath()), "checks.pl");
@@ -447,6 +447,7 @@ const BASE_DATA_SCHEMAS: Readonly<
447
447
  count: integerValue,
448
448
  factQuery: recordValue,
449
449
  branchStore: recordValue,
450
+ body: recordValue,
450
451
  },
451
452
  ["entities", "count"],
452
453
  ),
@@ -63,6 +63,16 @@ export const DECISION_FIRMNESS_RULES = [
63
63
 
64
64
  const DECISION_LANES = ["requirements", "adr"] as const;
65
65
 
66
+ /** The schema version from which a supersede must explain itself. */
67
+ const SUPERSEDES_REASON_SCHEMA_VERSION = 9;
68
+
69
+ /**
70
+ * Told on a KB older than schema 9: the pending schema upgrade acknowledges
71
+ * supersedes recorded before the rule existed, so no edit is required now.
72
+ */
73
+ const PRE_SCHEMA9_SUPERSEDES_NOTE =
74
+ " Supersedes recorded before schema 9 are acknowledged by the plan's schema-config-upgrade (kibi migrate --yes); add the reason only when you next touch the requirement.";
75
+
66
76
  /** Every authored requirement and ADR. */
67
77
  // implements REQ-kb-decision-firmness
68
78
  export function listDecisionEntities(workspaceRoot: string): AuthoredEntity[] {
@@ -164,7 +174,11 @@ export function evaluateDecisionFirmness(
164
174
  edges: ReadonlyMap<string, Set<string>>,
165
175
  rules: ReadonlySet<string> = new Set(DECISION_FIRMNESS_RULES),
166
176
  acknowledged: ReadonlySet<string> = new Set(),
177
+ options: Readonly<{ schemaVersion?: number | null }> = {},
167
178
  ): Violation[] {
179
+ const preSchema9 =
180
+ typeof options.schemaVersion === "number" &&
181
+ options.schemaVersion < SUPERSEDES_REASON_SCHEMA_VERSION;
168
182
  const findings: Violation[] = [];
169
183
  const superseded = supersededIds(edges);
170
184
  const byId = new Map(entities.map((entity) => [entity.id, entity]));
@@ -241,7 +255,7 @@ export function evaluateDecisionFirmness(
241
255
  rule: SUPERSEDES_REASON_MISSING_RULE,
242
256
  entityId: entity.id,
243
257
  description: `${entity.id} supersedes ${unacknowledged.join(", ")} without a '## ${WHY_THIS_CHANGED_HEADING}' section of at least ${MIN_WHY_CHANGED_WORDS} words`,
244
- suggestion: `Add '## ${WHY_THIS_CHANGED_HEADING}' to the body: name the evidence (a failing test, a measured cost, a constraint the old decision missed, a new requirement) and answer the superseded decision's reasoning. A preference is not evidence; propose it instead of superseding.`,
258
+ suggestion: `Add '## ${WHY_THIS_CHANGED_HEADING}' to the body: name the evidence (a failing test, a measured cost, a constraint the old decision missed, a new requirement) and answer the superseded decision's reasoning. A preference is not evidence; propose it instead of superseding.${preSchema9 ? PRE_SCHEMA9_SUPERSEDES_NOTE : ""}`,
245
259
  source: entity.path,
246
260
  evidence: { type: entity.type, supersedes: unacknowledged },
247
261
  });
@@ -271,10 +285,12 @@ export function collectDecisionFirmnessViolations(
271
285
  ): Violation[] {
272
286
  if (!DECISION_FIRMNESS_RULES.some((rule) => rules.has(rule))) return [];
273
287
  const entities = listDecisionEntities(workspaceRoot);
288
+ const manifest = readKbManifest(workspaceRoot);
274
289
  return evaluateDecisionFirmness(
275
290
  entities,
276
291
  authoredSupersedes(workspaceRoot, entities),
277
292
  rules,
278
- new Set(readKbManifest(workspaceRoot)?.supersedesReasonAcknowledged ?? []),
293
+ new Set(manifest?.supersedesReasonAcknowledged ?? []),
294
+ { schemaVersion: manifest?.schemaVersion ?? null },
279
295
  );
280
296
  }
@@ -1,3 +1,4 @@
1
+ import { readFile } from "node:fs/promises";
1
2
  import path from "node:path";
2
3
  import { compilerFingerprintReason } from "../../commands/sync/cache.js";
3
4
  import { effectiveFirmness } from "../../decision-firmness.js";
@@ -34,6 +35,7 @@ import {
34
35
  reloadFullEntities,
35
36
  validateEntityType,
36
37
  } from "./discovery-entities.js";
38
+ import { readAuthoredEntity } from "./entity-context.js";
37
39
  import {
38
40
  type MigrationConfigStatus,
39
41
  type MigrationPlan,
@@ -67,6 +69,8 @@ export type QueryInput = {
67
69
  readonly distinct?: string;
68
70
  readonly subjectKey?: string;
69
71
  readonly propertyKey?: string;
72
+ /** With `id`: also return the entity's authored Markdown body. */
73
+ readonly includeBody?: boolean;
70
74
  };
71
75
 
72
76
  /** A ready-to-run follow-up call the query suggests. */
@@ -97,8 +101,26 @@ export type QueryPayload = {
97
101
  readonly factQuery?: FactQueryPayload;
98
102
  /** Present when an empty answer came from a store never compiled for this branch. */
99
103
  readonly branchStore?: SearchBranchStoreNotice;
104
+ /** Present when `includeBody` was requested for an `id`. */
105
+ readonly body?: QueryBody;
100
106
  };
101
107
 
108
+ /**
109
+ * The authored Markdown body of the entity a kb_query `id` names: the text
110
+ * after its front matter, which kb_upsert `document.body` replaces. `text` is
111
+ * null when the entity has no readable authored document (a symbol, or a
112
+ * source that is not a workspace Markdown file).
113
+ */
114
+ export type QueryBody = Readonly<{
115
+ id: string;
116
+ path: string | null;
117
+ text: string | null;
118
+ truncated?: boolean;
119
+ }>;
120
+
121
+ /** Bodies longer than this are cut; the file itself stays the full record. */
122
+ export const QUERY_BODY_MAX_CHARS = 32_000;
123
+
102
124
  export const FACT_KINDS = [
103
125
  "subject",
104
126
  "property_value",
@@ -684,6 +706,11 @@ export async function executeQuery(
684
706
  const { type, id, tags, sourceFile, limit = 100, offset = 0 } = input;
685
707
  try {
686
708
  validateEntityType(type);
709
+ if (input.includeBody === true && (id === undefined || id === "")) {
710
+ throw new Error(
711
+ "includeBody needs id: it returns the authored body of one entity. Pass the entity's id.",
712
+ );
713
+ }
687
714
  if (hasTypedFactFilters(input)) {
688
715
  return await executeFactQuery(input, context, limit, offset);
689
716
  }
@@ -729,12 +756,23 @@ export async function executeQuery(
729
756
  return `${entityId} (${String(entity.title ?? "")}, status=${String(entity.status ?? "")})`;
730
757
  })
731
758
  .join(", ")}`;
759
+ const body =
760
+ input.includeBody === true && id !== undefined
761
+ ? await readQueryBody(
762
+ id,
763
+ paginated.find((entity) => entity.id === id),
764
+ context,
765
+ )
766
+ : undefined;
732
767
  return {
733
- content: [{ type: "text", text }],
768
+ content: [
769
+ { type: "text", text: body ? `${text}\n${queryBodyText(body)}` : text },
770
+ ],
734
771
  structuredContent: {
735
772
  entities: paginated,
736
773
  count: indexedPage.count,
737
774
  ...(storeNotice ? { branchStore: storeNotice } : {}),
775
+ ...(body ? { body } : {}),
738
776
  },
739
777
  };
740
778
  } catch (error) {
@@ -743,6 +781,55 @@ export async function executeQuery(
743
781
  }
744
782
  }
745
783
 
784
+ // The body is read from the entity's authored document inside the
785
+ // workspace, the same file kb_upsert rewrites; nothing outside it is read.
786
+ // implements REQ-kibi-operation-interface-parity
787
+ async function readQueryBody(
788
+ id: string,
789
+ entity: Readonly<Record<string, unknown>> | undefined,
790
+ context: OperationContext,
791
+ ): Promise<QueryBody> {
792
+ const source = typeof entity?.source === "string" ? entity.source : "";
793
+ const relative = source.split("#", 1)[0]?.trim() ?? "";
794
+ const root = path.resolve(context.workspaceRoot);
795
+ const resolved = path.resolve(root, relative);
796
+ if (!relative.endsWith(".md") || !resolved.startsWith(root + path.sep)) {
797
+ return { id, path: null, text: null };
798
+ }
799
+ const bodyPath = path.relative(root, resolved).split(path.sep).join("/");
800
+ let raw: string;
801
+ try {
802
+ raw = String(
803
+ context.fs
804
+ ? await context.fs.readFile(resolved)
805
+ : await readFile(resolved, "utf8"),
806
+ );
807
+ } catch {
808
+ return { id, path: bodyPath, text: null };
809
+ }
810
+ const authored = readAuthoredEntity(raw, bodyPath)?.body;
811
+ if (authored === undefined || authored === null) {
812
+ return { id, path: bodyPath, text: null };
813
+ }
814
+ return authored.length > QUERY_BODY_MAX_CHARS
815
+ ? {
816
+ id,
817
+ path: bodyPath,
818
+ text: authored.slice(0, QUERY_BODY_MAX_CHARS),
819
+ truncated: true,
820
+ }
821
+ : { id, path: bodyPath, text: authored };
822
+ }
823
+
824
+ function queryBodyText(body: QueryBody): string {
825
+ if (body.text === null) {
826
+ return body.path === null
827
+ ? `No authored document for ${body.id}, so there is no body to return.`
828
+ : `The authored document ${body.path} of ${body.id} could not be read.`;
829
+ }
830
+ return `Body of ${body.id} (${body.path}${body.truncated ? `, first ${QUERY_BODY_MAX_CHARS} characters` : ""}; replace it with kb_upsert document.body):\n${body.text}`;
831
+ }
832
+
746
833
  /**
747
834
  * Identifying metadata a caller needs to decide which hits to open.
748
835
  *
@@ -792,6 +879,13 @@ async function projectMatches<
792
879
  : matches.map(summarizeMatch);
793
880
  }
794
881
 
882
+ // A question about contradictions is answered by the check, not by the
883
+ // entities that happen to mention the word.
884
+ const CONTRADICTION_QUESTION =
885
+ /\b(contradict\w*|conflict\w*|inconsisten\w*)\b/i;
886
+ const CONTRADICTION_HINT =
887
+ ' Search only finds entities that mention a conflict. Whether current requirements contradict each other is decided by kb_check with rules ["domain-contradictions"]; what a proposed rule would conflict with, by kb_model mode predicates (decisionConflicts) on its text or a kb_upsert dryRun.';
888
+
795
889
  export async function executeSearch(
796
890
  input: SearchInput,
797
891
  context: OperationContext,
@@ -864,14 +958,14 @@ export async function executeSearch(
864
958
  );
865
959
  const text = `${
866
960
  intentResult.matches.length === 0
867
- ? `No intent search results for '${trimmedQuery}' (abstained).`
961
+ ? `No intent search results for '${trimmedQuery}' (abstained). No entity shares enough of the question's words: ask again in the words and language the KB is written in, or pass sourceLocations.`
868
962
  : `Found ${intentResult.matches.length} intent search results for '${trimmedQuery}'${intentResult.analysis.ambiguous ? " (top matches are close; confirm before relying on one)" : ""}. Top: ${paginated
869
963
  .slice(0, 5)
870
964
  .map((match) => String(match.entity.id ?? ""))
871
965
  .join(
872
966
  ", ",
873
967
  )}${answerLayer ? `. Governing: ${answerLayer.governing.map((req) => req.id).join(", ") || "none found"}` : ""}${topUngoverned ? ` (the top match ${topUngoverned.id} is linked to no requirement)` : ""}.`
874
- }${storeNote}`;
968
+ }${CONTRADICTION_QUESTION.test(trimmedQuery) ? CONTRADICTION_HINT : ""}${storeNote}`;
875
969
  return {
876
970
  content: [{ type: "text", text }],
877
971
  structuredContent: {
@@ -1121,6 +1215,13 @@ export async function executeStatus(
1121
1215
  : { proofSnapshotError: snapshotEvidence.error }),
1122
1216
  };
1123
1217
  const schemaStatus = readMigrationConfigStatus(context.workspaceRoot);
1218
+ // implements REQ-agent-guided-migration-orchestration
1219
+ // A schema upgrade plans the source rewrites kibi migrate performs with
1220
+ // it; the planner loads only when an upgrade is due.
1221
+ const sourcePlanner = schemaStatus.needsMigration
1222
+ ? (await import("../../operations/migration/schema6.js"))
1223
+ .buildSchema6MigrationFragment
1224
+ : undefined;
1124
1225
  const migrationPlan = buildActionsFromStatus({
1125
1226
  workspaceRoot: context.workspaceRoot,
1126
1227
  branchAttachment: attachment,
@@ -1135,6 +1236,15 @@ export async function executeStatus(
1135
1236
  ? snapshotEvidence.snapshot.hash
1136
1237
  : null,
1137
1238
  configStatus: schemaStatus,
1239
+ ...(sourcePlanner !== undefined
1240
+ ? {
1241
+ planSchemaUpgradeSources: (currentSchemaVersion: number | null) =>
1242
+ sourcePlanner({
1243
+ workspaceRoot: context.workspaceRoot,
1244
+ currentSchemaVersion,
1245
+ }),
1246
+ }
1247
+ : {}),
1138
1248
  });
1139
1249
  const listed = context.fs?.glob
1140
1250
  ? await context.fs.glob(
@@ -616,6 +616,14 @@ export function buildActionsFromCoverage(input: {
616
616
  return actions;
617
617
  }
618
618
 
619
+ /** Source-rewrite actions a schema upgrade performs, as its planner returns them. */
620
+ export type SchemaUpgradeSourceActions = Readonly<{
621
+ actions: readonly MigrationAction[];
622
+ /** Automatic actions that rewrite authored sources; the upgrade waits on them. */
623
+ sourceRewriteActionIds: readonly string[];
624
+ diagnostics: readonly string[];
625
+ }>;
626
+
619
627
  export type MigrationConfigStatus = Readonly<{
620
628
  status: "missing" | "invalid" | "older" | "current" | "newer";
621
629
  currentVersion: number | null;
@@ -706,8 +714,17 @@ export function buildActionsFromStatus(input: {
706
714
  kbSnapshotId?: string | null;
707
715
  workspaceSnapshot?: string | null;
708
716
  configStatus?: MigrationConfigStatus;
717
+ /**
718
+ * Plans the authored-source rewrites `kibi migrate --yes` performs on its
719
+ * way to the latest schema (the planner `kibi migrate` itself uses), so
720
+ * the schema upgrade declares every file it touches. Read-only.
721
+ */
722
+ planSchemaUpgradeSources?: (
723
+ currentSchemaVersion: number | null,
724
+ ) => SchemaUpgradeSourceActions;
709
725
  }): MigrationPlan {
710
726
  const actions: MigrationAction[] = [];
727
+ const diagnostics: string[] = [];
711
728
  const attachment = input.branchAttachment;
712
729
  const store = input.branchStore;
713
730
  const configStatus =
@@ -813,6 +830,21 @@ export function buildActionsFromStatus(input: {
813
830
  if (configStatus.needsMigration || legacyStorageMigrationRequired) {
814
831
  const operatorOnly =
815
832
  configStatus.status === "newer" || legacyStorageBlocked;
833
+ // implements REQ-agent-guided-migration-orchestration, REQ-cli-schema-migration
834
+ // The source rewrites the migrate run performs (legacy-link marks, origin
835
+ // backfill, lifecycle repairs, ...) are planned as their own actions with
836
+ // their own files, and the upgrade waits on them, so approving it means
837
+ // approving them. Knowledge still outside .kb/ is planned after it moves.
838
+ const sources =
839
+ !operatorOnly &&
840
+ !legacyStorageMigrationRequired &&
841
+ input.planSchemaUpgradeSources !== undefined
842
+ ? input.planSchemaUpgradeSources(configStatus.currentVersion)
843
+ : null;
844
+ if (sources !== null) {
845
+ actions.push(...sources.actions);
846
+ diagnostics.push(...sources.diagnostics);
847
+ }
816
848
  actions.push(
817
849
  migrationAction({
818
850
  id: "schema-config-upgrade",
@@ -835,11 +867,21 @@ export function buildActionsFromStatus(input: {
835
867
  ".kb/manifest.json",
836
868
  ".kb/migrations",
837
869
  ".kb/config.json",
870
+ // The migrate run keeps the canonical derived entries ignored.
871
+ ".gitignore",
872
+ // Unplanned source rewrites can touch any authored .kb/ file.
873
+ ...(sources === null ? [".kb/"] : []),
838
874
  ...legacyStorage.moves.map((move) => move.from),
839
875
  ...legacyStorage.removals.map((removal) => removal.path),
840
876
  ],
841
877
  evidence: {
842
878
  configStatus,
879
+ ...(sources === null
880
+ ? {
881
+ sourceRewrites:
882
+ "Not planned as separate actions: kibi migrate --yes also marks coarse symbol links legacy-link, backfills origin and repairs lifecycle findings in authored .kb/ sources. The plan lists them as their own actions once the upgrade can run automatically on the canonical .kb/ layout.",
883
+ }
884
+ : {}),
843
885
  ...(legacyStorageMigrationRequired
844
886
  ? {
845
887
  legacyStorage: {
@@ -852,7 +894,10 @@ export function buildActionsFromStatus(input: {
852
894
  }
853
895
  : {}),
854
896
  },
855
- dependsOn: storeState === "missing" ? ["branch-store-ensure"] : [],
897
+ dependsOn: [
898
+ ...(storeState === "missing" ? ["branch-store-ensure"] : []),
899
+ ...(sources?.sourceRewriteActionIds ?? []),
900
+ ],
856
901
  autoApplicable: !operatorOnly,
857
902
  }),
858
903
  );
@@ -1021,5 +1066,6 @@ export function buildActionsFromStatus(input: {
1021
1066
  ],
1022
1067
  incompleteDomains,
1023
1068
  actions,
1069
+ diagnostics,
1024
1070
  });
1025
1071
  }
@@ -208,6 +208,8 @@ export function supersessionCycleReviewActionInput(cycle: {
208
208
  export type SourcePathRewriteInput = Readonly<{
209
209
  entityId: string;
210
210
  file: string;
211
+ /** The field rewritten; absent means `source`. */
212
+ field?: "text_ref";
211
213
  from: string;
212
214
  to: string;
213
215
  }>;
@@ -260,7 +262,7 @@ export function sourcePathRewriteActionInput(
260
262
  ],
261
263
  evidence: {
262
264
  reason:
263
- "The authored source field is dead data: the compiled source is always the entity's own file, and Kibi ignores the field when compiling and never writes it. Each rewrite points a knowledge file named by its pre-canonical path (under documentation/, or relative to the knowledge root) at the same file under .kb/. Each removal deletes a value that names the entity's own file (reason self) or nothing Kibi can map (reason dangling). Only the source line changes.",
265
+ "The authored source field is dead data: the compiled source is always the entity's own file, and Kibi ignores the field when compiling and never writes it. Each rewrite points a knowledge file named by its pre-canonical path (under documentation/, or relative to the knowledge root) at the same file under .kb/; a rewrite with field text_ref does the same for a text_ref evidence path. Each removal deletes a source value that names the entity's own file (reason self) or nothing Kibi can map (reason dangling). Only the rewritten or removed line changes.",
264
266
  count: all.length,
265
267
  rewrites: rewrites.map((rewrite) => ({ ...rewrite })),
266
268
  removals: removals.map((removal) => ({ ...removal })),
@@ -275,6 +277,8 @@ export function sourcePathRewriteActionInput(
275
277
  // implements REQ-kibi-schema6-migration, REQ-core-validation-rules, REQ-kibi-kb-lifecycle-integrity
276
278
  export function sourcePathReviewActionInput(source: {
277
279
  entityId: string;
280
+ /** The field to repair; absent means `source`. */
281
+ field?: "text_ref";
278
282
  value: unknown;
279
283
  file?: string;
280
284
  reason?: string;
@@ -285,6 +289,32 @@ export function sourcePathReviewActionInput(source: {
285
289
  ? source.value
286
290
  : JSON.stringify(source.value);
287
291
  const where = source.file !== undefined ? ` in ${source.file}` : "";
292
+ if (source.field === "text_ref") {
293
+ const edit =
294
+ source.rewrite !== undefined
295
+ ? `replace the whole field with the single line 'text_ref: ${source.rewrite}'`
296
+ : "point text_ref at the evidence file's current path, or delete the field when that evidence is gone";
297
+ return {
298
+ id: `review-text-ref-dangling-${stableIdPart(source.entityId)}`,
299
+ code: SOURCE_PATH_REVIEW_CODE,
300
+ category: "quality",
301
+ safety: "review",
302
+ invocation: {
303
+ kind: "review",
304
+ instruction: `${source.entityId} has text_ref '${shown}'${where}, a file path that does not exist (${source.reason ?? "the edit would not read back as exactly that change"}). A person edits the frontmatter by hand: ${edit}. Then rerun kibi migrate.`,
305
+ },
306
+ affectedEntityIds: [source.entityId],
307
+ affectedFiles: source.file !== undefined ? [source.file] : [],
308
+ dispositionRequired: true,
309
+ evidence: {
310
+ entityId: source.entityId,
311
+ field: "text_ref",
312
+ text_ref: source.value,
313
+ ...(source.reason !== undefined ? { refused: source.reason } : {}),
314
+ ...(source.rewrite !== undefined ? { rewrite: source.rewrite } : {}),
315
+ },
316
+ };
317
+ }
288
318
  const edit =
289
319
  source.rewrite !== undefined
290
320
  ? `replace the whole field with the single line 'source: ${source.rewrite}'`
@@ -367,10 +397,12 @@ export function lifecycleActionsFromViolations(
367
397
  typeof evidence.value === "string"
368
398
  ? evidence.value
369
399
  : (JSON.stringify(evidence.value) ?? "");
400
+ const textRef = evidence.field === "text_ref";
370
401
  if (refused !== "" || file === "") {
371
402
  actions.push(
372
403
  sourcePathReviewActionInput({
373
404
  entityId,
405
+ ...(textRef ? { field: "text_ref" as const } : {}),
374
406
  value: evidence.value,
375
407
  ...(file !== "" ? { file } : {}),
376
408
  ...(refused !== "" ? { reason: refused } : {}),
@@ -378,7 +410,16 @@ export function lifecycleActionsFromViolations(
378
410
  }),
379
411
  );
380
412
  } else if (rewrite !== "") {
381
- rewrites.push({ entityId, file, from, to: rewrite });
413
+ rewrites.push({
414
+ entityId,
415
+ file,
416
+ ...(textRef ? { field: "text_ref" as const } : {}),
417
+ from,
418
+ to: rewrite,
419
+ });
420
+ } else if (textRef) {
421
+ // Kibi never removes evidence; an unmapped text_ref carries refused.
422
+ continue;
382
423
  } else {
383
424
  removals.push({
384
425
  entityId,
@@ -23,11 +23,17 @@ import {
23
23
  } from "../../operations/migration/source-paths.js";
24
24
  import type { Violation } from "../../utils/rule-registry.js";
25
25
 
26
- /** An authored source field that names no path, entity or URL. */
26
+ /** An authored source field that names no path, entity or URL, or a path-shaped text_ref that names no file. */
27
27
  // implements REQ-core-validation-rules, REQ-kibi-kb-lifecycle-integrity
28
28
  export const SOURCE_PATH_DANGLING_RULE = "source-path-dangling";
29
29
 
30
30
  function suggestion(source: SourceRepair): string {
31
+ if (source.field === "text_ref") {
32
+ if (source.refused !== undefined) {
33
+ return `kibi migrate cannot repair this text_ref (${source.refused}), so it plans a review_source_path_dangling action. Point text_ref at the evidence file's current path, or remove it when the evidence is gone.`;
34
+ }
35
+ return `The file moved to ${source.fix.kind === "rewrite" ? source.fix.to : source.file}; kibi migrate rewrites the text_ref field there (source_path_rewrite).`;
36
+ }
31
37
  const compiled = `The compiled source is always the entity's own file (${source.file}), so the field carries nothing Kibi compiles.`;
32
38
  if (source.refused !== undefined) {
33
39
  return `kibi migrate cannot edit this source field safely (${source.refused}), so it plans a review_source_path_dangling action for a person. ${compiled}`;
@@ -54,10 +60,14 @@ export function sourcePathDanglingViolations(
54
60
  return dangling.map((source) => ({
55
61
  rule: SOURCE_PATH_DANGLING_RULE,
56
62
  entityId: source.entityId,
57
- description: `${source.entityId} names source '${shownSourceValue(source.value)}', which is not an existing workspace path, an entity id or an http(s) URL`,
63
+ description:
64
+ source.field === "text_ref"
65
+ ? `${source.entityId} names text_ref '${shownSourceValue(source.value)}', a file path that does not exist in the workspace`
66
+ : `${source.entityId} names source '${shownSourceValue(source.value)}', which is not an existing workspace path, an entity id or an http(s) URL`,
58
67
  suggestion: suggestion(source),
59
68
  source: source.file,
60
69
  evidence: {
70
+ ...(source.field === "text_ref" ? { field: "text_ref" } : {}),
61
71
  value: source.value,
62
72
  file: source.file,
63
73
  ...(source.fix.kind === "rewrite"
@@ -132,6 +132,12 @@ export const querySpec = {
132
132
  description:
133
133
  "Optional zero-based pagination offset. Default: 0. Example: 50 to skip first 50 rows.",
134
134
  },
135
+ includeBody: {
136
+ type: "boolean",
137
+ default: false,
138
+ description:
139
+ "With id only: also return the entity's authored Markdown body (the prose after its front matter) as body {id, path, text}, so a stale statement in it can be corrected; kb_upsert document.body replaces it. Default: false.",
140
+ },
135
141
  },
136
142
  },
137
143
  requiresProlog: true,