@ian-pascoe/pi-todo 0.6.1 → 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 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, 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` group never skips, because its result is not a `todo` result. 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.1",
3
+ "version": "0.6.2",
4
4
  "private": false,
5
5
  "description": "Minimal session-native Todo List for Pi agents.",
6
6
  "keywords": [
@@ -4,12 +4,13 @@ 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
 
@@ -44,15 +45,81 @@ export function todoStateFromEntry(entry: SessionEntry): TodoStateSnapshot | und
44
45
  return parseTodoStateSnapshot(entry.data);
45
46
  }
46
47
 
47
- /** True when a successful `todo` result's details carry exactly the complete resulting list. */
48
- function resultRendersList(message: Message | undefined, tasks: TodoStateSnapshot["tasks"]) {
49
- if (message?.role !== "toolResult" || message.toolName !== "todo" || message.isError)
50
- return false;
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 {
51
100
  return (
52
- Value.Check(ListDetails, message.details) && isDeepStrictEqual(message.details.tasks, tasks)
101
+ message.role === "toolResult" &&
102
+ message.toolName === "codemode" &&
103
+ (message.nestedCalls?.calls ?? []).some((call) => call.name === "todo")
53
104
  );
54
105
  }
55
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
+
56
123
  function snapshotMessage(
57
124
  entry: SessionEntry,
58
125
  state: TodoStateSnapshot,
@@ -122,7 +189,8 @@ export function projectTodoContext(
122
189
  }
123
190
  }
124
191
  const outstanding = new Map<string, number>();
125
- // 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.
126
194
  let lastTodoResult: Message | undefined;
127
195
  let pending: { entry: SessionEntry; state: TodoStateSnapshot } | undefined;
128
196
  const project = (entry: SessionEntry, state: TodoStateSnapshot, finalResult?: Message): void => {
@@ -156,7 +224,8 @@ export function projectTodoContext(
156
224
  if (block.type === "toolCall")
157
225
  outstanding.set(block.id, (outstanding.get(block.id) ?? 0) + 1);
158
226
  } else if (message.role === "toolResult") {
159
- if (message.toolName === "todo") lastTodoResult = message;
227
+ // A `codemode` result counts only when it recorded a nested `todo` call.
228
+ if (message.toolName === "todo" || codemodeCalledTodo(message)) lastTodoResult = message;
160
229
  const remaining = (outstanding.get(message.toolCallId) ?? 0) - 1;
161
230
  if (remaining > 0) outstanding.set(message.toolCallId, remaining);
162
231
  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 })),