@quolu/lattice 0.79.5 → 0.80.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 (51) hide show
  1. package/docs/schemas/lattice.plan_create_input.v4.schema.json +8 -4
  2. package/docs/schemas/lattice.todo_extraction.v3.schema.json +5 -3
  3. package/docs/schemas/lattice.todo_extraction.v4.schema.json +5 -3
  4. package/package.json +1 -1
  5. package/src/fs-publish.mjs +9 -0
  6. package/src/mcp-startup-receipt.mjs +3 -3
  7. package/src/project-cli.mjs +2 -2
  8. package/src/runtime-os-observation.mjs +18 -1
  9. package/src/store-lock.mjs +250 -0
  10. package/src/todo-cli.mjs +6 -0
  11. package/src/todo-contracts.mjs +95 -50
  12. package/src/todo-migration.mjs +35 -10
  13. package/src/todo-note-store.mjs +45 -12
  14. package/src/todo-readiness.mjs +154 -0
  15. package/src/todo-status.mjs +4 -33
  16. package/src/todo-store-git-transaction.mjs +11 -6
  17. package/src/todo-store-view.mjs +47 -0
  18. package/src/todo-store.mjs +597 -398
  19. package/src/artifact-contracts-v2.mjs +0 -325
  20. package/src/boundary-compiler.mjs +0 -712
  21. package/src/bounded-seam.mjs +0 -230
  22. package/src/control-compiler.mjs +0 -532
  23. package/src/rc1-black-box-oracle.mjs +0 -906
  24. package/src/rc1-comparison.mjs +0 -154
  25. package/src/rc1-evidence-bundle.mjs +0 -456
  26. package/src/rc1-v4-campaign.mjs +0 -708
  27. package/src/rc1-v4-transform.mjs +0 -467
  28. package/src/rc1-v5-artifact-set.mjs +0 -855
  29. package/src/rc1-v5-behavior-evidence.mjs +0 -594
  30. package/src/rc1-v5-campaign.mjs +0 -797
  31. package/src/rc1-v5-transform.mjs +0 -421
  32. package/src/rc1-v6-artifact-set.mjs +0 -807
  33. package/src/rc1-v6-behavior-evidence.mjs +0 -273
  34. package/src/rc1-v6-campaign.mjs +0 -623
  35. package/src/rc1-v6-causal-binding.mjs +0 -473
  36. package/src/rc1-v6-measurement.mjs +0 -313
  37. package/src/rc2-artifact-set.mjs +0 -1584
  38. package/src/rc2-campaign.mjs +0 -1495
  39. package/src/rc2-delivery-policy-front-end.mjs +0 -1079
  40. package/src/rc2-delivery-policy-oracle.mjs +0 -134
  41. package/src/rc2-delivery-policy-transform.mjs +0 -1127
  42. package/src/rc2-rc1-transfer-front-end.mjs +0 -511
  43. package/src/rc3-actual-dogfood.mjs +0 -652
  44. package/src/rc3-dogfood-scaffold.mjs +0 -315
  45. package/src/rc3-scripted-campaign.mjs +0 -1383
  46. package/src/rc4-stage1-dogfood.mjs +0 -673
  47. package/src/runtime-scripted-executor.mjs +0 -163
  48. package/src/runtime-worktree-executor.mjs +0 -199
  49. package/src/seam-transform.mjs +0 -554
  50. package/src/treatment-compiler.mjs +0 -728
  51. package/src/treatment-runner.mjs +0 -656
