Skip to content

P1: memory_undo force rollback lets one repository remove another repository's private rows #153

Closed

Description

@WhollyMindless

Reproduced on current main 91f7a56 (v0.14.13), Windows/Bun 1.3.10, through the registered memory_undo tool.

The model-facing rollback path ignores its execution context and has no repository authorization. It restores a shared whole-day file, so force:true supplied by a session in A can remove private B memories that the same session cannot recall, forget, or merge.

Deterministic sequence:

  1. Seed an A row and snapshot its day file.
  2. Append a private B row to that same day file.
  3. A's memory_recall cannot see B's marker (control).
  4. A's memory_undo({list:true}) supplies the snapshot name.
  5. A calls memory_undo({snapshot,force:true}): success is reported and B's row disappears from the live store.

Observed: Restored ... from force snapshot; foreignSurvives=false. The safety snapshot exists, so this is unauthorized live-store rollback, not irrecoverable erasure. The default non-force mode correctly refuses this particular divergent snapshot; choosing force bypasses the concurrency check but should not grant authority over another repository.

Cause: tool entry has execute(args), calls restoreSnapshot without a Reader, and that helper replaces the entire shared file. Path containment protects the filesystem location, not ownership of its rows.

Suggested fix / acceptance: thread the caller's immutable Reader into model-facing rollback; preserve foreign live rows and do not revive/replace foreign snapshot rows in any mode. A simpler fail-closed alternative is to refuse mixed-repository restores from plugin sessions and direct the user to the explicit admin viewer. Keep the viewer/admin full-store rollback contract. Include a mixed A/B day-file test for force, merge, and default modes, plus same-repository controls.

Related: #131's rule that a known row id is not authority. A known snapshot name and model-selected force flag likewise are not admin authorization.

Runnable regression test (expected red on v0.14.13)

Save as review-undo-ownership.test.ts at the repository root, then run bun test ./review-undo-ownership.test.ts. Uses the repository's existing inert host harness and temporary stores; no live memory or model calls.

import { afterEach, beforeEach, expect, spyOn, test } from "bun:test";
import { mkdirSync, mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { basename, dirname, join, resolve } from "node:path";
import { loadPlugin, type LoadedPlugin } from "./test/harness.ts";
import { appendMemory, loadAll, snapshotFiles } from "./src/memory.ts";
import { rememberSessionDir, resetSessionScopes, resolveRepo, setRepoScope } from "./src/repokey.ts";
import { resetState } from "./src/state.ts";
import * as evidence from "./src/evidence.ts";

let root: string, a: string, b: string;
let plugins: LoadedPlugin[];
beforeEach(() => {
  root = mkdtempSync(join(tmpdir(), "osi-review-v01413-"));
  a = join(root, "repo-a"); b = join(root, "repo-b");
  mkdirSync(a); mkdirSync(b);
  process.env.OPENCODE_MEMORY_DIR = join(root, "memory");
  process.env.OPENCODE_COMMANDS_DIR = join(root, "commands");
  resetState(); resetSessionScopes(); setRepoScope(null); plugins = [];
});
afterEach(async () => {
  for (const p of plugins) await p.cleanup();
  resetState(); resetSessionScopes(); setRepoScope(null);
  delete process.env.OPENCODE_MEMORY_DIR; delete process.env.OPENCODE_COMMANDS_DIR;
  if (dirname(resolve(root)) !== resolve(tmpdir()) || !basename(root).startsWith("osi-review-v01413-")) {
    throw new Error("Unexpected fixture cleanup path");
  }
  rmSync(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 20 });
});
async function plugin(dir: string | null) {
  const p = await loadPlugin(dir); plugins.push(p); return p;
}
async function seed(dir: string, content: string) {
  const scope = resolveRepo(dir);
  return appendMemory({ type: "context", scope: "planning", content, status: "open", repo: scope.key, repoPath: scope.path });
}
const ctx = (sessionID: string) => ({ sessionID, id: "review-proof" });
const candidate = { type: "context", scope: "planning", status: "open", content: "UNRESOLVED_A_PRIVATE_RELEASE_PLAN" };

test("force undo from A must preserve a private B row that A cannot read", async () => {
  const pa = await plugin(a);
  const own = await seed(a, "A_SNAPSHOT_BASELINE");
  const file = (await loadAll()).find(m => m.ts === own)!.file;
  const [snapshot] = await snapshotFiles([file]);
  const foreign = await seed(b, "B_PRIVATE_ROW_ADDED_AFTER_A_SNAPSHOT");
  const hidden = await pa.tools.memory_recall.execute({ query: "B_PRIVATE_ROW_ADDED_AFTER_A_SNAPSHOT" }, ctx("ses-a"));
  expect(hidden.content).not.toContain("B_PRIVATE_ROW_ADDED_AFTER_A_SNAPSHOT");
  const listed = await pa.tools.memory_undo.execute({ list: true }, ctx("ses-a"));
  expect(listed.content).toContain(basename(snapshot!));
  const result = await pa.tools.memory_undo.execute({ snapshot: basename(snapshot!), force: true }, ctx("ses-a"));
  const foreignSurvives = (await loadAll()).some(m => m.ts === foreign);
  console.log("foreign-undo", JSON.stringify({result: result.content, foreignSurvives}));
  expect(foreignSurvives).toBe(true);
});

test("control: default undo refuses a rollback that would erase a later row", async () => {
  const pa = await plugin(a);
  const own = await seed(a, "A_SNAPSHOT_BASELINE");
  const [snapshot] = await snapshotFiles([(await loadAll()).find(m => m.ts === own)!.file]);
  const foreign = await seed(b, "B_LATER_PRIVATE_ROW");
  const result = await pa.tools.memory_undo.execute({ snapshot: basename(snapshot!) }, ctx("ses-a"));
  expect(result.content).toContain("Undo refused");
  expect((await loadAll()).some(m => m.ts === foreign)).toBe(true);
});

Activity

  1. WhollyMindless commented on Oct 28, 2026

    @WhollyMindless
    ContributorAuthor

    Taking this one — same branch as #143 (fix/security-143-153, off 91f7a56 / 0.14.13): both issues are the identical missing-threading pattern, execute(args) entries that never resolve readerFromSid, so the fix rides together as one "finish entry-point breadth" review.

    Plan per the acceptance criteria:

    • Thread the caller's Reader into the model-facing rollback path; row-level rule in every mode: foreign live rows survive, foreign snapshot rows are never revived or replaced. Reader-visible rows keep today's per-mode semantics (check/merge/force) unchanged.
    • The viewer/admin full-store rollback contract is preserved — breadth stays a property of the entry point.
    • Row-level over the fail-closed refusal because day files are shared across repos in any multi-repo store — a refusal-on-mixed would make session undo effectively dead there, while the row-level rule keeps undo working for your own rows.
    • The runnable probe in this issue body is verified red on 0.14.13 (controls green, including the default-mode refuse control) and will be adopted into test/ as the regression.
  2. WhollyMindless commented on Oct 28, 2026

    @WhollyMindless
    ContributorAuthor

    Closing as fixed by #156, merged in 1bd2123 and included in v0.14.15. Independent verification of final head 834ccba confirmed foreign live rows are preserved and foreign snapshot rows are not resurrected by session rollback; check, merge, force, unresolved-session, and admin controls passed. Verification: #156 (comment) .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions