opencode-supertask 0.1.40 → 0.2.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 +95 -1
- package/LICENSE +21 -0
- package/README.md +73 -39
- package/README.zh-CN.md +73 -39
- package/dist/cli/chunk-3PIYMBV3.js +45 -0
- package/dist/cli/chunk-3PIYMBV3.js.map +1 -0
- package/dist/cli/chunk-4FK5KE6G.js +111 -0
- package/dist/cli/chunk-4FK5KE6G.js.map +1 -0
- package/dist/cli/chunk-5VKFECHP.js +412 -0
- package/dist/cli/chunk-5VKFECHP.js.map +1 -0
- package/dist/cli/chunk-AZM76ISG.js +143 -0
- package/dist/cli/chunk-AZM76ISG.js.map +1 -0
- package/dist/cli/chunk-CMEP6IB4.js +9379 -0
- package/dist/cli/chunk-CMEP6IB4.js.map +1 -0
- package/dist/cli/chunk-DF56RPNL.js +65 -0
- package/dist/cli/chunk-DF56RPNL.js.map +1 -0
- package/dist/cli/chunk-DIJEKFBV.js +486 -0
- package/dist/cli/chunk-DIJEKFBV.js.map +1 -0
- package/dist/cli/chunk-DYXFSX5J.js +28 -0
- package/dist/cli/chunk-DYXFSX5J.js.map +1 -0
- package/dist/cli/chunk-EQWICVCJ.js +1701 -0
- package/dist/cli/chunk-EQWICVCJ.js.map +1 -0
- package/dist/cli/chunk-GHXFMJTS.js +42 -0
- package/dist/cli/chunk-GHXFMJTS.js.map +1 -0
- package/dist/cli/chunk-IYYBJHZF.js +28 -0
- package/dist/cli/chunk-IYYBJHZF.js.map +1 -0
- package/dist/cli/chunk-J34FWHQU.js +376 -0
- package/dist/cli/chunk-J34FWHQU.js.map +1 -0
- package/dist/cli/chunk-LX5CBJLP.js +121 -0
- package/dist/cli/chunk-LX5CBJLP.js.map +1 -0
- package/dist/cli/chunk-MVQ2XONT.js +139 -0
- package/dist/cli/chunk-MVQ2XONT.js.map +1 -0
- package/dist/cli/chunk-NCT3ETTK.js +1332 -0
- package/dist/cli/chunk-NCT3ETTK.js.map +1 -0
- package/dist/cli/chunk-YB7MYTE5.js +34 -0
- package/dist/cli/chunk-YB7MYTE5.js.map +1 -0
- package/dist/cli/chunk-YGELD2E5.js +5464 -0
- package/dist/cli/chunk-YGELD2E5.js.map +1 -0
- package/dist/cli/chunk-ZBH7YWXF.js +314 -0
- package/dist/cli/chunk-ZBH7YWXF.js.map +1 -0
- package/dist/cli/config-ED56ZLHY.js +20 -0
- package/dist/cli/config-ED56ZLHY.js.map +1 -0
- package/dist/cli/database-maintenance.service-F3IZU52F.js +16 -0
- package/dist/cli/database-maintenance.service-F3IZU52F.js.map +1 -0
- package/dist/cli/database-output-GH7F6X6D.js +165 -0
- package/dist/cli/database-output-GH7F6X6D.js.map +1 -0
- package/dist/cli/db-H7GIFWS2.js +26 -0
- package/dist/cli/db-H7GIFWS2.js.map +1 -0
- package/dist/cli/doctor-smoke-QXHF7UNN.js +150 -0
- package/dist/cli/doctor-smoke-QXHF7UNN.js.map +1 -0
- package/dist/cli/duration-Q3UMLO6F.js +10 -0
- package/dist/cli/duration-Q3UMLO6F.js.map +1 -0
- package/dist/cli/gateway-HNMX5ZTO.js +1738 -0
- package/dist/cli/gateway-HNMX5ZTO.js.map +1 -0
- package/dist/cli/gateway-wake-U4HTV56Q.js +14 -0
- package/dist/cli/gateway-wake-U4HTV56Q.js.map +1 -0
- package/dist/cli/index.js +626 -26342
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/job-templates-RPQJ4XXW.js +21 -0
- package/dist/cli/job-templates-RPQJ4XXW.js.map +1 -0
- package/dist/cli/logs-QD2N62KK.js +316 -0
- package/dist/cli/logs-QD2N62KK.js.map +1 -0
- package/dist/cli/output-2INTEUFQ.js +19 -0
- package/dist/cli/output-2INTEUFQ.js.map +1 -0
- package/dist/cli/pm2-WY644BUO.js +49 -0
- package/dist/cli/pm2-WY644BUO.js.map +1 -0
- package/dist/cli/task-run.service-QMDINJKG.js +15 -0
- package/dist/cli/task-run.service-QMDINJKG.js.map +1 -0
- package/dist/cli/task-template.service-SZBKGLCB.js +12 -0
- package/dist/cli/task-template.service-SZBKGLCB.js.map +1 -0
- package/dist/cli/task.service-ZVDJ6VSQ.js +19 -0
- package/dist/cli/task.service-ZVDJ6VSQ.js.map +1 -0
- package/dist/cli/update-FYD546I2.js +2058 -0
- package/dist/cli/update-FYD546I2.js.map +1 -0
- package/dist/cli/web-2S7DW7XA.js +9403 -0
- package/dist/cli/web-2S7DW7XA.js.map +1 -0
- package/dist/daemon/gateway-diagnostic-runner.d.ts +2 -0
- package/dist/daemon/gateway-diagnostic-runner.js +503 -0
- package/dist/daemon/gateway-diagnostic-runner.js.map +1 -0
- package/dist/gateway/index.d.ts +82 -1
- package/dist/gateway/index.js +9301 -2948
- package/dist/gateway/index.js.map +1 -1
- package/dist/plugin/supertask.d.ts +157 -1
- package/dist/plugin/supertask.js +5066 -1531
- package/dist/plugin/supertask.js.map +1 -1
- package/dist/pm2-cli-CEDhULjk.d.ts +40 -0
- package/dist/web/index.d.ts +36 -34
- package/dist/web/index.js +10030 -4341
- package/dist/web/index.js.map +1 -1
- package/dist/worker/index.d.ts +13 -0
- package/dist/worker/index.js +2147 -364
- package/dist/worker/index.js.map +1 -1
- package/dist/worker/launcher.d.ts +46 -1
- package/dist/worker/launcher.js +218 -68
- package/dist/worker/launcher.js.map +1 -1
- package/drizzle/0009_little_weapon_omega.sql +7 -0
- package/drizzle/meta/0009_snapshot.json +681 -0
- package/drizzle/meta/_journal.json +7 -0
- package/package.json +15 -6
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
// src/daemon/pm2-cli.ts
|
|
2
|
+
import { execSync, spawnSync } from "child_process";
|
|
3
|
+
import {
|
|
4
|
+
accessSync,
|
|
5
|
+
constants,
|
|
6
|
+
existsSync,
|
|
7
|
+
mkdirSync,
|
|
8
|
+
readFileSync,
|
|
9
|
+
statSync,
|
|
10
|
+
writeFileSync
|
|
11
|
+
} from "fs";
|
|
12
|
+
import { homedir } from "os";
|
|
13
|
+
import { delimiter, dirname, isAbsolute, join, resolve } from "path";
|
|
14
|
+
import { fileURLToPath } from "url";
|
|
15
|
+
import { Database } from "bun:sqlite";
|
|
16
|
+
var __dirname = dirname(fileURLToPath(import.meta.url));
|
|
17
|
+
var PROCESS_NAME = "supertask-gateway";
|
|
18
|
+
var MAC_LAUNCH_AGENT_LABEL = "com.supertask.pm2-resurrect";
|
|
19
|
+
var PM2_DEFAULT_KILL_TIMEOUT_MS = 1600;
|
|
20
|
+
var GATEWAY_LOCK_STALE_MS = 3e4;
|
|
21
|
+
function runtimeHome(env) {
|
|
22
|
+
return resolve(env.HOME || homedir());
|
|
23
|
+
}
|
|
24
|
+
function runtimePath(value, cwd) {
|
|
25
|
+
return resolve(cwd, value);
|
|
26
|
+
}
|
|
27
|
+
function databasePath(env = process.env, cwd = process.cwd()) {
|
|
28
|
+
return env.SUPERTASK_DB_PATH ? runtimePath(env.SUPERTASK_DB_PATH, cwd) : join(runtimeHome(env), ".local/share/opencode/tasks.db");
|
|
29
|
+
}
|
|
30
|
+
function versionFile(env = process.env, cwd = process.cwd()) {
|
|
31
|
+
return env.SUPERTASK_VERSION_FILE ? runtimePath(env.SUPERTASK_VERSION_FILE, cwd) : join(runtimeHome(env), ".local/share/opencode/supertask-gateway-version");
|
|
32
|
+
}
|
|
33
|
+
function getRunningVersion(env = process.env, cwd = process.cwd()) {
|
|
34
|
+
try {
|
|
35
|
+
const path = versionFile(env, cwd);
|
|
36
|
+
if (!existsSync(path)) return null;
|
|
37
|
+
return readFileSync(path, "utf-8").trim() || null;
|
|
38
|
+
} catch {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function packageVersionFromGatewayEntry(gatewayEntry) {
|
|
43
|
+
if (gatewayEntry === null) return null;
|
|
44
|
+
let directory = dirname(gatewayEntry);
|
|
45
|
+
for (let depth = 0; depth < 6; depth += 1) {
|
|
46
|
+
const packagePath = join(directory, "package.json");
|
|
47
|
+
if (existsSync(packagePath)) {
|
|
48
|
+
try {
|
|
49
|
+
const pkg = JSON.parse(readFileSync(packagePath, "utf8"));
|
|
50
|
+
if (pkg.name === "opencode-supertask" && typeof pkg.version === "string") {
|
|
51
|
+
return pkg.version;
|
|
52
|
+
}
|
|
53
|
+
} catch {
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const parent = dirname(directory);
|
|
57
|
+
if (parent === directory) break;
|
|
58
|
+
directory = parent;
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
function resolveRuntimeExecutable(command, env, cwd) {
|
|
63
|
+
if (isAbsolute(command) || command.includes("/")) return runtimePath(command, cwd);
|
|
64
|
+
for (const entry of (env.PATH ?? "").split(delimiter)) {
|
|
65
|
+
if (!entry) continue;
|
|
66
|
+
const candidate = resolve(cwd, entry, command);
|
|
67
|
+
try {
|
|
68
|
+
accessSync(candidate, constants.X_OK);
|
|
69
|
+
return candidate;
|
|
70
|
+
} catch {
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return command;
|
|
74
|
+
}
|
|
75
|
+
function pm2Bin(env = process.env) {
|
|
76
|
+
return env.SUPERTASK_PM2_BIN ?? (process.platform === "win32" ? "pm2.cmd" : "pm2");
|
|
77
|
+
}
|
|
78
|
+
function pm2CommandTimeoutMs(env = process.env) {
|
|
79
|
+
const configured = Number(env.SUPERTASK_PM2_COMMAND_TIMEOUT_MS ?? 15e3);
|
|
80
|
+
return Number.isFinite(configured) && configured > 0 ? configured : 15e3;
|
|
81
|
+
}
|
|
82
|
+
function resolvePm2Bin(env = process.env) {
|
|
83
|
+
const configured = pm2Bin(env);
|
|
84
|
+
if (isAbsolute(configured)) return configured;
|
|
85
|
+
const result = spawnSync("which", [configured], { encoding: "utf8", env });
|
|
86
|
+
const resolved = result.status === 0 ? result.stdout.trim().split("\n")[0] : "";
|
|
87
|
+
if (!resolved) throw new Error(`[supertask] \u65E0\u6CD5\u89E3\u6790 pm2 \u53EF\u6267\u884C\u6587\u4EF6: ${configured}`);
|
|
88
|
+
return resolved;
|
|
89
|
+
}
|
|
90
|
+
function launchAgentPath() {
|
|
91
|
+
return process.env.SUPERTASK_LAUNCH_AGENT_PATH ?? join(runtimeHome(process.env), "Library/LaunchAgents", `${MAC_LAUNCH_AGENT_LABEL}.plist`);
|
|
92
|
+
}
|
|
93
|
+
function launchctlBin() {
|
|
94
|
+
return process.env.SUPERTASK_LAUNCHCTL_BIN ?? "launchctl";
|
|
95
|
+
}
|
|
96
|
+
function systemctlBin(env = process.env) {
|
|
97
|
+
return env.SUPERTASK_SYSTEMCTL_BIN ?? "systemctl";
|
|
98
|
+
}
|
|
99
|
+
function xmlUnescape(value) {
|
|
100
|
+
return value.replaceAll("'", "'").replaceAll(""", '"').replaceAll(">", ">").replaceAll("<", "<").replaceAll("&", "&");
|
|
101
|
+
}
|
|
102
|
+
function plistValue(contents, key) {
|
|
103
|
+
const escapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
104
|
+
const match = contents.match(new RegExp(`<key>\\s*${escapedKey}\\s*</key>\\s*<string>([^<]*)</string>`));
|
|
105
|
+
return match?.[1] ? xmlUnescape(match[1]) : null;
|
|
106
|
+
}
|
|
107
|
+
function isPm2Installed(env = process.env) {
|
|
108
|
+
const result = spawnSync(pm2Bin(env), ["--version"], {
|
|
109
|
+
stdio: "ignore",
|
|
110
|
+
env,
|
|
111
|
+
shell: process.platform === "win32",
|
|
112
|
+
timeout: pm2CommandTimeoutMs(env),
|
|
113
|
+
killSignal: "SIGKILL"
|
|
114
|
+
});
|
|
115
|
+
return result.status === 0;
|
|
116
|
+
}
|
|
117
|
+
function resolveCommand(command, env = process.env) {
|
|
118
|
+
const lookup = process.platform === "win32" ? "where" : "which";
|
|
119
|
+
const result = spawnSync(lookup, [command], { encoding: "utf8", env });
|
|
120
|
+
return result.status === 0 ? result.stdout.trim().split("\n")[0] || null : null;
|
|
121
|
+
}
|
|
122
|
+
function npmPreferredEnvironment(baseEnv = process.env) {
|
|
123
|
+
if (process.platform === "win32") return null;
|
|
124
|
+
const npm = resolveCommand("npm", baseEnv) ?? resolveCommand("npm", process.env);
|
|
125
|
+
const node = resolveCommand("node", baseEnv) ?? resolveCommand("node", process.env);
|
|
126
|
+
if (!npm || !node) return null;
|
|
127
|
+
let command;
|
|
128
|
+
try {
|
|
129
|
+
command = resolvePm2Bin(baseEnv);
|
|
130
|
+
} catch {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
const existingPathEntries = (baseEnv.PATH ?? "").split(delimiter).filter(Boolean);
|
|
134
|
+
const path = [.../* @__PURE__ */ new Set([
|
|
135
|
+
dirname(command),
|
|
136
|
+
dirname(npm),
|
|
137
|
+
dirname(node),
|
|
138
|
+
...existingPathEntries,
|
|
139
|
+
"/usr/local/bin",
|
|
140
|
+
"/usr/bin",
|
|
141
|
+
"/bin"
|
|
142
|
+
])].join(delimiter);
|
|
143
|
+
return { command, env: { ...baseEnv, PATH: path } };
|
|
144
|
+
}
|
|
145
|
+
function pm2Exec(args, options = {}) {
|
|
146
|
+
const baseEnv = options.env ?? process.env;
|
|
147
|
+
const npmEnvironment = options.preferNpm ? npmPreferredEnvironment(baseEnv) : null;
|
|
148
|
+
const effectiveEnv = npmEnvironment?.env ?? baseEnv;
|
|
149
|
+
const result = spawnSync(npmEnvironment?.command ?? pm2Bin(effectiveEnv), args, {
|
|
150
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
151
|
+
encoding: "utf-8",
|
|
152
|
+
env: effectiveEnv,
|
|
153
|
+
shell: process.platform === "win32",
|
|
154
|
+
timeout: options.timeoutMs ?? pm2CommandTimeoutMs(effectiveEnv),
|
|
155
|
+
killSignal: "SIGKILL"
|
|
156
|
+
});
|
|
157
|
+
const output = `${result.stdout ?? ""}${result.stderr ?? ""}`.trim();
|
|
158
|
+
if (result.error) return { ok: false, output: result.error.message };
|
|
159
|
+
return { ok: result.status === 0, output };
|
|
160
|
+
}
|
|
161
|
+
function requirePm2(args, action, options = {}) {
|
|
162
|
+
const result = pm2Exec(args, options);
|
|
163
|
+
if (!result.ok) throw new Error(`[supertask] ${action} failed: ${result.output || "unknown pm2 error"}`);
|
|
164
|
+
return result.output;
|
|
165
|
+
}
|
|
166
|
+
function pm2JsonList(env = process.env) {
|
|
167
|
+
const output = requirePm2(["jlist"], "pm2 jlist", { env });
|
|
168
|
+
try {
|
|
169
|
+
const parsed = JSON.parse(output);
|
|
170
|
+
if (!Array.isArray(parsed)) throw new Error("result is not an array");
|
|
171
|
+
return parsed;
|
|
172
|
+
} catch (error) {
|
|
173
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
174
|
+
throw new Error(`[supertask] Invalid pm2 jlist output: ${message}`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function gatewayEntryFromProcess(processInfo) {
|
|
178
|
+
const args = processInfo?.pm2_env?.args ?? processInfo?.args;
|
|
179
|
+
const candidates = Array.isArray(args) ? [...args].reverse() : typeof args === "string" ? [args] : [];
|
|
180
|
+
const savedCwd = processInfo?.pm2_env?.pm_cwd ?? processInfo?.pm_cwd;
|
|
181
|
+
for (const candidate of candidates) {
|
|
182
|
+
const path = typeof savedCwd === "string" ? runtimePath(candidate, savedCwd) : candidate;
|
|
183
|
+
if (existsSync(path)) return resolve(path);
|
|
184
|
+
}
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
function gatewayEnvironmentFromProcess(processInfo) {
|
|
188
|
+
const saved = processInfo?.pm2_env?.env ?? processInfo?.env;
|
|
189
|
+
if (!saved) return { ...process.env };
|
|
190
|
+
const env = {};
|
|
191
|
+
for (const [key, value] of Object.entries(saved)) {
|
|
192
|
+
if (typeof value === "string") env[key] = value;
|
|
193
|
+
}
|
|
194
|
+
return env;
|
|
195
|
+
}
|
|
196
|
+
function gatewayRuntimeFromProcess(processInfo) {
|
|
197
|
+
const gatewayEntry = gatewayEntryFromProcess(processInfo);
|
|
198
|
+
if (!gatewayEntry) return null;
|
|
199
|
+
const savedBunPath = processInfo?.pm2_env?.pm_exec_path ?? processInfo?.pm_exec_path;
|
|
200
|
+
const savedCwd = processInfo?.pm2_env?.pm_cwd ?? processInfo?.pm_cwd;
|
|
201
|
+
const savedEnv = gatewayEnvironmentFromProcess(processInfo);
|
|
202
|
+
if (typeof savedBunPath !== "string" || typeof savedCwd !== "string") return null;
|
|
203
|
+
try {
|
|
204
|
+
accessSync(savedBunPath, constants.X_OK);
|
|
205
|
+
if (!statSync(savedCwd).isDirectory()) return null;
|
|
206
|
+
if (spawnSync(savedBunPath, ["--version"], {
|
|
207
|
+
stdio: "ignore",
|
|
208
|
+
env: savedEnv,
|
|
209
|
+
timeout: pm2CommandTimeoutMs(savedEnv),
|
|
210
|
+
killSignal: "SIGKILL"
|
|
211
|
+
}).status !== 0) return null;
|
|
212
|
+
} catch {
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
const killTimeout = processInfo?.pm2_env?.kill_timeout ?? processInfo?.kill_timeout;
|
|
216
|
+
return {
|
|
217
|
+
gatewayEntry,
|
|
218
|
+
bunPath: savedBunPath,
|
|
219
|
+
cwd: resolve(savedCwd),
|
|
220
|
+
env: savedEnv,
|
|
221
|
+
killTimeoutMs: Number.isInteger(killTimeout) && killTimeout >= 5e3 ? killTimeout : void 0,
|
|
222
|
+
pm2KillTimeoutMs: Number.isInteger(killTimeout) ? killTimeout : PM2_DEFAULT_KILL_TIMEOUT_MS
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
function hasRestorableSavedGatewayRuntime(processInfo) {
|
|
226
|
+
return gatewayRuntimeFromProcess(processInfo) !== null;
|
|
227
|
+
}
|
|
228
|
+
function queryGatewayLock(path, query, fallback) {
|
|
229
|
+
if (!existsSync(path)) return fallback;
|
|
230
|
+
let database = null;
|
|
231
|
+
try {
|
|
232
|
+
database = new Database(path, { readonly: true });
|
|
233
|
+
return query(database);
|
|
234
|
+
} catch {
|
|
235
|
+
return fallback;
|
|
236
|
+
} finally {
|
|
237
|
+
database?.close();
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
function isGatewayReady(expectedPid, path = databasePath()) {
|
|
241
|
+
return queryGatewayLock(path, (database) => {
|
|
242
|
+
const row = database.query(
|
|
243
|
+
"SELECT pid, heartbeat_at, ready_at FROM gateway_lock WHERE id = 1"
|
|
244
|
+
).get();
|
|
245
|
+
if (!row || row.ready_at == null) return false;
|
|
246
|
+
if (expectedPid !== void 0 && row.pid !== expectedPid) return false;
|
|
247
|
+
const ageMs = Date.now() - row.heartbeat_at;
|
|
248
|
+
return ageMs >= -5e3 && ageMs < GATEWAY_LOCK_STALE_MS;
|
|
249
|
+
}, false);
|
|
250
|
+
}
|
|
251
|
+
function gatewayVersionFromLock(expectedPid, path) {
|
|
252
|
+
return queryGatewayLock(path, (database) => {
|
|
253
|
+
const row = database.query(
|
|
254
|
+
"SELECT pid, version FROM gateway_lock WHERE id = 1"
|
|
255
|
+
).get();
|
|
256
|
+
if (!row || row.pid !== expectedPid) return void 0;
|
|
257
|
+
return row.version;
|
|
258
|
+
}, void 0);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// src/daemon/pm2-scope.ts
|
|
262
|
+
import { spawnSync as spawnSync2 } from "child_process";
|
|
263
|
+
import { join as join3, resolve as resolve2 } from "path";
|
|
264
|
+
import { Database as Database3 } from "bun:sqlite";
|
|
265
|
+
|
|
266
|
+
// src/gateway/config.ts
|
|
267
|
+
import { homedir as homedir2 } from "os";
|
|
268
|
+
import { join as join2 } from "path";
|
|
269
|
+
var DEFAULT_CONFIG_PATH = join2(homedir2(), ".config/opencode/supertask.json");
|
|
270
|
+
|
|
271
|
+
// src/daemon/management-lock.ts
|
|
272
|
+
import { Database as Database2 } from "bun:sqlite";
|
|
273
|
+
|
|
274
|
+
// src/daemon/pm2-scope.ts
|
|
275
|
+
function canonicalManagementLockPath(env = process.env, cwd = process.cwd()) {
|
|
276
|
+
const home = runtimeHome(env);
|
|
277
|
+
const pm2Home = env.PM2_HOME ? runtimePath(env.PM2_HOME, cwd) : join3(home, ".pm2");
|
|
278
|
+
return join3(pm2Home, "supertask-gateway.manage.sqlite");
|
|
279
|
+
}
|
|
280
|
+
function managementLockPath(env = process.env, cwd = process.cwd()) {
|
|
281
|
+
const override = env.SUPERTASK_PM2_MANAGEMENT_LOCK;
|
|
282
|
+
if (override) return runtimePath(override, cwd);
|
|
283
|
+
return join3(runtimeScope({ env, cwd }).pm2Home, "supertask-gateway.manage.sqlite");
|
|
284
|
+
}
|
|
285
|
+
function runtimeScope(runtime) {
|
|
286
|
+
const { cwd, env } = runtime;
|
|
287
|
+
const home = runtimeHome(env);
|
|
288
|
+
return {
|
|
289
|
+
cwd: resolve2(cwd),
|
|
290
|
+
databasePath: env.SUPERTASK_DB_PATH ? runtimePath(env.SUPERTASK_DB_PATH, cwd) : join3(home, ".local/share/opencode/tasks.db"),
|
|
291
|
+
configPath: env.SUPERTASK_CONFIG_PATH ? runtimePath(env.SUPERTASK_CONFIG_PATH, cwd) : join3(home, ".config/opencode/supertask.json"),
|
|
292
|
+
opencodePath: resolveRuntimeExecutable(env.SUPERTASK_OPENCODE_BIN ?? "opencode", env, cwd),
|
|
293
|
+
home,
|
|
294
|
+
pm2Home: env.PM2_HOME ? runtimePath(env.PM2_HOME, cwd) : join3(home, ".pm2"),
|
|
295
|
+
managementLockPath: canonicalManagementLockPath(env, cwd)
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
function scopesMatch(left, right) {
|
|
299
|
+
return left.databasePath === right.databasePath && left.configPath === right.configPath && left.opencodePath === right.opencodePath && left.home === right.home && left.pm2Home === right.pm2Home && left.managementLockPath === right.managementLockPath;
|
|
300
|
+
}
|
|
301
|
+
function currentScope() {
|
|
302
|
+
return runtimeScope({ cwd: process.cwd(), env: process.env });
|
|
303
|
+
}
|
|
304
|
+
function diagnoseOpenCodeRuntime(runtime = {
|
|
305
|
+
cwd: process.cwd(),
|
|
306
|
+
env: process.env
|
|
307
|
+
}) {
|
|
308
|
+
const executable = runtimeScope(runtime).opencodePath;
|
|
309
|
+
const result = spawnSync2(executable, ["--version"], {
|
|
310
|
+
cwd: runtime.cwd,
|
|
311
|
+
env: runtime.env,
|
|
312
|
+
encoding: "utf8",
|
|
313
|
+
timeout: 1e4,
|
|
314
|
+
killSignal: "SIGKILL"
|
|
315
|
+
});
|
|
316
|
+
const ok = result.status === 0 && result.error === void 0;
|
|
317
|
+
return {
|
|
318
|
+
ok,
|
|
319
|
+
executable,
|
|
320
|
+
version: ok ? result.stdout.trim() || result.stderr.trim() || null : null,
|
|
321
|
+
error: ok ? null : result.error?.message || result.stderr.trim() || result.stdout.trim() || `exit code ${result.status}`
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// src/daemon/pm2-startup.ts
|
|
326
|
+
import { spawnSync as spawnSync3 } from "child_process";
|
|
327
|
+
import {
|
|
328
|
+
accessSync as accessSync2,
|
|
329
|
+
chmodSync,
|
|
330
|
+
constants as constants2,
|
|
331
|
+
existsSync as existsSync2,
|
|
332
|
+
mkdirSync as mkdirSync2,
|
|
333
|
+
readFileSync as readFileSync2,
|
|
334
|
+
rmSync,
|
|
335
|
+
writeFileSync as writeFileSync2
|
|
336
|
+
} from "fs";
|
|
337
|
+
import { userInfo } from "os";
|
|
338
|
+
import { dirname as dirname2, join as join4, resolve as resolve3 } from "path";
|
|
339
|
+
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
340
|
+
var __dirname2 = dirname2(fileURLToPath2(import.meta.url));
|
|
341
|
+
function readRestorableDumpGatewayRuntime(pm2Home) {
|
|
342
|
+
const dump = JSON.parse(readFileSync2(join4(pm2Home, "dump.pm2"), "utf8"));
|
|
343
|
+
if (!Array.isArray(dump)) return null;
|
|
344
|
+
const gateway = dump.find((item) => item.name === PROCESS_NAME);
|
|
345
|
+
const runtime = gatewayRuntimeFromProcess(gateway);
|
|
346
|
+
return runtime && hasRestorableSavedGatewayRuntime(gateway) ? runtime : null;
|
|
347
|
+
}
|
|
348
|
+
function isMacLaunchAgentConfigured(expectedPm2Home, expectedRuntime) {
|
|
349
|
+
if (typeof process.getuid !== "function") return false;
|
|
350
|
+
const plistPath = launchAgentPath();
|
|
351
|
+
if (!existsSync2(plistPath)) return false;
|
|
352
|
+
try {
|
|
353
|
+
const contents = readFileSync2(plistPath, "utf8");
|
|
354
|
+
const argumentsBlock = contents.match(
|
|
355
|
+
/<key>\s*ProgramArguments\s*<\/key>\s*<array>([\s\S]*?)<\/array>/
|
|
356
|
+
)?.[1];
|
|
357
|
+
const programArguments = argumentsBlock ? [...argumentsBlock.matchAll(/<string>([^<]*)<\/string>/g)].map((match) => xmlUnescape(match[1])) : [];
|
|
358
|
+
const bunPath = programArguments[0];
|
|
359
|
+
const supervisorEntry = programArguments[1];
|
|
360
|
+
const pm2Path = programArguments[2];
|
|
361
|
+
const pm2Home = plistValue(contents, "PM2_HOME");
|
|
362
|
+
const managementLock = plistValue(contents, "SUPERTASK_PM2_MANAGEMENT_LOCK");
|
|
363
|
+
if (!bunPath || !supervisorEntry || !pm2Path || !pm2Home || !managementLock) return false;
|
|
364
|
+
if (expectedPm2Home && resolve3(pm2Home) !== resolve3(expectedPm2Home)) return false;
|
|
365
|
+
const expectedManagementLock = expectedRuntime ? managementLockPath(expectedRuntime.env, expectedRuntime.cwd) : process.env.SUPERTASK_PM2_MANAGEMENT_LOCK ? managementLockPath() : join4(expectedPm2Home ?? currentScope().pm2Home, "supertask-gateway.manage.sqlite");
|
|
366
|
+
if (managementLock !== expectedManagementLock) return false;
|
|
367
|
+
accessSync2(bunPath, constants2.X_OK);
|
|
368
|
+
accessSync2(supervisorEntry, constants2.R_OK);
|
|
369
|
+
accessSync2(pm2Path, constants2.X_OK);
|
|
370
|
+
if (spawnSync3(bunPath, ["--version"], {
|
|
371
|
+
stdio: "ignore",
|
|
372
|
+
timeout: pm2CommandTimeoutMs(),
|
|
373
|
+
killSignal: "SIGKILL"
|
|
374
|
+
}).status !== 0) return false;
|
|
375
|
+
if (spawnSync3(pm2Path, ["--version"], {
|
|
376
|
+
stdio: "ignore",
|
|
377
|
+
env: { ...process.env, PM2_HOME: pm2Home },
|
|
378
|
+
timeout: pm2CommandTimeoutMs(),
|
|
379
|
+
killSignal: "SIGKILL"
|
|
380
|
+
}).status !== 0) return false;
|
|
381
|
+
const loaded = spawnSync3(
|
|
382
|
+
launchctlBin(),
|
|
383
|
+
["print", `gui/${process.getuid()}/${MAC_LAUNCH_AGENT_LABEL}`],
|
|
384
|
+
{
|
|
385
|
+
encoding: "utf8",
|
|
386
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
387
|
+
timeout: pm2CommandTimeoutMs(),
|
|
388
|
+
killSignal: "SIGKILL"
|
|
389
|
+
}
|
|
390
|
+
);
|
|
391
|
+
if (loaded.status !== 0) return false;
|
|
392
|
+
if (!loaded.stdout.includes("state = running")) return false;
|
|
393
|
+
if (!loaded.stdout.includes(`path = ${plistPath}`)) return false;
|
|
394
|
+
if (!loaded.stdout.includes(`program = ${bunPath}`)) return false;
|
|
395
|
+
const dumpRuntime = readRestorableDumpGatewayRuntime(pm2Home);
|
|
396
|
+
if (!dumpRuntime) return false;
|
|
397
|
+
return expectedRuntime === void 0 || dumpRuntime.gatewayEntry === expectedRuntime.gatewayEntry && dumpRuntime.bunPath === expectedRuntime.bunPath && dumpRuntime.cwd === expectedRuntime.cwd && scopesMatch(runtimeScope(dumpRuntime), runtimeScope(expectedRuntime));
|
|
398
|
+
} catch {
|
|
399
|
+
return false;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
function systemdUnitMatchesPm2Home(unitContent, expectedPm2Home) {
|
|
403
|
+
const normalizedExpected = resolve3(expectedPm2Home);
|
|
404
|
+
const envAssignPattern = /(?<![A-Za-z0-9_])PM2_HOME=(?:"([^"\r\n]+)"|'([^'\r\n]+)'|([^\s"']+))/g;
|
|
405
|
+
let match;
|
|
406
|
+
while ((match = envAssignPattern.exec(unitContent)) !== null) {
|
|
407
|
+
const rawValue = (match[1] ?? match[2] ?? match[3] ?? "").trim();
|
|
408
|
+
if (rawValue && resolve3(rawValue) === normalizedExpected) {
|
|
409
|
+
return true;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return false;
|
|
413
|
+
}
|
|
414
|
+
function isLinuxStartupConfigured(env = process.env, cwd = process.cwd(), expectedRuntime) {
|
|
415
|
+
const unit = env.SUPERTASK_PM2_SYSTEMD_UNIT ?? `pm2-${userInfo().username}.service`;
|
|
416
|
+
const enabled = spawnSync3(systemctlBin(env), ["is-enabled", unit], {
|
|
417
|
+
encoding: "utf8",
|
|
418
|
+
env,
|
|
419
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
420
|
+
timeout: pm2CommandTimeoutMs(env),
|
|
421
|
+
killSignal: "SIGKILL"
|
|
422
|
+
});
|
|
423
|
+
if (enabled.status !== 0 || enabled.stdout.trim() !== "enabled") return false;
|
|
424
|
+
const contents = spawnSync3(systemctlBin(env), ["cat", unit], {
|
|
425
|
+
encoding: "utf8",
|
|
426
|
+
env,
|
|
427
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
428
|
+
timeout: pm2CommandTimeoutMs(env),
|
|
429
|
+
killSignal: "SIGKILL"
|
|
430
|
+
});
|
|
431
|
+
if (contents.status !== 0) return false;
|
|
432
|
+
const expectedPm2Home = runtimeScope({ cwd, env }).pm2Home;
|
|
433
|
+
if (!contents.stdout.includes("resurrect") || !systemdUnitMatchesPm2Home(contents.stdout, expectedPm2Home)) {
|
|
434
|
+
return false;
|
|
435
|
+
}
|
|
436
|
+
try {
|
|
437
|
+
const runtime = readRestorableDumpGatewayRuntime(expectedPm2Home);
|
|
438
|
+
if (!runtime) return false;
|
|
439
|
+
return runtime.cwd === resolve3(cwd) && scopesMatch(runtimeScope(runtime), runtimeScope({ cwd, env })) && (expectedRuntime === void 0 || runtime.gatewayEntry === expectedRuntime.gatewayEntry && runtime.bunPath === expectedRuntime.bunPath);
|
|
440
|
+
} catch {
|
|
441
|
+
return false;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// src/daemon/pm2-lifecycle.ts
|
|
446
|
+
function getGatewayDiagnostic(options = {}) {
|
|
447
|
+
const producerScope = currentScope();
|
|
448
|
+
if (!isPm2Installed()) {
|
|
449
|
+
return {
|
|
450
|
+
pm2Installed: false,
|
|
451
|
+
processFound: false,
|
|
452
|
+
status: null,
|
|
453
|
+
pid: null,
|
|
454
|
+
ready: false,
|
|
455
|
+
runningVersion: getRunningVersion(),
|
|
456
|
+
gatewayEntry: null,
|
|
457
|
+
gatewayPackageVersion: null,
|
|
458
|
+
logRotationInstalled: false,
|
|
459
|
+
startupConfigured: process.platform === "darwin" || process.platform === "linux" ? false : null,
|
|
460
|
+
currentScope: producerScope,
|
|
461
|
+
gatewayScope: null,
|
|
462
|
+
scopeMatches: false,
|
|
463
|
+
gatewayOpenCode: null
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
const processes = pm2JsonList();
|
|
467
|
+
const gateway = processes.find((item) => item.name === PROCESS_NAME);
|
|
468
|
+
const runtime = gatewayRuntimeFromProcess(gateway);
|
|
469
|
+
const gatewayEnv = runtime?.env ?? process.env;
|
|
470
|
+
const managedScope = runtime ? runtimeScope(runtime) : null;
|
|
471
|
+
const pid = typeof gateway?.pid === "number" ? gateway.pid : null;
|
|
472
|
+
const readyPath = managedScope?.databasePath ?? databasePath();
|
|
473
|
+
const lockedVersion = pid == null ? void 0 : gatewayVersionFromLock(pid, readyPath);
|
|
474
|
+
const gatewayEntry = runtime?.gatewayEntry ?? null;
|
|
475
|
+
return {
|
|
476
|
+
pm2Installed: true,
|
|
477
|
+
processFound: gateway != null,
|
|
478
|
+
status: gateway?.pm2_env?.status ?? null,
|
|
479
|
+
pid,
|
|
480
|
+
ready: pid != null && isGatewayReady(pid, readyPath),
|
|
481
|
+
runningVersion: lockedVersion === void 0 ? getRunningVersion(gatewayEnv, runtime?.cwd) : lockedVersion,
|
|
482
|
+
gatewayEntry,
|
|
483
|
+
gatewayPackageVersion: packageVersionFromGatewayEntry(gatewayEntry),
|
|
484
|
+
logRotationInstalled: processes.some((item) => item.name === "pm2-logrotate" && item.pm2_env?.status === "online"),
|
|
485
|
+
startupConfigured: process.platform === "darwin" ? isMacLaunchAgentConfigured(
|
|
486
|
+
managedScope?.pm2Home ?? producerScope.pm2Home,
|
|
487
|
+
runtime ?? void 0
|
|
488
|
+
) : process.platform === "linux" ? isLinuxStartupConfigured(gatewayEnv, runtime?.cwd, runtime ?? void 0) : null,
|
|
489
|
+
currentScope: producerScope,
|
|
490
|
+
gatewayScope: managedScope,
|
|
491
|
+
scopeMatches: managedScope != null && scopesMatch(producerScope, managedScope),
|
|
492
|
+
gatewayOpenCode: runtime === null || !options.probeOpenCode ? null : diagnoseOpenCodeRuntime(runtime)
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// src/daemon/gateway-diagnostic-runner.ts
|
|
497
|
+
try {
|
|
498
|
+
console.log(JSON.stringify(getGatewayDiagnostic()));
|
|
499
|
+
} catch (error) {
|
|
500
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
501
|
+
process.exitCode = 1;
|
|
502
|
+
}
|
|
503
|
+
//# sourceMappingURL=gateway-diagnostic-runner.js.map
|