@openchamber/web 2.0.0 → 2.0.2
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/dist/assets/{CodeMirrorCommentWidgets-hJbl3JX4.js → CodeMirrorCommentWidgets-CmKmyb9T.js} +1 -1
- package/dist/assets/DiffView-8-PFpZ2p.js +4 -0
- package/dist/assets/FilesView-0Ie20mEk.js +17 -0
- package/dist/assets/{GitView-DY4pelsO.js → GitView-DNUSu00w.js} +4 -4
- package/dist/assets/{GuestAttachDialog-D6mHDTB2.js → GuestAttachDialog-Cv6QzjHC.js} +1 -1
- package/dist/assets/GuestSurfacePane-Bt3jB_Ye.js +1 -0
- package/dist/assets/{JsonTreeView-DEXI2NvJ.js → JsonTreeView-BlyHTRhM.js} +1 -1
- package/dist/assets/{LinearIssuesView-BTNc-4dN.js → LinearIssuesView-CSl2MUfA.js} +1 -1
- package/dist/assets/MarkdownImageGallery-2UXJF-5B.js +1 -0
- package/dist/assets/MarkdownRendererImpl-DVHqZoug.js +12 -0
- package/dist/assets/{OnboardingScreen-FJUreEgI.js → OnboardingScreen-CIdXJpfc.js} +1 -1
- package/dist/assets/{PatchDiff-C0oOkKnO.js → PatchDiff-ByvnbG5T.js} +1 -1
- package/dist/assets/{PierreDiffViewer-asuSQAsf.js → PierreDiffViewer-BXieg0mH.js} +3 -3
- package/dist/assets/{PlanView-DGbasNHP.js → PlanView-DEIi3Q7M.js} +4 -4
- package/dist/assets/PluginPane-BEPUWVho.js +3 -0
- package/dist/assets/SettingsView-CiNh-RbW.js +30 -0
- package/dist/assets/{SettingsWindow-By4cqnqy.js → SettingsWindow-BIz224xs.js} +1 -1
- package/dist/assets/{SubmoduleDiffSummary-C0P2tXLU.js → SubmoduleDiffSummary-CXKmlps9.js} +1 -1
- package/dist/assets/{ToolOutputDialog-Cayc-yIM.js → ToolOutputDialog-BsgAK8xu.js} +6 -6
- package/dist/assets/{ToolPartDiffPreview-B0jmpPQz.js → ToolPartDiffPreview-vdPObXOf.js} +2 -2
- package/dist/assets/{VirtualizedFileDiff-DsOpvWZD.js → VirtualizedFileDiff-BkFNKWS5.js} +1 -1
- package/dist/assets/{WalkthroughView-CCakjyn9.js → WalkthroughView-6oE-MuDL.js} +2 -2
- package/dist/assets/{appThemeRegistry-Bw5oTm41.js → appThemeRegistry-aHUNHoqP.js} +1 -1
- package/dist/assets/changesTree-CePHbFcj.js +1 -0
- package/dist/assets/commitAndPush-Bon9r94r.js +1 -0
- package/dist/assets/{computeVirtualFileMetrics-B-4aLf4A.js → computeVirtualFileMetrics-DKqr_1AQ.js} +2 -2
- package/dist/assets/de-CgAJ51z7.js +10 -0
- package/dist/assets/es-zGi_ravM.js +16 -0
- package/dist/assets/fr-CPfogrWx.js +14 -0
- package/dist/assets/index-CHfphYL3.js +41 -0
- package/dist/assets/{index-B6r0Kv-B.js → index-DpFttslC.js} +1 -1
- package/dist/assets/index-DvMJ3w6V.css +1 -0
- package/dist/assets/{isDiffPlainText-Ok-kgtJA.js → isDiffPlainText-kzAgpIgi.js} +1 -1
- package/dist/assets/{isFilePlainText-0dLnkYm8.js → isFilePlainText-Uj6y-jzz.js} +1 -1
- package/dist/assets/ja-DMk6wd-U.js +16 -0
- package/dist/assets/ko-BFpzJ8jc.js +16 -0
- package/dist/assets/{main-CjXBM_Xe.js → main-CbfMxn-I.js} +2 -2
- package/dist/assets/main-anyZFMIs.js +896 -0
- package/dist/assets/{markdown-shiki.worker-CahLvHIq.js → markdown-shiki.worker-Bg8SoAJJ.js} +8 -5
- package/dist/assets/markdownCore-CmM47cAE.js +7 -0
- package/dist/assets/{miniChat-DZ3OAeLN.js → miniChat-CiAKBC10.js} +2 -2
- package/dist/assets/{mobile-CH0LOIF-.js → mobile-CsT4sJwC.js} +2 -2
- package/dist/assets/patchFileDiff-DeaE08W0.js +13 -0
- package/dist/assets/pl-VdRPcZrB.js +16 -0
- package/dist/assets/pt-BR-BRys_lOW.js +16 -0
- package/dist/assets/{renderElectronMiniChatApp-CB1uLYvh.js → renderElectronMiniChatApp-C5677jy0.js} +2 -2
- package/dist/assets/renderMobileApp-D4dBBAqQ.js +25 -0
- package/dist/assets/runtimeEndpointReset-5r0hkd-2.js +27 -0
- package/dist/assets/scheduledTasksApi-BZCNJ588.js +1 -0
- package/dist/assets/{shikiHighlight-CocY80VR.js → shikiHighlight-BSUEa3yF.js} +1 -1
- package/dist/assets/{textMateThemeFromAppTheme-CF77r8FA.js → textMateThemeFromAppTheme-CsATFZIV.js} +1 -1
- package/dist/assets/tr-CA9ckNnm.js +7 -0
- package/dist/assets/uk-byjR-y8D.js +16 -0
- package/dist/assets/{useAppFontEffects-Dc5nTHi8.js → useAppFontEffects-BWQZ5DeI.js} +161 -157
- package/dist/assets/{useInlineCommentController-Dbza5c8l.js → useInlineCommentController-DlFyVfBu.js} +2 -2
- package/dist/assets/{useRootScrollLock-CSCLv396.js → useRootScrollLock-Cp1eFoM2.js} +2 -2
- package/dist/assets/{vendor-codemirror-language-data-CkmYWcRI.js → vendor-codemirror-language-data-BwwommM0.js} +2 -2
- package/dist/assets/{vendor-opencode-client-_BAHQo4R.js → vendor-opencode-client-DYdsLbCM.js} +2 -2
- package/dist/assets/vendor-zod-BSoePkpP.js +39 -0
- package/dist/assets/{web-components-nsStrYq9.js → web-components-D3Yjr4Zx.js} +1 -1
- package/dist/assets/zh-CN-DRabgxdG.js +16 -0
- package/dist/assets/zh-TW-rbY5c9UK.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 +4 -4
- package/server/index.js +82 -0
- package/server/lib/agent-tool/DOCUMENTATION.md +5 -0
- package/server/lib/agent-tool/runtime.js +34 -6
- package/server/lib/agent-tool/runtime.test.js +15 -0
- package/server/lib/browser-control/DOCUMENTATION.md +14 -2
- package/server/lib/event-stream/global-hub.js +23 -1
- package/server/lib/event-stream/global-hub.test.js +35 -0
- package/server/lib/event-stream/global-ws-bridge.js +3 -1
- package/server/lib/event-stream/upstream-reader.js +3 -2
- package/server/lib/git/DOCUMENTATION.md +1 -1
- package/server/lib/git/service.js +22 -0
- package/server/lib/git/service.test.js +8 -0
- package/server/lib/guests/DOCUMENTATION.md +4 -3
- package/server/lib/guests/catalog.js +19 -1
- package/server/lib/guests/catalog.test.js +22 -0
- package/server/lib/guests/html-styles.js +5 -3
- package/server/lib/guests/html-styles.test.js +3 -1
- package/server/lib/guests/routes.js +11 -3
- package/server/lib/guests/service.js +5 -0
- package/server/lib/guests/surface.js +55 -9
- package/server/lib/guests/surface.test.js +56 -6
- package/server/lib/notifications/DOCUMENTATION.md +10 -0
- package/server/lib/notifications/emit-route.js +118 -0
- package/server/lib/notifications/emit-route.test.js +112 -0
- package/server/lib/openchamber-control/actions.js +16 -2
- package/server/lib/openchamber-control/service.js +23 -0
- package/server/lib/openchamber-control/service.test.js +31 -0
- package/server/lib/openchamber-sessions/fork-inheritance.js +103 -0
- package/server/lib/openchamber-sessions/fork-inheritance.test.js +69 -0
- package/server/lib/openchamber-sessions/routes.js +11 -0
- package/server/lib/openchamber-sessions/routes.test.js +18 -0
- package/server/lib/opencode/DOCUMENTATION.md +37 -24
- package/server/lib/opencode/bootstrap-runtime.js +14 -1
- package/server/lib/opencode/compatibility.js +6 -3
- package/server/lib/opencode/compatibility.test.js +8 -0
- package/server/lib/opencode/config-entity-v2.test.js +27 -0
- package/server/lib/opencode/config-paths.test.js +12 -0
- package/server/lib/opencode/config-v2.d.ts +2 -0
- package/server/lib/opencode/config-v2.js +42 -0
- package/server/lib/opencode/config-v2.test.js +17 -0
- package/server/lib/opencode/core-routes.js +20 -4
- package/server/lib/opencode/core-routes.test.js +41 -0
- package/server/lib/opencode/env-runtime.js +1 -8
- package/server/lib/opencode/feature-routes-runtime.js +2 -1
- package/server/lib/opencode/lifecycle.js +30 -2
- package/server/lib/opencode/lifecycle.test.js +27 -0
- package/server/lib/opencode/managed-config-file.js +18 -6
- package/server/lib/opencode/managed-config-file.test.js +23 -7
- package/server/lib/opencode/mcp.js +7 -2
- package/server/lib/opencode/openchamber-routes.js +29 -11
- package/server/lib/opencode/openchamber-routes.test.js +18 -0
- package/server/lib/opencode/project-directory-runtime.js +8 -0
- package/server/lib/opencode/project-directory-runtime.test.js +13 -0
- package/server/lib/opencode/provider-source-route.test.js +59 -0
- package/server/lib/opencode/providers.js +3 -0
- package/server/lib/opencode/providers.test.js +62 -0
- package/server/lib/opencode/proxy.js +38 -2
- package/server/lib/opencode/routes-upgrade.test.js +1 -1
- package/server/lib/opencode/routes.js +36 -1
- package/server/lib/opencode/server-utils-runtime.js +5 -0
- package/server/lib/opencode/settings-helpers.js +10 -0
- package/server/lib/opencode/settings-helpers.test.js +2 -2
- package/server/lib/opencode/settings-registry.json +17 -0
- package/server/lib/opencode/settings-runtime.js +1 -0
- package/server/lib/opencode/shared.js +49 -4
- package/server/lib/opencode/shared.test.js +79 -1
- package/server/lib/opencode/shutdown-runtime.js +3 -0
- package/server/lib/opencode/skill-routes.js +28 -12
- package/server/lib/opencode/skill-routes.test.js +121 -1
- package/server/lib/opencode/upgrade-capability.js +0 -14
- package/server/lib/opencode/v1-migration-topup.js +21 -15
- package/server/lib/opencode/v1-migration-topup.test.js +14 -6
- package/server/lib/opencode/v2-install.d.ts +1 -0
- package/server/lib/opencode/v2-install.js +8 -7
- package/server/lib/opencode/v2-install.test.js +13 -4
- package/server/lib/opencode/watcher.js +3 -1
- package/server/lib/opencode/websearch-config.js +13 -1
- package/server/lib/quota/providers/kimi.js +1 -1
- package/server/lib/realtime-proxy.js +5 -1
- package/server/lib/realtime-proxy.test.js +29 -0
- package/server/lib/relay/tunnel-host.js +4 -1
- package/server/lib/relay/tunnel-host.test.js +11 -0
- package/server/lib/session-assist/DOCUMENTATION.md +17 -5
- package/server/lib/session-assist/runtime.js +19 -2
- package/server/lib/session-assist/runtime.test.js +8 -0
- package/server/lib/session-goal/DOCUMENTATION.md +11 -0
- package/server/lib/small-model/DOCUMENTATION.md +11 -10
- package/server/lib/small-model/index.js +27 -32
- package/server/lib/small-model/index.test.js +28 -16
- package/server/lib/spaces/DOCUMENTATION.md +381 -20
- package/server/lib/spaces/code-in-bait.js +145 -6
- package/server/lib/spaces/code-in.js +10 -150
- package/server/lib/spaces/code-in.test.js +7 -83
- package/server/lib/spaces/code-out.js +1819 -0
- package/server/lib/spaces/code-out.test.js +3945 -0
- package/server/lib/spaces/code-transfer.js +173 -0
- package/server/lib/spaces/command-stream.test.js +181 -0
- package/server/lib/spaces/dispatcher.js +670 -0
- package/server/lib/spaces/dispatcher.test.js +582 -0
- package/server/lib/spaces/gatekeeper-channel.js +2 -0
- package/server/lib/spaces/gatekeeper-channel.test.js +9 -3
- package/server/lib/spaces/gatekeeper-program.cjs +4 -1
- package/server/lib/spaces/gatekeeper-program.test.js +3 -0
- package/server/lib/spaces/hardening.js +20 -4
- package/server/lib/spaces/hardening.test.js +18 -3
- package/server/lib/spaces/host-git.js +7 -4
- package/server/lib/spaces/host-git.test.js +6 -2
- package/server/lib/spaces/host.js +304 -0
- package/server/lib/spaces/host.test.js +273 -0
- package/server/lib/spaces/journey.js +393 -0
- package/server/lib/spaces/journey.test.js +384 -0
- package/server/lib/spaces/layout.js +26 -2
- package/server/lib/spaces/manager.js +4 -3
- package/server/lib/spaces/manager.test.js +6 -0
- package/server/lib/spaces/places/code-out.docker.live.test.js +412 -0
- package/server/lib/spaces/places/contract-suite.js +36 -0
- package/server/lib/spaces/places/dispatcher.docker.live.test.js +296 -0
- package/server/lib/spaces/places/docker.js +15 -3
- package/server/lib/spaces/places/docker.test.js +33 -2
- package/server/lib/spaces/places/escape-suite.js +86 -14
- package/server/lib/spaces/places/memory-place.js +24 -1
- package/server/lib/spaces/places/registry.js +1 -1
- package/server/lib/spaces/places/server.docker.live.test.js +69 -33
- package/server/lib/spaces/places/sessions.docker.live.test.js +220 -0
- package/server/lib/spaces/routes.js +207 -0
- package/server/lib/spaces/routes.test.js +211 -0
- package/server/lib/spaces/run-command.js +261 -8
- package/server/lib/spaces/run-command.test.js +193 -3
- package/server/lib/spaces/space-events.js +153 -0
- package/server/lib/spaces/space-events.test.js +160 -0
- package/server/lib/spaces/space-records.js +98 -0
- package/server/lib/spaces/space-records.test.js +73 -0
- package/server/lib/spaces/space-sessions.js +210 -0
- package/server/lib/spaces/space-sessions.test.js +161 -0
- package/server/lib/spaces/websocket.js +329 -0
- package/server/lib/spaces/websocket.test.js +345 -0
- package/server/lib/ui-auth/ui-auth.js +8 -2
- package/server/lib/ui-auth/ui-auth.test.js +29 -0
- package/server/opencode-proxy.test.js +113 -0
- package/dist/assets/DiffView-BucGEJYa.js +0 -4
- package/dist/assets/FilesView-CQcaaAVP.js +0 -17
- package/dist/assets/GuestSurfacePane--DJ7rsmY.js +0 -1
- package/dist/assets/MarkdownImageGallery-wcUUcf8l.js +0 -1
- package/dist/assets/MarkdownRendererImpl-BPe2TVV2.js +0 -12
- package/dist/assets/PluginPane-DImv8hV2.js +0 -3
- package/dist/assets/SettingsView-DOjuOljf.js +0 -29
- package/dist/assets/commitAndPush-sGEelpjh.js +0 -1
- package/dist/assets/de-CD9utsNj.js +0 -10
- package/dist/assets/es-NNum2rX8.js +0 -16
- package/dist/assets/fr-CJLGmduQ.js +0 -14
- package/dist/assets/index-DjGPR5vW.js +0 -41
- package/dist/assets/index-fCXJ-8Qt.css +0 -1
- package/dist/assets/ja-BuddHNi6.js +0 -16
- package/dist/assets/ko-BrwMbHmf.js +0 -16
- package/dist/assets/main-CxVyUDU_.js +0 -896
- package/dist/assets/markdownCore-B6PpX409.js +0 -5
- package/dist/assets/patchFileDiff-BFRGKaGH.js +0 -13
- package/dist/assets/pl-BwN4TNlP.js +0 -16
- package/dist/assets/pt-BR-BhMtownO.js +0 -16
- package/dist/assets/renderMobileApp-nQ4sjQtb.js +0 -25
- package/dist/assets/runtimeEndpointReset-9ZGlXU4J.js +0 -27
- package/dist/assets/scheduledTasksApi-6DsL_SwM.js +0 -1
- package/dist/assets/tr-DRzvqMdi.js +0 -7
- package/dist/assets/uk-b2aYOSNh.js +0 -16
- package/dist/assets/vendor-zod-Gea9uBlY.js +0 -39
- package/dist/assets/zh-CN-DZeUThm_.js +0 -16
- package/dist/assets/zh-TW-2fRE7uc6.js +0 -16
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
const NOTIFICATION_EMIT_PATH = '/api/notifications/emit';
|
|
4
|
+
|
|
5
|
+
const RATE_WINDOW_MS = 10_000;
|
|
6
|
+
const RATE_MAX_PER_WINDOW = 10;
|
|
7
|
+
|
|
8
|
+
const optionalText = (max) => z.string().trim().min(1).max(max).optional();
|
|
9
|
+
|
|
10
|
+
const emitBodySchema = z.object({
|
|
11
|
+
title: optionalText(120),
|
|
12
|
+
body: optionalText(500),
|
|
13
|
+
tag: optionalText(128),
|
|
14
|
+
sessionId: optionalText(128),
|
|
15
|
+
directory: optionalText(4096),
|
|
16
|
+
// Opt-in for notices that matter even while OpenChamber is in front.
|
|
17
|
+
showWhenFocused: z.boolean().optional(),
|
|
18
|
+
}).refine((value) => value.title || value.body, { message: 'title or body required' });
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Raises a notification on behalf of a plugin or the agent, through the same
|
|
22
|
+
* live delivery the built-in triggers use. `kind` is always `plugin`, so a
|
|
23
|
+
* caller cannot impersonate kinds the UI treats specially. One rate window is
|
|
24
|
+
* shared by every caller, the HTTP route and the agent tool alike.
|
|
25
|
+
*
|
|
26
|
+
* `emit` resolves to `{ status, body }`: 200 delivered or skipped by settings,
|
|
27
|
+
* 400 invalid input, 429 rate limited (with `retryAfter` seconds).
|
|
28
|
+
*/
|
|
29
|
+
export const createPluginNotificationEmitter = (dependencies) => {
|
|
30
|
+
const {
|
|
31
|
+
readSettingsFromDiskMigrated,
|
|
32
|
+
emitDesktopNotification,
|
|
33
|
+
broadcastUiNotification,
|
|
34
|
+
now = Date.now,
|
|
35
|
+
} = dependencies;
|
|
36
|
+
|
|
37
|
+
let windowStartedAt = 0;
|
|
38
|
+
let windowCount = 0;
|
|
39
|
+
|
|
40
|
+
const takeRateSlot = () => {
|
|
41
|
+
const current = now();
|
|
42
|
+
if (current - windowStartedAt >= RATE_WINDOW_MS) {
|
|
43
|
+
windowStartedAt = current;
|
|
44
|
+
windowCount = 0;
|
|
45
|
+
}
|
|
46
|
+
if (windowCount >= RATE_MAX_PER_WINDOW) {
|
|
47
|
+
return Math.ceil((windowStartedAt + RATE_WINDOW_MS - current) / 1000);
|
|
48
|
+
}
|
|
49
|
+
windowCount += 1;
|
|
50
|
+
return 0;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const emit = async (input) => {
|
|
54
|
+
const parsed = emitBodySchema.safeParse(input);
|
|
55
|
+
if (!parsed.success) {
|
|
56
|
+
return { status: 400, body: { error: parsed.error.issues[0]?.message ?? 'invalid notification' } };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const retryAfter = takeRateSlot();
|
|
60
|
+
if (retryAfter > 0) {
|
|
61
|
+
return { status: 429, retryAfter, body: { error: 'too many notifications' } };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const settings = await readSettingsFromDiskMigrated();
|
|
65
|
+
if (settings.nativeNotificationsEnabled === false) {
|
|
66
|
+
return { status: 200, body: { delivered: false, reason: 'notifications-disabled' } };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const payload = {
|
|
70
|
+
title: parsed.data.title ?? 'OpenChamber',
|
|
71
|
+
body: parsed.data.body,
|
|
72
|
+
tag: parsed.data.tag,
|
|
73
|
+
kind: 'plugin',
|
|
74
|
+
sessionId: parsed.data.sessionId,
|
|
75
|
+
directory: parsed.data.directory,
|
|
76
|
+
requireHidden: parsed.data.showWhenFocused !== true && settings.notificationMode !== 'always',
|
|
77
|
+
};
|
|
78
|
+
const desktopNotificationDelivered = emitDesktopNotification(payload) === true;
|
|
79
|
+
broadcastUiNotification(payload, { desktopNotificationDelivered });
|
|
80
|
+
return { status: 200, body: { delivered: true } };
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
return { emit };
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* `POST /api/notifications/emit`. Two callers are accepted: a plugin inside
|
|
88
|
+
* the managed OpenCode (the agent-tool bearer token, checked before API auth)
|
|
89
|
+
* and any client that passes the regular API auth.
|
|
90
|
+
*/
|
|
91
|
+
export const registerNotificationEmitRoutes = (app, dependencies) => {
|
|
92
|
+
const { express, isAgentToolRequestAuthorized, emitter } = dependencies;
|
|
93
|
+
|
|
94
|
+
const jsonBody = express.json({ limit: '16kb' });
|
|
95
|
+
|
|
96
|
+
const respond = async (req, res, next) => {
|
|
97
|
+
try {
|
|
98
|
+
const result = await emitter.emit(req.body);
|
|
99
|
+
if (result.retryAfter) res.setHeader('Retry-After', String(result.retryAfter));
|
|
100
|
+
return res.status(result.status).json(result.body);
|
|
101
|
+
} catch (error) {
|
|
102
|
+
return next(error);
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
// Registered before API auth: a managed plugin carries no UI session.
|
|
108
|
+
registerPluginRoute: () => {
|
|
109
|
+
app.post(NOTIFICATION_EMIT_PATH, jsonBody, (req, res, next) => (
|
|
110
|
+
isAgentToolRequestAuthorized(req) ? respond(req, res, next) : next()
|
|
111
|
+
));
|
|
112
|
+
},
|
|
113
|
+
// Registered after API auth: everything else is an ordinary API caller.
|
|
114
|
+
registerApiRoute: () => {
|
|
115
|
+
app.post(NOTIFICATION_EMIT_PATH, jsonBody, respond);
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
import request from 'supertest';
|
|
4
|
+
import { createPluginNotificationEmitter, registerNotificationEmitRoutes } from './emit-route.js';
|
|
5
|
+
|
|
6
|
+
const PLUGIN_TOKEN = 'Bearer plugin-token';
|
|
7
|
+
const UI_TOKEN = 'Bearer ui-token';
|
|
8
|
+
|
|
9
|
+
const createApp = ({ settings = { nativeNotificationsEnabled: true, notificationMode: 'hidden-only' }, now = () => 0 } = {}) => {
|
|
10
|
+
const delivered = [];
|
|
11
|
+
const app = express();
|
|
12
|
+
const emitter = createPluginNotificationEmitter({
|
|
13
|
+
readSettingsFromDiskMigrated: async () => settings,
|
|
14
|
+
emitDesktopNotification: () => false,
|
|
15
|
+
broadcastUiNotification: (payload, options) => delivered.push({ payload, options }),
|
|
16
|
+
now,
|
|
17
|
+
});
|
|
18
|
+
const routes = registerNotificationEmitRoutes(app, {
|
|
19
|
+
express,
|
|
20
|
+
isAgentToolRequestAuthorized: (req) => req.headers.authorization === PLUGIN_TOKEN,
|
|
21
|
+
emitter,
|
|
22
|
+
});
|
|
23
|
+
routes.registerPluginRoute();
|
|
24
|
+
// Stands in for the server's API auth gate between the two registrations.
|
|
25
|
+
app.use('/api', (req, res, next) => (
|
|
26
|
+
req.headers.authorization === UI_TOKEN ? next() : res.status(401).json({ error: 'UI session missing' })
|
|
27
|
+
));
|
|
28
|
+
routes.registerApiRoute();
|
|
29
|
+
return { app, delivered };
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
describe('POST /api/notifications/emit', () => {
|
|
33
|
+
it('delivers a plugin notification without a UI session', async () => {
|
|
34
|
+
const { app, delivered } = createApp();
|
|
35
|
+
const response = await request(app)
|
|
36
|
+
.post('/api/notifications/emit')
|
|
37
|
+
.set('Authorization', PLUGIN_TOKEN)
|
|
38
|
+
.send({ title: ' Build done ', body: 'Ready', sessionId: 'ses_1' });
|
|
39
|
+
|
|
40
|
+
expect(response.status).toBe(200);
|
|
41
|
+
expect(response.body).toEqual({ delivered: true });
|
|
42
|
+
expect(delivered).toEqual([{
|
|
43
|
+
payload: {
|
|
44
|
+
title: 'Build done',
|
|
45
|
+
body: 'Ready',
|
|
46
|
+
tag: undefined,
|
|
47
|
+
kind: 'plugin',
|
|
48
|
+
sessionId: 'ses_1',
|
|
49
|
+
directory: undefined,
|
|
50
|
+
requireHidden: true,
|
|
51
|
+
},
|
|
52
|
+
options: { desktopNotificationDelivered: false },
|
|
53
|
+
}]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('accepts an ordinary API caller and rejects an unauthenticated one', async () => {
|
|
57
|
+
const { app, delivered } = createApp();
|
|
58
|
+
await request(app).post('/api/notifications/emit').set('Authorization', UI_TOKEN).send({ body: 'Hi' }).expect(200);
|
|
59
|
+
await request(app).post('/api/notifications/emit').send({ body: 'Hi' }).expect(401);
|
|
60
|
+
await request(app).post('/api/notifications/emit').set('Authorization', 'Bearer wrong').send({ body: 'Hi' }).expect(401);
|
|
61
|
+
expect(delivered.map(({ payload }) => payload.title)).toEqual(['OpenChamber']);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('never lets a caller choose the notification kind', async () => {
|
|
65
|
+
const { app, delivered } = createApp();
|
|
66
|
+
await request(app)
|
|
67
|
+
.post('/api/notifications/emit')
|
|
68
|
+
.set('Authorization', PLUGIN_TOKEN)
|
|
69
|
+
.send({ title: 'x', kind: 'opencode-restart-interrupted' })
|
|
70
|
+
.expect(200);
|
|
71
|
+
expect(delivered[0].payload.kind).toBe('plugin');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('rejects empty and oversized payloads', async () => {
|
|
75
|
+
const { app, delivered } = createApp();
|
|
76
|
+
const post = (body) => request(app).post('/api/notifications/emit').set('Authorization', PLUGIN_TOKEN).send(body);
|
|
77
|
+
await post({}).expect(400);
|
|
78
|
+
await post({ title: ' ' }).expect(400);
|
|
79
|
+
await post({ title: 'x'.repeat(121) }).expect(400);
|
|
80
|
+
await post({ body: 'x'.repeat(501) }).expect(400);
|
|
81
|
+
expect(delivered).toEqual([]);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('limits the rate and recovers after the window', async () => {
|
|
85
|
+
let clock = 0;
|
|
86
|
+
const { app, delivered } = createApp({ now: () => clock });
|
|
87
|
+
const post = () => request(app).post('/api/notifications/emit').set('Authorization', PLUGIN_TOKEN).send({ body: 'tick' });
|
|
88
|
+
for (let index = 0; index < 10; index += 1) await post().expect(200);
|
|
89
|
+
const limited = await post();
|
|
90
|
+
expect(limited.status).toBe(429);
|
|
91
|
+
expect(limited.headers['retry-after']).toBe('10');
|
|
92
|
+
clock = 10_000;
|
|
93
|
+
await post().expect(200);
|
|
94
|
+
expect(delivered).toHaveLength(11);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('follows the notifications setting and the always mode', async () => {
|
|
98
|
+
const off = createApp({ settings: { nativeNotificationsEnabled: false } });
|
|
99
|
+
const skipped = await request(off.app).post('/api/notifications/emit').set('Authorization', PLUGIN_TOKEN).send({ body: 'x' });
|
|
100
|
+
expect(skipped.body).toEqual({ delivered: false, reason: 'notifications-disabled' });
|
|
101
|
+
expect(off.delivered).toEqual([]);
|
|
102
|
+
|
|
103
|
+
const always = createApp({ settings: { nativeNotificationsEnabled: true, notificationMode: 'always' } });
|
|
104
|
+
await request(always.app).post('/api/notifications/emit').set('Authorization', PLUGIN_TOKEN).send({ body: 'x' }).expect(200);
|
|
105
|
+
expect(always.delivered[0].payload.requireHidden).toBe(false);
|
|
106
|
+
|
|
107
|
+
const focused = createApp();
|
|
108
|
+
await request(focused.app).post('/api/notifications/emit').set('Authorization', PLUGIN_TOKEN).send({ body: 'x', showWhenFocused: true }).expect(200);
|
|
109
|
+
expect(focused.delivered[0].payload.requireHidden).toBe(false);
|
|
110
|
+
await request(focused.app).post('/api/notifications/emit').set('Authorization', PLUGIN_TOKEN).send({ body: 'x', showWhenFocused: 'yes' }).expect(400);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
@@ -38,8 +38,8 @@ export const OPENCHAMBER_AGENT_TOOL_ACTIONS = Object.freeze(
|
|
|
38
38
|
);
|
|
39
39
|
|
|
40
40
|
export const OPENCHAMBER_WEB_ACTION_DEFINITIONS = Object.freeze([
|
|
41
|
-
{ action: 'browser.open', title: 'Open a page in the browser panel', description: 'Open url in the in-app browser panel; use it to look at the running app. Set viewport to mobile, tablet or desktop to lay the page out at that size' },
|
|
42
|
-
{ action: 'browser.snapshot', title: 'Read the open page', description: 'Read the open page: url, title, visible text, and interactive elements with the selectors the other browser actions accept. Pass selector to read only that part of a long page. Reports any errors the page logged' },
|
|
41
|
+
{ action: 'browser.open', title: 'Open a page in the browser panel', description: 'Open url in the in-app browser panel; use it to look at the running app. Without tabId it opens a new background tab and answers with its tabId: pass that tabId to the following actions to keep working there. With tabId it loads url in that tab. Set viewport to mobile, tablet or desktop to lay the page out at that size' },
|
|
42
|
+
{ action: 'browser.snapshot', title: 'Read the open page', description: 'Read the open page: url, title, visible text, and interactive elements with the selectors the other browser actions accept. Pass selector to read only that part of a long page. Reports any errors the page logged, and lists the open tabs with the ids tabId accepts' },
|
|
43
43
|
{ action: 'browser.click', title: 'Click on the open page', description: 'Click an element; give selector, or text to match a link or button by its visible label' },
|
|
44
44
|
{ action: 'browser.type', title: 'Type into the open page', description: 'Type value into the field matched by selector; set submit to press Enter afterwards' },
|
|
45
45
|
{ action: 'browser.scroll', title: 'Scroll the open page', description: 'Scroll the page; direction is up, down, top, or bottom, or pass selector to bring one element into view' },
|
|
@@ -76,6 +76,18 @@ export const OPENCHAMBER_MEMORY_ACTIONS = Object.freeze(
|
|
|
76
76
|
OPENCHAMBER_MEMORY_ACTION_DEFINITIONS.map(({ action }) => action),
|
|
77
77
|
);
|
|
78
78
|
|
|
79
|
+
/**
|
|
80
|
+
* Notify is its own tool so it can stay off by default and disappear
|
|
81
|
+
* completely: an agent that can page the user is a choice the user makes.
|
|
82
|
+
*/
|
|
83
|
+
export const OPENCHAMBER_NOTIFY_ACTION_DEFINITIONS = Object.freeze([
|
|
84
|
+
{ action: 'notify.send', title: 'Notify the user', description: 'Send the user a notification; requires title, body is optional. By default it appears only while the user is away from OpenChamber; set showWhenFocused only when it cannot wait' },
|
|
85
|
+
]);
|
|
86
|
+
|
|
87
|
+
export const OPENCHAMBER_NOTIFY_ACTIONS = Object.freeze(
|
|
88
|
+
OPENCHAMBER_NOTIFY_ACTION_DEFINITIONS.map(({ action }) => action),
|
|
89
|
+
);
|
|
90
|
+
|
|
79
91
|
/**
|
|
80
92
|
* Which actions each managed tool may ask for.
|
|
81
93
|
*
|
|
@@ -90,6 +102,7 @@ const ACTIONS_BY_TOOL = Object.freeze({
|
|
|
90
102
|
openchamber: OPENCHAMBER_AGENT_TOOL_ACTIONS,
|
|
91
103
|
openchamber_web: OPENCHAMBER_WEB_ACTIONS,
|
|
92
104
|
openchamber_memory: OPENCHAMBER_MEMORY_ACTIONS,
|
|
105
|
+
openchamber_notify: OPENCHAMBER_NOTIFY_ACTIONS,
|
|
93
106
|
});
|
|
94
107
|
|
|
95
108
|
const bareName = (action) => {
|
|
@@ -136,4 +149,5 @@ export const OPENCHAMBER_ALL_ACTIONS = Object.freeze([
|
|
|
136
149
|
...OPENCHAMBER_CONTROL_ACTIONS,
|
|
137
150
|
...OPENCHAMBER_WEB_ACTIONS,
|
|
138
151
|
...OPENCHAMBER_MEMORY_ACTIONS,
|
|
152
|
+
...OPENCHAMBER_NOTIFY_ACTIONS,
|
|
139
153
|
]);
|
|
@@ -154,6 +154,7 @@ export const createOpenChamberControlService = (dependencies) => {
|
|
|
154
154
|
scheduledTaskService,
|
|
155
155
|
browserControl = null,
|
|
156
156
|
fileOpen = null,
|
|
157
|
+
notifyUser = null,
|
|
157
158
|
agentMemoryActions = null,
|
|
158
159
|
// Archive lives in OpenChamber's own store now — v2 has no route that sets
|
|
159
160
|
// Session.time.archived — so an unwired store simply means nothing is archived.
|
|
@@ -365,6 +366,12 @@ export const createOpenChamberControlService = (dependencies) => {
|
|
|
365
366
|
*/
|
|
366
367
|
const browserAction = async (action, input, signal, contextDirectory, contextSessionId) => {
|
|
367
368
|
const parameters = {};
|
|
369
|
+
// Any action may name a tab; the browser that issued the id resolves it.
|
|
370
|
+
const tabId = asNonEmptyString(input.tabId);
|
|
371
|
+
if (tabId) {
|
|
372
|
+
if (tabId.length > 128) throw new OpenChamberControlError('tabId must be an id from browser.snapshot tabs', 400);
|
|
373
|
+
parameters.tabId = tabId;
|
|
374
|
+
}
|
|
368
375
|
|
|
369
376
|
const readViewport = (required) => {
|
|
370
377
|
const viewport = asNonEmptyString(input.viewport);
|
|
@@ -511,6 +518,22 @@ export const createOpenChamberControlService = (dependencies) => {
|
|
|
511
518
|
}
|
|
512
519
|
return browserAction(action, input, options.signal, contextDirectory, options.contextSessionId);
|
|
513
520
|
}
|
|
521
|
+
if (action === 'notify.send') {
|
|
522
|
+
if (!notifyUser) {
|
|
523
|
+
throw new OpenChamberControlError('Notifications are not available on this server', 503);
|
|
524
|
+
}
|
|
525
|
+
const result = await notifyUser({
|
|
526
|
+
title: input.title,
|
|
527
|
+
body: input.body,
|
|
528
|
+
showWhenFocused: input.showWhenFocused,
|
|
529
|
+
sessionId: asNonEmptyString(options.contextSessionId) || undefined,
|
|
530
|
+
directory: asNonEmptyString(contextDirectory) || undefined,
|
|
531
|
+
});
|
|
532
|
+
if (result.status !== 200) {
|
|
533
|
+
throw new OpenChamberControlError(result.body.error, result.status);
|
|
534
|
+
}
|
|
535
|
+
return result.body;
|
|
536
|
+
}
|
|
514
537
|
if (action === 'file.open') {
|
|
515
538
|
if (!fileOpen) {
|
|
516
539
|
throw new OpenChamberControlError('The file viewer is not available on this server', 503);
|
|
@@ -294,6 +294,31 @@ describe('file.open', () => {
|
|
|
294
294
|
});
|
|
295
295
|
});
|
|
296
296
|
|
|
297
|
+
describe('notify.send', () => {
|
|
298
|
+
it('sends the notice for the calling session and returns what was delivered', async () => {
|
|
299
|
+
const notifyUser = vi.fn(async () => ({ status: 200, body: { delivered: true } }));
|
|
300
|
+
const { service } = createService({ notifyUser });
|
|
301
|
+
|
|
302
|
+
const result = await service.execute('notify.send', { title: 'Done', body: 'All green', showWhenFocused: true }, '/repo', { contextSessionId: 'ses_1' });
|
|
303
|
+
|
|
304
|
+
expect(notifyUser).toHaveBeenCalledWith({ title: 'Done', body: 'All green', showWhenFocused: true, sessionId: 'ses_1', directory: '/repo' });
|
|
305
|
+
expect(result).toEqual({ delivered: true });
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
it('turns a refused notice into an error the agent can read', async () => {
|
|
309
|
+
const notifyUser = vi.fn(async () => ({ status: 429, retryAfter: 4, body: { error: 'too many notifications' } }));
|
|
310
|
+
const { service } = createService({ notifyUser });
|
|
311
|
+
|
|
312
|
+
await expect(service.execute('notify.send', { title: 'Done' }, '/repo'))
|
|
313
|
+
.rejects.toMatchObject({ statusCode: 429, message: 'too many notifications' });
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it('answers 503 when this server has no notifier wired', async () => {
|
|
317
|
+
const { service } = createService({});
|
|
318
|
+
await expect(service.execute('notify.send', { title: 'Done' }, '/repo')).rejects.toMatchObject({ statusCode: 503 });
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
|
|
297
322
|
describe('browser capture', () => {
|
|
298
323
|
const pixel = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==';
|
|
299
324
|
|
|
@@ -338,6 +363,12 @@ describe('browser capture', () => {
|
|
|
338
363
|
await expect(service.execute('browser.capture', {})).rejects.toThrow(/directory is required/);
|
|
339
364
|
});
|
|
340
365
|
|
|
366
|
+
it('passes the tab the agent named to the browser', async () => {
|
|
367
|
+
const { service, directory, request } = await createBrowserService({ base64: pixel, mime: 'image/png' });
|
|
368
|
+
await service.execute('browser.capture', { tabId: ' tab-2 ' }, directory);
|
|
369
|
+
expect(request).toHaveBeenCalledWith('browser.capture', { tabId: 'tab-2' }, expect.anything());
|
|
370
|
+
});
|
|
371
|
+
|
|
341
372
|
it('passes a label through to the browser and leaves other actions untouched', async () => {
|
|
342
373
|
const { service, directory, request } = await createBrowserService({ base64: pixel, mime: 'image/png' });
|
|
343
374
|
await service.execute('browser.capture', { label: 'before' }, directory);
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What a fork made through the OpenChamber sessions API (CLI, scheduled tasks)
|
|
5
|
+
* keeps from its source session's OpenChamber metadata. Server twin of the UI's
|
|
6
|
+
* `packages/ui/src/lib/sessionForkInheritance.ts`: UI forks call OpenCode
|
|
7
|
+
* directly and repair themselves; forks made here are repaired here.
|
|
8
|
+
*
|
|
9
|
+
* OpenCode 2.x copies the source's `metadata` wholesale into the fork, so the
|
|
10
|
+
* goal already arrives. Two repairs remain:
|
|
11
|
+
*
|
|
12
|
+
* - Source-owned links: the source's btw fork (`btwSessionID`), its review
|
|
13
|
+
* session (`reviewSessionID`), and the btw marker when the source is itself a
|
|
14
|
+
* btw side thread.
|
|
15
|
+
* - A file-backed goal objective: the text lives in a file keyed by the session
|
|
16
|
+
* id, so the fork needs its own copy. When the copy cannot be written the
|
|
17
|
+
* fork's goal carries the text inline instead.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
const NamespaceSchema = z.looseObject({
|
|
21
|
+
btwSessionID: z.unknown().optional(),
|
|
22
|
+
reviewSessionID: z.unknown().optional(),
|
|
23
|
+
kind: z.unknown().optional(),
|
|
24
|
+
goal: z.unknown().optional(),
|
|
25
|
+
});
|
|
26
|
+
const FileBackedGoalSchema = z.looseObject({ id: z.string().min(1), objectiveFile: z.literal(true) });
|
|
27
|
+
|
|
28
|
+
const openChamberNamespace = (metadata) => {
|
|
29
|
+
const parsed = z.looseObject({ openchamber: NamespaceSchema }).safeParse(metadata);
|
|
30
|
+
return parsed.success ? parsed.data.openchamber : null;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/** Merge patch (RFC 7386) removing source-owned links; null when there are none. */
|
|
34
|
+
export const sourceOwnedLinksPatch = (metadata) => {
|
|
35
|
+
const namespace = openChamberNamespace(metadata);
|
|
36
|
+
if (!namespace) return null;
|
|
37
|
+
const patch = {};
|
|
38
|
+
for (const key of ['btwSessionID', 'reviewSessionID']) {
|
|
39
|
+
if (key in namespace) patch[key] = null;
|
|
40
|
+
}
|
|
41
|
+
if (namespace.kind === 'btw') {
|
|
42
|
+
for (const key of ['kind', 'originalSessionID', 'btwBoundaryMessageID']) {
|
|
43
|
+
if (key in namespace) patch[key] = null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// A fork must not pursue the source's goal in parallel: an active goal
|
|
47
|
+
// arrives paused and the user resumes it (same rule as UI forks).
|
|
48
|
+
const goal = namespace.goal;
|
|
49
|
+
if (goal && typeof goal === 'object' && !Array.isArray(goal) && goal.status === 'active') {
|
|
50
|
+
patch.goal = { status: 'paused', statusReason: 'paused in fork' };
|
|
51
|
+
}
|
|
52
|
+
return Object.keys(patch).length > 0 ? patch : null;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const fileBackedGoal = (metadata) => {
|
|
56
|
+
const parsed = FileBackedGoalSchema.safeParse(openChamberNamespace(metadata)?.goal);
|
|
57
|
+
return parsed.success ? parsed.data : null;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Repair a freshly created fork. Best-effort: the fork already exists and is
|
|
62
|
+
* usable, so every failure is logged and swallowed.
|
|
63
|
+
*
|
|
64
|
+
* @param {object} options
|
|
65
|
+
* @param {string} options.sourceSessionID
|
|
66
|
+
* @param {{ id: string, metadata?: object }} options.fork The fork record OpenCode returned.
|
|
67
|
+
* @param {(sessionID: string) => Promise<string | null>} options.readObjective
|
|
68
|
+
* @param {(sessionID: string, content: string) => Promise<unknown>} options.writeObjective Throws on failure.
|
|
69
|
+
* @param {(sessionID: string, patch: object) => Promise<unknown>} options.writeMetadata
|
|
70
|
+
*/
|
|
71
|
+
export const applyForkInheritance = async ({ sourceSessionID, fork, readObjective, writeObjective, writeMetadata }) => {
|
|
72
|
+
const metadata = fork?.metadata;
|
|
73
|
+
const goal = fileBackedGoal(metadata);
|
|
74
|
+
let inlineObjective = null;
|
|
75
|
+
if (goal) {
|
|
76
|
+
try {
|
|
77
|
+
const content = await readObjective(sourceSessionID);
|
|
78
|
+
if (content === null) {
|
|
79
|
+
console.warn('[fork] source goal objective unavailable; fork keeps the inline fallback');
|
|
80
|
+
} else {
|
|
81
|
+
try {
|
|
82
|
+
await writeObjective(fork.id, content);
|
|
83
|
+
} catch (error) {
|
|
84
|
+
console.warn('[fork] failed to copy the goal objective file; inlining it', error?.message ?? error);
|
|
85
|
+
inlineObjective = content;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
} catch (error) {
|
|
89
|
+
console.warn('[fork] failed to read the source goal objective', error?.message ?? error);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const namespacePatch = sourceOwnedLinksPatch(metadata) ?? {};
|
|
94
|
+
if (inlineObjective !== null) {
|
|
95
|
+
namespacePatch.goal = { ...namespacePatch.goal, objective: inlineObjective, objectiveFile: false };
|
|
96
|
+
}
|
|
97
|
+
if (Object.keys(namespacePatch).length === 0) return;
|
|
98
|
+
try {
|
|
99
|
+
await writeMetadata(fork.id, { openchamber: namespacePatch });
|
|
100
|
+
} catch (error) {
|
|
101
|
+
console.warn('[fork] failed to update the fork metadata', error?.message ?? error);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { applyForkInheritance, sourceOwnedLinksPatch } from './fork-inheritance.js';
|
|
3
|
+
|
|
4
|
+
const goal = (objectiveFile) => ({ id: 'goal_1', objective: objectiveFile ? '' : 'Ship it', objectiveFile, status: 'paused' });
|
|
5
|
+
|
|
6
|
+
const harness = (metadata, overrides = {}) => {
|
|
7
|
+
const deps = {
|
|
8
|
+
readObjective: vi.fn(async () => 'Objective from file'),
|
|
9
|
+
writeObjective: vi.fn(async () => undefined),
|
|
10
|
+
writeMetadata: vi.fn(async () => undefined),
|
|
11
|
+
...overrides,
|
|
12
|
+
};
|
|
13
|
+
const run = () => applyForkInheritance({ sourceSessionID: 'ses_source', fork: { id: 'ses_fork', metadata }, ...deps });
|
|
14
|
+
return { deps, run };
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
describe('sourceOwnedLinksPatch', () => {
|
|
18
|
+
it('pauses an active goal so the fork does not pursue it in parallel', () => {
|
|
19
|
+
expect(sourceOwnedLinksPatch({ openchamber: { goal: { id: 'g', status: 'active' } } }))
|
|
20
|
+
.toEqual({ goal: { status: 'paused', statusReason: 'paused in fork' } });
|
|
21
|
+
expect(sourceOwnedLinksPatch({ openchamber: { goal: { id: 'g', status: 'complete' } } })).toBeNull();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('removes the btw and review links but keeps the goal', () => {
|
|
25
|
+
expect(sourceOwnedLinksPatch({ openchamber: { goal: goal(false), btwSessionID: 'b', reviewSessionID: 'r' } }))
|
|
26
|
+
.toEqual({ btwSessionID: null, reviewSessionID: null });
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('removes the btw marker of a btw source, keeps a review marker', () => {
|
|
30
|
+
expect(sourceOwnedLinksPatch({ openchamber: { kind: 'btw', originalSessionID: 'p', btwBoundaryMessageID: 'm' } }))
|
|
31
|
+
.toEqual({ kind: null, originalSessionID: null, btwBoundaryMessageID: null });
|
|
32
|
+
expect(sourceOwnedLinksPatch({ openchamber: { kind: 'review', originalSessionID: 'p' } })).toBeNull();
|
|
33
|
+
expect(sourceOwnedLinksPatch(undefined)).toBeNull();
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('applyForkInheritance', () => {
|
|
38
|
+
it('does nothing for an inline goal without source links', async () => {
|
|
39
|
+
const h = harness({ openchamber: { goal: goal(false) } });
|
|
40
|
+
await h.run();
|
|
41
|
+
expect(h.deps.readObjective).not.toHaveBeenCalled();
|
|
42
|
+
expect(h.deps.writeMetadata).not.toHaveBeenCalled();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('copies a file-backed objective and strips source links', async () => {
|
|
46
|
+
const h = harness({ openchamber: { goal: goal(true), btwSessionID: 'b' } });
|
|
47
|
+
await h.run();
|
|
48
|
+
expect(h.deps.readObjective).toHaveBeenCalledWith('ses_source');
|
|
49
|
+
expect(h.deps.writeObjective).toHaveBeenCalledWith('ses_fork', 'Objective from file');
|
|
50
|
+
expect(h.deps.writeMetadata).toHaveBeenCalledWith('ses_fork', { openchamber: { btwSessionID: null } });
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('inlines the objective when the file copy fails', async () => {
|
|
54
|
+
const h = harness({ openchamber: { goal: goal(true) } }, {
|
|
55
|
+
writeObjective: vi.fn(async () => { throw new Error('disk full'); }),
|
|
56
|
+
});
|
|
57
|
+
await h.run();
|
|
58
|
+
expect(h.deps.writeMetadata).toHaveBeenCalledWith('ses_fork', {
|
|
59
|
+
openchamber: { goal: { objective: 'Objective from file', objectiveFile: false } },
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('swallows a metadata write failure', async () => {
|
|
64
|
+
const h = harness({ openchamber: { reviewSessionID: 'r' } }, {
|
|
65
|
+
writeMetadata: vi.fn(async () => { throw new Error('offline'); }),
|
|
66
|
+
});
|
|
67
|
+
await expect(h.run()).resolves.toBeUndefined();
|
|
68
|
+
});
|
|
69
|
+
});
|
|
@@ -9,7 +9,9 @@ import { AUTO_MODEL_REF, isAutoModel } from '../routing/defaults.js';
|
|
|
9
9
|
import { parseScheduledCommandPrompt } from '../scheduled-tasks/runtime.js';
|
|
10
10
|
import { buildGoalIntroText, createSessionGoal } from '../session-goal/create.js';
|
|
11
11
|
import { OpenChamberControlError, asControlError } from '../openchamber-control/error.js';
|
|
12
|
+
import { readObjective, writeObjective } from '../session-goal/objectives.js';
|
|
12
13
|
import { createArchiveStore } from './archive-store.js';
|
|
14
|
+
import { applyForkInheritance } from './fork-inheritance.js';
|
|
13
15
|
import { createOpenCodeClient as defaultCreateOpenCodeClient } from './opencode-client.js';
|
|
14
16
|
import { createSessionMetadataStore, createOpenCodeSessionMetadata } from './session-metadata-store.js';
|
|
15
17
|
|
|
@@ -889,6 +891,15 @@ export const createOpenChamberSessionService = (dependencies) => {
|
|
|
889
891
|
messageID: asNonEmptyString(payload.messageId) || undefined,
|
|
890
892
|
});
|
|
891
893
|
targetSessionID = targetSession.id;
|
|
894
|
+
// Before the prompt goes out, so a goal armed by this dispatch writes
|
|
895
|
+
// over the copied objective rather than the other way round.
|
|
896
|
+
await applyForkInheritance({
|
|
897
|
+
sourceSessionID,
|
|
898
|
+
fork: targetSession,
|
|
899
|
+
readObjective,
|
|
900
|
+
writeObjective,
|
|
901
|
+
writeMetadata: (sessionID, patch) => writeMetadata(sessionID, patch, directory),
|
|
902
|
+
});
|
|
892
903
|
}
|
|
893
904
|
|
|
894
905
|
const baselineAssistantMessageId = await latestCompletedAssistantMessageID({
|
|
@@ -920,6 +920,24 @@ describe('openchamber session routes', () => {
|
|
|
920
920
|
}));
|
|
921
921
|
});
|
|
922
922
|
|
|
923
|
+
it('strips source-owned links from a fork before dispatching', async () => {
|
|
924
|
+
sessionForkMock.mockImplementationOnce(async () => ({
|
|
925
|
+
id: 'ses_fork',
|
|
926
|
+
title: 'Forked session',
|
|
927
|
+
metadata: { openchamber: { btwSessionID: 'ses_btw', reviewSessionID: 'ses_review', assist: { recap: 'kept' } } },
|
|
928
|
+
}));
|
|
929
|
+
const { app, sessionMetadataStore } = createApp();
|
|
930
|
+
sessionMetadataStore.entries.set('ses_fork', {
|
|
931
|
+
openchamber: { btwSessionID: 'ses_btw', reviewSessionID: 'ses_review', assist: { recap: 'kept' } },
|
|
932
|
+
});
|
|
933
|
+
await request(app)
|
|
934
|
+
.post('/api/openchamber/sessions/ses_source/fork')
|
|
935
|
+
.send({ directory: '/repo/app', prompt: 'Carry on', model: 'openai/gpt-5.5', agent: 'build' })
|
|
936
|
+
.expect(200);
|
|
937
|
+
|
|
938
|
+
expect(sessionMetadataStore.entries.get('ses_fork')).toEqual({ openchamber: { assist: { recap: 'kept' } } });
|
|
939
|
+
});
|
|
940
|
+
|
|
923
941
|
it('rejects send and fork requests without a prompt before calling OpenCode', async () => {
|
|
924
942
|
const { app } = createApp();
|
|
925
943
|
await request(app)
|