@quolu/lattice 0.28.0 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quolu/lattice",
3
- "version": "0.28.0",
3
+ "version": "0.29.0",
4
4
  "description": "Lattice — phase-aware TODO graph compiler and conflict-aware orchestration runtime",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/todo-cli.mjs CHANGED
@@ -964,15 +964,21 @@ async function witnessScaffold({ repoRoot, planKey, inputRef }) {
964
964
  }
965
965
  const { queries, paths } = buildWitnessObservationQuerySet(draft);
966
966
  const collected = await collectSensorEvidence({ cwd: repoRoot, querySet: { queries } });
967
- const affectedTestsByPath = {};
967
+ const observationByPath = {};
968
968
  queries.forEach((query, index) => {
969
969
  if (query.operation !== 'affected') return;
970
970
  const entry = collected.outcomes[index]?.targets?.[0];
971
971
  // 観測できていないものを空配列へ丸めない。丸めるとdriftでcompileが落ちる。
972
- if (entry?.path_state === 'absent' || !Array.isArray(entry?.data?.affectedTests)) return;
973
- affectedTestsByPath[query.target] = [...entry.data.affectedTests];
972
+ // 不存在(absent)は観測**できている**——fsのlstat結果である。未観測と混ぜると、
973
+ // 創作境界を宣言したToDoが「まだ確かめていない」側へ落ちる(ADR 0136)。
974
+ if (!Array.isArray(entry?.data?.affectedTests) || !Array.isArray(entry?.data?.changedFiles)) return;
975
+ observationByPath[query.target] = {
976
+ state: entry.path_state === 'absent' ? 'absent' : 'present',
977
+ affectedTests: [...entry.data.affectedTests],
978
+ changedFiles: [...entry.data.changedFiles],
979
+ };
974
980
  });
