Skip to content

Commit 260d84b

Browse files
committed
fix: scope /opencode:cancel default to current Claude session
Without ref, resolveCancelableJob now filters running jobs by sessionId so a cancel in session A cannot kill jobs in session B. Explicit ref still searches all sessions — naming a job counts as intent. Closes #45. Port of openai/codex-plugin-cc#84.
1 parent 6665b2d commit 260d84b

3 files changed

Lines changed: 68 additions & 7 deletions

File tree

‎plugins/opencode/scripts/lib/job-control.mjs‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,30 @@ export function resolveResultJob(jobs, ref) {
131131

132132
/**
133133
* Resolve a job that can be canceled (running).
134+
*
135+
* When opts.sessionId is set, the default target (no ref) is restricted to
136+
* jobs from that session so `/opencode:cancel` doesn't reach across Claude
137+
* sessions and kill unrelated work. An explicit ref still searches all
138+
* running jobs — if the user names a job, they asked for it by name.
139+
*
134140
* @param {object[]} jobs
135141
* @param {string} [ref]
136-
* @returns {{ job: object|null, ambiguous: boolean }}
142+
* @param {{ sessionId?: string }} [opts]
143+
* @returns {{ job: object|null, ambiguous: boolean, sessionScoped?: boolean }}
137144
*/
138-
export function resolveCancelableJob(jobs, ref) {
145+
export function resolveCancelableJob(jobs, ref, opts = {}) {
139146
const running = jobs.filter((j) => j.status === "running");
140-
if (!ref) {
141-
return { job: running[0] ?? null, ambiguous: running.length > 1 };
147+
if (ref) {
148+
return matchJobReference(running, ref);
142149
}
143-
return matchJobReference(running, ref);
150+
const scoped = opts.sessionId
151+
? running.filter((j) => j.sessionId === opts.sessionId)
152+
: running;
153+
return {
154+
job: scoped[0] ?? null,
155+
ambiguous: scoped.length > 1,
156+
sessionScoped: Boolean(opts.sessionId),
157+
};
144158
}
145159

146160
/**

‎plugins/opencode/scripts/opencode-companion.mjs‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,16 +549,25 @@ async function handleCancel(argv) {
549549

550550
const workspace = await resolveWorkspace();
551551
const state = loadState(workspace);
552+
const sessionId = getClaudeSessionId();
552553

553-
const { job, ambiguous } = resolveCancelableJob(state.jobs ?? [], ref);
554+
const { job, ambiguous, sessionScoped } = resolveCancelableJob(
555+
state.jobs ?? [],
556+
ref,
557+
{ sessionId }
558+
);
554559

555560
if (ambiguous) {
556561
console.error("Multiple running jobs. Please specify a job ID prefix.");
557562
process.exit(1);
558563
}
559564

560565
if (!job) {
561-
console.log("No active job to cancel.");
566+
console.log(
567+
sessionScoped
568+
? "No active OpenCode jobs to cancel for this session."
569+
: "No active job to cancel."
570+
);
562571
return;
563572
}
564573

‎tests/job-control.test.mjs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,44 @@ describe("job-control", () => {
6767
assert.equal(job, null);
6868
});
6969

70+
it("resolveCancelableJob default is scoped to sessionId when provided", () => {
71+
const multiSession = [
72+
{ id: "task-mine", status: "running", type: "task", sessionId: "S1", updatedAt: "2026-01-01T02:00:00Z", createdAt: "2026-01-01T01:30:00Z" },
73+
{ id: "task-other", status: "running", type: "task", sessionId: "S2", updatedAt: "2026-01-01T02:05:00Z", createdAt: "2026-01-01T01:35:00Z" },
74+
];
75+
const { job, sessionScoped } = resolveCancelableJob(multiSession, undefined, { sessionId: "S1" });
76+
assert.equal(job.id, "task-mine");
77+
assert.equal(sessionScoped, true);
78+
});
79+
80+
it("resolveCancelableJob default returns null when session has no running jobs", () => {
81+
const multiSession = [
82+
{ id: "task-other", status: "running", type: "task", sessionId: "S2", updatedAt: "2026-01-01T02:05:00Z", createdAt: "2026-01-01T01:35:00Z" },
83+
];
84+
const { job, sessionScoped } = resolveCancelableJob(multiSession, undefined, { sessionId: "S1" });
85+
assert.equal(job, null);
86+
assert.equal(sessionScoped, true);
87+
});
88+
89+
it("resolveCancelableJob explicit ref searches across sessions", () => {
90+
const multiSession = [
91+
{ id: "task-mine", status: "running", type: "task", sessionId: "S1", updatedAt: "2026-01-01T02:00:00Z", createdAt: "2026-01-01T01:30:00Z" },
92+
{ id: "task-other", status: "running", type: "task", sessionId: "S2", updatedAt: "2026-01-01T02:05:00Z", createdAt: "2026-01-01T01:35:00Z" },
93+
];
94+
const { job } = resolveCancelableJob(multiSession, "task-other", { sessionId: "S1" });
95+
assert.equal(job.id, "task-other");
96+
});
97+
98+
it("resolveCancelableJob default is ambiguous when session has multiple running jobs", () => {
99+
const multiSession = [
100+
{ id: "task-a", status: "running", type: "task", sessionId: "S1", updatedAt: "2026-01-01T02:00:00Z", createdAt: "2026-01-01T01:30:00Z" },
101+
{ id: "task-b", status: "running", type: "task", sessionId: "S1", updatedAt: "2026-01-01T02:05:00Z", createdAt: "2026-01-01T01:35:00Z" },
102+
];
103+
const { job, ambiguous } = resolveCancelableJob(multiSession, undefined, { sessionId: "S1" });
104+
assert.equal(job.id, "task-a");
105+
assert.equal(ambiguous, true);
106+
});
107+
70108
it("buildStatusSnapshot separates running and finished", () => {
71109
const snapshot = buildStatusSnapshot(jobs, "/tmp/test");
72110
assert.equal(snapshot.running.length, 1);

0 commit comments

Comments
 (0)