@quolu/lattice 0.104.0 → 0.106.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/README.ja.md +4 -4
- package/docs/bridge-setup.md +2 -2
- package/package.json +1 -1
- package/src/cli-help.mjs +2 -0
- package/src/project-cli.mjs +1 -1
- package/src/todo-audit-pending.mjs +8 -8
- package/src/todo-cli.mjs +18 -7
- package/src/todo-contracts.mjs +26 -2
- package/src/todo-gantt-html-style.mjs +1 -1
- package/src/todo-gantt-html.mjs +6 -6
- package/src/todo-gantt-scope.mjs +3 -3
- package/src/todo-note-store.mjs +20 -2
- package/src/todo-status.mjs +5 -5
- package/src/todo-store.mjs +311 -183
- package/src/todo-version-compact.mjs +110 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* b10-plan-copy-per-add: a retained plan version keeps only what differs from its successor (Store internal, pure).
|
|
3
|
+
*
|
|
4
|
+
* Every revision used to leave a full plan.json, a revision.json that repeats the same plan, and a snapshot.json
|
|
5
|
+
* in its version directory forever (612 revisions = 475 MB for a 415 KB plan). Once a successor is active, the
|
|
6
|
+
* predecessor's three files are redundant: `compact.json` records the reverse patch (successor plan -> this plan),
|
|
7
|
+
* the revision without its repeated `desired_plan`, and the three digests readers look up. Nothing is dropped:
|
|
8
|
+
* `reconstructPlan` rebuilds the exact canonical plan, checked against `plan_digest` before anything is deleted.
|
|
9
|
+
*/
|
|
10
|
+
import { readFile } from 'node:fs/promises';
|
|
11
|
+
import { gunzipSync, gzipSync } from 'node:zlib';
|
|
12
|
+
import { canonicalizeTodoArtifact, isTodoDigest, isTodoIdentifier, todoSelfDigest } from './todo-contracts.mjs';
|
|
13
|
+
|
|
14
|
+
export const TODO_VERSION_COMPACT_SCHEMA = 'lattice.todo_version_compact.v1';
|
|
15
|
+
export const TODO_VERSION_COMPACT_FILE = 'compact.json.gz';
|
|
16
|
+
|
|
17
|
+
const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
18
|
+
const same = (left, right) => canonicalizeTodoArtifact(left) === canonicalizeTodoArtifact(right);
|
|
19
|
+
|
|
20
|
+
/** Patch node: {"=": value} replace, {"o": {key: node}, "x": [deleted keys]} object edit, {"a": [at, deleted, inserted]} splice. */
|
|
21
|
+
export function diffJson(from, to) {
|
|
22
|
+
if (isObject(from) && isObject(to)) {
|
|
23
|
+
const o = {}; const x = Object.keys(from).filter((key) => !(key in to));
|
|
24
|
+
for (const [key, value] of Object.entries(to)) {
|
|
25
|
+
if (!(key in from)) o[key] = { '=': value };
|
|
26
|
+
else if (!same(from[key], value)) o[key] = diffJson(from[key], value);
|
|
27
|
+
}
|
|
28
|
+
return { o, x };
|
|
29
|
+
}
|
|
30
|
+
if (Array.isArray(from) && Array.isArray(to)) {
|
|
31
|
+
const keys = from.map((item) => canonicalizeTodoArtifact(item)); const next = to.map((item) => canonicalizeTodoArtifact(item));
|
|
32
|
+
let head = 0;
|
|
33
|
+
while (head < keys.length && head < next.length && keys[head] === next[head]) head += 1;
|
|
34
|
+
let tail = 0;
|
|
35
|
+
while (tail < keys.length - head && tail < next.length - head
|
|
36
|
+
&& keys[keys.length - 1 - tail] === next[next.length - 1 - tail]) tail += 1;
|
|
37
|
+
return { a: [head, keys.length - head - tail, to.slice(head, next.length - tail)] };
|
|
38
|
+
}
|
|
39
|
+
return { '=': to };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function applyJsonPatch(from, patch) {
|
|
43
|
+
if ('=' in patch) return patch['='];
|
|
44
|
+
if ('a' in patch) {
|
|
45
|
+
const [at, deleted, inserted] = patch.a;
|
|
46
|
+
return [...from.slice(0, at), ...inserted, ...from.slice(at + deleted)];
|
|
47
|
+
}
|
|
48
|
+
const result = { ...from };
|
|
49
|
+
for (const key of patch.x) delete result[key];
|
|
50
|
+
for (const [key, node] of Object.entries(patch.o)) result[key] = applyJsonPatch(from[key], node);
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The compact record of `retained` once `anchor` (its direct successor, full) is active. Null when the round trip is not exact. */
|
|
55
|
+
export function buildVersionCompact({ retained, anchor, revision }) {
|
|
56
|
+
const patch = diffJson(anchor, retained);
|
|
57
|
+
if (!same(applyJsonPatch(anchor, patch), retained) || anchor.predecessor_plan_digest !== retained.plan_digest) return null;
|
|
58
|
+
const { desired_plan: repeated, ...slim } = revision ?? {};
|
|
59
|
+
if (revision && !same(repeated, retained)) return null;
|
|
60
|
+
const record = {
|
|
61
|
+
schema: TODO_VERSION_COMPACT_SCHEMA, project_id: retained.project_id, plan_key: retained.plan_key,
|
|
62
|
+
plan_version: retained.plan_version, plan_digest: retained.plan_digest,
|
|
63
|
+
predecessor_plan_digest: retained.predecessor_plan_digest ?? null,
|
|
64
|
+
predecessor_plan_version: revision?.predecessor?.plan_version ?? null, topology_digest: retained.topology_digest,
|
|
65
|
+
anchor_plan_version: anchor.plan_version, anchor_plan_digest: anchor.plan_digest,
|
|
66
|
+
plan_patch: patch, revision: revision ? slim : null, compact_digest: '',
|
|
67
|
+
};
|
|
68
|
+
record.compact_digest = todoSelfDigest(record, 'compact_digest');
|
|
69
|
+
return record;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function validateVersionCompact(value) {
|
|
73
|
+
const keys = ['schema', 'project_id', 'plan_key', 'plan_version', 'plan_digest', 'predecessor_plan_digest', 'predecessor_plan_version',
|
|
74
|
+
'topology_digest', 'anchor_plan_version', 'anchor_plan_digest', 'plan_patch', 'revision', 'compact_digest'];
|
|
75
|
+
return isObject(value) && Object.keys(value).length === keys.length && keys.every((key) => key in value)
|
|
76
|
+
&& value.schema === TODO_VERSION_COMPACT_SCHEMA
|
|
77
|
+
&& [value.project_id, value.plan_key, value.plan_version, value.anchor_plan_version].every(isTodoIdentifier)
|
|
78
|
+
&& [value.plan_digest, value.topology_digest, value.anchor_plan_digest, value.compact_digest].every(isTodoDigest)
|
|
79
|
+
&& (value.predecessor_plan_digest === null || isTodoDigest(value.predecessor_plan_digest))
|
|
80
|
+
&& (value.predecessor_plan_version === null || isTodoIdentifier(value.predecessor_plan_version))
|
|
81
|
+
&& isObject(value.plan_patch) && (value.revision === null || isObject(value.revision))
|
|
82
|
+
&& value.compact_digest === todoSelfDigest(value, 'compact_digest');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The exact plan of `record`, from its anchor's full plan. Throws when the result does not match plan_digest's plan. */
|
|
86
|
+
export function reconstructPlan(record, anchorPlan) {
|
|
87
|
+
if (anchorPlan.plan_digest !== record.anchor_plan_digest) throw new Error('compact_anchor_mismatch');
|
|
88
|
+
const plan = applyJsonPatch(anchorPlan, record.plan_patch);
|
|
89
|
+
if (plan.plan_digest !== record.plan_digest || plan.plan_version !== record.plan_version) throw new Error('compact_plan_mismatch');
|
|
90
|
+
return plan;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The revision a compact record stood in for, with its repeated desired_plan put back Null for a genesis version. */
|
|
94
|
+
export const reconstructRevision = (record, plan) => (record.revision === null ? null : { ...record.revision, desired_plan: plan });
|
|
95
|
+
|
|
96
|
+
/** The record as stored: canonical JSON line, gzip-packed (a revision's per-task migration is long and repetitive). */
|
|
97
|
+
export const packVersionCompact = (record) => gzipSync(Buffer.from(`${canonicalizeTodoArtifact(record)}\n`, 'utf8'), { level: 9 });
|
|
98
|
+
|
|
99
|
+
/** The compact record in `directory`, or null when there is none. Throws when the file is not a canonical, digest-true record. */
|
|
100
|
+
export async function readVersionCompact(directory, maxBytes = 8 * 1024 * 1024) {
|
|
101
|
+
let packed;
|
|
102
|
+
try { packed = await readFile(`${directory}/${TODO_VERSION_COMPACT_FILE}`); } catch (error) {
|
|
103
|
+
if (error?.code === 'ENOENT') return null;
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
const text = gunzipSync(packed, { maxOutputLength: maxBytes }).toString('utf8');
|
|
107
|
+
const value = JSON.parse(text);
|
|
108
|
+
if (text !== `${canonicalizeTodoArtifact(value)}\n` || !validateVersionCompact(value)) throw new Error('compact_record_invalid');
|
|
109
|
+
return value;
|
|
110
|
+
}
|