@openchamber/web 2.0.2 → 2.0.4
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/lib/DOCUMENTATION.md +1 -1
- package/bin/lib/commands-connect-url.js +6 -5
- package/bin/lib/commands-tunnel.js +24 -5
- package/dist/assets/{CodeMirrorCommentWidgets-CmKmyb9T.js → CodeMirrorCommentWidgets-C_zL-eri.js} +1 -1
- package/dist/assets/{DiffView-8-PFpZ2p.js → DiffView-BeN2_YYF.js} +3 -3
- package/dist/assets/FilesView-CN34wm_N.js +17 -0
- package/dist/assets/{GitView-DNUSu00w.js → GitView-DLDazOo1.js} +4 -4
- package/dist/assets/{GuestAttachDialog-Cv6QzjHC.js → GuestAttachDialog-C0xe_Lyy.js} +1 -1
- package/dist/assets/{GuestSurfacePane-Bt3jB_Ye.js → GuestSurfacePane-BdaurnDr.js} +1 -1
- package/dist/assets/{JsonTreeView-BlyHTRhM.js → JsonTreeView-B1Xq-k2t.js} +1 -1
- package/dist/assets/LinearIssuesView-BryoAHow.js +1 -0
- package/dist/assets/{MarkdownImageGallery-2UXJF-5B.js → MarkdownImageGallery-DfuEoaGq.js} +1 -1
- package/dist/assets/MarkdownRendererImpl-B_ZcWOvX.js +12 -0
- package/dist/assets/OnboardingScreen-C22bGmnP.js +1 -0
- package/dist/assets/{PatchDiff-ByvnbG5T.js → PatchDiff-0xd6UeFB.js} +1 -1
- package/dist/assets/{PierreDiffViewer-BXieg0mH.js → PierreDiffViewer-BC950e3F.js} +3 -3
- package/dist/assets/PlanView-B7Xgihqk.js +5 -0
- package/dist/assets/PluginPane-2C71F8uF.js +3 -0
- package/dist/assets/SettingsView-m_zoFiaT.js +30 -0
- package/dist/assets/{SettingsWindow-BIz224xs.js → SettingsWindow-CofS020b.js} +1 -1
- package/dist/assets/{SubmoduleDiffSummary-CXKmlps9.js → SubmoduleDiffSummary-BSA9gnIO.js} +1 -1
- package/dist/assets/{ToolOutputDialog-BsgAK8xu.js → ToolOutputDialog-DprkVKcj.js} +7 -7
- package/dist/assets/{ToolPartDiffPreview-vdPObXOf.js → ToolPartDiffPreview-Dq0go_fX.js} +2 -2
- package/dist/assets/{VirtualizedFileDiff-BkFNKWS5.js → VirtualizedFileDiff-Kp8hKJdT.js} +1 -1
- package/dist/assets/WalkthroughView-DGnTzqIU.js +4 -0
- package/dist/assets/{appThemeRegistry-aHUNHoqP.js → appThemeRegistry-BdEHBA4s.js} +1 -1
- package/dist/assets/{commitAndPush-Bon9r94r.js → commitAndPush-LwtVa2Oc.js} +1 -1
- package/dist/assets/{computeVirtualFileMetrics-DKqr_1AQ.js → computeVirtualFileMetrics-6V1xNpO9.js} +2 -2
- package/dist/assets/de-DlgBHUAI.js +10 -0
- package/dist/assets/es-BC8G41U8.js +16 -0
- package/dist/assets/fr-I0z3vVjy.js +14 -0
- package/dist/assets/index-CgR5rJq_.js +41 -0
- package/dist/assets/{index-DpFttslC.js → index-CmNPYddV.js} +1 -1
- package/dist/assets/index-adyy_sEk.css +1 -0
- package/dist/assets/{isDiffPlainText-kzAgpIgi.js → isDiffPlainText-1bXcWCrw.js} +1 -1
- package/dist/assets/{isFilePlainText-Uj6y-jzz.js → isFilePlainText-CBAppvy1.js} +1 -1
- package/dist/assets/ja-BIElEsY8.js +16 -0
- package/dist/assets/ko-DRnnHetA.js +16 -0
- package/dist/assets/{main-CbfMxn-I.js → main-CzjTMTfW.js} +2 -2
- package/dist/assets/main-DVX34beR.js +886 -0
- package/dist/assets/markdownCore-Cxbkn5NR.js +7 -0
- package/dist/assets/{miniChat-CiAKBC10.js → miniChat-CP68IL8Q.js} +2 -2
- package/dist/assets/{mobile-CsT4sJwC.js → mobile-BrlBrNfZ.js} +2 -2
- package/dist/assets/nl-DzUV-kwp.js +16 -0
- package/dist/assets/{patchFileDiff-DeaE08W0.js → patchFileDiff-pqRSHLHI.js} +2 -2
- package/dist/assets/pl-Bb-cb7k7.js +16 -0
- package/dist/assets/pt-BR-CyIWrTcE.js +16 -0
- package/dist/assets/renderElectronMiniChatApp-DKIilR7N.js +2 -0
- package/dist/assets/renderMobileApp-B0z6-x58.js +25 -0
- package/dist/assets/runtimeEndpointReset-uu1DBwLL.js +52 -0
- package/dist/assets/{scheduledTasksApi-BZCNJ588.js → scheduledTasksApi-BuzWT445.js} +1 -1
- package/dist/assets/{shikiHighlight-BSUEa3yF.js → shikiHighlight-BWGrY5YU.js} +1 -1
- package/dist/assets/{textMateThemeFromAppTheme-CsATFZIV.js → textMateThemeFromAppTheme-BSUFChBY.js} +1 -1
- package/dist/assets/tr-DXEDSoYO.js +7 -0
- package/dist/assets/uk-C8hN--q_.js +16 -0
- package/dist/assets/useAppFontEffects-Dsl_sLrj.js +7366 -0
- package/dist/assets/useInlineCommentController-d5vntsQ_.js +2 -0
- package/dist/assets/{useRootScrollLock-Cp1eFoM2.js → useRootScrollLock-2rd5Y6gR.js} +2 -2
- package/dist/assets/{vendor-base-ui-CsXYnR5c.js → vendor-base-ui-DnNniUWp.js} +1 -1
- package/dist/assets/{vendor-codemirror-language-data-BwwommM0.js → vendor-codemirror-language-data-B1EetJur.js} +2 -2
- package/dist/assets/vendor-opencode-client-DSZl7jSi.js +12 -0
- package/dist/assets/{vendor-tanstack-react-virtual-ClYN8CzB.js → vendor-tanstack-react-virtual--0sBrLRh.js} +1 -1
- package/dist/assets/{vendor-tanstack-virtual-core-BewVQryB.js → vendor-tanstack-virtual-core-BoA4iGMw.js} +1 -1
- package/dist/assets/{vendor-zod-BSoePkpP.js → vendor-zod-CxRSdNoV.js} +4 -4
- package/dist/assets/{web-components-D3Yjr4Zx.js → web-components-ngpHPEDU.js} +1 -1
- package/dist/assets/zh-CN-CtzXdjMZ.js +16 -0
- package/dist/assets/zh-TW-Ds0CADSz.js +16 -0
- package/dist/index.html +5 -5
- package/dist/mini-chat.html +5 -5
- package/dist/mobile.html +5 -5
- package/package.json +5 -5
- package/server/index.js +82 -4
- package/server/lib/agent-tool/DOCUMENTATION.md +8 -1
- package/server/lib/agent-tool/runtime.js +13 -4
- package/server/lib/agent-tool/runtime.test.js +15 -0
- package/server/lib/context-obligatory/DOCUMENTATION.md +10 -7
- package/server/lib/context-obligatory/runtime.js +2 -1
- package/server/lib/context-obligatory/runtime.test.js +76 -30
- package/server/lib/dictation/DOCUMENTATION.md +4 -1
- package/server/lib/dictation/openai-compatible-session.js +7 -0
- package/server/lib/enterprise-mode.d.ts +30 -0
- package/server/lib/enterprise-mode.js +253 -0
- package/server/lib/enterprise-mode.test.js +247 -0
- package/server/lib/event-stream/translate-v2.js +6 -0
- package/server/lib/event-stream/translate-v2.test.js +6 -0
- package/server/lib/git/DOCUMENTATION.md +2 -1
- package/server/lib/git/routes.js +44 -1
- package/server/lib/git/routes.test.js +98 -1
- package/server/lib/git/service.js +146 -11
- package/server/lib/git/service.test.js +195 -0
- package/server/lib/github/routes.js +9 -19
- package/server/lib/github/routes.test.js +112 -0
- package/server/lib/guests/DOCUMENTATION.md +4 -2
- package/server/lib/guests/background.test.js +1 -1
- package/server/lib/guests/catalog.js +56 -2
- package/server/lib/guests/catalog.test.js +24 -1
- package/server/lib/guests/clone-network.test.js +52 -0
- package/server/lib/guests/clone.js +30 -4
- package/server/lib/guests/enterprise.js +68 -0
- package/server/lib/guests/enterprise.test.js +106 -0
- package/server/lib/guests/grant-scope.js +8 -2
- package/server/lib/guests/grant-scope.test.js +9 -0
- package/server/lib/guests/html-styles.js +16 -2
- package/server/lib/guests/html-styles.test.js +53 -4
- package/server/lib/guests/install.js +10 -0
- package/server/lib/guests/persist.js +1 -0
- package/server/lib/guests/routes.js +40 -5
- package/server/lib/guests/updates.js +9 -2
- package/server/lib/notifications/DOCUMENTATION.md +1 -1
- package/server/lib/notifications/runtime.js +22 -8
- package/server/lib/notifications/trigger-runtime.test.js +45 -0
- package/server/lib/openchamber-sessions/session-metadata-store.js +27 -0
- package/server/lib/openchamber-sessions/session-metadata-store.test.js +18 -0
- package/server/lib/opencode/DOCUMENTATION.md +13 -9
- package/server/lib/opencode/auth-state-runtime.js +2 -2
- package/server/lib/opencode/config-paths.test.js +1 -0
- package/server/lib/opencode/env-runtime.js +26 -3
- package/server/lib/opencode/env-runtime.test.js +57 -0
- package/server/lib/opencode/feature-routes-runtime.js +2 -0
- package/server/lib/opencode/hmr-state-runtime.js +4 -3
- package/server/lib/opencode/lifecycle.js +10 -5
- package/server/lib/opencode/lifecycle.test.js +6 -2
- package/server/lib/opencode/managed-config-file.js +2 -1
- package/server/lib/opencode/managed-config-file.test.js +18 -0
- package/server/lib/opencode/npm-registry-config.d.ts +1 -0
- package/server/lib/opencode/npm-registry-config.js +123 -0
- package/server/lib/opencode/npm-registry-config.test.js +167 -0
- package/server/lib/opencode/npm-registry.js +5 -6
- package/server/lib/opencode/npm-registry.test.js +30 -0
- package/server/lib/opencode/openchamber-routes.js +8 -0
- package/server/lib/opencode/project-icon-routes.js +2 -2
- package/server/lib/opencode/project-icon-routes.test.js +44 -0
- package/server/lib/opencode/provider-source-route.test.js +32 -0
- package/server/lib/opencode/routes.js +17 -2
- package/server/lib/opencode/settings-helpers.js +26 -3
- package/server/lib/opencode/settings-helpers.test.js +12 -5
- package/server/lib/opencode/settings-registry.json +35 -1
- package/server/lib/opencode/settings-runtime.js +1 -0
- package/server/lib/opencode/shutdown-runtime.js +2 -0
- package/server/lib/opencode/static-routes-runtime.js +9 -1
- package/server/lib/opencode/static-routes-runtime.test.js +58 -1
- package/server/lib/package-manager.js +4 -1
- package/server/lib/package-manager.test.js +24 -0
- package/server/lib/permission-auto-accept/DOCUMENTATION.md +23 -15
- package/server/lib/permission-auto-accept/modes.js +26 -0
- package/server/lib/permission-auto-accept/runtime.js +146 -40
- package/server/lib/permission-auto-accept/runtime.test.js +93 -9
- package/server/lib/quota/DOCUMENTATION.md +6 -0
- package/server/lib/quota/providers/zhipuai-coding-plan.js +74 -30
- package/server/lib/quota/providers/zhipuai-coding-plan.test.js +124 -0
- package/server/lib/relay/DOCUMENTATION.md +1 -1
- package/server/lib/relay/service.js +32 -12
- package/server/lib/relay/service.test.js +30 -0
- package/server/lib/routing/DOCUMENTATION.md +150 -41
- package/server/lib/routing/classifier.js +156 -0
- package/server/lib/routing/defaults.js +27 -0
- package/server/lib/routing/jev.js +5 -17
- package/server/lib/routing/routes.http.test.js +9 -0
- package/server/lib/routing/routes.js +27 -1
- package/server/lib/routing/runtime.js +199 -33
- package/server/lib/routing/runtime.test.js +299 -15
- package/server/lib/routing/store.js +57 -0
- package/server/lib/routing/store.test.js +37 -0
- package/server/lib/session-assist/DOCUMENTATION.md +17 -7
- package/server/lib/session-assist/context.js +42 -2
- package/server/lib/session-assist/context.test.js +38 -1
- package/server/lib/session-assist/runtime.js +57 -13
- package/server/lib/session-assist/runtime.test.js +45 -0
- package/server/lib/session-goal/DOCUMENTATION.md +47 -22
- package/server/lib/session-goal/audit.js +112 -0
- package/server/lib/session-goal/audit.test.js +58 -0
- package/server/lib/session-goal/runtime.js +113 -134
- package/server/lib/session-goal/runtime.test.js +146 -13
- package/server/lib/session-lineage.js +52 -0
- package/server/lib/session-lineage.test.js +45 -0
- package/server/lib/session-work/DOCUMENTATION.md +108 -0
- package/server/lib/session-work/questions.js +126 -0
- package/server/lib/session-work/runtime.js +278 -0
- package/server/lib/session-work/runtime.test.js +255 -0
- package/server/lib/session-work/state.js +48 -0
- package/server/lib/session-work/state.test.js +34 -0
- package/server/lib/small-model/DOCUMENTATION.md +14 -7
- package/server/lib/small-model/client.js +7 -4
- package/server/lib/small-model/index.js +11 -21
- package/server/lib/small-model/index.test.js +13 -26
- package/server/lib/spaces/DOCUMENTATION.md +79 -25
- package/server/lib/spaces/gatekeeper-channel.js +63 -11
- package/server/lib/spaces/gatekeeper-channel.test.js +34 -4
- package/server/lib/spaces/gatekeeper-program.cjs +64 -13
- package/server/lib/spaces/gatekeeper-program.test.js +115 -3
- package/server/lib/spaces/host.js +58 -13
- package/server/lib/spaces/host.test.js +34 -1
- package/server/lib/spaces/idle-stop.js +98 -0
- package/server/lib/spaces/idle-stop.test.js +130 -0
- package/server/lib/spaces/journey.js +427 -17
- package/server/lib/spaces/journey.test.js +592 -5
- package/server/lib/spaces/layout.js +27 -6
- package/server/lib/spaces/manager.test.js +1 -1
- package/server/lib/spaces/places/contract-suite.js +4 -2
- package/server/lib/spaces/places/docker-live-support.js +27 -4
- package/server/lib/spaces/places/docker.js +32 -5
- package/server/lib/spaces/places/docker.test.js +38 -8
- package/server/lib/spaces/places/escape-suite.js +32 -0
- package/server/lib/spaces/places/fake-docker.js +3 -1
- package/server/lib/spaces/places/grants.docker.live.test.js +197 -0
- package/server/lib/spaces/places/memory-place.js +3 -3
- package/server/lib/spaces/places/sessions.docker.live.test.js +4 -2
- package/server/lib/spaces/routes.js +49 -1
- package/server/lib/spaces/routes.test.js +31 -3
- package/server/lib/spaces/space-opencode.js +52 -0
- package/server/lib/spaces/space-opencode.test.js +41 -0
- package/server/lib/spaces/space-records.js +90 -2
- package/server/lib/spaces/space-records.test.js +58 -1
- package/server/lib/spaces/space-server.js +13 -1
- package/server/lib/spaces/space-server.test.js +13 -0
- package/server/lib/spaces/space-setup.js +173 -0
- package/server/lib/spaces/space-setup.test.js +174 -0
- package/server/lib/tts/DOCUMENTATION.md +3 -0
- package/server/lib/tts/base-url.js +8 -0
- package/server/lib/tts/routes.js +13 -1
- package/server/lib/tts/routes.test.js +26 -0
- package/server/lib/tunnels/DOCUMENTATION.md +2 -0
- package/server/lib/tunnels/routes.js +14 -0
- package/server/lib/tunnels/routes.test.js +26 -0
- package/server/lib/walkthrough/index.js +11 -7
- package/server/lib/walkthrough/jobs.test.js +8 -0
- package/server/lib/walkthrough/languages.js +1 -0
- package/server/lib/walkthrough/routes.js +7 -1
- package/dist/assets/FilesView-0Ie20mEk.js +0 -17
- package/dist/assets/LinearIssuesView-CSl2MUfA.js +0 -1
- package/dist/assets/MarkdownRendererImpl-DVHqZoug.js +0 -12
- package/dist/assets/OnboardingScreen-CIdXJpfc.js +0 -1
- package/dist/assets/PlanView-DEIi3Q7M.js +0 -5
- package/dist/assets/PluginPane-BEPUWVho.js +0 -3
- package/dist/assets/SettingsView-CiNh-RbW.js +0 -30
- package/dist/assets/WalkthroughView-6oE-MuDL.js +0 -4
- package/dist/assets/de-CgAJ51z7.js +0 -10
- package/dist/assets/es-zGi_ravM.js +0 -16
- package/dist/assets/fr-CPfogrWx.js +0 -14
- package/dist/assets/index-CHfphYL3.js +0 -41
- package/dist/assets/index-DvMJ3w6V.css +0 -1
- package/dist/assets/ja-DMk6wd-U.js +0 -16
- package/dist/assets/ko-BFpzJ8jc.js +0 -16
- package/dist/assets/main-anyZFMIs.js +0 -896
- package/dist/assets/markdownCore-CmM47cAE.js +0 -7
- package/dist/assets/pl-VdRPcZrB.js +0 -16
- package/dist/assets/pt-BR-BRys_lOW.js +0 -16
- package/dist/assets/renderElectronMiniChatApp-C5677jy0.js +0 -2
- package/dist/assets/renderMobileApp-D4dBBAqQ.js +0 -25
- package/dist/assets/runtimeEndpointReset-5r0hkd-2.js +0 -27
- package/dist/assets/tr-CA9ckNnm.js +0 -7
- package/dist/assets/uk-byjR-y8D.js +0 -16
- package/dist/assets/useAppFontEffects-BWQZ5DeI.js +0 -7341
- package/dist/assets/useInlineCommentController-DlFyVfBu.js +0 -2
- package/dist/assets/vendor-opencode-client-DYdsLbCM.js +0 -12
- package/dist/assets/zh-CN-DRabgxdG.js +0 -16
- package/dist/assets/zh-TW-rbY5c9UK.js +0 -16
|
@@ -54,7 +54,7 @@ export function createMemoryPlace({ id = 'memory' } = {}) {
|
|
|
54
54
|
if (spaces.has(requireSpaceId(spaceId))) {
|
|
55
55
|
throw new SpaceError('space_name_taken', `Space ${spaceId} already exists`);
|
|
56
56
|
}
|
|
57
|
-
spaces.set(spaceId, { id: spaceId, name, project, created, state: 'running', orphans: [], damaged: false, missing: [] });
|
|
57
|
+
spaces.set(spaceId, { id: spaceId, name, project, created, state: 'running', stoppedIdle: false, gatekeeperRunning: true, orphans: [], damaged: false, missing: [] });
|
|
58
58
|
for (const target of TARGETS) {
|
|
59
59
|
// A container has a hostname of its own, as a real one does, so a caller can tell them apart.
|
|
60
60
|
containers.set(`${spaceId}:${target}`, { hostname: crypto.randomBytes(6).toString('hex') });
|
|
@@ -103,8 +103,8 @@ export function createMemoryPlace({ id = 'memory' } = {}) {
|
|
|
103
103
|
}
|
|
104
104
|
return net.connect({ host: '127.0.0.1', port: server.address().port });
|
|
105
105
|
},
|
|
106
|
-
stop: async (spaceId) => { requireSpace(spaceId)
|
|
107
|
-
start: async (spaceId) => { requireSpace(spaceId)
|
|
106
|
+
stop: async (spaceId) => { Object.assign(requireSpace(spaceId), { state: 'exited', gatekeeperRunning: false }); },
|
|
107
|
+
start: async (spaceId) => { Object.assign(requireSpace(spaceId), { state: 'running', stoppedIdle: false, gatekeeperRunning: true }); },
|
|
108
108
|
remove: async (spaceId) => {
|
|
109
109
|
const removed = [];
|
|
110
110
|
for (const target of TARGETS) {
|
|
@@ -163,7 +163,9 @@ describe.skipIf(!LIVE_DOCKER_ENABLED)('sessions and events of a real space: dock
|
|
|
163
163
|
expect(ids).toContain(hostSession.id);
|
|
164
164
|
expect(ids).toContain(session.id);
|
|
165
165
|
expect(merged.data.find((entry) => entry.id === session.id).location.directory).toBe(repo);
|
|
166
|
-
|
|
166
|
+
// Since 4c each space entry carries its name and the project it was made for; this space's
|
|
167
|
+
// project is not one the host registered, so both of those are null here.
|
|
168
|
+
expect(merged.spaces).toEqual([{ id: spec.id, name: spec.name, projectDirectory: null, directory: null, state: 'complete', sessions: expect.any(Number) }]);
|
|
167
169
|
|
|
168
170
|
expect(await until(() => received.some((event) => event.spaceId === spec.id && event.payload.type === 'session.created' && event.payload.data?.sessionID === session.id), 30_000)).toBe(true);
|
|
169
171
|
expect(await until(() => text.includes(session.id), 30_000)).toBe(true);
|
|
@@ -177,7 +179,7 @@ describe.skipIf(!LIVE_DOCKER_ENABLED)('sessions and events of a real space: dock
|
|
|
177
179
|
await sleep(2_100);
|
|
178
180
|
const merged = await (await fetch(url('/api/session'))).json();
|
|
179
181
|
expect(merged.data.map((entry) => entry.id)).toContain(hostSession.id);
|
|
180
|
-
expect(merged.spaces).toEqual([{ id: spec.id, state: 'stale', sessions: expect.any(Number) }]);
|
|
182
|
+
expect(merged.spaces).toEqual([{ id: spec.id, name: spec.name, projectDirectory: null, directory: null, state: 'stale', sessions: expect.any(Number) }]);
|
|
181
183
|
expect(merged.spaces[0].sessions).toBeGreaterThan(0);
|
|
182
184
|
const socket = await new Promise((resolve) => {
|
|
183
185
|
const attempt = new WebSocket(wsUrl(`/api/spaces/${spec.id}/terminal/ws`), { headers: { origin: 'http://app.test' } });
|
|
@@ -30,6 +30,14 @@ const STATUS_BY_CODE = new Map([
|
|
|
30
30
|
['invalid_branch_name', 400],
|
|
31
31
|
['invalid_space_id', 400],
|
|
32
32
|
['invalid_request_body', 400],
|
|
33
|
+
['invalid_grant_request', 400],
|
|
34
|
+
['provider_not_supported', 400],
|
|
35
|
+
['invalid_domain', 400],
|
|
36
|
+
['invalid_idle_stop', 400],
|
|
37
|
+
['network_is_open', 409],
|
|
38
|
+
['too_many_domains', 409],
|
|
39
|
+
['secret_source_missing', 409],
|
|
40
|
+
['space_record_unreadable', 409],
|
|
33
41
|
['space_preparing', 409],
|
|
34
42
|
['space_creation_failed', 409],
|
|
35
43
|
['space_busy', 409],
|
|
@@ -48,6 +56,9 @@ const STATUS_BY_CODE = new Map([
|
|
|
48
56
|
['nothing_to_apply', 409],
|
|
49
57
|
['place_cannot_restrict_network', 409],
|
|
50
58
|
['space_remove_incomplete', 502],
|
|
59
|
+
['gatekeeper_missing', 409],
|
|
60
|
+
['invalid_setup_commands', 400],
|
|
61
|
+
['space_setup_running', 409],
|
|
51
62
|
]);
|
|
52
63
|
|
|
53
64
|
/** One JSON answer per failure, with a stable code. Details travel as data; a stack never does. */
|
|
@@ -165,6 +176,14 @@ export function registerSpaceRoutes(app, { getJourney, getPlaces = () => [], rea
|
|
|
165
176
|
}
|
|
166
177
|
});
|
|
167
178
|
|
|
179
|
+
// The idle stop setting (decision 11): kept in the settings and told to every running space.
|
|
180
|
+
app.get(`${SPACES_ROUTE}/idle-stop`, withJourney(async (journey, _req, res) => {
|
|
181
|
+
res.json(await journey.readIdleStopSetting());
|
|
182
|
+
}));
|
|
183
|
+
app.put(`${SPACES_ROUTE}/idle-stop`, withJourney(async (journey, req, res) => {
|
|
184
|
+
res.json(await journey.changeIdleStop(requireBody(req)));
|
|
185
|
+
}));
|
|
186
|
+
|
|
168
187
|
// The places funnel: each place asked what it can do, now, because the user is looking.
|
|
169
188
|
app.get(`${SPACES_ROUTE}/places`, withJourney(async (_journey, _req, res) => {
|
|
170
189
|
const places = await Promise.all(getPlaces().map(async (place) => ({ id: place.id, ...(await place.check()) })));
|
|
@@ -172,7 +191,7 @@ export function registerSpaceRoutes(app, { getJourney, getPlaces = () => [], rea
|
|
|
172
191
|
}));
|
|
173
192
|
|
|
174
193
|
app.get(SPACES_ROUTE, withJourney(async (journey, _req, res) => {
|
|
175
|
-
res.json({ spaces: await journey.listSpaces() });
|
|
194
|
+
res.json({ spaces: await journey.listSpaces({ access: true }) });
|
|
176
195
|
}));
|
|
177
196
|
|
|
178
197
|
// Answers as soon as the space has an id; the steps follow as `openchamber:space-progress` events.
|
|
@@ -188,10 +207,39 @@ export function registerSpaceRoutes(app, { getJourney, getPlaces = () => [], rea
|
|
|
188
207
|
res.json(await journey.stopSpace(spaceIdOf(req)));
|
|
189
208
|
}));
|
|
190
209
|
|
|
210
|
+
// The repair actions, from soft to hard: OpenCode inside, then the container with a fresh token.
|
|
211
|
+
app.post(`${SPACES_ROUTE}/:id/restart-opencode`, withJourney(async (journey, req, res) => {
|
|
212
|
+
res.json(await journey.restartOpenCode(spaceIdOf(req)));
|
|
213
|
+
}));
|
|
214
|
+
|
|
215
|
+
app.post(`${SPACES_ROUTE}/:id/restart`, withJourney(async (journey, req, res) => {
|
|
216
|
+
res.json(await journey.restartSpace(spaceIdOf(req)));
|
|
217
|
+
}));
|
|
218
|
+
|
|
191
219
|
app.delete(`${SPACES_ROUTE}/:id`, withJourney(async (journey, req, res) => {
|
|
192
220
|
res.json(await journey.removeSpace(spaceIdOf(req)));
|
|
193
221
|
}));
|
|
194
222
|
|
|
223
|
+
// A grant for a running space; the key in the body goes to the gatekeeper and nowhere else.
|
|
224
|
+
app.post(`${SPACES_ROUTE}/:id/grants`, withJourney(async (journey, req, res) => {
|
|
225
|
+
res.json(await journey.grantAccess(spaceIdOf(req), requireBody(req)));
|
|
226
|
+
}));
|
|
227
|
+
|
|
228
|
+
// A domain added to the allowlist of a running space, live; the record keeps it for the next start.
|
|
229
|
+
app.post(`${SPACES_ROUTE}/:id/network/domains`, withJourney(async (journey, req, res) => {
|
|
230
|
+
res.json(await journey.openDomain(spaceIdOf(req), requireBody(req)));
|
|
231
|
+
}));
|
|
232
|
+
|
|
233
|
+
// The project's setup commands, again: answers once they began; each step follows as an event.
|
|
234
|
+
app.post(`${SPACES_ROUTE}/:id/setup`, withJourney(async (journey, req, res) => {
|
|
235
|
+
res.json(await journey.runSetup(spaceIdOf(req), requireBody(req)));
|
|
236
|
+
}));
|
|
237
|
+
|
|
238
|
+
// How the last run went, with the end of the output of the command that failed.
|
|
239
|
+
app.get(`${SPACES_ROUTE}/:id/setup`, withJourney(async (journey, req, res) => {
|
|
240
|
+
res.json(await journey.readSetup(spaceIdOf(req)));
|
|
241
|
+
}));
|
|
242
|
+
|
|
195
243
|
app.get(`${SPACES_ROUTE}/:id/journal`, withJourney(async (journey, req, res) => {
|
|
196
244
|
res.json(await journey.readJournal(spaceIdOf(req)));
|
|
197
245
|
}));
|
|
@@ -53,11 +53,19 @@ const journeyOf = (overrides = {}) => {
|
|
|
53
53
|
createSpace: record('createSpace', { id: ID, state: 'preparing' }),
|
|
54
54
|
startSpace: record('startSpace', { id: ID, state: 'running' }),
|
|
55
55
|
stopSpace: record('stopSpace', { id: ID, state: 'exited' }),
|
|
56
|
+
restartSpace: record('restartSpace', { id: ID, state: 'running', networkRestored: true }),
|
|
57
|
+
restartOpenCode: record('restartOpenCode', { id: ID, state: 'running' }),
|
|
56
58
|
removeSpace: record('removeSpace', { id: ID, removed: true }),
|
|
59
|
+
grantAccess: record('grantAccess', { grant: { id: 'open-1' } }),
|
|
60
|
+
openDomain: record('openDomain', { network: { mode: 'allowlist', domains: ['registry.npmjs.org'] } }),
|
|
57
61
|
readJournal: record('readJournal', { records: [], dropped: 0, since: 'now' }),
|
|
58
62
|
previewApply: record('previewApply', { changedPaths: 2 }),
|
|
59
63
|
applySpace: record('applySpace', { applied: { status: 'applied' } }),
|
|
60
64
|
stopAllSpaces: record('stopAllSpaces', { stopped: [], stillRunning: [] }),
|
|
65
|
+
readIdleStopSetting: record('readIdleStopSetting', { enabled: true, hours: 4 }),
|
|
66
|
+
changeIdleStop: record('changeIdleStop', { enabled: false, hours: 8 }),
|
|
67
|
+
runSetup: record('runSetup', { id: ID, state: 'running', setup: { state: 'running', index: 0, total: 1, command: 'npm ci' } }),
|
|
68
|
+
readSetup: record('readSetup', { setup: null, output: null }),
|
|
61
69
|
...overrides,
|
|
62
70
|
};
|
|
63
71
|
};
|
|
@@ -65,7 +73,7 @@ const journeyOf = (overrides = {}) => {
|
|
|
65
73
|
describe('space routes', () => {
|
|
66
74
|
it('answers every journey route with 404 and isolated_spaces_off while the feature is off, and the switch still works', async () => {
|
|
67
75
|
const { call, calls } = await serve({ journey: null, switchState: { enabled: false } });
|
|
68
|
-
for (const [method, path] of [['GET', ''], ['POST', ''], ['GET', '/places'], ['POST', `/${ID}/start`], ['POST', `/${ID}/stop`], ['DELETE', `/${ID}`], ['GET', `/${ID}/journal`], ['GET', `/${ID}/apply`], ['POST', `/${ID}/apply`]]) {
|
|
76
|
+
for (const [method, path] of [['GET', ''], ['POST', ''], ['GET', '/places'], ['POST', `/${ID}/start`], ['POST', `/${ID}/stop`], ['POST', `/${ID}/restart`], ['POST', `/${ID}/restart-opencode`], ['POST', `/${ID}/grants`], ['DELETE', `/${ID}`], ['GET', `/${ID}/journal`], ['GET', `/${ID}/apply`], ['POST', `/${ID}/apply`], ['GET', '/idle-stop'], ['PUT', '/idle-stop'], ['POST', `/${ID}/setup`], ['GET', `/${ID}/setup`]]) {
|
|
69
77
|
expect(await call(method, `${SPACES_ROUTE}${path}`, method === 'GET' || method === 'DELETE' ? undefined : {}), `${method} ${path}`).toEqual({ status: 404, body: { code: 'isolated_spaces_off', message: 'Isolated spaces are turned off.', details: null } });
|
|
70
78
|
}
|
|
71
79
|
expect(await call('GET', `${SPACES_ROUTE}/switch`)).toEqual({ status: 200, body: { enabled: false, spaces: [] } });
|
|
@@ -99,15 +107,34 @@ describe('space routes', () => {
|
|
|
99
107
|
expect(await call('POST', SPACES_ROUTE, request)).toEqual({ status: 202, body: { id: ID, state: 'preparing' } });
|
|
100
108
|
expect(await call('POST', `${SPACES_ROUTE}/${ID}/start`)).toEqual({ status: 200, body: { id: ID, state: 'running' } });
|
|
101
109
|
expect(await call('POST', `${SPACES_ROUTE}/${ID}/stop`)).toEqual({ status: 200, body: { id: ID, state: 'exited' } });
|
|
110
|
+
expect(await call('POST', `${SPACES_ROUTE}/${ID}/restart`)).toEqual({ status: 200, body: { id: ID, state: 'running', networkRestored: true } });
|
|
111
|
+
expect(await call('POST', `${SPACES_ROUTE}/${ID}/restart-opencode`)).toEqual({ status: 200, body: { id: ID, state: 'running' } });
|
|
112
|
+
expect(await call('POST', `${SPACES_ROUTE}/${ID}/grants`, { kind: 'domain', upstream: 'https://registry.example.com/' })).toEqual({ status: 200, body: { grant: { id: 'open-1' } } });
|
|
113
|
+
expect(await call('POST', `${SPACES_ROUTE}/${ID}/network/domains`, { domain: 'registry.npmjs.org' })).toEqual({ status: 200, body: { network: { mode: 'allowlist', domains: ['registry.npmjs.org'] } } });
|
|
102
114
|
expect(await call('GET', `${SPACES_ROUTE}/${ID}/journal`)).toEqual({ status: 200, body: { records: [], dropped: 0, since: 'now' } });
|
|
103
115
|
expect(await call('GET', `${SPACES_ROUTE}/${ID}/apply`)).toEqual({ status: 200, body: { changedPaths: 2 } });
|
|
104
116
|
expect(await call('POST', `${SPACES_ROUTE}/${ID}/apply`, { as: 'branch', branch: 'b' })).toEqual({ status: 200, body: { applied: { status: 'applied' } } });
|
|
117
|
+
expect(await call('POST', `${SPACES_ROUTE}/${ID}/setup`, { commands: ['npm ci'] })).toEqual({ status: 200, body: { id: ID, state: 'running', setup: { state: 'running', index: 0, total: 1, command: 'npm ci' } } });
|
|
118
|
+
expect(await call('GET', `${SPACES_ROUTE}/${ID}/setup`)).toEqual({ status: 200, body: { setup: null, output: null } });
|
|
105
119
|
expect(await call('DELETE', `${SPACES_ROUTE}/${ID}`)).toEqual({ status: 200, body: { id: ID, removed: true } });
|
|
106
120
|
expect(journey.calls).toEqual([
|
|
107
|
-
['listSpaces'], ['createSpace', request], ['startSpace', ID], ['stopSpace', ID], ['readJournal', ID], ['previewApply', ID], ['applySpace', ID, { as: 'branch', branch: 'b' }], ['removeSpace', ID],
|
|
121
|
+
['listSpaces', { access: true }], ['createSpace', request], ['startSpace', ID], ['stopSpace', ID], ['restartSpace', ID], ['restartOpenCode', ID], ['grantAccess', ID, { kind: 'domain', upstream: 'https://registry.example.com/' }], ['openDomain', ID, { domain: 'registry.npmjs.org' }], ['readJournal', ID], ['previewApply', ID], ['applySpace', ID, { as: 'branch', branch: 'b' }], ['runSetup', ID, { commands: ['npm ci'] }], ['readSetup', ID], ['removeSpace', ID],
|
|
108
122
|
]);
|
|
109
123
|
});
|
|
110
124
|
|
|
125
|
+
it('reads and changes the idle stop setting through the journey, and refuses a body that is not an object', async () => {
|
|
126
|
+
const journey = journeyOf();
|
|
127
|
+
const { call } = await serve({ journey });
|
|
128
|
+
expect(await call('GET', `${SPACES_ROUTE}/idle-stop`)).toEqual({ status: 200, body: { enabled: true, hours: 4 } });
|
|
129
|
+
expect(await call('PUT', `${SPACES_ROUTE}/idle-stop`, { enabled: false, hours: 8 })).toEqual({ status: 200, body: { enabled: false, hours: 8 } });
|
|
130
|
+
expect(await call('PUT', `${SPACES_ROUTE}/idle-stop`, [8])).toMatchObject({ status: 400, body: { code: 'invalid_request_body' } });
|
|
131
|
+
expect(journey.calls).toEqual([['readIdleStopSetting'], ['changeIdleStop', { enabled: false, hours: 8 }]]);
|
|
132
|
+
|
|
133
|
+
const refusing = journeyOf({ changeIdleStop: async () => { throw new SpaceError('invalid_idle_stop', 'whole hours from 1 to 168'); } });
|
|
134
|
+
const second = await serve({ journey: refusing });
|
|
135
|
+
expect(await second.call('PUT', `${SPACES_ROUTE}/idle-stop`, { enabled: true, hours: 0 })).toMatchObject({ status: 400, body: { code: 'invalid_idle_stop' } });
|
|
136
|
+
});
|
|
137
|
+
|
|
111
138
|
it('refuses an id that is not a space id before the journey is asked, and a body that is not an object', async () => {
|
|
112
139
|
const journey = journeyOf();
|
|
113
140
|
const { call } = await serve({ journey });
|
|
@@ -123,8 +150,9 @@ describe('space routes', () => {
|
|
|
123
150
|
const res = { status(code) { this.code = code; return this; }, json(body) { answers.push({ status: this.code, body }); } };
|
|
124
151
|
const cases = [
|
|
125
152
|
['space_not_found', 404], ['isolated_spaces_off', 404], ['project_not_registered', 400], ['invalid_network', 400], ['space_preparing', 409], ['space_not_running', 409],
|
|
126
|
-
['space_busy', 409], ['space_creation_failed', 409],
|
|
153
|
+
['space_busy', 409], ['space_creation_failed', 409], ['invalid_grant_request', 400], ['secret_source_missing', 409], ['space_record_unreadable', 409], ['invalid_domain', 400], ['network_is_open', 409], ['too_many_domains', 409],
|
|
127
154
|
['branch_exists', 409], ['changes_do_not_apply', 409], ['changes_route_closed', 409], ['nothing_to_apply', 409], ['place_cannot_restrict_network', 409],
|
|
155
|
+
['invalid_setup_commands', 400], ['space_setup_running', 409],
|
|
128
156
|
['space_remove_incomplete', 502], ['docker_command_failed', 502], ['code_out_failed', 502],
|
|
129
157
|
];
|
|
130
158
|
for (const [code, status] of cases) {
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// The one place that writes OpenCode's own files inside a space, so an OpenCode format change is
|
|
2
|
+
// a one-file edit (DESIGN.md, "Parts"). Today: the provider configuration that sends a provider's
|
|
3
|
+
// model calls through the gatekeeper's window. The login record for the short OpenAI token and
|
|
4
|
+
// the move of a chat to the host's archive belong here too, in their stages.
|
|
5
|
+
//
|
|
6
|
+
// What is written here cooperates and enforces nothing. The agent can change or delete the file;
|
|
7
|
+
// the gatekeeper is what keeps the key out of the space and the space away from the internet.
|
|
8
|
+
|
|
9
|
+
import { SpaceError } from './errors.js';
|
|
10
|
+
import { tail } from './exec-http.js';
|
|
11
|
+
import { IMAGE_ONLY_PATH, IMAGE_SH, SPACE_OPENCODE_CONFIG_DIRECTORY, SPACE_OPENCODE_CONFIG_PATH, spaceWindowUrl } from './layout.js';
|
|
12
|
+
|
|
13
|
+
// OpenCode takes a key from this option and its auth plugins stay out of the way; the window
|
|
14
|
+
// throws it away and puts the real one in its place. It is not a secret and never was one.
|
|
15
|
+
export const WINDOW_PLACEHOLDER_KEY = 'space-window';
|
|
16
|
+
|
|
17
|
+
// Through a temporary name, so OpenCode never reads half a file. The configuration is JSON on
|
|
18
|
+
// stdin: nothing of it is an argument.
|
|
19
|
+
const WRITE_CONFIG_SCRIPT = [
|
|
20
|
+
IMAGE_ONLY_PATH,
|
|
21
|
+
`mkdir -p ${SPACE_OPENCODE_CONFIG_DIRECTORY}`,
|
|
22
|
+
`&& cat > ${SPACE_OPENCODE_CONFIG_PATH}.new && mv ${SPACE_OPENCODE_CONFIG_PATH}.new ${SPACE_OPENCODE_CONFIG_PATH}`,
|
|
23
|
+
].join(' ');
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* OpenCode's global configuration for a space with these model grants: each provider, under the
|
|
27
|
+
* id the host's catalog gives it, with its base URL at the window and a placeholder key. The
|
|
28
|
+
* composer offers the host's catalog, and the same provider id inside is what makes that choice
|
|
29
|
+
* work in the space unchanged. Decided with the maintainer on 2026-09-26.
|
|
30
|
+
*/
|
|
31
|
+
export function buildProviderConfig(grants) {
|
|
32
|
+
const provider = {};
|
|
33
|
+
for (const grant of grants) {
|
|
34
|
+
if (grant.kind !== 'model') continue;
|
|
35
|
+
provider[grant.provider] = { options: { baseURL: spaceWindowUrl(grant.id), apiKey: WINDOW_PLACEHOLDER_KEY } };
|
|
36
|
+
}
|
|
37
|
+
return { $schema: 'https://opencode.ai/config.json', provider };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** `exec` is the place operation, always for the space container. */
|
|
41
|
+
export function createSpaceOpenCode({ exec }) {
|
|
42
|
+
/** Writes the whole global configuration from the model grants the record holds. */
|
|
43
|
+
const writeProviderConfig = async (spaceId, grants) => {
|
|
44
|
+
const text = `${JSON.stringify(buildProviderConfig(grants), null, 2)}\n`;
|
|
45
|
+
const result = await exec(spaceId, [IMAGE_SH, '-c', WRITE_CONFIG_SCRIPT], { stdin: text });
|
|
46
|
+
if (result.code !== 0) {
|
|
47
|
+
throw new SpaceError('space_setup_failed', `Could not write the provider configuration inside the space: ${tail(result.stderr) || `exit code ${result.code}`}`);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
return { writeProviderConfig };
|
|
52
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { WINDOW_PLACEHOLDER_KEY, buildProviderConfig, createSpaceOpenCode } from './space-opencode.js';
|
|
4
|
+
|
|
5
|
+
const ID = 'a1b2c3d4e5f6';
|
|
6
|
+
const GRANTS = [
|
|
7
|
+
{ kind: 'model', id: 'anthropic', provider: 'anthropic', upstream: 'https://api.anthropic.com/v1', header: 'x-api-key', source: { kind: 'typed' } },
|
|
8
|
+
{ kind: 'domain', id: 'open-0a1b2c3d4e5f', upstream: 'https://registry.example.com/npm/' },
|
|
9
|
+
{ kind: 'model', id: 'openai', provider: 'openai', upstream: 'https://api.openai.com/v1', header: 'authorization', source: { kind: 'env', name: 'OPENAI_API_KEY' } },
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
describe('the provider configuration inside a space', () => {
|
|
13
|
+
it('names each granted provider as the host does, at the window, with a placeholder key, and leaves domains out', () => {
|
|
14
|
+
expect(buildProviderConfig(GRANTS)).toEqual({
|
|
15
|
+
$schema: 'https://opencode.ai/config.json',
|
|
16
|
+
provider: {
|
|
17
|
+
anthropic: { options: { baseURL: 'http://gatekeeper:8080/model/anthropic', apiKey: WINDOW_PLACEHOLDER_KEY } },
|
|
18
|
+
openai: { options: { baseURL: 'http://gatekeeper:8080/model/openai', apiKey: WINDOW_PLACEHOLDER_KEY } },
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
expect(buildProviderConfig([])).toEqual({ $schema: 'https://opencode.ai/config.json', provider: {} });
|
|
22
|
+
// The upstream is the gatekeeper's business; nothing of it is written where the agent reads.
|
|
23
|
+
expect(JSON.stringify(buildProviderConfig(GRANTS))).not.toContain('api.anthropic.com');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('writes the whole file over exec, as JSON on stdin, through a temporary name', async () => {
|
|
27
|
+
const calls = [];
|
|
28
|
+
const opencode = createSpaceOpenCode({ exec: async (spaceId, argv, options) => { calls.push({ spaceId, argv, options }); return { code: 0, stdout: '', stderr: '' }; } });
|
|
29
|
+
await opencode.writeProviderConfig(ID, GRANTS);
|
|
30
|
+
expect(calls).toEqual([{
|
|
31
|
+
spaceId: ID,
|
|
32
|
+
argv: ['/bin/sh', '-c', 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin; mkdir -p /home/space/.config/opencode && cat > /home/space/.config/opencode/opencode.json.new && mv /home/space/.config/opencode/opencode.json.new /home/space/.config/opencode/opencode.json'],
|
|
33
|
+
options: { stdin: `${JSON.stringify(buildProviderConfig(GRANTS), null, 2)}\n` },
|
|
34
|
+
}]);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('says what failed inside', async () => {
|
|
38
|
+
const opencode = createSpaceOpenCode({ exec: async () => ({ code: 1, stdout: '', stderr: 'sh: cannot create: Read-only file system\n' }) });
|
|
39
|
+
await expect(opencode.writeProviderConfig(ID, GRANTS)).rejects.toMatchObject({ code: 'space_setup_failed', message: expect.stringContaining('Read-only file system') });
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -12,6 +12,7 @@ import path from 'node:path';
|
|
|
12
12
|
import { z } from 'zod';
|
|
13
13
|
|
|
14
14
|
import { requireSpaceId } from './labels.js';
|
|
15
|
+
import { MAX_KEPT_OUTPUT_CHARACTERS, MAX_SETUP_COMMAND_LENGTH, MAX_SETUP_COMMANDS } from './space-setup.js';
|
|
15
16
|
|
|
16
17
|
const RECORDS_DIRECTORY = path.join('spaces', 'records');
|
|
17
18
|
|
|
@@ -20,13 +21,70 @@ const NETWORK_MODES = Object.freeze(['allowlist', 'open']);
|
|
|
20
21
|
const DOMAIN_PATTERN = /^(?=.{1,253}$)(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z][a-z0-9-]{0,61}[a-z0-9]$/;
|
|
21
22
|
const MAX_DOMAINS = 200;
|
|
22
23
|
|
|
24
|
+
/** One name of an allowlist. */
|
|
25
|
+
export const domainSchema = z.string().regex(DOMAIN_PATTERN);
|
|
26
|
+
|
|
23
27
|
export const networkSchema = z.object({
|
|
24
28
|
mode: z.enum(NETWORK_MODES),
|
|
25
|
-
domains: z.array(
|
|
29
|
+
domains: z.array(domainSchema).max(MAX_DOMAINS).default([]),
|
|
26
30
|
});
|
|
27
31
|
|
|
28
32
|
const historySchema = z.enum(['pending', 'sent', 'already_complete', 'host_shallow', 'failed']);
|
|
29
33
|
|
|
34
|
+
const GRANT_ID_PATTERN = /^[A-Za-z0-9._-]{1,64}$/;
|
|
35
|
+
// zod runs the refinement even when `.url()` failed, so the parse of the URL must not throw here.
|
|
36
|
+
const isHttpUrl = (value) => {
|
|
37
|
+
try {
|
|
38
|
+
return /^https?:$/.test(new URL(value).protocol);
|
|
39
|
+
} catch {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
const upstreamSchema = z.string().url().refine(isHttpUrl, 'http or https');
|
|
44
|
+
/** Where the host finds a secret again, never the secret: an environment variable of the host's by name, or the user typed it once. */
|
|
45
|
+
export const secretSourceSchema = z.discriminatedUnion('kind', [
|
|
46
|
+
z.object({ kind: z.literal('env'), name: z.string().regex(/^[A-Za-z_][A-Za-z0-9_]{0,255}$/) }).strict(),
|
|
47
|
+
z.object({ kind: z.literal('typed') }).strict(),
|
|
48
|
+
]);
|
|
49
|
+
/**
|
|
50
|
+
* A grant as the host remembers it. Strict, so a value under any name is refused rather than
|
|
51
|
+
* written: a record holds where a secret comes from and never what it is (decision 5).
|
|
52
|
+
*/
|
|
53
|
+
export const grantSchema = z.discriminatedUnion('kind', [
|
|
54
|
+
z.object({
|
|
55
|
+
kind: z.literal('model'),
|
|
56
|
+
id: z.string().regex(GRANT_ID_PATTERN),
|
|
57
|
+
provider: z.string().regex(GRANT_ID_PATTERN),
|
|
58
|
+
upstream: upstreamSchema,
|
|
59
|
+
header: z.string().regex(/^[a-z0-9-]{1,64}$/),
|
|
60
|
+
source: secretSourceSchema,
|
|
61
|
+
}).strict(),
|
|
62
|
+
z.object({
|
|
63
|
+
kind: z.literal('domain'),
|
|
64
|
+
id: z.string().regex(GRANT_ID_PATTERN),
|
|
65
|
+
upstream: upstreamSchema,
|
|
66
|
+
}).strict(),
|
|
67
|
+
]);
|
|
68
|
+
const MAX_GRANTS = 100;
|
|
69
|
+
|
|
70
|
+
// The setup commands' last run (5d-4): begun, finished, or failed with the end of its output,
|
|
71
|
+
// which came from inside the space and is text to show. `space-setup.js` keeps it within these.
|
|
72
|
+
const setupCountSchema = z.number().int().min(1).max(MAX_SETUP_COMMANDS);
|
|
73
|
+
const setupSchema = z.discriminatedUnion('state', [
|
|
74
|
+
z.object({ state: z.literal('running'), total: setupCountSchema, startedAt: z.string() }).strict(),
|
|
75
|
+
z.object({ state: z.literal('done'), total: setupCountSchema, finishedAt: z.string() }).strict(),
|
|
76
|
+
z.object({
|
|
77
|
+
state: z.literal('failed'),
|
|
78
|
+
total: setupCountSchema,
|
|
79
|
+
index: z.number().int().min(0).max(MAX_SETUP_COMMANDS - 1),
|
|
80
|
+
command: z.string().max(MAX_SETUP_COMMAND_LENGTH),
|
|
81
|
+
exitCode: z.number().int().nullable(),
|
|
82
|
+
timedOut: z.boolean(),
|
|
83
|
+
output: z.string().max(MAX_KEPT_OUTPUT_CHARACTERS),
|
|
84
|
+
finishedAt: z.string(),
|
|
85
|
+
}).strict(),
|
|
86
|
+
]);
|
|
87
|
+
|
|
30
88
|
const recordSchema = z.object({
|
|
31
89
|
version: z.literal(1),
|
|
32
90
|
network: networkSchema,
|
|
@@ -37,6 +95,10 @@ const recordSchema = z.object({
|
|
|
37
95
|
/** The commit the snapshot was taken from, which the history is sent behind. */
|
|
38
96
|
base: z.string().regex(/^[0-9a-f]{40}$|^[0-9a-f]{64}$/).nullable().default(null),
|
|
39
97
|
history: historySchema.default('pending'),
|
|
98
|
+
/** The grants the user gave, said again to the gatekeeper after every start. */
|
|
99
|
+
grants: z.array(grantSchema).max(MAX_GRANTS).default([]),
|
|
100
|
+
/** The setup commands' last run, or null before any. */
|
|
101
|
+
setup: setupSchema.nullable().default(null),
|
|
40
102
|
});
|
|
41
103
|
|
|
42
104
|
/**
|
|
@@ -48,6 +110,32 @@ export function createSpaceRecords({ dataDir, logger = console }) {
|
|
|
48
110
|
const directory = path.join(dataDir, RECORDS_DIRECTORY);
|
|
49
111
|
const fileOf = (spaceId) => path.join(directory, `${requireSpaceId(spaceId)}.json`);
|
|
50
112
|
|
|
113
|
+
/**
|
|
114
|
+
* One grant the host cannot read, from a later version that knows another kind among the
|
|
115
|
+
* reasons, is dropped with a warning and never takes the network, the repository and the
|
|
116
|
+
* history of the record with it: the space then shows that grant as missing, not as unknown.
|
|
117
|
+
*/
|
|
118
|
+
const withReadableGrants = (spaceId, raw) => {
|
|
119
|
+
if (!(raw instanceof Object) || !Array.isArray(raw.grants)) return raw;
|
|
120
|
+
const grants = raw.grants.filter((grant) => grantSchema.safeParse(grant).success);
|
|
121
|
+
if (grants.length < raw.grants.length) {
|
|
122
|
+
logger.warn?.(`[spaces] the record of space ${spaceId} holds ${raw.grants.length - grants.length} grant(s) this host cannot read; they are left out`);
|
|
123
|
+
}
|
|
124
|
+
return { ...raw, grants };
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* A setup the host cannot read, from a later version among the reasons, is forgotten with a
|
|
129
|
+
* warning: it only says how the last run of the setup commands went, and must not make the
|
|
130
|
+
* network and the grants unreadable with it.
|
|
131
|
+
*/
|
|
132
|
+
const withReadableSetup = (spaceId, raw) => {
|
|
133
|
+
if (!(raw instanceof Object) || raw.setup === undefined || raw.setup === null) return raw;
|
|
134
|
+
if (setupSchema.safeParse(raw.setup).success) return raw;
|
|
135
|
+
logger.warn?.(`[spaces] the record of space ${spaceId} holds a setup this host cannot read; it is left out`);
|
|
136
|
+
return { ...raw, setup: null };
|
|
137
|
+
};
|
|
138
|
+
|
|
51
139
|
const read = (spaceId) => {
|
|
52
140
|
const file = fileOf(spaceId);
|
|
53
141
|
let text;
|
|
@@ -59,7 +147,7 @@ export function createSpaceRecords({ dataDir, logger = console }) {
|
|
|
59
147
|
return { status: 'unreadable', record: null };
|
|
60
148
|
}
|
|
61
149
|
try {
|
|
62
|
-
const parsed = recordSchema.safeParse(JSON.parse(text));
|
|
150
|
+
const parsed = recordSchema.safeParse(withReadableSetup(spaceId, withReadableGrants(spaceId, JSON.parse(text))));
|
|
63
151
|
if (parsed.success) return { status: 'ok', record: parsed.data };
|
|
64
152
|
} catch {
|
|
65
153
|
// Reported below.
|
|
@@ -26,7 +26,7 @@ describe('space records', () => {
|
|
|
26
26
|
expect(records.read(ID)).toEqual({ status: 'missing', record: null });
|
|
27
27
|
|
|
28
28
|
const written = records.write(ID, { network: { mode: 'allowlist', domains: ['api.anthropic.com'] }, repository: '/home/me/project' });
|
|
29
|
-
expect(written).toEqual({ version: 1, network: { mode: 'allowlist', domains: ['api.anthropic.com'] }, repository: '/home/me/project', spacePath: null, base: null, history: 'pending' });
|
|
29
|
+
expect(written).toEqual({ version: 1, network: { mode: 'allowlist', domains: ['api.anthropic.com'] }, repository: '/home/me/project', spacePath: null, base: null, history: 'pending', grants: [], setup: null });
|
|
30
30
|
const file = path.join(dataDir, 'spaces', 'records', `${ID}.json`);
|
|
31
31
|
if (process.platform !== 'win32') expect(fs.statSync(file).mode & 0o777).toBe(0o600);
|
|
32
32
|
expect(fs.readdirSync(path.dirname(file))).toEqual([`${ID}.json`]);
|
|
@@ -53,6 +53,63 @@ describe('space records', () => {
|
|
|
53
53
|
expect(fs.readFileSync(path.join(directory, `${ID}.json`), 'utf8')).toBe('not json');
|
|
54
54
|
});
|
|
55
55
|
|
|
56
|
+
it('remembers a grant without its value, and refuses one that carries a value under any name', () => {
|
|
57
|
+
const records = createSpaceRecords({ dataDir: temporary(), logger: quiet });
|
|
58
|
+
const model = { kind: 'model', id: 'anthropic', provider: 'anthropic', upstream: 'https://api.anthropic.com/v1', header: 'x-api-key', source: { kind: 'env', name: 'ANTHROPIC_API_KEY' } };
|
|
59
|
+
const typed = { ...model, id: 'openai', provider: 'openai', upstream: 'https://api.openai.com/v1', header: 'authorization', source: { kind: 'typed' } };
|
|
60
|
+
const domain = { kind: 'domain', id: 'open-0a1b2c3d4e5f', upstream: 'https://registry.example.com/npm/' };
|
|
61
|
+
const written = records.write(ID, { network: { mode: 'open' }, grants: [model, typed, domain] });
|
|
62
|
+
expect(written.grants).toEqual([model, typed, domain]);
|
|
63
|
+
expect(records.read(ID).record.grants).toEqual([model, typed, domain]);
|
|
64
|
+
|
|
65
|
+
for (const bad of [
|
|
66
|
+
{ ...model, secret: 'sk-live-1' },
|
|
67
|
+
{ ...model, value: 'sk-live-1' },
|
|
68
|
+
{ ...model, source: { kind: 'typed', value: 'sk-live-1' } },
|
|
69
|
+
{ ...model, source: { kind: 'env', name: 'ANTHROPIC_API_KEY', value: 'sk-live-1' } },
|
|
70
|
+
{ ...domain, secret: 'sk-live-1' },
|
|
71
|
+
{ ...domain, header: 'authorization' },
|
|
72
|
+
{ ...model, upstream: 'file:///etc/passwd' },
|
|
73
|
+
{ ...model, header: 'X-Api-Key' },
|
|
74
|
+
{ ...model, id: '../x' },
|
|
75
|
+
{ ...model, source: { kind: 'file', name: '/tmp/key' } },
|
|
76
|
+
]) {
|
|
77
|
+
expect(() => records.write(ID, { network: { mode: 'open' }, grants: [bad] }), JSON.stringify(bad)).toThrow();
|
|
78
|
+
}
|
|
79
|
+
// A grant this host cannot read, from a later version, is left out; the rest of the record stays.
|
|
80
|
+
const later = temporary();
|
|
81
|
+
const withUnknown = createSpaceRecords({ dataDir: later, logger: quiet });
|
|
82
|
+
fs.mkdirSync(path.join(later, 'spaces', 'records'), { recursive: true });
|
|
83
|
+
fs.writeFileSync(path.join(later, 'spaces', 'records', `${ID}.json`), JSON.stringify({ version: 1, network: { mode: 'allowlist', domains: ['a.example.com'] }, repository: '/home/me/project', history: 'sent', grants: [{ kind: 'git', id: 'github' }, domain] }));
|
|
84
|
+
expect(withUnknown.read(ID)).toEqual({ status: 'ok', record: expect.objectContaining({ network: { mode: 'allowlist', domains: ['a.example.com'] }, repository: '/home/me/project', history: 'sent', grants: [domain] }) });
|
|
85
|
+
// A malformed upstream is refused, not thrown.
|
|
86
|
+
expect(() => records.write(ID, { network: { mode: 'open' }, grants: [{ ...domain, upstream: 'not a url' }] })).toThrow();
|
|
87
|
+
|
|
88
|
+
// A record written before grants existed reads back with none.
|
|
89
|
+
const dataDir = temporary();
|
|
90
|
+
const older = createSpaceRecords({ dataDir, logger: quiet });
|
|
91
|
+
fs.mkdirSync(path.join(dataDir, 'spaces', 'records'), { recursive: true });
|
|
92
|
+
fs.writeFileSync(path.join(dataDir, 'spaces', 'records', `${ID}.json`), JSON.stringify({ version: 1, network: { mode: 'open', domains: [] } }));
|
|
93
|
+
expect(older.read(ID)).toEqual({ status: 'ok', record: expect.objectContaining({ grants: [] }) });
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('keeps the last run of the setup commands, and forgets only a setup it cannot read', () => {
|
|
97
|
+
const dataDir = temporary();
|
|
98
|
+
const records = createSpaceRecords({ dataDir, logger: quiet });
|
|
99
|
+
records.write(ID, { network: { mode: 'open' }, grants: [] });
|
|
100
|
+
const failed = { state: 'failed', total: 2, index: 1, command: 'npm install', exitCode: 1, timedOut: false, output: 'npm ERR! 403', finishedAt: '2026-09-28T10:00:00.000Z' };
|
|
101
|
+
expect(records.update(ID, { setup: failed }).record.setup).toEqual(failed);
|
|
102
|
+
expect(records.read(ID).record.setup).toEqual(failed);
|
|
103
|
+
// A value the host never writes is refused rather than kept.
|
|
104
|
+
expect(() => records.update(ID, { setup: { ...failed, secret: 'x' } })).toThrow();
|
|
105
|
+
expect(() => records.update(ID, { setup: { ...failed, output: 'x'.repeat(40 * 1024) } })).toThrow();
|
|
106
|
+
|
|
107
|
+
// A setup from a later version leaves the network and the grants readable.
|
|
108
|
+
const file = path.join(dataDir, 'spaces', 'records', `${ID}.json`);
|
|
109
|
+
fs.writeFileSync(file, JSON.stringify({ version: 1, network: { mode: 'allowlist', domains: ['a.example.com'] }, setup: { state: 'paused' } }));
|
|
110
|
+
expect(records.read(ID)).toEqual({ status: 'ok', record: expect.objectContaining({ network: { mode: 'allowlist', domains: ['a.example.com'] }, setup: null }) });
|
|
111
|
+
});
|
|
112
|
+
|
|
56
113
|
it('refuses a record that is not one, and an id that is not a space id', () => {
|
|
57
114
|
const records = createSpaceRecords({ dataDir: temporary(), logger: quiet });
|
|
58
115
|
expect(() => records.write(ID, { network: { mode: 'open', domains: ['not a name'] } })).toThrow();
|
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
IMAGE_CAT,
|
|
21
21
|
IMAGE_ONLY_PATH,
|
|
22
22
|
IMAGE_SH,
|
|
23
|
+
SPACE_IDLE_STOP_PATH,
|
|
23
24
|
SPACE_SERVER_HOST,
|
|
24
25
|
SPACE_SERVER_PORT,
|
|
25
26
|
SPACE_TOKEN_DIRECTORY,
|
|
@@ -49,6 +50,14 @@ const WRITE_TOKEN_SCRIPT = [
|
|
|
49
50
|
`&& cat > ${SPACE_TOKEN_PATH}.new && mv ${SPACE_TOKEN_PATH}.new ${SPACE_TOKEN_PATH}`,
|
|
50
51
|
].join(' ');
|
|
51
52
|
|
|
53
|
+
// The idle stop's setting, the same way: the server inside reads it once a minute, never half of it.
|
|
54
|
+
const WRITE_IDLE_STOP_SCRIPT = [
|
|
55
|
+
IMAGE_ONLY_PATH,
|
|
56
|
+
'umask 077;',
|
|
57
|
+
`mkdir -p ${SPACE_TOKEN_DIRECTORY} && chmod 700 ${SPACE_TOKEN_DIRECTORY}`,
|
|
58
|
+
`&& cat > ${SPACE_IDLE_STOP_PATH}.new && mv ${SPACE_IDLE_STOP_PATH}.new ${SPACE_IDLE_STOP_PATH}`,
|
|
59
|
+
].join(' ');
|
|
60
|
+
|
|
52
61
|
// Module resolution walks up from the project files, so one link above every project is enough.
|
|
53
62
|
// Only the plugin is linked, so project code does not quietly resolve our other packages.
|
|
54
63
|
const LINK_PLUGIN_SCRIPT = `${IMAGE_ONLY_PATH} mkdir -p "$1/node_modules/@opencode" && ln -sfn "$2" "$1/node_modules/@opencode/plugin"`;
|
|
@@ -94,6 +103,9 @@ export function createSpaceServerChannel({ exec, wait = pause, now = Date.now })
|
|
|
94
103
|
/** The token travels on stdin. It never appears in an argument, on the host or inside. */
|
|
95
104
|
const writeToken = (spaceId, token) => runFixedScript(spaceId, [IMAGE_SH, '-c', WRITE_TOKEN_SCRIPT], token, 'store the server token');
|
|
96
105
|
|
|
106
|
+
/** The idle stop's setting, `{ enabled, hours }`, for the server inside to read at its next check. */
|
|
107
|
+
const writeIdleStop = (spaceId, setting) => runFixedScript(spaceId, [IMAGE_SH, '-c', WRITE_IDLE_STOP_SCRIPT], JSON.stringify(setting), 'store the idle stop setting');
|
|
108
|
+
|
|
97
109
|
/**
|
|
98
110
|
* The host keeps no copy of the token. It reads it back when it needs it.
|
|
99
111
|
* The agent owns the file. It can make it huge, empty, or a FIFO that never ends, so the read has
|
|
@@ -186,5 +198,5 @@ export function createSpaceServerChannel({ exec, wait = pause, now = Date.now })
|
|
|
186
198
|
);
|
|
187
199
|
};
|
|
188
200
|
|
|
189
|
-
return { writeToken, readToken, linkPlugin, request, waitUntilReady };
|
|
201
|
+
return { writeToken, writeIdleStop, readToken, linkPlugin, request, waitUntilReady };
|
|
190
202
|
}
|
|
@@ -84,6 +84,19 @@ describe('space server channel: token', () => {
|
|
|
84
84
|
});
|
|
85
85
|
});
|
|
86
86
|
|
|
87
|
+
describe('space server channel: idle stop setting', () => {
|
|
88
|
+
it('writes the setting on stdin through a temporary name, beside the token', async () => {
|
|
89
|
+
const { channel, calls } = channelWith(ok());
|
|
90
|
+
await channel.writeIdleStop(ID, { enabled: true, hours: 6 });
|
|
91
|
+
|
|
92
|
+
expect(calls).toEqual([{
|
|
93
|
+
spaceId: ID,
|
|
94
|
+
argv: ['/bin/sh', '-c', 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin; umask 077; mkdir -p /home/space/.openchamber-space && chmod 700 /home/space/.openchamber-space && cat > /home/space/.openchamber-space/idle-stop.json.new && mv /home/space/.openchamber-space/idle-stop.json.new /home/space/.openchamber-space/idle-stop.json'],
|
|
95
|
+
options: { stdin: '{"enabled":true,"hours":6}' },
|
|
96
|
+
}]);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
87
100
|
describe('space server channel: plugin link', () => {
|
|
88
101
|
it('links only the plugin package, above every project of the space', async () => {
|
|
89
102
|
const { channel, calls } = channelWith(ok());
|