@quolu/lattice 0.115.0 → 0.117.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/bin/lattice.mjs +37 -9
- package/docs/schemas/lattice.plan_backchain.v1.schema.json +2 -2
- package/package.json +1 -1
- package/src/cli-help.mjs +9 -4
- package/src/plan-backchain.mjs +99 -46
- package/src/plan-draft.mjs +448 -0
- package/src/project-cli.mjs +3 -1
- package/src/runtime-error-reporting.mjs +119 -105
- package/src/runtime-errors.mjs +37 -9
- package/src/todo-cli.mjs +12 -7
- package/src/todo-note-store.mjs +195 -113
- package/src/todo-store.mjs +29 -14
package/src/todo-note-store.mjs
CHANGED
|
@@ -9,6 +9,7 @@ import { renamePublishedFile, syncPublishedFile } from './fs-publish.mjs';
|
|
|
9
9
|
import { normalizeCheckoutEol } from './todo-store-view.mjs';
|
|
10
10
|
import { memoByFileIdentity } from './todo-version-memo.mjs';
|
|
11
11
|
import { TODO_VERSION_COMPACT_FILE, readVersionCompact } from './todo-version-compact.mjs';
|
|
12
|
+
import { publishDerivedPlanCache, readDerivedPlanCache } from './todo-store.mjs';
|
|
12
13
|
|
|
13
14
|
import {
|
|
14
15
|
TODO_LIMITS,
|
|
@@ -338,50 +339,83 @@ function noteProjectionEntry(event, supersededBy) {
|
|
|
338
339
|
};
|
|
339
340
|
}
|
|
340
341
|
|
|
341
|
-
|
|
342
|
+
/**
|
|
343
|
+
* One migration's entries that change a task, `[from, to | null]` (null: removed). A migration lists every task of its
|
|
344
|
+
* predecessor (the writer refuses an incomplete one: predecessor_task_migration_incomplete), so an entry that keeps the id
|
|
345
|
+
* says nothing and is left out; this is what keeps the lineage small on a plan with thousands of tasks and versions.
|
|
346
|
+
*/
|
|
347
|
+
function changedTaskMoves(taskMigration, planVersion) {
|
|
348
|
+
const seen = new Set();
|
|
349
|
+
const moves = [];
|
|
350
|
+
for (const entry of taskMigration) {
|
|
351
|
+
const to = entry?.to_task_id === 'removed' ? null : entry?.to_task_id;
|
|
352
|
+
if (!isTodoIdentifier(entry?.from_task_id) || !(to === null || isTodoIdentifier(to)) || seen.has(entry.from_task_id)) {
|
|
353
|
+
fail('NOTE_PROJECTION_INVALID', 'note_task_migration_invalid', { plan_version: planVersion });
|
|
354
|
+
}
|
|
355
|
+
seen.add(entry.from_task_id);
|
|
356
|
+
if (to !== entry.from_task_id) moves.push([entry.from_task_id, to]);
|
|
357
|
+
}
|
|
358
|
+
return moves;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* w2-impl: where a note's origin (plan version, task) ends up at the head, without walking every later version per note.
|
|
363
|
+
* `versions` runs from the oldest version to the head along the predecessor chain; `moves` are `[index, from, to | null]`:
|
|
364
|
+
* the migration from versions[index] to versions[index + 1] renamed or removed `from`, in ascending index order.
|
|
365
|
+
*/
|
|
366
|
+
class NoteLineage {
|
|
367
|
+
constructor(versions, moves) {
|
|
368
|
+
this.versions = versions;
|
|
369
|
+
this.positions = new Map(versions.map((version, index) => [version, index]));
|
|
370
|
+
this.moves = new Map();
|
|
371
|
+
for (const [index, from, to] of moves) {
|
|
372
|
+
if (!this.moves.has(from)) this.moves.set(from, []);
|
|
373
|
+
this.moves.get(from).push([index, to]);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** The head task id that `taskId` of `version` became (callers check it is still in the plan), or null: removed, or a version off the chain. */
|
|
378
|
+
resolve(version, taskId) {
|
|
379
|
+
let index = this.positions.get(version);
|
|
380
|
+
if (index === undefined) return null;
|
|
381
|
+
for (let task = taskId; ;) {
|
|
382
|
+
// shortcut: linear in the task's own moves (a few), upgrade if one task is renamed thousands of times
|
|
383
|
+
const move = this.moves.get(task)?.find(([at]) => at >= index);
|
|
384
|
+
if (move === undefined) return task;
|
|
385
|
+
if (move[1] === null) return null;
|
|
386
|
+
[index, task] = [move[0] + 1, move[1]];
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/** The lineage of a hand-made migration list (the pure projector's input): its chain ending at `head`; versions off that chain resolve to null. */
|
|
392
|
+
function lineageFromMigrations(migrations, head) {
|
|
342
393
|
if (!Array.isArray(migrations)) throw new TypeError('todo note migrations must be an array');
|
|
343
|
-
const
|
|
394
|
+
const byTo = new Map();
|
|
395
|
+
const froms = new Set();
|
|
344
396
|
for (const migration of migrations) {
|
|
345
397
|
if (!exactRecord(migration, ['from_plan_version', 'to_plan_version', 'task_migration'])
|
|
346
398
|
|| !isTodoIdentifier(migration.from_plan_version)
|
|
347
399
|
|| !isTodoIdentifier(migration.to_plan_version)
|
|
348
400
|
|| migration.from_plan_version === migration.to_plan_version
|
|
349
401
|
|| !Array.isArray(migration.task_migration)
|
|
350
|
-
||
|
|
402
|
+
|| froms.has(migration.from_plan_version) || byTo.has(migration.to_plan_version)) {
|
|
351
403
|
fail('NOTE_PROJECTION_INVALID', 'note_migration_history_invalid');
|
|
352
404
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|| !isTodoIdentifier(entry.from_task_id)
|
|
357
|
-
|| !(entry.to_task_id === null || isTodoIdentifier(entry.to_task_id))
|
|
358
|
-
|| taskMap.has(entry.from_task_id)) {
|
|
359
|
-
fail('NOTE_PROJECTION_INVALID', 'note_task_migration_invalid');
|
|
360
|
-
}
|
|
361
|
-
taskMap.set(entry.from_task_id, entry.to_task_id === 'removed' ? null : entry.to_task_id);
|
|
362
|
-
}
|
|
363
|
-
index.set(migration.from_plan_version, {
|
|
364
|
-
toPlanVersion: migration.to_plan_version, taskMap,
|
|
365
|
-
});
|
|
405
|
+
froms.add(migration.from_plan_version);
|
|
406
|
+
byTo.set(migration.to_plan_version, { from: migration.from_plan_version,
|
|
407
|
+
moves: changedTaskMoves(migration.task_migration, migration.to_plan_version) });
|
|
366
408
|
}
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
if (seen.has(version)) fail('NOTE_PROJECTION_INVALID', 'note_migration_cycle');
|
|
376
|
-
seen.add(version);
|
|
377
|
-
const step = migrations.get(version);
|
|
378
|
-
if (step === undefined) return { kind: 'archived' };
|
|
379
|
-
const nextTaskId = step.taskMap.get(taskId);
|
|
380
|
-
if (nextTaskId === undefined || nextTaskId === null) return { kind: 'archived' };
|
|
381
|
-
version = step.toPlanVersion;
|
|
382
|
-
taskId = nextTaskId;
|
|
409
|
+
const versions = [head];
|
|
410
|
+
const steps = [];
|
|
411
|
+
for (let version = head; byTo.has(version);) {
|
|
412
|
+
const step = byTo.get(version);
|
|
413
|
+
if (versions.includes(step.from)) fail('NOTE_PROJECTION_INVALID', 'note_migration_cycle');
|
|
414
|
+
versions.unshift(step.from);
|
|
415
|
+
steps.unshift(step.moves);
|
|
416
|
+
version = step.from;
|
|
383
417
|
}
|
|
384
|
-
return
|
|
418
|
+
return new NoteLineage(versions, steps.flatMap((moves, index) => moves.map(([from, to]) => [index, from, to])));
|
|
385
419
|
}
|
|
386
420
|
|
|
387
421
|
/**
|
|
@@ -401,7 +435,8 @@ export function projectTodoNoteContext(options = {}) {
|
|
|
401
435
|
throw new TypeError('todo note projection options invalid');
|
|
402
436
|
}
|
|
403
437
|
const currentTaskIds = new Set(options.currentTaskIds);
|
|
404
|
-
const
|
|
438
|
+
const lineage = options.migrations instanceof NoteLineage
|
|
439
|
+
? options.migrations : lineageFromMigrations(options.migrations, options.currentPlanVersion);
|
|
405
440
|
// 訂正はscopeを跨げないので、supersedeの追跡もchainごとに閉じる。
|
|
406
441
|
const supersededBy = new Map();
|
|
407
442
|
for (const event of [...options.events, ...options.planEvents]) {
|
|
@@ -418,11 +453,9 @@ export function projectTodoNoteContext(options = {}) {
|
|
|
418
453
|
const planSequence = sequenceOf(options.planEvents);
|
|
419
454
|
for (const event of options.events) {
|
|
420
455
|
const entry = noteProjectionEntry(event, supersededBy.get(event.event_digest));
|
|
421
|
-
const target =
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
if (target.kind === 'archived') archived.push(entry);
|
|
425
|
-
else if (target.taskId === options.currentTaskId) taskCurrent.push(entry);
|
|
456
|
+
const target = lineage.resolve(event.plan_version, event.task_id);
|
|
457
|
+
if (target === null || !currentTaskIds.has(target)) archived.push(entry);
|
|
458
|
+
else if (target === options.currentTaskId) taskCurrent.push(entry);
|
|
426
459
|
}
|
|
427
460
|
// plan noteは特定のtaskに属さないので、task migrationで宛先を失うことがない。全taskの
|
|
428
461
|
// contextへ載せる——工程レベルの義務は「次に着手する誰か」へ届くべきもので、誰が着手するかは
|
|
@@ -498,80 +531,125 @@ async function readCanonicalJson(ref, { missing = false } = {}) {
|
|
|
498
531
|
return value;
|
|
499
532
|
}
|
|
500
533
|
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
const
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
const
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
'note_historical_plan_invalid', { plan_version: entry.name }));
|
|
524
|
-
if (compact !== null) {
|
|
525
|
-
if (compact.plan_key !== planKey || compact.plan_version !== entry.name) {
|
|
526
|
-
fail('NOTE_PROJECTION_INVALID', 'note_historical_plan_invalid', { plan_version: entry.name });
|
|
527
|
-
}
|
|
528
|
-
const migration = compact.revision?.task_migration;
|
|
529
|
-
if (compact.revision === null) return { migration: null };
|
|
530
|
-
if (compact.predecessor_plan_version === null || !Array.isArray(migration)) {
|
|
531
|
-
fail('NOTE_PROJECTION_INVALID', 'note_historical_revision_invalid', { plan_version: entry.name });
|
|
532
|
-
}
|
|
533
|
-
return { migration: { from_plan_version: compact.predecessor_plan_version, to_plan_version: entry.name,
|
|
534
|
-
task_migration: migration.map(({ from_task_id: fromTaskId, to_task_id: toTaskId }) => ({
|
|
535
|
-
from_task_id: fromTaskId, to_task_id: toTaskId })) } };
|
|
536
|
-
}
|
|
537
|
-
// A revision directory is published before manifest activation. A crash during
|
|
538
|
-
// that window can leave an unreferenced directory containing only a journal
|
|
539
|
-
// stub. It is not a historical plan until its canonical plan artifact exists.
|
|
540
|
-
// Do not make that unreachable residue poison note projection; if a note event
|
|
541
|
-
// names this version, the origin-version check below still fails closed.
|
|
542
|
-
const plan = await readCanonicalJson(planPath, { missing: true });
|
|
543
|
-
if (plan === null) return null;
|
|
544
|
-
if (!validateTodoPlan(plan) || plan.plan_key !== planKey || plan.plan_version !== entry.name) {
|
|
545
|
-
fail('NOTE_PROJECTION_INVALID', 'note_historical_plan_invalid', { plan_version: entry.name });
|
|
534
|
+
/**
|
|
535
|
+
* One retained version as the lineage needs it: `{predecessor, moves}` (its predecessor's version name, null for the first
|
|
536
|
+
* version, and `changedTaskMoves` of the migration into it), or null when the version does not exist.
|
|
537
|
+
*/
|
|
538
|
+
function readVersionMigration(base, name, planKey) {
|
|
539
|
+
const versionRoot = path.join(base, name);
|
|
540
|
+
const planPath = path.join(versionRoot, 'plan.json');
|
|
541
|
+
const revisionPath = path.join(versionRoot, 'revision.json');
|
|
542
|
+
const compactPath = path.join(versionRoot, TODO_VERSION_COMPACT_FILE);
|
|
543
|
+
// perf-store: validated once per process while the files are unchanged (todo-version-memo). w2: the memo holds only the
|
|
544
|
+
// moves, not the whole 3,321-entry task_migration (that array times 4,204 versions was the 1.6 GB).
|
|
545
|
+
return memoByFileIdentity([planPath, revisionPath, compactPath], async () => {
|
|
546
|
+
const record = (predecessor, taskMigration) => {
|
|
547
|
+
if (predecessor === name) fail('NOTE_PROJECTION_INVALID', 'note_migration_history_invalid');
|
|
548
|
+
return { predecessor, moves: changedTaskMoves(taskMigration, name) };
|
|
549
|
+
};
|
|
550
|
+
// b10: a superseded version keeps compact.json.gz (its revision, minus the plan it repeats) instead of plan.json/revision.json.
|
|
551
|
+
const compact = await readVersionCompact(versionRoot, undefined, { verify: false }).catch(() => fail('NOTE_PROJECTION_INVALID',
|
|
552
|
+
'note_historical_plan_invalid', { plan_version: name }));
|
|
553
|
+
if (compact !== null) {
|
|
554
|
+
if (compact.plan_key !== planKey || compact.plan_version !== name) {
|
|
555
|
+
fail('NOTE_PROJECTION_INVALID', 'note_historical_plan_invalid', { plan_version: name });
|
|
546
556
|
}
|
|
547
|
-
|
|
548
|
-
if (
|
|
549
|
-
|
|
550
|
-
|| revision.plan_key !== planKey || revision.predecessor?.plan_version === undefined
|
|
551
|
-
|| !Array.isArray(revision.task_migration)) {
|
|
552
|
-
fail('NOTE_PROJECTION_INVALID', 'note_historical_revision_invalid', { plan_version: entry.name });
|
|
557
|
+
if (compact.revision === null) return { predecessor: null, moves: [] };
|
|
558
|
+
if (compact.predecessor_plan_version === null || !Array.isArray(compact.revision?.task_migration)) {
|
|
559
|
+
fail('NOTE_PROJECTION_INVALID', 'note_historical_revision_invalid', { plan_version: name });
|
|
553
560
|
}
|
|
554
|
-
return
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
+
return record(compact.predecessor_plan_version, compact.revision.task_migration);
|
|
562
|
+
}
|
|
563
|
+
// A revision directory is published before manifest activation. A crash during
|
|
564
|
+
// that window can leave an unreferenced directory containing only a journal
|
|
565
|
+
// stub. It is not a historical plan until its canonical plan artifact exists.
|
|
566
|
+
// Do not make that unreachable residue poison note projection; if a note event
|
|
567
|
+
// names this version, the origin-version check below still fails closed.
|
|
568
|
+
const plan = await readCanonicalJson(planPath, { missing: true });
|
|
569
|
+
if (plan === null) return null;
|
|
570
|
+
if (!validateTodoPlan(plan) || plan.plan_key !== planKey || plan.plan_version !== name) {
|
|
571
|
+
fail('NOTE_PROJECTION_INVALID', 'note_historical_plan_invalid', { plan_version: name });
|
|
572
|
+
}
|
|
573
|
+
const revision = await readCanonicalJson(revisionPath, { missing: true });
|
|
574
|
+
if (revision === null) return { predecessor: null, moves: [] };
|
|
575
|
+
if (!(validateTodoRevision(revision) || validatePhaseTodoRevision(revision))
|
|
576
|
+
|| revision.plan_key !== planKey || revision.predecessor?.plan_version === undefined
|
|
577
|
+
|| !Array.isArray(revision.task_migration)) {
|
|
578
|
+
fail('NOTE_PROJECTION_INVALID', 'note_historical_revision_invalid', { plan_version: name });
|
|
579
|
+
}
|
|
580
|
+
return record(revision.predecessor.plan_version, revision.task_migration);
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
const LINEAGE_CACHE_DIRECTORY = '.lattice/generated/store-note-lineage';
|
|
585
|
+
const LINEAGE_CACHE_SCHEMA = 'lattice.todo_note_lineage_cache.v1';
|
|
586
|
+
// shortcut: a shorter chain reads in well under 0.1 s, so an ordinary `todo start` on a small plan leaves no file in a user's git tree; upgrade if a short chain gets slow
|
|
587
|
+
const LINEAGE_CACHE_MIN_VERSIONS = 32;
|
|
588
|
+
|
|
589
|
+
/** The cached `{versions, moves}` of a plan's lineage up to some head, or null for any miss (absent, unreadable, wrong plan, bad digest, bad shape). */
|
|
590
|
+
async function readLineageCache(root, planKey) {
|
|
591
|
+
const body = await readDerivedPlanCache(root, LINEAGE_CACHE_DIRECTORY, LINEAGE_CACHE_SCHEMA, planKey);
|
|
592
|
+
if (body === null || !Array.isArray(body.versions) || body.versions.length === 0
|
|
593
|
+
|| !body.versions.every(isTodoIdentifier) || new Set(body.versions).size !== body.versions.length
|
|
594
|
+
|| !Array.isArray(body.moves) || !body.moves.every((move, at) => Array.isArray(move) && move.length === 3
|
|
595
|
+
&& Number.isInteger(move[0]) && move[0] >= 0 && move[0] < body.versions.length - 1
|
|
596
|
+
&& (at === 0 || move[0] >= body.moves[at - 1][0])
|
|
597
|
+
&& isTodoIdentifier(move[1]) && (move[2] === null || isTodoIdentifier(move[2])))) return null;
|
|
598
|
+
return body;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* w2-impl: the lineage of `headVersion`, from the head back along its predecessors. Derived cache
|
|
603
|
+
* `.lattice/generated/store-note-lineage/<plan_key>.json` (best effort, never truth: any miss reads the chain; a failed write
|
|
604
|
+
* never fails the read; written only for a chain of at least LINEAGE_CACHE_MIN_VERSIONS versions): a cached head equal
|
|
605
|
+
* to this one costs no version read, a head some revisions later reads only the versions after the cached head. Only a chain
|
|
606
|
+
* that reaches the first version (or the cached head, itself rooted) is cached, so a version directory that is missing for a
|
|
607
|
+
* while can never leave a short lineage behind. Versions off the head's chain
|
|
608
|
+
* (unpublished residue) are not read; a note naming a version that does not exist at all is still refused.
|
|
609
|
+
*/
|
|
610
|
+
async function readNoteLineage(repoRoot, planKey, headVersion, eventVersions) {
|
|
611
|
+
const base = path.resolve(repoRoot, '.lattice/todo/plans', planKey);
|
|
612
|
+
const lineage = await memoByFileIdentity([path.join(base, headVersion, 'plan.json')], async () => {
|
|
613
|
+
const cached = await readLineageCache(repoRoot, planKey);
|
|
614
|
+
const stop = cached?.versions.at(-1) ?? null;
|
|
615
|
+
if (stop === headVersion) return new NoteLineage(cached.versions, cached.moves);
|
|
616
|
+
const fresh = []; // newest first
|
|
617
|
+
for (let name = headVersion, seen = new Set(); name !== null && name !== stop;) {
|
|
618
|
+
if (seen.has(name)) fail('NOTE_PROJECTION_INVALID', 'note_migration_cycle');
|
|
619
|
+
seen.add(name);
|
|
620
|
+
const record = await readVersionMigration(base, name, planKey);
|
|
621
|
+
if (record === null) break;
|
|
622
|
+
fresh.push({ name, ...record });
|
|
623
|
+
name = record.predecessor;
|
|
624
|
+
}
|
|
625
|
+
fresh.reverse();
|
|
626
|
+
const extends_ = stop !== null && fresh[0]?.predecessor === stop;
|
|
627
|
+
const rooted = extends_ || fresh[0]?.predecessor === null;
|
|
628
|
+
const versions = [...(extends_ ? cached.versions : []), ...fresh.map(({ name }) => name)];
|
|
629
|
+
const moves = extends_ ? [...cached.moves] : [];
|
|
630
|
+
fresh.forEach(({ moves: changed }, at) => {
|
|
631
|
+
const index = (extends_ ? cached.versions.length : 0) + at - 1; // the migration into this version
|
|
632
|
+
if (index >= 0) for (const [from, to] of changed) moves.push([index, from, to]);
|
|
561
633
|
});
|
|
562
|
-
if (
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
634
|
+
if (rooted && fresh.length > 0 && versions.length >= LINEAGE_CACHE_MIN_VERSIONS) {
|
|
635
|
+
await publishDerivedPlanCache(repoRoot, LINEAGE_CACHE_DIRECTORY, { schema: LINEAGE_CACHE_SCHEMA, plan_key: planKey,
|
|
636
|
+
versions, moves }).catch(() => {});
|
|
637
|
+
}
|
|
638
|
+
return new NoteLineage(versions, moves);
|
|
639
|
+
}, 'note-lineage');
|
|
640
|
+
for (const version of eventVersions) {
|
|
641
|
+
if (!lineage.positions.has(version) && await readVersionMigration(base, version, planKey) === null) {
|
|
642
|
+
fail('NOTE_PROJECTION_INVALID', 'note_origin_plan_version_unknown');
|
|
643
|
+
}
|
|
569
644
|
}
|
|
570
|
-
return
|
|
645
|
+
return lineage;
|
|
571
646
|
}
|
|
572
647
|
|
|
573
|
-
/**
|
|
574
|
-
|
|
648
|
+
/**
|
|
649
|
+
* active store memberと歴史revisionから、1 taskのnote contextの素材を一回で読む。`project(extra)`がcontextを作る。
|
|
650
|
+
* `extra`は読んだ後に積んだtask note(appendNoteが今書いたもの)で、素材を読み直さずに入れる。
|
|
651
|
+
*/
|
|
652
|
+
export async function readTodoNoteSource(options = {}) {
|
|
575
653
|
if (!exactRecord(options, ['repoRoot', 'store', 'planKey', 'taskId'])
|
|
576
654
|
|| !isTodoIdentifier(options.planKey) || !isTodoIdentifier(options.taskId)
|
|
577
655
|
|| options.store === null || typeof options.store !== 'object') {
|
|
@@ -590,18 +668,22 @@ export async function readTodoNoteContext(options = {}) {
|
|
|
590
668
|
});
|
|
591
669
|
const chain = await readTodoNoteEvents({ repoRoot, planKey: options.planKey });
|
|
592
670
|
const planChain = await readTodoNoteEvents({ repoRoot, planKey: options.planKey, scope: 'plan' });
|
|
593
|
-
const
|
|
671
|
+
const lineage = await readNoteLineage(repoRoot, options.planKey, member.plan.plan_version,
|
|
594
672
|
new Set(chain.events.map(({ plan_version: version }) => version)));
|
|
595
|
-
return projectTodoNoteContext({
|
|
673
|
+
return { project: (extra = []) => projectTodoNoteContext({
|
|
596
674
|
projectId: member.plan.project_id,
|
|
597
675
|
planKey: options.planKey,
|
|
598
676
|
currentPlanVersion: member.plan.plan_version,
|
|
599
677
|
currentTaskId: task.task_id,
|
|
600
678
|
currentTaskIds: member.plan.tasks.map(({ task_id: taskId }) => taskId),
|
|
601
|
-
events: chain.events,
|
|
679
|
+
events: [...chain.events, ...extra],
|
|
602
680
|
planEvents: planChain.events,
|
|
603
|
-
migrations,
|
|
604
|
-
});
|
|
681
|
+
migrations: lineage,
|
|
682
|
+
}) };
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
export async function readTodoNoteContext(options = {}) {
|
|
686
|
+
return (await readTodoNoteSource(options)).project();
|
|
605
687
|
}
|
|
606
688
|
|
|
607
689
|
/**
|
|
@@ -676,7 +758,7 @@ export async function readTodoNoteContextsForPlan(options = {}) {
|
|
|
676
758
|
});
|
|
677
759
|
const chain = await readTodoNoteEvents({ repoRoot, planKey: options.planKey });
|
|
678
760
|
const planChain = await readTodoNoteEvents({ repoRoot, planKey: options.planKey, scope: 'plan' });
|
|
679
|
-
const migrations = await
|
|
761
|
+
const migrations = await readNoteLineage(repoRoot, options.planKey, member.plan.plan_version,
|
|
680
762
|
new Set(chain.events.map(({ plan_version: version }) => version)));
|
|
681
763
|
const currentTaskIds = member.plan.tasks.map(({ task_id: taskId }) => taskId);
|
|
682
764
|
const projected = member.plan.tasks.map((task) => projectTodoNoteContext({
|
package/src/todo-store.mjs
CHANGED
|
@@ -2039,36 +2039,49 @@ function taskAddedGenesis(event) {
|
|
|
2039
2039
|
}
|
|
2040
2040
|
|
|
2041
2041
|
const TASK_ADDED_CACHE_DIRECTORY = '.lattice/generated/store-task-added';
|
|
2042
|
-
const TASK_ADDED_CACHE_SCHEMA = 'lattice.todo_task_added_cache.
|
|
2042
|
+
const TASK_ADDED_CACHE_SCHEMA = 'lattice.todo_task_added_cache.v2'; // v1 held null for plan-create tasks; a v1 file is a miss
|
|
2043
2043
|
|
|
2044
|
-
/**
|
|
2045
|
-
|
|
2044
|
+
/**
|
|
2045
|
+
* A derived per-plan cache file `<directory>/<plan_key>.json` (a body plus its `cache_digest`), or null for any miss: absent,
|
|
2046
|
+
* unreadable, wrong schema or plan, bad digest. Derived data is never truth: a miss is a recomputation (n3, w2).
|
|
2047
|
+
*/
|
|
2048
|
+
export async function readDerivedPlanCache(root, directory, schema, planKey) {
|
|
2046
2049
|
try {
|
|
2047
|
-
const bytes = await readFile(path.resolve(root,
|
|
2050
|
+
const bytes = await readFile(path.resolve(root, directory, `${planKey}.json`));
|
|
2048
2051
|
if (bytes.length > TODO_LIMITS.snapshotBytes) return null;
|
|
2049
2052
|
const { cache_digest: digest, ...body } = JSON.parse(bytes.toString('utf8'));
|
|
2050
|
-
|
|
2051
|
-
|| body.tasks === null || typeof body.tasks !== 'object' || digest !== sha256Bytes(Buffer.from(JSON.stringify(body), 'utf8'))) return null;
|
|
2052
|
-
return { head: body.head_plan_digest, tasks: new Map(Object.entries(body.tasks)) };
|
|
2053
|
+
return body.schema === schema && body.plan_key === planKey && digest === sha256Bytes(Buffer.from(JSON.stringify(body), 'utf8')) ? body : null;
|
|
2053
2054
|
} catch { return null; }
|
|
2054
2055
|
}
|
|
2055
2056
|
|
|
2056
|
-
|
|
2057
|
+
/** Atomically writes `body` (with `schema` and `plan_key`) as the derived cache of its plan. Callers ignore a failure: a lost write is a later recomputation. */
|
|
2058
|
+
export async function publishDerivedPlanCache(repoRoot, directory, body) {
|
|
2057
2059
|
const root = await canonicalRepoRoot(repoRoot); // macOS tmp is a symlink: ensureGeneratedDirectory wants the real path
|
|
2058
|
-
const ref = `${
|
|
2060
|
+
const ref = `${directory}/${body.plan_key}.json`;
|
|
2059
2061
|
await ensureGeneratedDirectory(root, ref);
|
|
2060
|
-
const body = { schema: TASK_ADDED_CACHE_SCHEMA, plan_key: plan.plan_key, head_plan_digest: plan.plan_digest,
|
|
2061
|
-
tasks: Object.fromEntries(plan.tasks.map(({ task_id: id }) => [id, added.has(id) ? [added.get(id).at, added.get(id).agent] : null])) };
|
|
2062
2062
|
const text = JSON.stringify(body);
|
|
2063
2063
|
await atomicWrite(path.resolve(root, ref), Buffer.from(`${text.slice(0, -1)},"cache_digest":"${sha256Bytes(Buffer.from(text, 'utf8'))}"}`, 'utf8'));
|
|
2064
2064
|
}
|
|
2065
2065
|
|
|
2066
|
+
/** The cached `task_id -> [at, agent] | null` of one plan head, or null for any miss. */
|
|
2067
|
+
async function readTaskAddedCache(root, planKey) {
|
|
2068
|
+
const body = await readDerivedPlanCache(root, TASK_ADDED_CACHE_DIRECTORY, TASK_ADDED_CACHE_SCHEMA, planKey);
|
|
2069
|
+
if (body === null || typeof body.head_plan_digest !== 'string' || body.tasks === null || typeof body.tasks !== 'object') return null;
|
|
2070
|
+
return { head: body.head_plan_digest, tasks: new Map(Object.entries(body.tasks)) };
|
|
2071
|
+
}
|
|
2072
|
+
|
|
2073
|
+
function publishTaskAddedCache(repoRoot, plan, added) {
|
|
2074
|
+
return publishDerivedPlanCache(repoRoot, TASK_ADDED_CACHE_DIRECTORY, { schema: TASK_ADDED_CACHE_SCHEMA, plan_key: plan.plan_key,
|
|
2075
|
+
head_plan_digest: plan.plan_digest,
|
|
2076
|
+
tasks: Object.fromEntries(plan.tasks.map(({ task_id: id }) => [id, added.has(id) ? [added.get(id).at, added.get(id).agent] : null])) });
|
|
2077
|
+
}
|
|
2078
|
+
|
|
2066
2079
|
/**
|
|
2067
2080
|
* n3-store-task-added: who added each task and when, for the member's active plan, from the retained revisions. A task is
|
|
2068
2081
|
* new in a version when it is not a `to_task_id` of that version's genesis `task_migration`; a carried one follows
|
|
2069
2082
|
* `to_task_id` -> `from_task_id` into the predecessor (two from ids onto one id: the first wins). One pass back along
|
|
2070
2083
|
* `predecessor_plan_digest` resolves every task: `{at, agent}` = that genesis's `recorded_at` and `actor.agent` (null
|
|
2071
|
-
* when absent). A task that came with plan create
|
|
2084
|
+
* when absent). A task that came with plan create gets the first version's genesis (the plan's creator). Read only for the Store: a missing or unreadable version ends
|
|
2072
2085
|
* the walk and leaves the tasks not yet resolved without an entry. A walk that reached the first version is kept in the
|
|
2073
2086
|
* derived `.lattice/generated/store-task-added/<plan_key>.json` (best effort, never truth: a miss walks), so the next
|
|
2074
2087
|
* revision's read stops at the cached head after its own genesis. Memoized per process by the active plan.json's identity.
|
|
@@ -2079,7 +2092,9 @@ export async function readTodoTaskAdded({ repoRoot = process.cwd(), member }) {
|
|
|
2079
2092
|
return memoByFileIdentity([path.resolve(root, member.descriptor.plan_ref)], async () => {
|
|
2080
2093
|
const added = new Map();
|
|
2081
2094
|
let genesis = taskAddedGenesis(member.journal.events[0]);
|
|
2082
|
-
if (genesis === null
|
|
2095
|
+
if (genesis === null) return added;
|
|
2096
|
+
const created = (tasks) => { for (const task of tasks) added.set(task, { at: genesis.at, agent: genesis.agent }); };
|
|
2097
|
+
if (genesis.predecessor === null) { created(plan.tasks.map(({ task_id: id }) => id)); return added; }
|
|
2083
2098
|
const cache = await readTaskAddedCache(root, plan.plan_key);
|
|
2084
2099
|
const fromCache = (id, tasks) => {
|
|
2085
2100
|
const entry = cache.tasks.get(id);
|
|
@@ -2112,8 +2127,8 @@ export async function readTodoTaskAdded({ repoRoot = process.cwd(), member }) {
|
|
|
2112
2127
|
const predecessor = byDigest.get(genesis.predecessor);
|
|
2113
2128
|
const prior = predecessor === undefined || steps > byDigest.size ? null : await readRetainedGenesis(root, predecessor.journalRef);
|
|
2114
2129
|
if (prior === null) break;
|
|
2115
|
-
if (prior.predecessor === null) { complete = true; break; } // the first version holds no add: its tasks came with plan create
|
|
2116
2130
|
genesis = prior;
|
|
2131
|
+
if (prior.predecessor === null) { created([...open.values()].flat()); complete = true; break; } // the first version: its tasks came with plan create, by its creator
|
|
2117
2132
|
}
|
|
2118
2133
|
} catch { /* an unreadable version leaves the tasks not yet resolved without an entry */ }
|
|
2119
2134
|
if (complete) await publishTaskAddedCache(root, plan, added).catch(() => {}); // derived: a lost write is a later walk
|