@openchamber/web 1.21.0 → 1.22.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/bin/cli.test.js +35 -0
- package/bin/lib/DOCUMENTATION.md +12 -0
- package/bin/lib/cli-settings-accessors.js +111 -0
- package/bin/lib/cli-settings-accessors.test.js +189 -0
- package/bin/lib/cli-startup.js +29 -9
- package/bin/lib/commands-connect-url.js +12 -14
- package/dist/assets/{CodeMirrorCommentWidgets-C2BopIs3.js → CodeMirrorCommentWidgets-CPvfxh0N.js} +1 -1
- package/dist/assets/CommitSection-CC5mFzO2.js +1 -0
- package/dist/assets/DiffView-6xGdyI0n.js +4 -0
- package/dist/assets/FilesView-Bm3vT63N.js +17 -0
- package/dist/assets/GitView-B6y1Y69c.js +6 -0
- package/dist/assets/{JsonTreeView-osju7J70.js → JsonTreeView-rwHz84MX.js} +1 -1
- package/dist/assets/LinearIssuesView-B0UM_Rkx.js +1 -0
- package/dist/assets/MarkdownImageGallery-sGilV9Hs.js +1 -0
- package/dist/assets/MarkdownRendererImpl--4sRsKBn.js +12 -0
- package/dist/assets/OnboardingScreen-CECL_Cms.js +1 -0
- package/dist/assets/{PatchDiff-Dan3mADn.js → PatchDiff-BGPl1s-U.js} +1 -1
- package/dist/assets/{PierreDiffViewer-CGoI2v0q.js → PierreDiffViewer-Cvz9Z-io.js} +2 -2
- package/dist/assets/PlanView-BFMsnHk3.js +5 -0
- package/dist/assets/SettingsView-_Gtn5SdT.js +28 -0
- package/dist/assets/{SettingsWindow-DXF-MTHG.js → SettingsWindow-C1Qa09YO.js} +1 -1
- package/dist/assets/{ToolOutputDialog-DrCa1JRk.js → ToolOutputDialog-DzQlzMF4.js} +6 -6
- package/dist/assets/{ToolPartDiffPreview-B_tfvlNF.js → ToolPartDiffPreview-DfVOfwHy.js} +2 -2
- package/dist/assets/{VirtualizedFileDiff-CN_BctrR.js → VirtualizedFileDiff-DT5MDv-u.js} +1 -1
- package/dist/assets/WalkthroughView-D8sLH_M8.js +4 -0
- package/dist/assets/{computeVirtualFileMetrics-Gi1d5eU1.js → computeVirtualFileMetrics-DmpLWhll.js} +1 -1
- package/dist/assets/{contextPanelEmbeddedChat-CpzVNWAv.js → contextPanelEmbeddedChat-BK78t3M_.js} +1 -1
- package/dist/assets/de-C3luvSd_.js +9 -0
- package/dist/assets/es-CmU4HLv9.js +15 -0
- package/dist/assets/fr-Bgyni40l.js +13 -0
- package/dist/assets/index-C57jT0Sa.css +1 -0
- package/dist/assets/{index-Brm3X9rs.js → index-CWYs7sMK.js} +1 -1
- package/dist/assets/index-b0zDkWyg.js +7 -0
- package/dist/assets/{isDiffPlainText-pH9kAJQH.js → isDiffPlainText-BO_q5ezD.js} +1 -1
- package/dist/assets/{isFilePlainText-CVKy_dgB.js → isFilePlainText-D8qPqUAf.js} +1 -1
- package/dist/assets/ja-C1ImKinT.js +15 -0
- package/dist/assets/ko-CP7OdxYH.js +15 -0
- package/dist/assets/main-D4rd8Hy5.js +928 -0
- package/dist/assets/{main-vUCh0VB0.js → main-D7fwjW2z.js} +2 -2
- package/dist/assets/{markdown-shiki.worker-CKGJ1J33.js → markdown-shiki.worker-D5fBU-lw.js} +5 -5
- package/dist/assets/markdownCore-BcmkWZPO.js +3 -0
- package/dist/assets/{miniChat-BlzGEYgE.js → miniChat-D5rraz-g.js} +2 -2
- package/dist/assets/{mobile-CkX6H-IT.js → mobile-Cg_DE3KL.js} +2 -2
- package/dist/assets/{patchFileDiff-BCTsE43U.js → patchFileDiff-Bpql0QGw.js} +3 -3
- package/dist/assets/pl-DQYSiGYX.js +15 -0
- package/dist/assets/pt-BR-DytpsW4M.js +15 -0
- package/dist/assets/{renderElectronMiniChatApp-CJYe_KOL.js → renderElectronMiniChatApp-B_Bn7BbN.js} +2 -2
- package/dist/assets/renderMobileApp-DLfxMzGJ.js +25 -0
- package/dist/assets/runtimeEndpointReset-HlRmXFGW.js +16 -0
- package/dist/assets/scheduledTasksApi-BPvxrynl.js +1 -0
- package/dist/assets/{shikiHighlight-BgTPSITe.js → shikiHighlight-CFAVs_lu.js} +1 -1
- package/dist/assets/tr-aizaUvft.js +6 -0
- package/dist/assets/uk-DwnRxJZd.js +15 -0
- package/dist/assets/useAppFontEffects-DWXGRUPZ.js +7310 -0
- package/dist/assets/{useInlineCommentController-DFFv0hae.js → useInlineCommentController-Cc0OCXr0.js} +2 -2
- package/dist/assets/useRootScrollLock-BFZtfciC.js +1 -0
- package/dist/assets/{vendor-base-ui-_6j2xOWl.js → vendor-base-ui-BeMD8X_w.js} +1 -1
- package/dist/assets/{vendor-codemirror-language-data-DkQX2EBa.js → vendor-codemirror-language-data-BGFszm8n.js} +2 -2
- package/dist/assets/{vendor-floating-ui-core-CXmKXOXc.js → vendor-floating-ui-core-PuGzNPnN.js} +1 -1
- package/dist/assets/{vendor-floating-ui-dom-WyXykhNi.js → vendor-floating-ui-dom-BTvzWks8.js} +1 -1
- package/dist/assets/{vendor-floating-ui-react-dom-DKpLs02S.js → vendor-floating-ui-react-dom-DxYCWdHj.js} +1 -1
- package/dist/assets/{vendor-floating-ui-utils-BzXdqYpP.js → vendor-floating-ui-utils-D6FqdtJt.js} +1 -1
- package/dist/assets/vendor-linkify-it-DZCzzDkK.js +1 -0
- package/dist/assets/vendor-marked-linkify-it-CXI_LHIE.js +1 -0
- package/dist/assets/vendor-uc.micro-BAd0H0z4.js +1 -0
- package/dist/assets/{web-components-DLnGY4fs.js → web-components-CD-GFayc.js} +4 -4
- package/dist/assets/zh-CN-UDNMxm7r.js +15 -0
- package/dist/assets/zh-TW-BsswdYL-.js +15 -0
- package/dist/index.html +5 -5
- package/dist/mini-chat.html +3 -3
- package/dist/mobile.html +3 -3
- package/dist/site.webmanifest +1 -1
- package/dist/sw.js +1 -1
- package/package.json +2 -2
- package/public/site.webmanifest +1 -1
- package/server/index.js +6 -2
- package/server/lib/cloudflare-tunnel.js +1 -1
- package/server/lib/dictation/DOCUMENTATION.md +19 -6
- package/server/lib/dictation/local/model-catalog.js +220 -0
- package/server/lib/dictation/local/model-catalog.test.js +42 -0
- package/server/lib/dictation/local/sherpa-tts.js +41 -20
- package/server/lib/dictation/local/worker-process.js +2 -0
- package/server/lib/dictation/runtime.js +4 -0
- package/server/lib/dictation/service.js +27 -4
- package/server/lib/fs/DOCUMENTATION.md +5 -0
- package/server/lib/fs/routes.js +150 -0
- package/server/lib/fs/routes.test.js +308 -3
- package/server/lib/git/DOCUMENTATION.md +4 -3
- package/server/lib/git/service.js +128 -6
- package/server/lib/git/service.test.js +191 -1
- package/server/lib/linear/DOCUMENTATION.md +97 -0
- package/server/lib/linear/OAUTH-BROKER-HANDOFF.md +91 -0
- package/server/lib/linear/auth.js +436 -0
- package/server/lib/linear/auth.test.js +242 -0
- package/server/lib/linear/client.js +191 -0
- package/server/lib/linear/index.js +61 -0
- package/server/lib/linear/issues.js +499 -0
- package/server/lib/linear/issues.test.js +512 -0
- package/server/lib/linear/mapping.js +188 -0
- package/server/lib/linear/mapping.test.js +147 -0
- package/server/lib/linear/oauth.js +345 -0
- package/server/lib/linear/oauth.test.js +166 -0
- package/server/lib/linear/parse.js +23 -0
- package/server/lib/linear/routes.js +432 -0
- package/server/lib/linear/routes.test.js +661 -0
- package/server/lib/linear/status-runtime.js +64 -0
- package/server/lib/linear/status-runtime.test.js +167 -0
- package/server/lib/linear/status.js +280 -0
- package/server/lib/linear/status.test.js +271 -0
- package/server/lib/linear/teams.js +72 -0
- package/server/lib/linear/teams.test.js +94 -0
- package/server/lib/markdown-image-grants/DOCUMENTATION.md +4 -0
- package/server/lib/markdown-image-grants/routes.js +3 -1
- package/server/lib/markdown-image-grants/routes.test.js +14 -0
- package/server/lib/openchamber-sessions/routes.js +4 -1
- package/server/lib/openchamber-sessions/routes.test.js +23 -0
- package/server/lib/opencode/env-runtime.js +17 -1
- package/server/lib/opencode/env-runtime.test.js +56 -0
- package/server/lib/opencode/feature-routes-runtime.js +2 -0
- package/server/lib/opencode/lifecycle.js +48 -9
- package/server/lib/opencode/lifecycle.test.js +125 -1
- package/server/lib/opencode/managed-process-registry.d.ts +13 -0
- package/server/lib/opencode/managed-process-registry.js +213 -163
- package/server/lib/opencode/managed-process-registry.test.mjs +285 -0
- package/server/lib/opencode/project-directory-runtime.js +16 -11
- package/server/lib/opencode/project-directory-runtime.test.js +32 -12
- package/server/lib/opencode/pwa-manifest-routes.js +1 -1
- package/server/lib/opencode/routes-upgrade.test.js +96 -12
- package/server/lib/opencode/routes.js +53 -5
- package/server/lib/opencode/settings-helpers.js +3 -0
- package/server/lib/opencode/settings-helpers.test.js +10 -0
- package/server/lib/opencode/static-routes-runtime.js +3 -3
- package/server/lib/opencode/theme-runtime.js +1 -1
- package/server/lib/opencode/theme-runtime.test.js +129 -0
- package/server/lib/projects/project-config.js +33 -5
- package/server/lib/projects/project-config.test.js +85 -0
- package/server/lib/quota/DOCUMENTATION.md +19 -0
- package/server/lib/quota/providers/copilot.js +26 -10
- package/server/lib/quota/providers/copilot.test.js +78 -0
- package/server/lib/scheduled-tasks/DOCUMENTATION.md +7 -0
- package/server/lib/small-model/DOCUMENTATION.md +11 -5
- package/server/lib/small-model/call.js +64 -20
- package/server/lib/small-model/call.test.js +184 -2
- package/server/lib/tts/DOCUMENTATION.md +1 -0
- package/server/lib/tts/language-detect.js +210 -0
- package/server/lib/tts/language-detect.test.js +76 -0
- package/server/lib/tts/routes.js +23 -1
- package/server/lib/tts/routes.test.js +26 -0
- package/server/lib/tunnels/install-help.js +2 -2
- package/server/lib/tunnels/install-help.test.js +9 -0
- package/server/lib/walkthrough/languages.js +1 -0
- package/dist/assets/CommitSection-HJeMUAWb.js +0 -1
- package/dist/assets/DiffView-D5cvbEPb.js +0 -4
- package/dist/assets/FilesView-DPU8aNPL.js +0 -17
- package/dist/assets/GitView-Ct6xWrVH.js +0 -6
- package/dist/assets/MarkdownImageGallery-EujHSZ2M.js +0 -1
- package/dist/assets/MarkdownRendererImpl-CPzu4kIr.js +0 -12
- package/dist/assets/OnboardingScreen-w2VG4fWY.js +0 -1
- package/dist/assets/PlanView-Dgg-WTEc.js +0 -4
- package/dist/assets/SessionSwitcherDropdown-CJTrc8K7.js +0 -1
- package/dist/assets/SettingsView-D9yRuFZw.js +0 -28
- package/dist/assets/WalkthroughView-D9Q4lr5x.js +0 -4
- package/dist/assets/de-B_LdJwEM.js +0 -9
- package/dist/assets/es-CubfsAYV.js +0 -15
- package/dist/assets/fr-5qJM0nFS.js +0 -13
- package/dist/assets/index-BW82eK5v.css +0 -1
- package/dist/assets/index-rlLjZUM5.js +0 -7
- package/dist/assets/ja-DJYIBUlH.js +0 -15
- package/dist/assets/ko-DcUBkB7y.js +0 -15
- package/dist/assets/main-DLBxKIoX.js +0 -926
- package/dist/assets/markdownCore-DG6GHLF4.js +0 -3
- package/dist/assets/pl-rB6BLcio.js +0 -15
- package/dist/assets/pt-BR-DcwtlO-o.js +0 -15
- package/dist/assets/renderMobileApp-hQJTnr8B.js +0 -25
- package/dist/assets/runtimeEndpointReset-BDh7r8BL.js +0 -15
- package/dist/assets/scheduledTasksApi-CAGPh_gZ.js +0 -1
- package/dist/assets/uk-INZlFD9A.js +0 -15
- package/dist/assets/useAppFontEffects-Cll4gnsh.js +0 -7268
- package/dist/assets/zh-CN-Dzda8xa1.js +0 -15
- package/dist/assets/zh-TW-Cwn3rNYZ.js +0 -15
|
@@ -32,13 +32,26 @@
|
|
|
32
32
|
// been reparented to init/pid 1, or the recorded owner pid is dead. A
|
|
33
33
|
// child still owned by a live instance is left untouched.
|
|
34
34
|
//
|
|
35
|
+
// All filesystem and child-process operations here are ASYNCHRONOUS. The web
|
|
36
|
+
// server runs in-process inside the Electron main event loop (and other hosts),
|
|
37
|
+
// so any `spawnSync`/`*Sync` FS call blocks the single event loop — which also
|
|
38
|
+
// serves UI asset requests and realtime SSE traffic. The startup reaper can
|
|
39
|
+
// iterate several registry entries and, on Windows, each one spawns `tasklist`
|
|
40
|
+
// (100-500ms) and possibly `taskkill`; doing that synchronously stalls the
|
|
41
|
+
// whole process and is what caused the 1.13.3 `openchamber-ui://` lag
|
|
42
|
+
// regression (#1841). `execFile`/`fsp.*` keep the event loop responsive while
|
|
43
|
+
// the reaper waits on the kernel.
|
|
44
|
+
//
|
|
35
45
|
// The VS Code extension cannot import this module (it does not bundle the web
|
|
36
46
|
// package); it carries a parity implementation that reads/writes the SAME dir.
|
|
37
47
|
|
|
38
|
-
import
|
|
48
|
+
import fsp from 'node:fs/promises';
|
|
39
49
|
import os from 'node:os';
|
|
40
50
|
import path from 'node:path';
|
|
41
|
-
import {
|
|
51
|
+
import { execFile } from 'node:child_process';
|
|
52
|
+
import { promisify } from 'node:util';
|
|
53
|
+
|
|
54
|
+
const defaultExecFileAsync = promisify(execFile);
|
|
42
55
|
|
|
43
56
|
const resolveRegistryDir = () => {
|
|
44
57
|
const override = process.env.OPENCHAMBER_MANAGED_PROCESS_REGISTRY;
|
|
@@ -48,67 +61,6 @@ const resolveRegistryDir = () => {
|
|
|
48
61
|
|
|
49
62
|
const entryFilePath = (pid) => path.join(resolveRegistryDir(), `${pid}.json`);
|
|
50
63
|
|
|
51
|
-
const writeEntryFile = (entry) => {
|
|
52
|
-
const dir = resolveRegistryDir();
|
|
53
|
-
try {
|
|
54
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
55
|
-
const filePath = path.join(dir, `${entry.pid}.json`);
|
|
56
|
-
const tmp = `${filePath}.tmp-${process.pid}`;
|
|
57
|
-
fs.writeFileSync(tmp, JSON.stringify(entry, null, 2));
|
|
58
|
-
fs.renameSync(tmp, filePath);
|
|
59
|
-
} catch {
|
|
60
|
-
// Best-effort: a failed registry write must never break spawn/shutdown.
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const readAllEntries = () => {
|
|
65
|
-
const dir = resolveRegistryDir();
|
|
66
|
-
let names = [];
|
|
67
|
-
try {
|
|
68
|
-
names = fs.readdirSync(dir).filter((name) => name.endsWith('.json'));
|
|
69
|
-
} catch {
|
|
70
|
-
return [];
|
|
71
|
-
}
|
|
72
|
-
const out = [];
|
|
73
|
-
for (const name of names) {
|
|
74
|
-
const filePath = path.join(dir, name);
|
|
75
|
-
try {
|
|
76
|
-
const entry = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
77
|
-
if (entry && Number.isInteger(entry.pid)) {
|
|
78
|
-
out.push({ entry, filePath });
|
|
79
|
-
} else {
|
|
80
|
-
fs.rmSync(filePath, { force: true });
|
|
81
|
-
}
|
|
82
|
-
} catch {
|
|
83
|
-
// Corrupt/partial file — drop it.
|
|
84
|
-
try { fs.rmSync(filePath, { force: true }); } catch {}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return out;
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
/** Record an OpenCode process WE spawned so a future run can reap it if orphaned. */
|
|
91
|
-
export const registerManagedProcess = ({ pid, ownerPid, port, binary, runtime } = {}) => {
|
|
92
|
-
if (!Number.isInteger(pid)) return;
|
|
93
|
-
writeEntryFile({
|
|
94
|
-
pid,
|
|
95
|
-
ownerPid: Number.isInteger(ownerPid) ? ownerPid : process.pid,
|
|
96
|
-
port: Number.isInteger(port) ? port : null,
|
|
97
|
-
binary: typeof binary === 'string' ? binary : null,
|
|
98
|
-
runtime: typeof runtime === 'string' ? runtime : 'web',
|
|
99
|
-
startedAt: new Date().toISOString(),
|
|
100
|
-
});
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
/** Drop a pid from the registry (after we have killed/closed it ourselves). */
|
|
104
|
-
export const unregisterManagedProcess = (pid) => {
|
|
105
|
-
if (!Number.isInteger(pid)) return;
|
|
106
|
-
try {
|
|
107
|
-
fs.rmSync(entryFilePath(pid), { force: true });
|
|
108
|
-
} catch {
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
|
|
112
64
|
const isPidAlive = (pid) => {
|
|
113
65
|
if (!Number.isInteger(pid)) return false;
|
|
114
66
|
try {
|
|
@@ -122,38 +74,6 @@ const isPidAlive = (pid) => {
|
|
|
122
74
|
|
|
123
75
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
124
76
|
|
|
125
|
-
// Returns { ppid, command } for a live pid on Unix, or null if it can't be read.
|
|
126
|
-
const readUnixProcInfo = (pid) => {
|
|
127
|
-
try {
|
|
128
|
-
const result = spawnSync('ps', ['-p', String(pid), '-o', 'ppid=,command='], {
|
|
129
|
-
encoding: 'utf8',
|
|
130
|
-
timeout: 3000,
|
|
131
|
-
windowsHide: true,
|
|
132
|
-
});
|
|
133
|
-
const line = (result.stdout || '').trim();
|
|
134
|
-
if (!line) return null;
|
|
135
|
-
const match = line.match(/^\s*(\d+)\s+(.*)$/);
|
|
136
|
-
if (!match) return null;
|
|
137
|
-
return { ppid: Number.parseInt(match[1], 10), command: match[2] };
|
|
138
|
-
} catch {
|
|
139
|
-
return null;
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
// Windows image name for a pid (e.g. "opencode.exe"), or null.
|
|
144
|
-
const readWindowsImageName = (pid) => {
|
|
145
|
-
try {
|
|
146
|
-
const result = spawnSync('tasklist', ['/FI', `PID eq ${pid}`, '/FO', 'CSV', '/NH'], {
|
|
147
|
-
encoding: 'utf8',
|
|
148
|
-
timeout: 3000,
|
|
149
|
-
windowsHide: true,
|
|
150
|
-
});
|
|
151
|
-
return (result.stdout || '').trim() || null;
|
|
152
|
-
} catch {
|
|
153
|
-
return null;
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
|
|
157
77
|
const commandIdentifiesOurServer = (command, entry) => {
|
|
158
78
|
if (typeof command !== 'string') return false;
|
|
159
79
|
const lower = command.toLowerCase();
|
|
@@ -164,88 +84,218 @@ const commandIdentifiesOurServer = (command, entry) => {
|
|
|
164
84
|
return true;
|
|
165
85
|
};
|
|
166
86
|
|
|
167
|
-
|
|
168
|
-
|
|
87
|
+
/**
|
|
88
|
+
* Build the registry API over injectable filesystem and child-process
|
|
89
|
+
* dependencies. Production callers use the default instance exported below;
|
|
90
|
+
* tests pass their own `fs`/`execFileAsync` instead of mocking node builtins.
|
|
91
|
+
*/
|
|
92
|
+
export const createManagedProcessRegistry = ({ fs = fsp, execFileAsync = defaultExecFileAsync } = {}) => {
|
|
93
|
+
const writeEntryFile = async (entry) => {
|
|
94
|
+
const dir = resolveRegistryDir();
|
|
169
95
|
try {
|
|
170
|
-
|
|
96
|
+
await fs.mkdir(dir, { recursive: true });
|
|
97
|
+
const filePath = path.join(dir, `${entry.pid}.json`);
|
|
98
|
+
const tmp = `${filePath}.tmp-${process.pid}`;
|
|
99
|
+
await fs.writeFile(tmp, JSON.stringify(entry, null, 2));
|
|
100
|
+
await fs.rename(tmp, filePath);
|
|
171
101
|
} catch {
|
|
102
|
+
// Best-effort: a failed registry write must never break spawn/shutdown.
|
|
172
103
|
}
|
|
173
|
-
|
|
174
|
-
}
|
|
104
|
+
};
|
|
175
105
|
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
|
|
106
|
+
const readAllEntries = async () => {
|
|
107
|
+
const dir = resolveRegistryDir();
|
|
108
|
+
let names = [];
|
|
109
|
+
try {
|
|
110
|
+
names = await fs.readdir(dir);
|
|
111
|
+
} catch {
|
|
112
|
+
return [];
|
|
113
|
+
}
|
|
114
|
+
const out = [];
|
|
115
|
+
for (const name of names.filter((value) => value.endsWith('.json'))) {
|
|
116
|
+
const filePath = path.join(dir, name);
|
|
117
|
+
try {
|
|
118
|
+
const entry = JSON.parse(await fs.readFile(filePath, 'utf8'));
|
|
119
|
+
if (entry && Number.isInteger(entry.pid)) {
|
|
120
|
+
out.push({ entry, filePath });
|
|
121
|
+
} else {
|
|
122
|
+
await fs.rm(filePath, { force: true });
|
|
123
|
+
}
|
|
124
|
+
} catch {
|
|
125
|
+
// Corrupt/partial file — drop it.
|
|
126
|
+
try {
|
|
127
|
+
await fs.rm(filePath, { force: true });
|
|
128
|
+
} catch {
|
|
129
|
+
// ignore
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return out;
|
|
179
134
|
};
|
|
180
135
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
136
|
+
/** Record an OpenCode process WE spawned so a future run can reap it if orphaned. */
|
|
137
|
+
const registerManagedProcess = async ({ pid, ownerPid, port, binary, runtime } = {}) => {
|
|
138
|
+
if (!Number.isInteger(pid)) return;
|
|
139
|
+
await writeEntryFile({
|
|
140
|
+
pid,
|
|
141
|
+
ownerPid: Number.isInteger(ownerPid) ? ownerPid : process.pid,
|
|
142
|
+
port: Number.isInteger(port) ? port : null,
|
|
143
|
+
binary: typeof binary === 'string' ? binary : null,
|
|
144
|
+
runtime: typeof runtime === 'string' ? runtime : 'web',
|
|
145
|
+
startedAt: new Date().toISOString(),
|
|
146
|
+
});
|
|
147
|
+
};
|
|
190
148
|
|
|
191
|
-
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
if (process.platform === 'win32') {
|
|
199
|
-
const image = readWindowsImageName(entry.pid);
|
|
200
|
-
const looksLikeOpencode = typeof image === 'string' && image.toLowerCase().includes('opencode');
|
|
201
|
-
// Windows lacks reliable reparent-to-1 semantics (job objects usually kill
|
|
202
|
-
// children with the parent), so we reap only when the owner is provably dead
|
|
203
|
-
// AND the image still looks like opencode.
|
|
204
|
-
if (looksLikeOpencode && ownerGone) {
|
|
205
|
-
await killOrphan(entry.pid);
|
|
206
|
-
log?.(`[lifecycle] reaped orphaned OpenCode pid ${entry.pid} (owner ${entry.ownerPid} gone)`);
|
|
207
|
-
return true;
|
|
149
|
+
/** Drop a pid from the registry (after we have killed/closed it ourselves). */
|
|
150
|
+
const unregisterManagedProcess = async (pid) => {
|
|
151
|
+
if (!Number.isInteger(pid)) return;
|
|
152
|
+
try {
|
|
153
|
+
await fs.rm(entryFilePath(pid), { force: true });
|
|
154
|
+
} catch {
|
|
155
|
+
// Best-effort: dropping a missing file is not an error.
|
|
208
156
|
}
|
|
209
|
-
|
|
210
|
-
}
|
|
157
|
+
};
|
|
211
158
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
159
|
+
// Returns { ppid, command } for a live pid on Unix, or null if it can't be read.
|
|
160
|
+
const readUnixProcInfo = async (pid) => {
|
|
161
|
+
try {
|
|
162
|
+
const { stdout } = await execFileAsync('ps', ['-p', String(pid), '-o', 'ppid=,command='], {
|
|
163
|
+
encoding: 'utf8',
|
|
164
|
+
timeout: 3000,
|
|
165
|
+
windowsHide: true,
|
|
166
|
+
});
|
|
167
|
+
const line = (stdout || '').trim();
|
|
168
|
+
if (!line) return null;
|
|
169
|
+
const match = line.match(/^\s*(\d+)\s+(.*)$/);
|
|
170
|
+
if (!match) return null;
|
|
171
|
+
return { ppid: Number.parseInt(match[1], 10), command: match[2] };
|
|
172
|
+
} catch {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
};
|
|
215
176
|
|
|
216
|
-
|
|
217
|
-
|
|
177
|
+
// Windows image name for a pid (e.g. "opencode.exe"), or null.
|
|
178
|
+
const readWindowsImageName = async (pid) => {
|
|
179
|
+
try {
|
|
180
|
+
const { stdout } = await execFileAsync('tasklist', ['/FI', `PID eq ${pid}`, '/FO', 'CSV', '/NH'], {
|
|
181
|
+
encoding: 'utf8',
|
|
182
|
+
timeout: 3000,
|
|
183
|
+
windowsHide: true,
|
|
184
|
+
});
|
|
185
|
+
return (stdout || '').trim() || null;
|
|
186
|
+
} catch {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
};
|
|
218
190
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
191
|
+
const killOrphan = async (pid) => {
|
|
192
|
+
if (process.platform === 'win32') {
|
|
193
|
+
try {
|
|
194
|
+
await execFileAsync('taskkill', ['/PID', String(pid), '/T', '/F'], {
|
|
195
|
+
stdio: 'ignore',
|
|
196
|
+
timeout: 5000,
|
|
197
|
+
windowsHide: true,
|
|
198
|
+
});
|
|
199
|
+
} catch {
|
|
200
|
+
// Best-effort: a failed kill is not fatal (startup reaper is a backstop).
|
|
201
|
+
}
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
223
204
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
205
|
+
const signalTree = (signal) => {
|
|
206
|
+
try {
|
|
207
|
+
process.kill(-pid, signal);
|
|
208
|
+
} catch {
|
|
209
|
+
// process group may already be gone
|
|
210
|
+
}
|
|
211
|
+
try {
|
|
212
|
+
process.kill(pid, signal);
|
|
213
|
+
} catch {
|
|
214
|
+
// pid may already be gone
|
|
215
|
+
}
|
|
216
|
+
};
|
|
232
217
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
try {
|
|
237
|
-
const wasReaped = await processEntry(entry, { log });
|
|
238
|
-
if (wasReaped) reaped += 1;
|
|
239
|
-
// Drop the file when the process is gone (reaped now, or already dead);
|
|
240
|
-
// keep it only while the process is still alive and owned by a live owner.
|
|
241
|
-
drop = wasReaped || !isPidAlive(entry.pid);
|
|
242
|
-
} catch (error) {
|
|
243
|
-
log?.(`[lifecycle] reap check failed for pid ${entry.pid}: ${error?.message ?? error}`);
|
|
218
|
+
signalTree('SIGTERM');
|
|
219
|
+
for (let waited = 0; waited < 1500 && isPidAlive(pid); waited += 150) {
|
|
220
|
+
await sleep(150);
|
|
244
221
|
}
|
|
245
|
-
if (
|
|
246
|
-
|
|
222
|
+
if (isPidAlive(pid)) {
|
|
223
|
+
signalTree('SIGKILL');
|
|
224
|
+
await sleep(300);
|
|
247
225
|
}
|
|
248
|
-
}
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
// Decide+act on a single registry entry. Returns true if it was reaped.
|
|
229
|
+
const processEntry = async (entry, { log }) => {
|
|
230
|
+
// Dead pid → nothing to do (caller drops the file).
|
|
231
|
+
if (!isPidAlive(entry.pid)) return false;
|
|
249
232
|
|
|
250
|
-
|
|
233
|
+
const ownerGone = Number.isInteger(entry.ownerPid) && !isPidAlive(entry.ownerPid);
|
|
234
|
+
|
|
235
|
+
if (process.platform === 'win32') {
|
|
236
|
+
const image = await readWindowsImageName(entry.pid);
|
|
237
|
+
const looksLikeOpencode = typeof image === 'string' && image.toLowerCase().includes('opencode');
|
|
238
|
+
// Windows lacks reliable reparent-to-1 semantics (job objects usually kill
|
|
239
|
+
// children with the parent), so we reap only when the owner is provably dead
|
|
240
|
+
// AND the image still looks like opencode.
|
|
241
|
+
if (looksLikeOpencode && ownerGone) {
|
|
242
|
+
await killOrphan(entry.pid);
|
|
243
|
+
log?.(`[lifecycle] reaped orphaned OpenCode pid ${entry.pid} (owner ${entry.ownerPid} gone)`);
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const info = await readUnixProcInfo(entry.pid);
|
|
250
|
+
// Can't verify identity (or it's not our server) → leave it alone.
|
|
251
|
+
if (!info || !commandIdentifiesOurServer(info.command, entry)) return false;
|
|
252
|
+
|
|
253
|
+
const orphaned = info.ppid === 1 || ownerGone;
|
|
254
|
+
if (!orphaned) return false; // still owned by a live instance
|
|
255
|
+
|
|
256
|
+
await killOrphan(entry.pid);
|
|
257
|
+
log?.(`[lifecycle] reaped orphaned OpenCode pid ${entry.pid} (reparented/owner gone)`);
|
|
258
|
+
return true;
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Kill any genuinely-orphaned OpenCode processes WE previously spawned, and
|
|
263
|
+
* prune their registry files. Safe to call at startup before spawning a new
|
|
264
|
+
* server. Returns { inspected, reaped }.
|
|
265
|
+
*/
|
|
266
|
+
const reapOrphanedProcesses = async ({ log } = {}) => {
|
|
267
|
+
const records = await readAllEntries();
|
|
268
|
+
if (records.length === 0) return { inspected: 0, reaped: 0 };
|
|
269
|
+
|
|
270
|
+
let reaped = 0;
|
|
271
|
+
for (const { entry, filePath } of records) {
|
|
272
|
+
let drop = false;
|
|
273
|
+
try {
|
|
274
|
+
const wasReaped = await processEntry(entry, { log });
|
|
275
|
+
if (wasReaped) reaped += 1;
|
|
276
|
+
// Drop the file when the process is gone (reaped now, or already dead);
|
|
277
|
+
// keep it only while the process is still alive and owned by a live owner.
|
|
278
|
+
drop = wasReaped || !isPidAlive(entry.pid);
|
|
279
|
+
} catch (error) {
|
|
280
|
+
log?.(`[lifecycle] reap check failed for pid ${entry.pid}: ${error?.message ?? error}`);
|
|
281
|
+
}
|
|
282
|
+
if (drop) {
|
|
283
|
+
try {
|
|
284
|
+
await fs.rm(filePath, { force: true });
|
|
285
|
+
} catch {
|
|
286
|
+
// best-effort
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return { inspected: records.length, reaped };
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
return { registerManagedProcess, unregisterManagedProcess, reapOrphanedProcesses };
|
|
251
295
|
};
|
|
296
|
+
|
|
297
|
+
const defaultRegistry = createManagedProcessRegistry();
|
|
298
|
+
|
|
299
|
+
export const registerManagedProcess = defaultRegistry.registerManagedProcess;
|
|
300
|
+
export const unregisterManagedProcess = defaultRegistry.unregisterManagedProcess;
|
|
301
|
+
export const reapOrphanedProcesses = defaultRegistry.reapOrphanedProcesses;
|