@indexyz/pi-todo 0.0.0-stage → 0.1.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/CHANGELOG.md +8 -0
- package/LICENSE +21 -0
- package/README.md +81 -2
- package/config.ts +141 -0
- package/docs/configuration.md +108 -0
- package/docs/overlay.md +128 -0
- package/docs/tool-schema.md +191 -0
- package/index.ts +302 -0
- package/locales/de.json +19 -0
- package/locales/en.json +17 -0
- package/locales/es.json +19 -0
- package/locales/fr.json +19 -0
- package/locales/pt-BR.json +19 -0
- package/locales/pt.json +19 -0
- package/locales/ru.json +19 -0
- package/locales/uk.json +19 -0
- package/locales/zh.json +19 -0
- package/package.json +87 -4
- package/state/i18n-bridge.ts +64 -0
- package/state/invariants.ts +20 -0
- package/state/replay.ts +142 -0
- package/state/selectors.ts +108 -0
- package/state/state-reducer.ts +324 -0
- package/state/state.ts +18 -0
- package/state/store.ts +125 -0
- package/state/task-graph.ts +61 -0
- package/todo-overlay.ts +255 -0
- package/todo.ts +168 -0
- package/tool/normalize-params.ts +44 -0
- package/tool/response-envelope.ts +114 -0
- package/tool/sanitize.ts +34 -0
- package/tool/types.ts +188 -0
- package/view/format.ts +202 -0
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
import type { Task, TaskAction, TaskMutationParams, TaskStatus } from "../tool/types.ts";
|
|
2
|
+
import { isTransitionValid } from "./invariants.ts";
|
|
3
|
+
import type { TaskState } from "./state.ts";
|
|
4
|
+
import { detectCycle } from "./task-graph.ts";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Reducer outcome. Closed tagged union — adding a new action requires extending
|
|
8
|
+
* this union AND the response-envelope's `formatContent` switch (compiler-
|
|
9
|
+
* enforced exhaustive). Mirrors the `Effect` pattern in
|
|
10
|
+
* `packages/rpiv-ask-user-question/state/state-reducer.ts:14-30`.
|
|
11
|
+
*
|
|
12
|
+
* `error` carries the message in-band so callers can pattern-match on
|
|
13
|
+
* `op.kind === "error"` without a side-channel boolean.
|
|
14
|
+
*/
|
|
15
|
+
export type Op =
|
|
16
|
+
| { kind: "create"; taskIds: number[] }
|
|
17
|
+
| { kind: "update"; id: number; fromStatus: TaskStatus; toStatus: TaskStatus; changed: boolean }
|
|
18
|
+
| { kind: "delete"; id: number; subject: string }
|
|
19
|
+
| { kind: "list"; statusFilter?: TaskStatus; includeDeleted: boolean }
|
|
20
|
+
| { kind: "get"; task: Task }
|
|
21
|
+
| { kind: "clear"; count: number }
|
|
22
|
+
| { kind: "error"; message: string };
|
|
23
|
+
|
|
24
|
+
export interface ApplyResult {
|
|
25
|
+
state: TaskState;
|
|
26
|
+
op: Op;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* True when `op` changed the task state — the calls whose post-commit snapshot
|
|
31
|
+
* must be persisted for replay. Reads (`list`/`get`), failures, and a no-effect
|
|
32
|
+
* `update` leave state untouched. Exhaustive over `Op` (compiler-enforced), so a
|
|
33
|
+
* new action must decide here whether it persists.
|
|
34
|
+
*/
|
|
35
|
+
export function changesState(op: Op): boolean {
|
|
36
|
+
switch (op.kind) {
|
|
37
|
+
case "create":
|
|
38
|
+
case "delete":
|
|
39
|
+
case "clear":
|
|
40
|
+
return true;
|
|
41
|
+
case "update":
|
|
42
|
+
return op.changed;
|
|
43
|
+
case "list":
|
|
44
|
+
case "get":
|
|
45
|
+
case "error":
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function errorResult(state: TaskState, message: string): ApplyResult {
|
|
51
|
+
return { state, op: { kind: "error", message } };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Per-task create fields: on a batch they belong to each `tasks[]` item. */
|
|
55
|
+
const BATCH_ITEM_FIELDS = ["description", "activeForm", "blockedBy", "owner", "metadata"] as const;
|
|
56
|
+
|
|
57
|
+
function asCreateItem(value: unknown): TaskMutationParams | undefined {
|
|
58
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return undefined;
|
|
59
|
+
return value as TaskMutationParams;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function buildCreatedTask(id: number, spec: TaskMutationParams, subject: string): Task {
|
|
63
|
+
const newTask: Task = {
|
|
64
|
+
id,
|
|
65
|
+
subject,
|
|
66
|
+
status: "pending",
|
|
67
|
+
};
|
|
68
|
+
if (spec.description) newTask.description = spec.description;
|
|
69
|
+
if (spec.activeForm) newTask.activeForm = spec.activeForm;
|
|
70
|
+
if (spec.blockedBy?.length) newTask.blockedBy = [...spec.blockedBy];
|
|
71
|
+
if (spec.owner) newTask.owner = spec.owner;
|
|
72
|
+
if (spec.metadata) newTask.metadata = { ...spec.metadata };
|
|
73
|
+
return newTask;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function validateCreateBlockedBy(
|
|
77
|
+
state: TaskState,
|
|
78
|
+
blockedBy: number[] | undefined,
|
|
79
|
+
siblingIds: ReadonlySet<number>,
|
|
80
|
+
): string | undefined {
|
|
81
|
+
if (!blockedBy?.length) return undefined;
|
|
82
|
+
for (const dep of blockedBy) {
|
|
83
|
+
if (siblingIds.has(dep)) {
|
|
84
|
+
return `blockedBy: #${dep} is another item in this batch; create the prerequisite first, then the dependent with blockedBy`;
|
|
85
|
+
}
|
|
86
|
+
const depTask = state.tasks.find((t) => t.id === dep);
|
|
87
|
+
if (!depTask) return `blockedBy: #${dep} not found`;
|
|
88
|
+
if (depTask.status === "deleted") return `blockedBy: #${dep} is deleted`;
|
|
89
|
+
}
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function sameNumberList(a: number[] | undefined, b: number[] | undefined): boolean {
|
|
94
|
+
const x = a ?? [];
|
|
95
|
+
const y = b ?? [];
|
|
96
|
+
return x.length === y.length && x.every((v, i) => v === y[i]);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function sameRecord(
|
|
100
|
+
a: Record<string, unknown> | undefined,
|
|
101
|
+
b: Record<string, unknown> | undefined,
|
|
102
|
+
): boolean {
|
|
103
|
+
return JSON.stringify(a ?? null) === JSON.stringify(b ?? null);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Did this `update` change anything? Compares the task before/after the params
|
|
108
|
+
* are applied. A no-effect update — `status` set to its current value, or any
|
|
109
|
+
* field re-sent unchanged — returns false, letting the response envelope say
|
|
110
|
+
* "No change" instead of "Updated #N". Without this, a no-op update is
|
|
111
|
+
* indistinguishable from a real mutation, which can drive a model to re-issue
|
|
112
|
+
* the same call in a loop.
|
|
113
|
+
*
|
|
114
|
+
* blockedBy is order-sensitive (the reducer preserves insertion order);
|
|
115
|
+
* metadata round-trips through JSON persistence, so JSON-equality is the
|
|
116
|
+
* operative notion of "changed".
|
|
117
|
+
*/
|
|
118
|
+
function taskChanged(before: Task, after: Task): boolean {
|
|
119
|
+
return (
|
|
120
|
+
before.subject !== after.subject ||
|
|
121
|
+
before.status !== after.status ||
|
|
122
|
+
before.description !== after.description ||
|
|
123
|
+
before.activeForm !== after.activeForm ||
|
|
124
|
+
before.owner !== after.owner ||
|
|
125
|
+
!sameNumberList(before.blockedBy, after.blockedBy) ||
|
|
126
|
+
!sameRecord(before.metadata, after.metadata)
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Pure reducer: (state, action, params) → (state, op). The response envelope (`tool/response-envelope.ts`) owns
|
|
132
|
+
* formatting, the store (`state/store.ts`) owns commit.
|
|
133
|
+
*
|
|
134
|
+
* Validation is in-line: structural guards (`subject required`, `id required`,
|
|
135
|
+
* `at least one mutable field`) plus state-aware checks (transition legality,
|
|
136
|
+
* dangling/deleted blockedBy, self-block, cycles). Decision: validation stays
|
|
137
|
+
* in-reducer.
|
|
138
|
+
*/
|
|
139
|
+
export function applyTaskMutation(
|
|
140
|
+
state: TaskState,
|
|
141
|
+
action: TaskAction,
|
|
142
|
+
params: TaskMutationParams,
|
|
143
|
+
): ApplyResult {
|
|
144
|
+
switch (action) {
|
|
145
|
+
case "create": {
|
|
146
|
+
const batch = params.tasks;
|
|
147
|
+
|
|
148
|
+
if (Array.isArray(batch) && params.subject !== undefined) {
|
|
149
|
+
return errorResult(state, "create requires subject or tasks[], not both");
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (Array.isArray(batch)) {
|
|
153
|
+
// Each item carries its own fields; a top-level one would apply to no item.
|
|
154
|
+
const topLevel = BATCH_ITEM_FIELDS.filter((field) => params[field] !== undefined);
|
|
155
|
+
if (topLevel.length) {
|
|
156
|
+
return errorResult(
|
|
157
|
+
state,
|
|
158
|
+
`create with tasks[] takes ${topLevel.join(", ")} on each item, not at the top level`,
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
if (batch.length === 0) {
|
|
162
|
+
return errorResult(state, "tasks[] must be non-empty");
|
|
163
|
+
}
|
|
164
|
+
const items: Array<{ spec: TaskMutationParams; subject: string }> = [];
|
|
165
|
+
for (let i = 0; i < batch.length; i++) {
|
|
166
|
+
const spec = asCreateItem(batch[i]);
|
|
167
|
+
const subject = spec?.subject;
|
|
168
|
+
if (!spec || !subject?.trim()) {
|
|
169
|
+
return errorResult(state, `tasks[${i}]: subject required for create`);
|
|
170
|
+
}
|
|
171
|
+
items.push({ spec, subject });
|
|
172
|
+
}
|
|
173
|
+
const siblingIds = new Set(items.map((_, i) => state.nextId + i));
|
|
174
|
+
for (const item of items) {
|
|
175
|
+
const blockedError = validateCreateBlockedBy(state, item.spec.blockedBy, siblingIds);
|
|
176
|
+
if (blockedError) return errorResult(state, blockedError);
|
|
177
|
+
}
|
|
178
|
+
const created = items.map((item, i) =>
|
|
179
|
+
buildCreatedTask(state.nextId + i, item.spec, item.subject),
|
|
180
|
+
);
|
|
181
|
+
return {
|
|
182
|
+
state: { tasks: [...state.tasks, ...created], nextId: state.nextId + created.length },
|
|
183
|
+
op: { kind: "create", taskIds: created.map((t) => t.id) },
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (!params.subject?.trim()) {
|
|
188
|
+
return errorResult(state, "subject required for create");
|
|
189
|
+
}
|
|
190
|
+
const blockedError = validateCreateBlockedBy(state, params.blockedBy, new Set());
|
|
191
|
+
if (blockedError) return errorResult(state, blockedError);
|
|
192
|
+
const newTask = buildCreatedTask(state.nextId, params, params.subject);
|
|
193
|
+
return {
|
|
194
|
+
state: { tasks: [...state.tasks, newTask], nextId: state.nextId + 1 },
|
|
195
|
+
op: { kind: "create", taskIds: [newTask.id] },
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
case "update": {
|
|
200
|
+
if (params.id === undefined) return errorResult(state, "id required for update");
|
|
201
|
+
const idx = state.tasks.findIndex((t) => t.id === params.id);
|
|
202
|
+
if (idx === -1) return errorResult(state, `#${params.id} not found`);
|
|
203
|
+
const current = state.tasks[idx];
|
|
204
|
+
|
|
205
|
+
const hasMutation =
|
|
206
|
+
params.subject !== undefined ||
|
|
207
|
+
params.description !== undefined ||
|
|
208
|
+
params.activeForm !== undefined ||
|
|
209
|
+
params.status !== undefined ||
|
|
210
|
+
params.owner !== undefined ||
|
|
211
|
+
params.metadata !== undefined ||
|
|
212
|
+
(params.addBlockedBy && params.addBlockedBy.length > 0) ||
|
|
213
|
+
(params.removeBlockedBy && params.removeBlockedBy.length > 0);
|
|
214
|
+
if (!hasMutation)
|
|
215
|
+
return errorResult(
|
|
216
|
+
state,
|
|
217
|
+
"update requires at least one mutable field: subject, description, activeForm, status, owner, metadata, addBlockedBy, or removeBlockedBy",
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
let newStatus = current.status;
|
|
221
|
+
if (params.status !== undefined) {
|
|
222
|
+
if (!isTransitionValid(current.status, params.status)) {
|
|
223
|
+
return errorResult(state, `illegal transition ${current.status} → ${params.status}`);
|
|
224
|
+
}
|
|
225
|
+
newStatus = params.status;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
let newBlockedBy = current.blockedBy ? [...current.blockedBy] : [];
|
|
229
|
+
if (params.removeBlockedBy?.length) {
|
|
230
|
+
const toRemove = new Set(params.removeBlockedBy);
|
|
231
|
+
newBlockedBy = newBlockedBy.filter((dep) => !toRemove.has(dep));
|
|
232
|
+
}
|
|
233
|
+
if (params.addBlockedBy?.length) {
|
|
234
|
+
for (const dep of params.addBlockedBy) {
|
|
235
|
+
if (dep === current.id)
|
|
236
|
+
return errorResult(state, `cannot block #${current.id} on itself`);
|
|
237
|
+
const depTask = state.tasks.find((t) => t.id === dep);
|
|
238
|
+
if (!depTask) return errorResult(state, `addBlockedBy: #${dep} not found`);
|
|
239
|
+
if (depTask.status === "deleted")
|
|
240
|
+
return errorResult(state, `addBlockedBy: #${dep} is deleted`);
|
|
241
|
+
if (!newBlockedBy.includes(dep)) newBlockedBy.push(dep);
|
|
242
|
+
}
|
|
243
|
+
if (detectCycle(state.tasks, current.id, newBlockedBy)) {
|
|
244
|
+
return errorResult(state, "addBlockedBy would create a cycle in the blockedBy graph");
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
let newMetadata = current.metadata;
|
|
249
|
+
if (params.metadata !== undefined) {
|
|
250
|
+
const merged: Record<string, unknown> = { ...current.metadata };
|
|
251
|
+
for (const [k, v] of Object.entries(params.metadata)) {
|
|
252
|
+
if (v === null) delete merged[k];
|
|
253
|
+
else merged[k] = v;
|
|
254
|
+
}
|
|
255
|
+
newMetadata = Object.keys(merged).length ? merged : undefined;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const updated: Task = { ...current, status: newStatus };
|
|
259
|
+
if (params.subject !== undefined) updated.subject = params.subject;
|
|
260
|
+
if (params.description !== undefined) updated.description = params.description;
|
|
261
|
+
if (params.activeForm !== undefined) updated.activeForm = params.activeForm;
|
|
262
|
+
if (params.owner !== undefined) updated.owner = params.owner;
|
|
263
|
+
if (newBlockedBy.length) updated.blockedBy = newBlockedBy;
|
|
264
|
+
else delete updated.blockedBy;
|
|
265
|
+
if (newMetadata === undefined) delete updated.metadata;
|
|
266
|
+
else updated.metadata = newMetadata;
|
|
267
|
+
|
|
268
|
+
const newTasks = [...state.tasks];
|
|
269
|
+
newTasks[idx] = updated;
|
|
270
|
+
return {
|
|
271
|
+
state: { tasks: newTasks, nextId: state.nextId },
|
|
272
|
+
op: {
|
|
273
|
+
kind: "update",
|
|
274
|
+
id: updated.id,
|
|
275
|
+
fromStatus: current.status,
|
|
276
|
+
toStatus: newStatus,
|
|
277
|
+
changed: taskChanged(current, updated),
|
|
278
|
+
},
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
case "list": {
|
|
283
|
+
return {
|
|
284
|
+
state,
|
|
285
|
+
op: {
|
|
286
|
+
kind: "list",
|
|
287
|
+
includeDeleted: params.includeDeleted === true,
|
|
288
|
+
...(params.status !== undefined ? { statusFilter: params.status } : {}),
|
|
289
|
+
},
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
case "get": {
|
|
294
|
+
if (params.id === undefined) return errorResult(state, "id required for get");
|
|
295
|
+
const task = state.tasks.find((t) => t.id === params.id);
|
|
296
|
+
if (!task) return errorResult(state, `#${params.id} not found`);
|
|
297
|
+
return { state, op: { kind: "get", task } };
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
case "delete": {
|
|
301
|
+
if (params.id === undefined) return errorResult(state, "id required for delete");
|
|
302
|
+
const idx = state.tasks.findIndex((t) => t.id === params.id);
|
|
303
|
+
if (idx === -1) return errorResult(state, `#${params.id} not found`);
|
|
304
|
+
const current = state.tasks[idx];
|
|
305
|
+
if (current.status === "deleted")
|
|
306
|
+
return errorResult(state, `#${current.id} is already deleted`);
|
|
307
|
+
const updated: Task = { ...current, status: "deleted" };
|
|
308
|
+
const newTasks = [...state.tasks];
|
|
309
|
+
newTasks[idx] = updated;
|
|
310
|
+
return {
|
|
311
|
+
state: { tasks: newTasks, nextId: state.nextId },
|
|
312
|
+
op: { kind: "delete", id: updated.id, subject: updated.subject },
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
case "clear": {
|
|
317
|
+
const count = state.tasks.length;
|
|
318
|
+
return {
|
|
319
|
+
state: { tasks: [], nextId: 1 },
|
|
320
|
+
op: { kind: "clear", count },
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
package/state/state.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Task } from "../tool/types.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Canonical state for the todo tool. Single source of truth — both the reducer
|
|
5
|
+
* (`state/state-reducer.ts`) and the live store cell (`state/store.ts`) read
|
|
6
|
+
* this shape. Replay (`state/replay.ts`) returns a fresh `TaskState`; the
|
|
7
|
+
* lifecycle handlers in `index.ts` write it via `replaceState`.
|
|
8
|
+
*
|
|
9
|
+
* The shape is intentionally minimal — no derived caches or runtime cells.
|
|
10
|
+
* Selectors in `state/selectors.ts` are pure of `TaskState` and own all
|
|
11
|
+
* derivations (visible/grouped/counted/etc).
|
|
12
|
+
*/
|
|
13
|
+
export interface TaskState {
|
|
14
|
+
tasks: Task[];
|
|
15
|
+
nextId: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const EMPTY_STATE: TaskState = { tasks: [], nextId: 1 };
|
package/state/store.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type { Task } from "../tool/types.ts";
|
|
2
|
+
import { EMPTY_STATE, type TaskState } from "./state.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Per-session live state: a Map partitioned by session id, so a detached/child
|
|
6
|
+
* session (distinct sid) can never read or clobber another session's tasks.
|
|
7
|
+
*
|
|
8
|
+
* The Map is the single mutation seam — only `commitState` / `replaceState` /
|
|
9
|
+
* `evictSession` write it; the reducer (`state/state-reducer.ts`) stays pure.
|
|
10
|
+
*/
|
|
11
|
+
const sessions = new Map<string, TaskState>();
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Ctx-less render pointer: which slot do the ctx-free readers (the overlay's
|
|
15
|
+
* `getSnapshot()`, the tool's `renderCall()`) render? Set when the first UI
|
|
16
|
+
* session claims the foreground, before the overlay is loaded (creator-ownership
|
|
17
|
+
* — see `index.ts`). A *distinct* concept from the three task-state mutation
|
|
18
|
+
* seams; it is not a 4th writer of task state.
|
|
19
|
+
*/
|
|
20
|
+
let activeRenderSession = "";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Session-id extractor. Structural ctx type (no Pi-runtime import) —
|
|
24
|
+
* mirrors `replay.ts`'s ctx shape so `state/` stays Pi-import-free. Returns
|
|
25
|
+
* `… ?? ""` so an unknown/empty session resolves to "" rather than undefined
|
|
26
|
+
* (keeps the key a plain string for callers and the `index.ts` sid-gate).
|
|
27
|
+
*/
|
|
28
|
+
export function sid(ctx: { sessionManager: { getSessionId(): string } }): string {
|
|
29
|
+
return ctx.sessionManager.getSessionId() ?? "";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Fresh, non-aliasing EMPTY_STATE copy (never returns `EMPTY_STATE.tasks`). */
|
|
33
|
+
function freshState(): TaskState {
|
|
34
|
+
return { tasks: [...EMPTY_STATE.tasks], nextId: EMPTY_STATE.nextId };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Get-or-read a session's slot: the committed slot by identity, or a fresh
|
|
38
|
+
* EMPTY_STATE copy (not stored) when the slot is absent. */
|
|
39
|
+
function slotFor(sessionId: string): TaskState {
|
|
40
|
+
return sessions.get(sessionId) ?? freshState();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Live tasks accessor for a session. Returned `readonly Task[]` so callers
|
|
45
|
+
* (overlay render hook, `/todos` command, `renderCall` subject lookup) cannot
|
|
46
|
+
* mutate the live slot. Consumers must not cast back.
|
|
47
|
+
*/
|
|
48
|
+
export function getTodos(sessionId: string): readonly Task[] {
|
|
49
|
+
return slotFor(sessionId).tasks;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function getNextId(sessionId: string): number {
|
|
53
|
+
return slotFor(sessionId).nextId;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Snapshot accessor used by reducer callers to pass canonical state in. */
|
|
57
|
+
export function getState(sessionId: string): TaskState {
|
|
58
|
+
return slotFor(sessionId);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Replay seam. Lifecycle handlers in `index.ts` call this on
|
|
63
|
+
* `session_start` / `session_compact` / `session_tree` after
|
|
64
|
+
* `replayFromBranch` decodes the latest snapshot, keyed to the session.
|
|
65
|
+
*/
|
|
66
|
+
export function replaceState(sessionId: string, next: TaskState): void {
|
|
67
|
+
sessions.set(sessionId, next);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Post-reducer commit seam. Tool `execute()` calls this with the reducer's
|
|
72
|
+
* `state` output to publish the new canonical state to live readers (overlay,
|
|
73
|
+
* `/todos`, `renderCall`), keyed to the calling session.
|
|
74
|
+
*/
|
|
75
|
+
export function commitState(sessionId: string, next: TaskState): void {
|
|
76
|
+
sessions.set(sessionId, next);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Drop a session's slot on `session_shutdown`. No-op if the slot is absent. */
|
|
80
|
+
export function evictSession(sessionId: string): void {
|
|
81
|
+
sessions.delete(sessionId);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Ctx-less render reader: the slot the overlay / `renderCall` render.
|
|
86
|
+
* Resolves to the `activeRenderSession` slot, or a fresh EMPTY_STATE copy when
|
|
87
|
+
* no foreground has been set yet.
|
|
88
|
+
*/
|
|
89
|
+
export function getRenderState(): TaskState {
|
|
90
|
+
return slotFor(activeRenderSession);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Set the ctx-less render pointer when the first UI session claims foreground. */
|
|
94
|
+
export function setActiveRenderSession(sessionId: string): void {
|
|
95
|
+
activeRenderSession = sessionId;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Reads the foreground render pointer — the sid the `index.ts` sid-gate compares
|
|
100
|
+
* against, and the slot `getRenderState()` resolves to. Distinct from
|
|
101
|
+
* `setActiveRenderSession` (the writer): the gate *reads* this pointer, and
|
|
102
|
+
* foreground teardown must *clear* it.
|
|
103
|
+
*/
|
|
104
|
+
export function getActiveRenderSession(): string {
|
|
105
|
+
return activeRenderSession;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Foreground teardown (session_shutdown of the foreground session). Resets the
|
|
110
|
+
* pointer to "" so the next `hasUI` session_start reclaims the foreground.
|
|
111
|
+
*/
|
|
112
|
+
export function clearActiveRenderSession(): void {
|
|
113
|
+
activeRenderSession = "";
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Test-setup reset. Wired into the global `test/setup.ts` `beforeEach` via
|
|
118
|
+
* the existing `__resetState` import path. Signature unchanged ⇒ no
|
|
119
|
+
* `test/setup.ts` edit needed. Clears BOTH the session Map and the render
|
|
120
|
+
* pointer so filesystem/detect resets start from a clean state.
|
|
121
|
+
*/
|
|
122
|
+
export function __resetState(): void {
|
|
123
|
+
sessions.clear();
|
|
124
|
+
activeRenderSession = "";
|
|
125
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Task } from "../tool/types.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Detect whether merging `newBlockedBy` into `taskId`'s `blockedBy` set would
|
|
5
|
+
* introduce a cycle in the dependency graph.
|
|
6
|
+
*
|
|
7
|
+
* Pure of any module state; takes the existing `taskList` and the proposed
|
|
8
|
+
* additions explicitly so the reducer can ask "would this update cycle?"
|
|
9
|
+
* without mutating state first.
|
|
10
|
+
*/
|
|
11
|
+
export function detectCycle(
|
|
12
|
+
taskList: readonly Task[],
|
|
13
|
+
taskId: number,
|
|
14
|
+
newBlockedBy: readonly number[],
|
|
15
|
+
): boolean {
|
|
16
|
+
const edges = new Map<number, number[]>();
|
|
17
|
+
for (const t of taskList) {
|
|
18
|
+
if (t.id === taskId) {
|
|
19
|
+
const merged = new Set([...(t.blockedBy ?? []), ...newBlockedBy]);
|
|
20
|
+
edges.set(t.id, [...merged]);
|
|
21
|
+
} else {
|
|
22
|
+
edges.set(t.id, t.blockedBy ? [...t.blockedBy] : []);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const visiting = new Set<number>();
|
|
27
|
+
const visited = new Set<number>();
|
|
28
|
+
const hasCycleFrom = (node: number): boolean => {
|
|
29
|
+
if (visiting.has(node)) return true;
|
|
30
|
+
if (visited.has(node)) return false;
|
|
31
|
+
visiting.add(node);
|
|
32
|
+
for (const nb of edges.get(node) ?? []) {
|
|
33
|
+
if (hasCycleFrom(nb)) return true;
|
|
34
|
+
}
|
|
35
|
+
visiting.delete(node);
|
|
36
|
+
visited.add(node);
|
|
37
|
+
return false;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
for (const node of edges.keys()) {
|
|
41
|
+
if (hasCycleFrom(node)) return true;
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Build the inverse adjacency map: for each task `T`, which other tasks list
|
|
48
|
+
* `T` in their `blockedBy`. Consumed by `selectShowTaskIds` (overlay gating)
|
|
49
|
+
* and the `get` action's "blocks: #x, #y" suffix line.
|
|
50
|
+
*/
|
|
51
|
+
export function deriveBlocks(taskList: readonly Task[]): Map<number, number[]> {
|
|
52
|
+
const blocks = new Map<number, number[]>();
|
|
53
|
+
for (const t of taskList) {
|
|
54
|
+
for (const dep of t.blockedBy ?? []) {
|
|
55
|
+
const arr = blocks.get(dep) ?? [];
|
|
56
|
+
arr.push(t.id);
|
|
57
|
+
blocks.set(dep, arr);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return blocks;
|
|
61
|
+
}
|