-
Notifications
You must be signed in to change notification settings - Fork 141
persist human review notes to a JSON sidecar via --store-notes #473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
muratovv
wants to merge
2
commits into
modem-dev:main
Choose a base branch
from
muratovv:feat/persistent-c-notes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "hunkdiff": minor | ||
| --- | ||
|
|
||
| Add an opt-in `--store-notes <path>` flag that persists human review notes to a JSON sidecar (cwd-relative). Notes survive closing the TUI and can be read back off disk by an agent. Omitting the flag keeps notes in-memory only, preserving current behavior. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { afterEach, describe, expect, test } from "bun:test"; | ||
| import { chmodSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import type { UserReviewNote } from "../ui/hooks/useReviewController"; | ||
| import { readUserNotes, userNotesWriteWarning, writeUserNotes } from "./userNotesStore"; | ||
|
|
||
| const tempDirs: string[] = []; | ||
|
|
||
| afterEach(() => { | ||
| while (tempDirs.length > 0) { | ||
| const dir = tempDirs.pop(); | ||
| if (dir) { | ||
| rmSync(dir, { recursive: true, force: true }); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| function createTempDir(prefix: string) { | ||
| const dir = mkdtempSync(join(tmpdir(), prefix)); | ||
| tempDirs.push(dir); | ||
| return dir; | ||
| } | ||
|
|
||
| function note(id: string, summary: string): UserReviewNote { | ||
| return { | ||
| id, | ||
| source: "user", | ||
| filePath: "src/foo.ts", | ||
| hunkIndex: 0, | ||
| side: "new", | ||
| line: 1, | ||
| summary, | ||
| author: "user", | ||
| editable: true, | ||
| } as UserReviewNote; | ||
| } | ||
|
|
||
| describe("userNotesStore", () => { | ||
| test("writeUserNotes round-trips through readUserNotes, creating missing dirs", () => { | ||
| const root = createTempDir("hunk-notes-"); | ||
| const path = join(root, ".hunk", "notes.json"); | ||
| const map = { "repo:0:src/foo.ts": [note("user:1", "first"), note("user:2", "second")] }; | ||
|
|
||
| writeUserNotes(path, map); | ||
|
|
||
| expect(readUserNotes(path)).toEqual(map); | ||
| }); | ||
|
|
||
| test("writeUserNotes replaces existing notes atomically without leaving temp litter", () => { | ||
| const root = createTempDir("hunk-notes-"); | ||
| const path = join(root, "notes.json"); | ||
|
|
||
| writeUserNotes(path, { "repo:0:src/foo.ts": [note("user:1", "first")] }); | ||
| writeUserNotes(path, { "repo:0:src/bar.ts": [note("user:2", "second")] }); | ||
|
|
||
| // Full replacement, and the temp sibling used for the atomic rename is gone. | ||
| expect(readUserNotes(path)).toEqual({ "repo:0:src/bar.ts": [note("user:2", "second")] }); | ||
| expect(readdirSync(root).filter((name) => name.endsWith(".tmp"))).toEqual([]); | ||
| }); | ||
|
|
||
| test("readUserNotes tolerates a missing or malformed sidecar", () => { | ||
| const root = createTempDir("hunk-notes-"); | ||
| expect(readUserNotes(join(root, "absent.json"))).toEqual({}); | ||
|
|
||
| const malformed = join(root, "bad.json"); | ||
| writeFileSync(malformed, "{ not json"); | ||
| expect(readUserNotes(malformed)).toEqual({}); | ||
| }); | ||
|
|
||
| test("readUserNotes drops entries that are not plausible note arrays", () => { | ||
| const root = createTempDir("hunk-notes-"); | ||
| const path = join(root, "notes.json"); | ||
| writeFileSync(path, JSON.stringify({ good: [note("user:1", "ok")], bad: [{ nope: true }] })); | ||
|
|
||
| expect(Object.keys(readUserNotes(path))).toEqual(["good"]); | ||
| }); | ||
|
|
||
| describe("userNotesWriteWarning", () => { | ||
| test("no warning when the sidecar is missing but an ancestor is writable", () => { | ||
| const root = createTempDir("hunk-notes-"); | ||
| expect(userNotesWriteWarning(join(root, ".hunk", "notes.json"))).toBeUndefined(); | ||
| }); | ||
|
|
||
| test("no warning when the sidecar already exists and is writable (prior review)", () => { | ||
| const root = createTempDir("hunk-notes-"); | ||
| const path = join(root, "notes.json"); | ||
| writeUserNotes(path, { "repo:0:src/foo.ts": [note("user:1", "prior")] }); | ||
|
|
||
| expect(userNotesWriteWarning(path)).toBeUndefined(); | ||
| }); | ||
|
|
||
| test("warns when an existing sidecar is read-only", () => { | ||
| const root = createTempDir("hunk-notes-"); | ||
| const path = join(root, "notes.json"); | ||
| writeFileSync(path, "{}"); | ||
| chmodSync(path, 0o444); | ||
|
|
||
| const warning = userNotesWriteWarning(path); | ||
| expect(warning).toContain(path); | ||
| expect(warning).toContain("not be saved"); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /** | ||
| * Disk persistence for human-authored review notes ("c" notes). | ||
| * | ||
| * Notes are mirrored to the JSON sidecar at the caller-supplied `--store-notes` | ||
| * path so they survive closing the TUI and can be read back by an AI agent | ||
| * directly off disk. Every disk operation is best-effort: a read failure yields | ||
| * an empty map and a write failure is swallowed, so persistence never crashes | ||
| * the review UI. Writes go through a temp sibling + atomic rename so an | ||
| * interrupted write (SIGTERM, OOM, power loss) can never truncate an existing | ||
| * sidecar and lose prior notes. Set `HUNK_DEBUG=1` to surface swallowed errors. | ||
| */ | ||
| import { | ||
| accessSync, | ||
| constants, | ||
| existsSync, | ||
| mkdirSync, | ||
| readFileSync, | ||
| renameSync, | ||
| rmSync, | ||
| writeFileSync, | ||
| } from "node:fs"; | ||
| import { dirname } from "node:path"; | ||
| import type { UserReviewNote } from "../ui/hooks/useReviewController"; | ||
|
|
||
| export type UserNotesMap = Record<string, UserReviewNote[]>; | ||
|
|
||
| /** Walk up from a path to the closest ancestor directory that already exists. */ | ||
| function nearestExistingDir(path: string): string { | ||
| let dir = dirname(path); | ||
| while (!existsSync(dir) && dirname(dir) !== dir) { | ||
| dir = dirname(dir); | ||
| } | ||
| return dir; | ||
| } | ||
|
|
||
| /** | ||
| * Best-effort startup heads-up: return a warning when the sidecar at `path` | ||
| * cannot be written, else undefined. Existence is fine — only un-writability | ||
| * warns. An existing sidecar must itself be writable; a missing one needs its | ||
| * nearest existing ancestor writable so `mkdir -p` can create the rest. This is | ||
| * a UX signal only; the write path stays fault-tolerant regardless. | ||
| */ | ||
| export function userNotesWriteWarning(path: string): string | undefined { | ||
| const target = existsSync(path) ? path : nearestExistingDir(path); | ||
| try { | ||
| accessSync(target, constants.W_OK); | ||
| return undefined; | ||
| } catch { | ||
| return `hunk: cannot write review notes to ${path}; notes from this session will not be saved.`; | ||
| } | ||
| } | ||
|
|
||
| /** Emit a swallowed-error diagnostic only when the user opted into debug output. */ | ||
| function debugUserNotesError(action: string, path: string, error: unknown): void { | ||
| if (process.env.HUNK_DEBUG === "1") { | ||
| process.stderr.write(`hunk: failed to ${action} user notes at ${path}: ${String(error)}\n`); | ||
| } | ||
| } | ||
|
|
||
| /** Return whether one parsed value is an array of plausible user notes. */ | ||
| function isUserNoteArray(value: unknown): value is UserReviewNote[] { | ||
| return ( | ||
| Array.isArray(value) && | ||
| value.every( | ||
| (entry) => | ||
| typeof entry === "object" && | ||
| entry !== null && | ||
| typeof (entry as { id?: unknown }).id === "string" && | ||
| typeof (entry as { summary?: unknown }).summary === "string", | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** Read persisted human notes, tolerating a missing or malformed sidecar file. */ | ||
| export function readUserNotes(path: string): UserNotesMap { | ||
| let parsed: unknown; | ||
| try { | ||
| parsed = JSON.parse(readFileSync(path, "utf8")); | ||
| } catch (error) { | ||
| if ((error as NodeJS.ErrnoException)?.code !== "ENOENT") { | ||
| debugUserNotesError("read", path, error); | ||
| } | ||
| return {}; | ||
| } | ||
|
|
||
| if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) { | ||
| return {}; | ||
| } | ||
|
|
||
| const map: UserNotesMap = {}; | ||
| for (const [fileId, notes] of Object.entries(parsed as Record<string, unknown>)) { | ||
| if (isUserNoteArray(notes)) { | ||
| map[fileId] = notes; | ||
| } | ||
| } | ||
| return map; | ||
| } | ||
|
|
||
| /** | ||
| * Persist human notes to the caller-supplied sidecar path, creating parent dirs. | ||
| * | ||
| * The payload is written to a temp sibling and then `renameSync`d into place. | ||
| * `rename(2)` is atomic on POSIX, so a crash mid-write leaves the previous | ||
| * sidecar intact rather than a truncated file that `readUserNotes` would discard | ||
| * — preserving the durability the feature promises. | ||
| */ | ||
| export function writeUserNotes(path: string, map: UserNotesMap): void { | ||
| const tempPath = `${path}.${process.pid}.tmp`; | ||
| try { | ||
| mkdirSync(dirname(path), { recursive: true }); | ||
| writeFileSync(tempPath, JSON.stringify(map, null, 2), { encoding: "utf8" }); | ||
| renameSync(tempPath, path); | ||
| } catch (error) { | ||
| // Best-effort: a write failure must never crash the review UI. Drop any | ||
| // partial temp file so a failed write can't litter the sidecar directory. | ||
| try { | ||
| rmSync(tempPath, { force: true }); | ||
| } catch { | ||
| // ignore cleanup failure | ||
| } | ||
| debugUserNotesError("write", path, error); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
writeFileSynctruncates the file before writing, so a SIGTERM, OOM kill, or power-loss mid-write leaves a partial JSON blob. On the next startup,readUserNotescatches the parse error and returns{}, silently discarding every note the user wrote in previous sessions — exactly the failure mode the feature promises to prevent.The safe pattern is: write to a sibling temp file (e.g.
path + ".tmp"), thenrenameSyncinto place.rename(2)is atomic on POSIX, so a kill between write and rename leaves the old sidecar intact rather than an empty one.Prompt To Fix With AI