opencode-copilot-delegate 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/dist/index.js +110 -21
- package/dist/runtime/notify.d.ts +8 -1
- package/dist/runtime/pid-file.d.ts +3 -0
- package/dist/runtime/task-status.d.ts +9 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# opencode-copilot-delegate
|
|
2
2
|
|
|
3
|
+
## 0.9.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 80b549e: Improve runtime observability and fix a silent failure in completion notifications.
|
|
8
|
+
|
|
9
|
+
- `killProcessTree` now classifies fkill failures by probing the process group with `process.kill(-pid, 0)`. ESRCH is reported as an already-gone group and the failure is suppressed; alive or unknown states preserve the original throw with a more accurate warning. The probe targets `-pid` (the process group) rather than the leader so children leaking after a leader exit are no longer misclassified as benign.
|
|
10
|
+
- `notifyCompletion`'s fallback `client.app.log` call now uses the structured SDK shape (`{ body: { service, level, message } }`) and is wrapped in `try/catch` so synchronous SDK throws can no longer escape the documented "never throws" contract.
|
|
11
|
+
- All runtime warnings now share the `[copilot-delegate]` prefix across `kill-tree`, `orphan-reaper`, `pid-file`, `task-registry`, and `task-status`, making operator log filtering predictable.
|
|
12
|
+
- Internal contract documentation: `setStatus` and `writeChains` carry JSDoc covering terminal-state transitions and the per-file serialize chain.
|
|
13
|
+
|
|
14
|
+
- 4f2e8b5: Harden PID file and orphan-reaper state path handling against same-user symlink attacks.
|
|
15
|
+
|
|
16
|
+
- Use no-follow PID file opens for read and truncate paths so symlinked `.pids` files are rejected instead of read or truncated.
|
|
17
|
+
- Reject symlinked PID file parent directories before orphan reaping, cleanup, and plugin init state-directory creation so `orphans/` scans and cleanup do not follow attacker-controlled links.
|
|
18
|
+
|
|
3
19
|
## 0.8.0
|
|
4
20
|
|
|
5
21
|
### Minor Changes
|
package/dist/index.js
CHANGED
|
@@ -9343,7 +9343,35 @@ async function fkill(inputs, options = {}) {
|
|
|
9343
9343
|
}
|
|
9344
9344
|
}
|
|
9345
9345
|
|
|
9346
|
+
// src/lib/errno.ts
|
|
9347
|
+
function isErrnoException(e) {
|
|
9348
|
+
return e instanceof Error && "code" in e;
|
|
9349
|
+
}
|
|
9350
|
+
|
|
9346
9351
|
// src/lib/kill-tree.ts
|
|
9352
|
+
function formatError(error) {
|
|
9353
|
+
if (error instanceof AggregateError && error.errors.length > 0) {
|
|
9354
|
+
return error.errors.map((entry) => String(entry)).join("; ");
|
|
9355
|
+
}
|
|
9356
|
+
if (error instanceof Error) {
|
|
9357
|
+
return error.message;
|
|
9358
|
+
}
|
|
9359
|
+
return String(error);
|
|
9360
|
+
}
|
|
9361
|
+
function probeRootPid(pid) {
|
|
9362
|
+
try {
|
|
9363
|
+
process.kill(-pid, 0);
|
|
9364
|
+
return { state: "alive" };
|
|
9365
|
+
} catch (error) {
|
|
9366
|
+
if (isErrnoException(error) && error.code === "ESRCH") {
|
|
9367
|
+
return { state: "gone" };
|
|
9368
|
+
}
|
|
9369
|
+
return {
|
|
9370
|
+
state: "unknown",
|
|
9371
|
+
code: isErrnoException(error) ? error.code : undefined
|
|
9372
|
+
};
|
|
9373
|
+
}
|
|
9374
|
+
}
|
|
9347
9375
|
async function killProcessTree(pid) {
|
|
9348
9376
|
if (!Number.isFinite(pid) || pid <= 1) {
|
|
9349
9377
|
return;
|
|
@@ -9353,7 +9381,22 @@ async function killProcessTree(pid) {
|
|
|
9353
9381
|
forceAfterTimeout: 2000,
|
|
9354
9382
|
waitForExit: 5000
|
|
9355
9383
|
};
|
|
9356
|
-
|
|
9384
|
+
try {
|
|
9385
|
+
await fkill(-pid, options);
|
|
9386
|
+
} catch (error) {
|
|
9387
|
+
const probe = probeRootPid(pid);
|
|
9388
|
+
const failure = formatError(error);
|
|
9389
|
+
if (probe.state === "gone") {
|
|
9390
|
+
console.warn(`[copilot-delegate] fkill failed for pid ${pid}, but root pid ${pid} is already gone; skipping follow-up kill handling (${failure})`);
|
|
9391
|
+
return;
|
|
9392
|
+
}
|
|
9393
|
+
if (probe.state === "alive") {
|
|
9394
|
+
console.warn(`[copilot-delegate] fkill failed for pid ${pid}, and root pid ${pid} is still alive; processes may remain running (${failure})`);
|
|
9395
|
+
throw error;
|
|
9396
|
+
}
|
|
9397
|
+
console.warn(`[copilot-delegate] fkill failed for pid ${pid}, and root pid probe failed with ${probe.code ?? "unknown error"}; process state is uncertain (${failure})`);
|
|
9398
|
+
throw error;
|
|
9399
|
+
}
|
|
9357
9400
|
}
|
|
9358
9401
|
|
|
9359
9402
|
// src/lib/normalize-tool-arg-schemas.ts
|
|
@@ -9385,29 +9428,52 @@ function normalizeToolArgSchemas(toolDefinition) {
|
|
|
9385
9428
|
|
|
9386
9429
|
// src/runtime/orphan-reaper.ts
|
|
9387
9430
|
import { spawn as spawn2 } from "child_process";
|
|
9388
|
-
import { readdir
|
|
9431
|
+
import { readdir } from "fs/promises";
|
|
9389
9432
|
import { basename as basename2, extname, join as join2 } from "path";
|
|
9390
9433
|
|
|
9391
|
-
// src/lib/errno.ts
|
|
9392
|
-
function isErrnoException(e) {
|
|
9393
|
-
return e instanceof Error && "code" in e;
|
|
9394
|
-
}
|
|
9395
|
-
|
|
9396
9434
|
// src/runtime/pid-file.ts
|
|
9397
9435
|
import { randomBytes } from "crypto";
|
|
9398
9436
|
import {
|
|
9399
9437
|
chmod,
|
|
9400
9438
|
constants as fsConstants,
|
|
9439
|
+
lstat,
|
|
9401
9440
|
mkdir,
|
|
9402
9441
|
open,
|
|
9403
|
-
readFile,
|
|
9404
9442
|
rename,
|
|
9405
|
-
unlink
|
|
9406
|
-
writeFile
|
|
9443
|
+
unlink
|
|
9407
9444
|
} from "fs/promises";
|
|
9408
9445
|
import { homedir } from "os";
|
|
9409
9446
|
import { dirname, join } from "path";
|
|
9410
9447
|
var writeChains = new Map;
|
|
9448
|
+
var oNoFollow = fsConstants.O_NOFOLLOW ?? 0;
|
|
9449
|
+
function symlinkPathError(path11) {
|
|
9450
|
+
return new Error(`[copilot-delegate] refusing symlink path: ${path11}`);
|
|
9451
|
+
}
|
|
9452
|
+
async function assertNotSymlink(path11) {
|
|
9453
|
+
const stats = await lstat(path11);
|
|
9454
|
+
if (stats.isSymbolicLink()) {
|
|
9455
|
+
throw symlinkPathError(path11);
|
|
9456
|
+
}
|
|
9457
|
+
}
|
|
9458
|
+
async function assertPluginStateDirNotSymlink(filePath) {
|
|
9459
|
+
try {
|
|
9460
|
+
await assertNotSymlink(dirname(dirname(filePath)));
|
|
9461
|
+
} catch (e) {
|
|
9462
|
+
if (!isErrnoException(e) || e.code !== "ENOENT")
|
|
9463
|
+
throw e;
|
|
9464
|
+
}
|
|
9465
|
+
}
|
|
9466
|
+
async function assertOrphansDirNotSymlink(filePath) {
|
|
9467
|
+
await assertNotSymlink(dirname(filePath));
|
|
9468
|
+
}
|
|
9469
|
+
async function readPidFileNoFollow(filePath) {
|
|
9470
|
+
const fd = await open(filePath, oNoFollow | fsConstants.O_RDONLY);
|
|
9471
|
+
try {
|
|
9472
|
+
return await fd.readFile({ encoding: "utf-8" });
|
|
9473
|
+
} finally {
|
|
9474
|
+
await fd.close();
|
|
9475
|
+
}
|
|
9476
|
+
}
|
|
9411
9477
|
async function serializeWrite(filePath, work) {
|
|
9412
9478
|
const prev = writeChains.get(filePath) ?? Promise.resolve();
|
|
9413
9479
|
const next = prev.catch(() => {}).then(work);
|
|
@@ -9426,10 +9492,12 @@ function resolveInstancePidFilePath() {
|
|
|
9426
9492
|
}
|
|
9427
9493
|
async function appendPidEntry(filePath, pid, comm, lstart) {
|
|
9428
9494
|
await serializeWrite(filePath, async () => {
|
|
9495
|
+
await assertPluginStateDirNotSymlink(filePath);
|
|
9429
9496
|
await mkdir(dirname(filePath), { recursive: true, mode: 448 });
|
|
9497
|
+
await assertOrphansDirNotSymlink(filePath);
|
|
9430
9498
|
let existing = "";
|
|
9431
9499
|
try {
|
|
9432
|
-
existing = await
|
|
9500
|
+
existing = await readPidFileNoFollow(filePath);
|
|
9433
9501
|
} catch (e) {
|
|
9434
9502
|
if (!isErrnoException(e) || e.code !== "ENOENT")
|
|
9435
9503
|
throw e;
|
|
@@ -9438,7 +9506,7 @@ async function appendPidEntry(filePath, pid, comm, lstart) {
|
|
|
9438
9506
|
`;
|
|
9439
9507
|
const content = existing + line;
|
|
9440
9508
|
const tempPath = `${filePath}.tmp.${randomBytes(8).toString("hex")}`;
|
|
9441
|
-
const fd = await open(tempPath, fsConstants.O_EXCL | fsConstants.O_CREAT | fsConstants.O_WRONLY, 384);
|
|
9509
|
+
const fd = await open(tempPath, fsConstants.O_EXCL | fsConstants.O_CREAT | oNoFollow | fsConstants.O_WRONLY, 384);
|
|
9442
9510
|
try {
|
|
9443
9511
|
await fd.writeFile(content, { encoding: "utf-8" });
|
|
9444
9512
|
} finally {
|
|
@@ -9452,7 +9520,8 @@ async function removePidEntry(filePath, pid) {
|
|
|
9452
9520
|
await serializeWrite(filePath, async () => {
|
|
9453
9521
|
let existing = "";
|
|
9454
9522
|
try {
|
|
9455
|
-
|
|
9523
|
+
await assertOrphansDirNotSymlink(filePath);
|
|
9524
|
+
existing = await readPidFileNoFollow(filePath);
|
|
9456
9525
|
} catch (e) {
|
|
9457
9526
|
if (!isErrnoException(e) || e.code !== "ENOENT")
|
|
9458
9527
|
throw e;
|
|
@@ -9468,7 +9537,7 @@ async function removePidEntry(filePath, pid) {
|
|
|
9468
9537
|
const content = filtered.join(`
|
|
9469
9538
|
`);
|
|
9470
9539
|
const tempPath = `${filePath}.tmp.${randomBytes(8).toString("hex")}`;
|
|
9471
|
-
const fd = await open(tempPath, fsConstants.O_EXCL | fsConstants.O_CREAT | fsConstants.O_WRONLY, 384);
|
|
9540
|
+
const fd = await open(tempPath, fsConstants.O_EXCL | fsConstants.O_CREAT | oNoFollow | fsConstants.O_WRONLY, 384);
|
|
9472
9541
|
try {
|
|
9473
9542
|
await fd.writeFile(content, { encoding: "utf-8" });
|
|
9474
9543
|
} finally {
|
|
@@ -9484,7 +9553,13 @@ async function removePidEntry(filePath, pid) {
|
|
|
9484
9553
|
async function truncatePidFile(filePath) {
|
|
9485
9554
|
await serializeWrite(filePath, async () => {
|
|
9486
9555
|
try {
|
|
9487
|
-
await
|
|
9556
|
+
await assertOrphansDirNotSymlink(filePath);
|
|
9557
|
+
const fd = await open(filePath, oNoFollow | fsConstants.O_WRONLY);
|
|
9558
|
+
try {
|
|
9559
|
+
await fd.truncate(0);
|
|
9560
|
+
} finally {
|
|
9561
|
+
await fd.close();
|
|
9562
|
+
}
|
|
9488
9563
|
} catch (e) {
|
|
9489
9564
|
if (!isErrnoException(e) || e.code !== "ENOENT")
|
|
9490
9565
|
throw e;
|
|
@@ -9494,6 +9569,7 @@ async function truncatePidFile(filePath) {
|
|
|
9494
9569
|
async function unlinkPidFile(filePath) {
|
|
9495
9570
|
await serializeWrite(filePath, async () => {
|
|
9496
9571
|
try {
|
|
9572
|
+
await assertOrphansDirNotSymlink(filePath);
|
|
9497
9573
|
await unlink(filePath);
|
|
9498
9574
|
} catch (e) {
|
|
9499
9575
|
if (!isErrnoException(e) || e.code !== "ENOENT")
|
|
@@ -9519,7 +9595,7 @@ function runPs(args, timeoutMs = DEFAULT_PS_TIMEOUT_MS) {
|
|
|
9519
9595
|
let timedOut = false;
|
|
9520
9596
|
const timeout = setTimeout(() => {
|
|
9521
9597
|
timedOut = true;
|
|
9522
|
-
console.warn(`[
|
|
9598
|
+
console.warn(`[copilot-delegate] ps invocation exceeded ${timeoutMs}ms timeout: ps ${args.join(" ")}`);
|
|
9523
9599
|
child.kill("SIGTERM");
|
|
9524
9600
|
}, timeoutMs);
|
|
9525
9601
|
child.stdout?.on("data", (chunk) => {
|
|
@@ -9616,7 +9692,8 @@ async function processEntries(entries, killProcessTree2, getPidIdentity2, signal
|
|
|
9616
9692
|
async function readPidFileEntries(filePath) {
|
|
9617
9693
|
let content;
|
|
9618
9694
|
try {
|
|
9619
|
-
|
|
9695
|
+
await assertOrphansDirNotSymlink(filePath);
|
|
9696
|
+
content = await readPidFileNoFollow(filePath);
|
|
9620
9697
|
} catch {
|
|
9621
9698
|
return null;
|
|
9622
9699
|
}
|
|
@@ -9633,6 +9710,7 @@ async function readPidFileEntries(filePath) {
|
|
|
9633
9710
|
}
|
|
9634
9711
|
async function cleanupAfterReap(filePath, isCurrent, currentInstancePath) {
|
|
9635
9712
|
try {
|
|
9713
|
+
await assertOrphansDirNotSymlink(isCurrent ? currentInstancePath : filePath);
|
|
9636
9714
|
if (isCurrent) {
|
|
9637
9715
|
await truncatePidFile(currentInstancePath);
|
|
9638
9716
|
return false;
|
|
@@ -9687,6 +9765,7 @@ async function doReap(opts) {
|
|
|
9687
9765
|
} = opts;
|
|
9688
9766
|
let files;
|
|
9689
9767
|
try {
|
|
9768
|
+
await assertOrphansDirNotSymlink(currentInstancePath);
|
|
9690
9769
|
files = await readdir(pidFileDir);
|
|
9691
9770
|
} catch {
|
|
9692
9771
|
return {
|
|
@@ -9709,7 +9788,7 @@ async function doReap(opts) {
|
|
|
9709
9788
|
const filePath = join2(pidFileDir, file);
|
|
9710
9789
|
const stem = basename2(file, extname(file));
|
|
9711
9790
|
if (!/^[1-9]\d*$/.test(stem)) {
|
|
9712
|
-
console.warn(`[
|
|
9791
|
+
console.warn(`[copilot-delegate] Skipping non-numeric PID file: ${file}`);
|
|
9713
9792
|
continue;
|
|
9714
9793
|
}
|
|
9715
9794
|
const filePid = Number(stem);
|
|
@@ -9747,7 +9826,7 @@ async function reapOrphans(opts) {
|
|
|
9747
9826
|
let timeoutId;
|
|
9748
9827
|
const timeoutPromise = new Promise((resolve) => {
|
|
9749
9828
|
timeoutId = setTimeout(() => {
|
|
9750
|
-
console.warn(`[
|
|
9829
|
+
console.warn(`[copilot-delegate] reapOrphans exceeded ${reapTimeoutMs}ms budget; returning empty result`);
|
|
9751
9830
|
controller.abort();
|
|
9752
9831
|
resolve({ ...zeros, timedOut: true });
|
|
9753
9832
|
}, reapTimeoutMs);
|
|
@@ -9808,7 +9887,7 @@ function createTask(input, taskId = `cpl_${randomUUID()}`) {
|
|
|
9808
9887
|
if (identity3) {
|
|
9809
9888
|
appendPidEntry(pidFilePath, task.pid, identity3.comm, identity3.lstart).catch(() => {});
|
|
9810
9889
|
} else {
|
|
9811
|
-
console.warn(`[
|
|
9890
|
+
console.warn(`[copilot-delegate] ps lookup returned null for pid ${task.pid}; subprocess will not be tracked for orphan reaping`);
|
|
9812
9891
|
}
|
|
9813
9892
|
}).catch(() => {});
|
|
9814
9893
|
}
|
|
@@ -9922,7 +10001,15 @@ async function notifyCompletion(client, task) {
|
|
|
9922
10001
|
}
|
|
9923
10002
|
});
|
|
9924
10003
|
} catch (error) {
|
|
9925
|
-
|
|
10004
|
+
try {
|
|
10005
|
+
client.app.log({
|
|
10006
|
+
body: {
|
|
10007
|
+
service: "copilot-delegate",
|
|
10008
|
+
level: "warn",
|
|
10009
|
+
message: `Failed to inject notification for ${task.taskId}: ${error}`
|
|
10010
|
+
}
|
|
10011
|
+
}).catch(() => {});
|
|
10012
|
+
} catch {}
|
|
9926
10013
|
}
|
|
9927
10014
|
try {
|
|
9928
10015
|
const variant = task.status === "complete" ? "success" : "error";
|
|
@@ -10443,7 +10530,9 @@ async function initializePlugin({ client, directory }) {
|
|
|
10443
10530
|
const currentInstancePath = resolveInstancePidFilePath();
|
|
10444
10531
|
const pidFileDir = dirname2(currentInstancePath);
|
|
10445
10532
|
try {
|
|
10533
|
+
await assertPluginStateDirNotSymlink(currentInstancePath);
|
|
10446
10534
|
await mkdir2(pidFileDir, { recursive: true, mode: 448 });
|
|
10535
|
+
await assertOrphansDirNotSymlink(currentInstancePath);
|
|
10447
10536
|
await reapOrphans({
|
|
10448
10537
|
pidFileDir,
|
|
10449
10538
|
currentInstancePath,
|
package/dist/runtime/notify.d.ts
CHANGED
|
@@ -36,7 +36,14 @@ export type NotifyClient = {
|
|
|
36
36
|
}) => unknown;
|
|
37
37
|
};
|
|
38
38
|
app: {
|
|
39
|
-
log: (
|
|
39
|
+
log: (opts: {
|
|
40
|
+
body: {
|
|
41
|
+
service: string;
|
|
42
|
+
level: string;
|
|
43
|
+
message: string;
|
|
44
|
+
extra?: unknown;
|
|
45
|
+
};
|
|
46
|
+
}) => Promise<unknown>;
|
|
40
47
|
};
|
|
41
48
|
};
|
|
42
49
|
/** Task information needed for building the notification. */
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
export declare function assertPluginStateDirNotSymlink(filePath: string): Promise<void>;
|
|
2
|
+
export declare function assertOrphansDirNotSymlink(filePath: string): Promise<void>;
|
|
3
|
+
export declare function readPidFileNoFollow(filePath: string): Promise<string>;
|
|
1
4
|
export declare function resolveInstancePidFilePath(): string;
|
|
2
5
|
export declare function appendPidEntry(filePath: string, pid: number, comm: string, lstart: string): Promise<void>;
|
|
3
6
|
export declare function removePidEntry(filePath: string, pid: number): Promise<void>;
|
|
@@ -2,6 +2,15 @@ import type { TaskStatus } from './envelope';
|
|
|
2
2
|
export interface SetStatusOptions {
|
|
3
3
|
pidFilePath?: string;
|
|
4
4
|
}
|
|
5
|
+
/**
|
|
6
|
+
* Transition `task` to `newStatus`, enforcing the forward-only lifecycle.
|
|
7
|
+
*
|
|
8
|
+
* Idempotent on any terminal state (complete, failed, cancelled): once a task
|
|
9
|
+
* reaches a terminal status every subsequent call is a no-op. This prevents
|
|
10
|
+
* both terminal→terminal re-classification and terminal→non-terminal
|
|
11
|
+
* resurrection. When transitioning to a terminal state and `pidFilePath` is
|
|
12
|
+
* provided, the task's PID entry is removed from the orphan-reaper file.
|
|
13
|
+
*/
|
|
5
14
|
export declare function setStatus(task: {
|
|
6
15
|
status: TaskStatus;
|
|
7
16
|
pid: number;
|