pi-todo-ledger 0.12.0 → 0.12.1

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 CHANGED
@@ -98,9 +98,10 @@ Both sessions need a persisted pi journal: in-memory sessions and new sessions t
98
98
  The source confirms the new snapshot's exact journal entry on disk and syncs it before emitting `todo:changed` or publishing the file.
99
99
  The destination does the same before acknowledging receipt; a retry verifies the receipt in the raw journal entry, before replay normalizes any legacy fields.
100
100
  Before the append the ledger records the journal's size and verifies its last complete entry.
101
- If the append throws after writing bytes, the ledger verifies that the original journal and record are still present, truncates the extra bytes, and syncs the file before pi writes the error result.
102
- The transfer still fails, but it can be retried; when no bytes were written no truncation is needed.
103
- If repair cannot be completed, the error result reports the cause and the session refuses further ledger writes until the journal is repaired and pi restarted.
101
+ If the append throws, the transfer fails; you can retry if pi rolled back its in-memory entry and the journal's identity, size and last record are unchanged.
102
+ If the journal grew, pi cannot tell the ledger which bytes it wrote, so the ledger leaves the file intact, even when the extra bytes look like part of the expected record.
103
+ If the journal grew or the ledger cannot verify the original tail, the result reports both the append error and the verification failure.
104
+ The session then refuses further ledger writes; stop pi, repair the journal and restart before retrying.
104
105
  A sync error after append is not a rollback: the tool reports failure and restores its live list from the snapshot it can verify on disk, so a later edit cannot erase the receipt or revive a moved task.
105
106
  If the journal cannot be verified, that pi process blocks further ledger writes for the session; restore the journal and restart pi before retrying.
106
107
  They keep the old task data and add `moved`; `list` and `get` show the destination and time, but `update` cannot change them.
@@ -27,9 +27,10 @@ Guards must opt in with `moves: true` to judge the whole proposed change, so a g
27
27
  An in-memory session or a new session without a journal cannot send or receive a move.
28
28
  A transfer compares the manager's last entry with the journal tail, confirms the exact committed snapshot bytes and syncs the journal before reporting success; a retry confirms the snapshot on disk too.
29
29
  If pi throws partway through appending a transfer snapshot, it restores its in-memory position but may leave part of a JSON line on disk.
30
- Before returning the error, the ledger truncates the journal to its verified pre-append byte size and syncs it, so pi can append the tool result and the next message on clean record boundaries.
31
- If the repair fails, the result names the failure and the ledger blocks further writes for that session.
32
- A process or machine crash between the partial write and the truncate can still leave an incomplete line: pi journal appends are not crash-atomic, whether the record is a move, a message or another entry.
30
+ The ledger checks the journal without modifying it: if the same file still ends at the same byte with the same record, the transfer can be retried.
31
+ Pi does not report how many bytes it wrote before throwing, so the ledger preserves any extra bytes rather than risk removing another writer's entry.
32
+ If the journal grew or the original tail cannot be verified, the result names both failures and the ledger blocks further writes for that session until the journal is repaired and pi restarted.
33
+ This cannot stop pi itself from appending a tool result after a partial line.
33
34
  Only after committing the source snapshot does the tool publish a private JSON file under pi's agent directory, by atomic rename, and return its absolute `details.movePath`.
34
35
  The file contains the batch identity, source, destination and selected tasks with their original statuses and only internal dependencies; it is not itself a list snapshot.
35
36
  If publication fails after commit, or the acknowledgement is lost, repeat the exact selection and destination on the source: the tombstones materialize the *same* file, without another change event or another move.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-todo-ledger",
3
- "version": "0.12.0",
3
+ "version": "0.12.1",
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",
@@ -58,7 +58,7 @@ export function makeFinishCall(pi: ExtensionAPI, namespaces: NamespaceRegistry,
58
58
  let recovered = before;
59
59
  try {
60
60
  if (appended) recovered = journal.recoverState();
61
- else journal.rollbackAppend(tail);
61
+ else journal.verifyUnchangedAfterFailure(tail);
62
62
  } catch (repairError) {
63
63
  fenced.add(sessionId);
64
64
  const detail = repairError instanceof Error ? repairError.message : String(repairError);
@@ -1,4 +1,4 @@
1
- import { closeSync, fstatSync, fsyncSync, ftruncateSync, openSync, readSync, type Stats } from "node:fs";
1
+ import { closeSync, fstatSync, fsyncSync, openSync, readSync, type Stats } from "node:fs";
2
2
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
3
3
  import { stateOf } from "../state/replay.ts";
4
4
  import { isSnapshotV1 } from "./schema.ts";
@@ -72,12 +72,12 @@ export class TransferJournal {
72
72
  });
73
73
  }
74
74
 
75
- /** Pi removed the failed entry from memory. Undo any bytes it appended before pi records this tool's error result. */
76
- rollbackAppend(tail: JournalTail): void {
75
+ /** Pi rolls back its in-memory entry but does not report which bytes it wrote. Never truncate on its behalf. */
76
+ verifyUnchangedAfterFailure(tail: JournalTail): void {
77
77
  this.writers.claim(this.manager.getSessionId(), this.path);
78
78
  const last = this.manager.getEntries().at(-1) ?? this.manager.getHeader();
79
79
  if (!tail.line.equals(Buffer.from(JSON.stringify(last) + "\n"))) throw new Error("pi did not roll back its journal position");
80
- const growth = (fd: number, size: number, stat: Stats): boolean => {
80
+ this.inspect((fd, size, stat) => {
81
81
  const start = tail.size - tail.line.length;
82
82
  if (
83
83
  stat.dev !== tail.dev ||
@@ -86,15 +86,10 @@ export class TransferJournal {
86
86
  !matchesAt(fd, start, tail.line) ||
87
87
  (start > 0 && !matchesAt(fd, start - 1, Buffer.from("\n")))
88
88
  ) {
89
- throw new Error("journal identity or original record changed; refusing to truncate");
89
+ throw new Error("journal identity or original record changed");
90
90
  }
91
- return size > tail.size;
92
- };
93
- if (!this.inspect(growth)) return; // An unchanged journal needs no write access.
94
- this.inspect((fd, size, stat) => {
95
- if (growth(fd, size, stat)) ftruncateSync(fd, tail.size);
96
- fsyncSync(fd);
97
- }, "r+");
91
+ if (size > tail.size) throw new Error(`journal grew by ${size - tail.size} bytes; cannot identify the writer, leaving journal intact`);
92
+ });
98
93
  }
99
94
 
100
95
  /** A fresh write must match the proposal exactly, including the entry at the manager's leaf. */
@@ -133,13 +128,12 @@ export class TransferJournal {
133
128
  return entry.data;
134
129
  }
135
130
 
136
- private inspect<T>(check: (fd: number, size: number, stat: Stats) => T, access: "r" | "r+" = "r"): T {
131
+ private inspect<T>(check: (fd: number, size: number, stat: Stats) => T): T {
137
132
  if (this.manager.getSessionFile() !== this.path) throw new Error("session journal changed during transfer");
138
133
  let fd: number;
139
134
  try {
140
- fd = openSync(this.path, access);
135
+ fd = openSync(this.path, "r");
141
136
  } catch (error) {
142
- if (access === "r+") throw error;
143
137
  throw new Error(REQUIRED, { cause: error });
144
138
  }
145
139
  try {