pi-todo-ledger 0.2.0 → 0.3.0
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/CONTRACT.md +5 -2
- package/README.md +2 -1
- package/docs/tool-schema.md +4 -1
- package/package.json +1 -1
- package/schema/todo-v1.schema.json +4 -0
- package/src/ledger/contract.ts +3 -2
- package/src/ledger/import-snapshot.ts +9 -2
- package/src/ledger/schema.ts +1 -1
- package/src/state/state-reducer.ts +6 -0
- package/src/todo.ts +1 -1
- package/src/tool/types.ts +5 -0
package/CONTRACT.md
CHANGED
|
@@ -15,6 +15,8 @@ The schema's `$defs/task` is the exact definition.
|
|
|
15
15
|
| Property | Type | Meaning |
|
|
16
16
|
| --- | --- | --- |
|
|
17
17
|
| `id` | integer ≥ 1 | Unique within one list, that is one branch of one session. Ids are not stable identities: `clear` restarts them at 1, and two branches of a session (after `/tree`) can give the same id to different tasks. |
|
|
18
|
+
| `uid` | string, optional | A random `t_` plus ten lowercase base32 characters. Create assigns it once; update, replay, and import retain it. Older tasks keep no uid. |
|
|
19
|
+
| `origin` | object, optional | On import from a known session, `{ session }` identifies the session that wrote the imported snapshot. |
|
|
18
20
|
| `subject` | string | Short imperative line. |
|
|
19
21
|
| `status` | `pending`, `in_progress`, `completed`, `deleted` | `deleted` is a tombstone that stays in the list. |
|
|
20
22
|
| `description` | string, optional | Long-form detail. |
|
|
@@ -26,11 +28,12 @@ The schema's `$defs/task` is the exact definition.
|
|
|
26
28
|
| `anchor` | non-empty string, optional | The check that shows the task is done, such as a test command. |
|
|
27
29
|
| `evidence` | Evidence[], optional | Runs of checks, oldest first. |
|
|
28
30
|
|
|
29
|
-
An evidence entry is `{ command, sha?, result, at }`:
|
|
31
|
+
An evidence entry is `{ command, sha?, ref?, result, at }`:
|
|
30
32
|
|
|
31
33
|
| Property | Type | Meaning |
|
|
32
34
|
| --- | --- | --- |
|
|
33
35
|
| `command` | non-empty string | The check that ran. |
|
|
36
|
+
| `ref` | string, optional | Tool call id of the run, or `child:<id>`; up to 64 characters without whitespace. |
|
|
34
37
|
| `sha` | string, optional | Revision the check ran on, such as a git commit SHA. |
|
|
35
38
|
| `result` | `pass` or `fail` | How the run ended. |
|
|
36
39
|
| `at` | string | ISO 8601 time of the run in UTC, as `Date.prototype.toISOString` writes it. The tool converts the caller's time to that form, fills in the call time when the caller leaves it out, and rejects a time more than five minutes after the call. |
|
|
@@ -69,7 +72,7 @@ Calls that change nothing write no entry: `list`, `get`, an `update` whose value
|
|
|
69
72
|
Since 0.2.0, `todo` accepts `{ "action": "import", "path": "handover.json" }` as an additive operation within v1.
|
|
70
73
|
The file contains the snapshot `data` itself, not the surrounding session entry.
|
|
71
74
|
Import requires an empty list (even a tombstone counts as a task), unique task ids, a task for every `blockedBy` id, and a `nextId` greater than every task id.
|
|
72
|
-
|
|
75
|
+
New snapshots carry the writer's `sessionId`. Import uses it to set each task's `origin.session`, replacing any previous source. If the snapshot lacks `sessionId`, import leaves `origin` untouched. All other task fields, including historical evidence timestamps, and the supplied `nextId` are preserved.
|
|
73
76
|
|
|
74
77
|
### Migration from rpiv-todo
|
|
75
78
|
|
package/README.md
CHANGED
|
@@ -8,7 +8,8 @@ On top of them it publishes a bookkeeping contract, [CONTRACT.md](CONTRACT.md):
|
|
|
8
8
|
- **Change event.** `todo:changed` on `pi.events` carries `{ v: 1, sessionId, tasks }` after every change and when a session's list is restored.
|
|
9
9
|
- **Namespace validators.** An extension that owns `metadata.<ns>` registers a validator with `todo:namespace`; the tool runs it before committing and returns its refusal as a tool error.
|
|
10
10
|
- **Change guards.** An extension that must approve whole calls (including creates without metadata, batches, deletes, and `clear`) registers an async guard with `todo:guard`; the tool awaits it before committing.
|
|
11
|
-
- **
|
|
11
|
+
- **Stable identity.** Each new task gets a `uid` that survives replay and import. Imported tasks also name their source session when the snapshot records it.
|
|
12
|
+
- **Bookkeeping fields.** Tasks gain `points` (effort, 0 to 10), `anchor` (the check that shows the task is done), and `evidence` (an append-only list of check runs, each `{ command, sha?, ref?, result, at }`).
|
|
12
13
|
|
|
13
14
|
## Replaces rpiv-todo
|
|
14
15
|
|
package/docs/tool-schema.md
CHANGED
|
@@ -48,6 +48,7 @@ todo({
|
|
|
48
48
|
anchor?: string, // the check that shows the task is done; update replaces
|
|
49
49
|
evidence?: Array<{ // check runs; update appends, never replaces
|
|
50
50
|
command: string,
|
|
51
|
+
ref?: string, // tool call id or child:<id>; ≤64 characters, no whitespace
|
|
51
52
|
sha?: string, // revision the check ran on
|
|
52
53
|
result: "pass" | "fail",
|
|
53
54
|
at?: string, // ISO 8601; stored in UTC; defaults to the time of the call
|
|
@@ -129,6 +130,8 @@ tasks' `blockedBy` arrays.
|
|
|
129
130
|
params: Record<string, unknown>,
|
|
130
131
|
tasks: Array<{
|
|
131
132
|
id: number,
|
|
133
|
+
uid?: string, // stable across branches and import
|
|
134
|
+
origin?: { session: string }, // source session on import, when known
|
|
132
135
|
subject: string,
|
|
133
136
|
description?: string,
|
|
134
137
|
activeForm?: string,
|
|
@@ -138,7 +141,7 @@ tasks' `blockedBy` arrays.
|
|
|
138
141
|
metadata?: Record<string, unknown>,
|
|
139
142
|
points?: number,
|
|
140
143
|
anchor?: string,
|
|
141
|
-
evidence?: Array<{ command: string, sha?: string, result: "pass" | "fail", at: string }>,
|
|
144
|
+
evidence?: Array<{ command: string, sha?: string, ref?: string, result: "pass" | "fail", at: string }>,
|
|
142
145
|
}>,
|
|
143
146
|
nextId: number,
|
|
144
147
|
error?: string, // present only on a rejected call
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-todo-ledger",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A todo list for pi agents with a public bookkeeping contract: snapshot entries, change events, and namespace validators. Fork of @juicesharp/rpiv-todo",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"required": ["v", "tasks", "nextId"],
|
|
11
11
|
"properties": {
|
|
12
12
|
"v": { "const": 1 },
|
|
13
|
+
"sessionId": { "type": "string", "minLength": 1 },
|
|
13
14
|
"tasks": {
|
|
14
15
|
"description": "Every task in id order, including deleted tombstones.",
|
|
15
16
|
"type": "array",
|
|
@@ -30,6 +31,8 @@
|
|
|
30
31
|
"required": ["id", "subject", "status"],
|
|
31
32
|
"properties": {
|
|
32
33
|
"id": { "type": "integer", "minimum": 1 },
|
|
34
|
+
"uid": { "type": "string", "pattern": "^t_[a-z2-7]{10}$" },
|
|
35
|
+
"origin": { "type": "object", "required": ["session"], "properties": { "session": { "type": "string", "minLength": 1 } } },
|
|
33
36
|
"subject": { "type": "string" },
|
|
34
37
|
"status": { "$ref": "#/$defs/taskStatus" },
|
|
35
38
|
"description": { "type": "string" },
|
|
@@ -72,6 +75,7 @@
|
|
|
72
75
|
"type": "object",
|
|
73
76
|
"required": ["command", "result", "at"],
|
|
74
77
|
"properties": {
|
|
78
|
+
"ref": { "type": "string", "maxLength": 64, "pattern": "^\\S*$" },
|
|
75
79
|
"command": {
|
|
76
80
|
"description": "The check that ran.",
|
|
77
81
|
"type": "string",
|
package/src/ledger/contract.ts
CHANGED
|
@@ -32,6 +32,7 @@ export const READY_EVENT = "todo:ready";
|
|
|
32
32
|
|
|
33
33
|
/** Data of a `todo/v1` session entry. */
|
|
34
34
|
export interface TodoSnapshotV1 {
|
|
35
|
+
sessionId?: string;
|
|
35
36
|
v: 1;
|
|
36
37
|
tasks: Task[];
|
|
37
38
|
nextId: number;
|
|
@@ -104,8 +105,8 @@ export function copyTasks(tasks: readonly Task[]): Task[] {
|
|
|
104
105
|
return structuredClone(tasks) as Task[];
|
|
105
106
|
}
|
|
106
107
|
|
|
107
|
-
export function toSnapshot(state: TaskState): TodoSnapshotV1 {
|
|
108
|
-
return { v: CONTRACT_VERSION, tasks: copyTasks(state.tasks), nextId: state.nextId };
|
|
108
|
+
export function toSnapshot(state: TaskState, sessionId: string): TodoSnapshotV1 {
|
|
109
|
+
return { v: CONTRACT_VERSION, sessionId, tasks: copyTasks(state.tasks), nextId: state.nextId };
|
|
109
110
|
}
|
|
110
111
|
|
|
111
112
|
export function toChangedEvent(sessionId: string, state: TaskState): TodoChangedEventV1 {
|
|
@@ -16,8 +16,13 @@ export async function readImport(before: TaskState, cwd: string, path?: string,
|
|
|
16
16
|
}
|
|
17
17
|
if (!isSnapshotV1(snapshot)) throw new Error("import file does not match the todo/v1 snapshot schema");
|
|
18
18
|
const ids = new Set<number>();
|
|
19
|
-
|
|
19
|
+
const uids = new Set<string>();
|
|
20
|
+
for (const { id, uid } of snapshot.tasks) {
|
|
20
21
|
if (ids.has(id)) throw new Error(`duplicate task id #${id}`);
|
|
22
|
+
if (uid !== undefined) {
|
|
23
|
+
if (uids.has(uid)) throw new Error(`duplicate task uid ${uid} (#${id})`);
|
|
24
|
+
uids.add(uid);
|
|
25
|
+
}
|
|
21
26
|
if (snapshot.nextId <= id) throw new Error(`nextId must be greater than task id #${id}`);
|
|
22
27
|
ids.add(id);
|
|
23
28
|
}
|
|
@@ -26,5 +31,7 @@ export async function readImport(before: TaskState, cwd: string, path?: string,
|
|
|
26
31
|
if (!ids.has(dep)) throw new Error(`#${task.id} blockedBy: #${dep} not found`);
|
|
27
32
|
}
|
|
28
33
|
}
|
|
29
|
-
|
|
34
|
+
const session = snapshot.sessionId;
|
|
35
|
+
const tasks = session ? snapshot.tasks.map(task => ({ ...task, origin: { session } })) : snapshot.tasks;
|
|
36
|
+
return { tasks, nextId: snapshot.nextId };
|
|
30
37
|
}
|
package/src/ledger/schema.ts
CHANGED
|
@@ -16,6 +16,6 @@ export const TODO_V1_SCHEMA: Record<string, unknown> = JSON.parse(readFileSync(S
|
|
|
16
16
|
const validator = Compile(TODO_V1_SCHEMA as never);
|
|
17
17
|
|
|
18
18
|
/** True when `value` is valid `todo/v1` entry data. */
|
|
19
|
-
export function isSnapshotV1(value: unknown): value is { v: 1; tasks: Task[]; nextId: number } {
|
|
19
|
+
export function isSnapshotV1(value: unknown): value is { v: 1; sessionId?: string; tasks: Task[]; nextId: number } {
|
|
20
20
|
return validator.Check(value);
|
|
21
21
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
1
2
|
import {
|
|
2
3
|
type Evidence,
|
|
3
4
|
type EvidenceInput,
|
|
@@ -107,6 +108,9 @@ function validateEvidenceInput(value: unknown, now: string): string | undefined
|
|
|
107
108
|
const e = value as Record<string, unknown>;
|
|
108
109
|
if (typeof e.command !== "string" || !e.command.trim()) return "command must be a non-empty string";
|
|
109
110
|
if (e.result !== "pass" && e.result !== "fail") return 'result must be "pass" or "fail"';
|
|
111
|
+
if (e.ref !== undefined && (typeof e.ref !== "string" || !e.ref || Array.from(e.ref).length > 64 || /\s/u.test(e.ref))) {
|
|
112
|
+
return "ref must be a non-empty string of up to 64 characters without whitespace";
|
|
113
|
+
}
|
|
110
114
|
if (e.sha !== undefined && typeof e.sha !== "string") return "sha must be a string";
|
|
111
115
|
if (e.at !== undefined && (typeof e.at !== "string" || Number.isNaN(Date.parse(e.at)))) {
|
|
112
116
|
return "at must be an ISO 8601 time";
|
|
@@ -121,6 +125,7 @@ function validateEvidenceInput(value: unknown, now: string): string | undefined
|
|
|
121
125
|
function toEvidence(input: EvidenceInput, now: string): Evidence {
|
|
122
126
|
return {
|
|
123
127
|
command: input.command,
|
|
128
|
+
...(input.ref !== undefined ? { ref: input.ref } : {}),
|
|
124
129
|
...(input.sha !== undefined ? { sha: input.sha } : {}),
|
|
125
130
|
result: input.result,
|
|
126
131
|
at: input.at === undefined ? now : new Date(input.at).toISOString(),
|
|
@@ -135,6 +140,7 @@ function asCreateItem(value: unknown): TaskMutationParams | undefined {
|
|
|
135
140
|
function buildCreatedTask(id: number, spec: TaskMutationParams, subject: string, now: string): Task {
|
|
136
141
|
const newTask: Task = {
|
|
137
142
|
id,
|
|
143
|
+
uid: `t_${Array.from(randomBytes(10), byte => "abcdefghijklmnopqrstuvwxyz234567"[byte & 31]).join("")}`,
|
|
138
144
|
subject,
|
|
139
145
|
status: "pending",
|
|
140
146
|
};
|
package/src/todo.ts
CHANGED
|
@@ -152,7 +152,7 @@ export function registerTodoTool(pi: ExtensionAPI, namespaces: NamespaceRegistry
|
|
|
152
152
|
// replay. Pi's own codemode persists its `store()` the same way.
|
|
153
153
|
// The entry is written before the live state commits: if appending throws, the call
|
|
154
154
|
// fails with the list unchanged, so live state never runs ahead of the branch.
|
|
155
|
-
pi.appendEntry(SNAPSHOT_ENTRY_TYPE, toSnapshot(result.state));
|
|
155
|
+
pi.appendEntry(SNAPSHOT_ENTRY_TYPE, toSnapshot(result.state, sessionId));
|
|
156
156
|
commitState(sessionId, result.state);
|
|
157
157
|
pi.events.emit(CHANGED_EVENT, toChangedEvent(sessionId, result.state));
|
|
158
158
|
return buildToolResult(action, input, result.state, result.op);
|
package/src/tool/types.ts
CHANGED
|
@@ -39,6 +39,7 @@ export type EvidenceResult = "pass" | "fail";
|
|
|
39
39
|
|
|
40
40
|
/** One recorded run of a check: what ran, on which revision, how it ended, and when. */
|
|
41
41
|
export interface Evidence {
|
|
42
|
+
ref?: string;
|
|
42
43
|
command: string;
|
|
43
44
|
sha?: string;
|
|
44
45
|
result: EvidenceResult;
|
|
@@ -48,6 +49,7 @@ export interface Evidence {
|
|
|
48
49
|
|
|
49
50
|
/** Evidence as a caller supplies it: `at` defaults to the time of the call. */
|
|
50
51
|
export interface EvidenceInput {
|
|
52
|
+
ref?: string;
|
|
51
53
|
command: string;
|
|
52
54
|
sha?: string;
|
|
53
55
|
result: EvidenceResult;
|
|
@@ -59,6 +61,8 @@ export const POINTS_MIN = 0;
|
|
|
59
61
|
export const POINTS_MAX = 10;
|
|
60
62
|
|
|
61
63
|
export interface Task {
|
|
64
|
+
uid?: string;
|
|
65
|
+
origin?: { session: string };
|
|
62
66
|
id: number;
|
|
63
67
|
subject: string;
|
|
64
68
|
description?: string;
|
|
@@ -139,6 +143,7 @@ const PointsSchema = (description: string) =>
|
|
|
139
143
|
const AnchorSchema = (description: string) => Type.String({ minLength: 1, description });
|
|
140
144
|
|
|
141
145
|
const EvidenceInputSchema = Type.Object({
|
|
146
|
+
ref: Type.Optional(Type.String({ minLength: 1, maxLength: 64, pattern: "^\\S*$", description: "Run or child:<id>" })),
|
|
142
147
|
command: Type.String({ minLength: 1, description: "The check that ran, e.g. 'npm test -- replay'" }),
|
|
143
148
|
sha: Type.Optional(Type.String({ description: "Revision the check ran on, e.g. a git commit SHA" })),
|
|
144
149
|
result: StringEnum(["pass", "fail"] as const, { description: "How the run ended" }),
|