From 550b2c0f8dccd02e078e63d079393e385f98aef8 Mon Sep 17 00:00:00 2001 From: famewolf Date: Wed, 18 Nov 2026 11:41:08 -0500 Subject: [PATCH 1/2] fix(v2): make /auto-resume off/on strictly per-session (remove global wildcard) --- src/v2/index.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/v2/index.ts b/src/v2/index.ts index 90aff15..46376bb 100644 --- a/src/v2/index.ts +++ b/src/v2/index.ts @@ -54,7 +54,7 @@ type AutoResumePlugin = { const define = (plugin: T): T => plugin -import { appendFileSync, readFileSync, existsSync, mkdirSync, statSync, writeFileSync } from "node:fs" +import { appendFileSync, readFileSync, existsSync, mkdirSync, statSync, writeFileSync, unlinkSync } from "node:fs" import { homedir } from "node:os" import { dirname, isAbsolute, join } from "node:path" import { fileURLToPath } from "node:url" @@ -2176,9 +2176,10 @@ export default define({ } function isPaused(sid: string): boolean { const s = readPausedSet() - return s.has("*") || s.has(sid) + return s.has(sid) } function setPaused(sid: string, on: boolean): void { + if (!sid || !sid.startsWith("ses_")) return const s = readPausedSet() if (on) s.add(sid) else s.delete(sid) @@ -7275,7 +7276,7 @@ export default define({ // Pause is process-lifetime only: a restart re-arms auto-resume so a // forgotten pause cannot silently disable it forever. - try { writeFileSync(PAUSED_MARKER_FILE, "", "utf8") } catch {} + try { unlinkSync(PAUSED_MARKER_FILE) } catch {} if (gatedInUse.length > 0) { log("warn", `accepted-but-inert=${gatedInUse.join(",")} — this v2 build recognises these option(s) but does not act on them yet.`) From 0f4e0fdd2e22e9f22df32837b1728b57dda4b3a5 Mon Sep 17 00:00:00 2001 From: famewolf Date: Thu, 19 Nov 2026 11:26:24 -0500 Subject: [PATCH 2/2] docs+test(v2): per-session pause docs and tests - README: /auto-resume off|on is strictly session-scoped; the old --all global wildcard is ignored by v2; pause marker is deleted at startup, re-arming every session after a restart. - src/v2/index.ts: source-of-truth comment matches per-session behavior. - src/v2/index.session-pause.test.ts: bun:test suite covering per-session pause, wildcard/invalid-id rejection, off/on roundtrip, restart reset. --- README.md | 6 +- src/v2/index.session-pause.test.ts | 205 +++++++++++++++++++++++++++++ src/v2/index.ts | 5 +- 3 files changed, 211 insertions(+), 5 deletions(-) create mode 100644 src/v2/index.session-pause.test.ts diff --git a/README.md b/README.md index 94b4120..10fce46 100644 --- a/README.md +++ b/README.md @@ -378,9 +378,9 @@ _Motivated by:_ Long-running quota blocks (or any delicate work) can be paused without deleting the session: -- Slash command: `/auto-resume off` pauses the current session, `/auto-resume on` resumes, `/auto-resume` (or `/auto-resume status`) reports state. While paused, auto-resume will not inject a continue nudge and will not arm the quota ladder. -- CLI helper (works without opening the chat): `auto-resume-toggle off|on|status`, plus `auto-resume-toggle --all off|on` to pause/resume every session. -- Pause is sticky within a run: it stays off until an explicit `on`, so a forgotten pause won't silently re-arm. A restart of opencode/OpenChamber clears all pauses (re-arms auto-resume), so a stale pause can never outlive a session. (The marker file is `~/.config/opencode/auto-resume.paused`.) +- Slash command (v2): `/auto-resume off` pauses **only the current session**, `/auto-resume on` resumes it, and `/auto-resume` (or `/auto-resume status`) reports that session's state. The command is strictly session-scoped — there is no global/all-session form. While paused, auto-resume does not inject a continue nudge or arm the quota ladder for that session; other sessions are unaffected. +- CLI helper (works without opening the chat): `auto-resume-toggle off|on|status`. The old `auto-resume-toggle --all` form writes a global `*` wildcard that the v2 build ignores — use a session ID instead. +- A pause lasts only for the current opencode process. At startup the v2 plugin deletes the marker file (`~/.config/opencode/auto-resume.paused`), re-arming auto-resume for every session. A stale pause can never survive a restart. #### Todo list nudges diff --git a/src/v2/index.session-pause.test.ts b/src/v2/index.session-pause.test.ts new file mode 100644 index 0000000..825c4df --- /dev/null +++ b/src/v2/index.session-pause.test.ts @@ -0,0 +1,205 @@ +import { afterAll, describe, expect, test } from "bun:test" +import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { capturePluginLogs } from "./test-utils" + +// The pause marker path is computed from HOME at module load. Bind this suite to +// a temp HOME *before* importing the plugin so tests never touch the real +// ~/.config/opencode/auto-resume.paused. The query makes this a fresh module +// instance even when other test files imported ./index earlier in the process. +const tempHome = mkdtempSync(join(tmpdir(), "oc-ar-pause-")) +const prevHome = process.env.HOME +process.env.HOME = tempHome +const MARKER = join(tempHome, ".config", "opencode", "auto-resume.paused") +const { default: plugin } = await import(`./index?session-pause=${process.pid}`) + +afterAll(() => { + if (prevHome === undefined) delete process.env.HOME + else process.env.HOME = prevHome + rmSync(tempHome, { recursive: true, force: true }) +}) + +const A = "ses_pause_a" +const B = "ses_pause_b" + +function wait(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)) +} + +function makeEventStream() { + const queue: any[] = [] + const waiters: Array<(ev: any) => void> = [] + let closed = false + const stream = { + push(ev: any) { + if (waiters.length) waiters.shift()!(ev) + else queue.push(ev) + }, + close() { + closed = true + while (waiters.length) waiters.shift()!(null) + }, + subscribe: () => stream, + [Symbol.asyncIterator]() { + return { + next: () => + new Promise((resolve) => { + if (queue.length) return resolve({ value: queue.shift(), done: false }) + if (closed) return resolve({ value: undefined, done: true }) + waiters.push((ev) => + resolve(ev === null ? { value: undefined, done: true } : { value: ev, done: false }), + ) + }), + return: () => { + closed = true + return Promise.resolve({ value: undefined, done: true }) + }, + } + }, + } + return stream +} + +const OPTIONS = { + chunkTimeoutMs: 150, + toolTextCheckDelayMs: 0, + checkIntervalMs: 20, + gracePeriodMs: 0, + warmupMs: 0, + baseBackoffMs: 1, + maxBackoffMs: 2, + maxRetries: 3, + injectIntervalMs: 0, + subagentWaitMs: 40, + debug: true, +} + +const userMessage = (text: string, at: number, id = "msg_u") => ({ + type: "user", + id, + time: { created: at }, + content: [{ type: "text", text }], +}) + +async function setup(scope: string) { + const injected: Array<{ sid?: string; text?: string }> = [] + const commands: Record = {} + const stream = makeEventStream() + const capture = capturePluginLogs() + const history: Record = { + [A]: [userMessage("go", Date.now() - 60_000, "msg_a")], + [B]: [userMessage("go", Date.now() - 60_000, "msg_b")], + } + const ctx: any = { + app: { name: "opencode", version: "2.0.27", channel: "stable" }, + event: stream, + options: { ...OPTIONS, registryScope: scope }, + session: { + context: async (a: any) => history[a?.sessionID ?? A] ?? [], + active: async () => ({ [A]: {}, [B]: {} }), + interrupt: async () => ({}), + synthetic: async (a: any) => (injected.push({ sid: a?.sessionID, text: a?.text }), {}), + prompt: async (a: any) => (injected.push({ sid: a?.sessionID, text: a?.text }), {}), + }, + client: { + session: { + get: async ({ path }: any) => ({ data: { id: path?.id } }), + list: async () => ({ data: [{ id: A }, { id: B }] }), + }, + }, + storage: { get: async () => ({ todos: [] }), set: async () => {}, remove: async () => {} }, + tool: { transform: async (cb: any) => (cb({ add: () => {} }), { dispose() {} }), list: async () => [] }, + command: { + transform: async (cb: any) => { + cb({ add: (c: any) => (commands[c.name] = c) }) + return { dispose() {} } + }, + }, + } + const cleanup = await (plugin as any).setup(ctx) + await wait(60) + return { injected, commands, cleanup, capture, streamRef: stream } +} + +async function teardown(h: any) { + ;(h.cleanup as (() => void) | undefined)?.() + h.capture.stop() +} + +const push = (h: any, type: string, sid: string, data: Record = {}) => + h.streamRef.push({ type, data: { sessionID: sid, ...data } }) + +async function waitFor(what: () => boolean, budgetMs = 4000): Promise { + const deadline = Date.now() + budgetMs + while (Date.now() < deadline) { + if (what()) return true + await wait(10) + } + return what() +} + +async function command(h: any, sid: string, args: string) { + await h.commands["auto-resume"]?.execute({ sessionID: sid, args }) +} + +// The command's own replies go through session.prompt too — exclude them so we +// only count actual stall nudges. +const nudges = (h: any, sid: string) => + h.injected.filter((i: any) => i.sid === sid && !String(i.text ?? "").startsWith("auto-resume ")).length + +describe("per-session /auto-resume pause", () => { + test("off pauses only the target session; another session still recovers", async () => { + const h = await setup("pause-only-target") + await command(h, A, "off") + expect(existsSync(MARKER)).toBe(true) + expect(readFileSync(MARKER, "utf8")).toContain(A) + expect(readFileSync(MARKER, "utf8")).not.toContain("*") + + push(h, "session.execution.started", A) + await wait(700) + expect(nudges(h, A)).toBe(0) + + push(h, "session.execution.started", B) + expect(await waitFor(() => nudges(h, B) >= 1)).toBe(true) + await teardown(h) + }) + + test("a global '*' wildcard and non-session ids never pause a session", async () => { + const h = await setup("pause-no-wildcard") + writeFileSync(MARKER, "*\n", "utf8") + await command(h, "*", "off") + await command(h, "", "off") + await command(h, "all", "off") + expect(readFileSync(MARKER, "utf8")).not.toContain("ses_") + + push(h, "session.execution.started", A) + expect(await waitFor(() => nudges(h, A) >= 1)).toBe(true) + await teardown(h) + }) + + test("on resumes the session without touching the marker of another", async () => { + const h = await setup("pause-resume") + await command(h, A, "off") + await command(h, B, "off") + await command(h, A, "on") + const marker = readFileSync(MARKER, "utf8") + expect(marker).not.toContain(A) + expect(marker).toContain(B) + await teardown(h) + }) + + test("restart deletes the marker and re-arms every session", async () => { + const first = await setup("pause-restart-1") + await command(first, A, "off") + expect(existsSync(MARKER)).toBe(true) + await teardown(first) + + const second = await setup("pause-restart-2") + expect(existsSync(MARKER)).toBe(false) + + push(second, "session.execution.started", A) + expect(await waitFor(() => nudges(second, A) >= 1)).toBe(true) + await teardown(second) + }) +}) diff --git a/src/v2/index.ts b/src/v2/index.ts index 46376bb..a9bc46a 100644 --- a/src/v2/index.ts +++ b/src/v2/index.ts @@ -2159,8 +2159,9 @@ export default define({ } // ---- Pause / resume (user-controlled) --------------------------------- - // Source of truth: a marker file listing paused session ids (one per line) - // or "*" for all. Written by the CLI helper and the /auto-resume command. + // Source of truth: a marker file listing paused session ids (one per line). + // Only real `ses_*` ids count — the old global "*" wildcard is ignored, so + // pause is strictly per-session. Written by the CLI helper and the /auto-resume command. function readPausedSet(): Set { try { const raw = readFileSync(PAUSED_MARKER_FILE, "utf8")