backeer-codex 0.12.0 → 0.13.1
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/.codex-plugin/plugin.json +1 -1
- package/README.md +59 -0
- package/dist/opencode.mjs +982 -529
- package/package.json +3 -3
- package/plugin.json +1 -1
- package/scripts/check-package.mjs +4 -1
- package/scripts/notification-worker.mjs +19 -0
- package/skills/backeer/SKILL.md +33 -0
- package/src/approval-bridge.mjs +28 -9
- package/src/approval-history.mjs +62 -0
- package/src/cli.mjs +3 -0
- package/src/command-frames.mjs +50 -0
- package/src/companion.mjs +33 -16
- package/src/gateway.mjs +13 -41
- package/src/hook-handlers.mjs +15 -10
- package/src/install-lock.mjs +43 -0
- package/src/installer.mjs +8 -1
- package/src/notification-outbox.mjs +170 -0
- package/src/version.mjs +1 -1
package/src/installer.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { acquireInstallLock } from "./install-lock.mjs";
|
|
1
2
|
import { randomUUID } from "node:crypto";
|
|
2
3
|
import fs from "node:fs/promises";
|
|
3
4
|
import os from "node:os";
|
|
@@ -101,7 +102,7 @@ function runCodexInstall(marketplaceName) {
|
|
|
101
102
|
});
|
|
102
103
|
}
|
|
103
104
|
|
|
104
|
-
|
|
105
|
+
async function installLocked(options = {}) {
|
|
105
106
|
const paths = personalPaths(options.homeDirectory);
|
|
106
107
|
// Validate before touching an installed plugin: a corrupt registry must not
|
|
107
108
|
// leave new files behind while reporting that the installation failed.
|
|
@@ -144,3 +145,9 @@ export async function installPlugin(options = {}) {
|
|
|
144
145
|
return { ...paths, marketplaceName };
|
|
145
146
|
}
|
|
146
147
|
|
|
148
|
+
|
|
149
|
+
export async function installPlugin(options = {}) {
|
|
150
|
+
const paths = personalPaths(options.homeDirectory);
|
|
151
|
+
const release = await acquireInstallLock(path.dirname(paths.pluginDirectory));
|
|
152
|
+
try { return await installLocked(options); } finally { await release(); }
|
|
153
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import { constants } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
5
|
+
import { spawn } from "node:child_process";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { dataDirectory } from "./config.mjs";
|
|
8
|
+
|
|
9
|
+
const READY = /^ready-\d+-[a-f0-9-]+\.json$/;
|
|
10
|
+
const ACTIVE = /^active-(\d+)-[a-f0-9-]+--(ready-\d+-[a-f0-9-]+\.json)$/;
|
|
11
|
+
const workers = new Set();
|
|
12
|
+
|
|
13
|
+
export function notificationOutboxDirectory(config, env = process.env) {
|
|
14
|
+
const scope = createHash("sha256").update(JSON.stringify([config.gatewayUrl, config.ingestToken])).digest("hex");
|
|
15
|
+
return path.join(dataDirectory(env), "notification-outbox", scope);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async function privateDirectory(directory) {
|
|
19
|
+
await fs.mkdir(directory, { recursive: true, mode: 0o700 });
|
|
20
|
+
const stat = await fs.lstat(directory);
|
|
21
|
+
if (!stat.isDirectory() || stat.isSymbolicLink() ||
|
|
22
|
+
(process.platform !== "win32" && ((stat.mode & 0o077) || stat.uid !== process.getuid()))) {
|
|
23
|
+
throw new Error("Private notification outbox required");
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function prepareDirectory(directory) {
|
|
28
|
+
await privateDirectory(path.dirname(directory));
|
|
29
|
+
await privateDirectory(directory);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Persist before any network attempt. Retries reuse the original externalId;
|
|
33
|
+
// gateway deduplication provides at-least-once delivery without a new event ID.
|
|
34
|
+
export async function enqueueNotification(config, body, env = process.env) {
|
|
35
|
+
if (!config.ingestToken) throw new Error("Notification source is not configured");
|
|
36
|
+
const directory = notificationOutboxDirectory(config, env);
|
|
37
|
+
await prepareDirectory(directory);
|
|
38
|
+
const id = randomUUID();
|
|
39
|
+
const record = JSON.stringify({ origin: config.gatewayUrl,
|
|
40
|
+
body: { ...body, externalId: body.externalId || `backeer-codex-outbox:${id}` } });
|
|
41
|
+
const temporary = path.join(directory, `writing-${id}.tmp`);
|
|
42
|
+
const ready = path.join(directory, `ready-${Date.now()}-${id}.json`);
|
|
43
|
+
const handle = await fs.open(temporary, "wx", 0o600);
|
|
44
|
+
try {
|
|
45
|
+
try { await handle.writeFile(record); await handle.sync(); }
|
|
46
|
+
finally { await handle.close(); }
|
|
47
|
+
await fs.rename(temporary, ready);
|
|
48
|
+
if (process.platform !== "win32") {
|
|
49
|
+
const folder = await fs.open(directory, constants.O_RDONLY | (constants.O_DIRECTORY ?? 0));
|
|
50
|
+
try { await folder.sync(); } finally { await folder.close(); }
|
|
51
|
+
}
|
|
52
|
+
} finally { await fs.unlink(temporary).catch(() => {}); }
|
|
53
|
+
return { delivered: false, queued: true };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function readRecord(file, origin) {
|
|
57
|
+
const handle = await fs.open(file, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0));
|
|
58
|
+
try {
|
|
59
|
+
const stat = await handle.stat();
|
|
60
|
+
if (!stat.isFile() ||
|
|
61
|
+
(process.platform !== "win32" && ((stat.mode & 0o077) || stat.uid !== process.getuid()))) {
|
|
62
|
+
throw new Error("Invalid notification outbox record");
|
|
63
|
+
}
|
|
64
|
+
const record = JSON.parse(await handle.readFile("utf8"));
|
|
65
|
+
if (record.origin !== origin || !record.body || typeof record.body !== "object" ||
|
|
66
|
+
Array.isArray(record.body) || typeof record.body.externalId !== "string" || !record.body.externalId) {
|
|
67
|
+
throw new Error("Invalid notification outbox payload");
|
|
68
|
+
}
|
|
69
|
+
return record.body;
|
|
70
|
+
} finally { await handle.close(); }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function recover(directory) {
|
|
74
|
+
for (const name of await fs.readdir(directory)) {
|
|
75
|
+
const match = ACTIVE.exec(name);
|
|
76
|
+
if (!match) continue;
|
|
77
|
+
const file = path.join(directory, name);
|
|
78
|
+
try {
|
|
79
|
+
const stat = await fs.lstat(file);
|
|
80
|
+
if (!stat.isFile() || stat.isSymbolicLink()) continue;
|
|
81
|
+
let dead = false;
|
|
82
|
+
try { process.kill(Number(match[1]), 0); }
|
|
83
|
+
catch (error) { dead = error.code === "ESRCH"; }
|
|
84
|
+
// Rename preserves the original enqueue time. Age alone must never steal
|
|
85
|
+
// an old record from a live sender; sends already have a request deadline.
|
|
86
|
+
if (dead) await fs.rename(file, path.join(directory, match[2]));
|
|
87
|
+
} catch (error) { if (error.code !== "ENOENT") throw error; }
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function send(config, body, timeoutMs) {
|
|
92
|
+
const response = await fetch(`${config.gatewayUrl}/v1/events`, {
|
|
93
|
+
method: "POST", redirect: "error",
|
|
94
|
+
headers: { "Content-Type": "application/json", Accept: "application/json", Authorization: `Bearer ${config.ingestToken}` },
|
|
95
|
+
body: JSON.stringify(body), signal: AbortSignal.timeout(timeoutMs),
|
|
96
|
+
});
|
|
97
|
+
// Lifecycle delivery needs only the HTTP result, never an actionable event ID.
|
|
98
|
+
await response.body?.cancel().catch(() => {});
|
|
99
|
+
if (!response.ok) throw Object.assign(new Error(`Backeer HTTP ${response.status}`), { status: response.status });
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export async function notificationOutboxStats(config, env = process.env) {
|
|
103
|
+
let names;
|
|
104
|
+
try { names = await fs.readdir(notificationOutboxDirectory(config, env)); }
|
|
105
|
+
catch (error) { if (error.code === "ENOENT") return { pending: 0, failed: 0 }; throw error; }
|
|
106
|
+
return { pending: names.filter(name => READY.test(name) || ACTIVE.test(name)).length,
|
|
107
|
+
failed: names.filter(name => name.startsWith("failed-")).length };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export async function flushNotifications(config, { env = process.env, maxItems = 64, timeoutMs = 10_000 } = {}) {
|
|
111
|
+
const directory = notificationOutboxDirectory(config, env);
|
|
112
|
+
await prepareDirectory(directory);
|
|
113
|
+
await recover(directory);
|
|
114
|
+
const names = (await fs.readdir(directory)).filter(name => READY.test(name)).sort().slice(0, maxItems);
|
|
115
|
+
let delivered = 0, retryable = false;
|
|
116
|
+
for (const name of names) {
|
|
117
|
+
const ready = path.join(directory, name);
|
|
118
|
+
const active = path.join(directory, `active-${process.pid}-${randomUUID()}--${name}`);
|
|
119
|
+
try { await fs.rename(ready, active); }
|
|
120
|
+
catch (error) { if (error.code === "ENOENT") continue; throw error; }
|
|
121
|
+
let body;
|
|
122
|
+
try { body = await readRecord(active, config.gatewayUrl); }
|
|
123
|
+
catch {
|
|
124
|
+
await fs.rename(active, path.join(directory, name.replace(/^ready-/, "failed-")));
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
try {
|
|
128
|
+
await send(config, body, timeoutMs);
|
|
129
|
+
await fs.unlink(active);
|
|
130
|
+
delivered++;
|
|
131
|
+
} catch (error) {
|
|
132
|
+
retryable = !Number.isInteger(error.status) || error.status === 429 || error.status >= 500;
|
|
133
|
+
await fs.rename(active, retryable ? ready : path.join(directory, name.replace(/^ready-/, "failed-")));
|
|
134
|
+
if (retryable) break;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return { delivered, retryable, ...(await notificationOutboxStats(config, env)) };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Workers are bounded and have no open pipe to the host after handoff. Concurrent
|
|
141
|
+
// hooks use atomic record leases; a stopped/crashed worker is recovered next run.
|
|
142
|
+
export async function scheduleNotificationFlush(config, env = process.env) {
|
|
143
|
+
if (!config.ingestToken) return;
|
|
144
|
+
const directory = notificationOutboxDirectory(config, env);
|
|
145
|
+
if (workers.has(directory)) return;
|
|
146
|
+
workers.add(directory);
|
|
147
|
+
try {
|
|
148
|
+
if (!(await notificationOutboxStats(config, env)).pending) { workers.delete(directory); return; }
|
|
149
|
+
const child = spawn(process.execPath, [fileURLToPath(new URL("../scripts/notification-worker.mjs", import.meta.url))], {
|
|
150
|
+
detached: true, stdio: ["pipe", "ignore", "ignore"], windowsHide: true,
|
|
151
|
+
});
|
|
152
|
+
child.once("exit", () => workers.delete(directory));
|
|
153
|
+
child.once("error", () => workers.delete(directory));
|
|
154
|
+
await new Promise((resolve, reject) => {
|
|
155
|
+
child.once("error", reject);
|
|
156
|
+
child.stdin.once("error", reject);
|
|
157
|
+
child.stdin.end(JSON.stringify({ config: { gatewayUrl: config.gatewayUrl, ingestToken: config.ingestToken },
|
|
158
|
+
dataDirectory: dataDirectory(env) }), resolve);
|
|
159
|
+
});
|
|
160
|
+
child.unref();
|
|
161
|
+
} catch (error) { workers.delete(directory); throw error; }
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export async function queueNotification(config, body, env = process.env) {
|
|
165
|
+
const result = await enqueueNotification(config, body, env);
|
|
166
|
+
// Worker startup is best effort; persistence is not. A later hook retries any
|
|
167
|
+
// backlog left by an unavailable executable or an interrupted worker.
|
|
168
|
+
await scheduleNotificationFlush(config, env).catch(() => {});
|
|
169
|
+
return result;
|
|
170
|
+
}
|
package/src/version.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = "0.
|
|
1
|
+
export const VERSION = "0.13.0";
|