975
- const { witnessSet, reasons } = buildWitnessSet({ draft, affectedTestsByPath });
981
+ const { witnessSet, reasons } = buildWitnessSet({ draft, observationByPath });
976
982
  if (witnessSet === null) {
977
983
  throw new TodoStoreError('WITNESS_SCAFFOLD_INCOMPLETE', 'witness_scaffold_incomplete', undefined, {
978
984
  reasons, next_action: 'resolve_declaration_then_retry',
@@ -18,6 +18,34 @@ const compareText = (left, right) => left < right ? -1 : left > right ? 1 : 0;
18
18
  const sortedUnique = (values) => [...new Set(values)].sort(compareText);
19
19
 
20
20
  export const WITNESS_DRAFT_SCHEMA = 'lattice.todo_witness_draft.v1';
21
+ /** 創作宣言を書ける版。v1はstringのownsだけを受け、`creates`を表現できない。 */
22
+ export const WITNESS_DRAFT_SCHEMA_V2 = 'lattice.todo_witness_draft.v2';
23
+ const DRAFT_SCHEMAS = Object.freeze([WITNESS_DRAFT_SCHEMA, WITNESS_DRAFT_SCHEMA_V2]);
24
+
25
+ /**
26
+ * ownsの1件を正規化する。
27
+ *
28
+ * v2では`{ path, creates: true }`を書ける。まだ存在しないpathを所有するToDo——新module・
29
+ * 新doc・新testの追加——は、これが無いと道具で宣言を作れない(ADR 0136)。
30
+ */
31
+ function ownEntry(value, { allowCreates }) {
32
+ if (typeof value === 'string') return isTodoRef(value) ? { target: value, creates: false } : null;
33
+ if (!allowCreates || value === null || typeof value !== 'object' || Array.isArray(value)) return null;
34
+ const keys = Object.keys(value).sort();
35
+ if (keys.length !== 2 || keys[0] !== 'creates' || keys[1] !== 'path') return null;
36
+ if (!isTodoRef(value.path) || value.creates !== true) return null;
37
+ // prefix形(末尾/)はaffectedがunresolvedを返すので、file単位に限る(ADR 0136)。
38
+ if (value.path.endsWith('/')) return null;
39
+ return { target: value.path, creates: true };
40
+ }
41
+
42
+ /** 下書きの1 taskが宣言する所有を正規化する。1件でも形が壊れていればnull。 */
43
+ export function draftOwnEntries(task, schema) {
44
+ const allowCreates = schema === WITNESS_DRAFT_SCHEMA_V2;
45
+ if (!Array.isArray(task?.owns)) return null;
46
+ const entries = task.owns.map((own) => ownEntry(own, { allowCreates }));
47
+ return entries.some((entry) => entry === null) ? null : entries;
48
+ }
21
49
 
22
50
  function reject(reasons) {
23
51
  return { witnessSet: null, queries: [], reasons: sortedUnique(reasons) };
@@ -26,7 +54,7 @@ function reject(reasons) {
26
54
  /** 下書きの形。AIが書く欄だけを持ち、観測で埋まる欄は持たない。 */
27
55
  export function validateWitnessDraft(value) {
28
56
  if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;
29
- if (value.schema !== WITNESS_DRAFT_SCHEMA) return false;
57
+ if (!DRAFT_SCHEMAS.includes(value.schema)) return false;
30
58
  if (!isTodoIdentifier(value.project_id) || !isTodoIdentifier(value.plan_key)) return false;
31
59
  if (value.capacity === null || typeof value.capacity !== 'object'
32
60
  || !Number.isSafeInteger(value.capacity.executors) || value.capacity.executors < 1) return false;
@@ -35,7 +63,7 @@ export function validateWitnessDraft(value) {
35
63
  if (entries.length === 0) return false;
36
64
  return entries.every(([taskId, task]) => isTodoIdentifier(taskId)
37
65
  && task !== null && typeof task === 'object' && !Array.isArray(task)
38
- && Array.isArray(task.owns) && task.owns.every(isTodoRef)
66
+ && draftOwnEntries(task, value.schema) !== null
39
67
  && (task.reads === undefined || (Array.isArray(task.reads) && task.reads.every(isTodoRef)))
40
68
  && (task.unknowns === undefined || (Array.isArray(task.unknowns)
41
69
  && task.unknowns.every((entry) => entry !== null && typeof entry === 'object'
@@ -52,7 +80,8 @@ function queryIdFor(index) {
52
80
 
53
81
  /** 下書きから、観測に要るquery setを組む。所有pathごとに1つのaffected queryを引く。 */
54
82
  export function buildWitnessObservationQuerySet(draft) {
55
- const paths = sortedUnique(Object.values(draft.tasks).flatMap(({ owns }) => owns));
83
+ const paths = sortedUnique(Object.values(draft.tasks)
84
+ .flatMap((task) => (draftOwnEntries(task, draft.schema) ?? []).map(({ target }) => target)));
56
85
  return {
57
86
  queries: [
58
87
  { id: 'witness-status', operation: 'status' },
@@ -69,9 +98,11 @@ export function buildWitnessObservationQuerySet(draft) {
69
98
  *
70
99
  * @param {object} options
71
100
  * @param {object} options.draft `lattice.todo_witness_draft.v1`
72
- * @param {object} options.affectedTestsByPath 所有pathごとのfresh観測
101
+ * @param {object} options.observationByPath 所有pathごとのfresh観測
102
+ * `{ state: 'absent'|'present', affectedTests: string[], changedFiles: string[] }`。
103
+ * 観測できていないpathは**欄そのものを置かない**——空で置くと不在と区別できない。
73
104
  */
74
- export function buildWitnessSet({ draft, affectedTestsByPath } = {}) {
105
+ export function buildWitnessSet({ draft, observationByPath } = {}) {
75
106
  if (!validateWitnessDraft(draft)) return reject(['draft_invalid']);
76
107
  const { paths } = buildWitnessObservationQuerySet(draft);
77
108
  const queryIdByPath = new Map(paths.map((target, index) => [target, queryIdFor(index)]));
@@ -79,21 +110,39 @@ export function buildWitnessSet({ draft, affectedTestsByPath } = {}) {
79
110
  const reasons = [];
80
111
  const manualWitness = {};
81
112
  for (const [taskId, task] of Object.entries(draft.tasks).sort(([left], [right]) => compareText(left, right))) {
82
- const owns = sortedUnique(task.owns);
113
+ const entries = draftOwnEntries(task, draft.schema) ?? [];
114
+ const owns = [...new Map(entries.map((entry) => [entry.target, entry])).values()]
115
+ .sort((left, right) => compareText(left.target, right.target));
83
116
  if (owns.length === 0) { reasons.push(`owns_empty:${taskId}`); continue; }
84
117
  // affected_testsは宣言とfresh観測をbinding単位でexact比較する。複数pathを所有すると
85
118
  // 観測集合が一致しない限り必ず落ちるので、今の契約では表現できない(2026-07-27の実測)。
86
119
  if (owns.length > 1) { reasons.push(`multiple_owned_paths_unsupported:${taskId}`); continue; }
87
- const [target] = owns;
88
- const affected = affectedTestsByPath?.[target];
120
+ const [own] = owns;
121
+ const target = own.target;
122
+ const observed = observationByPath?.[target];
89
123
  // 観測できていないことを空配列へ丸めない。丸めるとdriftでcompileが落ちる。
90
- if (!Array.isArray(affected)) { reasons.push(`affected_tests_unobserved:${target}`); continue; }
124
+ if (observed === undefined) { reasons.push(`affected_tests_unobserved:${target}`); continue; }
125
+ if (own.creates) {
126
+ // 宣言が実態と合っているかを確かめるのが道具の役目である。front endが要求する形
127
+ // (fresh absent・blast radiusが空・changedFilesが対象1件)をここで満たしておかないと、
128
+ // 通る宣言を作ったつもりでcompileで落ちる(ADR 0136)。
129
+ if (observed.state !== 'absent') { reasons.push(`creates_path_present:${target}`); continue; }
130
+ if (observed.affectedTests.length !== 0
131
+ || observed.changedFiles.length !== 1
132
+ || observed.changedFiles[0] !== target) {
133
+ reasons.push(`creates_unverified:${target}`); continue;
134
+ }
135
+ } else if (observed.state === 'absent') {
136
+ // 不存在のpathを黙って通さない。作るつもりならそう宣言する、が次の一手である。
137
+ reasons.push(`path_absent_declare_creates:${target}`); continue;
138
+ }
139
+ const affected = own.creates ? [] : observed.affectedTests;
91
140
  for (const anchor of task.concern_anchors ?? []) {
92
141
  // `within`は自分が所有している資源に限る。所有していない資源の内側に担当を主張させない。
93
- if (!owns.includes(anchor.within)) reasons.push(`anchor_outside_owned:${taskId}:${anchor.within}`);
142
+ if (anchor.within !== target) reasons.push(`anchor_outside_owned:${taskId}:${anchor.within}`);
94
143
  }
95
144
  manualWitness[taskId] = {
96
- owns: [{ kind: 'path', target }],
145
+ owns: [own.creates ? { kind: 'path', target, creates: true } : { kind: 'path', target }],
97
146
  reads: sortedUnique(task.reads ?? []),
98
147
  writes: [target],
99
148
  resources: [],