@ngockhoale/ckit 0.0.0-stage → 0.1.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 (246) hide show
  1. package/.env.example +21 -0
  2. package/README.md +70 -2
  3. package/dist/src/adapters/agent/claude/errors.d.ts +13 -0
  4. package/dist/src/adapters/agent/claude/errors.js +16 -0
  5. package/dist/src/adapters/agent/claude/normalize.d.ts +11 -0
  6. package/dist/src/adapters/agent/claude/normalize.js +83 -0
  7. package/dist/src/adapters/agent/errors.d.ts +13 -0
  8. package/dist/src/adapters/agent/errors.js +19 -0
  9. package/dist/src/adapters/agent/fixtures/claude-transcript.d.ts +70 -0
  10. package/dist/src/adapters/agent/fixtures/claude-transcript.js +15 -0
  11. package/dist/src/adapters/agent/fixtures/omp-transcript.d.ts +60 -0
  12. package/dist/src/adapters/agent/fixtures/omp-transcript.js +14 -0
  13. package/dist/src/adapters/agent/normalize.d.ts +18 -0
  14. package/dist/src/adapters/agent/normalize.js +77 -0
  15. package/dist/src/adapters/agent/transport.d.ts +35 -0
  16. package/dist/src/adapters/agent/transport.js +39 -0
  17. package/dist/src/adapters/agent/worker-service.d.ts +49 -0
  18. package/dist/src/adapters/agent/worker-service.js +77 -0
  19. package/dist/src/adapters/fake/fake-advisor.d.ts +15 -0
  20. package/dist/src/adapters/fake/fake-advisor.js +29 -0
  21. package/dist/src/adapters/fake/fake-agent.d.ts +12 -0
  22. package/dist/src/adapters/fake/fake-agent.js +21 -0
  23. package/dist/src/adapters/fake/fake-store.d.ts +13 -0
  24. package/dist/src/adapters/fake/fake-store.js +23 -0
  25. package/dist/src/adapters/fake/fake-web.d.ts +12 -0
  26. package/dist/src/adapters/fake/fake-web.js +21 -0
  27. package/dist/src/adapters/fake/fault-injector.d.ts +18 -0
  28. package/dist/src/adapters/fake/fault-injector.js +64 -0
  29. package/dist/src/adapters/fake/virtual-clock.d.ts +12 -0
  30. package/dist/src/adapters/fake/virtual-clock.js +35 -0
  31. package/dist/src/adapters/launch/discover.d.ts +18 -0
  32. package/dist/src/adapters/launch/discover.js +55 -0
  33. package/dist/src/adapters/launch/executor.d.ts +117 -0
  34. package/dist/src/adapters/launch/executor.js +383 -0
  35. package/dist/src/adapters/launch/profile.d.ts +35 -0
  36. package/dist/src/adapters/launch/profile.js +73 -0
  37. package/dist/src/adapters/launch/spawn.d.ts +29 -0
  38. package/dist/src/adapters/launch/spawn.js +18 -0
  39. package/dist/src/adapters/openai/openai-advisor.d.ts +56 -0
  40. package/dist/src/adapters/openai/openai-advisor.js +146 -0
  41. package/dist/src/cli/ckit.d.ts +5 -0
  42. package/dist/src/cli/ckit.js +384 -0
  43. package/dist/src/cli/demo.d.ts +1 -0
  44. package/dist/src/cli/demo.js +30 -0
  45. package/dist/src/cli/lifecycle.d.ts +19 -0
  46. package/dist/src/cli/lifecycle.js +90 -0
  47. package/dist/src/cli/sessions.d.ts +10 -0
  48. package/dist/src/cli/sessions.js +381 -0
  49. package/dist/src/cli/setup.d.ts +11 -0
  50. package/dist/src/cli/setup.js +115 -0
  51. package/dist/src/committee/decision.d.ts +84 -0
  52. package/dist/src/committee/decision.js +167 -0
  53. package/dist/src/committee/handover.d.ts +115 -0
  54. package/dist/src/committee/handover.js +191 -0
  55. package/dist/src/committee/no-progress.d.ts +44 -0
  56. package/dist/src/committee/no-progress.js +64 -0
  57. package/dist/src/committee/primary-registry.d.ts +15 -0
  58. package/dist/src/committee/primary-registry.js +60 -0
  59. package/dist/src/committee/reply-parser.d.ts +28 -0
  60. package/dist/src/committee/reply-parser.js +53 -0
  61. package/dist/src/committee/reviewer-scheduler.d.ts +101 -0
  62. package/dist/src/committee/reviewer-scheduler.js +163 -0
  63. package/dist/src/committee/role-epoch.d.ts +91 -0
  64. package/dist/src/committee/role-epoch.js +154 -0
  65. package/dist/src/core/control.d.ts +18 -0
  66. package/dist/src/core/control.js +29 -0
  67. package/dist/src/core/entities.d.ts +166 -0
  68. package/dist/src/core/entities.js +119 -0
  69. package/dist/src/core/errors.d.ts +9 -0
  70. package/dist/src/core/errors.js +17 -0
  71. package/dist/src/core/events.d.ts +72 -0
  72. package/dist/src/core/events.js +125 -0
  73. package/dist/src/core/ids.d.ts +47 -0
  74. package/dist/src/core/ids.js +32 -0
  75. package/dist/src/core/machine.d.ts +55 -0
  76. package/dist/src/core/machine.js +110 -0
  77. package/dist/src/core/policy.d.ts +34 -0
  78. package/dist/src/core/policy.js +59 -0
  79. package/dist/src/core/provenance.d.ts +20 -0
  80. package/dist/src/core/provenance.js +21 -0
  81. package/dist/src/core/record.d.ts +35 -0
  82. package/dist/src/core/record.js +67 -0
  83. package/dist/src/core/replay.d.ts +10 -0
  84. package/dist/src/core/replay.js +36 -0
  85. package/dist/src/core/run-task.d.ts +20 -0
  86. package/dist/src/core/run-task.js +48 -0
  87. package/dist/src/diagnostics/doctor.d.ts +78 -0
  88. package/dist/src/diagnostics/doctor.js +365 -0
  89. package/dist/src/diagnostics/journal-lock.d.ts +38 -0
  90. package/dist/src/diagnostics/journal-lock.js +181 -0
  91. package/dist/src/evidence/linker.d.ts +44 -0
  92. package/dist/src/evidence/linker.js +75 -0
  93. package/dist/src/evidence/report-parser.d.ts +27 -0
  94. package/dist/src/evidence/report-parser.js +17 -0
  95. package/dist/src/evidence/store.d.ts +36 -0
  96. package/dist/src/evidence/store.js +57 -0
  97. package/dist/src/gui/server.d.ts +27 -0
  98. package/dist/src/gui/server.js +497 -0
  99. package/dist/src/host/checkpoint.d.ts +22 -0
  100. package/dist/src/host/checkpoint.js +74 -0
  101. package/dist/src/host/export.d.ts +52 -0
  102. package/dist/src/host/export.js +45 -0
  103. package/dist/src/host/ipc.d.ts +57 -0
  104. package/dist/src/host/ipc.js +124 -0
  105. package/dist/src/host/leases.d.ts +27 -0
  106. package/dist/src/host/leases.js +40 -0
  107. package/dist/src/host/retention.d.ts +47 -0
  108. package/dist/src/host/retention.js +130 -0
  109. package/dist/src/host/supervisor.d.ts +59 -0
  110. package/dist/src/host/supervisor.js +64 -0
  111. package/dist/src/intake/owner-intake.d.ts +31 -0
  112. package/dist/src/intake/owner-intake.js +97 -0
  113. package/dist/src/intake/types.d.ts +46 -0
  114. package/dist/src/intake/types.js +2 -0
  115. package/dist/src/intent/context-packet.d.ts +68 -0
  116. package/dist/src/intent/context-packet.js +76 -0
  117. package/dist/src/intent/idea-ledger.d.ts +50 -0
  118. package/dist/src/intent/idea-ledger.js +136 -0
  119. package/dist/src/intent/ledger.d.ts +35 -0
  120. package/dist/src/intent/ledger.js +40 -0
  121. package/dist/src/intent/registry.d.ts +51 -0
  122. package/dist/src/intent/registry.js +44 -0
  123. package/dist/src/intent/revision.d.ts +37 -0
  124. package/dist/src/intent/revision.js +48 -0
  125. package/dist/src/orchestration/dispatcher.d.ts +32 -0
  126. package/dist/src/orchestration/dispatcher.js +136 -0
  127. package/dist/src/orchestration/milestone-loop.d.ts +149 -0
  128. package/dist/src/orchestration/milestone-loop.js +132 -0
  129. package/dist/src/orchestration/recovery-coordinator.d.ts +74 -0
  130. package/dist/src/orchestration/recovery-coordinator.js +180 -0
  131. package/dist/src/orchestration/work-order-contract.d.ts +44 -0
  132. package/dist/src/orchestration/work-order-contract.js +85 -0
  133. package/dist/src/orchestration/work-queue.d.ts +56 -0
  134. package/dist/src/orchestration/work-queue.js +95 -0
  135. package/dist/src/ports/advisor.d.ts +33 -0
  136. package/dist/src/ports/advisor.js +2 -0
  137. package/dist/src/ports/agent.d.ts +31 -0
  138. package/dist/src/ports/agent.js +2 -0
  139. package/dist/src/ports/capabilities.d.ts +3 -0
  140. package/dist/src/ports/capabilities.js +5 -0
  141. package/dist/src/ports/clock.d.ts +5 -0
  142. package/dist/src/ports/clock.js +5 -0
  143. package/dist/src/ports/store.d.ts +13 -0
  144. package/dist/src/ports/store.js +2 -0
  145. package/dist/src/ports/web.d.ts +29 -0
  146. package/dist/src/ports/web.js +2 -0
  147. package/dist/src/run/api-run.d.ts +49 -0
  148. package/dist/src/run/api-run.js +173 -0
  149. package/dist/src/run/plan-run.d.ts +22 -0
  150. package/dist/src/run/plan-run.js +233 -0
  151. package/dist/src/run/plan.d.ts +36 -0
  152. package/dist/src/run/plan.js +119 -0
  153. package/dist/src/run/verify.d.ts +25 -0
  154. package/dist/src/run/verify.js +81 -0
  155. package/dist/src/security/redact.d.ts +7 -0
  156. package/dist/src/security/redact.js +21 -0
  157. package/dist/src/settings/env-config.d.ts +64 -0
  158. package/dist/src/settings/env-config.js +232 -0
  159. package/dist/src/storage/artifacts.d.ts +30 -0
  160. package/dist/src/storage/artifacts.js +105 -0
  161. package/dist/src/storage/audit.d.ts +6 -0
  162. package/dist/src/storage/audit.js +6 -0
  163. package/dist/src/storage/db.d.ts +9 -0
  164. package/dist/src/storage/db.js +43 -0
  165. package/dist/src/storage/events.d.ts +20 -0
  166. package/dist/src/storage/events.js +69 -0
  167. package/dist/src/storage/migrations.d.ts +8 -0
  168. package/dist/src/storage/migrations.js +48 -0
  169. package/dist/src/storage/outbox.d.ts +25 -0
  170. package/dist/src/storage/outbox.js +40 -0
  171. package/dist/src/storage/projections.d.ts +18 -0
  172. package/dist/src/storage/projections.js +40 -0
  173. package/dist/src/storage/reconcile.d.ts +14 -0
  174. package/dist/src/storage/reconcile.js +93 -0
  175. package/dist/src/storage/reservations.d.ts +12 -0
  176. package/dist/src/storage/reservations.js +80 -0
  177. package/dist/src/storage/store.d.ts +34 -0
  178. package/dist/src/storage/store.js +105 -0
  179. package/dist/src/team/attachments.d.ts +22 -0
  180. package/dist/src/team/attachments.js +211 -0
  181. package/dist/src/team/cli-models.d.ts +24 -0
  182. package/dist/src/team/cli-models.js +72 -0
  183. package/dist/src/team/config.d.ts +20 -0
  184. package/dist/src/team/config.js +113 -0
  185. package/dist/src/team/engine.d.ts +28 -0
  186. package/dist/src/team/engine.js +443 -0
  187. package/dist/src/team/journal.d.ts +30 -0
  188. package/dist/src/team/journal.js +70 -0
  189. package/dist/src/team/runners.d.ts +79 -0
  190. package/dist/src/team/runners.js +436 -0
  191. package/dist/src/team/secrets.d.ts +10 -0
  192. package/dist/src/team/secrets.js +68 -0
  193. package/dist/src/team/types.d.ts +157 -0
  194. package/dist/src/team/types.js +5 -0
  195. package/dist/src/tui/app.d.ts +29 -0
  196. package/dist/src/tui/app.js +96 -0
  197. package/dist/src/tui/control-model.d.ts +101 -0
  198. package/dist/src/tui/control-model.js +109 -0
  199. package/dist/src/tui/control-screens.d.ts +27 -0
  200. package/dist/src/tui/control-screens.js +85 -0
  201. package/dist/src/tui/graph-model.d.ts +34 -0
  202. package/dist/src/tui/graph-model.js +29 -0
  203. package/dist/src/tui/graph-screens.d.ts +13 -0
  204. package/dist/src/tui/graph-screens.js +4 -0
  205. package/dist/src/tui/inspector-model.d.ts +150 -0
  206. package/dist/src/tui/inspector-model.js +15 -0
  207. package/dist/src/tui/inspector-screens.d.ts +14 -0
  208. package/dist/src/tui/inspector-screens.js +61 -0
  209. package/dist/src/tui/live.d.ts +16 -0
  210. package/dist/src/tui/live.js +39 -0
  211. package/dist/src/tui/mission-action-screens.d.ts +15 -0
  212. package/dist/src/tui/mission-action-screens.js +105 -0
  213. package/dist/src/tui/mission-actions.d.ts +46 -0
  214. package/dist/src/tui/mission-actions.js +53 -0
  215. package/dist/src/tui/mission-model.d.ts +120 -0
  216. package/dist/src/tui/mission-model.js +164 -0
  217. package/dist/src/tui/mission-nav.d.ts +82 -0
  218. package/dist/src/tui/mission-nav.js +131 -0
  219. package/dist/src/tui/mission-plan-screens.d.ts +11 -0
  220. package/dist/src/tui/mission-plan-screens.js +40 -0
  221. package/dist/src/tui/mission-plan.d.ts +45 -0
  222. package/dist/src/tui/mission-plan.js +166 -0
  223. package/dist/src/tui/mission-screens.d.ts +7 -0
  224. package/dist/src/tui/mission-screens.js +263 -0
  225. package/dist/src/tui/provenance-label.d.ts +11 -0
  226. package/dist/src/tui/provenance-label.js +16 -0
  227. package/dist/src/tui/screens.d.ts +10 -0
  228. package/dist/src/tui/screens.js +34 -0
  229. package/dist/src/tui/session-outcome.d.ts +14 -0
  230. package/dist/src/tui/session-outcome.js +57 -0
  231. package/dist/src/tui/setup-model.d.ts +159 -0
  232. package/dist/src/tui/setup-model.js +234 -0
  233. package/dist/src/tui/setup-screens.d.ts +66 -0
  234. package/dist/src/tui/setup-screens.js +72 -0
  235. package/dist/src/tui/team/app.d.ts +24 -0
  236. package/dist/src/tui/team/app.js +228 -0
  237. package/dist/src/tui/team/demo-handle.d.ts +22 -0
  238. package/dist/src/tui/team/demo-handle.js +42 -0
  239. package/dist/src/tui/team/model.d.ts +81 -0
  240. package/dist/src/tui/team/model.js +133 -0
  241. package/gui/app.css +177 -0
  242. package/gui/app.js +677 -0
  243. package/gui/index.html +51 -0
  244. package/gui/network.js +413 -0
  245. package/package.json +44 -4
  246. package/src/storage/schema.sql +42 -0
