@quolu/lattice 0.103.0 → 0.104.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.
@@ -75,6 +75,65 @@ export function projectTodoCrossPlanDependencies(members) {
75
75
  });
76
76
  }
77
77
 
78
+ /**
79
+ * 接続 (schedule): one plan chain's connection_added events with how each ended. `ended` is 'removed' or
80
+ * 'resolved' (an add has at most one end, which Store append enforces); a state of 'removed' is not shown.
81
+ */
82
+ export function projectTodoScheduleConnections(events) {
83
+ const ends = new Map();
84
+ for (const event of events ?? []) {
85
+ if (event.kind === 'connection_removed') ends.set(event.payload.target_event_digest, { ended: 'removed', event });
86
+ if (event.kind === 'connection_resolved') ends.set(event.payload.target_event_digest, { ended: 'resolved', event });
87
+ }
88
+ return (events ?? []).filter((event) => event.kind === 'connection_added').map((event) => {
89
+ const end = ends.get(event.event_digest);
90
+ return { kind: 'schedule', task_id: event.payload.task_id, ref: event.payload.to.id,
91
+ state: end === undefined ? 'open' : end.ended === 'resolved' ? 'settled' : 'removed',
92
+ reason: event.payload.reason, since: event.recorded_at, event_digest: event.event_digest,
93
+ actor: event.actor, provenance: event.provenance, ended: end?.ended ?? null,
94
+ ...(end?.ended === 'resolved' ? { settled_at: end.event.recorded_at, evidence: end.event.payload.evidence } : {}) };
95
+ });
96
+ }
97
+
98
+ const connectionTaskKey = (projectId, planKey, taskId) => `${projectId}\0${planKey}\0${taskId}`;
99
+ export { connectionTaskKey as todoConnectionTaskKey };
100
+
101
+ /**
102
+ * 接続 per task, keyed `project\0plan\0task`: schedule connections from the plan chain, and task connections from
103
+ * today's cross-chart ledger edges (settled once that task is done or retired, open while its chart is unread).
104
+ * Pure; a removed connection is not listed. Entry: {kind, ref, state, reason, since, event_digest, settled_at?,
105
+ * evidence?, state_reason?}, sorted by since then digest.
106
+ */
107
+ export function projectTodoConnections(members, externalTasks = []) {
108
+ const external = new Map(externalTasks.map((entry) => [connectionTaskKey(entry.project_id, entry.plan_key, entry.task_id), entry]));
109
+ const byTask = new Map();
110
+ const push = (key, entry) => byTask.set(key, [...(byTask.get(key) ?? []), entry]);
111
+ for (const member of members) {
112
+ const { plan } = member;
113
+ for (const entry of projectTodoScheduleConnections(member.plan_scoped?.events)) {
114
+ if (entry.state === 'removed') continue;
115
+ const { task_id: taskId, actor: _actor, provenance: _provenance, ended: _ended, ...shown } = entry;
116
+ push(connectionTaskKey(plan.project_id, plan.plan_key, taskId), shown);
117
+ }
118
+ }
119
+ for (const dependency of projectTodoCrossPlanDependencies(members)) {
120
+ if (dependency.from.project_id === dependency.to.project_id) continue;
121
+ const { from, to } = dependency;
122
+ const source = external.get(connectionTaskKey(from.project_id, from.plan_key, from.task_id));
123
+ const settled = source?.finished === true;
124
+ push(connectionTaskKey(to.project_id, to.plan_key, to.task_id), {
125
+ kind: 'task', ref: `${from.project_id}/${from.plan_key}/${from.task_id}`,
126
+ state: settled ? 'settled' : 'open', reason: dependency.reason, since: dependency.connected_at,
127
+ event_digest: dependency.event_digest,
128
+ ...(!settled && (source === undefined || source.status === null || source.status === undefined)
129
+ ? { state_reason: source?.reason ?? 'chart_not_read' } : {}) });
130
+ }
131
+ for (const [key, entries] of byTask) {
132
+ byTask.set(key, entries.sort((left, right) => (left.since + left.event_digest < right.since + right.event_digest ? -1 : 1)));
133
+ }
134
+ return byTask;
135
+ }
136
+
78
137
  function mappedNodeRef(ref, plan, idMap) {
79
138
  if (ref.project_id !== plan.project_id || ref.plan_key !== plan.plan_key) return ref;
80
139
  return { ...ref, task_id: idMap.get(ref.task_id) ?? ref.task_id };
@@ -3,6 +3,7 @@ import {
3
3
  isPhaseTodoPlanSchema,
4
4
  isPhaselessTodoPlanSchema,
5
5
  projectTodoCrossPlanDependencies,
6
+ projectTodoScheduleConnections,
6
7
  normalizeCheckoutEol,
7
8
  isFinishedTodoTaskState,
8
9
  stepTodoReopenedPending,
@@ -1095,7 +1096,7 @@ function crossPlanDependencyTask(store, ref) {
1095
1096
  }
1096
1097
 
1097
1098
  /**
1098
- * xchart S3: only `todo dependency connect` (appendTodoEvent, `foreignFrom`) may record a predecessor on another
1099
+ * xchart S3: only `todo connect condition add` (`dependency connect --from-project`, retired alias) (appendTodoEvent, `foreignFrom`) may record a predecessor on another
1099
1100
  * chart; Plan checked it there just before. Genesis, import and connectionOnly keep refusing a foreign from.
1100
1101
  */
1101
1102
  function validateCrossPlanDependencyTransition(store, owner, input, { foreignFrom = false } = {}) {
@@ -1437,7 +1438,7 @@ function materializeImportedNarrativeAnchors(repoRoot, planInput, sources, cache
1437
1438
  if (sources === undefined) return planInput;
1438
1439
  if (!['lattice.todo_plan.v2', 'lattice.todo_plan.v6'].includes(planInput?.schema)
1439
1440
  || !Array.isArray(sources)
1440
- || sources.length > TODO_LIMITS.tasksPerPlan || !sources.every(narrativeAnchorSource)) {
1441
+ || !sources.every(narrativeAnchorSource)) {
1441
1442
  throw new TypeError('imported narrative anchor sources are invalid');
1442
1443
  }
1443
1444
  const byTask = new Map(sources.map((source) => [source.task_id, source]));
@@ -2448,6 +2449,30 @@ function resolveCanonicalTaskId(plan, requestedTaskId) {
2448
2449
  return matches[0].task_id;
2449
2450
  }
2450
2451
 
2452
+ /**
2453
+ * 接続: every write is idempotent so a retry after an unknown outcome replays to the same state. Returns
2454
+ * `{unchanged: true, target_event_digest}` (nothing is appended) for: an add whose task + schedule is already open,
2455
+ * a resolve of an entry already settled (the first evidence wins), a remove when the entry is not open. Anything
2456
+ * else that cannot apply is refused.
2457
+ */
2458
+ function validateConnectionTransition(member, event) {
2459
+ if (!['connection_added', 'connection_removed', 'connection_resolved'].includes(event.kind)) return null;
2460
+ const entries = projectTodoScheduleConnections(member.plan_scoped?.events ?? []);
2461
+ if (event.kind === 'connection_added') {
2462
+ const { task_id: taskId, to } = event.payload;
2463
+ const open = entries.find((entry) => entry.state === 'open' && entry.task_id === taskId && entry.ref === to.id);
2464
+ return open === undefined ? null : { unchanged: true, target_event_digest: open.event_digest };
2465
+ }
2466
+ const digest = event.payload.target_event_digest;
2467
+ const entry = entries.find((candidate) => candidate.event_digest === digest);
2468
+ if (entry === undefined) fail('DEPENDENCY_NOT_FOUND', 'connection_not_found', { target_event_digest: digest });
2469
+ if (event.kind === 'connection_removed') {
2470
+ return entry.state === 'open' ? null : { unchanged: true, target_event_digest: digest };
2471
+ }
2472
+ if (entry.ended === 'removed') fail('DEPENDENCY_INVALID', 'connection_already_removed', { target_event_digest: digest });
2473
+ return entry.state === 'settled' ? { unchanged: true, target_event_digest: digest } : null;
2474
+ }
2475
+
2451
2476
  export async function appendTodoEvent(options = {}) {
2452
2477
  requireWriter(options.writer, 'g5-authoring');
2453
2478
  if (typeof options.validateProspectiveEvent !== 'function') {
@@ -2482,9 +2507,15 @@ export async function appendTodoEvent(options = {}) {
2482
2507
  fail('DEPENDENCY_INVALID', 'cross_plan_dependency_already_removed', { target_event_digest: digest });
2483
2508
  }
2484
2509
  }
2510
+ const event = options.event.kind === 'connection_added'
2511
+ ? { ...options.event, payload: { ...options.event.payload,
2512
+ task_id: resolveCanonicalTaskId(member.plan, options.event.payload.task_id) } }
2513
+ : options.event;
2514
+ const unchanged = validateConnectionTransition(member, event);
2515
+ if (unchanged !== null) return { cross_plan_repairs: store.cross_plan_repairs, ...unchanged };
2485
2516
  return { cross_plan_repairs: store.cross_plan_repairs, ...await appendPlanScopedEvent({
2486
2517
  repoRoot, member, assertHeld,
2487
- input: { ...options.event, recorded_at: options.event.recorded_at ?? new Date().toISOString() },
2518
+ input: { ...event, recorded_at: event.recorded_at ?? new Date().toISOString() },
2488
2519
  }) };
2489
2520
  }
2490
2521
  const input = resolveTargetedEvent({
@@ -2683,6 +2714,14 @@ function carriedPlanScopedChain(store, previous, plan, journalRef, taskMigration
2683
2714
  payload: { from, to, reason: event.payload.reason } });
2684
2715
  pushPlanScopedEvent(successor.plan_scoped, carried);
2685
2716
  }
2717
+ // 接続: only the schedule connections still open travel; a settled or removed one equals no connection.
2718
+ for (const entry of projectTodoScheduleConnections(previous.plan_scoped?.events ?? [])) {
2719
+ const taskId = ids.get(entry.task_id);
2720
+ if (entry.state !== 'open' || taskId === undefined || taskId === 'removed') continue;
2721
+ pushPlanScopedEvent(successor.plan_scoped, buildPlanScopedEvent(successor, { kind: 'connection_added',
2722
+ actor: entry.actor, recorded_at: entry.since, provenance: entry.provenance,
2723
+ payload: { task_id: taskId, to: { kind: 'schedule', id: entry.ref }, reason: entry.reason } }));
2724
+ }
2686
2725
  return successor.plan_scoped;
2687
2726
  }
2688
2727
 
@@ -2910,7 +2949,7 @@ function buildHistoricalStart(plan, previous, input, genesis) {
2910
2949
  function historicalImportInputs(value, timestampKey) {
2911
2950
  const required = ['task_id', timestampKey, 'evidence'];
2912
2951
  const allowed = new Set([...required, 'actor', 'recorded_at', 'provenance']);
2913
- return Array.isArray(value) && value.length <= TODO_LIMITS.tasksPerPlan
2952
+ return Array.isArray(value)
2914
2953
  && value.every((entry) => entry !== null && typeof entry === 'object' && !Array.isArray(entry)
2915
2954
  && Object.getPrototypeOf(entry) === Object.prototype
2916
2955
  && required.every((key) => Object.hasOwn(entry, key))
@@ -3457,7 +3496,7 @@ async function appendPlanMembership(repoRoot, options, authored) {
3457
3496
  const full = importedCrossPlan.artifacts.find(({ sealed }) => sealed.length > 0);
3458
3497
  if (full !== undefined) {
3459
3498
  fail('STORE_WRITE_REFUSED', 'cross_plan_import_segment_full', { ref: full.ref,
3460
- next_action: 'import without this cross-plan edge, then connect it with `lattice todo dependency connect`' });
3499
+ next_action: 'import without this cross-plan edge, then add it with `lattice todo predecessor add`' });
3461
3500
  }
3462
3501
  const transactionRef = hasCrossPlanDependencies
3463
3502
  ? `${CROSS_PLAN_IMPORT_TRANSACTIONS_REF}/${transactionDigest}`