@popovych.co/witness 0.28.2 → 0.29.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 (207) hide show
  1. package/README.md +15 -3
  2. package/dist/abandon.d.ts +67 -0
  3. package/dist/act.d.ts +17 -0
  4. package/dist/act.js +70 -0
  5. package/dist/adopt.d.ts +21 -0
  6. package/dist/allowlist.d.ts +48 -0
  7. package/dist/allowlist.js +79 -5
  8. package/dist/arbiter.d.ts +37 -0
  9. package/dist/arbiter.js +7 -5
  10. package/dist/ask.d.ts +29 -0
  11. package/dist/ask.js +11 -5
  12. package/dist/autonomy.d.ts +46 -0
  13. package/dist/autonomy.js +24 -12
  14. package/dist/awake.d.ts +15 -0
  15. package/dist/basis.d.ts +9 -0
  16. package/dist/battery.d.ts +23 -0
  17. package/dist/bin.d.ts +2 -0
  18. package/dist/busy.d.ts +38 -0
  19. package/dist/busy.js +274 -0
  20. package/dist/caps.d.ts +21 -0
  21. package/dist/caps.js +24 -3
  22. package/dist/cli.d.ts +18 -0
  23. package/dist/config-file.d.ts +12 -0
  24. package/dist/config.d.ts +96 -0
  25. package/dist/config.js +2 -1
  26. package/dist/contain.d.ts +16 -0
  27. package/dist/context.d.ts +25 -0
  28. package/dist/criteria.d.ts +27 -0
  29. package/dist/criteria.js +2 -1
  30. package/dist/decide-view.d.ts +52 -0
  31. package/dist/decide-view.js +155 -0
  32. package/dist/decisions.d.ts +23 -0
  33. package/dist/deferral.d.ts +26 -0
  34. package/dist/delta.d.ts +58 -0
  35. package/dist/departure.d.ts +66 -0
  36. package/dist/design.d.ts +34 -0
  37. package/dist/drafts.d.ts +31 -0
  38. package/dist/drafts.js +5 -3
  39. package/dist/drift.d.ts +23 -0
  40. package/dist/drive-presence.d.ts +30 -0
  41. package/dist/drive-presence.js +29 -6
  42. package/dist/drive-report.d.ts +24 -0
  43. package/dist/drive-report.js +4 -4
  44. package/dist/drive.d.ts +114 -0
  45. package/dist/drive.js +328 -115
  46. package/dist/dsl.d.ts +23 -0
  47. package/dist/dsl.js +8 -3
  48. package/dist/evidence.d.ts +67 -0
  49. package/dist/evidence.js +8 -2
  50. package/dist/floor.d.ts +6 -0
  51. package/dist/fm.d.ts +9 -0
  52. package/dist/gate-run-view.d.ts +57 -0
  53. package/dist/gate-run-view.js +100 -0
  54. package/dist/gate.d.ts +91 -0
  55. package/dist/gate.js +51 -71
  56. package/dist/gates/decompose.d.ts +1 -0
  57. package/dist/gates/decompose.js +3 -1
  58. package/dist/gates/design.d.ts +1 -0
  59. package/dist/gates/design.js +6 -2
  60. package/dist/gates/implement.d.ts +1 -0
  61. package/dist/gates/implement.js +20 -5
  62. package/dist/gates/index.d.ts +5 -0
  63. package/dist/gates/plan.d.ts +1 -0
  64. package/dist/gates/plan.js +10 -4
  65. package/dist/gates/ship.d.ts +1 -0
  66. package/dist/gates/ship.js +13 -6
  67. package/dist/gitio.d.ts +35 -0
  68. package/dist/gitio.js +31 -3
  69. package/dist/glob.d.ts +2 -0
  70. package/dist/harness.d.ts +73 -0
  71. package/dist/harness.js +36 -15
  72. package/dist/history.d.ts +10 -0
  73. package/dist/idlists.d.ts +2 -0
  74. package/dist/install.d.ts +29 -0
  75. package/dist/journal.d.ts +74 -0
  76. package/dist/journal.js +5 -1
  77. package/dist/junit.d.ts +9 -0
  78. package/dist/live.d.ts +29 -0
  79. package/dist/live.js +15 -3
  80. package/dist/lock.d.ts +9 -0
  81. package/dist/matcher.d.ts +8 -0
  82. package/dist/migration.d.ts +25 -0
  83. package/dist/model.d.ts +8 -0
  84. package/dist/models.d.ts +9 -0
  85. package/dist/models.js +10 -0
  86. package/dist/needs-you.d.ts +91 -0
  87. package/dist/needs-you.js +153 -51
  88. package/dist/needs.d.ts +44 -0
  89. package/dist/needs.js +37 -7
  90. package/dist/notify.d.ts +12 -0
  91. package/dist/opener.d.ts +15 -0
  92. package/dist/pin.d.ts +12 -0
  93. package/dist/pin.js +9 -2
  94. package/dist/principles.d.ts +3 -0
  95. package/dist/probe.d.ts +1 -0
  96. package/dist/progress.d.ts +3 -0
  97. package/dist/progress.js +2 -0
  98. package/dist/recommend.d.ts +31 -0
  99. package/dist/recommend.js +51 -42
  100. package/dist/refusal.d.ts +20 -0
  101. package/dist/refusal.js +12 -4
  102. package/dist/registry.d.ts +1 -0
  103. package/dist/released-comments.d.ts +1 -0
  104. package/dist/review.d.ts +27 -0
  105. package/dist/reviewed.d.ts +27 -0
  106. package/dist/reviewed.js +9 -0
  107. package/dist/reviewer.d.ts +47 -0
  108. package/dist/rounds.d.ts +139 -0
  109. package/dist/rounds.js +75 -25
  110. package/dist/runner-lock.d.ts +5 -0
  111. package/dist/runner.d.ts +30 -0
  112. package/dist/scan.d.ts +27 -0
  113. package/dist/schemas.d.ts +11 -0
  114. package/dist/schemas.js +7 -3
  115. package/dist/session-clock.d.ts +16 -0
  116. package/dist/settings.d.ts +73 -0
  117. package/dist/settings.js +12 -2
  118. package/dist/settle.d.ts +62 -0
  119. package/dist/settle.js +177 -39
  120. package/dist/sha.d.ts +5 -0
  121. package/dist/ship-outcome.d.ts +24 -0
  122. package/dist/ship-outcome.js +76 -0
  123. package/dist/ship.d.ts +31 -0
  124. package/dist/ship.js +324 -82
  125. package/dist/slice.d.ts +45 -0
  126. package/dist/slice.js +2 -2
  127. package/dist/snapshot.d.ts +10 -0
  128. package/dist/spawn.d.ts +22 -0
  129. package/dist/stamp.d.ts +33 -0
  130. package/dist/stamp.js +20 -8
  131. package/dist/steps.d.ts +31 -0
  132. package/dist/steps.js +39 -10
  133. package/dist/toon.d.ts +17 -0
  134. package/dist/toon.js +150 -14
  135. package/dist/txn.d.ts +28 -0
  136. package/dist/usage.d.ts +25 -0
  137. package/dist/verbs/abandon.d.ts +2 -0
  138. package/dist/verbs/adopt.d.ts +2 -0
  139. package/dist/verbs/adopt.js +9 -4
  140. package/dist/verbs/ask.d.ts +2 -0
  141. package/dist/verbs/check.d.ts +2 -0
  142. package/dist/verbs/check.js +29 -10
  143. package/dist/verbs/clean.d.ts +2 -0
  144. package/dist/verbs/dashboard.d.ts +59 -0
  145. package/dist/verbs/dashboard.js +11 -3
  146. package/dist/verbs/decide.d.ts +46 -0
  147. package/dist/verbs/decide.js +168 -149
  148. package/dist/verbs/design.d.ts +2 -0
  149. package/dist/verbs/design.js +8 -5
  150. package/dist/verbs/diff.d.ts +2 -0
  151. package/dist/verbs/diff.js +4 -2
  152. package/dist/verbs/dismiss.d.ts +2 -0
  153. package/dist/verbs/dispatch.d.ts +2 -0
  154. package/dist/verbs/dispatch.js +2 -2
  155. package/dist/verbs/draft.d.ts +2 -0
  156. package/dist/verbs/draft.js +6 -3
  157. package/dist/verbs/drive.d.ts +2 -0
  158. package/dist/verbs/evidence.d.ts +5 -0
  159. package/dist/verbs/evidence.js +40 -3
  160. package/dist/verbs/floor.d.ts +2 -0
  161. package/dist/verbs/gate.d.ts +3 -0
  162. package/dist/verbs/index-verb.d.ts +2 -0
  163. package/dist/verbs/init.d.ts +2 -0
  164. package/dist/verbs/log.d.ts +9 -0
  165. package/dist/verbs/migrate.d.ts +13 -0
  166. package/dist/verbs/next.d.ts +78 -0
  167. package/dist/verbs/next.js +373 -131
  168. package/dist/verbs/read.d.ts +2 -0
  169. package/dist/verbs/read.js +3 -1
  170. package/dist/verbs/recap.d.ts +28 -0
  171. package/dist/verbs/recap.js +3 -3
  172. package/dist/verbs/recover.d.ts +5 -0
  173. package/dist/verbs/recover.js +14 -9
  174. package/dist/verbs/rename.d.ts +2 -0
  175. package/dist/verbs/rename.js +1 -1
  176. package/dist/verbs/retry.d.ts +2 -0
  177. package/dist/verbs/review.d.ts +2 -0
  178. package/dist/verbs/review.js +1 -1
  179. package/dist/verbs/satisfy.d.ts +2 -0
  180. package/dist/verbs/ship.d.ts +3 -0
  181. package/dist/verbs/start.d.ts +2 -0
  182. package/dist/verbs/start.js +12 -7
  183. package/dist/verbs/step-close.d.ts +2 -0
  184. package/dist/verbs/sync.d.ts +15 -0
  185. package/dist/verbs/trust.d.ts +4 -0
  186. package/dist/verbs/trust.js +34 -32
  187. package/dist/verbs/turn.d.ts +2 -0
  188. package/dist/verbs/wait.d.ts +9 -0
  189. package/dist/verbs/wait.js +33 -8
  190. package/dist/verbs/write.d.ts +18 -0
  191. package/dist/verbs/write.js +11 -5
  192. package/dist/verdict.d.ts +60 -0
  193. package/dist/version.d.ts +4 -0
  194. package/dist/worker-stream.d.ts +22 -0
  195. package/dist/worktree.d.ts +22 -0
  196. package/dist/worktree.js +1 -1
  197. package/package.json +7 -1
  198. package/plugin/.claude-plugin/plugin.json +1 -1
  199. package/plugin/commands/witness.md +17 -11
  200. package/plugin/hooks/canon-guard.mjs +71 -10
  201. package/plugin/hooks/session-dashboard.sh +1 -1
  202. package/plugin/skills/witness-brainstorm/SKILL.md +3 -3
  203. package/plugin/skills/witness-decompose/SKILL.md +3 -3
  204. package/plugin/skills/witness-design/SKILL.md +2 -2
  205. package/plugin/skills/witness-implement/SKILL.md +5 -5
  206. package/plugin/skills/witness-plan/SKILL.md +3 -3
  207. package/plugin/skills/witness-ship/SKILL.md +3 -3
