Repository navigation
fix: detach polled cloud handoffs at session end instead of cancelling them #18
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
hessabi
wants to merge
1
commit into
TheOnlyFusionCube:main
Choose a base branch
from
hessabi:fix/session-end-handoff-cancel
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
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
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,201 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { spawn } from "node:child_process"; | ||
| import fs from "node:fs"; | ||
| import http from "node:http"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
| import test from "node:test"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| import { isJobActive } from "../plugins/devin/scripts/lib/jobs.mjs"; | ||
| import { listJobs, readJobFile, resolveJobFile, upsertJob, writeJobFile } from "../plugins/devin/scripts/lib/state.mjs"; | ||
|
|
||
| const HOOK = fileURLToPath(new URL("../plugins/devin/scripts/session-lifecycle-hook.mjs", import.meta.url)); | ||
| const SESSION = "claude-session-example"; | ||
| const OTHER_SESSION = "claude-session-other-example"; | ||
|
|
||
| // Hook and fake API run in this process's event loop, so the hook is spawned | ||
| // asynchronously: that also lets Node reap the stand-in poller the hook kills. | ||
| function runSessionEnd({ cwd, env, sessionId = SESSION }) { | ||
| return new Promise((resolve, reject) => { | ||
| const child = spawn(process.execPath, [HOOK, "SessionEnd"], { cwd, env: { ...process.env, ...env } }); | ||
| let stderr = ""; | ||
| child.stderr.on("data", (chunk) => (stderr += chunk)); | ||
| child.on("error", reject); | ||
| child.on("close", (status) => resolve({ status, stderr })); | ||
| child.stdin.end(JSON.stringify({ hook_event_name: "SessionEnd", session_id: sessionId, cwd })); | ||
| }); | ||
| } | ||
|
|
||
| function spawnStandInProcess() { | ||
| const child = spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)"], { detached: true, stdio: "ignore" }); | ||
| const exited = new Promise((resolve) => child.on("exit", (code, signal) => resolve({ code, signal }))); | ||
| return { child, exited }; | ||
| } | ||
|
|
||
| async function createHarness() { | ||
| const requests = []; | ||
| const server = http.createServer((req, res) => { | ||
| requests.push(`${req.method} ${req.url}`); | ||
| res.writeHead(500); | ||
| res.end("{}"); | ||
| }); | ||
| await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); | ||
| const dir = fs.mkdtempSync(path.join(os.tmpdir(), "devin-session-end-test-")); | ||
| const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "devin-session-end-data-")); | ||
| const env = { | ||
| CLAUDE_PLUGIN_DATA: dataDir, | ||
| DEVIN_API_KEY: "apk_example", | ||
| DEVIN_API_URL: `http://127.0.0.1:${server.address().port}`, | ||
| DEVIN_COMPANION_ALLOW_LOOPBACK_API: "1" | ||
| }; | ||
| process.env.CLAUDE_PLUGIN_DATA = dataDir; | ||
| const spawned = []; | ||
| const seed = (job) => { | ||
| const record = { workspaceRoot: dir, status: "running", sessionId: SESSION, startedAt: new Date().toISOString(), ...job }; | ||
| writeJobFile(dir, record.id, record); | ||
| upsertJob(dir, record); | ||
| }; | ||
| const standIn = () => { | ||
| const entry = spawnStandInProcess(); | ||
| spawned.push(entry.child); | ||
| return entry; | ||
| }; | ||
| const stored = (id) => listJobs(dir).find((job) => job.id === id); | ||
| return { | ||
| dir, | ||
| env, | ||
| requests, | ||
| seed, | ||
| standIn, | ||
| stored, | ||
| storedFile: (id) => readJobFile(resolveJobFile(dir, id)), | ||
| run: (options = {}) => runSessionEnd({ cwd: dir, env, ...options }), | ||
| async cleanup() { | ||
| for (const child of spawned) { | ||
| try { | ||
| process.kill(-child.pid, "SIGKILL"); | ||
| } catch { | ||
| // already gone | ||
| } | ||
| } | ||
| await new Promise((resolve) => server.close(resolve)); | ||
| fs.rmSync(dir, { recursive: true, force: true }); | ||
| fs.rmSync(dataDir, { recursive: true, force: true }); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| test("SessionEnd stops a --wait handoff poller and detaches the job instead of cancelling it", async () => { | ||
| const h = await createHarness(); | ||
| try { | ||
| const poller = h.standIn(); | ||
| h.seed({ id: "devin-handoff-wait", kind: "handoff", phase: "running-cloud", pid: poller.child.pid, cloudSessionId: "devin-example1" }); | ||
|
|
||
| const result = await h.run(); | ||
| assert.equal(result.status, 0, result.stderr); | ||
|
|
||
| const exit = await poller.exited; | ||
| assert.equal(exit.signal, "SIGTERM"); | ||
|
|
||
| for (const job of [h.stored("devin-handoff-wait"), h.storedFile("devin-handoff-wait")]) { | ||
| assert.equal(job.status, "detached"); | ||
| assert.equal(job.pid, null); | ||
| assert.equal(job.cloudSessionId, "devin-example1"); | ||
| assert.equal(job.completedAt, undefined); | ||
| assert.match(job.phase, /poller stopped at session end/); | ||
| assert.ok(isJobActive(job)); | ||
| } | ||
| assert.deepEqual(h.requests, []); | ||
| } finally { | ||
| await h.cleanup(); | ||
| } | ||
| }); | ||
|
|
||
| test("SessionEnd leaves other sessions and non-polled jobs alone and still cancels local jobs", async () => { | ||
| const h = await createHarness(); | ||
| try { | ||
| const local = h.standIn(); | ||
| const foreign = h.standIn(); | ||
| h.seed({ id: "devin-task-local", kind: "task", phase: "running", pid: local.child.pid }); | ||
| h.seed({ | ||
| id: "devin-handoff-foreign", | ||
| kind: "handoff", | ||
| phase: "running-cloud", | ||
| pid: foreign.child.pid, | ||
| cloudSessionId: "devin-example2", | ||
| sessionId: OTHER_SESSION | ||
| }); | ||
| h.seed({ id: "devin-handoff-detached", kind: "handoff", status: "detached", phase: "detached", pid: null, cloudSessionId: "devin-example3" }); | ||
|
|
||
| const result = await h.run(); | ||
| assert.equal(result.status, 0, result.stderr); | ||
|
|
||
| assert.equal((await local.exited).signal, "SIGTERM"); | ||
| const task = h.stored("devin-task-local"); | ||
| assert.equal(task.status, "cancelled"); | ||
| assert.equal(task.phase, "cancelled"); | ||
| assert.equal(task.pid, null); | ||
| assert.ok(task.completedAt); | ||
|
|
||
| const other = h.stored("devin-handoff-foreign"); | ||
| assert.equal(other.status, "running"); | ||
| assert.equal(other.pid, foreign.child.pid); | ||
| process.kill(foreign.child.pid, 0); | ||
|
|
||
| const detached = h.stored("devin-handoff-detached"); | ||
| assert.equal(detached.status, "detached"); | ||
| assert.equal(detached.phase, "detached"); | ||
| assert.deepEqual(h.requests, []); | ||
| } finally { | ||
| await h.cleanup(); | ||
| } | ||
| }); | ||
|
|
||
| test("SessionEnd still cancels a handoff that has no cloud session yet", async () => { | ||
| const h = await createHarness(); | ||
| try { | ||
| const creating = h.standIn(); | ||
| h.seed({ id: "devin-handoff-creating", kind: "handoff", phase: "creating", pid: creating.child.pid }); | ||
|
|
||
| const result = await h.run(); | ||
| assert.equal(result.status, 0, result.stderr); | ||
| assert.equal((await creating.exited).signal, "SIGTERM"); | ||
| assert.equal(h.stored("devin-handoff-creating").status, "cancelled"); | ||
| assert.deepEqual(h.requests, []); | ||
| } finally { | ||
| await h.cleanup(); | ||
| } | ||
| }); | ||
|
|
||
| test("SessionEnd waits for the poller to exit and keeps a terminal state it wrote while dying", async () => { | ||
| const h = await createHarness(); | ||
| try { | ||
| // Stand-in poller that records "completed" on SIGTERM, then exits: the hook must not | ||
| // overwrite that with a stale snapshot taken before the kill. | ||
| const stateModule = fileURLToPath(new URL("../plugins/devin/scripts/lib/state.mjs", import.meta.url)); | ||
| const script = ` | ||
| import(${JSON.stringify(stateModule)}).then(({ upsertJob }) => { | ||
| process.on("SIGTERM", () => { | ||
| upsertJob(${JSON.stringify(h.dir)}, { id: "devin-handoff-done", status: "completed", phase: "exit", pid: null, completedAt: new Date().toISOString() }); | ||
| process.exit(0); | ||
| }); | ||
| console.log("ready"); | ||
| setInterval(() => {}, 1000); | ||
| });`; | ||
| const child = spawn(process.execPath, ["-e", script], { detached: true, stdio: ["ignore", "pipe", "ignore"] }); | ||
| const exited = new Promise((resolve) => child.on("exit", resolve)); | ||
| await new Promise((resolve) => child.stdout.once("data", resolve)); | ||
| h.seed({ id: "devin-handoff-done", kind: "handoff", phase: "running-cloud", pid: child.pid, cloudSessionId: "devin-example4" }); | ||
|
|
||
| const result = await h.run(); | ||
| assert.equal(result.status, 0, result.stderr); | ||
| await exited; | ||
| const job = h.stored("devin-handoff-done"); | ||
| assert.equal(job.status, "completed"); | ||
| assert.equal(job.phase, "exit"); | ||
| assert.deepEqual(h.requests, []); | ||
| } finally { | ||
| await h.cleanup(); | ||
| } | ||
| }); |
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.
Blocking – unchecked termination outcome.
terminateProcessTree(job.pid)can throw or reportdelivered: false;waitForExit(job.pid)returnsfalseon timeout/EPERM. Neither result is used, so lines 125–136 unconditionally clearpidand claimpoller stoppedwhile a SIGTERM-resistant poller may still be running. It can continueupsertJob/writeJobFile(including recordingfailedafter a polling error) and/devin:cancelloses the local PID. Please distinguish confirmed exit from unconfirmed exit, and only clear the PID/claim a stop after confirmation. For an unconfirmed stop, keep the cloud job active/refreshable, retain a means to kill the poller later and persist an accurate state/phase in both files. Simply retainingrunningis also insufficient if the PID subsequently exits:detectDeadJobwould markfailedbefore cloud refresh. Add a SIGTERM-ignoring poller test (longer than 1s), plus a simulated termination error/undelivered kill; assert a bounded hook, no HTTP calls, state/job-file consistency, continued/devin:statusrefresh, and correct/devin:cancelremote-stop behavior.