@quolu/lattice 0.85.2 → 0.85.4

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.
@@ -51,28 +51,85 @@ function reject(reasons) {
51
51
  return { witnessSet: null, queries: [], reasons: sortedUnique(reasons) };
52
52
  }
53
53
 
54
- /** 下書きの形。AIが書く欄だけを持ち、観測で埋まる欄は持たない。 */
55
- export function validateWitnessDraft(value) {
56
- if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;
57
- if (!DRAFT_SCHEMAS.includes(value.schema)) return false;
58
- if (!isTodoIdentifier(value.project_id) || !isTodoIdentifier(value.plan_key)) return false;
59
- if (value.capacity === null || typeof value.capacity !== 'object'
60
- || !Number.isSafeInteger(value.capacity.executors) || value.capacity.executors < 1) return false;
61
- if (value.tasks === null || typeof value.tasks !== 'object' || Array.isArray(value.tasks)) return false;
54
+ const DRAFT_TOP_LEVEL_KEYS = Object.freeze(['schema', 'project_id', 'plan_key', 'capacity', 'tasks']);
55
+ const plainObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
56
+ const refList = (value) => value === undefined || (Array.isArray(value) && value.every(isTodoRef));
57
+
58
+ /** 1 taskの下書きの最初の違反(相対path)、無ければnull。validateWitnessDraftと同じ規則。 */
59
+ function explainDraftTask(task, schema) {
60
+ if (!plainObject(task)) return { reason: 'not_an_object', path: '' };
61
+ if (draftOwnEntries(task, schema) === null) return { reason: 'invalid_owns', path: '/owns' };
62
+ for (const key of ['reads', 'writes']) {
63
+ if (!refList(task[key])) return { reason: 'invalid_repo_relative_paths', path: `/${key}` };
64
+ }
65
+ if (task.unknowns !== undefined && !(Array.isArray(task.unknowns) && task.unknowns.every((entry) => plainObject(entry)
66
+ && isTodoIdentifier(entry.kind) && typeof entry.ref === 'string' && entry.ref.length > 0))) {
67
+ return { reason: 'invalid_unknowns', path: '/unknowns' };
68
+ }
69
+ if (task.concern_anchors !== undefined && !(Array.isArray(task.concern_anchors)
70
+ && task.concern_anchors.every((anchor) => plainObject(anchor) && isTodoRef(anchor.within)
71
+ && Array.isArray(anchor.symbols) && anchor.symbols.length > 0 && anchor.symbols.every(isTodoIdentifier)))) {
72
+ return { reason: 'invalid_concern_anchors', path: '/concern_anchors' };
73
+ }
74
+ return null;
75
+ }
76
+
77
+ /**
78
+ * 下書きの形。AIが書く欄だけを持ち、観測で埋まる欄は持たない。
79
+ * a32: 拒否は最初に落ちた欄(JSON pointer)と理由、topの欠けたkeyを名指す。
80
+ */
81
+ export function explainWitnessDraft(value) {
82
+ const reject = (reason, at, extra = {}) => ({ valid: false, reason, path: at, ...extra });
83
+ if (!plainObject(value)) return reject('not_an_object', '/');
84
+ const missing = DRAFT_TOP_LEVEL_KEYS.filter((key) => !Object.hasOwn(value, key));
85
+ if (missing.length > 0) return reject('missing_top_level_keys', '/', { missing_keys: missing });
86
+ if (!DRAFT_SCHEMAS.includes(value.schema)) return reject('schema_mismatch', '/schema', { expected: DRAFT_SCHEMAS });
87
+ if (!isTodoIdentifier(value.project_id)) return reject('invalid_identifier', '/project_id');
88
+ if (!isTodoIdentifier(value.plan_key)) return reject('invalid_identifier', '/plan_key');
89
+ if (!plainObject(value.capacity) || !Number.isSafeInteger(value.capacity.executors) || value.capacity.executors < 1) {
90
+ return reject('invalid_capacity', '/capacity/executors');
91
+ }
92
+ if (!plainObject(value.tasks)) return reject('not_an_object', '/tasks');
62
93
  const entries = Object.entries(value.tasks);
63
- if (entries.length === 0) return false;
64
- return entries.every(([taskId, task]) => isTodoIdentifier(taskId)
65
- && task !== null && typeof task === 'object' && !Array.isArray(task)
66
- && draftOwnEntries(task, value.schema) !== null
67
- && (task.reads === undefined || (Array.isArray(task.reads) && task.reads.every(isTodoRef)))
68
- && (task.writes === undefined || (Array.isArray(task.writes) && task.writes.every(isTodoRef)))
69
- && (task.unknowns === undefined || (Array.isArray(task.unknowns)
70
- && task.unknowns.every((entry) => entry !== null && typeof entry === 'object'
71
- && isTodoIdentifier(entry.kind) && typeof entry.ref === 'string' && entry.ref.length > 0)))
72
- && (task.concern_anchors === undefined || (Array.isArray(task.concern_anchors)
73
- && task.concern_anchors.every((anchor) => anchor !== null && typeof anchor === 'object'
74
- && isTodoRef(anchor.within) && Array.isArray(anchor.symbols)
75
- && anchor.symbols.length > 0 && anchor.symbols.every(isTodoIdentifier)))));
94
+ if (entries.length === 0) return reject('empty_tasks', '/tasks');
95
+ for (const [taskId, task] of entries) {
96
+ if (!isTodoIdentifier(taskId)) return reject('invalid_identifier', `/tasks/${taskId}`);
97
+ const failure = explainDraftTask(task, value.schema);
98
+ if (failure !== null) return reject(failure.reason, `/tasks/${taskId}${failure.path}`);
99
+ }
100
+ return { valid: true };
101
+ }
102
+
103
+ export function validateWitnessDraft(value) {
104
+ return explainWitnessDraft(value).valid;
105
+ }
106
+
107
+ /**
108
+ * a32: the starting draft for a plan: every task still to do.
109
+ * a35: a task the recorded witness set already declares starts from that declaration (the inverse of
110
+ * buildWitnessSet: owns back to draft entries, writes without the owned paths it adds), so adding one
111
+ * task after a revision does not mean rewriting the others.
112
+ */
113
+ export function witnessDraftTemplate({ projectId, planKey, taskIds, witnessSet = null }) {
114
+ const recorded = witnessSet?.plan_key === planKey ? witnessSet.manual_witness : {};
115
+ const draftTask = (witness) => {
116
+ if (witness === undefined) return { owns: [], reads: [], writes: [] };
117
+ const owned = new Set(witness.owns.map(({ target }) => target));
118
+ return {
119
+ owns: witness.owns.map(({ target, creates }) => (creates ? { path: target, creates: true } : target)),
120
+ reads: [...witness.reads],
121
+ writes: witness.writes.filter((target) => !owned.has(target)),
122
+ ...(witness.unknowns.length > 0 ? { unknowns: witness.unknowns.map(({ kind, ref }) => ({ kind, ref })) } : {}),
123
+ ...(witness.concern_anchors?.length > 0 ? {
124
+ concern_anchors: witness.concern_anchors.map(({ within, symbols }) => ({ within: within.target, symbols: [...symbols] })),
125
+ } : {}),
126
+ };
127
+ };
128
+ return {
129
+ schema: WITNESS_DRAFT_SCHEMA_V2, project_id: projectId, plan_key: planKey,
130
+ capacity: { executors: witnessSet?.plan_key === planKey ? witnessSet.capacity.executors : 1 },
131
+ tasks: Object.fromEntries([...taskIds].sort(compareText).map((taskId) => [taskId, draftTask(recorded[taskId])])),
132
+ };
76
133
  }
77
134
 
78
135
  function queryIdFor(index) {