@@ -0,0 +1,181 @@
1
+ import { randomBytes } from 'node:crypto';
2
+ import { link, open, readFile, stat, unlink } from 'node:fs/promises';
3
+ import { hostname as osHostname } from 'node:os';
4
+ import { join } from 'node:path';
5
+ export const JOURNAL_LOCK_FILE = 'events.lock';
6
+ export const DEFAULT_LOCK_STALE_AFTER_MS = 600_000;
7
+ export const UNREADABLE_LOCK_GRACE_MS = 5_000;
8
+ const GUARD_SUFFIX = '.takeover';
9
+ /** Default liveness probe: signal 0; ESRCH means dead, EPERM means alive. */
10
+ export function isProcessAlive(pid) {
11
+ try {
12
+ process.kill(pid, 0);
13
+ return true;
14
+ }
15
+ catch (error) {
16
+ return error.code !== 'ESRCH';
17
+ }
18
+ }
19
+ function validateStale(value) {
20
+ if (value === undefined)
21
+ return DEFAULT_LOCK_STALE_AFTER_MS;
22
+ if (!Number.isSafeInteger(value) || value <= 0)
23
+ throw new TypeError('staleAfterMs must be a positive safe integer');
24
+ return value;
25
+ }
26
+ function parseOwner(raw) {
27
+ try {
28
+ const value = JSON.parse(raw);
29
+ if (!value || typeof value !== 'object' || value.version !== 1 || typeof value.pid !== 'number' || !Number.isSafeInteger(value.pid) || value.pid <= 0 || typeof value.hostname !== 'string' || typeof value.createdAt !== 'string' || typeof value.token !== 'string' || Number.isNaN(Date.parse(value.createdAt)))
30
+ return null;
31
+ return value;
32
+ }
33
+ catch {
34
+ return null;
35
+ }
36
+ }
37
+ async function snapshot(lockPath, options) {
38
+ const staleAfterMs = validateStale(options.staleAfterMs);
39
+ const now = (options.now ?? Date.now)();
40
+ const host = options.hostname ?? osHostname();
41
+ const isAlive = options.isAlive ?? isProcessAlive;
42
+ let info, raw;
43
+ try {
44
+ info = await stat(lockPath);
45
+ raw = await readFile(lockPath, 'utf8');
46
+ }
47
+ catch (error) {
48
+ if (error.code === 'ENOENT')
49
+ return null;
50
+ throw error;
51
+ }
52
+ const owner = parseOwner(raw);
53
+ if (!owner) {
54
+ const ageMs = Math.max(0, now - info.mtimeMs);
55
+ if (ageMs > UNREADABLE_LOCK_GRACE_MS)
56
+ return { inspection: { state: 'stale', reason: 'unreadable', owner: null, ageMs }, raw, ino: info.ino, unreadableBusy: false };
57
+ const synthetic = { version: 1, pid: 0, hostname: 'unknown', createdAt: new Date(info.mtimeMs).toISOString(), token: '' };
58
+ return { inspection: { state: 'held', owner: synthetic, ageMs, sameHost: false }, raw, ino: info.ino, unreadableBusy: true };
59
+ }
60
+ const ageMs = Math.max(0, now - Date.parse(owner.createdAt));
61
+ const sameHost = owner.hostname === host;
62
+ let inspection;
63
+ if (sameHost)
64
+ inspection = isAlive(owner.pid) ? { state: 'held', owner, ageMs, sameHost } : { state: 'stale', reason: 'dead-pid', owner, ageMs };
65
+ else
66
+ inspection = ageMs > staleAfterMs ? { state: 'stale', reason: 'expired', owner, ageMs } : { state: 'held', owner, ageMs, sameHost };
67
+ return { inspection, raw, ino: info.ino, unreadableBusy: false };
68
+ }
69
+ /** Read-only classification of the journal lock. */
70
+ export async function inspectJournalLock(stateDir, options = {}) {
71
+ validateStale(options.staleAfterMs);
72
+ return (await snapshot(join(stateDir, JOURNAL_LOCK_FILE), options))?.inspection ?? { state: 'absent' };
73
+ }
74
+ function busy(snap) {
75
+ const inspection = snap?.inspection;
76
+ if (inspection && inspection.state !== 'absent') {
77
+ if (snap.unreadableBusy || (inspection.state === 'stale' && inspection.owner === null))
78
+ return new Error(`event journal is busy (lock unreadable, age ${Math.round(inspection.ageMs)}ms); retry after that CKit command exits`);
79
+ if (inspection.owner)
80
+ return new Error(`event journal is busy (held by pid ${inspection.owner.pid} on ${inspection.owner.hostname} since ${inspection.owner.createdAt}); retry after that CKit command exits`);
81
+ }
82
+ return new Error('event journal is busy (another CKit command is recovering the lock); retry after that CKit command exits');
83
+ }
84
+ const code = (error) => error.code;
85
+ async function tryLink(stateDir, lockPath, ownerJson) {
86
+ const temp = join(stateDir, `events-lock-${randomBytes(6).toString('hex')}.tmp`);
87
+ const file = await open(temp, 'wx', 0o600);
88
+ try {
89
+ await file.writeFile(ownerJson, 'utf8');
90
+ await file.sync();
91
+ }
92
+ catch (error) {
93
+ await file.close().catch(() => undefined);
94
+ await unlink(temp).catch(() => undefined);
95
+ throw error;
96
+ }
97
+ await file.close();
98
+ try {
99
+ await link(temp, lockPath);
100
+ return true;
101
+ }
102
+ catch (error) {
103
+ if (code(error) === 'EEXIST')
104
+ return false;
105
+ throw error;
106
+ }
107
+ finally {
108
+ await unlink(temp).catch(() => undefined);
109
+ }
110
+ }
111
+ async function acquire(stateDir, lockPath, ownerJson, options) {
112
+ if (await tryLink(stateDir, lockPath, ownerJson))
113
+ return;
114
+ const snap = await snapshot(lockPath, options);
115
+ if (snap === null) {
116
+ if (await tryLink(stateDir, lockPath, ownerJson))
117
+ return;
118
+ throw busy(await snapshot(lockPath, options));
119
+ }
120
+ if (snap.inspection.state !== 'stale')
121
+ throw busy(snap);
122
+ await options.beforeTakeover?.();
123
+ const guardPath = `${lockPath}${GUARD_SUFFIX}`;
124
+ let guard;
125
+ try {
126
+ guard = await open(guardPath, 'wx', 0o600);
127
+ }
128
+ catch (error) {
129
+ if (code(error) !== 'EEXIST')
130
+ throw error;
131
+ // Remove an abandoned guard only if it is still the same file (ino + mtime) we judged stale.
132
+ const seen = await stat(guardPath).catch(() => null);
133
+ if (seen && (options.now ?? Date.now)() - seen.mtimeMs > UNREADABLE_LOCK_GRACE_MS) {
134
+ const recheck = await stat(guardPath).catch(() => null);
135
+ if (recheck && recheck.ino === seen.ino && recheck.mtimeMs === seen.mtimeMs)
136
+ await unlink(guardPath).catch(() => undefined);
137
+ }
138
+ throw busy(null);
139
+ }
140
+ try {
141
+ const again = await snapshot(lockPath, options);
142
+ if (again === null) {
143
+ if (await tryLink(stateDir, lockPath, ownerJson))
144
+ return;
145
+ throw busy(await snapshot(lockPath, options));
146
+ }
147
+ if (again.inspection.state !== 'stale' || again.raw !== snap.raw || again.ino !== snap.ino)
148
+ throw busy(again);
149
+ try {
150
+ await unlink(lockPath);
151
+ }
152
+ catch (error) {
153
+ if (code(error) === 'ENOENT')
154
+ throw busy(null);
155
+ throw error;
156
+ }
157
+ if (!(await tryLink(stateDir, lockPath, ownerJson)))
158
+ throw busy(await snapshot(lockPath, options));
159
+ }
160
+ finally {
161
+ await guard.close();
162
+ await unlink(guardPath).catch(() => undefined);
163
+ }
164
+ }
165
+ /** Atomic, owner-recorded journal lock with race-safe stale recovery; a live lock is never stolen. */
166
+ export async function withJournalLock(stateDir, operation, options = {}) {
167
+ validateStale(options.staleAfterMs);
168
+ const lockPath = join(stateDir, JOURNAL_LOCK_FILE);
169
+ const token = randomBytes(12).toString('hex');
170
+ const owner = { version: 1, pid: options.pid ?? process.pid, hostname: options.hostname ?? osHostname(), createdAt: new Date((options.now ?? Date.now)()).toISOString(), token };
171
+ await acquire(stateDir, lockPath, JSON.stringify(owner), options);
172
+ try {
173
+ return await operation();
174
+ }
175
+ finally {
176
+ const current = await readFile(lockPath, 'utf8').then(parseOwner, () => null);
177
+ if (current?.token === token)
178
+ await unlink(lockPath).catch(() => undefined);
179
+ }
180
+ }
181
+ //# sourceMappingURL=journal-lock.js.map
@@ -0,0 +1,44 @@
1
+ import type { VerificationLevel } from './store.js';
2
+ import { EvidenceStore } from './store.js';
3
+ export interface AcceptanceCriterion {
4
+ readonly criterionId: string;
5
+ readonly requiredLevel: VerificationLevel;
6
+ readonly revision: string;
7
+ readonly cwd: string;
8
+ }
9
+ export interface EvidenceLink {
10
+ readonly criterionId: string;
11
+ readonly evidenceId: string;
12
+ readonly verification: VerificationLevel;
13
+ readonly revision: string;
14
+ readonly cwd: string;
15
+ }
16
+ export interface EvidenceJustification {
17
+ readonly criterionId: string;
18
+ readonly evidenceId: string;
19
+ readonly reason: string;
20
+ }
21
+ export interface EvaluationContext {
22
+ readonly revision: string;
23
+ readonly cwd: string;
24
+ }
25
+ export interface CriterionEvaluation {
26
+ readonly criterionId: string;
27
+ readonly satisfied: boolean;
28
+ readonly links: readonly EvidenceLink[];
29
+ readonly escalation: string | null;
30
+ }
31
+ export declare class EvidenceLinker {
32
+ private readonly evidence;
33
+ private readonly criteria;
34
+ private readonly links;
35
+ private readonly justifications;
36
+ private readonly escalations;
37
+ constructor(evidence: EvidenceStore);
38
+ attach(criterion: AcceptanceCriterion, evidenceId: string): EvidenceLink;
39
+ justify(input: EvidenceJustification): void;
40
+ evaluate(criterionId: string, context: EvaluationContext): CriterionEvaluation;
41
+ complete(criterionIds: readonly string[], context: EvaluationContext): boolean;
42
+ getEscalations(): readonly string[];
43
+ getJustifications(): readonly EvidenceJustification[];
44
+ }
@@ -0,0 +1,75 @@
1
+ import { EvidenceStore } from './store.js';
2
+ const LEVEL = { reported: 0, observed: 1, verified: 2 };
3
+ export class EvidenceLinker {
4
+ evidence;
5
+ criteria = new Map();
6
+ links = new Map();
7
+ justifications = new Map();
8
+ escalations = [];
9
+ constructor(evidence) {
10
+ this.evidence = evidence;
11
+ }
12
+ attach(criterion, evidenceId) {
13
+ if (!criterion.criterionId || !criterion.revision || !criterion.cwd)
14
+ throw new TypeError('criterion ID, revision and cwd are required');
15
+ const record = this.evidence.get(evidenceId);
16
+ if (!record)
17
+ throw new TypeError(`unknown evidence: ${evidenceId}`);
18
+ const existingCriterion = this.criteria.get(criterion.criterionId);
19
+ if (existingCriterion && existingCriterion.requiredLevel !== criterion.requiredLevel)
20
+ throw new TypeError(`cannot change required level for criterion: ${criterion.criterionId}`);
21
+ if (!existingCriterion)
22
+ this.criteria.set(criterion.criterionId, Object.freeze({ ...criterion }));
23
+ const link = Object.freeze({ criterionId: criterion.criterionId, evidenceId, verification: record.verification, revision: record.revision, cwd: record.cwd });
24
+ const list = this.links.get(criterion.criterionId) ?? [];
25
+ list.push(link);
26
+ this.links.set(criterion.criterionId, list);
27
+ return link;
28
+ }
29
+ justify(input) {
30
+ if (!input.reason.trim())
31
+ throw new TypeError('justification reason is required');
32
+ const criterion = this.criteria.get(input.criterionId);
33
+ const record = this.evidence.get(input.evidenceId);
34
+ if (!criterion || !record || !(this.links.get(input.criterionId) ?? []).some(link => link.evidenceId === input.evidenceId))
35
+ throw new TypeError('justification must identify linked evidence and criterion');
36
+ const key = `${input.criterionId}\0${input.evidenceId}`;
37
+ this.justifications.set(key, Object.freeze({ ...input }));
38
+ }
39
+ evaluate(criterionId, context) {
40
+ const criterion = this.criteria.get(criterionId);
41
+ const links = Object.freeze([...(this.links.get(criterionId) ?? [])]);
42
+ if (!criterion)
43
+ return Object.freeze({ criterionId, satisfied: false, links, escalation: null });
44
+ const candidates = [];
45
+ let mismatch = false;
46
+ for (const link of links) {
47
+ const record = this.evidence.get(link.evidenceId);
48
+ if (!record)
49
+ continue;
50
+ const bound = record.revision === context.revision && record.cwd === context.cwd;
51
+ const justified = this.justifications.has(`${criterionId}\0${record.id}`);
52
+ if (!bound && !justified) {
53
+ mismatch = true;
54
+ continue;
55
+ }
56
+ candidates.push(record);
57
+ }
58
+ const eligible = candidates.filter(record => LEVEL[record.verification] >= LEVEL[criterion.requiredLevel] && record.issues.length === 0);
59
+ const outcomes = new Set(eligible.map(record => record.outcome).filter(value => value !== 'unknown'));
60
+ if (outcomes.has('pass') && outcomes.has('fail')) {
61
+ const escalation = `contradictory evidence for ${criterionId}`;
62
+ if (!this.escalations.includes(escalation))
63
+ this.escalations.push(escalation);
64
+ return Object.freeze({ criterionId, satisfied: false, links, escalation });
65
+ }
66
+ const passed = eligible.some(record => record.outcome === 'pass');
67
+ return Object.freeze({ criterionId, satisfied: passed && !mismatch, links, escalation: mismatch ? `evidence binding mismatch for ${criterionId}` : null });
68
+ }
69
+ complete(criterionIds, context) {
70
+ return criterionIds.every(id => this.evaluate(id, context).satisfied);
71
+ }
72
+ getEscalations() { return Object.freeze([...this.escalations]); }
73
+ getJustifications() { return Object.freeze([...this.justifications.values()]); }
74
+ }
75
+ //# sourceMappingURL=linker.js.map
@@ -0,0 +1,27 @@
1
+ import { EvidenceStore, type EvidenceRecord, type EvidenceOutcome, type VerificationLevel } from './store.js';
2
+ export { inferOutcome } from './store.js';
3
+ export interface ToolReportInput {
4
+ readonly source: string;
5
+ readonly raw: string;
6
+ readonly verification: VerificationLevel;
7
+ readonly revision: string;
8
+ readonly cwd: string;
9
+ readonly exitCode?: number | null;
10
+ readonly artifactRef?: string;
11
+ readonly artifactHash?: string;
12
+ readonly outcome?: EvidenceOutcome;
13
+ }
14
+ export interface PipelineReportInput {
15
+ readonly source: string;
16
+ readonly raw: string;
17
+ readonly revision: string;
18
+ readonly cwd: string;
19
+ readonly finalRunner?: {
20
+ readonly exitCode: number;
21
+ readonly proven: boolean;
22
+ };
23
+ readonly artifactRef?: string;
24
+ readonly artifactHash?: string;
25
+ }
26
+ export declare function parseToolReport(input: ToolReportInput, store?: EvidenceStore): EvidenceRecord;
27
+ export declare function parsePipelineReport(input: PipelineReportInput, store?: EvidenceStore): EvidenceRecord;
@@ -0,0 +1,17 @@
1
+ import { EvidenceStore, inferOutcome } from './store.js';
2
+ export { inferOutcome } from './store.js';
3
+ export function parseToolReport(input, store = new EvidenceStore()) {
4
+ return store.add({ ...input, outcome: input.outcome ?? inferOutcome(input.raw) });
5
+ }
6
+ export function parsePipelineReport(input, store = new EvidenceStore()) {
7
+ const proven = input.finalRunner?.proven === true && Number.isInteger(input.finalRunner.exitCode);
8
+ return store.add({
9
+ source: input.source, raw: input.raw, revision: input.revision, cwd: input.cwd,
10
+ verification: proven && input.finalRunner?.exitCode === 0 ? 'verified' : 'reported',
11
+ pipeline: true, finalRunnerProven: proven, exitCode: proven ? input.finalRunner.exitCode : null,
12
+ ...(input.artifactRef === undefined ? {} : { artifactRef: input.artifactRef }),
13
+ ...(input.artifactHash === undefined ? {} : { artifactHash: input.artifactHash }),
14
+ outcome: proven ? input.finalRunner.exitCode === 0 ? 'pass' : 'fail' : 'unknown',
15
+ });
16
+ }
17
+ //# sourceMappingURL=report-parser.js.map
@@ -0,0 +1,36 @@
1
+ export type VerificationLevel = 'reported' | 'observed' | 'verified';
2
+ export type EvidenceOutcome = 'pass' | 'fail' | 'unknown';
3
+ export interface EvidenceInput {
4
+ readonly source: string;
5
+ readonly raw: string;
6
+ readonly verification: VerificationLevel;
7
+ readonly revision: string;
8
+ readonly cwd: string;
9
+ readonly exitCode?: number | null;
10
+ readonly pipeline?: boolean;
11
+ readonly finalRunnerProven?: boolean;
12
+ readonly artifactRef?: string;
13
+ readonly artifactHash?: string;
14
+ readonly derivedFrom?: readonly string[];
15
+ readonly outcome?: EvidenceOutcome;
16
+ }
17
+ export interface EvidenceRecord extends Omit<EvidenceInput, 'artifactRef' | 'artifactHash' | 'derivedFrom' | 'exitCode' | 'pipeline' | 'finalRunnerProven' | 'outcome'> {
18
+ readonly id: string;
19
+ readonly verification: VerificationLevel;
20
+ readonly exitCode: number | null;
21
+ readonly pipeline: boolean;
22
+ readonly finalRunnerProven: boolean;
23
+ readonly artifactRef: string | null;
24
+ readonly artifactHash: string | null;
25
+ readonly derivedFrom: readonly string[];
26
+ readonly outcome: EvidenceOutcome;
27
+ readonly issues: readonly string[];
28
+ }
29
+ export declare class EvidenceStore {
30
+ private readonly records;
31
+ private nextId;
32
+ add(input: EvidenceInput): EvidenceRecord;
33
+ get(id: string): EvidenceRecord | undefined;
34
+ list(): readonly EvidenceRecord[];
35
+ }
36
+ export declare function inferOutcome(raw: string): EvidenceOutcome;
@@ -0,0 +1,57 @@
1
+ export class EvidenceStore {
2
+ records = new Map();
3
+ nextId = 1;
4
+ add(input) {
5
+ if (!input.source || !input.raw || !input.revision || !input.cwd)
6
+ throw new TypeError('evidence source, raw payload, revision and cwd are required');
7
+ if (!['reported', 'observed', 'verified'].includes(input.verification))
8
+ throw new TypeError('unknown verification level');
9
+ const issues = [];
10
+ let verification = input.verification;
11
+ let artifactRef = input.artifactRef ?? null;
12
+ let artifactHash = input.artifactHash ?? null;
13
+ if (artifactRef !== null && !/^[a-z][a-z0-9_-]{5,63}$/.test(artifactRef)) {
14
+ issues.push('invalid artifact reference');
15
+ artifactRef = null;
16
+ verification = 'reported';
17
+ }
18
+ if ((artifactRef === null) !== (artifactHash === null) || (artifactHash !== null && !/^[a-f0-9]{64}$/i.test(artifactHash))) {
19
+ issues.push('artifact hash missing or invalid');
20
+ verification = 'reported';
21
+ }
22
+ const pipeline = input.pipeline ?? false;
23
+ const finalRunnerProven = input.finalRunnerProven ?? false;
24
+ const exitCode = input.exitCode ?? null;
25
+ if (pipeline && (!finalRunnerProven || exitCode === null)) {
26
+ issues.push('pipeline final-runner exit is not proven');
27
+ if (verification === 'verified')
28
+ verification = 'reported';
29
+ }
30
+ if (pipeline && exitCode !== null && (!Number.isInteger(exitCode) || exitCode < 0)) {
31
+ issues.push('invalid final-runner exit code');
32
+ verification = 'reported';
33
+ }
34
+ const derivedFrom = Object.freeze([...(input.derivedFrom ?? [])]);
35
+ for (const id of derivedFrom)
36
+ if (!this.records.has(id))
37
+ issues.push(`unknown source evidence: ${id}`);
38
+ const id = `evidence-${String(this.nextId++).padStart(6, '0')}`;
39
+ const record = Object.freeze({
40
+ id, source: input.source, raw: input.raw, verification, revision: input.revision, cwd: input.cwd,
41
+ exitCode, pipeline, finalRunnerProven, artifactRef, artifactHash, derivedFrom,
42
+ outcome: input.outcome ?? inferOutcome(input.raw), issues: Object.freeze(issues),
43
+ });
44
+ this.records.set(id, record);
45
+ return record;
46
+ }
47
+ get(id) { return this.records.get(id); }
48
+ list() { return Object.freeze([...this.records.values()]); }
49
+ }
50
+ export function inferOutcome(raw) {
51
+ if (/\b(fail(?:ed|ure)?|error|not ok)\b/i.test(raw))
52
+ return 'fail';
53
+ if (/\b(pass(?:ed)?|success(?:ful)?)\b/i.test(raw))
54
+ return 'pass';
55
+ return 'unknown';
56
+ }
57
+ //# sourceMappingURL=store.js.map
@@ -0,0 +1,27 @@
1
+ /** host: default 127.0.0.1 (this machine only). Any other value (e.g. 0.0.0.0) is an explicit opt-in to LAN access; the token is still required. */
2
+ export interface GuiServerOptions {
3
+ projectRoot: string;
4
+ port?: number;
5
+ host?: string;
6
+ open?: boolean; /** Fixed access token (owner-chosen, >= 16 chars); default is a fresh random token per start. */
7
+ token?: string;
8
+ }
9
+ export interface GuiServer {
10
+ url: string;
11
+ close(): Promise<void>;
12
+ }
13
+ export declare function openBrowser(url: string, platform?: NodeJS.Platform): void;
14
+ export declare function startGuiServer(opts: GuiServerOptions): Promise<GuiServer>;
15
+ /** Folder the GUI works in: the current folder itself (symlinks resolved), never a parent. */
16
+ export declare function guiWorkspace(cwd: string): string;
17
+ export interface GuiSettings {
18
+ host: string;
19
+ port: number;
20
+ explicitPort: boolean;
21
+ open: boolean;
22
+ token?: string;
23
+ tokenIgnored: boolean;
24
+ }
25
+ /** Where `ckit gui` listens. Order: flags > shell environment > project env file > .ckit/gui.json > default (this machine only). */
26
+ export declare function resolveGuiSettings(root: string, shellEnv: Readonly<Record<string, string | undefined>>, args: readonly string[], outerRoot?: string): GuiSettings;
27
+ export declare function runGuiCommand(args: string[]): Promise<number>;