diff --git a/docs/HOT-SYNC.md b/docs/HOT-SYNC.md index 47d6e49..8f8ba81 100644 --- a/docs/HOT-SYNC.md +++ b/docs/HOT-SYNC.md @@ -34,7 +34,9 @@ captured in (`evidence.workspace`: HEAD, branch, uncommitted changes, and up to commit it ran at. The field is optional and travels inside the opaque evidence, so older clients ignore it, and it counts toward the same 30,000-character budget. When the checkpoint is shown in a tree that differs, the prompt gets one -warning line and marks on the affected excerpts. An excerpt of a file that has +warning line and marks on the affected excerpts. If HEAD has only moved forward on +the same branch (the saved commit is an ancestor), the line just says so, without a +warning. An excerpt of a file that has changed on disk since (usually edited by the agent itself) is dropped, and the file is named once instead. Nothing is restored or rewritten. Handoff capsules do not carry it yet. diff --git a/src/runtime/workspace-state.ts b/src/runtime/workspace-state.ts index 58b564f..89cdccc 100644 --- a/src/runtime/workspace-state.ts +++ b/src/runtime/workspace-state.ts @@ -143,6 +143,18 @@ export const sourceFilePath = (source: string): string | null => { return tokens.length > 1 && !last.startsWith("-") && /[./]/.test(last) && safeRelative(last) ? last : null; }; +/** Whether `ancestor` is an ancestor of (or equal to) `descendant` in the repository; false when git cannot tell. */ +const isAncestor = (workspaceRoot: string, ancestor: string, descendant: string): boolean => { + if (!SHA.test(ancestor) || !SHA.test(descendant)) return false; + try { + execFileSync("git", ["-C", workspaceRoot, "merge-base", "--is-ancestor", ancestor, descendant], { stdio: "ignore", timeout: GIT_TIMEOUT_MS }); + return true; + } catch { + return false; + } +}; +const forwardOnly = (notes: string[]): boolean => notes.length === 1 && notes[0]!.startsWith("moved forward from "); + export const describeWorkspaceDrift = (then: WorkspaceGitState | undefined, current: WorkspaceGitState | null, workspaceRoot: string | null): string => { if (!then) return ""; const notes: string[] = []; @@ -150,10 +162,15 @@ export const describeWorkspaceDrift = (then: WorkspaceGitState | undefined, curr if (then.head) notes.push(`written at ${where(then)}; no git checkout is readable here`); } else { const moved = Boolean(then.head && current.head && then.head !== current.head); - if (moved) notes.push(`written at ${where(then)}; now ${where(current)}`); + // HEAD moved forward on the same branch: usually the work was committed since (often the checkpoint's own + // uncommitted changes). That is progress, not drift; say so in one neutral clause, without a reread warning. + const forward = moved && workspaceRoot !== null && (!then.branch || then.branch === current.branch) && isAncestor(workspaceRoot, then.head!, current.head!); + // Ancestry does not show the saved uncommitted changes were committed (they may have been discarded), so say that softly. + if (forward) notes.push(`moved forward from ${short(then.head)} to ${where(current)} since this was written${then.dirty ? " (it was written with uncommitted changes, which may or may not be in these commits)" : ""}`); + else if (moved) notes.push(`written at ${where(then)}; now ${where(current)}`); else if (then.branch && current.branch && then.branch !== current.branch) notes.push(`written on ${then.branch}; now on ${current.branch}`); // Same commit and still uncommitted changes here is the same working tree as far as git can tell. - if (then.dirty && (moved || !current.dirty)) notes.push("written with uncommitted changes that are not in this tree, so results observed then may not hold here"); + if (then.dirty && !forward && (moved || !current.dirty)) notes.push("written with uncommitted changes that are not in this tree, so results observed then may not hold here"); if (workspaceRoot) { const missing: string[] = [], changed: string[] = []; for (const file of then.untracked) { @@ -169,6 +186,7 @@ export const describeWorkspaceDrift = (then: WorkspaceGitState | undefined, curr } } if (!notes.length) return ""; - const line = `Workspace drift (warning only; reread before relying on saved results): ${notes.join("; ")}.`; + // A forward move alone is not a warning. + const line = forwardOnly(notes) ? `Workspace ${notes[0]}.` : `Workspace drift (warning only; reread before relying on saved results): ${notes.join("; ")}.`; return line.length <= MAX_DRIFT_CHARS ? line : `${line.slice(0, MAX_DRIFT_CHARS - 3)}...`; }; diff --git a/tests/runtime/workspace-state.test.ts b/tests/runtime/workspace-state.test.ts index e1ace2f..ff5a7a2 100644 --- a/tests/runtime/workspace-state.test.ts +++ b/tests/runtime/workspace-state.test.ts @@ -69,9 +69,28 @@ describe("checkpoint git state", () => { git(root, "commit", "-q", "-am", "change"); const line = describeWorkspaceDrift(then, readWorkspaceGitState(root), root); expect(line.split("\n")).toHaveLength(1); - expect(line).toContain(`written at ${then.head!.slice(0, 10)} on main; now ${git(root, "rev-parse", "HEAD").slice(0, 10)}`); + // A commit on top of the saved one is progress: one neutral clause, no uncommitted-changes warning. + expect(line).toContain(`moved forward from ${then.head!.slice(0, 10)} to ${git(root, "rev-parse", "HEAD").slice(0, 10)} on main`); expect(line).toContain("untracked tool.py is not present here"); - expect(line).toContain("uncommitted changes that are not in this tree"); + expect(line).not.toContain("uncommitted changes that are not in this tree"); + }); + + it("states a plain forward move without a warning, and still warns when the saved commit is not an ancestor", () => { + const root = repo(); + writeFileSync(path.join(root, "rules.md"), "Batches hold 40 items.\n"); + const then = readWorkspaceGitState(root)!; + expect(then.dirty).toBe(true); + git(root, "commit", "-q", "-am", "work"); + const forward = describeWorkspaceDrift(then, readWorkspaceGitState(root), root); + expect(forward).toBe(`Workspace moved forward from ${then.head!.slice(0, 10)} to ${git(root, "rev-parse", "HEAD").slice(0, 10)} on main since this was written (it was written with uncommitted changes, which may or may not be in these commits).`); + expect(forward).not.toContain("reread"); + // History rewritten so the saved commit is not an ancestor of HEAD on the same branch: the full warning returns. + git(root, "checkout", "-q", "--orphan", "rewritten"); + git(root, "commit", "-q", "-m", "unrelated"); + git(root, "branch", "-q", "-M", "main"); + const diverged = describeWorkspaceDrift(then, readWorkspaceGitState(root), root); + expect(diverged).toContain("Workspace drift (warning only"); + expect(diverged).toContain("uncommitted changes that are not in this tree"); }); it("marks command output run at another commit and file excerpts that changed, in rendered checkpoints", () => { @@ -92,7 +111,7 @@ describe("checkpoint git state", () => { writeFileSync(path.join(root, "rules.md"), "Batches hold 43 items.\n"); git(root, "commit", "-q", "-am", "change"); const moved = renderHotCheckpoints(stored, 4200, { root, git: readWorkspaceGitState(root) }); - expect(moved).toContain("Workspace drift"); + expect(moved).toContain("Workspace moved forward from"); // The stale excerpt is dropped and the file named once; no reread instruction. expect(moved).toContain('Changed on disk since observed (old excerpts omitted; the current files are authoritative): "rules.md"'); expect(moved).not.toContain("Batches hold 37 items.");