@jmtrin/kevin-core 1.3.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 (145) hide show
  1. package/dist/Archiver.d.ts +42 -0
  2. package/dist/Archiver.js +83 -0
  3. package/dist/ArtifactWriter.d.ts +93 -0
  4. package/dist/ArtifactWriter.js +291 -0
  5. package/dist/CausalChain.d.ts +22 -0
  6. package/dist/CausalChain.js +197 -0
  7. package/dist/ChatBridge.d.ts +41 -0
  8. package/dist/ChatBridge.js +103 -0
  9. package/dist/ConflictDetector.d.ts +35 -0
  10. package/dist/ConflictDetector.js +260 -0
  11. package/dist/ContextInjector.d.ts +222 -0
  12. package/dist/ContextInjector.js +533 -0
  13. package/dist/ConventionMiner.d.ts +35 -0
  14. package/dist/ConventionMiner.js +242 -0
  15. package/dist/Curator.d.ts +118 -0
  16. package/dist/Curator.js +326 -0
  17. package/dist/DashboardHtml.d.ts +5 -0
  18. package/dist/DashboardHtml.js +180 -0
  19. package/dist/Feedback.d.ts +67 -0
  20. package/dist/Feedback.js +120 -0
  21. package/dist/HookLiveness.d.ts +88 -0
  22. package/dist/HookLiveness.js +322 -0
  23. package/dist/InjectionLedger.d.ts +98 -0
  24. package/dist/InjectionLedger.js +295 -0
  25. package/dist/LessonFixer.d.ts +44 -0
  26. package/dist/LessonFixer.js +45 -0
  27. package/dist/Materializer.d.ts +84 -0
  28. package/dist/Materializer.js +181 -0
  29. package/dist/MemoryService.d.ts +360 -0
  30. package/dist/MemoryService.js +1242 -0
  31. package/dist/Migrate.d.ts +24 -0
  32. package/dist/Migrate.js +285 -0
  33. package/dist/PatternMiner.d.ts +49 -0
  34. package/dist/PatternMiner.js +132 -0
  35. package/dist/QualityGate.d.ts +124 -0
  36. package/dist/QualityGate.js +119 -0
  37. package/dist/Reflector.d.ts +91 -0
  38. package/dist/Reflector.js +307 -0
  39. package/dist/RepoIdentity.d.ts +124 -0
  40. package/dist/RepoIdentity.js +300 -0
  41. package/dist/RepoTruth.d.ts +80 -0
  42. package/dist/RepoTruth.js +599 -0
  43. package/dist/Retrospective.d.ts +18 -0
  44. package/dist/Retrospective.js +216 -0
  45. package/dist/SharedLayer.d.ts +159 -0
  46. package/dist/SharedLayer.js +462 -0
  47. package/dist/Store.d.ts +29 -0
  48. package/dist/Store.js +50 -0
  49. package/dist/ToolCallObserver.d.ts +32 -0
  50. package/dist/ToolCallObserver.js +201 -0
  51. package/dist/TuiActions.d.ts +43 -0
  52. package/dist/TuiActions.js +181 -0
  53. package/dist/TuiSnapshots.d.ts +24 -0
  54. package/dist/TuiSnapshots.js +159 -0
  55. package/dist/capabilities.d.ts +6 -0
  56. package/dist/capabilities.js +1 -0
  57. package/dist/columns.d.ts +11 -0
  58. package/dist/columns.js +54 -0
  59. package/dist/confidence.d.ts +8 -0
  60. package/dist/confidence.js +34 -0
  61. package/dist/contract.d.ts +68 -0
  62. package/dist/contract.js +430 -0
  63. package/dist/diff.d.ts +8 -0
  64. package/dist/diff.js +182 -0
  65. package/dist/env.d.ts +5 -0
  66. package/dist/env.js +12 -0
  67. package/dist/escape.d.ts +39 -0
  68. package/dist/escape.js +60 -0
  69. package/dist/fingerprint.d.ts +27 -0
  70. package/dist/fingerprint.js +73 -0
  71. package/dist/host.d.ts +18 -0
  72. package/dist/host.js +7 -0
  73. package/dist/idle-pipeline.d.ts +9 -0
  74. package/dist/idle-pipeline.js +44 -0
  75. package/dist/index.d.ts +72 -0
  76. package/dist/index.js +109 -0
  77. package/dist/inferability.d.ts +32 -0
  78. package/dist/inferability.js +88 -0
  79. package/dist/kevin_approve.d.ts +34 -0
  80. package/dist/kevin_approve.js +52 -0
  81. package/dist/kevin_audit.d.ts +230 -0
  82. package/dist/kevin_audit.js +631 -0
  83. package/dist/kevin_bench.d.ts +11 -0
  84. package/dist/kevin_bench.js +83 -0
  85. package/dist/kevin_conflicts.d.ts +9 -0
  86. package/dist/kevin_conflicts.js +50 -0
  87. package/dist/kevin_contract.d.ts +8 -0
  88. package/dist/kevin_contract.js +59 -0
  89. package/dist/kevin_doctor.d.ts +63 -0
  90. package/dist/kevin_doctor.js +197 -0
  91. package/dist/kevin_facts.d.ts +42 -0
  92. package/dist/kevin_facts.js +36 -0
  93. package/dist/kevin_forget.d.ts +33 -0
  94. package/dist/kevin_forget.js +260 -0
  95. package/dist/kevin_native.d.ts +29 -0
  96. package/dist/kevin_native.js +79 -0
  97. package/dist/kevin_propose.d.ts +23 -0
  98. package/dist/kevin_propose.js +14 -0
  99. package/dist/kevin_publish.d.ts +38 -0
  100. package/dist/kevin_publish.js +18 -0
  101. package/dist/kevin_why.d.ts +23 -0
  102. package/dist/kevin_why.js +111 -0
  103. package/dist/memory-format.d.ts +20 -0
  104. package/dist/memory-format.js +59 -0
  105. package/dist/metrics.d.ts +97 -0
  106. package/dist/metrics.js +288 -0
  107. package/dist/migrations/001_initial.sql +92 -0
  108. package/dist/migrations/002_indexes.sql +14 -0
  109. package/dist/migrations/003_v02_signal.sql +58 -0
  110. package/dist/migrations/004_v03_knowledge.sql +138 -0
  111. package/dist/migrations/005_v04_signal.sql +57 -0
  112. package/dist/migrations/006_v05_glassbox.sql +118 -0
  113. package/dist/migrations/007_v06_pull.sql +145 -0
  114. package/dist/migrations/008_v07_truth.sql +124 -0
  115. package/dist/migrations/009_v08_team.sql +100 -0
  116. package/dist/migrations/010_v09_native.sql +78 -0
  117. package/dist/migrations/011_v10_proven.sql +69 -0
  118. package/dist/migrations/012_v11_drift.sql +24 -0
  119. package/dist/native.d.ts +23 -0
  120. package/dist/native.js +1 -0
  121. package/dist/okf-export.d.ts +3 -0
  122. package/dist/okf-export.js +134 -0
  123. package/dist/okf-import.d.ts +76 -0
  124. package/dist/okf-import.js +271 -0
  125. package/dist/okf.d.ts +107 -0
  126. package/dist/okf.js +303 -0
  127. package/dist/perf.d.ts +35 -0
  128. package/dist/perf.js +174 -0
  129. package/dist/query-tokenizer.d.ts +13 -0
  130. package/dist/query-tokenizer.js +133 -0
  131. package/dist/redact.d.ts +2 -0
  132. package/dist/redact.js +18 -0
  133. package/dist/replay-types.d.ts +69 -0
  134. package/dist/replay-types.js +156 -0
  135. package/dist/replay.d.ts +44 -0
  136. package/dist/replay.js +215 -0
  137. package/dist/sqlite-adapter.d.ts +12 -0
  138. package/dist/sqlite-adapter.js +78 -0
  139. package/dist/time-ms.d.ts +1 -0
  140. package/dist/time-ms.js +16 -0
  141. package/dist/tui-types.d.ts +59 -0
  142. package/dist/tui-types.js +4 -0
  143. package/dist/uuid.d.ts +1 -0
  144. package/dist/uuid.js +50 -0
  145. package/package.json +28 -0
