@quolu/lattice 0.16.0 → 0.17.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quolu/lattice",
3
- "version": "0.16.0",
3
+ "version": "0.17.0",
4
4
  "description": "Lattice — phase-aware TODO graph compiler and conflict-aware orchestration runtime",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -62,6 +62,7 @@
62
62
  "precheck": "node --check src/bridge-launch-agent.mjs",
63
63
  "check": "node --check bin/lattice.mjs && node --check bin/lattice-mcp.mjs && node --check bin/lattice-dashboard.mjs && node --check bin/lattice-bridge.mjs && node --check bin/lattice-scripted-adapter.mjs && node --check src/cli-stdio.mjs && node --check src/bridge-address.mjs && node --check src/bridge-registrar.mjs && node --check src/todo-independence-guidance.mjs && node --check src/todo-independence.mjs && node --check src/todo-independence-contracts.mjs && node --check src/seam-proposal-contracts.mjs && node --check src/seam-proposal-queries.mjs && node --check src/seam-proposal.mjs && node --check src/todo-gantt-layout.mjs && node --check src/todo-gantt-scope.mjs && node --check src/bridge-config.mjs && node --check src/bridge-server.mjs && node --check src/bridge-daemon.mjs && node --check src/bridge-cli.mjs && node --check src/project-cli.mjs && node --check src/sensor-cli.mjs && node --check src/sensor-runtime.mjs && node --check src/sensor-adapter.mjs && node --check src/factory-diagnostics.mjs && node --check src/runtime-errors.mjs && node --check src/runtime-contracts.mjs && node --check src/runtime-event-store.mjs && node --check src/runtime-adapter-registry.mjs && node --check src/runtime-scripted-adapter-controller.mjs && node --check src/hash-chain.mjs && node --check src/dag-chain.mjs && node --check src/todo-contracts.mjs && node --check src/todo-chain.mjs && node --check src/todo-store.mjs && node --check src/todo-migration.mjs && node --check src/todo-revision.mjs && node --check src/todo-status.mjs && node --check src/todo-cli.mjs && node --check src/todo-dashboard-registry.mjs && node --check src/todo-gantt-presentation.mjs && node --check src/todo-gantt-live.mjs && node --check src/bounded-seam.mjs && node --check src/todo-narrative-anchor.mjs && node --check src/todo-markdown-renderer.mjs && node --check src/todo-gantt-svg.mjs && node --check src/todo-gantt-html.mjs && node --check src/runtime-projection.mjs && node --check src/runtime-decision-verifier.mjs && node --check src/runtime-front-end.mjs && node --check src/runtime-cli.mjs && node --check src/rc3-dogfood-scaffold.mjs && node --check src/runtime-engine.mjs && node --check src/runtime-scripted-executor.mjs && node --check src/runtime-diff-observer.mjs && node --check src/runtime-worktree-executor.mjs && node --check src/runtime-hold-recompile.mjs && node --check src/rc3-scripted-campaign.mjs && node --check src/rc3-actual-dogfood.mjs && node --check src/rc4-stage1-dogfood.mjs && node --check research/fixtures/dispatch-record/src/dispatch-record.mjs && node --check test/research-dispatch-record.test.mjs",
64
64
  "check:project-identity": "node --check src/project-identity.mjs",
65
- "ci": "npm run test && npm run test:sensor && npm run check && npm run check:project-identity"
65
+ "ci": "npm run test && npm run test:sensor && npm run check && npm run check:project-identity && npm run verify:todo-store",
66
+ "verify:todo-store": "node bin/lattice.mjs todo verify --json"
66
67
  }
67
68
  }
@@ -17,6 +17,9 @@ const STOP_REQUEST_SCHEMA = 'lattice.bridge_stop_request.v1';
17
17
  const STOP_RECEIPT_SCHEMA = 'lattice.bridge_stop_receipt.v1';
18
18
  const ACTIVE_MARKER_SCHEMA = 'lattice.bridge_daemon_active.v1';
19
19
  const CONTROL_MAX_BYTES = 65_536;
20
+ /** control fileの差し替え競合だけを吸収する再読の上限と間隔(`readStrictJson`が唯一の使用者)。 */
21
+ const CONTROL_READ_RETRY_LIMIT = 5;
22
+ const CONTROL_READ_RETRY_DELAY_MS = 20;
20
23
 
