kibi-cli 0.16.1 → 0.18.1

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 (229) hide show
  1. package/dist/cli-json-command.d.ts.map +1 -1
  2. package/dist/cli-json-command.js +87 -11
  3. package/dist/cli-register-maintenance.d.ts.map +1 -1
  4. package/dist/cli-register-maintenance.js +8 -0
  5. package/dist/cli-register-reporting.d.ts.map +1 -1
  6. package/dist/cli-register-reporting.js +5 -0
  7. package/dist/cli.d.ts.map +1 -1
  8. package/dist/cli.js +2 -1
  9. package/dist/commands/aggregated-checks.d.ts.map +1 -1
  10. package/dist/commands/aggregated-checks.js +1 -0
  11. package/dist/commands/check.d.ts.map +1 -1
  12. package/dist/commands/check.js +1 -1
  13. package/dist/commands/coverage.d.ts +5 -0
  14. package/dist/commands/coverage.d.ts.map +1 -1
  15. package/dist/commands/coverage.js +5 -0
  16. package/dist/commands/discovery-table.d.ts.map +1 -1
  17. package/dist/commands/discovery-table.js +96 -5
  18. package/dist/commands/sync/cache.d.ts +2 -0
  19. package/dist/commands/sync/cache.d.ts.map +1 -1
  20. package/dist/commands/sync/cache.js +8 -0
  21. package/dist/commands/sync/manifest.d.ts +12 -1
  22. package/dist/commands/sync/manifest.d.ts.map +1 -1
  23. package/dist/commands/sync/manifest.js +22 -5
  24. package/dist/commands/sync/persistence.d.ts.map +1 -1
  25. package/dist/commands/sync/persistence.js +76 -2
  26. package/dist/commands/sync.d.ts.map +1 -1
  27. package/dist/commands/sync.js +72 -15
  28. package/dist/commands/usage-metrics.d.ts +66 -2
  29. package/dist/commands/usage-metrics.d.ts.map +1 -1
  30. package/dist/commands/usage-metrics.js +34 -24
  31. package/dist/commands/usage-remediation.d.ts +9 -0
  32. package/dist/commands/usage-remediation.d.ts.map +1 -0
  33. package/dist/commands/usage-remediation.js +52 -0
  34. package/dist/extractors/markdown.d.ts +17 -1
  35. package/dist/extractors/markdown.d.ts.map +1 -1
  36. package/dist/extractors/markdown.js +81 -2
  37. package/dist/logic/ir.d.ts +112 -0
  38. package/dist/logic/ir.d.ts.map +1 -0
  39. package/dist/logic/ir.js +729 -0
  40. package/dist/operations/modeling/logic-modeling.d.ts +22 -0
  41. package/dist/operations/modeling/logic-modeling.d.ts.map +1 -0
  42. package/dist/operations/modeling/logic-modeling.js +120 -0
  43. package/dist/operations/modeling/model-requirement.d.ts.map +1 -1
  44. package/dist/operations/modeling/model-requirement.js +83 -2
  45. package/dist/operations/modeling/predicate-applyplan.d.ts +1 -1
  46. package/dist/operations/modeling/predicate-applyplan.d.ts.map +1 -1
  47. package/dist/operations/modeling/predicate-applyplan.js +29 -4
  48. package/dist/operations/modeling/predicate-inference.d.ts.map +1 -1
  49. package/dist/operations/modeling/predicate-inference.js +3 -0
  50. package/dist/operations/modeling/predicate-loader.d.ts.map +1 -1
  51. package/dist/operations/modeling/predicate-loader.js +7 -1
  52. package/dist/operations/modeling/predicate-ranker.d.ts.map +1 -1
  53. package/dist/operations/modeling/predicate-ranker.js +40 -2
  54. package/dist/operations/modeling/predicate-types.d.ts +12 -1
  55. package/dist/operations/modeling/predicate-types.d.ts.map +1 -1
  56. package/dist/operations/modeling/requirement-types.d.ts +12 -0
  57. package/dist/operations/modeling/requirement-types.d.ts.map +1 -1
  58. package/dist/operations/modeling/suggest-predicates.d.ts.map +1 -1
  59. package/dist/operations/modeling/suggest-predicates.js +35 -11
  60. package/dist/operations/mutation/audit.js +1 -1
  61. package/dist/operations/mutation/contradictions.d.ts +1 -1
  62. package/dist/operations/mutation/contradictions.d.ts.map +1 -1
  63. package/dist/operations/mutation/contradictions.js +23 -18
  64. package/dist/operations/mutation/relationships.d.ts.map +1 -1
  65. package/dist/operations/mutation/relationships.js +8 -0
  66. package/dist/operations/mutation/serialization.d.ts.map +1 -1
  67. package/dist/operations/mutation/serialization.js +6 -0
  68. package/dist/operations/mutation/strict-fact.d.ts.map +1 -1
  69. package/dist/operations/mutation/strict-fact.js +68 -3
  70. package/dist/operations/mutation/upsert.d.ts +3 -1
  71. package/dist/operations/mutation/upsert.d.ts.map +1 -1
  72. package/dist/operations/mutation/upsert.js +51 -12
  73. package/dist/operations/mutation/validate-upsert.d.ts.map +1 -1
  74. package/dist/operations/mutation/validate-upsert.js +18 -2
  75. package/dist/operations/mutation/validation.d.ts.map +1 -1
  76. package/dist/operations/mutation/validation.js +8 -0
  77. package/dist/operations/semantic-advisor/analysis-receipt.d.ts +2 -2
  78. package/dist/operations/semantic-advisor/analysis-receipt.d.ts.map +1 -1
  79. package/dist/operations/semantic-advisor/analysis-receipt.js +30 -16
  80. package/dist/operations/semantic-advisor/analyze-prose.d.ts +1 -1
  81. package/dist/operations/semantic-advisor/analyze-prose.d.ts.map +1 -1
  82. package/dist/operations/semantic-advisor/analyze-prose.js +464 -27
  83. package/dist/operations/semantic-advisor/clauses.d.ts +1 -0
  84. package/dist/operations/semantic-advisor/clauses.d.ts.map +1 -1
  85. package/dist/operations/semantic-advisor/clauses.js +7 -7
  86. package/dist/operations/semantic-advisor/ingestion-boundary.d.ts +18 -0
  87. package/dist/operations/semantic-advisor/ingestion-boundary.d.ts.map +1 -0
  88. package/dist/operations/semantic-advisor/ingestion-boundary.js +167 -0
  89. package/dist/operations/semantic-advisor/observation-plan.d.ts.map +1 -1
  90. package/dist/operations/semantic-advisor/observation-plan.js +15 -3
  91. package/dist/operations/semantic-advisor/shared.d.ts +9 -0
  92. package/dist/operations/semantic-advisor/shared.d.ts.map +1 -1
  93. package/dist/operations/semantic-advisor/shared.js +38 -4
  94. package/dist/operations/semantic-advisor/types.d.ts +66 -1
  95. package/dist/operations/semantic-advisor/types.d.ts.map +1 -1
  96. package/dist/prolog/codec.d.ts.map +1 -1
  97. package/dist/prolog/codec.js +19 -2
  98. package/dist/prolog.d.ts +6 -0
  99. package/dist/prolog.d.ts.map +1 -1
  100. package/dist/prolog.js +247 -63
  101. package/dist/public/diagnostic-usage.d.ts +17 -0
  102. package/dist/public/diagnostic-usage.d.ts.map +1 -0
  103. package/dist/public/diagnostic-usage.js +152 -0
  104. package/dist/public/distribution-parity.d.ts +90 -0
  105. package/dist/public/distribution-parity.d.ts.map +1 -0
  106. package/dist/public/distribution-parity.js +367 -0
  107. package/dist/public/impact/full-kb-quality.d.ts +2 -0
  108. package/dist/public/impact/full-kb-quality.d.ts.map +1 -1
  109. package/dist/public/impact/full-kb-quality.js +53 -1
  110. package/dist/public/impact-diagnostics.d.ts +2 -0
  111. package/dist/public/impact-diagnostics.d.ts.map +1 -1
  112. package/dist/public/impact-diagnostics.js +1 -0
  113. package/dist/public/operations/catalog.d.ts +235 -11
  114. package/dist/public/operations/catalog.d.ts.map +1 -1
  115. package/dist/public/operations/check-executor.d.ts.map +1 -1
  116. package/dist/public/operations/check-executor.js +4 -0
  117. package/dist/public/operations/discovery-executors.d.ts +6 -0
  118. package/dist/public/operations/discovery-executors.d.ts.map +1 -1
  119. package/dist/public/operations/discovery-executors.js +18 -2
  120. package/dist/public/operations/index.d.ts +5 -0
  121. package/dist/public/operations/index.d.ts.map +1 -1
  122. package/dist/public/operations/index.js +5 -0
  123. package/dist/public/operations/legacy-migration-plan.d.ts +113 -0
  124. package/dist/public/operations/legacy-migration-plan.d.ts.map +1 -0
  125. package/dist/public/operations/legacy-migration-plan.js +409 -0
  126. package/dist/public/operations/node-ports.d.ts.map +1 -1
  127. package/dist/public/operations/node-ports.js +90 -0
  128. package/dist/public/operations/repair-plan.d.ts +184 -0
  129. package/dist/public/operations/repair-plan.d.ts.map +1 -0
  130. package/dist/public/operations/repair-plan.js +397 -0
  131. package/dist/public/operations/runtime-types.d.ts +7 -0
  132. package/dist/public/operations/runtime-types.d.ts.map +1 -1
  133. package/dist/public/operations/specs/check.d.ts +3 -3
  134. package/dist/public/operations/specs/check.d.ts.map +1 -1
  135. package/dist/public/operations/specs/check.js +5 -2
  136. package/dist/public/operations/specs/discovery.d.ts +1 -1
  137. package/dist/public/operations/specs/discovery.js +1 -1
  138. package/dist/public/operations/specs/modeling.d.ts +35 -1
  139. package/dist/public/operations/specs/modeling.d.ts.map +1 -1
  140. package/dist/public/operations/specs/modeling.js +32 -1
  141. package/dist/public/operations/specs/mutation-schemas.d.ts +133 -3
  142. package/dist/public/operations/specs/mutation-schemas.d.ts.map +1 -1
  143. package/dist/public/operations/specs/mutation-schemas.js +67 -1
  144. package/dist/public/operations/specs/reporting.d.ts +42 -1
  145. package/dist/public/operations/specs/reporting.d.ts.map +1 -1
  146. package/dist/public/operations/specs/reporting.js +70 -6
  147. package/dist/public/operations/specs/semantic.d.ts +30 -2
  148. package/dist/public/operations/specs/semantic.d.ts.map +1 -1
  149. package/dist/public/operations/specs/semantic.js +35 -3
  150. package/dist/public/operations/specs/upsert.d.ts +133 -3
  151. package/dist/public/operations/specs/upsert.d.ts.map +1 -1
  152. package/dist/public/operations/workspace-snapshot.d.ts +10 -0
  153. package/dist/public/operations/workspace-snapshot.d.ts.map +1 -0
  154. package/dist/public/operations/workspace-snapshot.js +20 -0
  155. package/dist/public/schemas/entity.d.ts.map +1 -1
  156. package/dist/public/schemas/entity.js +332 -0
  157. package/dist/public/schemas/relationship.d.ts.map +1 -1
  158. package/dist/public/schemas/relationship.js +1 -0
  159. package/dist/public/skills/kibi-traceability/SKILL.md +7 -1
  160. package/dist/public/skills/kibi-usage/SKILL.md +44 -12
  161. package/dist/public/skills/kibi-usage/resources/fact-lanes.md +12 -2
  162. package/dist/public/skills/kibi-usage/resources/logic-ir.md +43 -0
  163. package/dist/public/skills/kibi-usage/resources/operation-access.md +19 -19
  164. package/dist/public/skills/kibi-usage/resources/ui-requirements.md +96 -0
  165. package/dist/public/skills/kibi-usage/resources/workflows.md +40 -8
  166. package/dist/public/symbol-granularity.d.ts +9 -0
  167. package/dist/public/symbol-granularity.d.ts.map +1 -1
  168. package/dist/public/symbol-granularity.js +16 -0
  169. package/dist/public/telemetry-acceptance.d.ts +89 -0
  170. package/dist/public/telemetry-acceptance.d.ts.map +1 -0
  171. package/dist/public/telemetry-acceptance.js +597 -0
  172. package/dist/public/telemetry-remediation.d.ts +33 -0
  173. package/dist/public/telemetry-remediation.d.ts.map +1 -0
  174. package/dist/public/telemetry-remediation.js +257 -0
  175. package/dist/public/verification-receipt.d.ts +75 -0
  176. package/dist/public/verification-receipt.d.ts.map +1 -0
  177. package/dist/public/verification-receipt.js +129 -0
  178. package/dist/schemas/entity.schema.json +401 -4
  179. package/dist/traceability/staged-diagnostics.d.ts +1 -1
  180. package/dist/traceability/staged-diagnostics.d.ts.map +1 -1
  181. package/dist/traceability/staged-diagnostics.js +17 -10
  182. package/dist/traceability/staged-impact-contract.js +1 -1
  183. package/dist/traceability/staged-symbols-manifest.d.ts.map +1 -1
  184. package/dist/traceability/staged-symbols-manifest.js +28 -10
  185. package/dist/traceability/temp-kb.d.ts.map +1 -1
  186. package/dist/traceability/temp-kb.js +49 -3
  187. package/dist/types/entities.d.ts +1 -0
  188. package/dist/types/entities.d.ts.map +1 -1
  189. package/dist/types/relationships.d.ts +1 -1
  190. package/dist/types/relationships.d.ts.map +1 -1
  191. package/dist/utils/rule-registry.d.ts +2 -0
  192. package/dist/utils/rule-registry.d.ts.map +1 -1
  193. package/dist/utils/rule-registry.js +18 -0
  194. package/dist/utils/schema-version.d.ts +1 -1
  195. package/dist/utils/schema-version.js +1 -1
  196. package/package.json +34 -2
  197. package/src/public/diagnostic-usage.ts +206 -0
  198. package/src/public/distribution-parity.ts +580 -0
  199. package/src/public/impact/full-kb-quality.ts +74 -1
  200. package/src/public/impact-diagnostics.ts +16 -0
  201. package/src/public/operations/check-executor.ts +4 -0
  202. package/src/public/operations/discovery-executors.ts +24 -2
  203. package/src/public/operations/index.ts +5 -0
  204. package/src/public/operations/legacy-migration-plan.ts +688 -0
  205. package/src/public/operations/node-ports.ts +109 -0
  206. package/src/public/operations/repair-plan.ts +562 -0
  207. package/src/public/operations/runtime-types.ts +8 -0
  208. package/src/public/operations/specs/check.ts +5 -2
  209. package/src/public/operations/specs/discovery.ts +1 -1
  210. package/src/public/operations/specs/modeling.ts +39 -1
  211. package/src/public/operations/specs/mutation-schemas.ts +77 -1
  212. package/src/public/operations/specs/reporting.ts +91 -6
  213. package/src/public/operations/specs/semantic.ts +42 -3
  214. package/src/public/operations/workspace-snapshot.ts +28 -0
  215. package/src/public/schemas/entity.ts +340 -1
  216. package/src/public/schemas/relationship.ts +1 -0
  217. package/src/public/skills/kibi-traceability/SKILL.md +7 -1
  218. package/src/public/skills/kibi-usage/SKILL.md +44 -12
  219. package/src/public/skills/kibi-usage/resources/fact-lanes.md +12 -2
  220. package/src/public/skills/kibi-usage/resources/logic-ir.md +43 -0
  221. package/src/public/skills/kibi-usage/resources/operation-access.md +19 -19
  222. package/src/public/skills/kibi-usage/resources/ui-requirements.md +96 -0
  223. package/src/public/skills/kibi-usage/resources/workflows.md +40 -8
  224. package/src/public/symbol-granularity.ts +25 -0
  225. package/src/public/telemetry-acceptance.ts +859 -0
  226. package/src/public/telemetry-remediation.ts +462 -0
  227. package/src/public/verification-receipt.ts +170 -0
  228. package/src/schemas/entity.schema.json +401 -4
  229. package/src/schemas/relationship.schema.json +1 -0