@@ -0,0 +1,83 @@
1
+ /**
2
+ * K10-019 — kevin_bench: reports what `npm run bench` recorded.
3
+ *
4
+ * It NEVER runs the benchmark — running it from inside a live session
5
+ * would measure the session and pollute the user's database. The module
6
+ * imports nothing from scripts/bench.ts; the corpus digest of the on-disk
7
+ * corpus is recomputed here with the same fnv1a64 discipline so a result
8
+ * computed from a different corpus is visibly a different measurement.
9
+ */
10
+ import { readFileSync, statSync } from "node:fs";
11
+ import { join } from "node:path";
12
+ import { resolveEnv } from "./env.js";
13
+ import { fnv1a64 } from "./fingerprint.js";
14
+ function onDiskCorpusDigest(cwd) {
15
+ try {
16
+ const dir = join(cwd, "bench", "corpus");
17
+ statSync(dir);
18
+ const mem = readFileSync(join(dir, "memories.jsonl"), "utf8");
19
+ const q = readFileSync(join(dir, "queries.jsonl"), "utf8");
20
+ return {
21
+ digest: fnv1a64(`${mem}\n${q}`),
22
+ queries: q.split("\n").filter((l) => l.trim().length > 0).length,
23
+ };
24
+ }
25
+ catch {
26
+ return null;
27
+ }
28
+ }
29
+ function noRuns() {
30
+ return {
31
+ has_runs: false,
32
+ hint: "npm run bench",
33
+ };
34
+ }
35
+ export function buildKevinBench(deps, args, env) {
36
+ if (args.action === "status") {
37
+ const count = deps.store
38
+ .prepare("SELECT COUNT(*) AS c FROM bench_runs")
39
+ .get();
40
+ if (!count || count.c === 0)
41
+ return noRuns();
42
+ const latest = deps.store
43
+ .prepare("SELECT corpus_digest, contract_digest, package_version, runtime, ran_at FROM bench_runs ORDER BY id DESC LIMIT 1")
44
+ .get();
45
+ if (!latest)
46
+ return noRuns();
47
+ const disk = onDiskCorpusDigest(deps.cwd ?? resolveEnv(env).projectRoot);
48
+ return {
49
+ has_runs: true,
50
+ total_runs: count.c,
51
+ corpus_digest: latest.corpus_digest,
52
+ contract_digest: latest.contract_digest,
53
+ package_version: latest.package_version,
54
+ runtime: latest.runtime,
55
+ last_ran_at: latest.ran_at,
56
+ corpus_on_disk: disk === null ? null : disk.digest,
57
+ matches_disk_corpus: disk !== null && disk.digest === latest.corpus_digest,
58
+ };
59
+ }
60
+ const latest = deps.store
61
+ .prepare("SELECT corpus_digest, contract_digest, package_version, runtime, ran_at FROM bench_runs ORDER BY id DESC LIMIT 1")
62
+ .get();
63
+ if (!latest)
64
+ return noRuns();
65
+ const rows = deps.store
66
+ .prepare("SELECT arm, k, precision_at_k, recall_at_k, mrr FROM bench_runs WHERE corpus_digest = ? AND ran_at = ?")
67
+ .all(latest.corpus_digest, latest.ran_at);
68
+ return {
69
+ has_runs: true,
70
+ corpus_digest: latest.corpus_digest,
71
+ contract_digest: latest.contract_digest,
72
+ package_version: latest.package_version,
73
+ runtime: latest.runtime,
74
+ ran_at: latest.ran_at,
75
+ arms: rows.map((r) => ({
76
+ arm: r.arm,
77
+ k: r.k,
78
+ precision_at_k: r.precision_at_k,
79
+ recall_at_k: r.recall_at_k,
80
+ mrr: r.mrr,
81
+ })),
82
+ };
83
+ }
@@ -0,0 +1,9 @@
1
+ import type { ConflictDetector } from "./ConflictDetector.js";
2
+ import type { Store } from "./Store.js";
3
+ export interface KevinConflictsDeps {
4
+ store: Store;
5
+ detector: ConflictDetector;
6
+ projectId: string;
7
+ }
8
+ export type ConflictAction = "list" | "acknowledge" | "resolve";
9
+ export declare function executeKevinConflicts(deps: KevinConflictsDeps, action: ConflictAction, id?: string, keep?: string, requestedStatus?: "open" | "acknowledged" | "resolved"): Record<string, unknown>;
@@ -0,0 +1,50 @@
1
+ export function executeKevinConflicts(deps, action, id, keep, requestedStatus) {
2
+ if (action === "acknowledge") {
3
+ if (!id)
4
+ return { error: "missing_id" };
5
+ // Mirror resolve()'s honesty: acknowledging a nonexistent or foreign id
6
+ // must not be reported as a success.
7
+ const row = deps.store
8
+ .prepare("SELECT id FROM memory_conflicts WHERE id = ? AND project_id = ?")
9
+ .get(id, deps.projectId);
10
+ if (!row)
11
+ return { error: "not_found", id };
12
+ deps.detector.acknowledge(id);
13
+ return { id, status: "acknowledged" };
14
+ }
15
+ if (action === "resolve") {
16
+ if (!id || !keep)
17
+ return { error: "missing_id_or_keep" };
18
+ const row = deps.store
19
+ .prepare("SELECT memory_a, memory_b FROM memory_conflicts WHERE id = ? AND project_id = ?")
20
+ .get(id, deps.projectId);
21
+ if (!row)
22
+ return { error: "not_found", id };
23
+ if (keep !== row.memory_a && keep !== row.memory_b) {
24
+ return { error: "invalid_keep", id, keep };
25
+ }
26
+ deps.detector.resolve(id, keep);
27
+ return {
28
+ id,
29
+ status: "resolved",
30
+ kept: keep,
31
+ other: keep === row.memory_a ? row.memory_b : row.memory_a,
32
+ };
33
+ }
34
+ const status = requestedStatus ?? "open";
35
+ const rows = deps.store
36
+ .prepare(`SELECT id, kind, memory_a, memory_b, fact_id, detail, status, detected_at
37
+ FROM memory_conflicts WHERE project_id = ? AND status = ?
38
+ ORDER BY detected_at, id`)
39
+ .all(deps.projectId, status);
40
+ const counts = deps.store
41
+ .prepare(`SELECT status, COUNT(*) AS n FROM memory_conflicts
42
+ WHERE project_id = ? GROUP BY status`)
43
+ .all(deps.projectId);
44
+ return {
45
+ open: counts.find((row) => row.status === "open")?.n ?? 0,
46
+ acknowledged: counts.find((row) => row.status === "acknowledged")?.n ?? 0,
47
+ resolved: counts.find((row) => row.status === "resolved")?.n ?? 0,
48
+ conflicts: rows,
49
+ };
50
+ }
@@ -0,0 +1,8 @@
1
+ export interface KevinContractDeps {
2
+ packageVersion: string;
3
+ }
4
+ export interface KevinContractArgs {
5
+ clause?: string;
6
+ format?: "summary" | "full";
7
+ }
8
+ export declare function buildKevinContract(deps: KevinContractDeps, args: KevinContractArgs): Record<string, unknown>;
@@ -0,0 +1,59 @@
1
+ /**
2
+ * K10-018 — kevin_contract: the frozen surface, inspectable at runtime.
3
+ *
4
+ * Read-only. Returns the live contract with its digest and per-clause
5
+ * stability/since/deprecation; with `clause`, one clause's full value.
6
+ * No filesystem paths, no project ids, no LLM, no network.
7
+ */
8
+ import { contractDigest, describeContract, } from "./contract.js";
9
+ export function buildKevinContract(deps, args) {
10
+ const contract = describeContract();
11
+ const digest = contractDigest(contract);
12
+ const format = args.format ?? "summary";
13
+ if (args.clause !== undefined) {
14
+ const clause = contract.clauses.find((c) => c.id === args.clause);
15
+ if (!clause) {
16
+ return {
17
+ error: "unknown_clause",
18
+ clause: args.clause,
19
+ known_clauses: contract.clauses.map((c) => c.id),
20
+ };
21
+ }
22
+ return {
23
+ contract_version: contract.contractVersion,
24
+ digest,
25
+ package_version: deps.packageVersion,
26
+ clause: {
27
+ id: clause.id,
28
+ title: clause.title,
29
+ stability: clause.stability,
30
+ since: clause.since,
31
+ ...(clause.deprecated ? { deprecated: clause.deprecated } : {}),
32
+ ...(clause.replacement ? { replacement: clause.replacement } : {}),
33
+ value: clause.value,
34
+ },
35
+ };
36
+ }
37
+ if (format === "full") {
38
+ return {
39
+ contract_version: contract.contractVersion,
40
+ digest,
41
+ package_version: deps.packageVersion,
42
+ clauses: contract.clauses,
43
+ deprecated: contract.clauses.filter((c) => c.deprecated).map((c) => c.id),
44
+ };
45
+ }
46
+ return {
47
+ contract_version: contract.contractVersion,
48
+ digest,
49
+ package_version: deps.packageVersion,
50
+ clauses: contract.clauses.map((c) => ({
51
+ id: c.id,
52
+ title: c.title,
53
+ stability: c.stability,
54
+ since: c.since,
55
+ ...(c.deprecated ? { deprecated: c.deprecated } : {}),
56
+ })),
57
+ deprecated: contract.clauses.filter((c) => c.deprecated).map((c) => c.id),
58
+ };
59
+ }
@@ -0,0 +1,63 @@
1
+ import { type KevinEnv } from "./env.js";
2
+ import type { Store } from "./Store.js";
3
+ import type { HostSurface } from "./host.js";
4
+ import type { SettingsReader } from "./native.js";
5
+ export interface DoctorHook {
6
+ readonly hook: string;
7
+ readonly experimental: boolean;
8
+ readonly state: "live" | "dead" | "unknown";
9
+ readonly fire_count: number;
10
+ readonly expected_count: number;
11
+ readonly since?: string;
12
+ }
13
+ export interface DoctorReport {
14
+ readonly host: {
15
+ readonly plugin_version: string | null;
16
+ readonly flavour: string;
17
+ readonly shell_available: boolean;
18
+ readonly v2: {
19
+ readonly skill: boolean;
20
+ readonly reference: boolean;
21
+ };
22
+ };
23
+ readonly hooks: DoctorHook[];
24
+ readonly dependencies: {
25
+ readonly declared: string[];
26
+ readonly zod_copies: number | null;
27
+ readonly note?: string;
28
+ };
29
+ readonly native: {
30
+ readonly enabled: boolean;
31
+ readonly registered: {
32
+ readonly skill: boolean;
33
+ readonly reference: boolean;
34
+ };
35
+ readonly verified: {
36
+ readonly skill: boolean;
37
+ readonly reference: boolean;
38
+ };
39
+ };
40
+ /** v1.0.0 (K10-021) — scopes whose most recent sample exceeded their
41
+ * p95 budget. Omitted entirely on pre-011 databases (no table). */
42
+ readonly perf?: {
43
+ readonly scopes_over_budget: readonly string[];
44
+ };
45
+ readonly verdict: "healthy" | "degraded" | "unknown";
46
+ readonly reason: string;
47
+ readonly partial: boolean;
48
+ }
49
+ /**
50
+ * Count resolved zod copies under `cwd`'s dependency tree. The expected
51
+ * value after K9-005 is 1; a second copy is exactly the regression this
52
+ * field exists to catch. On any walk failure the count is `null` and the
53
+ * note says so — never a guess.
54
+ */
55
+ export declare function countZodCopies(cwd: string): {
56
+ copies: number | null;
57
+ note?: string;
58
+ };
59
+ export interface DoctorOptions {
60
+ /** Directory to walk for zod copies; defaults to env.projectRoot. */
61
+ readonly zodRoot?: string;
62
+ }
63
+ export declare function buildDoctor(store: Store, host: HostSurface, settings: SettingsReader, options?: DoctorOptions, env?: KevinEnv): DoctorReport;
@@ -0,0 +1,197 @@
1
+ import { existsSync, readdirSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { resolveEnv } from "./env.js";
4
+ import { reduceVerdict, } from "./HookLiveness.js";
5
+ import { HOOK_NAMES } from "./Migrate.js";
6
+ /** Dead first, then unknown, then live — the failure is the first thing
7
+ * on screen. Ties break on the canonical hook name. */
8
+ const STATE_ORDER = {
9
+ dead: 0,
10
+ unknown: 1,
11
+ live: 2,
12
+ };
13
+ /** The hooks block is pure SQL over the persisted hook_liveness table:
14
+ * the dead flag is materialized at expect() time (K9-010) so flush() can
15
+ * persist dead_since even when report() never runs — this block reads
16
+ * that persisted state, independent of any live HookLiveness object. */
17
+ function hooksBlock(store) {
18
+ let rows = [];
19
+ try {
20
+ rows = store
21
+ .prepare("SELECT hook, experimental, fire_count, expected_count, dead_since FROM hook_liveness")
22
+ .all();
23
+ }
24
+ catch {
25
+ return { hooks: [], partial: true };
26
+ }
27
+ // Only hooks the plugin knows about are reported; a foreign row (from
28
+ // a future release) is skipped, mirroring HookLiveness.loadFromDb.
29
+ const reports = rows
30
+ .filter((row) => HOOK_NAMES.includes(row.hook))
31
+ .map((row) => ({
32
+ hook: row.hook,
33
+ experimental: row.experimental === 1,
34
+ state: row.fire_count > 0
35
+ ? "live"
36
+ : row.dead_since !== null
37
+ ? "dead"
38
+ : "unknown",
39
+ firstSeenAt: null,
40
+ lastSeenAt: null,
41
+ fireCount: row.fire_count,
42
+ expectedCount: row.expected_count,
43
+ deadSince: row.dead_since,
44
+ }));
45
+ reports.sort((a, b) => STATE_ORDER[a.state] - STATE_ORDER[b.state] ||
46
+ a.hook.localeCompare(b.hook));
47
+ const hooks = reports.map((r) => ({
48
+ hook: r.hook,
49
+ experimental: r.experimental,
50
+ state: r.state,
51
+ fire_count: r.fireCount,
52
+ expected_count: r.expectedCount,
53
+ ...(r.deadSince !== null ? { since: r.deadSince } : {}),
54
+ }));
55
+ return { hooks, partial: false };
56
+ }
57
+ /**
58
+ * Count resolved zod copies under `cwd`'s dependency tree. The expected
59
+ * value after K9-005 is 1; a second copy is exactly the regression this
60
+ * field exists to catch. On any walk failure the count is `null` and the
61
+ * note says so — never a guess.
62
+ */
63
+ export function countZodCopies(cwd) {
64
+ try {
65
+ if (!existsSync(cwd)) {
66
+ return { copies: null, note: "zod resolution walk failed" };
67
+ }
68
+ const seen = new Set();
69
+ const stack = [cwd];
70
+ let copies = 0;
71
+ // Bounded walk: node_modules is normally shallow; nested copies
72
+ // (node_modules/<pkg>/node_modules/zod) sit within a few levels.
73
+ for (let depth = 0; stack.length > 0 && depth < 8; depth += 1) {
74
+ const next = [];
75
+ for (const dir of stack) {
76
+ const nm = join(dir, "node_modules");
77
+ if (!existsSync(nm))
78
+ continue;
79
+ let entries;
80
+ try {
81
+ entries = readdirSync(nm, { withFileTypes: true });
82
+ }
83
+ catch {
84
+ continue;
85
+ }
86
+ for (const entry of entries) {
87
+ if (!entry.isDirectory())
88
+ continue;
89
+ if (entry.name === "zod") {
90
+ const pkg = join(nm, "zod", "package.json");
91
+ if (existsSync(pkg) && !seen.has(pkg)) {
92
+ seen.add(pkg);
93
+ copies += 1;
94
+ }
95
+ }
96
+ else if (entry.name !== ".bin" && entry.name !== ".cache") {
97
+ next.push(join(nm, entry.name));
98
+ }
99
+ }
100
+ }
101
+ stack.length = 0;
102
+ stack.push(...next);
103
+ }
104
+ return { copies };
105
+ }
106
+ catch {
107
+ return { copies: null, note: "zod resolution walk failed" };
108
+ }
109
+ }
110
+ /** v1.0.0 (K10-021) — scopes whose most recent perf sample breached the
111
+ * p95 budget. The within_budget flag is materialized by Perf.flush()
112
+ * against the frozen BUDGETS; this block reads that persisted verdict
113
+ * and never re-aggregates. A missing table (pre-011 DB) yields no
114
+ * block rather than an empty one, matching the hooks precedent. */
115
+ function perfOverBudget(store) {
116
+ try {
117
+ const rows = store
118
+ .prepare("SELECT DISTINCT scope FROM perf_samples WHERE within_budget = 0 ORDER BY scope")
119
+ .all();
120
+ return rows.map((r) => r.scope);
121
+ }
122
+ catch {
123
+ return null;
124
+ }
125
+ }
126
+ function lastRegistration(store, surface) {
127
+ try {
128
+ const row = store
129
+ .prepare("SELECT surface, registered, verified FROM native_registrations WHERE surface = ? ORDER BY attached_at DESC, id DESC LIMIT 1")
130
+ .get(surface);
131
+ if (!row)
132
+ return { registered: false, verified: false };
133
+ return { registered: row.registered === 1, verified: row.verified === 1 };
134
+ }
135
+ catch {
136
+ return { registered: false, verified: false };
137
+ }
138
+ }
139
+ export function buildDoctor(store, host, settings, options = {}, env) {
140
+ // The host block comes from the frozen probe result captured at
141
+ // construction — kevin_doctor never re-probes (K9-004: probe once,
142
+ // freeze, reuse).
143
+ const hooks = hooksBlock(store);
144
+ let verdict = reduceVerdict(hooks.hooks.map((h) => ({
145
+ hook: h.hook,
146
+ experimental: h.experimental,
147
+ state: h.state,
148
+ firstSeenAt: null,
149
+ lastSeenAt: null,
150
+ fireCount: h.fire_count,
151
+ expectedCount: h.expected_count,
152
+ deadSince: h.since ?? null,
153
+ })));
154
+ // v1.0.0 (K10-021) — a slow plugin is not healthy even when every
155
+ // hook is live. A budget breach degrades the verdict and names the
156
+ // scope; a dead hook keeps its reason; `unknown` is never rounded up.
157
+ const breaches = perfOverBudget(store);
158
+ if (breaches !== null &&
159
+ breaches.length > 0 &&
160
+ verdict.verdict !== "degraded") {
161
+ verdict = {
162
+ verdict: "degraded",
163
+ reason: `perf budget exceeded: ${breaches.join(", ")}`,
164
+ };
165
+ }
166
+ const zod = countZodCopies(options.zodRoot ?? resolveEnv(env).projectRoot);
167
+ const enabled = settings.getSetting("native_registration_enabled", "0") === "1";
168
+ return {
169
+ host: {
170
+ plugin_version: host.pluginVersion,
171
+ flavour: host.flavour,
172
+ shell_available: host.hasShell,
173
+ v2: { skill: host.v2.skill, reference: host.v2.reference },
174
+ },
175
+ hooks: hooks.hooks,
176
+ dependencies: {
177
+ declared: ["@opencode-ai/plugin"],
178
+ zod_copies: zod.copies,
179
+ ...(zod.note !== undefined ? { note: zod.note } : {}),
180
+ },
181
+ native: {
182
+ enabled,
183
+ registered: {
184
+ skill: lastRegistration(store, "skill").registered,
185
+ reference: lastRegistration(store, "reference").registered,
186
+ },
187
+ verified: {
188
+ skill: lastRegistration(store, "skill").verified,
189
+ reference: lastRegistration(store, "reference").verified,
190
+ },
191
+ },
192
+ verdict: verdict.verdict,
193
+ reason: verdict.reason,
194
+ ...(breaches !== null ? { perf: { scopes_over_budget: breaches } } : {}),
195
+ partial: hooks.partial,
196
+ };
197
+ }
@@ -0,0 +1,42 @@
1
+ import type { MemoryService } from "./MemoryService.js";
2
+ import type { RepoTruth } from "./RepoTruth.js";
3
+ import type { Store } from "./Store.js";
4
+ /**
5
+ * v0.7.0 (K7-009 / plan §5.7) — `kevin_facts`.
6
+ *
7
+ * The user-facing answer to "why did Kevin stop suggesting that?". Lists the
8
+ * repository facts the current project was scanned for and every memory that
9
+ * was de-ranked by a contradiction, with its `truth_penalty`, `contradicted_at`
10
+ * and the human-readable reasons. Pure read apart from the optional re-scan. No
11
+ * LLM, no network, no hot-path cost.
12
+ */
13
+ export interface KevinFactsDeps {
14
+ store: Store;
15
+ memoryService: MemoryService;
16
+ repoTruth: RepoTruth;
17
+ projectId: string;
18
+ }
19
+ export interface PenalizedMemory {
20
+ id: string;
21
+ type: string;
22
+ truth_penalty: number;
23
+ contradicted_at: string | null;
24
+ reasons: string[];
25
+ }
26
+ export interface KevinFactsResult {
27
+ project_id: string;
28
+ scanned_at: string | null;
29
+ truncated: {
30
+ is_truncated: true;
31
+ count: number;
32
+ } | {
33
+ is_truncated: false;
34
+ };
35
+ facts: {
36
+ file: string;
37
+ key_path: string;
38
+ value: string;
39
+ }[];
40
+ penalized: PenalizedMemory[];
41
+ }
42
+ export declare function buildKevinFacts(deps: KevinFactsDeps, refresh: boolean): KevinFactsResult;
@@ -0,0 +1,36 @@
1
+ export function buildKevinFacts(deps, refresh) {
2
+ if (refresh)
3
+ deps.repoTruth.scan();
4
+ // Penalized memories: scoped to the project, only those with a penalty.
5
+ const rows = deps.store
6
+ .prepare(`SELECT id, type, truth_penalty, contradicted_at
7
+ FROM memories
8
+ WHERE project_id = ? AND truth_penalty > 0
9
+ ORDER BY created_at`)
10
+ .all(deps.projectId);
11
+ const penalized = rows.map((r) => {
12
+ const mem = deps.memoryService.getById(r.id);
13
+ return {
14
+ id: r.id,
15
+ type: r.type,
16
+ truth_penalty: r.truth_penalty,
17
+ contradicted_at: r.contradicted_at,
18
+ reasons: mem ? deps.repoTruth.contradictions(mem) : [],
19
+ };
20
+ });
21
+ const stored = deps.repoTruth.facts();
22
+ const truncatedFact = stored.find((f) => f.keyPath === "_truncated");
23
+ const facts = stored
24
+ .filter((f) => f.keyPath !== "_truncated")
25
+ .map((f) => ({ file: f.file, key_path: f.keyPath, value: f.value }));
26
+ const scanned_at = deps.repoTruth.scannedAt();
27
+ return {
28
+ project_id: deps.projectId,
29
+ scanned_at,
30
+ truncated: truncatedFact
31
+ ? { is_truncated: true, count: Number(truncatedFact.value) || 0 }
32
+ : { is_truncated: false },
33
+ facts,
34
+ penalized,
35
+ };
36
+ }
@@ -0,0 +1,33 @@
1
+ import type { MemoryService } from "./MemoryService.js";
2
+ import type { SharedLayer } from "./SharedLayer.js";
3
+ import type { Store } from "./Store.js";
4
+ import type { Metrics } from "./metrics.js";
5
+ export interface ForgetInput {
6
+ ids: string[];
7
+ confirm?: boolean;
8
+ }
9
+ export interface Deps {
10
+ store: Store;
11
+ memoryService: MemoryService;
12
+ sharedLayer: SharedLayer;
13
+ okfPath: string;
14
+ metrics: Metrics;
15
+ }
16
+ export interface ForgetResult {
17
+ action: "forget";
18
+ ok: boolean;
19
+ dry_run: boolean;
20
+ per_id: Array<{
21
+ id: string;
22
+ archived: boolean;
23
+ reason?: string;
24
+ tombstone?: {
25
+ entry_id: string;
26
+ planned: boolean;
27
+ applied: boolean;
28
+ };
29
+ }>;
30
+ noop?: boolean;
31
+ reason?: string;
32
+ }
33
+ export declare function handleForget(input: ForgetInput, deps: Deps): ForgetResult;