@juicesharp/rpiv-todo 1.0.10 → 1.0.11
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.md +9 -1
- package/index.ts +7 -5
- package/package.json +13 -3
- package/state/invariants.ts +20 -0
- package/state/replay.ts +38 -0
- package/state/selectors.ts +107 -0
- package/state/state-reducer.ts +187 -0
- package/state/state.ts +18 -0
- package/state/store.ts +54 -0
- package/state/task-graph.ts +57 -0
- package/todo-overlay.ts +36 -137
- package/todo.ts +63 -626
- package/tool/response-envelope.ts +94 -0
- package/tool/types.ts +128 -0
- package/view/format.ts +168 -0
package/README.md
CHANGED
|
@@ -3,10 +3,18 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/@juicesharp/rpiv-todo)
|
|
4
4
|
[](https://opensource.org/licenses/MIT)
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
Give the model a todo list it can keep across long sessions. `rpiv-todo` adds the `todo` tool, the `/todos` slash command, and a live overlay above the editor to [Pi Agent](https://github.com/badlogic/pi-mono) — tasks survive `/reload` and conversation compaction, so the model picks up where it left off.
|
|
7
7
|
|
|
8
8
|

|
|
9
9
|
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- **Live overlay above the editor** — see the model's plan at all times; auto-hides when empty.
|
|
13
|
+
- **Survives `/reload` and compaction** — tasks replay from the conversation branch, not disk.
|
|
14
|
+
- **Status states** — pending, in_progress, completed, plus a deleted tombstone for audit.
|
|
15
|
+
- **Dependency tracking** — `blockedBy` with cycle detection, so the model can sequence work.
|
|
16
|
+
- **Smart truncation** — 12-line collapse threshold; completed tasks drop first, pending tasks stay visible last.
|
|
17
|
+
|
|
10
18
|
## Install
|
|
11
19
|
|
|
12
20
|
```bash
|
package/index.ts
CHANGED
|
@@ -11,7 +11,9 @@
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
14
|
-
import {
|
|
14
|
+
import { replayFromBranch } from "./state/replay.js";
|
|
15
|
+
import { replaceState } from "./state/store.js";
|
|
16
|
+
import { registerTodosCommand, registerTodoTool, TOOL_NAME } from "./todo.js";
|
|
15
17
|
import { TodoOverlay } from "./todo-overlay.js";
|
|
16
18
|
|
|
17
19
|
export default function (pi: ExtensionAPI) {
|
|
@@ -22,7 +24,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
22
24
|
registerTodosCommand(pi);
|
|
23
25
|
|
|
24
26
|
pi.on("session_start", async (_event, ctx) => {
|
|
25
|
-
|
|
27
|
+
replaceState(replayFromBranch(ctx));
|
|
26
28
|
if (ctx.hasUI) {
|
|
27
29
|
todoOverlay ??= new TodoOverlay();
|
|
28
30
|
todoOverlay.setUICtx(ctx.ui);
|
|
@@ -31,12 +33,12 @@ export default function (pi: ExtensionAPI) {
|
|
|
31
33
|
});
|
|
32
34
|
|
|
33
35
|
pi.on("session_compact", async (_event, ctx) => {
|
|
34
|
-
|
|
36
|
+
replaceState(replayFromBranch(ctx));
|
|
35
37
|
todoOverlay?.update();
|
|
36
38
|
});
|
|
37
39
|
|
|
38
40
|
pi.on("session_tree", async (_event, ctx) => {
|
|
39
|
-
|
|
41
|
+
replaceState(replayFromBranch(ctx));
|
|
40
42
|
todoOverlay?.update();
|
|
41
43
|
});
|
|
42
44
|
|
|
@@ -45,7 +47,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
45
47
|
todoOverlay = undefined;
|
|
46
48
|
});
|
|
47
49
|
|
|
48
|
-
// Reads getTodos() at render time; do NOT call
|
|
50
|
+
// Reads getTodos() at render time; do NOT call replayFromBranch here
|
|
49
51
|
// (branch is stale — message_end runs after tool_execution_end).
|
|
50
52
|
pi.on("tool_execution_end", async (event) => {
|
|
51
53
|
if (event.toolName !== TOOL_NAME || event.isError) return;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@juicesharp/rpiv-todo",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "Pi extension:
|
|
3
|
+
"version": "1.0.11",
|
|
4
|
+
"description": "Pi extension: gives the model a todo list with a live overlay that survives /reload and compaction",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
7
7
|
"pi-extension",
|
|
@@ -30,8 +30,18 @@
|
|
|
30
30
|
"index.ts",
|
|
31
31
|
"todo.ts",
|
|
32
32
|
"todo-overlay.ts",
|
|
33
|
+
"LICENSE",
|
|
33
34
|
"README.md",
|
|
34
|
-
"
|
|
35
|
+
"state/invariants.ts",
|
|
36
|
+
"state/replay.ts",
|
|
37
|
+
"state/selectors.ts",
|
|
38
|
+
"state/state-reducer.ts",
|
|
39
|
+
"state/state.ts",
|
|
40
|
+
"state/store.ts",
|
|
41
|
+
"state/task-graph.ts",
|
|
42
|
+
"tool/response-envelope.ts",
|
|
43
|
+
"tool/types.ts",
|
|
44
|
+
"view/format.ts"
|
|
35
45
|
],
|
|
36
46
|
"pi": {
|
|
37
47
|
"extensions": [
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { TaskStatus } from "../tool/types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Allowed forward transitions per source status. `completed` is one-way to
|
|
5
|
+
* `deleted` (never back to `in_progress`); `deleted` is terminal.
|
|
6
|
+
*
|
|
7
|
+
* Idempotent same→same is checked separately in `isTransitionValid` so this
|
|
8
|
+
* table only enumerates actual transitions.
|
|
9
|
+
*/
|
|
10
|
+
export const VALID_TRANSITIONS: Record<TaskStatus, ReadonlySet<TaskStatus>> = {
|
|
11
|
+
pending: new Set(["in_progress", "completed", "deleted"]),
|
|
12
|
+
in_progress: new Set(["pending", "completed", "deleted"]),
|
|
13
|
+
completed: new Set(["deleted"]),
|
|
14
|
+
deleted: new Set(),
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export function isTransitionValid(from: TaskStatus, to: TaskStatus): boolean {
|
|
18
|
+
if (from === to) return true;
|
|
19
|
+
return VALID_TRANSITIONS[from].has(to);
|
|
20
|
+
}
|
package/state/replay.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { TaskDetails } from "../tool/types.js";
|
|
2
|
+
import { EMPTY_STATE, type TaskState } from "./state.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Discriminator for `details` envelopes that match the persisted `TaskDetails`
|
|
6
|
+
* shape. Defensive — branch entries from older or corrupt sessions are
|
|
7
|
+
* skipped silently.
|
|
8
|
+
*/
|
|
9
|
+
export function isTaskDetails(value: unknown): value is TaskDetails {
|
|
10
|
+
if (!value || typeof value !== "object") return false;
|
|
11
|
+
const v = value as Record<string, unknown>;
|
|
12
|
+
return Array.isArray(v.tasks) && typeof v.nextId === "number";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Walk the current branch in chronological order; the LAST `toolResult` whose
|
|
17
|
+
* `toolName === "todo"` and whose `details` shape matches `TaskDetails` wins
|
|
18
|
+
* (last-write-wins). When no matching entry exists, returns `EMPTY_STATE`.
|
|
19
|
+
*
|
|
20
|
+
* Pure of module state — `index.ts` writes the returned snapshot into the
|
|
21
|
+
* store after this returns. The function explicitly does NOT touch the store
|
|
22
|
+
* cell.
|
|
23
|
+
*/
|
|
24
|
+
export function replayFromBranch(ctx: { sessionManager: { getBranch(): Iterable<unknown> } }): TaskState {
|
|
25
|
+
let result: TaskState = { tasks: [...EMPTY_STATE.tasks], nextId: EMPTY_STATE.nextId };
|
|
26
|
+
for (const entry of ctx.sessionManager.getBranch()) {
|
|
27
|
+
const e = entry as { type?: string; message?: { role?: string; toolName?: string; details?: unknown } };
|
|
28
|
+
if (e.type !== "message") continue;
|
|
29
|
+
const msg = e.message;
|
|
30
|
+
if (!msg || msg.role !== "toolResult" || msg.toolName !== "todo") continue;
|
|
31
|
+
if (!isTaskDetails(msg.details)) continue;
|
|
32
|
+
result = {
|
|
33
|
+
tasks: msg.details.tasks.map((t) => ({ ...t })),
|
|
34
|
+
nextId: msg.details.nextId,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { Task, TaskStatus } from "../tool/types.js";
|
|
2
|
+
import type { TaskState } from "./state.js";
|
|
3
|
+
|
|
4
|
+
/** Tasks excluding deleted tombstones — the canonical "what's visible". */
|
|
5
|
+
export function selectVisibleTasks(state: TaskState): readonly Task[] {
|
|
6
|
+
return state.tasks.filter((t) => t.status !== "deleted");
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Group visible tasks by status. Iteration order at the call site uses
|
|
11
|
+
* (`completed`, `inProgress`, `pending`) to match the `/todos` header part
|
|
12
|
+
* order pinned by `todo.command.test.ts`.
|
|
13
|
+
*/
|
|
14
|
+
export interface TasksByStatus {
|
|
15
|
+
pending: readonly Task[];
|
|
16
|
+
inProgress: readonly Task[];
|
|
17
|
+
completed: readonly Task[];
|
|
18
|
+
}
|
|
19
|
+
export function selectTasksByStatus(state: TaskState): TasksByStatus {
|
|
20
|
+
const visible = selectVisibleTasks(state);
|
|
21
|
+
return {
|
|
22
|
+
pending: visible.filter((t) => t.status === "pending"),
|
|
23
|
+
inProgress: visible.filter((t) => t.status === "in_progress"),
|
|
24
|
+
completed: visible.filter((t) => t.status === "completed"),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Total counts for the overlay heading (`Todos (n/m)`) and `/todos` header. */
|
|
29
|
+
export interface TodoCounts {
|
|
30
|
+
total: number;
|
|
31
|
+
pending: number;
|
|
32
|
+
inProgress: number;
|
|
33
|
+
completed: number;
|
|
34
|
+
}
|
|
35
|
+
export function selectTodoCounts(state: TaskState): TodoCounts {
|
|
36
|
+
const groups = selectTasksByStatus(state);
|
|
37
|
+
return {
|
|
38
|
+
total: groups.pending.length + groups.inProgress.length + groups.completed.length,
|
|
39
|
+
pending: groups.pending.length,
|
|
40
|
+
inProgress: groups.inProgress.length,
|
|
41
|
+
completed: groups.completed.length,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Whether any visible task carries a `blockedBy` reference. The overlay uses
|
|
47
|
+
* this to gate the `#id` prefix on per-task rows — without at least one
|
|
48
|
+
* `⛓ #N` suffix, the per-row id has no anchor.
|
|
49
|
+
*/
|
|
50
|
+
export function selectShowTaskIds(state: TaskState): boolean {
|
|
51
|
+
return selectVisibleTasks(state).some((t) => t.blockedBy && t.blockedBy.length > 0);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Resolve a task's subject by id from the live state for renderCall's
|
|
56
|
+
* accent label. `undefined` when the id is unknown — caller falls back to
|
|
57
|
+
* `#id` plain rendering.
|
|
58
|
+
*/
|
|
59
|
+
export function selectTaskSubjectById(state: TaskState, id: number): string | undefined {
|
|
60
|
+
return state.tasks.find((t) => t.id === id)?.subject;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Overlay layout decision. Encapsulates the "drop completed first, then
|
|
65
|
+
* truncate non-completed tail" rule pre-refactor lived in
|
|
66
|
+
* `todo-overlay.ts:144-188`. `budget` is the body-slot count (caller passes
|
|
67
|
+
* `MAX_WIDGET_LINES - 1` to reserve the heading row); on overflow the
|
|
68
|
+
* selector reserves one more slot internally for the summary row. Returns
|
|
69
|
+
* the visible task slice plus the overflow summary parts.
|
|
70
|
+
*/
|
|
71
|
+
export interface OverlayLayout {
|
|
72
|
+
visible: readonly Task[];
|
|
73
|
+
hiddenCompleted: number;
|
|
74
|
+
truncatedTail: number;
|
|
75
|
+
}
|
|
76
|
+
export function selectOverlayLayout(state: TaskState, budget: number): OverlayLayout {
|
|
77
|
+
const all = selectVisibleTasks(state);
|
|
78
|
+
if (all.length <= budget) {
|
|
79
|
+
return { visible: all, hiddenCompleted: 0, truncatedTail: 0 };
|
|
80
|
+
}
|
|
81
|
+
const innerBudget = budget - 1;
|
|
82
|
+
const nonCompleted = all.filter((t) => t.status !== "completed");
|
|
83
|
+
const totalCompleted = all.length - nonCompleted.length;
|
|
84
|
+
if (nonCompleted.length <= innerBudget) {
|
|
85
|
+
const kept = new Set<Task>(nonCompleted);
|
|
86
|
+
for (const t of all) {
|
|
87
|
+
if (kept.size >= innerBudget) break;
|
|
88
|
+
if (t.status === "completed") kept.add(t);
|
|
89
|
+
}
|
|
90
|
+
const visible = all.filter((t) => kept.has(t));
|
|
91
|
+
const shownCompleted = visible.filter((t) => t.status === "completed").length;
|
|
92
|
+
return { visible, hiddenCompleted: totalCompleted - shownCompleted, truncatedTail: 0 };
|
|
93
|
+
}
|
|
94
|
+
const visible = nonCompleted.slice(0, innerBudget);
|
|
95
|
+
const truncatedTail = nonCompleted.length - innerBudget;
|
|
96
|
+
return { visible, hiddenCompleted: totalCompleted, truncatedTail };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Helper: whether any visible task is `pending` or `in_progress`. The overlay
|
|
101
|
+
* uses this to pick the heading icon (`accent`+`●` vs `dim`+`○`).
|
|
102
|
+
*/
|
|
103
|
+
export function selectHasActive(state: TaskState): boolean {
|
|
104
|
+
return selectVisibleTasks(state).some((t) => t.status === "in_progress" || t.status === "pending");
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export const ACTIVE_STATUSES: ReadonlySet<TaskStatus> = new Set(["pending", "in_progress"]);
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import type { Task, TaskAction, TaskMutationParams, TaskStatus } from "../tool/types.js";
|
|
2
|
+
import { isTransitionValid } from "./invariants.js";
|
|
3
|
+
import type { TaskState } from "./state.js";
|
|
4
|
+
import { detectCycle } from "./task-graph.js";
|
|
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"; taskId: number }
|
|
17
|
+
| { kind: "update"; id: number; fromStatus: TaskStatus; toStatus: TaskStatus }
|
|
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
|
+
function errorResult(state: TaskState, message: string): ApplyResult {
|
|
30
|
+
return { state, op: { kind: "error", message } };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Pure reducer: (state, action, params) → (state, op). Mirrors the
|
|
35
|
+
* `applyTaskMutation` of pre-refactor `todo.ts` minus content/details
|
|
36
|
+
* formatting; the response envelope (`tool/response-envelope.ts`) owns
|
|
37
|
+
* formatting, the store (`state/store.ts`) owns commit.
|
|
38
|
+
*
|
|
39
|
+
* Validation is in-line: structural guards (`subject required`, `id required`,
|
|
40
|
+
* `at least one mutable field`) plus state-aware checks (transition legality,
|
|
41
|
+
* dangling/deleted blockedBy, self-block, cycles). Decision: validation stays
|
|
42
|
+
* in-reducer — see Plan §Decisions §Decision 2.
|
|
43
|
+
*/
|
|
44
|
+
export function applyTaskMutation(state: TaskState, action: TaskAction, params: TaskMutationParams): ApplyResult {
|
|
45
|
+
switch (action) {
|
|
46
|
+
case "create": {
|
|
47
|
+
if (!params.subject || !params.subject.trim()) {
|
|
48
|
+
return errorResult(state, "subject required for create");
|
|
49
|
+
}
|
|
50
|
+
if (params.blockedBy?.length) {
|
|
51
|
+
for (const dep of params.blockedBy) {
|
|
52
|
+
const depTask = state.tasks.find((t) => t.id === dep);
|
|
53
|
+
if (!depTask) return errorResult(state, `blockedBy: #${dep} not found`);
|
|
54
|
+
if (depTask.status === "deleted") return errorResult(state, `blockedBy: #${dep} is deleted`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const newTask: Task = {
|
|
58
|
+
id: state.nextId,
|
|
59
|
+
subject: params.subject,
|
|
60
|
+
status: "pending",
|
|
61
|
+
};
|
|
62
|
+
if (params.description) newTask.description = params.description;
|
|
63
|
+
if (params.activeForm) newTask.activeForm = params.activeForm;
|
|
64
|
+
if (params.blockedBy?.length) newTask.blockedBy = [...params.blockedBy];
|
|
65
|
+
if (params.owner) newTask.owner = params.owner;
|
|
66
|
+
if (params.metadata) newTask.metadata = { ...params.metadata };
|
|
67
|
+
|
|
68
|
+
const newTasks = [...state.tasks, newTask];
|
|
69
|
+
return {
|
|
70
|
+
state: { tasks: newTasks, nextId: state.nextId + 1 },
|
|
71
|
+
op: { kind: "create", taskId: newTask.id },
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
case "update": {
|
|
76
|
+
if (params.id === undefined) return errorResult(state, "id required for update");
|
|
77
|
+
const idx = state.tasks.findIndex((t) => t.id === params.id);
|
|
78
|
+
if (idx === -1) return errorResult(state, `#${params.id} not found`);
|
|
79
|
+
const current = state.tasks[idx];
|
|
80
|
+
|
|
81
|
+
const hasMutation =
|
|
82
|
+
params.subject !== undefined ||
|
|
83
|
+
params.description !== undefined ||
|
|
84
|
+
params.activeForm !== undefined ||
|
|
85
|
+
params.status !== undefined ||
|
|
86
|
+
params.owner !== undefined ||
|
|
87
|
+
params.metadata !== undefined ||
|
|
88
|
+
(params.addBlockedBy && params.addBlockedBy.length > 0) ||
|
|
89
|
+
(params.removeBlockedBy && params.removeBlockedBy.length > 0);
|
|
90
|
+
if (!hasMutation) return errorResult(state, "update requires at least one mutable field");
|
|
91
|
+
|
|
92
|
+
let newStatus = current.status;
|
|
93
|
+
if (params.status !== undefined) {
|
|
94
|
+
if (!isTransitionValid(current.status, params.status)) {
|
|
95
|
+
return errorResult(state, `illegal transition ${current.status} → ${params.status}`);
|
|
96
|
+
}
|
|
97
|
+
newStatus = params.status;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
let newBlockedBy = current.blockedBy ? [...current.blockedBy] : [];
|
|
101
|
+
if (params.removeBlockedBy?.length) {
|
|
102
|
+
const toRemove = new Set(params.removeBlockedBy);
|
|
103
|
+
newBlockedBy = newBlockedBy.filter((dep) => !toRemove.has(dep));
|
|
104
|
+
}
|
|
105
|
+
if (params.addBlockedBy?.length) {
|
|
106
|
+
for (const dep of params.addBlockedBy) {
|
|
107
|
+
if (dep === current.id) return errorResult(state, `cannot block #${current.id} on itself`);
|
|
108
|
+
const depTask = state.tasks.find((t) => t.id === dep);
|
|
109
|
+
if (!depTask) return errorResult(state, `addBlockedBy: #${dep} not found`);
|
|
110
|
+
if (depTask.status === "deleted") return errorResult(state, `addBlockedBy: #${dep} is deleted`);
|
|
111
|
+
if (!newBlockedBy.includes(dep)) newBlockedBy.push(dep);
|
|
112
|
+
}
|
|
113
|
+
if (detectCycle(state.tasks, current.id, newBlockedBy)) {
|
|
114
|
+
return errorResult(state, "addBlockedBy would create a cycle in the blockedBy graph");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
let newMetadata = current.metadata;
|
|
119
|
+
if (params.metadata !== undefined) {
|
|
120
|
+
const merged: Record<string, unknown> = { ...(current.metadata ?? {}) };
|
|
121
|
+
for (const [k, v] of Object.entries(params.metadata)) {
|
|
122
|
+
if (v === null) delete merged[k];
|
|
123
|
+
else merged[k] = v;
|
|
124
|
+
}
|
|
125
|
+
newMetadata = Object.keys(merged).length ? merged : undefined;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const updated: Task = { ...current, status: newStatus };
|
|
129
|
+
if (params.subject !== undefined) updated.subject = params.subject;
|
|
130
|
+
if (params.description !== undefined) updated.description = params.description;
|
|
131
|
+
if (params.activeForm !== undefined) updated.activeForm = params.activeForm;
|
|
132
|
+
if (params.owner !== undefined) updated.owner = params.owner;
|
|
133
|
+
if (newBlockedBy.length) updated.blockedBy = newBlockedBy;
|
|
134
|
+
else delete updated.blockedBy;
|
|
135
|
+
if (newMetadata === undefined) delete updated.metadata;
|
|
136
|
+
else updated.metadata = newMetadata;
|
|
137
|
+
|
|
138
|
+
const newTasks = [...state.tasks];
|
|
139
|
+
newTasks[idx] = updated;
|
|
140
|
+
return {
|
|
141
|
+
state: { tasks: newTasks, nextId: state.nextId },
|
|
142
|
+
op: { kind: "update", id: updated.id, fromStatus: current.status, toStatus: newStatus },
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
case "list": {
|
|
147
|
+
return {
|
|
148
|
+
state,
|
|
149
|
+
op: {
|
|
150
|
+
kind: "list",
|
|
151
|
+
includeDeleted: params.includeDeleted === true,
|
|
152
|
+
...(params.status !== undefined ? { statusFilter: params.status } : {}),
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
case "get": {
|
|
158
|
+
if (params.id === undefined) return errorResult(state, "id required for get");
|
|
159
|
+
const task = state.tasks.find((t) => t.id === params.id);
|
|
160
|
+
if (!task) return errorResult(state, `#${params.id} not found`);
|
|
161
|
+
return { state, op: { kind: "get", task } };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
case "delete": {
|
|
165
|
+
if (params.id === undefined) return errorResult(state, "id required for delete");
|
|
166
|
+
const idx = state.tasks.findIndex((t) => t.id === params.id);
|
|
167
|
+
if (idx === -1) return errorResult(state, `#${params.id} not found`);
|
|
168
|
+
const current = state.tasks[idx];
|
|
169
|
+
if (current.status === "deleted") return errorResult(state, `#${current.id} is already deleted`);
|
|
170
|
+
const updated: Task = { ...current, status: "deleted" };
|
|
171
|
+
const newTasks = [...state.tasks];
|
|
172
|
+
newTasks[idx] = updated;
|
|
173
|
+
return {
|
|
174
|
+
state: { tasks: newTasks, nextId: state.nextId },
|
|
175
|
+
op: { kind: "delete", id: updated.id, subject: updated.subject },
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
case "clear": {
|
|
180
|
+
const count = state.tasks.length;
|
|
181
|
+
return {
|
|
182
|
+
state: { tasks: [], nextId: 1 },
|
|
183
|
+
op: { kind: "clear", count },
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
package/state/state.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Task } from "../tool/types.js";
|
|
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,54 @@
|
|
|
1
|
+
import type { Task } from "../tool/types.js";
|
|
2
|
+
import { EMPTY_STATE, type TaskState } from "./state.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Module-level live state cell. Pre-refactor this lived as bare `tasks` /
|
|
6
|
+
* `nextId` consts in `todo.ts`; centralizing here keeps the store as the
|
|
7
|
+
* single mutation seam and lets the reducer remain pure.
|
|
8
|
+
*/
|
|
9
|
+
let state: TaskState = { tasks: [...EMPTY_STATE.tasks], nextId: EMPTY_STATE.nextId };
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Live tasks accessor. Returned `readonly Task[]` so callers (overlay render
|
|
13
|
+
* hook, `/todos` command, `renderCall` subject lookup) cannot mutate the live
|
|
14
|
+
* cell. Consumers must not cast back.
|
|
15
|
+
*/
|
|
16
|
+
export function getTodos(): readonly Task[] {
|
|
17
|
+
return state.tasks;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function getNextId(): number {
|
|
21
|
+
return state.nextId;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Snapshot accessor used by reducer callers to pass canonical state in. */
|
|
25
|
+
export function getState(): TaskState {
|
|
26
|
+
return state;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Replay seam. Lifecycle handlers in `index.ts` call this on
|
|
31
|
+
* `session_start` / `session_compact` / `session_tree` after
|
|
32
|
+
* `replayFromBranch` decodes the latest snapshot.
|
|
33
|
+
*/
|
|
34
|
+
export function replaceState(next: TaskState): void {
|
|
35
|
+
state = next;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Post-reducer commit seam. Tool execute() calls this with the reducer's
|
|
40
|
+
* `state` output to publish the new canonical state to live readers (overlay,
|
|
41
|
+
* `/todos`, renderCall).
|
|
42
|
+
*/
|
|
43
|
+
export function commitState(next: TaskState): void {
|
|
44
|
+
state = next;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Test-setup reset. Wired into the global `test/setup.ts` `beforeEach` via
|
|
49
|
+
* the existing `__resetState` import path. Name preserved verbatim — see
|
|
50
|
+
* Plan §Decisions §Decision 7.
|
|
51
|
+
*/
|
|
52
|
+
export function __resetState(): void {
|
|
53
|
+
state = { tasks: [...EMPTY_STATE.tasks], nextId: EMPTY_STATE.nextId };
|
|
54
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Task } from "../tool/types.js";
|
|
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(taskList: readonly Task[], taskId: number, newBlockedBy: readonly number[]): boolean {
|
|
12
|
+
const edges = new Map<number, number[]>();
|
|
13
|
+
for (const t of taskList) {
|
|
14
|
+
if (t.id === taskId) {
|
|
15
|
+
const merged = new Set([...(t.blockedBy ?? []), ...newBlockedBy]);
|
|
16
|
+
edges.set(t.id, [...merged]);
|
|
17
|
+
} else {
|
|
18
|
+
edges.set(t.id, t.blockedBy ? [...t.blockedBy] : []);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const visiting = new Set<number>();
|
|
23
|
+
const visited = new Set<number>();
|
|
24
|
+
const hasCycleFrom = (node: number): boolean => {
|
|
25
|
+
if (visiting.has(node)) return true;
|
|
26
|
+
if (visited.has(node)) return false;
|
|
27
|
+
visiting.add(node);
|
|
28
|
+
for (const nb of edges.get(node) ?? []) {
|
|
29
|
+
if (hasCycleFrom(nb)) return true;
|
|
30
|
+
}
|
|
31
|
+
visiting.delete(node);
|
|
32
|
+
visited.add(node);
|
|
33
|
+
return false;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
for (const node of edges.keys()) {
|
|
37
|
+
if (hasCycleFrom(node)) return true;
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Build the inverse adjacency map: for each task `T`, which other tasks list
|
|
44
|
+
* `T` in their `blockedBy`. Consumed by `selectShowTaskIds` (overlay gating)
|
|
45
|
+
* and the `get` action's "blocks: #x, #y" suffix line.
|
|
46
|
+
*/
|
|
47
|
+
export function deriveBlocks(taskList: readonly Task[]): Map<number, number[]> {
|
|
48
|
+
const blocks = new Map<number, number[]>();
|
|
49
|
+
for (const t of taskList) {
|
|
50
|
+
for (const dep of t.blockedBy ?? []) {
|
|
51
|
+
const arr = blocks.get(dep) ?? [];
|
|
52
|
+
arr.push(t.id);
|
|
53
|
+
blocks.set(dep, arr);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return blocks;
|
|
57
|
+
}
|