@quolu/lattice 0.85.1 → 0.85.3

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,65 @@ 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
+ /** a32: the starting draft for a plan: every task still to do, with nothing declared yet. */
108
+ export function witnessDraftTemplate({ projectId, planKey, taskIds }) {
109
+ return {
110
+ schema: WITNESS_DRAFT_SCHEMA_V2, project_id: projectId, plan_key: planKey, capacity: { executors: 1 },
111
+ tasks: Object.fromEntries([...taskIds].sort(compareText).map((taskId) => [taskId, { owns: [], reads: [], writes: [] }])),
112
+ };
76
113
  }
77
114
 
78
115
  function queryIdFor(index) {