@ian-pascoe/pi-todo 0.1.1 → 0.1.3
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 +8 -2
- package/package.json +1 -1
- package/src/pi-todo-extension.ts +37 -24
- package/src/todo-context.ts +155 -0
package/README.md
CHANGED
|
@@ -26,12 +26,18 @@ The `todo` tool supports five actions:
|
|
|
26
26
|
|
|
27
27
|
Task status is `pending`, `active`, or `completed`; `add` defaults to `pending`. Tasks are flat, duplicate titles are allowed, and multiple Tasks may be active. Set `description` to `null` during `update` to remove it.
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
Each changed Todo List is projected from its immutable session state entry as a hidden full snapshot at a fixed conversation position. Mutations inside a tool group appear after all sibling results; later requests keep earlier snapshots in place. Clearing the list produces an explicit empty snapshot. The system prompt and tool description do not change.
|
|
30
|
+
|
|
31
|
+
After compaction, a fixed baseline immediately after the summary restores the state from before the retained Tail. Retained and newer mutations follow chronologically. This preserves previously written conversation cache prefixes between checkpoints; it does not guarantee provider cache hits.
|
|
30
32
|
|
|
31
33
|
## UI and persistence
|
|
32
34
|
|
|
33
35
|
In interactive mode, a compact widget above the editor shows status counts and up to five Tasks. Tool calls and results use custom transcript rendering. `/todo clear` confirms before manually clearing the list.
|
|
34
36
|
|
|
35
|
-
State is stored only in
|
|
37
|
+
State is stored only in `pi-todo-state` session entries, without a second message journal. It follows the active session branch and survives reloads, restarts, and compaction. Legacy state entries use the same projection. The package has no settings or external state.
|
|
38
|
+
|
|
39
|
+
A failed journal write leaves acknowledged Todo state unchanged and disables Todo in that loaded session, including across `/reload`. Pi has an existing persistence limitation: subsequent entries can reference the unwritten entry, so reopening may omit earlier history from the selected branch even though it remains in the file. Todo does not repair session history; recovery is outside this cache-prefix change.
|
|
40
|
+
|
|
41
|
+
Missing or ambiguous conversation anchors abort the request instead of silently relocating snapshots. Extensions that rewrite those anchors are unsupported.
|
|
36
42
|
|
|
37
43
|
This is privileged extension code: review it before installing it into an agent that can access local files, tools, or credentials.
|
package/package.json
CHANGED
package/src/pi-todo-extension.ts
CHANGED
|
@@ -2,15 +2,12 @@ import { StringEnum } from "@earendil-works/pi-ai";
|
|
|
2
2
|
import type { ExtensionAPI, ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
3
3
|
import { type Component, Text, truncateToWidth } from "@earendil-works/pi-tui";
|
|
4
4
|
import { Type } from "typebox";
|
|
5
|
-
import {
|
|
5
|
+
import { projectTodoContext, todoStateFromEntry } from "./todo-context.js";
|
|
6
6
|
import {
|
|
7
7
|
applyTodoAction,
|
|
8
8
|
createEmptyTodoState,
|
|
9
|
-
formatTodoList,
|
|
10
|
-
parseTodoStateSnapshot,
|
|
11
9
|
TODO_ACTIONS,
|
|
12
10
|
TODO_STATUSES,
|
|
13
|
-
TodoStateRecord,
|
|
14
11
|
todoStatusMarker,
|
|
15
12
|
type TodoActionInput,
|
|
16
13
|
type TodoStateSnapshot,
|
|
@@ -20,6 +17,17 @@ import {
|
|
|
20
17
|
} from "./todo-list.js";
|
|
21
18
|
|
|
22
19
|
const TODO_STATE_ENTRY_TYPE = "pi-todo-state";
|
|
20
|
+
const JOURNAL_FAULT = Symbol.for("@ian-pascoe/pi-todo/journal-fault");
|
|
21
|
+
|
|
22
|
+
function assertTodoJournalReadable(context: ExtensionContext): void {
|
|
23
|
+
const manager = context.sessionManager;
|
|
24
|
+
const marker = Object.getOwnPropertyDescriptor(manager, JOURNAL_FAULT);
|
|
25
|
+
if (marker !== undefined && marker.value === manager.getHeader()) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
"Todo journal write failed; Todo is disabled in this loaded session, including after /reload",
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
23
31
|
const TODO_WIDGET_ID = "pi-todo";
|
|
24
32
|
const TODO_WIDGET_STATUS_ORDER: readonly TodoStatus[] = ["active", "pending", "completed"];
|
|
25
33
|
const TODO_COLLAPSED_TASK_LIMIT = 5;
|
|
@@ -44,14 +52,7 @@ const TodoParameters = Type.Object({
|
|
|
44
52
|
|
|
45
53
|
function restoreTodoState(context: ExtensionContext): TodoStateSnapshot {
|
|
46
54
|
for (const entry of context.sessionManager.getBranch().toReversed()) {
|
|
47
|
-
|
|
48
|
-
entry.type !== "custom" ||
|
|
49
|
-
entry.customType !== TODO_STATE_ENTRY_TYPE ||
|
|
50
|
-
!Value.Check(TodoStateRecord, entry.data)
|
|
51
|
-
) {
|
|
52
|
-
continue;
|
|
53
|
-
}
|
|
54
|
-
const snapshot = parseTodoStateSnapshot(entry.data);
|
|
55
|
+
const snapshot = todoStateFromEntry(entry);
|
|
55
56
|
if (snapshot) return snapshot;
|
|
56
57
|
}
|
|
57
58
|
return createEmptyTodoState();
|
|
@@ -113,32 +114,43 @@ export default function piTodoExtension(pi: ExtensionAPI): void {
|
|
|
113
114
|
);
|
|
114
115
|
};
|
|
115
116
|
const commitTodoState = (snapshot: TodoStateSnapshot, context: ExtensionContext): void => {
|
|
117
|
+
try {
|
|
118
|
+
pi.appendEntry(TODO_STATE_ENTRY_TYPE, snapshot);
|
|
119
|
+
} catch (cause) {
|
|
120
|
+
// Todo-owned metadata survives /reload; no Pi-owned journal or agent fields are changed.
|
|
121
|
+
Object.defineProperty(context.sessionManager, JOURNAL_FAULT, {
|
|
122
|
+
value: context.sessionManager.getHeader(),
|
|
123
|
+
configurable: true,
|
|
124
|
+
});
|
|
125
|
+
context.abort();
|
|
126
|
+
throw cause;
|
|
127
|
+
}
|
|
116
128
|
state = snapshot;
|
|
117
|
-
pi.appendEntry(TODO_STATE_ENTRY_TYPE, state);
|
|
118
129
|
updateTodoWidget(context);
|
|
119
130
|
};
|
|
120
131
|
const runTodoAction = (input: TodoActionInput, context: ExtensionContext) => {
|
|
132
|
+
assertTodoJournalReadable(context);
|
|
121
133
|
const result = applyTodoAction(state, input);
|
|
122
134
|
if (!result.ok) throw result.error;
|
|
123
135
|
if (result.state !== state) commitTodoState(result.state, context);
|
|
124
136
|
return result;
|
|
125
137
|
};
|
|
126
138
|
const restoreState = (context: ExtensionContext): void => {
|
|
139
|
+
assertTodoJournalReadable(context);
|
|
127
140
|
state = restoreTodoState(context);
|
|
128
141
|
updateTodoWidget(context);
|
|
129
142
|
};
|
|
130
143
|
pi.on("session_start", (_event, context) => restoreState(context));
|
|
131
144
|
pi.on("session_tree", (_event, context) => restoreState(context));
|
|
132
|
-
pi.on("context", (event) => {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
return { messages: [...event.messages, todoListMessage] };
|
|
145
|
+
pi.on("context", (event, context) => {
|
|
146
|
+
try {
|
|
147
|
+
assertTodoJournalReadable(context);
|
|
148
|
+
return { messages: projectTodoContext(context.sessionManager.getBranch(), event.messages) };
|
|
149
|
+
} catch (cause) {
|
|
150
|
+
// Pi logs context exceptions and continues; abort the active run as well.
|
|
151
|
+
context.abort();
|
|
152
|
+
throw cause;
|
|
153
|
+
}
|
|
142
154
|
});
|
|
143
155
|
|
|
144
156
|
pi.registerTool<typeof TodoParameters, TodoToolDetails | undefined>({
|
|
@@ -147,7 +159,8 @@ export default function piTodoExtension(pi: ExtensionAPI): void {
|
|
|
147
159
|
description: "Manage the current session branch's Todo List.",
|
|
148
160
|
parameters: TodoParameters,
|
|
149
161
|
executionMode: "sequential",
|
|
150
|
-
async execute(_toolCallId, params,
|
|
162
|
+
async execute(_toolCallId, params, signal, _onUpdate, context) {
|
|
163
|
+
signal?.throwIfAborted();
|
|
151
164
|
const result = runTodoAction(params, context);
|
|
152
165
|
return {
|
|
153
166
|
content: [{ type: "text", text: result.message }],
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from "node:util";
|
|
2
|
+
import {
|
|
3
|
+
buildSessionContext,
|
|
4
|
+
type ContextEvent,
|
|
5
|
+
type SessionEntry,
|
|
6
|
+
} from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import { Type } from "typebox";
|
|
8
|
+
import { Value } from "typebox/value";
|
|
9
|
+
import {
|
|
10
|
+
formatTodoList,
|
|
11
|
+
parseTodoStateSnapshot,
|
|
12
|
+
TodoStateRecord,
|
|
13
|
+
type TodoStateSnapshot,
|
|
14
|
+
} from "./todo-list.js";
|
|
15
|
+
|
|
16
|
+
type Message = ContextEvent["messages"][number];
|
|
17
|
+
const ProjectionDetails = Type.Object(
|
|
18
|
+
{
|
|
19
|
+
version: Type.Literal(1),
|
|
20
|
+
stateEntryId: Type.String(),
|
|
21
|
+
checkpointId: Type.Union([Type.String(), Type.Null()]),
|
|
22
|
+
},
|
|
23
|
+
{ additionalProperties: false },
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
/** Context handlers never receive system messages, so anchors must skip Pi's system state. */
|
|
27
|
+
function conversationMessage(entry: SessionEntry): Message | undefined {
|
|
28
|
+
return buildSessionContext([entry], entry.id).messages.find(
|
|
29
|
+
(message) => message.role !== "system",
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Validates both the serialized shape and Todo List invariants at the journal boundary. */
|
|
34
|
+
export function todoStateFromEntry(entry: SessionEntry): TodoStateSnapshot | undefined {
|
|
35
|
+
if (
|
|
36
|
+
entry.type !== "custom" ||
|
|
37
|
+
entry.customType !== "pi-todo-state" ||
|
|
38
|
+
!Value.Check(TodoStateRecord, entry.data)
|
|
39
|
+
)
|
|
40
|
+
return undefined;
|
|
41
|
+
return parseTodoStateSnapshot(entry.data);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function snapshotMessage(
|
|
45
|
+
entry: SessionEntry,
|
|
46
|
+
state: TodoStateSnapshot,
|
|
47
|
+
checkpointId: string | null,
|
|
48
|
+
): Message {
|
|
49
|
+
return {
|
|
50
|
+
role: "custom",
|
|
51
|
+
customType: "pi-todo-context",
|
|
52
|
+
display: false,
|
|
53
|
+
content: `Todo List:\n${formatTodoList(state.tasks)}`,
|
|
54
|
+
timestamp: Date.parse(entry.timestamp),
|
|
55
|
+
details: { version: 1, stateEntryId: entry.id, checkpointId },
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Reprojects only Todo-owned snapshots at immutable journal boundaries, never at the live tail. */
|
|
60
|
+
export function projectTodoContext(
|
|
61
|
+
branch: readonly SessionEntry[],
|
|
62
|
+
incoming: ContextEvent["messages"],
|
|
63
|
+
): ContextEvent["messages"] {
|
|
64
|
+
const messages = incoming.filter(
|
|
65
|
+
(message) =>
|
|
66
|
+
!(
|
|
67
|
+
message.role === "custom" &&
|
|
68
|
+
message.customType === "pi-todo-context" &&
|
|
69
|
+
Value.Check(ProjectionDetails, message.details)
|
|
70
|
+
),
|
|
71
|
+
);
|
|
72
|
+
if (!branch.some((entry) => todoStateFromEntry(entry))) return messages;
|
|
73
|
+
const checkpoint = branch.findLast((entry) => entry.type === "compaction");
|
|
74
|
+
let start = 0;
|
|
75
|
+
let anchor: Message | undefined;
|
|
76
|
+
let previousContent: string | undefined;
|
|
77
|
+
const insertions = new Map<number, Message[]>();
|
|
78
|
+
let previousIndex = -1;
|
|
79
|
+
const insert = (snapshot: Message): void => {
|
|
80
|
+
let index = -1;
|
|
81
|
+
if (anchor) {
|
|
82
|
+
// ponytail: linear exact matching per mutation; index fingerprints if large journals make this measurable.
|
|
83
|
+
const matches = messages.flatMap((message, i) =>
|
|
84
|
+
isDeepStrictEqual(message, anchor) ? [i] : [],
|
|
85
|
+
);
|
|
86
|
+
if (matches.length !== 1)
|
|
87
|
+
throw new Error(
|
|
88
|
+
"Todo context anchor is missing or ambiguous; cannot preserve snapshot order",
|
|
89
|
+
);
|
|
90
|
+
index = matches[0]!;
|
|
91
|
+
}
|
|
92
|
+
if (index < previousIndex) throw new Error("Todo context anchors are reordered");
|
|
93
|
+
previousIndex = index;
|
|
94
|
+
const group = insertions.get(index) ?? [];
|
|
95
|
+
group.push(snapshot);
|
|
96
|
+
insertions.set(index, group);
|
|
97
|
+
};
|
|
98
|
+
if (checkpoint) {
|
|
99
|
+
start = branch.findIndex((entry) => entry.id === checkpoint.firstKeptEntryId);
|
|
100
|
+
const checkpointIndex = branch.indexOf(checkpoint);
|
|
101
|
+
if (start < 0 || start > checkpointIndex)
|
|
102
|
+
throw new Error("Todo checkpoint cutoff is unavailable");
|
|
103
|
+
anchor = conversationMessage(checkpoint);
|
|
104
|
+
for (const entry of branch.slice(0, start).toReversed()) {
|
|
105
|
+
const state = todoStateFromEntry(entry);
|
|
106
|
+
if (!state) continue;
|
|
107
|
+
previousContent = formatTodoList(state.tasks);
|
|
108
|
+
insert(snapshotMessage(entry, state, checkpoint.id));
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const outstanding = new Map<string, number>();
|
|
113
|
+
let pending: Message[] = [];
|
|
114
|
+
for (const entry of branch.slice(start)) {
|
|
115
|
+
if (entry === checkpoint) continue;
|
|
116
|
+
const state = todoStateFromEntry(entry);
|
|
117
|
+
if (state) {
|
|
118
|
+
const content = formatTodoList(state.tasks);
|
|
119
|
+
if (
|
|
120
|
+
content !== previousContent &&
|
|
121
|
+
(previousContent !== undefined || state.tasks.length > 0)
|
|
122
|
+
) {
|
|
123
|
+
const snapshot = snapshotMessage(entry, state, null);
|
|
124
|
+
if (outstanding.size > 0) pending.push(snapshot);
|
|
125
|
+
else insert(snapshot);
|
|
126
|
+
}
|
|
127
|
+
previousContent = content;
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
const message = conversationMessage(entry);
|
|
131
|
+
if (!message) continue;
|
|
132
|
+
if (message.role === "assistant") {
|
|
133
|
+
if (outstanding.size > 0 && pending.length > 0)
|
|
134
|
+
throw new Error("Todo mutation has an incomplete tool group");
|
|
135
|
+
outstanding.clear();
|
|
136
|
+
for (const block of message.content)
|
|
137
|
+
if (block.type === "toolCall")
|
|
138
|
+
outstanding.set(block.id, (outstanding.get(block.id) ?? 0) + 1);
|
|
139
|
+
} else if (message.role === "toolResult") {
|
|
140
|
+
const remaining = (outstanding.get(message.toolCallId) ?? 0) - 1;
|
|
141
|
+
if (remaining > 0) outstanding.set(message.toolCallId, remaining);
|
|
142
|
+
else outstanding.delete(message.toolCallId);
|
|
143
|
+
}
|
|
144
|
+
anchor = message;
|
|
145
|
+
if (outstanding.size === 0 && pending.length > 0) {
|
|
146
|
+
for (const snapshot of pending) insert(snapshot);
|
|
147
|
+
pending = [];
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (pending.length > 0) throw new Error("Todo mutation has an incomplete tool group");
|
|
151
|
+
return [
|
|
152
|
+
...(insertions.get(-1) ?? []),
|
|
153
|
+
...messages.flatMap((message, index) => [message, ...(insertions.get(index) ?? [])]),
|
|
154
|
+
];
|
|
155
|
+
}
|