@ian-pascoe/pi-todo 0.6.1 → 0.6.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 +4 -2
- package/package.json +1 -1
- package/src/pi-todo-extension.ts +7 -0
- package/src/todo-context.ts +109 -11
- package/src/todo-list.ts +1 -1
package/README.md
CHANGED
|
@@ -43,9 +43,11 @@ A Pi `codemode` script receives the result as an object (the tool declares an `o
|
|
|
43
43
|
|
|
44
44
|
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.
|
|
45
45
|
|
|
46
|
-
Each tool group that changes the Todo List is projected from the immutable session state entries as one hidden full **Todo List Snapshot** of the group's final state, at a fixed conversation position after all of the group's sibling results. A tool group is one assistant turn's tool calls, including several `todo` calls inside one `codemode` script, so ten calls in a script add one Todo List Snapshot, not ten. A group that ends in the state it began with adds none, and so does a group whose last `todo` result is a successful `list`, batch `add`, or batch `update` whose `details.tasks` equal the complete final Todo List, since the model already saw that list. Partial results, such as a single `update` or a batch `update` of some Tasks, still add one. A `codemode`
|
|
46
|
+
Each tool group that changes the Todo List is projected from the immutable session state entries as one hidden full **Todo List Snapshot** of the group's final state, at a fixed conversation position after all of the group's sibling results. A tool group is one assistant turn's tool calls, including several `todo` calls inside one `codemode` script, so ten calls in a script add one Todo List Snapshot, not ten. A group that ends in the state it began with adds none, and so does a group whose last `todo` result is a successful `list`, batch `add`, or batch `update` whose `details.tasks` equal the complete final Todo List, since the model already saw that list. Partial results, such as a single `update` or a batch `update` of some Tasks, still add one. A `codemode` result counts only when Pi recorded a nested `todo` call on it, so an unrelated script never replaces an earlier `todo` result. Such a group skips the same way when its result is successful, its output is at most 64 KiB, and the last Todo List it prints as JSON (such as a nested `todo` call's returned value) equals the complete final Todo List; Pi records nested calls without their results, so the script's output text is the only evidence, and a script that prints no list, a partial result, or a stale list still adds one. A change made outside a tool group, such as `/todo clear`, still projects its own Todo List Snapshot right away. Each Todo List Snapshot starts with the header `Todo List state from the pi-todo extension (not a user message):` so it does not read like user input. Later requests keep earlier Todo List Snapshots in place. Clearing the list produces an explicit empty Todo List Snapshot. The system prompt and tool definition stay the same for the whole session.
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
The tool adds one line to the Available tools section of Pi's default system prompt (`todo: Track this session's Tasks in the Todo List`) and one guideline: when the Todo List has Tasks, mark a Task active when you start it and completed as soon as it is done, and bring the list up to date before context is compacted. Both are fixed text, so they never change the prompt prefix. A custom system prompt (`SYSTEM.md`) omits tool snippets and guidelines, as Pi does for every tool.
|
|
49
|
+
|
|
50
|
+
After compaction, a fixed baseline immediately after the summary restores the state from before the retained Tail. Retained and newer tool groups follow chronologically. When the Todo List still has unfinished Tasks at the checkpoint, a **Checkpoint Snapshot** follows the retained Tail, where the next Context Window resumes: it repeats the list as compaction left it and asks the agent to reconcile it with the work already done and the plan carried forward (a Handoff's own next steps otherwise quietly replace the list). It replaces the Tail's last Snapshot when that sits at the same position and shows the same list, such as the baseline when the Tail is empty, so the list is not printed twice. An empty or fully completed list adds none. This preserves previously written conversation cache prefixes between checkpoints; it does not guarantee provider cache hits.
|
|
49
51
|
|
|
50
52
|
`todo` declares MCP-style `annotations`: not read-only, but non-destructive (it only appends to the session's own journal), not idempotent, and closed-world. Pi reports them through `pi.getAllTools()` for permission extensions and does not send them to model providers.
|
|
51
53
|
|
package/package.json
CHANGED
package/src/pi-todo-extension.ts
CHANGED
|
@@ -40,6 +40,11 @@ function assertTodoJournalReadable(context: ExtensionContext): void {
|
|
|
40
40
|
}
|
|
41
41
|
const TODO_WIDGET_ID = "pi-todo";
|
|
42
42
|
|
|
43
|
+
/** Static system prompt lines, so they never move the cache prefix within a session. */
|
|
44
|
+
export const TODO_PROMPT_SNIPPET = "Track this session's Tasks in the Todo List";
|
|
45
|
+
export const TODO_PROMPT_GUIDELINE =
|
|
46
|
+
"When the Todo List has Tasks, keep it current: mark a Task active when you start it and completed as soon as it is done, and bring it up to date before context is compacted.";
|
|
47
|
+
|
|
43
48
|
const TodoParameters = Type.Object({
|
|
44
49
|
action: StringEnum(TODO_ACTIONS),
|
|
45
50
|
id: Type.Optional(
|
|
@@ -185,6 +190,8 @@ export default function piTodoExtension(pi: ExtensionAPI): void {
|
|
|
185
190
|
name: "todo",
|
|
186
191
|
label: "Todo",
|
|
187
192
|
description: "Manage the current session branch's Todo List.",
|
|
193
|
+
promptSnippet: TODO_PROMPT_SNIPPET,
|
|
194
|
+
promptGuidelines: [TODO_PROMPT_GUIDELINE],
|
|
188
195
|
parameters: TodoParameters,
|
|
189
196
|
annotations: {
|
|
190
197
|
readOnlyHint: false,
|
package/src/todo-context.ts
CHANGED
|
@@ -4,18 +4,22 @@ import {
|
|
|
4
4
|
type ContextEvent,
|
|
5
5
|
type SessionEntry,
|
|
6
6
|
} from "@earendil-works/pi-coding-agent";
|
|
7
|
-
import { Type } from "typebox";
|
|
7
|
+
import { Type, type Static } from "typebox";
|
|
8
8
|
import { Value } from "typebox/value";
|
|
9
9
|
import {
|
|
10
10
|
formatTodoList,
|
|
11
11
|
parseTodoStateSnapshot,
|
|
12
12
|
TodoStateRecord,
|
|
13
|
+
TodoTaskRecord,
|
|
13
14
|
type TodoStateSnapshot,
|
|
14
15
|
} from "./todo-list.js";
|
|
15
16
|
|
|
16
17
|
type Message = ContextEvent["messages"][number];
|
|
17
18
|
/** Marks a snapshot as extension state so the model does not read it as user-authored. */
|
|
18
19
|
const SNAPSHOT_HEADER = "Todo List state from the pi-todo extension (not a user message):";
|
|
20
|
+
/** Closes a Checkpoint Snapshot: a Handoff's own plan otherwise silently replaces the Todo List. */
|
|
21
|
+
export const CHECKPOINT_INSTRUCTION =
|
|
22
|
+
"Context was compacted while Tasks were unfinished. Before continuing, reconcile this Todo List with the work already done and the plan carried forward: mark finished Tasks completed, remove abandoned ones, and keep it current as you work.";
|
|
19
23
|
const ListDetails = Type.Object({ tasks: Type.Array(Type.Unknown()) });
|
|
20
24
|
const ProjectionDetails = Type.Object(
|
|
21
25
|
{
|
|
@@ -44,25 +48,93 @@ export function todoStateFromEntry(entry: SessionEntry): TodoStateSnapshot | und
|
|
|
44
48
|
return parseTodoStateSnapshot(entry.data);
|
|
45
49
|
}
|
|
46
50
|
|
|
47
|
-
/**
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
+
/** A printed result's `tasks`, or a bare non-empty Task array a script returned. */
|
|
52
|
+
const ShownTasks = Type.Union([
|
|
53
|
+
Type.Object({ tasks: Type.Array(TodoTaskRecord) }),
|
|
54
|
+
Type.Array(TodoTaskRecord, { minItems: 1 }),
|
|
55
|
+
]);
|
|
56
|
+
/** Longer output is treated as not showing the list; Pi itself truncates output near this size. */
|
|
57
|
+
const MAX_SCANNED_OUTPUT_CHARS = 65_536;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The last Todo List a `codemode` script printed as JSON, however deeply it nested it.
|
|
61
|
+
*
|
|
62
|
+
* Deliberate coupling to codemode's output text: Pi records a script's nested calls on the
|
|
63
|
+
* `codemode` tool result without their results (`nestedCalls` holds names, arguments and
|
|
64
|
+
* statuses only), so what the model saw can only be read from the output. This is a best-effort
|
|
65
|
+
* dedupe: anything unrecognised keeps the Snapshot, which is always safe.
|
|
66
|
+
*
|
|
67
|
+
* One pass tracks bracket nesting and string state, and parses each balanced value that mentions
|
|
68
|
+
* Tasks, so the cost is bounded by output size times nesting depth.
|
|
69
|
+
*/
|
|
70
|
+
function lastListInText(text: string): readonly Static<typeof TodoTaskRecord>[] | undefined {
|
|
71
|
+
let last: readonly Static<typeof TodoTaskRecord>[] | undefined;
|
|
72
|
+
if (text.length > MAX_SCANNED_OUTPUT_CHARS) return last;
|
|
73
|
+
const opened: number[] = [];
|
|
74
|
+
let inString = false;
|
|
75
|
+
for (let i = 0; i < text.length; i++) {
|
|
76
|
+
const char = text[i];
|
|
77
|
+
if (inString) {
|
|
78
|
+
if (char === "\\") i++;
|
|
79
|
+
else if (char === '"') inString = false;
|
|
80
|
+
} else if (char === '"') inString = opened.length > 0;
|
|
81
|
+
else if (char === "{" || char === "[") opened.push(i);
|
|
82
|
+
else if (char === "}" || char === "]") {
|
|
83
|
+
const start = opened.pop();
|
|
84
|
+
if (start === undefined || text[start] !== (char === "}" ? "{" : "[")) {
|
|
85
|
+
opened.length = 0;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const candidate = text.slice(start, i + 1);
|
|
89
|
+
if (!candidate.includes('"title"') && !candidate.includes('"tasks"')) continue;
|
|
90
|
+
try {
|
|
91
|
+
const value = JSON.parse(candidate);
|
|
92
|
+
if (Value.Check(ShownTasks, value)) last = Array.isArray(value) ? value : value.tasks;
|
|
93
|
+
} catch {
|
|
94
|
+
// Not JSON: prose that merely contains brackets.
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return last;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** True when Pi recorded a nested `todo` call on this `codemode` result. */
|
|
102
|
+
function codemodeCalledTodo(message: Message): boolean {
|
|
51
103
|
return (
|
|
52
|
-
|
|
104
|
+
message.role === "toolResult" &&
|
|
105
|
+
message.toolName === "codemode" &&
|
|
106
|
+
(message.nestedCalls?.calls ?? []).some((call) => call.name === "todo")
|
|
53
107
|
);
|
|
54
108
|
}
|
|
55
109
|
|
|
110
|
+
/**
|
|
111
|
+
* True when a successful `todo` result's details, or the last list the output of a successful
|
|
112
|
+
* `codemode` script that called `todo` printed, carry exactly the complete resulting list.
|
|
113
|
+
*/
|
|
114
|
+
function resultRendersList(message: Message | undefined, tasks: TodoStateSnapshot["tasks"]) {
|
|
115
|
+
if (message?.role !== "toolResult" || message.isError) return false;
|
|
116
|
+
if (message.toolName === "todo")
|
|
117
|
+
return (
|
|
118
|
+
Value.Check(ListDetails, message.details) && isDeepStrictEqual(message.details.tasks, tasks)
|
|
119
|
+
);
|
|
120
|
+
if (!codemodeCalledTodo(message)) return false;
|
|
121
|
+
const text = message.content.flatMap((block) => (block.type === "text" ? [block.text] : []));
|
|
122
|
+
const shown = lastListInText(text.join("\n"));
|
|
123
|
+
return shown !== undefined && isDeepStrictEqual(shown, tasks);
|
|
124
|
+
}
|
|
125
|
+
|
|
56
126
|
function snapshotMessage(
|
|
57
127
|
entry: SessionEntry,
|
|
58
128
|
state: TodoStateSnapshot,
|
|
59
129
|
checkpointId: string | null,
|
|
130
|
+
instruction?: string,
|
|
60
131
|
): Message {
|
|
132
|
+
const list = `${SNAPSHOT_HEADER}\n${formatTodoList(state.tasks)}`;
|
|
61
133
|
return {
|
|
62
134
|
role: "custom",
|
|
63
135
|
customType: "pi-todo-context",
|
|
64
136
|
display: false,
|
|
65
|
-
content: `${
|
|
137
|
+
content: instruction === undefined ? list : `${list}\n\n${instruction}`,
|
|
66
138
|
timestamp: Date.parse(entry.timestamp),
|
|
67
139
|
details: { version: 1, stateEntryId: entry.id, checkpointId },
|
|
68
140
|
};
|
|
@@ -86,9 +158,12 @@ export function projectTodoContext(
|
|
|
86
158
|
let start = 0;
|
|
87
159
|
let anchor: Message | undefined;
|
|
88
160
|
let previousContent: string | undefined;
|
|
161
|
+
// The latest state at the point being projected; at the checkpoint, the state it compacted.
|
|
162
|
+
let current: { entry: SessionEntry; state: TodoStateSnapshot } | undefined;
|
|
89
163
|
const insertions = new Map<number, Message[]>();
|
|
90
164
|
let previousIndex = -1;
|
|
91
|
-
|
|
165
|
+
/** `supersedes` replaces the last Snapshot at the same position when it shows the same list. */
|
|
166
|
+
const insert = (snapshot: Message, supersedes?: string): void => {
|
|
92
167
|
let index = -1;
|
|
93
168
|
if (anchor) {
|
|
94
169
|
// ponytail: linear exact matching per mutation; index fingerprints if large journals make this measurable.
|
|
@@ -104,6 +179,8 @@ export function projectTodoContext(
|
|
|
104
179
|
if (index < previousIndex) throw new Error("Todo context anchors are reordered");
|
|
105
180
|
previousIndex = index;
|
|
106
181
|
const group = insertions.get(index) ?? [];
|
|
182
|
+
const last = group.at(-1);
|
|
183
|
+
if (last?.role === "custom" && last.content === supersedes) group.pop();
|
|
107
184
|
group.push(snapshot);
|
|
108
185
|
insertions.set(index, group);
|
|
109
186
|
};
|
|
@@ -116,13 +193,29 @@ export function projectTodoContext(
|
|
|
116
193
|
for (const entry of branch.slice(0, start).toReversed()) {
|
|
117
194
|
const state = todoStateFromEntry(entry);
|
|
118
195
|
if (!state) continue;
|
|
196
|
+
current = { entry, state };
|
|
119
197
|
previousContent = formatTodoList(state.tasks);
|
|
120
198
|
insert(snapshotMessage(entry, state, checkpoint.id));
|
|
121
199
|
break;
|
|
122
200
|
}
|
|
123
201
|
}
|
|
202
|
+
/**
|
|
203
|
+
* After the retained Tail, where the next Context Window resumes, unfinished Tasks get one
|
|
204
|
+
* Checkpoint Snapshot asking the agent to reconcile them. It replaces an identical Snapshot at the
|
|
205
|
+
* same position, such as the baseline when the Tail is empty, rather than repeating the list.
|
|
206
|
+
*/
|
|
207
|
+
const projectCheckpoint = (checkpointId: string): void => {
|
|
208
|
+
if (!current?.state.tasks.some((task) => task.status !== "completed")) return;
|
|
209
|
+
const { entry, state } = current;
|
|
210
|
+
previousContent = formatTodoList(state.tasks);
|
|
211
|
+
insert(
|
|
212
|
+
snapshotMessage(entry, state, checkpointId, CHECKPOINT_INSTRUCTION),
|
|
213
|
+
`${SNAPSHOT_HEADER}\n${previousContent}`,
|
|
214
|
+
);
|
|
215
|
+
};
|
|
124
216
|
const outstanding = new Map<string, number>();
|
|
125
|
-
// A tool group projects only its final state, once its last result has landed.
|
|
217
|
+
// A tool group projects only its final state, once its last result has landed. Nested `todo`
|
|
218
|
+
// calls in a `codemode` script leave no results in the journal, so the script's result stands in.
|
|
126
219
|
let lastTodoResult: Message | undefined;
|
|
127
220
|
let pending: { entry: SessionEntry; state: TodoStateSnapshot } | undefined;
|
|
128
221
|
const project = (entry: SessionEntry, state: TodoStateSnapshot, finalResult?: Message): void => {
|
|
@@ -138,9 +231,13 @@ export function projectTodoContext(
|
|
|
138
231
|
previousContent = content;
|
|
139
232
|
};
|
|
140
233
|
for (const entry of branch.slice(start)) {
|
|
141
|
-
if (entry === checkpoint)
|
|
234
|
+
if (entry === checkpoint) {
|
|
235
|
+
projectCheckpoint(entry.id);
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
142
238
|
const state = todoStateFromEntry(entry);
|
|
143
239
|
if (state) {
|
|
240
|
+
current = { entry, state };
|
|
144
241
|
if (outstanding.size > 0) pending = { entry, state };
|
|
145
242
|
else project(entry, state);
|
|
146
243
|
continue;
|
|
@@ -156,7 +253,8 @@ export function projectTodoContext(
|
|
|
156
253
|
if (block.type === "toolCall")
|
|
157
254
|
outstanding.set(block.id, (outstanding.get(block.id) ?? 0) + 1);
|
|
158
255
|
} else if (message.role === "toolResult") {
|
|
159
|
-
|
|
256
|
+
// A `codemode` result counts only when it recorded a nested `todo` call.
|
|
257
|
+
if (message.toolName === "todo" || codemodeCalledTodo(message)) lastTodoResult = message;
|
|
160
258
|
const remaining = (outstanding.get(message.toolCallId) ?? 0) - 1;
|
|
161
259
|
if (remaining > 0) outstanding.set(message.toolCallId, remaining);
|
|
162
260
|
else outstanding.delete(message.toolCallId);
|
package/src/todo-list.ts
CHANGED
|
@@ -73,7 +73,7 @@ const PositiveSafeIntegerRecord = Type.Integer({
|
|
|
73
73
|
minimum: 1,
|
|
74
74
|
maximum: Number.MAX_SAFE_INTEGER,
|
|
75
75
|
});
|
|
76
|
-
const TodoTaskRecord = Type.Object({
|
|
76
|
+
export const TodoTaskRecord = Type.Object({
|
|
77
77
|
id: PositiveSafeIntegerRecord,
|
|
78
78
|
title: Type.String({ minLength: 1 }),
|
|
79
79
|
description: Type.Optional(Type.String({ minLength: 1 })),
|