21
24
  export function bridgeDaemonDescriptorPath(env = process.env) {
22
25
  return path.join(bridgeConfigPaths(env).root, 'bridge-daemon.json');
@@ -66,7 +69,16 @@ function isIsoTimestamp(value) {
66
69
  try { return new Date(value).toISOString() === value; } catch { return false; }
67
70
  }
68
71
 
69
- async function readStrictJson(ref, code, label) {
72
+ /** 読み取り中の差し替え・消失につけるmarker。内容の異常と同じ顔をさせない。 */
73
+ const CONTROL_READ_RACE = Symbol('bridge_control_read_race');
74
+
75
+ function controlReadRace(message) {
76
+ const error = new Error(message);
77
+ error[CONTROL_READ_RACE] = true;
78
+ return error;
79
+ }
80
+
81
+ async function readStrictJsonOnce(ref, label) {
70
82
  let before;
71
83
  let handle;
72
84
  try {
@@ -77,12 +89,12 @@ async function readStrictJson(ref, code, label) {
77
89
  const opened = await handle.stat();
78
90
  if (!opened.isFile() || opened.dev !== before.dev || opened.ino !== before.ino
79
91
  || opened.size !== before.size || opened.size > CONTROL_MAX_BYTES) {
80
- throw new Error(`${label} changed during validation`);
92
+ throw controlReadRace(`${label} changed during validation`);
81
93
  }
82
94
  const text = await handle.readFile('utf8');
83
95
  const after = await lstat(ref);
84
96
  if (after.dev !== opened.dev || after.ino !== opened.ino || after.size !== opened.size) {
85
- throw new Error(`${label} changed during read`);
97
+ throw controlReadRace(`${label} changed during read`);
86
98
  }
87
99
  const errors = [];
88
100
  const tree = parseTree(text, errors, { allowTrailingComma: false, disallowComments: true });
@@ -91,11 +103,37 @@ async function readStrictJson(ref, code, label) {
91
103
  }
92
104
  return JSON.parse(text);
93
105
  } catch (error) {
94
- if (error?.code === 'ENOENT' && before === undefined) return null;
95
- throw new BridgeConfigError(code, `${label} invalid`, undefined, error);
106
+ // 最初のlstatで無い=未起動。読み始めた後に消えたのは停止との競合で、再読でnullへ落ち着く。
107
+ if (error?.code === 'ENOENT') {
108
+ if (before === undefined) return null;
109
+ throw controlReadRace(`${label} removed during read`);
110
+ }
111
+ throw error;
96
112
  } finally { await handle?.close(); }
97
113
  }
98
114
 
115
+ /**
116
+ * 公開はatomic renameなので、読み手はinodeの差し替えに必ず出会う。差し替えを跨いだ読みは
117
+ * 内容の異常ではなく再読で解ける競合であり、壊れたcontrol fileと同じerrorにしてはならない。
118
+ *
119
+ * 再試行するのは`CONTROL_READ_RACE`が付いた3条件だけである——検証中のinode/size変化、
120
+ * 読み取り中のinode/size変化、最初のlstat後の消失。最大`CONTROL_READ_RETRY_LIMIT`回、
121
+ * 間隔`CONTROL_READ_RETRY_DELAY_MS`で再読し、超えたら他と同じtyped errorで落とす。
122
+ * JSON不正・schema不正・mode不正は競合ではないので一度も再試行せず即fail closedにする。
123
+ */
124
+ async function readStrictJson(ref, code, label) {
125
+ for (let attempt = 0; ; attempt += 1) {
126
+ try {
127
+ return await readStrictJsonOnce(ref, label);
128
+ } catch (error) {
129
+ if (error?.[CONTROL_READ_RACE] !== true || attempt >= CONTROL_READ_RETRY_LIMIT) {
130
+ throw new BridgeConfigError(code, `${label} invalid`, undefined, error);
131
+ }
132
+ await new Promise((resolve) => setTimeout(resolve, CONTROL_READ_RETRY_DELAY_MS));
133
+ }
134
+ }
135
+ }
136
+
99
137
  async function readStrictControl(ref, schema, keys) {
100
138
  const value = await readStrictJson(ref, 'BRIDGE_STOP_CONTROL_INVALID', 'bridge stop control');
101
139
  if (value === null) return null;
@@ -15,7 +15,10 @@ import {
15
15
  isGitSha,
16
16
  severabilityOfConflictKind,
17
17
  } from './todo-independence-contracts.mjs';
18
- import { SEAM_PROPOSAL_GUIDANCE_CODES } from './todo-independence-guidance.mjs';
18
+ import {
19
+ SEAM_PROPOSAL_GUIDANCE_CODES,
20
+ seamProposalGuidanceCode,
21
+ } from './todo-independence-guidance.mjs';
19
22
 
20
23
  export const SEAM_PROPOSAL_SCHEMA = 'lattice.seam_proposal.v1';
21
24
  export const SEAM_PROPOSAL_PROJECTION_SCHEMA = 'lattice.seam_proposal_projection.v1';
@@ -309,19 +312,21 @@ export function validateSeamProposal(value) {
309
312
  }
310
313
  }
311
314
 
312
- const PROJECTION_GUIDANCE_BY_COVERAGE = Object.freeze({
313
- missing: 'seam_proposal_unrecorded',
314
- stale: 'seam_proposal_stale',
315
- superseded: 'seam_proposal_superseded',
316
- verified: 'seam_proposal_verified',
317
- });
318
-
319
- function projectionGuidance(value, coverage) {
320
- return exactRecord(value, ['code', 'message', 'next_action'])
321
- && SEAM_PROPOSAL_GUIDANCE_CODES.includes(value.code)
322
- && value.code === PROJECTION_GUIDANCE_BY_COVERAGE[coverage]
323
- && boundedText(value.message)
324
- && isTodoIdentifier(value.next_action);
315
+ /**
316
+ * 案内は、鮮度と載っているunknownから一意に決まる。生成側の文言をそのまま載せる規約なので
317
+ * shapeだけを見ると、投影が状況と噛み合わない案内を載せても通ってしまう。codeは規則正本へ
318
+ * 引き直して照合する(ADR 0130 Decision 1・2)。
319
+ */
320
+ function projectionGuidance(value, coverage, components) {
321
+ if (!exactRecord(value, ['code', 'message', 'next_action'])
322
+ || !SEAM_PROPOSAL_GUIDANCE_CODES.includes(value.code)
323
+ || !boundedText(value.message)
324
+ || !isTodoIdentifier(value.next_action)) return false;
325
+ const unknownKinds = Array.isArray(components)
326
+ ? components.flatMap((component) => (Array.isArray(component?.unknowns)
327
+ ? component.unknowns.map((unknown) => unknown?.kind) : []))
328
+ : [];
329
+ return value.code === seamProposalGuidanceCode({ coverage, unknownKinds });
325
330
  }
326
331
 
327
332
  function projectionComponent(value) {
@@ -383,7 +388,7 @@ export function validateSeamProposalProjection(value) {
383
388
  || !isTodoIdentifier(value.plan_key)
384
389
  || !TODO_INDEPENDENCE_COVERAGE.includes(value.coverage)
385
390
  || !isGitSha(value.current_base_sha)
386
- || !projectionGuidance(value.guidance, value.coverage)
391
+ || !projectionGuidance(value.guidance, value.coverage, value.components)
387
392
  || !isTodoDigest(value.result_digest)) return false;
388
393
 
389
394
  if (value.coverage === 'missing') {
package/src/todo-cli.mjs CHANGED
@@ -1013,7 +1013,10 @@ async function seamProposal({ repoRoot, requestedPlanKey }) {
1013
1013
  topology_digest: artifact?.source_binding.topology_digest ?? null,
1014
1014
  independence_result_digest: artifact?.source_binding.independence_result_digest ?? null,
1015
1015
  compiled_at: artifact?.compiled_at ?? null,
1016
- guidance: selectSeamProposalGuidance({ coverage }),
1016
+ guidance: selectSeamProposalGuidance({
1017
+ coverage,
1018
+ unknownKinds: components.flatMap(({ unknowns }) => unknowns.map(({ kind }) => kind)),
1019
+ }),
1017
1020
  component_count: artifact === null ? null : components.length,
1018
1021
  conflict_resource_count: artifact === null ? null : components
1019
1022
  .reduce((count, component) => count + component.conflicts.length, 0),
@@ -24,9 +24,39 @@ export const SEAM_PROPOSAL_GUIDANCE_CODES = Object.freeze([
24
24
  'seam_proposal_unrecorded',
25
25
  'seam_proposal_superseded',
26
26
  'seam_proposal_stale',
27
+ 'seam_proposal_binding_overlap',
28
+ 'seam_proposal_binding_outside_resource',
29
+ 'seam_proposal_binding_symbol_unresolved',
30
+ 'seam_proposal_binding_resource_unresolved',
31
+ 'seam_proposal_binding_ambiguous',
32
+ 'seam_proposal_binding_missing',
27
33
  'seam_proposal_verified',
28
34
  ]);
29
35
 
36
+ /**
37
+ * 束縛失敗のunknown種別から案内codeを引く表。並びは優先順で、先頭ほど次の一歩が具体的である。
38
+ *
39
+ * 宣言が壊れている状況を、宣言が無い状況より上に置く。壊れている方は直す対象が
40
+ * 一意に決まるのに対し、無い方は何をどう宣言するかから決めることになるからである。
41
+ */
42
+ const SEAM_PROPOSAL_BINDING_GUIDANCE = Object.freeze([
43
+ Object.freeze(['concern_anchor_overlap', 'seam_proposal_binding_overlap']),
44
+ Object.freeze(['concern_anchor_outside_resource', 'seam_proposal_binding_outside_resource']),
45
+ Object.freeze(['concern_anchor_unresolved', 'seam_proposal_binding_symbol_unresolved']),
46
+ Object.freeze(['concern_anchor_resource_unresolved', 'seam_proposal_binding_resource_unresolved']),
47
+ Object.freeze(['semantic_owner_binding_ambiguous', 'seam_proposal_binding_ambiguous']),
48
+ Object.freeze(['semantic_owner_binding_missing', 'seam_proposal_binding_missing']),
49
+ ]);
50
+
51
+ /**
52
+ * 束縛の案内へ共通で添える、記録が更新される条件。
53
+ *
54
+ * concern_anchorsはwitness setにあり、seam提案はindependence記録のwitness_set_digestと
55
+ * 一致する宣言しか読まない。宣言を直しただけでは提案は変わらないので、そこまで述べないと
56
+ * 「直したのに同じunknownが出る」で止まる。
57
+ */
58
+ const BINDING_RECOMPILE_HINT = '宣言はwitness setにあり、independence compileとseam-proposal compileを通し直すまで提案へ写らない。';
59
+
30
60
  const CATALOG = Object.freeze({
31
61
  independence_no_ready_frontier: Object.freeze({
32
62
  message: '着手候補が無いため、並列可否を述べる対象が無い。',
@@ -76,6 +106,30 @@ const CATALOG = Object.freeze({
76
106
  message: 'seam提案の生成後にHEADが進み、記録時点の構造証拠は現在のcodeを指していない。',
77
107
  next_action: 'compile_seam_proposal',
78
108
  }),
109
+ seam_proposal_binding_overlap: Object.freeze({
110
+ message: `同じ資源について、複数のToDoが同じsymbolを自分の担当として宣言している。どちら側へ切るか決まらないため、切断候補を束縛できない。${BINDING_RECOMPILE_HINT}`,
111
+ next_action: 'split_overlapping_concern_anchors_then_recompile',
112
+ }),
113
+ seam_proposal_binding_outside_resource: Object.freeze({
114
+ message: `concern_anchorsが挙げたsymbolが、withinで指した資源の外にある。その資源の分割を説明しないので束縛へ使えない。${BINDING_RECOMPILE_HINT}`,
115
+ next_action: 'correct_concern_anchors_then_recompile',
116
+ }),
117
+ seam_proposal_binding_symbol_unresolved: Object.freeze({
118
+ message: `concern_anchorsが挙げたsymbolを、sensorが同名同pathで解決できなかった。近い別symbolへは寄せないため、束縛は成立していない。${BINDING_RECOMPILE_HINT}`,
119
+ next_action: 'correct_concern_anchors_then_recompile',
120
+ }),
121
+ seam_proposal_binding_resource_unresolved: Object.freeze({
122
+ message: `concern_anchorsのwithinが指す資源をsensorで解決できなかった。宣言がどの資源についてのものか確定しない。${BINDING_RECOMPILE_HINT}`,
123
+ next_action: 'correct_concern_anchors_then_recompile',
124
+ }),
125
+ seam_proposal_binding_ambiguous: Object.freeze({
126
+ message: `宣言したsymbolが切断候補の複数側へ当たるため、どのToDoがどちらを所有するか一意に決まらない。${BINDING_RECOMPILE_HINT}`,
127
+ next_action: 'narrow_concern_anchors_then_recompile',
128
+ }),
129
+ seam_proposal_binding_missing: Object.freeze({
130
+ message: `係争資源の中でどのToDoが何を触るかが宣言されていないため、切断候補を所有者へ束縛できない。依存線でも呼び出し辺でも所有は決まらない。${BINDING_RECOMPILE_HINT}`,
131
+ next_action: 'declare_concern_anchors_then_recompile',
132
+ }),
79
133
  seam_proposal_verified: Object.freeze({
80
134
  message: 'seam提案の記録は現在のplan、並列可否記録、HEADと一致している。',
81
135
  next_action: 'none',
@@ -135,21 +189,37 @@ export function selectIndependenceGuidance({
135
189
  return todoIndependenceGuidance('independence_verified');
136
190
  }
137
191
 
138
- export function selectSeamProposalGuidance({ coverage }) {
139
- const code = coverage === 'missing' ? 'seam_proposal_unrecorded'
140
- : coverage === 'superseded' ? 'seam_proposal_superseded'
141
- : coverage === 'stale' ? 'seam_proposal_stale'
142
- : coverage === 'verified' ? 'seam_proposal_verified' : null;
143
- if (code === null) throw new TypeError(`unknown seam proposal coverage: ${coverage}`);
144
- return todoIndependenceGuidance(code);
192
+ /**
193
+ * seam提案の状況から案内codeを引く。投影の生成側と契約の検査側が同じ規則を読むための正本。
194
+ *
195
+ * 鮮度(coverage)を束縛失敗より先に見る。記録が古い・別topologyについてのものである時、
196
+ * そこに載っているunknownは現在のcodeについての事実ではないため、先に再compileが要る。
197
+ * 記録が現在と一致している時だけ、束縛失敗が「今直せる状況」になる。
198
+ */
199
+ export function seamProposalGuidanceCode({ coverage, unknownKinds = [] }) {
200
+ if (coverage === 'missing') return 'seam_proposal_unrecorded';
201
+ if (coverage === 'superseded') return 'seam_proposal_superseded';
202
+ if (coverage === 'stale') return 'seam_proposal_stale';
203
+ if (coverage !== 'verified') throw new TypeError(`unknown seam proposal coverage: ${coverage}`);
204
+ const kinds = new Set(unknownKinds);
205
+ const binding = SEAM_PROPOSAL_BINDING_GUIDANCE.find(([kind]) => kinds.has(kind));
206
+ return binding === undefined ? 'seam_proposal_verified' : binding[1];
207
+ }
208
+
209
+ export function selectSeamProposalGuidance({ coverage, unknownKinds = [] }) {
210
+ return todoIndependenceGuidance(seamProposalGuidanceCode({ coverage, unknownKinds }));
145
211
  }
146
212
 
147
213
  /**
148
214
  * 宣言からcompileを経て読むまでの順序。helpとMCP instructionsが同じ手順を語るための正本。
149
215
  * 面ごとに手順を書き直すと、片方だけが古くなる。
216
+ *
217
+ * 宣言できる欄を挙げる面はここだけなので、witness契約へ欄を足したらここへも足す。
218
+ * 足さないと、能力はあるのに機械が黙る面が残る(ADR 0130)。
150
219
  */
151
220
  export const TODO_INDEPENDENCE_WORKFLOW = Object.freeze([
152
221
  '1. 宣言する: .lattice/todo/witness/<plan_key>.json へ、ToDoごとのowns/reads/writes/affected_testsを書く',
222
+ ' 係争資源しか所有していないToDoは、その資源の中で自分が触るsymbolをconcern_anchorsへ宣言できる(witness set v2)。並列可否の判定には写らず、切断候補の束縛だけに効く',
153
223
  '2. 判定する: lattice todo independence compile --plan <key> --input <ref>(実sensorを引き、clean worktreeが要る)',
154
224
  '3. 読む: lattice todo independence --plan <key> --json(sensorを引かず、記録とHEAD照合だけで返る)',
155
225
  '4. 追従する: plan改訂後は lattice todo independence witness migrate --plan <key> で宣言を写してから再compileする',