@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,230 +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 {
7
- exactRecord, isTodoDigest, isTodoIdentifier, isTodoRef, todoSelfDigest,
8
- } from './todo-contracts.mjs';
9
-
10
- const GIT_OID = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/u;
11
-
12
- function sortedUniqueRefs(value, { nonempty = false } = {}) {
13
- return Array.isArray(value) && (!nonempty || value.length > 0) && value.every(isTodoRef)
14
- && value.every((entry, index) => index === 0 || value[index - 1] < entry);
15
- }
16
-
17
- /**
18
- * v2の三面surface(ADR 0137 Decision 1)。所有だけがtaskへ紐づく。
19
- *
20
- * 残余はsymbolを列挙しない。移らなかったものすべてという補集合であり、列挙を要求すると
21
- * 原fileの全symbol目録が契約の一部になる。
22
- */
23
- function seamSurface(value, taskIds) {
24
- if (!exactRecord(value, ['role', 'path', 'owner_task_ids', 'symbols'])
25
- || !['task_owned', 'shared', 'residual'].includes(value.role)
26
- || !isTodoRef(value.path)
27
- || !Array.isArray(value.owner_task_ids)
28
- || !sortedUniqueSymbols(value.symbols)) return false;
29
- if (value.role === 'task_owned') {
30
- return value.owner_task_ids.length === 1 && taskIds.has(value.owner_task_ids[0])
31
- && value.symbols.length > 0;
32
- }
33
- if (value.owner_task_ids.length !== 0) return false;
34
- return value.role === 'shared' ? value.symbols.length > 0 : value.symbols.length === 0;
35
- }
36
-
37
- function sortedUniqueSymbols(value) {
38
- return Array.isArray(value) && value.every(isTodoIdentifier)
39
- && value.every((entry, index) => index === 0 || value[index - 1] < entry);
40
- }
41
-
42
- function validateBoundedSeamCandidateV2(value) {
43
- if (!exactRecord(value, [
44
- 'schema', 'candidate_id', 'base_sha', 'manifest_digest', 'finding_digest',
45
- 'source_path', 'todo_refs', 'surfaces', 'allowed_paths', 'required_paths',
46
- 'verification_policy', 'candidate_digest',
47
- ])
48
- || !isTodoIdentifier(value.candidate_id) || !GIT_OID.test(value.base_sha)
49
- || !isTodoDigest(value.manifest_digest) || !isTodoDigest(value.finding_digest)
50
- || !isTodoRef(value.source_path)
51
- || !Array.isArray(value.todo_refs) || value.todo_refs.length < 2
52
- || !value.todo_refs.every((entry) => exactRecord(entry, ['plan_key', 'task_id'])
53
- && isTodoIdentifier(entry.plan_key) && isTodoIdentifier(entry.task_id))
54
- || !value.todo_refs.every((entry, index) => index === 0
55
- || `${value.todo_refs[index - 1].plan_key}\0${value.todo_refs[index - 1].task_id}`
56
- < `${entry.plan_key}\0${entry.task_id}`)) return false;
57
-
58
- const taskIds = new Set(value.todo_refs.map(({ task_id: taskId }) => taskId));
59
- if (!Array.isArray(value.surfaces)
60
- || !value.surfaces.every((surface) => seamSurface(surface, taskIds))
61
- || !value.surfaces.every((surface, index) => index === 0
62
- || value.surfaces[index - 1].path < surface.path)) return false;
63
-
64
- // 所有面は全taskへ1つずつ。残余は原path上に必ず1つ。共有面は0または1(粒度は未裁定)。
65
- const owned = value.surfaces.filter(({ role }) => role === 'task_owned');
66
- const shared = value.surfaces.filter(({ role }) => role === 'shared');
67
- const residual = value.surfaces.filter(({ role }) => role === 'residual');
68
- const ownerIds = new Set(owned.flatMap(({ owner_task_ids: ids }) => ids));
69
- if (owned.length !== taskIds.size || ownerIds.size !== taskIds.size
70
- || shared.length > 1 || residual.length !== 1
71
- || residual[0].path !== value.source_path) return false;
72
-
73
- // symbolは1面にしか属せない。二重所有は移動先が決まらない。
74
- const symbols = value.surfaces.flatMap(({ symbols: list }) => list);
75
- if (new Set(symbols).size !== symbols.length) return false;
76
-
77
- if (!sortedUniqueRefs(value.allowed_paths, { nonempty: true })
78
- || !sortedUniqueRefs(value.required_paths, { nonempty: true })
79
- || !value.required_paths.every((entry) => value.allowed_paths.includes(entry))
80
- || !value.surfaces.every(({ path }) => value.allowed_paths.includes(path))
81
- || !value.required_paths.includes(value.source_path)
82
- || !owned.every(({ path }) => value.required_paths.includes(path))) return false;
83
-
84
- // ADR 0138の五条件。1つでもfalseなら、その候補は採用条件を緩めた別物である。
85
- return exactRecord(value.verification_policy, [
86
- 'focused_test_refs', 'require_behavior_equivalence', 'require_fresh_sensor',
87
- 'require_overlap_reduction', 'require_no_new_conflict_pairs',
88
- 'require_parallelism_improvement',
89
- ])
90
- && sortedUniqueRefs(value.verification_policy.focused_test_refs, { nonempty: true })
91
- && value.verification_policy.require_behavior_equivalence === true
92
- && value.verification_policy.require_fresh_sensor === true
93
- && value.verification_policy.require_overlap_reduction === true
94
- && value.verification_policy.require_no_new_conflict_pairs === true
95
- && value.verification_policy.require_parallelism_improvement === true
96
- && isTodoDigest(value.candidate_digest)
97
- && value.candidate_digest === todoSelfDigest(value, 'candidate_digest');
98
- }
99
-
100
- export function validateBoundedSeamCandidate(value) {
101
- try {
102
- if (value?.schema === 'lattice.bounded_seam_candidate.v2') {
103
- return validateBoundedSeamCandidateV2(value);
104
- }
105
- return exactRecord(value, [
106
- 'schema', 'candidate_id', 'base_sha', 'manifest_digest', 'finding_digest',
107
- 'todo_refs', 'anchor', 'allowed_paths', 'required_paths', 'verification_policy',
108
- 'candidate_digest',
109
- ]) && value.schema === 'lattice.bounded_seam_candidate.v1'
110
- && isTodoIdentifier(value.candidate_id) && GIT_OID.test(value.base_sha)
111
- && isTodoDigest(value.manifest_digest) && isTodoDigest(value.finding_digest)
112
- && Array.isArray(value.todo_refs) && value.todo_refs.length >= 2
113
- && value.todo_refs.every((entry) => exactRecord(entry, ['plan_key', 'task_id'])
114
- && isTodoIdentifier(entry.plan_key) && isTodoIdentifier(entry.task_id))
115
- && value.todo_refs.every((entry, index) => index === 0
116
- || `${value.todo_refs[index - 1].plan_key}\0${value.todo_refs[index - 1].task_id}`
117
- < `${entry.plan_key}\0${entry.task_id}`)
118
- && exactRecord(value.anchor, ['path', 'symbol', 'start_line', 'end_line'])
119
- && isTodoRef(value.anchor.path) && isTodoIdentifier(value.anchor.symbol)
120
- && Number.isSafeInteger(value.anchor.start_line) && value.anchor.start_line >= 1
121
- && Number.isSafeInteger(value.anchor.end_line) && value.anchor.end_line >= value.anchor.start_line
122
- && sortedUniqueRefs(value.allowed_paths, { nonempty: true })
123
- && sortedUniqueRefs(value.required_paths, { nonempty: true })
124
- && value.required_paths.every((entry) => value.allowed_paths.includes(entry))
125
- && value.allowed_paths.includes(value.anchor.path)
126
- && exactRecord(value.verification_policy, [
127
- 'focused_test_ids', 'require_behavior_equivalence', 'require_fresh_sensor',
128
- 'require_overlap_reduction',
129
- ]) && Array.isArray(value.verification_policy.focused_test_ids)
130
- && value.verification_policy.focused_test_ids.length > 0
131
- && value.verification_policy.focused_test_ids.every(isTodoIdentifier)
132
- && value.verification_policy.focused_test_ids.every((entry, index) => index === 0
133
- || value.verification_policy.focused_test_ids[index - 1] < entry)
134
- && value.verification_policy.require_behavior_equivalence === true
135
- && value.verification_policy.require_fresh_sensor === true
136
- && value.verification_policy.require_overlap_reduction === true
137
- && isTodoDigest(value.candidate_digest)
138
- && value.candidate_digest === todoSelfDigest(value, 'candidate_digest');
139
- } catch { return false; }
140
- }
141
-
142
- function run(command, args, cwd) {
143
- return new Promise((resolve, reject) => {
144
- const child = spawn(command, args, { cwd, shell: false, stdio: ['ignore', 'pipe', 'pipe'] });
145
- const stdout = []; const stderr = [];
146
- child.stdout.on('data', (chunk) => stdout.push(chunk));
147
- child.stderr.on('data', (chunk) => stderr.push(chunk));
148
- child.once('error', reject);
149
- child.once('close', (code, signal) => {
150
- if (code === 0 && signal === null) resolve(Buffer.concat(stdout));
151
- else reject(new Error(`${command} failed (${signal ?? code}): ${Buffer.concat(stderr).toString('utf8').trim()}`));
152
- });
153
- });
154
- }
155
-
156
- async function fingerprint(repoRoot) {
157
- const [head, status, refs] = await Promise.all([
158
- run('git', ['rev-parse', 'HEAD'], repoRoot),
159
- run('git', ['status', '--porcelain=v1', '--untracked-files=all'], repoRoot),
160
- run('git', ['for-each-ref', '--format=%(refname) %(objectname)'], repoRoot),
161
- ]);
162
- return Buffer.concat([head, status, refs]).toString('hex');
163
- }
164
-
165
- async function changedPaths(worktreePath) {
166
- const bytes = await run('git', ['status', '--porcelain=v1', '-z', '--untracked-files=all'], worktreePath);
167
- const fields = bytes.toString('utf8').split('\0').filter(Boolean);
168
- const paths = [];
169
- for (let index = 0; index < fields.length; index += 1) {
170
- const field = fields[index];
171
- const status = field.slice(0, 2);
172
- paths.push(field.slice(3));
173
- if (status.includes('R') || status.includes('C')) paths.push(fields[++index]);
174
- }
175
- return [...new Set(paths)].sort();
176
- }
177
-
178
- function outcome(candidate, decision, reason, changed, verification, cleanupOk) {
179
- const result = { schema: 'lattice.bounded_seam_outcome.v1', candidate_id: candidate.candidate_id,
180
- candidate_digest: candidate.candidate_digest, decision, reason, changed_paths: changed,
181
- verification, cleanup_ok: cleanupOk, outcome_digest: '' };
182
- result.outcome_digest = todoSelfDigest(result, 'outcome_digest');
183
- return result;
184
- }
185
-
186
- export async function runBoundedSeamCandidate({ repoRoot, candidate, transform, verify }) {
187
- if (!validateBoundedSeamCandidate(candidate)) throw new TypeError('bounded seam candidate invalid');
188
- if (typeof repoRoot !== 'string' || typeof transform !== 'function' || typeof verify !== 'function') {
189
- throw new TypeError('bounded seam runner options invalid');
190
- }
191
- const before = await fingerprint(repoRoot);
192
- const head = (await run('git', ['rev-parse', 'HEAD'], repoRoot)).toString('utf8').trim();
193
- if (head !== candidate.base_sha) return outcome(candidate, 'rejected', 'stale_base', [], null, true);
194
- const worktreeRoot = await mkdtemp(path.join(tmpdir(), 'lattice-bounded-seam-'));
195
- const worktreePath = path.join(worktreeRoot, 'tree');
196
- let changed = [];
197
- let result;
198
- let cleanupOk = false;
199
- try {
200
- await run('git', ['worktree', 'add', '--detach', worktreePath, candidate.base_sha], repoRoot);
201
- try { await transform({ worktreePath, candidate: structuredClone(candidate) }); }
202
- catch { result = outcome(candidate, 'rejected', 'transform_failed', [], null, false); }
203
- if (result === undefined) changed = await changedPaths(worktreePath);
204
- if (result === undefined && (changed.some((entry) => !candidate.allowed_paths.includes(entry))
205
- || candidate.required_paths.some((entry) => !changed.includes(entry)))) {
206
- result = outcome(candidate, 'rejected', 'scope_drift', changed, null, false);
207
- } else if (result === undefined) {
208
- let verification;
209
- try { verification = await verify({ worktreePath, candidate: structuredClone(candidate), changed_paths: changed }); }
210
- catch { verification = null; }
211
- const valid = exactRecord(verification, [
212
- 'behavior_equivalent', 'focused_tests_passed', 'sensor_fresh', 'overlap_reduced',
213
- 'evidence_digest',
214
- ]) && verification.behavior_equivalent === true && verification.focused_tests_passed === true
215
- && verification.sensor_fresh === true && verification.overlap_reduced === true
216
- && isTodoDigest(verification.evidence_digest);
217
- result = outcome(candidate, valid ? 'accepted' : 'rejected',
218
- valid ? 'all_gates_passed' : 'verification_failed', changed, verification, false);
219
- }
220
- } finally {
221
- try {
222
- await run('git', ['worktree', 'remove', '--force', worktreePath], repoRoot);
223
- await rm(worktreeRoot, { recursive: true, force: true });
224
- cleanupOk = true;
225
- } catch { cleanupOk = false; }
226
- }
227
- const after = await fingerprint(repoRoot);
228
- if (before !== after) throw new TypeError('bounded seam runner modified canonical repository');
229
- return outcome(candidate, result.decision, result.reason, changed, result.verification, cleanupOk);
230
- }