Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

fix(hooks): write journal rows in the order the decisions were made

Jack Rudenko235affe92a00
diff --git a/packages/cli/src/hooks/journal.test.ts b/packages/cli/src/hooks/journal.test.ts
index 5a83d591..f5ad9608 100644
--- a/packages/cli/src/hooks/journal.test.ts
+++ b/packages/cli/src/hooks/journal.test.ts
@@ -119,6 +119,27 @@ describe("recordDecision", () => {
});
});
+ it("keeps unawaited same-tick appends in call order, so the last line implies the earlier ones", async () => {
+ // The request path fires recordDecision without awaiting it. Unchained, the appends raced
+ // on stat/mkdir and landed shuffled; repeat on fresh journals to make a reorder near-certain.
+ for (let attempt = 0; attempt < 50; attempt++) {
+ await inTempDir(async (dir) => {
+ const path = join(dir, ".claudish", "behavior-journal.jsonl");
+ const actions = ["removeText", "replaceText", "appendText", "prependText"];
+ const pending = actions.map((action) =>
+ recordDecision(journalEntry({ surface: "response", decision: "matched", action }), path)
+ );
+ await pending.at(-1);
+ const landed = readFileSync(path, "utf8")
+ .trimEnd()
+ .split("\n")
+ .map((line) => JSON.parse(line).action);
+ expect(landed).toEqual(actions);
+ await Promise.all(pending);
+ });
+ }
+ });
+
it("resolves when the journal path is unwritable", async () => {
await inTempDir(async (dir) => {
const parentFile = join(dir, "not-a-directory");
diff --git a/packages/cli/src/hooks/journal.ts b/packages/cli/src/hooks/journal.ts
index 4334a47f..8a358321 100644
--- a/packages/cli/src/hooks/journal.ts
+++ b/packages/cli/src/hooks/journal.ts
@@ -237,9 +237,33 @@ async function prune(path: string): Promise<void> {
}
/**
- * Append one decision. Never throws, never awaited by the request path.
+ * The last pending append per journal path in this process.
+ *
+ * The request path fires `recordDecision` without awaiting it, so one hook step's actions start
+ * several appends in the same tick. Each append first awaits a `stat` (and on a fresh journal a
+ * `mkdir`), and those settle in no particular order: unchained, the lines reached the file in a
+ * shuffled order, and the last decision could be on disk while an earlier one was still pending.
+ * Chaining keeps the file in decision order, so a reader that sees a line also sees every line
+ * this process decided before it. Cross-process appends stay unordered, as before.
+ */
+const pendingAppends = new Map<string, Promise<void>>();
+
+/**
+ * Append one decision. Never throws, never awaited by the request path. Appends from one process
+ * reach the file in call order.
*/
-export async function recordDecision(entry: JournalEntry, path = journalPath()): Promise<void> {
+export function recordDecision(entry: JournalEntry, path = journalPath()): Promise<void> {
+ const previous = pendingAppends.get(path) ?? Promise.resolve();
+ const next = previous.then(() => appendDecision(entry, path));
+ pendingAppends.set(path, next);
+ // Drop the entry once the chain is idle, so the map holds at most one promise per busy path.
+ void next.then(() => {
+ if (pendingAppends.get(path) === next) pendingAppends.delete(path);
+ });
+ return next;
+}
+
+async function appendDecision(entry: JournalEntry, path: string): Promise<void> {
try {
const size = await stat(path).then(
(s) => s.size,