@@ -54,11 +54,17 @@ const FACT_STRING_FIELDS = new Set([
54
54
  "claim_text",
55
55
  "predicate_name",
56
56
  "predicate_namespace",
57
+ "rule_hash",
58
+ "rule_schema_id",
59
+ "rule_name",
60
+ "semantic_key",
57
61
  ]);
58
62
  const FACT_NUMBER_FIELDS = new Set([
59
63
  "value_int",
60
64
  "value_number",
61
65
  "predicate_arity",
66
+ "claim_span_start",
67
+ "claim_span_end",
62
68
  ]);
63
69
  const FACT_BOOLEAN_FIELDS = new Set(["value_bool", "closed_world"]);
64
70
  const FACT_STRING_ARRAY_FIELDS = new Set([
@@ -94,6 +100,10 @@ function serializeTypedFactFields(entity) {
94
100
  fields.push(`${field}=${toPrologString(String(value))}`);
95
101
  }
96
102
  }
103
+ const ruleIr = getEntityField(entity, "rule_ir");
104
+ if (ruleIr !== undefined && ruleIr !== null) {
105
+ fields.push(`rule_ir=${toPrologString(JSON.stringify(ruleIr))}`);
106
+ }
97
107
  for (const field of FACT_ATOM_FIELDS) {
98
108
  const value = getEntityField(entity, field);
99
109
  if (value !== undefined && value !== null) {
@@ -123,7 +133,7 @@ function serializeTypedFactFields(entity) {
123
133
  }
124
134
  return fields;
125
135
  }
126
- function buildEntityAssertionGoal(entity) {
136
+ function buildEntityAssertionGoal(entity, sourceFile) {
127
137
  const props = [
128
138
  `id=${toPrologAtom(entity.id)}`,
129
139
  `title=${toPrologString(entity.title)}`,
@@ -143,9 +153,45 @@ function buildEntityAssertionGoal(entity) {
143
153
  props.push(`severity=${toPrologAtom(entity.severity)}`);
144
154
  if (entity.text_ref)
145
155
  props.push(`text_ref=${toPrologString(entity.text_ref)}`);
156
+ if (entity.type === "req" && entity.semantic_text) {
157
+ props.push(`semantic_text=${toPrologString(entity.semantic_text)}`);
158
+ }
146
159
  if (entity.type === "req" && entity.logic_claims) {
147
160
  props.push(`logic_claims=[${entity.logic_claims.map(toPrologAtom).join(",")}]`);
148
161
  }
162
+ if (entity.type === "req" && entity.semantic_clauses) {
163
+ props.push(`semantic_clauses=[${entity.semantic_clauses.map(toPrologString).join(",")}]`);
164
+ }
165
+ if (entity.type === "req" && entity.semantic_inventory_version) {
166
+ props.push(`semantic_inventory_version=${toPrologString(entity.semantic_inventory_version)}`);
167
+ }
168
+ if (entity.type === "req" && entity.semantic_source_field) {
169
+ props.push(`semantic_source_field=${toPrologString(entity.semantic_source_field)}`);
170
+ }
171
+ if (entity.type === "req" && entity.semantic_source_hash) {
172
+ props.push(`semantic_source_hash=${toPrologString(entity.semantic_source_hash)}`);
173
+ }
174
+ if (entity.type === "req" && entity.semantic_inventory) {
175
+ props.push(`semantic_inventory=${toPrologString(JSON.stringify(entity.semantic_inventory))}`);
176
+ }
177
+ if (sourceFile)
178
+ props.push(`sourceFile=${toPrologString(sourceFile)}`);
179
+ if (entity.type === "symbol") {
180
+ if (entity.symbol_role)
181
+ props.push(`symbol_role=${toPrologAtom(entity.symbol_role)}`);
182
+ if (entity.granularity_reason)
183
+ props.push(`granularity_reason=${toPrologAtom(entity.granularity_reason)}`);
184
+ for (const key of [
185
+ "sourceLine",
186
+ "sourceColumn",
187
+ "sourceEndLine",
188
+ "sourceEndColumn",
189
+ ]) {
190
+ const value = entity[key];
191
+ if (typeof value === "number")
192
+ props.push(`${key}=${value}`);
193
+ }
194
+ }
149
195
  if (entity.type === "fact") {
150
196
  props.push(...serializeTypedFactFields(entity));
151
197
  }
@@ -202,11 +248,11 @@ async function consultOverlay(ctx) {
202
248
  export { consultOverlay };
203
249
  // implements REQ-014
204
250
  export async function projectStagedEntities(prolog, results) {
205
- for (const { entity } of results) {
251
+ for (const { entity, sourceFile } of results) {
206
252
  // Retract stale relationships before re-asserting entity
207
253
  // (kb_assert_entity_no_audit preserves relationships on upsert)
208
254
  await prolog.query(`kb_retract_entity_relationships('${entity.id}')`);
209
- const assertEntityResult = await prolog.query(buildEntityAssertionGoal(entity));
255
+ const assertEntityResult = await prolog.query(buildEntityAssertionGoal(entity, sourceFile));
210
256
  if (!assertEntityResult.success) {
211
257
  throw new Error(`Failed to assert staged entity ${entity.id}: ${assertEntityResult.error || "unknown error"}`);
212
258
  }
@@ -47,6 +47,7 @@ export interface TestVerificationFields {
47
47
  }
48
48
  export type Requirement = BaseEntity & {
49
49
  type: "req";
50
+ semantic_text?: string;
50
51
  logic_claims?: string[];
51
52
  };
52
53
  export type Scenario = BaseEntity & {
@@ -1 +1 @@
1
- {"version":3,"file":"entities.d.ts","sourceRoot":"","sources":["../../src/types/entities.ts"],"names":[],"mappings":"AAkBA,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,KAAK,CA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AAA;CAAE,CAAC;AACnD,MAAM,MAAM,MAAM,GAAG,UAAU,GAAG;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,CAAC;AACrD,MAAM,MAAM,IAAI,GAAG,UAAU,GAAG,UAAU,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAE9D,MAAM,MAAM,MAAM,GACd,WAAW,GACX,QAAQ,GACR,UAAU,GACV,GAAG,GACH,IAAI,GACJ,KAAK,GACL,MAAM,GACN,IAAI,CAAC"}
@@ -1,5 +1,5 @@
1
1
  export interface BaseRelationship {
2
- type: "depends_on" | "specified_by" | "verified_by" | "validates" | "implements" | "covered_by" | "executable_for" | "constrained_by" | "constrains" | "requires_property" | "requires_predicate" | "guards" | "publishes" | "consumes" | "supersedes" | "relates_to";
2
+ type: "depends_on" | "specified_by" | "verified_by" | "validates" | "implements" | "covered_by" | "executable_for" | "constrained_by" | "constrains" | "requires_property" | "requires_predicate" | "requires_rule" | "guards" | "publishes" | "consumes" | "supersedes" | "relates_to";
3
3
  from: string;
4
4
  to: string;
5
5
  created_at?: string;
@@ -1 +1 @@
1
- {"version":3,"file":"relationships.d.ts","sourceRoot":"","sources":["../../src/types/relationships.ts"],"names":[],"mappings":"AAkBA,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EACA,YAAY,GACZ,cAAc,GACd,aAAa,GACb,WAAW,GACX,YAAY,GACZ,YAAY,GACZ,gBAAgB,GAChB,gBAAgB,GAChB,YAAY,GACZ,mBAAmB,GACnB,oBAAoB,GACpB,QAAQ,GACR,WAAW,GACX,UAAU,GACV,YAAY,GACZ,YAAY,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,eAAe,gBAAgB,CAAC"}
1
+ {"version":3,"file":"relationships.d.ts","sourceRoot":"","sources":["../../src/types/relationships.ts"],"names":[],"mappings":"AAkBA,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EACA,YAAY,GACZ,cAAc,GACd,aAAa,GACb,WAAW,GACX,YAAY,GACZ,YAAY,GACZ,gBAAgB,GAChB,gBAAgB,GAChB,YAAY,GACZ,mBAAmB,GACnB,oBAAoB,GACpB,eAAe,GACf,QAAQ,GACR,WAAW,GACX,UAAU,GACV,YAAY,GACZ,YAAY,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,eAAe,gBAAgB,CAAC"}
@@ -18,6 +18,8 @@ export interface Violation {
18
18
  description: string;
19
19
  suggestion?: string;
20
20
  source?: string;
21
+ /** Exact source-bound proof evidence, when the check can produce it. */
22
+ evidence?: Readonly<Record<string, unknown>>;
21
23
  }
22
24
  export interface ChecksConfig {
23
25
  rules: Record<string, boolean>;
@@ -1 +1 @@
1
- {"version":3,"file":"rule-registry.d.ts","sourceRoot":"","sources":["../../src/utils/rule-registry.ts"],"names":[],"mappings":"AAkBA;;;GAGG;AAEH,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,OAAO,CAAC;IACxB,QAAQ,EAAE,UAAU,GAAG,WAAW,GAAG,WAAW,GAAG,cAAc,CAAC;CACnE;AAED,MAAM,WAAW,yBAAyB;IACxC,UAAU,EAAE,OAAO,CAAC;CACrB;AAED,mCAAmC;AACnC,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAA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;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,wEAAwE;IACxE,QAAQ,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CAC9C;AAED,MAAM,WAAW,YAAY;IAC3B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/B,kBAAkB,EAAE,yBAAyB,CAAC;CAC/C;AAED;;;GAGG;AACH,eAAO,MAAM,KAAK,EAAE,SAAS,cAAc,EAsHjC,CAAC;AAEX;;GAEG;AACH,eAAO,MAAM,UAAU,aAAoC,CAAC;AAE5D;;GAEG;AACH,eAAO,MAAM,qBAAqB,EAAE,YAKnC,CAAC;AAEF;;;;;GAKG;AAEH,wBAAgB,iBAAiB,CAC/B,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACrC,QAAQ,CAAC,EAAE,MAAM,GAChB,GAAG,CAAC,MAAM,CAAC,CAsBb;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAK5D;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,OAAO,CAAC,EAAE,OAAO,CAAC,YAAY,CAAC,GAC9B,YAAY,CAWd"}
@@ -99,6 +99,24 @@ export const RULES = [
99
99
  defaultEnabled: true,
100
100
  category: "integrity",
101
101
  },
102
+ {
103
+ name: "rule-safety",
104
+ description: "Stored Logic IR rules use only the typed, bounded, stratified vocabulary",
105
+ defaultEnabled: true,
106
+ category: "integrity",
107
+ },
108
+ {
109
+ name: "rule-verifiability",
110
+ description: "Requirement rule links resolve to safe rules and registered rule schemas",
111
+ defaultEnabled: true,
112
+ category: "integrity",
113
+ },
114
+ {
115
+ name: "semantic-completeness",
116
+ description: "Every inventoried assertive proposition is modeled or explicitly classified",
117
+ defaultEnabled: false,
118
+ category: "integrity",
119
+ },
102
120
  {
103
121
  name: "query-plan-safety",
104
122
  description: "Detect Prolog validation clauses that place negation before generator calls",
@@ -1,5 +1,5 @@
1
1
  import type { KbConfig } from "./config.js";
2
- export declare const LATEST_KB_SCHEMA_VERSION = 3;
2
+ export declare const LATEST_KB_SCHEMA_VERSION = 4;
3
3
  export interface SchemaVersionStatus {
4
4
  status: "missing" | "invalid" | "older" | "current" | "newer";
5
5
  currentVersion: number | null;
@@ -1,5 +1,5 @@
1
1
  // implements REQ-003
2
- export const LATEST_KB_SCHEMA_VERSION = 3;
2
+ export const LATEST_KB_SCHEMA_VERSION = 4;
3
3
  // implements REQ-003
4
4
  export function normalizeSchemaVersion(schemaVersion) {
5
5
  if (schemaVersion === undefined || schemaVersion === null) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kibi-cli",
3
- "version": "0.16.1",
3
+ "version": "0.18.1",
4
4
  "type": "module",
5
5
  "description": "Kibi CLI for knowledge base management",
6
6
  "engines": {
@@ -34,6 +34,14 @@
34
34
  "types": "./dist/public/operations/runtime-types.d.ts",
35
35
  "default": "./dist/public/operations/runtime-types.js"
36
36
  },
37
+ "./operations/node-ports": {
38
+ "types": "./dist/public/operations/node-ports.d.ts",
39
+ "default": "./dist/public/operations/node-ports.js"
40
+ },
41
+ "./verification-receipt": {
42
+ "types": "./dist/public/verification-receipt.d.ts",
43
+ "default": "./dist/public/verification-receipt.js"
44
+ },
37
45
  "./operations/semantic-advisor/analyze-prose": {
38
46
  "types": "./dist/operations/semantic-advisor/analyze-prose.d.ts",
39
47
  "default": "./dist/operations/semantic-advisor/analyze-prose.js"
@@ -90,6 +98,26 @@
90
98
  "types": "./dist/public/impact-diagnostics.d.ts",
91
99
  "default": "./dist/public/impact-diagnostics.js"
92
100
  },
101
+ "./telemetry-acceptance": {
102
+ "types": "./dist/public/telemetry-acceptance.d.ts",
103
+ "default": "./dist/public/telemetry-acceptance.js"
104
+ },
105
+ "./telemetry-remediation": {
106
+ "types": "./dist/public/telemetry-remediation.d.ts",
107
+ "default": "./dist/public/telemetry-remediation.js"
108
+ },
109
+ "./diagnostic-usage": {
110
+ "types": "./dist/public/diagnostic-usage.d.ts",
111
+ "default": "./dist/public/diagnostic-usage.js"
112
+ },
113
+ "./distribution-parity": {
114
+ "types": "./dist/public/distribution-parity.d.ts",
115
+ "default": "./dist/public/distribution-parity.js"
116
+ },
117
+ "./legacy-migration-plan": {
118
+ "types": "./dist/public/operations/legacy-migration-plan.d.ts",
119
+ "default": "./dist/public/operations/legacy-migration-plan.js"
120
+ },
93
121
  "./public/symbol-granularity": {
94
122
  "types": "./dist/public/symbol-granularity.d.ts",
95
123
  "default": "./dist/public/symbol-granularity.js"
@@ -107,6 +135,10 @@
107
135
  "default": "./dist/public/skills.js"
108
136
  },
109
137
  "./operations": "./dist/public/operations/index.js",
138
+ "./logic/ir": {
139
+ "types": "./dist/logic/ir.d.ts",
140
+ "default": "./dist/logic/ir.js"
141
+ },
110
142
  "./operations/mutation/relationships": {
111
143
  "types": "./dist/operations/mutation/relationships.d.ts",
112
144
  "default": "./dist/operations/mutation/relationships.js"
@@ -157,7 +189,7 @@
157
189
  "fast-glob": "^3.2.12",
158
190
  "gray-matter": "^4.0.3",
159
191
  "js-yaml": "^4.1.0",
160
- "kibi-core": "^0.7.1",
192
+ "kibi-core": "^0.9.1",
161
193
  "ts-morph": "^23.0.0",
162
194
  "ignore": "^5.3.0"
163
195
  },
@@ -0,0 +1,206 @@
1
+ import { createHash, randomUUID } from "node:crypto";
2
+ import { appendFileSync, mkdirSync } from "node:fs";
3
+ import path from "node:path";
4
+
5
+ export interface DiagnosticUsageInput {
6
+ readonly workspaceRoot: string;
7
+ readonly tool: string;
8
+ readonly businessArgs: Readonly<Record<string, unknown>>;
9
+ readonly telemetry: Readonly<Record<string, unknown>> | null;
10
+ readonly startedAt: Date;
11
+ readonly finishedAt?: Date;
12
+ readonly status: "success" | "error";
13
+ readonly result?: unknown;
14
+ readonly error?: string;
15
+ readonly requestId?: string;
16
+ readonly logPath?: string;
17
+ }
18
+
19
+ function isRecord(value: unknown): value is Record<string, unknown> {
20
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
21
+ }
22
+
23
+ function resultRecord(result: unknown): Record<string, unknown> | undefined {
24
+ if (!isRecord(result)) return undefined;
25
+ return isRecord(result.structuredContent) ? result.structuredContent : result;
26
+ }
27
+
28
+ function stringArray(value: unknown): string[] {
29
+ return Array.isArray(value)
30
+ ? value.filter((item): item is string => typeof item === "string")
31
+ : [];
32
+ }
33
+
34
+ function canonicalValue(value: unknown): unknown {
35
+ if (Array.isArray(value)) return value.map(canonicalValue);
36
+ if (!isRecord(value)) return value;
37
+ return Object.fromEntries(
38
+ Object.entries(value)
39
+ .filter(([key]) => key !== "_diagnostic_telemetry")
40
+ .sort(([left], [right]) => left.localeCompare(right))
41
+ .map(([key, child]) => [key, canonicalValue(child)]),
42
+ );
43
+ }
44
+
45
+ export function diagnosticMutationFingerprint(
46
+ args: Readonly<Record<string, unknown>>,
47
+ ): string {
48
+ return createHash("sha256")
49
+ .update(JSON.stringify(canonicalValue(args)))
50
+ .digest("hex");
51
+ }
52
+
53
+ function appendMutationFields(
54
+ fields: Record<string, unknown>,
55
+ args: Readonly<Record<string, unknown>>,
56
+ ): void {
57
+ fields.mutation_fingerprint = diagnosticMutationFingerprint(args);
58
+ const type = typeof args.type === "string" ? args.type : "unknown";
59
+ const id = typeof args.id === "string" ? args.id : "unknown";
60
+ fields.mutation_target = `${type}:${id}`;
61
+ }
62
+
63
+ function appendSemanticFields(
64
+ fields: Record<string, unknown>,
65
+ receipt: unknown,
66
+ ): void {
67
+ if (!isRecord(receipt)) return;
68
+ const inventory = isRecord(receipt.inventory_contract)
69
+ ? receipt.inventory_contract
70
+ : undefined;
71
+ if (typeof inventory?.source_hash === "string") {
72
+ fields.semantic_source_hash = inventory.source_hash;
73
+ }
74
+ if (typeof receipt.logic_readiness === "string") {
75
+ fields.semantic_logic_readiness = receipt.logic_readiness;
76
+ }
77
+ if (typeof receipt.candidate_lane === "string") {
78
+ fields.semantic_candidate_lane = receipt.candidate_lane;
79
+ }
80
+ }
81
+
82
+ const RECEIPT_GAP_CODES = new Set([
83
+ "missing_verification_receipt",
84
+ "stale_verification_receipt",
85
+ "failed_verification_receipt",
86
+ "invalid_verification_receipt",
87
+ "verification_snapshot_unavailable",
88
+ ]);
89
+
90
+ function appendCoverageFields(
91
+ fields: Record<string, unknown>,
92
+ args: Readonly<Record<string, unknown>>,
93
+ result: Record<string, unknown>,
94
+ ): void {
95
+ const rows = Array.isArray(result.rows) ? result.rows.filter(isRecord) : [];
96
+ const summary = isRecord(result.summary) ? result.summary : {};
97
+ const gapCodes = rows.flatMap((row) => stringArray(row.proofGaps));
98
+ const repairPlan = isRecord(result.repairPlan)
99
+ ? result.repairPlan
100
+ : undefined;
101
+ const scope = isRecord(repairPlan?.scope) ? repairPlan.scope : undefined;
102
+ fields.coverage_by = typeof args.by === "string" ? args.by : "req";
103
+ fields.coverage_requirement_count = Number(summary.total ?? rows.length);
104
+ fields.coverage_proven_count = Number(summary.proofProven ?? 0);
105
+ fields.coverage_proof_missing_count = Number(summary.proofMissing ?? 0);
106
+ fields.coverage_proof_gap_count = gapCodes.length;
107
+ fields.coverage_gap_codes = [...new Set(gapCodes)].sort();
108
+ fields.coverage_receipt_gap_count = gapCodes.filter((code) =>
109
+ RECEIPT_GAP_CODES.has(code),
110
+ ).length;
111
+ if (typeof scope?.complete === "boolean") {
112
+ fields.coverage_scope_complete = scope.complete;
113
+ }
114
+ }
115
+
116
+ export function deriveDiagnosticUsageFields(
117
+ tool: string,
118
+ args: Readonly<Record<string, unknown>>,
119
+ telemetry: Readonly<Record<string, unknown>> | null,
120
+ result: unknown,
121
+ ): Record<string, unknown> {
122
+ const fields: Record<string, unknown> = {
123
+ telemetry_status: telemetry ? "provided" : "missing",
124
+ };
125
+ if (telemetry) {
126
+ fields.telemetry_is_autonomous = telemetry.is_autonomous ?? null;
127
+ fields.telemetry_confidence_score = telemetry.confidence_score ?? null;
128
+ fields.telemetry_attempt_number = telemetry.attempt_number ?? null;
129
+ if (typeof telemetry.session_id === "string") {
130
+ fields.session_id = telemetry.session_id;
131
+ }
132
+ if (typeof telemetry.actor_id === "string") {
133
+ fields.actor_id = telemetry.actor_id;
134
+ }
135
+ }
136
+
137
+ const structured = resultRecord(result);
138
+ if (tool === "kb_query" || tool === "kb_search") {
139
+ const resultCount = Number(structured?.count ?? 0);
140
+ fields.result_count = resultCount;
141
+ fields.zero_results = resultCount === 0;
142
+ fields.result_summary =
143
+ resultCount === 0 ? "0 results" : `${resultCount} results`;
144
+ }
145
+ if (tool === "kb_check") {
146
+ const count = Number(structured?.count ?? 0);
147
+ fields.violation_count = count;
148
+ fields.requested_rules = Array.isArray(args.rules) ? args.rules : [];
149
+ fields.result_summary =
150
+ count === 0 ? "0 violations" : `${count} violations`;
151
+ }
152
+ if (tool === "kb_coverage" && structured) {
153
+ appendCoverageFields(fields, args, structured);
154
+ fields.result_summary = `${String(
155
+ fields.coverage_proven_count,
156
+ )} proven; ${String(fields.coverage_proof_gap_count)} proof gaps`;
157
+ }
158
+ if (tool === "kb_semantic_advisor" && structured) {
159
+ appendSemanticFields(fields, structured.receipt);
160
+ }
161
+ if (tool === "kb_validate_upsert") {
162
+ appendMutationFields(fields, args);
163
+ fields.validation_valid = structured?.valid === true;
164
+ appendSemanticFields(fields, structured?.semanticAdvisor);
165
+ }
166
+ if (tool === "kb_upsert") {
167
+ appendMutationFields(fields, args);
168
+ appendSemanticFields(fields, structured?.semanticAdvisor);
169
+ }
170
+ if (!fields.result_summary) fields.result_summary = `${tool} completed`;
171
+ return fields;
172
+ }
173
+
174
+ export function appendCliDiagnosticUsage(input: DiagnosticUsageInput): void {
175
+ const finishedAt = input.finishedAt ?? new Date();
176
+ const requestId = input.requestId ?? `cli-${randomUUID()}`;
177
+ const logPath =
178
+ input.logPath ??
179
+ process.env.KIBI_CLI_DIAGNOSTIC_USAGE_LOG_PATH ??
180
+ path.join(input.workspaceRoot, ".kb", "usage.log");
181
+ mkdirSync(path.dirname(logPath), { recursive: true });
182
+ appendFileSync(
183
+ logPath,
184
+ `${JSON.stringify({
185
+ timestamp: finishedAt.toISOString(),
186
+ request_id: requestId,
187
+ tool: input.tool,
188
+ interface: "cli_json",
189
+ telemetry: input.telemetry,
190
+ business_args: input.businessArgs,
191
+ status: input.status,
192
+ started_at: input.startedAt.toISOString(),
193
+ finished_at: finishedAt.toISOString(),
194
+ duration_ms: finishedAt.getTime() - input.startedAt.getTime(),
195
+ active_branch: process.env.KIBI_BRANCH ?? null,
196
+ ...(input.error ? { error_message: input.error } : {}),
197
+ ...deriveDiagnosticUsageFields(
198
+ input.tool,
199
+ input.businessArgs,
200
+ input.telemetry,
201
+ input.result,
202
+ ),
203
+ })}\n`,
204
+ "utf8",
205
+ );
206
+ }