@@ -1,163 +0,0 @@
1
- import { selfDigest, validateExecutorPacket } from './runtime-contracts.mjs';
2
-
3
- /**
4
- * RC3-E 決定論的scripted executor adapter(ADR 0044 Decision 9)。
5
- *
6
- * provider非依存adapter interfaceのprimary実装。挙動はscript(TODOごとのstep列)
7
- * だけで決まり、wall-clock・乱数・環境に依存しない。scripted campaign(RC3-H)が
8
- * 既知の正解集合を持つ条件注入に使う。
9
- *
10
- * adapter interface(provider非依存契約):
11
- * dispatch({ packet }) → { executor_handle, worktree_id }
12
- * observe({ executor_handle }) → { state: 'running' }
13
- * | { state: 'unknown' } … timeout相当
14
- * | { state: 'checkpoint_ready', checkpoint }
15
- * | { state: 'terminal', receipt }
16
- *
17
- * - 同一taskの重複dispatchはtyped rejectする(Decision 9.4)。
18
- * - `unknown`はfailureではない。同一handleでの再observeだけが正規の回収経路で、
19
- * scriptは`stall`stepでこの経路を決定論的に再現する。
20
- * - terminal receiptはdispatch済みpacketへ帰属し(handle・worktree・packet digest)、
21
- * scriptのoverridesで帰属破壊・stale epoch等の敵対条件を注入できる。
22
- */
23
-
24
- const STEP_KINDS = new Set(['checkpoint', 'stall', 'hold_request', 'terminal']);
25
-
26
- function fail(reason) {
27
- throw new TypeError(`scripted executor契約違反: ${reason}`);
28
- }
29
-
30
- function plainRecord(value) {
31
- return value !== null
32
- && typeof value === 'object'
33
- && !Array.isArray(value)
34
- && Object.getPrototypeOf(value) === Object.prototype;
35
- }
36
-
37
- function validateScript(script) {
38
- if (!plainRecord(script)) fail('scriptがplain objectではない');
39
- for (const [todoId, steps] of Object.entries(script)) {
40
- if (!Array.isArray(steps) || steps.length === 0) fail(`script.${todoId}がstep配列ではない`);
41
- for (const step of steps) {
42
- if (!plainRecord(step) || !STEP_KINDS.has(step.kind)) {
43
- fail(`script.${todoId}に未知のstepがある: ${String(step?.kind)}`);
44
- }
45
- }
46
- if (steps.filter((step) => step.kind === 'terminal').length > 1) {
47
- fail(`script.${todoId}のterminal stepは1つまで`);
48
- }
49
- }
50
- }
51
-
52
- /**
53
- * script駆動のexecutor adapterを作る。
54
- */
55
- export function createScriptedExecutorAdapter(options = {}) {
56
- if (!plainRecord(options) || !plainRecord(options.script)) {
57
- fail('createScriptedExecutorAdapter optionsが不正');
58
- }
59
- validateScript(options.script);
60
- const script = structuredClone(options.script);
61
-
62
- // handle → { packet, steps, cursor, terminalReported }
63
- const active = new Map();
64
- // 重複拒否はtodo_id単位(task_refの付け替えによる同一TODOの二重起動と、
65
- // 決定論的handleの衝突・上書きを塞ぐ。review P1採用)。redispatchは
66
- // RC3-Gの新context契約が新しいadapter instanceで行う。
67
- const dispatchedTodoIds = new Set();
68
-
69
- function buildReceipt(handleState, step) {
70
- const { packet } = handleState;
71
- const overrides = plainRecord(step.receipt_overrides) ? step.receipt_overrides : {};
72
- const receipt = {
73
- schema: 'lattice.executor_receipt.v1',
74
- receipt_id: `${packet.todo_id}-r1`,
75
- executor_handle: handleState.executorHandle,
76
- worktree_id: handleState.worktreeId,
77
- base_sha: packet.base_sha,
78
- plan_epoch: packet.plan_epoch,
79
- packet_digest: packet.packet_digest,
80
- todo_id: packet.todo_id,
81
- checkpoint_digest: typeof step.checkpoint_digest === 'string'
82
- ? step.checkpoint_digest
83
- : (handleState.lastCheckpointDigest ?? 'c'.repeat(64)),
84
- observed_diff: Array.isArray(step.observed_diff)
85
- ? structuredClone(step.observed_diff)
86
- : packet.scope.writes.map((path) => ({ path, change: 'modified' })),
87
- ...structuredClone(overrides),
88
- };
89
- receipt.receipt_digest = selfDigest(receipt, 'receipt_digest');
90
- return receipt;
91
- }
92
-
93
- return {
94
- kind: 'scripted',
95
-
96
- async dispatch({ packet } = {}) {
97
- if (!validateExecutorPacket(packet)) fail('dispatchにはexecutor_packet.v1が必要');
98
- const steps = script[packet.todo_id];
99
- if (steps === undefined) fail(`scriptに存在しないTODOをdispatchできない: ${packet.todo_id}`);
100
- if (dispatchedTodoIds.has(packet.todo_id)) {
101
- fail(`同一TODOの重複dispatchを拒否する: ${packet.todo_id}`);
102
- }
103
- dispatchedTodoIds.add(packet.todo_id);
104
- const executorHandle = `scripted-${packet.todo_id}-h1`;
105
- const worktreeId = `wt-${packet.todo_id}`;
106
- active.set(executorHandle, {
107
- packet: structuredClone(packet),
108
- executorHandle,
109
- worktreeId,
110
- steps: structuredClone(steps),
111
- cursor: 0,
112
- terminalReported: false,
113
- lastCheckpointDigest: null,
114
- });
115
- return { executor_handle: executorHandle, worktree_id: worktreeId };
116
- },
117
-
118
- /**
119
- * epoch rebind(Decision 7.3): content不変のままplan epochだけを更新する。
120
- * 以後のterminal receiptは新epochを名乗る(packet_digestはdispatch時の
121
- * ものを保持=dispatch記録への帰属は不変)。
122
- */
123
- async rebind({ executor_handle: executorHandle, rebind } = {}) {
124
- const handleState = active.get(executorHandle);
125
- if (handleState === undefined) fail(`未知のhandleをrebindできない: ${String(executorHandle)}`);
126
- if (!plainRecord(rebind) || rebind.schema !== 'lattice.epoch_rebind_packet.v1'
127
- || rebind.todo_id !== handleState.packet.todo_id) {
128
- fail(`rebind packetが不正: ${String(executorHandle)}`);
129
- }
130
- handleState.packet.plan_epoch = rebind.new_plan_epoch;
131
- return { executor_handle: executorHandle, plan_epoch: rebind.new_plan_epoch };
132
- },
133
-
134
- async observe({ executor_handle: executorHandle } = {}) {
135
- const handleState = active.get(executorHandle);
136
- if (handleState === undefined) fail(`未知のhandleを観測できない: ${String(executorHandle)}`);
137
- if (handleState.terminalReported) {
138
- fail(`terminal報告済みhandleの再観測: ${executorHandle}`);
139
- }
140
- if (handleState.cursor >= handleState.steps.length) {
141
- return { state: 'running' };
142
- }
143
- const step = handleState.steps[handleState.cursor];
144
- handleState.cursor += 1;
145
- if (step.kind === 'stall') {
146
- return { state: 'unknown' };
147
- }
148
- if (step.kind === 'checkpoint') {
149
- if (!plainRecord(step.checkpoint)) fail('checkpoint stepにcheckpoint payloadが必要');
150
- if (typeof step.checkpoint.checkpoint_digest === 'string') {
151
- handleState.lastCheckpointDigest = step.checkpoint.checkpoint_digest;
152
- }
153
- return { state: 'checkpoint_ready', checkpoint: structuredClone(step.checkpoint) };
154
- }
155
- if (step.kind === 'hold_request') {
156
- if (!plainRecord(step.finding)) fail('hold_request stepにfinding payloadが必要');
157
- return { state: 'hold_requested', finding: structuredClone(step.finding) };
158
- }
159
- handleState.terminalReported = true;
160
- return { state: 'terminal', receipt: buildReceipt(handleState, step) };
161
- },
162
- };
163
- }
@@ -1,199 +0,0 @@
1
- import { spawn } from 'node:child_process';
2
- import { mkdtemp, rm } from 'node:fs/promises';
3
- import { tmpdir } from 'node:os';
4
- import path from 'node:path';
5
-
6
- import { captureWorktreeDiff } from './runtime-diff-observer.mjs';
7
- import { selfDigest, validateExecutorPacket } from './runtime-contracts.mjs';
8
-
9
- /**
10
- * RC3-F isolated worktree executor adapter(ADR 0044 Decision 9、plan RC3-F)。
11
- *
12
- * disposable repoのbase_shaからdetached worktreeをtmpdir配下へprovisionし、
13
- * caller提供のwork関数(TODOごとのfile変更)を実行して、実diffだけを
14
- * checkpoint/receiptとして報告する。canonical worktreeへは書かない。
15
- *
16
- * - adapter契約はscripted executorと同一(dispatch/observe)。
17
- * - commit・branch切替等の禁止操作はdiff observerのHEAD drift検査でfail loud。
18
- * - terminal時にworktreeをcleanupし、失敗を成功へ丸めず観測結果
19
- * (cleanup_ok・residual_paths)としてterminal observationへ載せる。
20
- * - 同一TODOの重複dispatchはtyped reject(Decision 9.4)。
21
- */
22
-
23
- function fail(reason) {
24
- throw new TypeError(`worktree executor契約違反: ${reason}`);
25
- }
26
-
27
- function plainRecord(value) {
28
- return value !== null
29
- && typeof value === 'object'
30
- && !Array.isArray(value)
31
- && Object.getPrototypeOf(value) === Object.prototype;
32
- }
33
-
34
- function run(command, args, cwd) {
35
- return new Promise((resolve, reject) => {
36
- const child = spawn(command, args, { cwd, shell: false, stdio: ['ignore', 'pipe', 'pipe'] });
37
- const stdout = [];
38
- const stderr = [];
39
- child.stdout.on('data', (chunk) => stdout.push(chunk));
40
- child.stderr.on('data', (chunk) => stderr.push(chunk));
41
- child.once('error', reject);
42
- child.once('close', (code, signal) => {
43
- if (code === 0 && signal === null) resolve(Buffer.concat(stdout).toString('utf8'));
44
- else {
45
- reject(new TypeError(
46
- `${command} ${args[0]} failed (${signal ?? code}): ${Buffer.concat(stderr).toString('utf8').trim()}`,
47
- ));
48
- }
49
- });
50
- });
51
- }
52
-
53
- /**
54
- * disposable repoに対するworktree executor adapterを作る。
55
- * work: { [todoId]: async ({ worktreePath }) => void }
56
- */
57
- export function createWorktreeExecutorAdapter(options = {}) {
58
- if (!plainRecord(options)
59
- || typeof options.repoRoot !== 'string' || options.repoRoot.length === 0
60
- || !plainRecord(options.work)) {
61
- fail('createWorktreeExecutorAdapter optionsが不正');
62
- }
63
- const { repoRoot, work } = options;
64
- const active = new Map();
65
- const dispatchedTodoIds = new Set();
66
- let handleCounter = 0;
67
-
68
- // canonical repo(disposable repoの本体worktreeと共有refs)への書込・ref作成を
69
- // 協調前提の検出guardとして観測する(Decision 9.2。malicious executorはNon-goal)。
70
- async function canonicalFingerprint() {
71
- const head = await run('git', ['rev-parse', 'HEAD'], repoRoot);
72
- const status = await run('git', ['status', '--porcelain=v1', '--untracked-files=all', '--ignored=matching'], repoRoot);
73
- const refs = await run('git', ['for-each-ref', '--format=%(refname) %(objectname)'], repoRoot);
74
- return `${head.trim()}\n${status}\n${refs}`;
75
- }
76
-
77
- async function cleanupWorktree(worktreePath, worktreeRoot) {
78
- try {
79
- await run('git', ['worktree', 'remove', '--force', worktreePath], repoRoot);
80
- await rm(worktreeRoot, { recursive: true, force: true });
81
- return { cleanup_ok: true, residual_paths: [] };
82
- } catch (error) {
83
- return {
84
- cleanup_ok: false,
85
- residual_paths: [worktreeRoot],
86
- recovery: `git worktree remove --force ${worktreePath} を手動再実行する`,
87
- message: String(error?.message ?? error),
88
- };
89
- }
90
- }
91
-
92
- return {
93
- kind: 'isolated-worktree',
94
-
95
- async dispatch({ packet } = {}) {
96
- if (!validateExecutorPacket(packet)) fail('dispatchにはexecutor_packet.v1が必要');
97
- const workFn = work[packet.todo_id];
98
- if (typeof workFn !== 'function') fail(`work関数が未定義のTODO: ${packet.todo_id}`);
99
- if (dispatchedTodoIds.has(packet.todo_id)) {
100
- fail(`同一TODOの重複dispatchを拒否する: ${packet.todo_id}`);
101
- }
102
- dispatchedTodoIds.add(packet.todo_id);
103
-
104
- handleCounter += 1;
105
- const executorHandle = `worktree-${packet.todo_id}-h${handleCounter}`;
106
- const worktreeId = `wt-${packet.todo_id}-${handleCounter}`;
107
- const worktreeRoot = await mkdtemp(path.join(tmpdir(), `lattice-rc3-wt-${packet.todo_id}-`));
108
- const worktreePath = path.join(worktreeRoot, 'tree');
109
- const fingerprintBefore = await canonicalFingerprint();
110
- try {
111
- await run('git', ['worktree', 'add', '--detach', worktreePath, packet.base_sha], repoRoot);
112
- await workFn({ worktreePath });
113
- const fingerprintAfter = await canonicalFingerprint();
114
- if (fingerprintAfter !== fingerprintBefore) {
115
- fail(`work関数がcanonical repoまたは共有refsを変更した: ${packet.todo_id}`);
116
- }
117
- } catch (error) {
118
- // 失敗経路でもworktreeを孤立させない。cleanup失敗は失敗messageへ載せて
119
- // fail loudし、retryできるようdispatch予約を解放する。
120
- const cleanup = await cleanupWorktree(worktreePath, worktreeRoot);
121
- dispatchedTodoIds.delete(packet.todo_id);
122
- const residual = cleanup.cleanup_ok ? '' : `(cleanup失敗、残存path: ${cleanup.residual_paths.join(', ')})`;
123
- throw new TypeError(`dispatch失敗: ${String(error?.message ?? error)}${residual}`);
124
- }
125
-
126
- active.set(executorHandle, {
127
- packet: structuredClone(packet),
128
- executorHandle,
129
- worktreeId,
130
- worktreeRoot,
131
- worktreePath,
132
- checkpoint: null,
133
- terminalReported: false,
134
- });
135
- return { executor_handle: executorHandle, worktree_id: worktreeId };
136
- },
137
-
138
- /** epoch rebind(Decision 7.3): content不変のままplan epochだけを更新する。 */
139
- async rebind({ executor_handle: executorHandle, rebind } = {}) {
140
- const handleState = active.get(executorHandle);
141
- if (handleState === undefined) fail(`未知のhandleをrebindできない: ${String(executorHandle)}`);
142
- if (!plainRecord(rebind) || rebind.schema !== 'lattice.epoch_rebind_packet.v1'
143
- || rebind.todo_id !== handleState.packet.todo_id) {
144
- fail(`rebind packetが不正: ${String(executorHandle)}`);
145
- }
146
- handleState.packet.plan_epoch = rebind.new_plan_epoch;
147
- return { executor_handle: executorHandle, plan_epoch: rebind.new_plan_epoch };
148
- },
149
-
150
- async observe({ executor_handle: executorHandle } = {}) {
151
- const handleState = active.get(executorHandle);
152
- if (handleState === undefined) fail(`未知のhandleを観測できない: ${String(executorHandle)}`);
153
- if (handleState.terminalReported) fail(`terminal報告済みhandleの再観測: ${executorHandle}`);
154
- const { packet } = handleState;
155
-
156
- if (handleState.checkpoint === null) {
157
- // 1回目: 実diffをbounded canonical recordへ変換してcheckpoint報告。
158
- // 観測失敗(HEAD drift等の禁止操作検出を含む)時はworktreeを回収してから
159
- // fail loudする(無証拠の恒久leakを残さない)。
160
- let checkpoint;
161
- try {
162
- checkpoint = await captureWorktreeDiff({
163
- worktreePath: handleState.worktreePath,
164
- baseSha: packet.base_sha,
165
- });
166
- } catch (error) {
167
- handleState.terminalReported = true;
168
- const cleanup = await cleanupWorktree(handleState.worktreePath, handleState.worktreeRoot);
169
- const residual = cleanup.cleanup_ok ? '' : `(cleanup失敗、残存path: ${cleanup.residual_paths.join(', ')})`;
170
- throw new TypeError(`${String(error?.message ?? error)}${residual}`);
171
- }
172
- handleState.checkpoint = checkpoint;
173
- return { state: 'checkpoint_ready', checkpoint: structuredClone(checkpoint) };
174
- }
175
-
176
- // 2回目: terminal receipt+worktree cleanup(失敗を丸めず観測へ載せる)。
177
- handleState.terminalReported = true;
178
- const receipt = {
179
- schema: 'lattice.executor_receipt.v1',
180
- receipt_id: `${packet.todo_id}-r1`,
181
- executor_handle: handleState.executorHandle,
182
- worktree_id: handleState.worktreeId,
183
- base_sha: packet.base_sha,
184
- plan_epoch: packet.plan_epoch,
185
- packet_digest: packet.packet_digest,
186
- todo_id: packet.todo_id,
187
- checkpoint_digest: handleState.checkpoint.checkpoint_digest,
188
- observed_diff: handleState.checkpoint.diff.entries.map((entry) => ({
189
- path: entry.path,
190
- change: entry.change,
191
- })),
192
- };
193
- receipt.receipt_digest = selfDigest(receipt, 'receipt_digest');
194
-
195
- const cleanup = await cleanupWorktree(handleState.worktreePath, handleState.worktreeRoot);
196
- return { state: 'terminal', receipt, cleanup };
197
- },
198
- };
199
- }