@ian-pascoe/pi-todo 0.6.0 → 0.6.2
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 +1 -1
- package/package.json +2 -2
- package/src/pi-todo-extension.ts +9 -2
- package/src/todo-context.ts +93 -5
- package/src/todo-list.ts +1 -1
package/README.md
CHANGED
|
@@ -43,7 +43,7 @@ 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. 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 description do not change.
|
|
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 description do not change.
|
|
47
47
|
|
|
48
48
|
After compaction, a fixed baseline immediately after the summary restores the state from before the retained Tail. Retained and newer tool groups follow chronologically. This preserves previously written conversation cache prefixes between checkpoints; it does not guarantee provider cache hits.
|
|
49
49
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ian-pascoe/pi-todo",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Minimal session-native Todo List for Pi agents.",
|
|
6
6
|
"keywords": [
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"provenance": true
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@ian-pascoe/pi-utils": "^0.
|
|
36
|
+
"@ian-pascoe/pi-utils": "^0.6.0"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
39
|
"@earendil-works/pi-ai": ">=1.1.0",
|
package/src/pi-todo-extension.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { StringEnum } from "@earendil-works/pi-ai";
|
|
2
|
-
import
|
|
2
|
+
import {
|
|
3
|
+
defineTool,
|
|
4
|
+
type ExtensionAPI,
|
|
5
|
+
type ExtensionContext,
|
|
6
|
+
type Theme,
|
|
7
|
+
} from "@earendil-works/pi-coding-agent";
|
|
3
8
|
import { type Component } from "@earendil-works/pi-tui";
|
|
9
|
+
import { acceptNullForOptionalArguments } from "@ian-pascoe/pi-utils/null-optional-arguments";
|
|
4
10
|
import { noticeText } from "@ian-pascoe/pi-utils/ui";
|
|
5
11
|
import { Type } from "typebox";
|
|
6
12
|
import { projectTodoContext, todoStateFromEntry } from "./todo-context.js";
|
|
@@ -175,7 +181,7 @@ export default function piTodoExtension(pi: ExtensionAPI): void {
|
|
|
175
181
|
}
|
|
176
182
|
});
|
|
177
183
|
|
|
178
|
-
|
|
184
|
+
const todoTool = defineTool<typeof TodoParameters, TodoToolDetails | undefined>({
|
|
179
185
|
name: "todo",
|
|
180
186
|
label: "Todo",
|
|
181
187
|
description: "Manage the current session branch's Todo List.",
|
|
@@ -211,6 +217,7 @@ export default function piTodoExtension(pi: ExtensionAPI): void {
|
|
|
211
217
|
renderResult: (result, options, theme, context) =>
|
|
212
218
|
renderTodoResult(result, options, theme, context),
|
|
213
219
|
});
|
|
220
|
+
pi.registerTool(acceptNullForOptionalArguments(todoTool));
|
|
214
221
|
|
|
215
222
|
pi.registerCommand("todo", {
|
|
216
223
|
description: "Manage the Todo List",
|
package/src/todo-context.ts
CHANGED
|
@@ -4,18 +4,20 @@ 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
|
+
const ListDetails = Type.Object({ tasks: Type.Array(Type.Unknown()) });
|
|
19
21
|
const ProjectionDetails = Type.Object(
|
|
20
22
|
{
|
|
21
23
|
version: Type.Literal(1),
|
|
@@ -43,6 +45,81 @@ export function todoStateFromEntry(entry: SessionEntry): TodoStateSnapshot | und
|
|
|
43
45
|
return parseTodoStateSnapshot(entry.data);
|
|
44
46
|
}
|
|
45
47
|
|
|
48
|
+
/** A printed result's `tasks`, or a bare non-empty Task array a script returned. */
|
|
49
|
+
const ShownTasks = Type.Union([
|
|
50
|
+
Type.Object({ tasks: Type.Array(TodoTaskRecord) }),
|
|
51
|
+
Type.Array(TodoTaskRecord, { minItems: 1 }),
|
|
52
|
+
]);
|
|
53
|
+
/** Longer output is treated as not showing the list; Pi itself truncates output near this size. */
|
|
54
|
+
const MAX_SCANNED_OUTPUT_CHARS = 65_536;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The last Todo List a `codemode` script printed as JSON, however deeply it nested it.
|
|
58
|
+
*
|
|
59
|
+
* Deliberate coupling to codemode's output text: Pi records a script's nested calls on the
|
|
60
|
+
* `codemode` tool result without their results (`nestedCalls` holds names, arguments and
|
|
61
|
+
* statuses only), so what the model saw can only be read from the output. This is a best-effort
|
|
62
|
+
* dedupe: anything unrecognised keeps the Snapshot, which is always safe.
|
|
63
|
+
*
|
|
64
|
+
* One pass tracks bracket nesting and string state, and parses each balanced value that mentions
|
|
65
|
+
* Tasks, so the cost is bounded by output size times nesting depth.
|
|
66
|
+
*/
|
|
67
|
+
function lastListInText(text: string): readonly Static<typeof TodoTaskRecord>[] | undefined {
|
|
68
|
+
let last: readonly Static<typeof TodoTaskRecord>[] | undefined;
|
|
69
|
+
if (text.length > MAX_SCANNED_OUTPUT_CHARS) return last;
|
|
70
|
+
const opened: number[] = [];
|
|
71
|
+
let inString = false;
|
|
72
|
+
for (let i = 0; i < text.length; i++) {
|
|
73
|
+
const char = text[i];
|
|
74
|
+
if (inString) {
|
|
75
|
+
if (char === "\\") i++;
|
|
76
|
+
else if (char === '"') inString = false;
|
|
77
|
+
} else if (char === '"') inString = opened.length > 0;
|
|
78
|
+
else if (char === "{" || char === "[") opened.push(i);
|
|
79
|
+
else if (char === "}" || char === "]") {
|
|
80
|
+
const start = opened.pop();
|
|
81
|
+
if (start === undefined || text[start] !== (char === "}" ? "{" : "[")) {
|
|
82
|
+
opened.length = 0;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
const candidate = text.slice(start, i + 1);
|
|
86
|
+
if (!candidate.includes('"title"') && !candidate.includes('"tasks"')) continue;
|
|
87
|
+
try {
|
|
88
|
+
const value = JSON.parse(candidate);
|
|
89
|
+
if (Value.Check(ShownTasks, value)) last = Array.isArray(value) ? value : value.tasks;
|
|
90
|
+
} catch {
|
|
91
|
+
// Not JSON: prose that merely contains brackets.
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return last;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** True when Pi recorded a nested `todo` call on this `codemode` result. */
|
|
99
|
+
function codemodeCalledTodo(message: Message): boolean {
|
|
100
|
+
return (
|
|
101
|
+
message.role === "toolResult" &&
|
|
102
|
+
message.toolName === "codemode" &&
|
|
103
|
+
(message.nestedCalls?.calls ?? []).some((call) => call.name === "todo")
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* True when a successful `todo` result's details, or the last list the output of a successful
|
|
109
|
+
* `codemode` script that called `todo` printed, carry exactly the complete resulting list.
|
|
110
|
+
*/
|
|
111
|
+
function resultRendersList(message: Message | undefined, tasks: TodoStateSnapshot["tasks"]) {
|
|
112
|
+
if (message?.role !== "toolResult" || message.isError) return false;
|
|
113
|
+
if (message.toolName === "todo")
|
|
114
|
+
return (
|
|
115
|
+
Value.Check(ListDetails, message.details) && isDeepStrictEqual(message.details.tasks, tasks)
|
|
116
|
+
);
|
|
117
|
+
if (!codemodeCalledTodo(message)) return false;
|
|
118
|
+
const text = message.content.flatMap((block) => (block.type === "text" ? [block.text] : []));
|
|
119
|
+
const shown = lastListInText(text.join("\n"));
|
|
120
|
+
return shown !== undefined && isDeepStrictEqual(shown, tasks);
|
|
121
|
+
}
|
|
122
|
+
|
|
46
123
|
function snapshotMessage(
|
|
47
124
|
entry: SessionEntry,
|
|
48
125
|
state: TodoStateSnapshot,
|
|
@@ -112,11 +189,19 @@ export function projectTodoContext(
|
|
|
112
189
|
}
|
|
113
190
|
}
|
|
114
191
|
const outstanding = new Map<string, number>();
|
|
115
|
-
// A tool group projects only its final state, once its last result has landed.
|
|
192
|
+
// A tool group projects only its final state, once its last result has landed. Nested `todo`
|
|
193
|
+
// calls in a `codemode` script leave no results in the journal, so the script's result stands in.
|
|
194
|
+
let lastTodoResult: Message | undefined;
|
|
116
195
|
let pending: { entry: SessionEntry; state: TodoStateSnapshot } | undefined;
|
|
117
|
-
const project = (entry: SessionEntry, state: TodoStateSnapshot): void => {
|
|
196
|
+
const project = (entry: SessionEntry, state: TodoStateSnapshot, finalResult?: Message): void => {
|
|
118
197
|
const content = formatTodoList(state.tasks);
|
|
119
|
-
|
|
198
|
+
// The model already saw this exact list in the group's last result; a Snapshot would repeat it.
|
|
199
|
+
const rendered = resultRendersList(finalResult, state.tasks);
|
|
200
|
+
if (
|
|
201
|
+
!rendered &&
|
|
202
|
+
content !== previousContent &&
|
|
203
|
+
(previousContent !== undefined || state.tasks.length > 0)
|
|
204
|
+
)
|
|
120
205
|
insert(snapshotMessage(entry, state, null));
|
|
121
206
|
previousContent = content;
|
|
122
207
|
};
|
|
@@ -134,17 +219,20 @@ export function projectTodoContext(
|
|
|
134
219
|
if (outstanding.size > 0 && pending)
|
|
135
220
|
throw new Error("Todo mutation has an incomplete tool group");
|
|
136
221
|
outstanding.clear();
|
|
222
|
+
lastTodoResult = undefined;
|
|
137
223
|
for (const block of message.content)
|
|
138
224
|
if (block.type === "toolCall")
|
|
139
225
|
outstanding.set(block.id, (outstanding.get(block.id) ?? 0) + 1);
|
|
140
226
|
} else if (message.role === "toolResult") {
|
|
227
|
+
// A `codemode` result counts only when it recorded a nested `todo` call.
|
|
228
|
+
if (message.toolName === "todo" || codemodeCalledTodo(message)) lastTodoResult = message;
|
|
141
229
|
const remaining = (outstanding.get(message.toolCallId) ?? 0) - 1;
|
|
142
230
|
if (remaining > 0) outstanding.set(message.toolCallId, remaining);
|
|
143
231
|
else outstanding.delete(message.toolCallId);
|
|
144
232
|
}
|
|
145
233
|
anchor = message;
|
|
146
234
|
if (outstanding.size === 0 && pending) {
|
|
147
|
-
project(pending.entry, pending.state);
|
|
235
|
+
project(pending.entry, pending.state, lastTodoResult);
|
|
148
236
|
pending = undefined;
|
|
149
237
|
}
|
|
150
238
|
}
|
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 })),
|