Skip to content

Commit 05e70f3

Browse files
committed
fix: polish pr51 follow-up fixes
1 parent 485d25c commit 05e70f3

4 files changed

Lines changed: 27 additions & 33 deletions

File tree

‎plugins/opencode/scripts/lib/opencode-server.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// (Apache License 2.0 §4(b) modification notice — see NOTICE.)
2121

2222
import { spawn } from "node:child_process";
23+
import fs from "node:fs";
2324
import path from "node:path";
2425
import { platformShellOption } from "./process.mjs";
2526

‎plugins/opencode/scripts/lib/process.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function platformShellOption() {
3636
* @returns {Promise<string|null>}
3737
*/
3838
export async function resolveOpencodeBinary() {
39-
return new Promise((resolve, reject) => {
39+
return new Promise((resolve) => {
4040
const isWin = process.platform === "win32";
4141
const locator = isWin ? "where" : "which";
4242
const proc = spawn(locator, ["opencode"], {
@@ -46,7 +46,7 @@ export async function resolveOpencodeBinary() {
4646
});
4747
let out = "";
4848
proc.stdout.on("data", (d) => (out += d));
49-
proc.on("error", (err) => reject(err));
49+
proc.on("error", () => resolve(null));
5050
proc.on("close", (code) => {
5151
if (code !== 0) return resolve(null);
5252
// `where` returns all matches separated by CRLF; pick the first.

‎plugins/opencode/scripts/lib/state.mjs‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ function waitForMigration(primaryState) {
3535
}
3636

3737
function acquireFallbackLock(fallbackDir) {
38-
const lockPath = path.join(fallbackDir, ".migration.lock");
39-
ensureDir(fallbackDir);
38+
const lockPath = `${fallbackDir}.migration.lock`;
39+
ensureDir(path.dirname(lockPath));
4040
const deadline = Date.now() + FALLBACK_LOCK_STALE_MS;
4141
while (true) {
4242
try {
43-
return fs.openSync(lockPath, "wx");
43+
return { fd: fs.openSync(lockPath, "wx"), lockPath };
4444
} catch (err) {
4545
if (err?.code !== "EEXIST") throw err;
4646
try {
@@ -58,11 +58,12 @@ function acquireFallbackLock(fallbackDir) {
5858
}
5959
}
6060

61-
function releaseFallbackLock(fd, lockPath) {
62-
if (fd != null) {
63-
try { fs.closeSync(fd); } catch {}
61+
function releaseFallbackLock(lock) {
62+
if (!lock) return;
63+
if (lock.fd != null) {
64+
try { fs.closeSync(lock.fd); } catch {}
6465
}
65-
try { fs.rmSync(lockPath, { force: true }); } catch {}
66+
try { fs.rmSync(lock.lockPath, { force: true }); } catch {}
6667
}
6768

6869
function acquireMigrationLock(lockPath, primaryState) {
@@ -181,14 +182,14 @@ function migrateTmpdirStateIfNeeded(fallbackDir, primaryDir) {
181182
const lockPath = `${primaryDir}.migrate.lock`;
182183
const stageDir = `${primaryDir}.migrate-${process.pid}-${Date.now()}-${crypto.randomBytes(4).toString("hex")}`;
183184
let lockFd = null;
184-
let fallbackLockFd = null;
185+
let fallbackLock = null;
185186
try {
186187
lockFd = acquireMigrationLock(lockPath, primaryState);
187188
if (lockFd === null) return;
188189
if (fs.existsSync(primaryState) || !fs.existsSync(fallbackState)) return;
189190

190-
fallbackLockFd = acquireFallbackLock(fallbackDir);
191-
if (fallbackLockFd === null) return;
191+
fallbackLock = acquireFallbackLock(fallbackDir);
192+
if (fallbackLock === null) return;
192193

193194
assertSafeMigrationTree(fallbackDir);
194195
fs.rmSync(stageDir, { recursive: true, force: true });
@@ -217,9 +218,7 @@ function migrateTmpdirStateIfNeeded(fallbackDir, primaryDir) {
217218
}
218219
fs.rmSync(lockPath, { force: true });
219220
}
220-
if (fallbackLockFd !== null) {
221-
releaseFallbackLock(fallbackLockFd, path.join(fallbackDir, ".migration.lock"));
222-
}
221+
releaseFallbackLock(fallbackLock);
223222
}
224223
}
225224

@@ -311,12 +310,16 @@ function releaseStateLock(lock) {
311310
try {
312311
fs.rmSync(lock.lockPath, { force: true });
313312
const dirPath = path.dirname(lock.lockPath);
313+
let dirFd = null;
314314
try {
315-
const dirFd = fs.openSync(dirPath, "r");
315+
dirFd = fs.openSync(dirPath, "r");
316316
fs.fsyncSync(dirFd);
317-
fs.closeSync(dirFd);
318317
} catch {
319318
// fsync best-effort
319+
} finally {
320+
if (dirFd !== null) {
321+
try { fs.closeSync(dirFd); } catch {}
322+
}
320323
}
321324
} catch {}
322325
}

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,6 @@ async function handleTask(argv) {
520520
// Set up a worktree session if requested. Foreground mode only.
521521
let worktreeSession = null;
522522
let effectiveCwd = workspace;
523-
let taskError = null;
524523
if (useWorktree) {
525524
try {
526525
worktreeSession = await createWorktreeSession(workspace);
@@ -542,9 +541,8 @@ async function handleTask(argv) {
542541
}
543542

544543
// Foreground mode
545-
let result;
546544
try {
547-
result = await runTrackedJob(workspace, job, async ({ report, log }) => {
545+
const result = await runTrackedJob(workspace, job, async ({ report, log }) => {
548546
report("starting", "Connecting to OpenCode server...");
549547
const client = await connect({ cwd: effectiveCwd });
550548

@@ -624,19 +622,16 @@ async function handleTask(argv) {
624622

625623
console.log(result.rendered);
626624
} catch (err) {
627-
taskError = err;
628-
// Don't clean up worktree here - let finally handle it
629-
console.error(`Task failed: ${err.message}`);
630-
process.exit(1);
631-
} finally {
632-
// Always clean up worktree on exit, whether success or failure
625+
// On any failure with a worktree, clean it up so we don't leak dirs.
633626
if (worktreeSession) {
634627
try {
635-
await cleanupWorktreeSession(worktreeSession, { keep: taskError === null });
628+
await cleanupWorktreeSession(worktreeSession, { keep: false });
636629
} catch {
637630
// best-effort
638631
}
639632
}
633+
console.error(`Task failed: ${err.message}`);
634+
process.exit(1);
640635
}
641636
}
642637

@@ -1031,12 +1026,7 @@ function saveLastReview(workspace, rendered) {
10311026
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
10321027
tmp = `${file}.${process.pid}.${Date.now()}.tmp`;
10331028
fs.writeFileSync(tmp, rendered, "utf8");
1034-
try {
1035-
fs.unlinkSync(file);
1036-
} catch (err) {
1037-
if (err?.code !== "ENOENT") throw err;
1038-
}
1039-
fs.copyFileSync(tmp, file);
1029+
fs.renameSync(tmp, file);
10401030
tmp = null;
10411031
} catch {
10421032
// best-effort

0 commit comments

Comments
 (0)