@@ -0,0 +1,15 @@
1
+ export declare const AWAKE_SCRIPT: string;
2
+ export declare const CTRL_C_EXIT = 3221225786;
3
+ export interface AwakeArgv {
4
+ file: string;
5
+ args: string[];
6
+ env?: Record<string, string>;
7
+ shown: string;
8
+ }
9
+ export declare function awakeArgv(env: Record<string, string | undefined>, platform: NodeJS.Platform, pid: number): AwakeArgv | undefined;
10
+ export declare function lostWhy(platform: NodeJS.Platform, file: string, code: number | null, signal: NodeJS.Signals | null, said?: string): string | undefined;
11
+ export interface Awake {
12
+ shown?: string;
13
+ stop(): void;
14
+ }
15
+ export declare function keepAwake(env: Record<string, string | undefined>, platform: NodeJS.Platform, pid: number, onLost?: (why: string) => void): Awake;
@@ -0,0 +1,9 @@
1
+ import type { CanonDoc } from './scan.js';
2
+ export interface Basis {
3
+ spec: string;
4
+ decisions?: string;
5
+ }
6
+ export declare function specBasis(root: string, doc: CanonDoc): Basis;
7
+ export declare function planBasis(plan: CanonDoc): Basis;
8
+ export declare function basisMoved(pinned: Basis, current: Basis): Array<'spec' | 'decisions'>;
9
+ export declare function planDecisionsTerm(root: string, plan: CanonDoc, parent: CanonDoc): string | undefined;
@@ -0,0 +1,23 @@
1
+ import type { Ctx } from './cli.js';
2
+ import type { Harness } from './harness.js';
3
+ import { type Result, type Violation } from './refusal.js';
4
+ import { type InvokeExtras } from './reviewer.js';
5
+ import type { ReviewerVerdict } from './rounds.js';
6
+ import { type Reviewed } from './verdict.js';
7
+ export interface BatteryLens {
8
+ name: string;
9
+ prompt: string;
10
+ reviewed: Reviewed;
11
+ }
12
+ export interface BatteryRun {
13
+ verdicts: ReviewerVerdict[];
14
+ malformed: Array<{
15
+ reviewer: string;
16
+ violations: Violation[];
17
+ }>;
18
+ rerolled: string[];
19
+ }
20
+ export declare function runBattery(ctx: Ctx, harness: Harness, model: string, lenses: readonly BatteryLens[], opts: {
21
+ cwd: string;
22
+ pinField?: string;
23
+ } & InvokeExtras): Promise<Result<BatteryRun>>;
package/dist/bin.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/dist/busy.d.ts ADDED
@@ -0,0 +1,38 @@
1
+ import type { Ctx } from './cli.js';
2
+ import type { Config } from './config.js';
3
+ import { type Violation } from './refusal.js';
4
+ export type BusyKind = 'settle' | 'ship';
5
+ export interface BusyLock {
6
+ pid: number;
7
+ since: number;
8
+ until: number;
9
+ }
10
+ export declare const BUSY_DIR = ".witness/live/busy";
11
+ export declare const BUSY_MAX_MS = 86400000;
12
+ export declare const busyIo: {
13
+ create(p: string, text: string): void;
14
+ unlink(p: string): void;
15
+ };
16
+ export type TakeBusy = {
17
+ taken: true;
18
+ release: () => void;
19
+ } | {
20
+ taken: false;
21
+ holder: BusyLock;
22
+ } | {
23
+ taken: false;
24
+ refused: Violation[];
25
+ };
26
+ export declare function takeBusy(root: string, kind: BusyKind, target: string, until: number, opts?: {
27
+ now?: number;
28
+ pid?: number;
29
+ }): TakeBusy;
30
+ export declare function busyHolder(root: string, kind: BusyKind, target: string, opts?: {
31
+ now?: number;
32
+ pid?: number;
33
+ }): BusyLock | undefined;
34
+ export declare function sweepDeadBusy(root: string, opts?: {
35
+ pid?: number;
36
+ }): number;
37
+ export declare function settleUntil(cfg: Config, env: Ctx['env'], now: number): number;
38
+ export declare function shipUntil(cfg: Config, env: Ctx['env'], now: number): number;
package/dist/busy.js ADDED
@@ -0,0 +1,274 @@
1
+ import { closeSync, constants, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, unlinkSync, writeSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { DRIVE_DEADLINE_ENV } from './drive-presence.js';
4
+ import { isId } from './dsl.js';
5
+ import { LOCK_WAIT_MS, isAlive, pause, takeStale } from './lock.js';
6
+ import { v } from './refusal.js';
7
+ import { ensureExcluded } from './worktree.js';
8
+ export const BUSY_DIR = '.witness/live/busy';
9
+ // A lock whose `until` lies more than this after its `since` is stale, whatever it claims.
10
+ export const BUSY_MAX_MS = 86_400_000;
11
+ // An empty or unparseable file is a write in progress for this long (`lock.ts`'s grace).
12
+ const UNREADABLE_GRACE_MS = 1_000;
13
+ // Windows: a scanner or the indexer can hold a fresh file for a moment (EPERM, EBUSY, EACCES).
14
+ const CREATE_RETRY_MS = 1_000;
15
+ const RETRY_PAUSE_MS = 50;
16
+ const TRANSIENT = new Set(['EPERM', 'EBUSY', 'EACCES']);
17
+ // A `since` this far ahead of now is a tampered lock or a clock set wrong, never a live holder.
18
+ const SINCE_SLACK_MS = 60_000;
19
+ // The one seam for file errors, so a test can stand in for a Windows scanner.
20
+ export const busyIo = {
21
+ create(p, text) {
22
+ const fd = openSync(p, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o644);
23
+ try {
24
+ writeSync(fd, text);
25
+ }
26
+ finally {
27
+ closeSync(fd);
28
+ }
29
+ },
30
+ unlink(p) {
31
+ unlinkSync(p);
32
+ },
33
+ };
34
+ const unwritable = (why) => v(BUSY_DIR, 'lock-unwritable', why, 'a writable .witness/live/busy folder, not a link — witness writes its busy locks there');
35
+ // The folder, or why it cannot be used. A link anywhere on the way is refused, never followed.
36
+ function busyDir(root, create) {
37
+ const live = join(root, '.witness', 'live');
38
+ const dir = join(root, BUSY_DIR);
39
+ for (const p of [live, dir]) {
40
+ try {
41
+ if (!lstatSync(p).isDirectory())
42
+ return { refused: unwritable(`${p === live ? '.witness/live' : BUSY_DIR} is not a folder`) };
43
+ }
44
+ catch (e) {
45
+ if (e.code !== 'ENOENT')
46
+ return { refused: unwritable(e.code ?? String(e)) };
47
+ if (!create)
48
+ return undefined;
49
+ try {
50
+ mkdirSync(p, { recursive: true });
51
+ }
52
+ catch (err) {
53
+ return { refused: unwritable(err.code ?? String(err)) };
54
+ }
55
+ }
56
+ }
57
+ return dir;
58
+ }
59
+ const lockName = (kind, target) => `${target}.${kind}.json`;
60
+ function parseLock(text) {
61
+ let raw;
62
+ try {
63
+ raw = JSON.parse(text);
64
+ }
65
+ catch {
66
+ return 'unreadable';
67
+ }
68
+ const r = raw;
69
+ const num = (x) => typeof x === 'number' && Number.isFinite(x) && x >= 0;
70
+ if (!(Number.isInteger(r?.pid) && r.pid > 0 && num(r.since) && num(r.until)))
71
+ return 'malformed';
72
+ return { pid: r.pid, since: r.since, until: r.until };
73
+ }
74
+ function readLock(p) {
75
+ try {
76
+ if (lstatSync(p).isSymbolicLink())
77
+ return 'link';
78
+ const text = readFileSync(p, 'utf8');
79
+ const parsed = parseLock(text);
80
+ return typeof parsed === 'string' ? parsed : { lock: parsed };
81
+ }
82
+ catch (e) {
83
+ return e.code === 'ENOENT' ? 'absent' : 'unreadable';
84
+ }
85
+ }
86
+ // Live: its pid is alive, its `until` has not passed, and it claims no more than the cap.
87
+ const live = (l, now) => l.until >= l.since && l.until - l.since <= BUSY_MAX_MS && l.since <= now + SINCE_SLACK_MS && now <= l.until && isAlive(l.pid);
88
+ const olderThanGrace = (p, now) => {
89
+ try {
90
+ return now - lstatSync(p).mtimeMs >= UNREADABLE_GRACE_MS;
91
+ }
92
+ catch {
93
+ return true;
94
+ }
95
+ };
96
+ // Stale to `pid`: not live, or its own (no lock nests: a lock naming the reader is never busy to it).
97
+ function stale(p, text, pid, now) {
98
+ if (text === undefined) {
99
+ try {
100
+ if (lstatSync(p).isSymbolicLink())
101
+ return true;
102
+ }
103
+ catch {
104
+ return true;
105
+ }
106
+ return olderThanGrace(p, now);
107
+ }
108
+ const parsed = parseLock(text);
109
+ if (parsed === 'unreadable')
110
+ return olderThanGrace(p, now);
111
+ if (parsed === 'malformed')
112
+ return true;
113
+ return parsed.pid === pid || !live(parsed, now);
114
+ }
115
+ // Takes `kind`'s lock on `target` until `until`, or names the live holder. Never waits on a
116
+ // holder: the only pauses are for a write in progress and a scanner holding a fresh file.
117
+ export function takeBusy(root, kind, target, until, opts = {}) {
118
+ if (!isId(target))
119
+ return { taken: false, refused: [v('target', 'unknown-id', target, 'an artifact id — lowercase letters, digits and dashes')] };
120
+ const pid = opts.pid ?? process.pid;
121
+ const now = () => opts.now ?? Date.now();
122
+ // Spec c2: a repo whose ignore rules predate this folder must never show it. Best effort.
123
+ try {
124
+ ensureExcluded(root);
125
+ }
126
+ catch { /* not a git checkout we can write to */ }
127
+ const dir = busyDir(root, true);
128
+ if (dir === undefined)
129
+ return { taken: false, refused: [unwritable('the folder could not be made')] };
130
+ if (typeof dir !== 'string')
131
+ return { taken: false, refused: [dir.refused] };
132
+ const p = join(dir, lockName(kind, target));
133
+ const since = now();
134
+ const text = JSON.stringify({ pid, since, until });
135
+ const t0 = Date.now();
136
+ for (;;) {
137
+ try {
138
+ busyIo.create(p, text);
139
+ return { taken: true, release: () => releaseIf(p, text) };
140
+ }
141
+ catch (e) {
142
+ const code = e.code;
143
+ if (code !== 'EEXIST') {
144
+ if (code !== undefined && TRANSIENT.has(code) && Date.now() - t0 < CREATE_RETRY_MS) {
145
+ pause(RETRY_PAUSE_MS);
146
+ continue;
147
+ }
148
+ return { taken: false, refused: [unwritable(code ?? String(e))] };
149
+ }
150
+ }
151
+ const held = readLock(p);
152
+ if (typeof held === 'object' && held.lock.pid !== pid && live(held.lock, now()))
153
+ return { taken: false, holder: held.lock };
154
+ let took;
155
+ try {
156
+ took = takeStale(p, (t) => stale(p, t, pid, now()));
157
+ }
158
+ catch (e) {
159
+ return { taken: false, refused: [unwritable(`the stale lock could not be removed (${e.code ?? String(e)})`)] };
160
+ }
161
+ if (took === 'taken')
162
+ continue;
163
+ // a write in progress, or another stealer at work: re-read shortly, within the same budget
164
+ if (Date.now() - t0 >= CREATE_RETRY_MS + UNREADABLE_GRACE_MS) {
165
+ const again = readLock(p);
166
+ if (typeof again === 'object')
167
+ return { taken: false, holder: again.lock };
168
+ return { taken: false, refused: [unwritable('the lock is being written by another process')] };
169
+ }
170
+ pause(RETRY_PAUSE_MS);
171
+ }
172
+ }
173
+ // A lock is released by its taker only, and only while it still holds what the taker wrote.
174
+ // Windows: a scanner holding the file a moment fails the unlink (EBUSY, EPERM); it is retried within
175
+ // a second, as a create is, so a released lock does not read live to others until its `until`.
176
+ function releaseIf(p, text) {
177
+ const t0 = Date.now();
178
+ for (;;) {
179
+ try {
180
+ if (lstatSync(p).isSymbolicLink())
181
+ return;
182
+ if (readFileSync(p, 'utf8') !== text)
183
+ return;
184
+ busyIo.unlink(p);
185
+ return;
186
+ }
187
+ catch (e) {
188
+ const code = e.code;
189
+ // gone already; an unlink that keeps failing is left to the dead-pid rule
190
+ if (code === undefined || !TRANSIENT.has(code) || Date.now() - t0 >= CREATE_RETRY_MS)
191
+ return;
192
+ pause(RETRY_PAUSE_MS);
193
+ }
194
+ }
195
+ }
196
+ // The live holder of `kind`'s lock on `target`, when it is a process other than `opts.pid`.
197
+ export function busyHolder(root, kind, target, opts = {}) {
198
+ if (!isId(target))
199
+ return undefined;
200
+ const dir = busyDir(root, false);
201
+ if (typeof dir !== 'string')
202
+ return undefined;
203
+ const held = readLock(join(dir, lockName(kind, target)));
204
+ if (typeof held !== 'object')
205
+ return undefined;
206
+ const pid = opts.pid ?? process.pid;
207
+ return held.lock.pid !== pid && live(held.lock, opts.now ?? Date.now()) ? held.lock : undefined;
208
+ }
209
+ // Drive's cleanup (`releaseDeadTurn`'s pattern, row 218): the locks of dead pids, and the
210
+ // caller's own leftovers, before Windows can hand a dead pid to another process. The count.
211
+ export function sweepDeadBusy(root, opts = {}) {
212
+ const dir = busyDir(root, false);
213
+ if (typeof dir !== 'string')
214
+ return 0;
215
+ const pid = opts.pid ?? process.pid;
216
+ let n = 0;
217
+ let names;
218
+ try {
219
+ names = readdirSync(dir);
220
+ }
221
+ catch {
222
+ return 0;
223
+ }
224
+ for (const name of names) {
225
+ if (!/\.(settle|ship)\.json$/.test(name))
226
+ continue;
227
+ const p = join(dir, name);
228
+ // Asked again under the steal guard, so a live lock written meanwhile is never removed. A
229
+ // write in progress (an empty file) is not dropped: its taker is mid-create.
230
+ const droppable = (t) => {
231
+ if (t === undefined) {
232
+ try {
233
+ return lstatSync(p).isSymbolicLink();
234
+ }
235
+ catch {
236
+ return false;
237
+ }
238
+ }
239
+ const l = parseLock(t);
240
+ return l === 'malformed' || (typeof l === 'object' && (l.pid === pid || !isAlive(l.pid) || !live(l, Date.now())));
241
+ };
242
+ const text = readLock(p) === 'link' ? undefined : (() => { try {
243
+ return readFileSync(p, 'utf8');
244
+ }
245
+ catch {
246
+ return '';
247
+ } })();
248
+ if (!droppable(text))
249
+ continue;
250
+ try {
251
+ if (takeStale(p, droppable) === 'taken')
252
+ n++;
253
+ }
254
+ catch { /* left to the next sweep */ }
255
+ }
256
+ return n;
257
+ }
258
+ // The bound a taker writes: the estimate, or inside a drive session the session's deadline when
259
+ // later — a reviewer there may wait out a usage limit until then (reviewer.ts) — and never more
260
+ // than the cap after now. The cap goes last (pass 6, P20), so no bound is stale at birth.
261
+ function bound(estimateMs, env, now) {
262
+ const deadline = Number(env[DRIVE_DEADLINE_ENV]) || 0;
263
+ return Math.min(Math.max(now + estimateMs, deadline), now + BUSY_MAX_MS);
264
+ }
265
+ // A self-repair reroll is a second arbitration, each of up to three timed attempts, then the
266
+ // door's lock wait — twice when `parkRefused` follows — and two minutes of slack.
267
+ const settleEstimate = (cfg) => 2 * 3 * cfg.gates.reviewerTimeoutMs + 2 * LOCK_WAIT_MS + 120_000;
268
+ export function settleUntil(cfg, env, now) {
269
+ return bound(settleEstimate(cfg), env, now);
270
+ }
271
+ // The ship gate's battery, the CI watch, both settles, and ten minutes for git and gh.
272
+ export function shipUntil(cfg, env, now) {
273
+ return bound(3 * cfg.gates.reviewerTimeoutMs + cfg.ship.ciSettleMs + 1_800_000 + 2 * settleEstimate(cfg) + 600_000, env, now);
274
+ }
package/dist/caps.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ import { type Ctx } from './cli.js';
2
+ import type { Config, DriveCaps } from './config.js';
3
+ import type { Entry } from './journal.js';
4
+ export type CapName = 'rounds' | 'upstream' | 'spawns' | 'wall-clock';
5
+ export interface TargetUse {
6
+ spawns: number;
7
+ ms: number;
8
+ }
9
+ export interface CapHit {
10
+ cap: CapName;
11
+ gate?: string;
12
+ note: string;
13
+ }
14
+ export declare function capHit(entries: Entry[], use: TargetUse, caps: DriveCaps): CapHit | undefined;
15
+ export declare function parkAtCap(ctx: Ctx, root: string, target: string, hit: CapHit, cfg: Config): Promise<{
16
+ parked: true;
17
+ } | {
18
+ parked: false;
19
+ why: string;
20
+ busy?: true;
21
+ }>;
package/dist/caps.js CHANGED
@@ -1,5 +1,6 @@
1
+ import { settleUntil, takeBusy } from './busy.js';
1
2
  import { EXIT } from './cli.js';
2
- import { fellBack, gateRuns, lastGateRun } from './rounds.js';
3
+ import { fellBack, gateRuns, lastGateRun, settledToken, unparkAt } from './rounds.js';
3
4
  import { duration } from './usage.js';
4
5
  import { gateSettled } from './verbs/next.js';
5
6
  const isCapPark = (e) => {
@@ -17,8 +18,9 @@ function openGates(entries) {
17
18
  }
18
19
  // First hit wins, in this order: rounds · upstream · spawns · wall clock.
19
20
  export function capHit(entries, use, caps) {
21
+ // Row 241 (R11): a human un-park restarts the window, as a cap park does.
20
22
  let lastPark = -1;
21
- entries.forEach((e, i) => { if (isCapPark(e))
23
+ entries.forEach((e, i) => { if (isCapPark(e) || unparkAt(entries, i))
22
24
  lastPark = i; });
23
25
  const since = entries.slice(lastPark + 1);
24
26
  // Every budget reset is ignored: this counts the rounds a gate ran, not its window. The
@@ -50,7 +52,23 @@ export function capHit(entries, use, caps) {
50
52
  // The park goes through A1's one door with `selected: 'policy'` and the cap as its rule.
51
53
  // `park` anchors it on the gate's last run even after the author moved the tree. A target
52
54
  // with no open gate has nothing to park on; a refusal from the door is named, not guessed.
53
- export async function parkAtCap(ctx, root, target, hit) {
55
+ export async function parkAtCap(ctx, root, target, hit, cfg) {
56
+ // Row 240 (design §4.1.2): under the stop's settle lock, taken before the stream is read. Held
57
+ // by another live process, nothing parks and drive tries the target again next round.
58
+ const lock = takeBusy(root, 'settle', target, settleUntil(cfg, ctx.env, Date.now()));
59
+ if (!lock.taken) {
60
+ if ('holder' in lock)
61
+ return { parked: false, busy: true, why: 'busy' };
62
+ return { parked: false, why: `the policy could not park it (${lock.refused[0]?.rule ?? 'lock-unwritable'})` };
63
+ }
64
+ try {
65
+ return await parkUnderLock(ctx, root, target, hit);
66
+ }
67
+ finally {
68
+ lock.release();
69
+ }
70
+ }
71
+ async function parkUnderLock(ctx, root, target, hit) {
54
72
  const { readStream } = await import('./journal.js');
55
73
  const entries = readStream(root, target);
56
74
  if (!entries.some((e) => e.t === 'gate-run'))
@@ -63,8 +81,11 @@ export async function parkAtCap(ctx, root, target, hit) {
63
81
  // Loaded on A1's precedent for settle.ts: decide.ts reaches the gate registry.
64
82
  const { applyDecision } = await import('./verbs/decide.js');
65
83
  const errs = [];
84
+ // Row 240 (R3): the token of this read, so a park read before another decision on the gate —
85
+ // a human's revise-upstream, a settler's — refuses `settled-since` instead of parking on top (P7).
66
86
  const code = await applyDecision({ ...ctx, cwd: root, err: (l) => { errs.push(l); ctx.err(l); } }, {
67
87
  gate, target, decision: 'stop', note: hit.note, selected: 'policy', rule: `cap-${hit.cap}`, park: true, runId: last.run_id,
88
+ token: settledToken(entries, gate, last.run_id),
68
89
  });
69
90
  if (code === EXIT.OK)
70
91
  return { parked: true };
package/dist/cli.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { version } from './version.js';
2
+ export interface Ctx {
3
+ cwd: string;
4
+ env: Record<string, string | undefined>;
5
+ isTTY: boolean;
6
+ out: (line: string) => void;
7
+ err: (line: string) => void;
8
+ ask: (question: string) => Promise<string>;
9
+ }
10
+ export declare const EXIT: {
11
+ readonly OK: 0;
12
+ readonly FINDINGS: 1;
13
+ readonly REFUSED: 2;
14
+ readonly BLOCKED: 3;
15
+ };
16
+ export { version };
17
+ export declare function usage(): string;
18
+ export declare function main(ctx: Ctx, argv: string[]): Promise<number>;
@@ -0,0 +1,12 @@
1
+ export interface HumanComments {
2
+ top: string[];
3
+ above: Map<string, string[]>;
4
+ end: string[];
5
+ }
6
+ export { inlineValue } from './settings.js';
7
+ export declare function renderConfig(values: Record<string, unknown>, comments?: HumanComments): string;
8
+ export declare function readComments(text: string): {
9
+ human: HumanComments;
10
+ legacy: number;
11
+ moved: number;
12
+ };
@@ -0,0 +1,96 @@
1
+ import { type Result, type Violation } from './refusal.js';
2
+ import { type Battery, type GateName, type Setting } from './settings.js';
3
+ export declare const SCHEMA_VERSION = 2;
4
+ export interface CanonPaths {
5
+ specs: string;
6
+ plans: string;
7
+ designs: string;
8
+ }
9
+ export declare const DEFAULT_PATHS: CanonPaths;
10
+ export declare const DOC_KEYS: readonly ["conventions", "design"];
11
+ export type DocKey = (typeof DOC_KEYS)[number];
12
+ export type DocsRegistry = Partial<Record<DocKey, string[]>>;
13
+ export declare const DEFAULT_STEPS_PER_DISPATCH: number;
14
+ export interface ImplementConfig {
15
+ stepsPerDispatch: number;
16
+ }
17
+ export declare const DEFAULT_CONTEXT_LIMIT: number;
18
+ export interface SessionConfig {
19
+ contextLimit: number;
20
+ }
21
+ export declare const DEFAULT_SESSION_TIMEOUT_MS: number;
22
+ export declare const DEFAULT_PARALLEL: number;
23
+ export interface DriveCaps {
24
+ rounds: number;
25
+ upstream: number;
26
+ spawns: number;
27
+ wallClockMs: number;
28
+ }
29
+ export declare const DEFAULT_CAPS: DriveCaps;
30
+ export interface DriveConfig {
31
+ sessionTimeoutMs: number;
32
+ parallel: number;
33
+ parallelSuites: boolean;
34
+ caps: DriveCaps;
35
+ }
36
+ export declare const DEFAULT_REVIEWER_TIMEOUT_MS: number;
37
+ export interface GateSettings {
38
+ model?: string;
39
+ reviewers: Battery;
40
+ }
41
+ export type GatesConfig = {
42
+ reviewerTimeoutMs: number;
43
+ } & Record<GateName, GateSettings>;
44
+ export interface CriteriaConfig {
45
+ runner?: string;
46
+ report?: string;
47
+ exclude: string[];
48
+ }
49
+ export type MergeStyle = 'merge' | 'squash' | 'rebase';
50
+ export interface ShipConfig {
51
+ test?: string;
52
+ lint?: string;
53
+ branch: string;
54
+ merge: MergeStyle;
55
+ ciSettleMs: number;
56
+ }
57
+ export declare const DEFAULT_CI_SETTLE_MS: number;
58
+ export declare const MAX_CI_SETTLE_MS = 1800000;
59
+ export interface Config {
60
+ schema: number;
61
+ paths: CanonPaths;
62
+ docs: DocsRegistry;
63
+ criteria: CriteriaConfig;
64
+ ship: ShipConfig;
65
+ harness?: string;
66
+ autonomy: boolean;
67
+ arbiter: {
68
+ model?: string;
69
+ };
70
+ session: SessionConfig;
71
+ implement: ImplementConfig;
72
+ gates: GatesConfig;
73
+ drive: DriveConfig;
74
+ warning?: string;
75
+ }
76
+ export type Resolved = Omit<Config, 'schema' | 'warning'>;
77
+ type Raw = Record<string, unknown>;
78
+ export declare function checkValue(s: Setting, val: unknown): Result<unknown>;
79
+ export declare function suggestionFor(path: string, known: ReadonlySet<string>): string | undefined;
80
+ export declare function unknownKey(path: string, known: ReadonlySet<string>): Violation;
81
+ export declare function keyViolations(raw: Raw, schema: number): Violation[];
82
+ export declare function resolveSettings(raw: Raw, mode: 'strict' | 'lenient'): Result<Resolved>;
83
+ export declare function resolvePaths(raw: Raw): Result<CanonPaths>;
84
+ export declare function resolveSession(raw: Raw): Result<SessionConfig>;
85
+ export declare function autonomyAt(root: string, rev: string): boolean;
86
+ export interface LocalConfig {
87
+ reviewerExtensions: string[];
88
+ opener?: string;
89
+ notify?: string;
90
+ }
91
+ export declare const localConfigPath: (root: string) => string;
92
+ export declare function loadLocalConfig(root: string): Result<LocalConfig>;
93
+ export declare function canonPaths(root: string): CanonPaths;
94
+ export declare const configPath: (root: string) => string;
95
+ export declare function loadConfig(root: string): Result<Config>;
96
+ export {};
package/dist/config.js CHANGED
@@ -69,8 +69,9 @@ export function checkValue(s, val) {
69
69
  return typeof val === 'boolean' ? ok(val) : invalid();
70
70
  case 'enum':
71
71
  return typeof val === 'string' && shape.values.includes(val) ? ok(val) : invalid();
72
+ // Row 239. A setting with a `pattern` holds only what it admits.
72
73
  case 'text':
73
- return typeof val === 'string' && val !== '' ? ok(val) : invalid();
74
+ return typeof val === 'string' && val !== '' && (s.pattern?.test(val) ?? true) ? ok(val) : invalid();
74
75
  case 'dir':
75
76
  return typeof val === 'string' && val !== '' ? relPath(field, val, 'directory') : invalid();
76
77
  case 'files': {
@@ -0,0 +1,16 @@
1
+ import { type ResolvedSpawn } from './spawn.js';
2
+ export declare const JOB_HOST_ENV = "WITNESS_JOB_HOST";
3
+ export declare const JOB_PROBE_EXIT = 86;
4
+ export type JobHost = {
5
+ ok: true;
6
+ file: string;
7
+ } | {
8
+ ok: false;
9
+ why: string;
10
+ };
11
+ export declare const JOB_SCRIPT: string;
12
+ export declare function winArg(arg: string): string;
13
+ export declare function jobTail(run: ResolvedSpawn): string;
14
+ export declare function contained(run: ResolvedSpawn, host: JobHost): ResolvedSpawn;
15
+ export declare function jobHost(env: Record<string, string | undefined>, platform?: NodeJS.Platform): JobHost;
16
+ export declare function containmentLine(host: JobHost, platform?: NodeJS.Platform): string;
@@ -0,0 +1,25 @@
1
+ import type { JournalCtx } from './journal.js';
2
+ type Env = Record<string, string | undefined>;
3
+ export declare const CONTEXT_ENV = "WITNESS_CONTEXT";
4
+ export interface Reading {
5
+ session: string;
6
+ tokens: number;
7
+ window: number;
8
+ }
9
+ export type Unmeasured = 'no adapter' | 'stale' | 'malformed' | 'no reading yet';
10
+ export type Fill = {
11
+ measured: true;
12
+ reading: Reading;
13
+ } | {
14
+ measured: false;
15
+ reason: Unmeasured;
16
+ };
17
+ export declare function sessionIdOf(env: Env): string | undefined;
18
+ export declare function sessionHash(id: string): string;
19
+ export declare function readFill(env: Env): Fill;
20
+ export declare function journalStamp(env: Env): JournalCtx | undefined;
21
+ export declare function withoutSession(env: Env): Env;
22
+ export declare function pctOf(tokens: number, window: number): number;
23
+ export declare function reached(tokens: number, window: number, limit: number): boolean;
24
+ export declare function windowLabel(window: number): string;
25
+ export {};
@@ -0,0 +1,27 @@
1
+ import type { Ctx } from './cli.js';
2
+ import type { TestOutcome } from './junit.js';
3
+ import { type Result } from './refusal.js';
4
+ import type { GateCheck } from './rounds.js';
5
+ import type { CanonDoc } from './scan.js';
6
+ export interface CriterionOutcome {
7
+ id: string;
8
+ kind: 'test' | 'cmd';
9
+ ok: boolean;
10
+ detail: string;
11
+ untrusted?: string;
12
+ held?: string[];
13
+ }
14
+ export declare function criterionMark(c: CriterionOutcome): string;
15
+ export interface CriteriaResult {
16
+ spec: string;
17
+ sha: string;
18
+ ok: boolean;
19
+ mode: 'filtered' | 'full-suite';
20
+ tagCount: number;
21
+ criteria: CriterionOutcome[];
22
+ }
23
+ export declare function runCriteria(runRoot: string, ctx: Ctx, doc: CanonDoc, opts?: {
24
+ trustRoot?: string;
25
+ suite?: TestOutcome[];
26
+ }): Promise<Result<CriteriaResult>>;
27
+ export declare function laneCheck(lane: Result<CriteriaResult>): GateCheck;
package/dist/criteria.js CHANGED
@@ -126,8 +126,9 @@ export function laneCheck(lane) {
126
126
  : [];
127
127
  // Row 179: a lane that passed with held criteria says so in its own record, so no reader
128
128
  // of the gate-run can take the green for a verification of what was not run.
129
+ // Row 239 (B13): with the spec, so a surface rebuilds the command from the ids (`heldSatisfy`).
129
130
  const held = lane.ok
130
- ? lane.value.criteria.flatMap((c) => (c.held ?? []).map((need) => ({ criterion: c.id, need, satisfy: satisfyCommand(lane.value.spec, need) })))
131
+ ? lane.value.criteria.flatMap((c) => (c.held ?? []).map((need) => ({ criterion: c.id, need, satisfy: satisfyCommand(lane.value.spec, need), spec: lane.value.spec })))
131
132
  : [];
132
133
  return {
133
134
  name: 'drift-lane',