@yuriai/web 0.2.25 → 0.2.27-beta.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/README.md +21 -1
- package/dist/assets/AgentAvatar-BW4Zg8bo.js +1 -0
- package/dist/assets/AssistantView-Cl0ZeXwr.js +3 -0
- package/dist/assets/{DiagramView-DN_rHdr9.js → DiagramView-D6wdXRzu.js} +1 -1
- package/dist/assets/{FadeInOnReveal-555z1y1N.js → FadeInOnReveal-gOsILpSk.js} +1 -1
- package/dist/assets/{GuiPluginSurface-BPMATuPa.js → GuiPluginSurface-DOZAQKX0.js} +1 -1
- package/dist/assets/{GuiPluginWindow-CAmfr79x.js → GuiPluginWindow-tGvc4_0g.js} +1 -1
- package/dist/assets/Icon-DvUDsV1z.js +1 -0
- package/dist/assets/{ImageLoadingPlaceholder-BMsQLN_b.js → ImageLoadingPlaceholder-9xYbKqtf.js} +1 -1
- package/dist/assets/{LoadingImage-DI7sb_H_.js → LoadingImage-CiOcKPfb.js} +1 -1
- package/dist/assets/MarkdownRenderer-DZkP4UNr.js +2 -0
- package/dist/assets/{MarkdownRendererImpl-B9K5q0Lc.js → MarkdownRendererImpl-BXzPUEEo.js} +1 -1
- package/dist/assets/{MarkstreamRendererImpl-9gUEVQck.js → MarkstreamRendererImpl-B-NWd069.js} +1 -1
- package/dist/assets/MobileDetailNavigation-5U28hfYf.js +1 -0
- package/dist/assets/{MobileResizableSheet-_6OvPSMI.js → MobileResizableSheet-ZL-zPa3W.js} +1 -1
- package/dist/assets/{MobileShareBridge-BrdOiu_r.js → MobileShareBridge-JSH_Y8Ps.js} +3 -3
- package/dist/assets/MobileTabPageHeader-Bj9MIS38.js +2 -0
- package/dist/assets/MultiRunWindow-ZPz62Aqg.js +1 -0
- package/dist/assets/{OnboardingScreen-g8PDztN3.js → OnboardingScreen-BQNoy9o1.js} +1 -1
- package/dist/assets/{OverlayScrollbar-COv8rH_z.js → OverlayScrollbar-BwCFalSw.js} +1 -1
- package/dist/assets/SettingsView-B17qt8dh.js +29 -0
- package/dist/assets/SettingsWindow-CIFNHb-F.js +1 -0
- package/dist/assets/{TerminalView-BLer-ZhM.js → TerminalView-BZZ8LW4d.js} +1 -1
- package/dist/assets/{ToolOutputDialog-DI-s2TBb.js → ToolOutputDialog-BKuVpgEu.js} +2 -2
- package/dist/assets/{YuriLogo-BP4j1iqV.js → YuriLogo-C1Ty-RTB.js} +1 -1
- package/dist/assets/appThemeRegistry-DqGuzMaH.js +1 -0
- package/dist/assets/{assistant-attachment-cache-Cis9yYGt.js → assistant-attachment-cache-B9iHLlm2.js} +1 -1
- package/dist/assets/{button-Be1cqulG.js → button-DuHjvKJJ.js} +1 -1
- package/dist/assets/{capgoAdapter-B31jIkD8.js → capgoAdapter-B-tddboe.js} +1 -1
- package/dist/assets/collapsible-B8eL5nLo.js +1 -0
- package/dist/assets/{device-hj359GjJ.js → device-BjRYYXNT.js} +1 -1
- package/dist/assets/{diagram-hEIRiKol.js → diagram-ChoQDKzy.js} +1 -1
- package/dist/assets/dialog-CTYD3Sbr.css +1 -0
- package/dist/assets/dialog-CVhqZux0.js +1 -0
- package/dist/assets/{dropdown-menu.styles-xxM2ofOp.js → dropdown-menu.styles-DGvlOX_5.js} +1 -1
- package/dist/assets/{es-Clj7CS2r.js → es-B8sAsuo5.js} +3 -3
- package/dist/assets/{fileMentionTouchSelection-CTh0Mc2g.js → fileMentionTouchSelection-Dd-iSIe2.js} +2 -2
- package/dist/assets/{fileQueries-COS23nLq.js → fileQueries-5a1jkme2.js} +1 -1
- package/dist/assets/{fileReferenceActions-Cus9oSW7.js → fileReferenceActions-BAoEu4z8.js} +1 -1
- package/dist/assets/{fr-dmrFQLCO.js → fr-BPpW8T-l.js} +3 -3
- package/dist/assets/{gitApi-deADOzlN.js → gitApi-D43bAQh1.js} +1 -1
- package/dist/assets/{gitApiHttp-BBRKl9WX.js → gitApiHttp-DHrRqnYh.js} +3 -3
- package/dist/assets/{gui-BHEKelUx.js → gui-9enrif_1.js} +1 -1
- package/dist/assets/{host-DrqLUbqt.js → host-D1EpSUMR.js} +1 -1
- package/dist/assets/image-loading-Dq1g_wpm.js +1 -0
- package/dist/assets/{input-DTdHYZ6r.js → input-E_oYiXZT.js} +1 -1
- package/dist/assets/insertionBoundaries-BQjiZAmZ.js +1 -0
- package/dist/assets/{ja-Co0dxiN_.js → ja-DEoFDi7P.js} +3 -3
- package/dist/assets/{ko-BltElAGq.js → ko-B45xHN6I.js} +3 -3
- package/dist/assets/main-C6MDfyZy.js +2 -0
- package/dist/assets/main-u5iQScJs.js +169 -0
- package/dist/assets/{markdownHeightCache-DnR01ECa.js → markdownHeightCache-heNXHdzb.js} +1 -1
- package/dist/assets/{markdownTheme-DHoKvD6O.js → markdownTheme-hpURsgju.js} +1 -1
- package/dist/assets/{mcpQueries-Cpb22_60.js → mcpQueries-BqKVe4Su.js} +1 -1
- package/dist/assets/miniChat-Dhfn71Re.js +2 -0
- package/dist/assets/mobile-DSVdPfcG.js +2 -0
- package/dist/assets/{mobileBackNavigation-BibkyIeB.js → mobileBackNavigation-DHxFQ7SY.js} +1 -1
- package/dist/assets/{multirun-CPvUnP91.js → multirun-CPOzaEQj.js} +2 -2
- package/dist/assets/{native-BMAEg2E6.js → native-De1DIkLZ.js} +1 -1
- package/dist/assets/{native-config-DA8S8EqV.js → native-config-oauL2ekt.js} +1 -1
- package/dist/assets/{persistence-CBL0OOdj.js → persistence-JyLTFv27.js} +1 -1
- package/dist/assets/{pl-qd6Wnl7H.js → pl-Bm3Je_AX.js} +3 -3
- package/dist/assets/projectMeta-YDMdD3AP.js +1 -0
- package/dist/assets/{pt-BR-CsOFfQXS.js → pt-BR-BiQwFHhL.js} +3 -3
- package/dist/assets/{react-BYRXYlGz.js → react-CURmDhtB.js} +1 -1
- package/dist/assets/{readiness-RiznV1AX.js → readiness-BveVUOIb.js} +1 -1
- package/dist/assets/{renderElectronMiniChatApp-DdGqTh4X.js → renderElectronMiniChatApp-NC8iRPyy.js} +2 -2
- package/dist/assets/renderMobileApp-D5ai1i6y.js +28 -0
- package/dist/assets/{runtime-file-download-B_c7JBoS.js → runtime-file-download-CcNIVp7u.js} +1 -1
- package/dist/assets/{runtime-file-resource-DpTxBCsp.js → runtime-file-resource-6np6lzoV.js} +1 -1
- package/dist/assets/runtimeConfig-BpcgdIV9.js +2 -0
- package/dist/assets/runtimeEndpointReset-BL9hV6J1.css +1 -0
- package/dist/assets/runtimeEndpointReset-ByKa_0nu.js +149 -0
- package/dist/assets/scheduledTaskNavigation-CZfAnPD-.js +1 -0
- package/dist/assets/sessionChangeSummary-8C8nv-EP.css +1 -0
- package/dist/assets/sessionChangeSummary-D6NlE_5k.js +1 -0
- package/dist/assets/{sessionLookup-B7kUOIpO.js → sessionLookup-C70EoX-V.js} +1 -1
- package/dist/assets/{sortable-tabs-strip-CQZ16rbO.js → sortable-tabs-strip-aKussO2c.js} +1 -1
- package/dist/assets/speechSettingsQueries-B6qf-mCh.js +4 -0
- package/dist/assets/src-NKCZI3Lg.css +2 -0
- package/dist/assets/{store-7wItZZ7_.js → store-BeD2keas.js} +4 -4
- package/dist/assets/{terminalApi-C0_GUlPR.js → terminalApi-B5o8UqWA.js} +1 -1
- package/dist/assets/{textarea-BU8JtaSQ.js → textarea-DmKaqHSb.js} +1 -1
- package/dist/assets/{tooltip-C8DhNU1d.js → tooltip-DUKu4nC6.js} +1 -1
- package/dist/assets/{touch-gestures-zegXCZEP.js → touch-gestures-K2_ZOkKW.js} +1 -1
- package/dist/assets/types-DRTMRW4R.js +2 -0
- package/dist/assets/{uk-BNrdZFvK.js → uk-CYZXWdd7.js} +3 -3
- package/dist/assets/{useAppFontEffects-Bs6kVjrE.js → useAppFontEffects-BiQf6tgZ.js} +55 -51
- package/dist/assets/useAppFontEffects-RpgNbjA4.css +1 -0
- package/dist/assets/{useAssistantUIStore-NlYBroMc.js → useAssistantUIStore-C9FPlYQz.js} +1 -1
- package/dist/assets/{useDesktopWindowControlsLayout-COBTly74.js → useDesktopWindowControlsLayout-DJxNmCWb.js} +1 -1
- package/dist/assets/useEffectiveDirectory-B8q00InF.js +1 -0
- package/dist/assets/{useFontPreferences-DQAS94Z4.js → useFontPreferences-DMiP_Yy-.js} +1 -1
- package/dist/assets/useMobileNavigationStore-Bn4SaalY.js +418 -0
- package/dist/assets/{useThemeSystem-8jhE4Zyk.js → useThemeSystem-CGF5jnIh.js} +1 -1
- package/dist/assets/{useUIStore-96W41KFb.js → useUIStore-DI7va1HH.js} +1 -1
- package/dist/assets/{useWorkerHighlightedLines-BZUO0Si5.js → useWorkerHighlightedLines-B6lWNk01.js} +1 -1
- package/dist/assets/{vendor-base-ui-CdzJ6My_.js → vendor-base-ui-BQjbsJ43.js} +1 -1
- package/dist/assets/{vendor-cmdk-RDxC-9Iz.js → vendor-cmdk-C3oBVL9X.js} +1 -1
- package/dist/assets/{vendor-dnd-kit-accessibility-COSwgjFs.js → vendor-dnd-kit-accessibility-DxpFzZhi.js} +1 -1
- package/dist/assets/{vendor-dnd-kit-core-5pNnzVdw.js → vendor-dnd-kit-core-B3g3zWQd.js} +1 -1
- package/dist/assets/{vendor-dnd-kit-sortable-Dm50I0m7.js → vendor-dnd-kit-sortable-BKewx0Py.js} +1 -1
- package/dist/assets/{vendor-framer-motion-BQ3-sJMO.js → vendor-framer-motion-C1twy-GF.js} +1 -1
- package/dist/assets/{vendor-markstream-react-Bm0cO70Z.js → vendor-markstream-react-BlbOg72y.js} +3 -3
- package/dist/assets/vendor-motion-BmLx2V78.js +1 -0
- package/dist/assets/{vendor-opencode-client-fxfj4IA_.js → vendor-opencode-client-UL_TecvQ.js} +1 -1
- package/dist/assets/{vendor-opencode-plugin-CQ5ogY33.js → vendor-opencode-plugin-Bw7Q6Nv9.js} +1 -1
- package/dist/assets/{vendor-pierre-diffs-DPBtNDJ5.js → vendor-pierre-diffs-Bj9Nqn6w.js} +1 -1
- package/dist/assets/{vendor-react-BP2cRVqX.js → vendor-react-D5d-V1vN.js} +1 -1
- package/dist/assets/{vendor-react-drawio-Ck9ZKdNN.js → vendor-react-drawio-C-iPmmED.js} +1 -1
- package/dist/assets/{vendor-reactuses-core-BJ-jJ4YM.js → vendor-reactuses-core-BcebBLwF.js} +1 -1
- package/dist/assets/{vendor-solid-js-Dmt4_-Zk.js → vendor-solid-js-0T254UHG.js} +1 -1
- package/dist/assets/{vendor-sonner-ZS8XIOVl.js → vendor-sonner-r2EJnfoo.js} +1 -1
- package/dist/assets/{vendor-stream-diffs-TgIdH4lX.js → vendor-stream-diffs-BnLmeqg2.js} +2 -2
- package/dist/assets/{vendor-tanstack-react-query-CdqCAt40.js → vendor-tanstack-react-query-C4SG3JZB.js} +1 -1
- package/dist/assets/vendor-tanstack-react-virtual-BeZn51tF.js +1 -0
- package/dist/assets/{vendor-zustand-o69pgTEK.js → vendor-zustand-B43HCgwC.js} +1 -1
- package/dist/assets/{zh-CN-DMvJXS23.js → zh-CN-CMLBHipA.js} +3 -3
- package/dist/assets/{zh-TW-D0wsKLs1.js → zh-TW-CJlmnnhK.js} +3 -3
- package/dist/index.html +18 -18
- package/dist/mini-chat.html +18 -18
- package/dist/mobile.html +18 -18
- package/package.json +1 -1
- package/server/lib/assistants/DOCUMENTATION.md +58 -37
- package/server/lib/assistants/cards.js +4 -0
- package/server/lib/assistants/cards.test.js +11 -1
- package/server/lib/assistants/contact-attachments.http.test.js +31 -7
- package/server/lib/assistants/contact-store.js +54 -27
- package/server/lib/assistants/contact-store.test.js +69 -8
- package/server/lib/assistants/contact-tools.js +68 -84
- package/server/lib/assistants/contact-tools.test.js +85 -42
- package/server/lib/assistants/harness.js +6 -3
- package/server/lib/assistants/harness.test.js +25 -10
- package/server/lib/assistants/service.js +276 -145
- package/server/lib/assistants/service.test.js +954 -54
- package/server/lib/assistants/yuri-project.http.test.js +47 -0
- package/server/lib/assistants/yuri-tools.js +1 -1
- package/server/lib/assistants/yuri-tools.test.js +3 -2
- package/server/lib/git/DOCUMENTATION.md +6 -0
- package/server/lib/git/branches.test.js +18 -0
- package/server/lib/git/service.js +6 -3
- package/server/lib/llm/DOCUMENTATION.md +15 -7
- package/server/lib/llm/bridge-client.js +58 -34
- package/server/lib/llm/bridge-client.test.js +123 -16
- package/server/lib/notifications/APNS.md +7 -4
- package/server/lib/notifications/DOCUMENTATION.md +3 -1
- package/server/lib/notifications/apns-runtime.js +14 -8
- package/server/lib/notifications/apns-runtime.test.js +28 -0
- package/server/lib/notifications/family-completion-gate.js +37 -31
- package/server/lib/notifications/family-completion-gate.test.js +32 -0
- package/server/lib/opencode/DOCUMENTATION.md +6 -0
- package/server/lib/opencode/hmr-isolation.test.js +56 -0
- package/server/lib/opencode/lifecycle.js +23 -27
- package/server/lib/opencode/network-runtime.js +8 -14
- package/server/lib/opencode/network-runtime.test.js +4 -4
- package/server/lib/opencode/proxy.js +2 -1
- package/server/lib/opencode/proxy.test.js +1 -0
- package/server/lib/relay/DOCUMENTATION.md +8 -1
- package/server/lib/relay/relay-server.e2e.test.ts +102 -1
- package/server/lib/relay/service.js +1 -1
- package/server/lib/relay/service.test.js +14 -1
- package/server/lib/request-cors.js +1 -1
- package/server/lib/request-cors.test.js +13 -1
- package/server/lib/session-goal/DOCUMENTATION.md +3 -0
- package/server/lib/session-goal/runtime.js +0 -4
- package/server/lib/session-index/DOCUMENTATION.md +18 -4
- package/server/lib/session-index/event-ingest.js +6 -0
- package/server/lib/session-index/event-ingest.test.js +10 -0
- package/server/lib/session-index/service.js +89 -13
- package/server/lib/session-index/service.test.js +144 -1
- package/server/lib/session-metadata/routes.test.js +44 -0
- package/server/lib/tts/DOCUMENTATION.md +24 -2
- package/server/lib/tts/direct-speech.test.js +28 -3
- package/server/opencode-proxy.test.js +5 -5
- package/dist/assets/AgentAvatar--wjVXk3I.js +0 -1
- package/dist/assets/AssistantView-CSpW-4g6.js +0 -3
- package/dist/assets/Icon-Bn4Eec3Y.js +0 -1
- package/dist/assets/MarkdownRenderer-DP2a0lfG.js +0 -2
- package/dist/assets/MobileDetailNavigation-C9NHt08G.js +0 -1
- package/dist/assets/MobileTabPageHeader-qe23xT2-.js +0 -2
- package/dist/assets/MultiRunWindow-CvHADKf9.js +0 -1
- package/dist/assets/SettingsView-DK8Jkkhd.js +0 -29
- package/dist/assets/SettingsWindow-EP8KZok8.js +0 -1
- package/dist/assets/appThemeRegistry-BnQF2GlY.js +0 -1
- package/dist/assets/collapsible-DqfguRMp.js +0 -1
- package/dist/assets/dialog-BS-KgM5J.js +0 -1
- package/dist/assets/image-loading-DJZLvhMd.js +0 -1
- package/dist/assets/insertionBoundaries-h2be7xbf.js +0 -1
- package/dist/assets/main-BpBrYqtJ.js +0 -169
- package/dist/assets/main-C2QklNP4.js +0 -2
- package/dist/assets/miniChat-C-9deNrt.js +0 -2
- package/dist/assets/mobile-DW7tEL-k.js +0 -2
- package/dist/assets/projectMeta-BwAS96mf.js +0 -1
- package/dist/assets/renderMobileApp-Bipi0hKv.js +0 -28
- package/dist/assets/runtimeConfig-Dx7Y6Yvp.js +0 -2
- package/dist/assets/runtimeEndpointReset-0buInzjw.js +0 -149
- package/dist/assets/runtimeEndpointReset-HYSytKO-.css +0 -1
- package/dist/assets/scheduledTaskNavigation-CYozx15H.js +0 -1
- package/dist/assets/sessionChangeSummary-BGisROOB.js +0 -1
- package/dist/assets/sessionChangeSummary-CRYRvpy7.css +0 -1
- package/dist/assets/speechSettingsQueries-CWXTghOr.js +0 -4
- package/dist/assets/src-BFsc_2gs.css +0 -2
- package/dist/assets/types-C9aO7yfx.js +0 -2
- package/dist/assets/useEffectiveDirectory-Bg0LmEqP.js +0 -1
- package/dist/assets/useMobileNavigationStore-hhiMBg5n.js +0 -418
- package/dist/assets/vendor-motion-B4xmxmaP.js +0 -1
- package/dist/assets/vendor-tanstack-react-virtual-B_EO--9t.js +0 -1
|
@@ -355,13 +355,200 @@ describe('assistants service', () => {
|
|
|
355
355
|
service.close();
|
|
356
356
|
});
|
|
357
357
|
|
|
358
|
+
it('renames itself through the real unified settings tool loop and preserves the contact across restart', async () => {
|
|
359
|
+
const directory = root();
|
|
360
|
+
const tips = [];
|
|
361
|
+
const create = vi.fn();
|
|
362
|
+
let round = 0;
|
|
363
|
+
let revisionBeforeNoop;
|
|
364
|
+
let snapshotBeforeNoop;
|
|
365
|
+
let service = setup(directory, { create }, {
|
|
366
|
+
onRevisionTip: (tip) => tips.push(tip),
|
|
367
|
+
runContactTurn: realRunContactTurn,
|
|
368
|
+
createChatCompletion: async ({ body }) => {
|
|
369
|
+
round++;
|
|
370
|
+
const names = body.tools.map((tool) => tool.function.name);
|
|
371
|
+
if (round === 1) {
|
|
372
|
+
expect(names).toContain('update_assistant_settings');
|
|
373
|
+
expect(names).not.toContain('update_name');
|
|
374
|
+
expect(names).not.toContain('update_avatar');
|
|
375
|
+
expect(names).not.toContain('update_default_prompt');
|
|
376
|
+
return toolBatch([['get_assistant_settings', {}]]);
|
|
377
|
+
}
|
|
378
|
+
expect(names).toContain('update_assistant_settings');
|
|
379
|
+
if (round === 2) return toolBatch([['update_assistant_settings', { settings: { name: ' 小薄荷 ' } }, 'rename-self']]);
|
|
380
|
+
if (round === 3) {
|
|
381
|
+
expect(body.messages.find((message) => message.tool_call_id === 'rename-self')?.content).toContain('小薄荷');
|
|
382
|
+
const snapshot = service.snapshot();
|
|
383
|
+
revisionBeforeNoop = snapshot.assistants.find((item) => item.name === '小薄荷').revision;
|
|
384
|
+
snapshotBeforeNoop = snapshot.revision;
|
|
385
|
+
expect(tips.some((tip) => tip.revision === snapshotBeforeNoop)).toBe(true);
|
|
386
|
+
return toolBatch([['update_assistant_settings', { settings: { name: '小薄荷' } }, 'same-name']]);
|
|
387
|
+
}
|
|
388
|
+
if (round === 4) {
|
|
389
|
+
expect(body.messages.find((message) => message.tool_call_id === 'same-name')?.content).toMatch(/updated.*false/);
|
|
390
|
+
expect(service.snapshot().revision).toBe(snapshotBeforeNoop);
|
|
391
|
+
return toolBatch([['get_assistant_settings', {}, 'read-renamed']]);
|
|
392
|
+
}
|
|
393
|
+
expect(body.messages.find((message) => message.tool_call_id === 'read-renamed')?.content).toContain('name: 小薄荷');
|
|
394
|
+
return { text: '以后我就叫小薄荷。' };
|
|
395
|
+
},
|
|
396
|
+
});
|
|
397
|
+
const own = service.createAssistant({ ...assistantInput, defaultPrompt: 'Research persona' });
|
|
398
|
+
const peer = service.createAssistant({ ...assistantInput, name: 'Peer' });
|
|
399
|
+
try {
|
|
400
|
+
const sent = await settleSend(service, own.id, { messageID: 'rename-self', text: '我要让你给自己改个名字。' });
|
|
401
|
+
expect(sent.settled.status).toBe('complete');
|
|
402
|
+
expect(round).toBe(5);
|
|
403
|
+
const renamed = service.snapshot().assistants.find((item) => item.id === own.id);
|
|
404
|
+
expect(renamed).toMatchObject({ id: own.id, name: '小薄荷', revision: own.revision + 1, avatarSeed: own.avatarSeed, defaultPrompt: own.defaultPrompt, providerID: own.providerID, modelID: own.modelID, workspacePath: own.workspacePath, sessionID: own.sessionID, sessionGeneration: own.sessionGeneration });
|
|
405
|
+
expect(renamed.revision).toBe(revisionBeforeNoop);
|
|
406
|
+
expect(service.snapshot().assistants.find((item) => item.id === peer.id)).toEqual(peer);
|
|
407
|
+
expect(create).not.toHaveBeenCalled();
|
|
408
|
+
const messages = service.contactMessages(own.id).messages;
|
|
409
|
+
expect(messages[0].text).toBe(greetingText);
|
|
410
|
+
expect(messages.at(-1).text).toBe('以后我就叫小薄荷。');
|
|
411
|
+
service.close();
|
|
412
|
+
service = setup(directory, {}, { runContactTurn: async ({ assistant }) => {
|
|
413
|
+
expect(assistant.name).toBe('小薄荷');
|
|
414
|
+
return { text: '名字还在。', bubbles: ['名字还在。'] };
|
|
415
|
+
} });
|
|
416
|
+
expect(service.snapshot().assistants.find((item) => item.id === own.id)).toEqual(renamed);
|
|
417
|
+
expect(service.contactMessages(own.id).messages).toEqual(messages);
|
|
418
|
+
expect((await settleSend(service, own.id, { messageID: 'name-after-restart', text: '你叫什么?' })).settled.status).toBe('complete');
|
|
419
|
+
} finally {
|
|
420
|
+
service.close();
|
|
421
|
+
fs.rmSync(directory, { recursive: true, force: true });
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
it('keeps the stored name and revision tip unchanged when a self rename write fails', async () => {
|
|
426
|
+
const directory = root();
|
|
427
|
+
const tips = [];
|
|
428
|
+
let result;
|
|
429
|
+
const service = setup(directory, {}, {
|
|
430
|
+
onRevisionTip: (tip) => tips.push(tip),
|
|
431
|
+
runContactTurn: async ({ tools }) => {
|
|
432
|
+
const snapshot = service.snapshot();
|
|
433
|
+
await Promise.resolve();
|
|
434
|
+
const tipCount = tips.length;
|
|
435
|
+
result = await tools.find((tool) => tool.name === 'update_assistant_settings').execute('rename-failed', { settings: { name: 'Not saved' } });
|
|
436
|
+
await Promise.resolve();
|
|
437
|
+
expect(service.snapshot().revision).toBe(snapshot.revision);
|
|
438
|
+
expect(tips).toHaveLength(tipCount);
|
|
439
|
+
return { text: '没改成功。', bubbles: ['没改成功。'] };
|
|
440
|
+
},
|
|
441
|
+
});
|
|
442
|
+
const own = service.createAssistant(assistantInput);
|
|
443
|
+
const db = new (require('better-sqlite3'))(path.join(directory, 'assistants.sqlite'));
|
|
444
|
+
try {
|
|
445
|
+
db.exec(`CREATE TRIGGER reject_name BEFORE UPDATE OF name ON assistant_v2
|
|
446
|
+
BEGIN SELECT RAISE(ABORT, 'name write failed'); END`);
|
|
447
|
+
await settleSend(service, own.id, { messageID: 'failed-rename', text: '给自己改名' });
|
|
448
|
+
expect(result).toMatchObject({ isError: true, details: { error: 'update_assistant_settings_failed' } });
|
|
449
|
+
expect(service.snapshot().assistants.find((item) => item.id === own.id)).toMatchObject({ name: own.name, revision: own.revision, updatedAt: own.updatedAt, avatarSeed: own.avatarSeed, sessionID: own.sessionID });
|
|
450
|
+
} finally {
|
|
451
|
+
db.close();
|
|
452
|
+
service.close();
|
|
453
|
+
fs.rmSync(directory, { recursive: true, force: true });
|
|
454
|
+
}
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
it.each([false, true])('rolls back a failed workspace settings write and reports cleanup failure=%s', async (cleanupFails) => {
|
|
458
|
+
const directory = root();
|
|
459
|
+
const project = path.join(directory, 'project');
|
|
460
|
+
fs.mkdirSync(project);
|
|
461
|
+
const remove = vi.fn(async () => {
|
|
462
|
+
if (cleanupFails) throw new Error('remove failed');
|
|
463
|
+
});
|
|
464
|
+
let created = 0;
|
|
465
|
+
let result;
|
|
466
|
+
const service = setup(directory, { create: async () => ({ id: `settings-session-${++created}` }), remove }, {
|
|
467
|
+
runContactTurn: async ({ tools }) => {
|
|
468
|
+
result = await tools.find((tool) => tool.name === 'update_assistant_settings').execute('workspace-write', { settings: { name: 'Not saved', workspacePath: project } });
|
|
469
|
+
return { text: '没改成功。', bubbles: ['没改成功。'] };
|
|
470
|
+
},
|
|
471
|
+
});
|
|
472
|
+
const own = service.createAssistant(assistantInput);
|
|
473
|
+
await service.ensure(own.id);
|
|
474
|
+
const before = service.snapshot().assistants[0];
|
|
475
|
+
const db = new (require('better-sqlite3'))(path.join(directory, 'assistants.sqlite'));
|
|
476
|
+
try {
|
|
477
|
+
db.exec(`CREATE TRIGGER reject_workspace BEFORE UPDATE OF workspace_path ON assistant_v2
|
|
478
|
+
BEGIN SELECT RAISE(ABORT, 'workspace write failed'); END`);
|
|
479
|
+
await settleSend(service, own.id, { messageID: 'workspace-write', text: '更新工作目录' });
|
|
480
|
+
expect(result).toMatchObject({ isError: true, details: { error: cleanupFails ? 'upstream_error' : 'update_assistant_settings_failed' } });
|
|
481
|
+
expect(remove).toHaveBeenCalledWith({ sessionID: 'settings-session-2' });
|
|
482
|
+
expect(service.snapshot().assistants[0]).toMatchObject({ name: before.name, revision: before.revision, workspacePath: before.workspacePath, sessionID: before.sessionID, sessionGeneration: before.sessionGeneration, historySessionCount: 0 });
|
|
483
|
+
expect(db.prepare('SELECT COUNT(*) AS count FROM assistant_session_history').get().count).toBe(0);
|
|
484
|
+
} finally { db.close(); service.close(); fs.rmSync(directory, { recursive: true, force: true }); }
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
it('updates every editable setting together, preserves omitted fields and supports clearing defaults', async () => {
|
|
488
|
+
const directory = root();
|
|
489
|
+
const workspacePath = path.join(directory, 'project');
|
|
490
|
+
fs.mkdirSync(workspacePath);
|
|
491
|
+
let phase = 0;
|
|
492
|
+
let expected;
|
|
493
|
+
let turnFailure;
|
|
494
|
+
const create = vi.fn(async () => ({ id: crypto.randomUUID() }));
|
|
495
|
+
let service = setup(directory, { create }, {
|
|
496
|
+
runContactTurn: async ({ tools, assistant }) => {
|
|
497
|
+
try {
|
|
498
|
+
const update = tools.find((tool) => tool.name === 'update_assistant_settings');
|
|
499
|
+
if (phase === 0) {
|
|
500
|
+
const settings = { name: 'Mint', avatarSeed: 'saved-recipe', defaultPrompt: 'Research in Chinese', providerID: 'new-provider', modelID: 'new-model', agent: 'general', variant: 'high', mode: 'stateless', workspacePath: fs.realpathSync(workspacePath) };
|
|
501
|
+
const result = await update.execute('all-settings', { settings });
|
|
502
|
+
expect(result.isError).not.toBe(true);
|
|
503
|
+
expect(result.details).toMatchObject({ updated: true, settings });
|
|
504
|
+
expected = settings;
|
|
505
|
+
expect(create.mock.calls[0][0]).toMatchObject({ title: '[Assistant] Mint', model: { id: 'new-model', providerID: 'new-provider', variant: 'high' }, agent: 'general' });
|
|
506
|
+
} else if (phase === 1) {
|
|
507
|
+
expect(assistant).toMatchObject(expected);
|
|
508
|
+
const before = service.snapshot().assistants.find((item) => item.id === assistant.id);
|
|
509
|
+
const invalid = await update.execute('invalid-workspace', { settings: { name: 'Do not save', workspacePath: path.dirname(directory) } });
|
|
510
|
+
expect(invalid).toMatchObject({ isError: true, details: { error: 'workspace_forbidden' } });
|
|
511
|
+
expect(service.snapshot().assistants.find((item) => item.id === assistant.id)).toEqual(before);
|
|
512
|
+
const cleared = { defaultPrompt: '', agent: null, variant: null, mode: 'continuous', workspacePath: null };
|
|
513
|
+
const result = await update.execute('clear-defaults', { settings: cleared });
|
|
514
|
+
expected = { ...expected, ...cleared };
|
|
515
|
+
expect(result.details).toMatchObject({ updated: true, settings: expected });
|
|
516
|
+
} else {
|
|
517
|
+
expect(assistant).toMatchObject(expected);
|
|
518
|
+
await update.execute('set-variant', { settings: { variant: 'high' } });
|
|
519
|
+
const changed = await update.execute('change-model', { settings: { modelID: 'another-model' } });
|
|
520
|
+
expect(changed.details.settings).toMatchObject({ modelID: 'another-model', variant: null, name: 'Mint', avatarSeed: 'saved-recipe' });
|
|
521
|
+
}
|
|
522
|
+
phase++;
|
|
523
|
+
return { text: 'Saved.', bubbles: ['Saved.'] };
|
|
524
|
+
} catch (error) { turnFailure = error; throw error; }
|
|
525
|
+
},
|
|
526
|
+
});
|
|
527
|
+
const own = service.createAssistant(assistantInput);
|
|
528
|
+
const peer = service.createAssistant({ ...assistantInput, name: 'Peer' });
|
|
529
|
+
try {
|
|
530
|
+
for (let index = 0; index < 3; index++) {
|
|
531
|
+
const sent = await settleSend(service, own.id, { messageID: `settings-${index}`, text: '更新设置' });
|
|
532
|
+
if (turnFailure) throw turnFailure;
|
|
533
|
+
expect(sent.settled.status).toBe('complete');
|
|
534
|
+
}
|
|
535
|
+
expect(phase).toBe(3);
|
|
536
|
+
expect(service.snapshot().assistants.find((item) => item.id === peer.id)).toEqual(peer);
|
|
537
|
+
const saved = service.snapshot().assistants.find((item) => item.id === own.id);
|
|
538
|
+
expect(saved.historySessionCount).toBe(1);
|
|
539
|
+
service.close();
|
|
540
|
+
service = setup(directory);
|
|
541
|
+
expect(service.snapshot().assistants.find((item) => item.id === own.id)).toEqual(saved);
|
|
542
|
+
} finally { service.close(); fs.rmSync(directory, { recursive: true, force: true }); }
|
|
543
|
+
});
|
|
544
|
+
|
|
358
545
|
it('persists self avatar tool changes, supports restore and leaves peers untouched', async () => {
|
|
359
546
|
const directory = root();
|
|
360
547
|
let result;
|
|
361
|
-
let params = {};
|
|
548
|
+
let params = { settings: { avatarSeed: null } };
|
|
362
549
|
const service = setup(directory, {}, {
|
|
363
550
|
runContactTurn: async ({ tools }) => {
|
|
364
|
-
result = await tools.find((tool) => tool.name === '
|
|
551
|
+
result = await tools.find((tool) => tool.name === 'update_assistant_settings').execute('avatar', params);
|
|
365
552
|
return { text: 'done', bubbles: ['done'] };
|
|
366
553
|
},
|
|
367
554
|
});
|
|
@@ -369,10 +556,10 @@ describe('assistants service', () => {
|
|
|
369
556
|
const peer = service.createAssistant({ ...assistantInput, name: 'Peer' });
|
|
370
557
|
try {
|
|
371
558
|
await settleSend(service, own.id, { messageID: 'avatar_random', parts: [{ type: 'text', text: '给自己换个头像' }] });
|
|
372
|
-
expect(result.details).
|
|
373
|
-
expect(result.details.avatarSeed).not.toBe(own.avatarSeed);
|
|
374
|
-
expect(service.snapshot().assistants.find((a) => a.id === own.id).avatarSeed).toBe(result.details.avatarSeed);
|
|
375
|
-
params = {
|
|
559
|
+
expect(result.details.updated).toBe(true);
|
|
560
|
+
expect(result.details.settings.avatarSeed).not.toBe(own.avatarSeed);
|
|
561
|
+
expect(service.snapshot().assistants.find((a) => a.id === own.id).avatarSeed).toBe(result.details.settings.avatarSeed);
|
|
562
|
+
params = { settings: { avatarSeed: own.avatarSeed } };
|
|
376
563
|
await settleSend(service, own.id, { messageID: 'avatar_restore', parts: [{ type: 'text', text: '恢复头像' }] });
|
|
377
564
|
expect(service.snapshot().assistants.find((a) => a.id === own.id)).toMatchObject({ avatarSeed: own.avatarSeed, sessionID: own.sessionID });
|
|
378
565
|
expect(service.snapshot().assistants.find((a) => a.id === peer.id)).toMatchObject({ avatarSeed: peer.avatarSeed, revision: peer.revision });
|
|
@@ -391,7 +578,7 @@ describe('assistants service', () => {
|
|
|
391
578
|
let result;
|
|
392
579
|
let service = setup(directory, {}, {
|
|
393
580
|
runContactTurn: async ({ tools }) => {
|
|
394
|
-
result = await tools.find((tool) => tool.name === '
|
|
581
|
+
result = await tools.find((tool) => tool.name === 'update_assistant_settings').execute('avatar', { settings: { avatarSeed: null } });
|
|
395
582
|
return { text: 'done', bubbles: ['done'] };
|
|
396
583
|
},
|
|
397
584
|
});
|
|
@@ -403,13 +590,13 @@ describe('assistants service', () => {
|
|
|
403
590
|
random.mockImplementation((max) => max === AVATAR_COLORS.length ? index : 0);
|
|
404
591
|
await settleSend(service, own.id, { messageID: `avatar-${color}`, parts: [{ type: 'text', text: '换个头像' }] });
|
|
405
592
|
expect(result.details.updated).toBe(true);
|
|
406
|
-
expect(result.details.avatarSeed).toMatch(/^yuri-avatar:v1:/);
|
|
407
|
-
expect(resolveAssistantAvatar(result.details.avatarSeed)).toMatchObject({ color, shape: 'round', eyes: 'capsule', expression: 'neutral', animate: true });
|
|
593
|
+
expect(result.details.settings.avatarSeed).toMatch(/^yuri-avatar:v1:/);
|
|
594
|
+
expect(resolveAssistantAvatar(result.details.settings.avatarSeed)).toMatchObject({ color, shape: 'round', eyes: 'capsule', expression: 'neutral', animate: true });
|
|
408
595
|
expect(service.snapshot().assistants.find((bot) => bot.id === peer.id).avatarSeed).toBe('legacy-peer');
|
|
409
596
|
}
|
|
410
597
|
service.close();
|
|
411
598
|
service = setup(directory);
|
|
412
|
-
expect(service.snapshot().assistants.find((bot) => bot.id === own.id).avatarSeed).toBe(result.details.avatarSeed);
|
|
599
|
+
expect(service.snapshot().assistants.find((bot) => bot.id === own.id).avatarSeed).toBe(result.details.settings.avatarSeed);
|
|
413
600
|
expect(service.snapshot().assistants.find((bot) => bot.id === peer.id).avatarSeed).toBe('legacy-peer');
|
|
414
601
|
} finally {
|
|
415
602
|
random.mockRestore();
|
|
@@ -573,7 +760,6 @@ describe('assistants service', () => {
|
|
|
573
760
|
service.appendContactCard(assistant.id, { cardType: 'session', sessionID: 'ses_race', directory, status: 'busy' })
|
|
574
761
|
service.processEvent({ type: 'session.idle', properties: { sessionID: 'ses_race' } })
|
|
575
762
|
service.processEvent({ type: 'session.error', properties: { sessionID: 'ses_race', error: { name: 'MessageAbortedError' } } })
|
|
576
|
-
await service.whenContactTurnSettled(assignedSessionResumeMessageID(assistant.id, 'ses_race', 'complete', 42))
|
|
577
763
|
await service.whenContactTurnSettled(assignedSessionResumeMessageID(assistant.id, 'ses_race', 'cancelled', 42))
|
|
578
764
|
expect(runContactTurn).toHaveBeenCalledTimes(1)
|
|
579
765
|
expect(resumes[0]).toContain('status: cancelled')
|
|
@@ -1573,7 +1759,7 @@ describe('assistants service', () => {
|
|
|
1573
1759
|
|
|
1574
1760
|
it('applies the complete workspace patch and creates metadata with the final name', async () => {
|
|
1575
1761
|
const directory = root(); const other = path.join(directory, 'other'); fs.mkdirSync(other); let created; const service = setup(directory, { create: async (input) => { created = input; return { data: { id: 'ses_workspace' } }; } }); const assistant = service.createAssistant(assistantInput);
|
|
1576
|
-
const updated = await service.updateAssistant(assistant.id, { expectedRevision: 1, workspacePath: other, name: 'Renamed', defaultPrompt: 'P', providerID: 'provider-2', modelID: 'model-2', agent: 'agent-2', enabled: false }); expect(updated).toMatchObject({ name: 'Renamed', defaultPrompt: 'P', providerID: 'provider-2', modelID: 'model-2', agent: 'agent-2', enabled:
|
|
1762
|
+
const updated = await service.updateAssistant(assistant.id, { expectedRevision: 1, workspacePath: other, name: 'Renamed', defaultPrompt: 'P', providerID: 'provider-2', modelID: 'model-2', agent: 'agent-2', enabled: false }); expect(updated).toMatchObject({ name: 'Renamed', defaultPrompt: 'P', providerID: 'provider-2', modelID: 'model-2', agent: 'agent-2', enabled: true, sessionID: 'ses_workspace' }); expect(updated).not.toHaveProperty('skillRoots');
|
|
1577
1763
|
// v2 create uses location.directory + title prefix; archive metadata write is unavailable.
|
|
1578
1764
|
expect(created).toMatchObject({ title: '[Assistant] Renamed', location: { directory: fs.realpathSync(other) } });
|
|
1579
1765
|
service.close();
|
|
@@ -1784,8 +1970,63 @@ describe('assistants service', () => {
|
|
|
1784
1970
|
service.close();
|
|
1785
1971
|
});
|
|
1786
1972
|
|
|
1787
|
-
it('
|
|
1788
|
-
const service = setup();
|
|
1973
|
+
it('ignores legacy enable flags on create and update and still accepts contact messages', async () => {
|
|
1974
|
+
const service = setup();
|
|
1975
|
+
try {
|
|
1976
|
+
const assistant = service.createAssistant({ ...assistantInput, enabled: false });
|
|
1977
|
+
expect(assistant.enabled).toBe(true);
|
|
1978
|
+
const updated = await service.updateAssistant(assistant.id, { expectedRevision: 1, enabled: false, name: 'Always available' });
|
|
1979
|
+
expect(updated).toMatchObject({ enabled: true, name: 'Always available', revision: 2 });
|
|
1980
|
+
expect((await settleSend(service, assistant.id, { messageID: 'legacy-disabled', text: 'hello' })).settled.status).toBe('complete');
|
|
1981
|
+
} finally { service.close(); }
|
|
1982
|
+
});
|
|
1983
|
+
|
|
1984
|
+
it('restores legacy disabled contacts once without changing their settings or transcript', async () => {
|
|
1985
|
+
const directory = root();
|
|
1986
|
+
let service = setup(directory);
|
|
1987
|
+
const own = service.createAssistant({ ...assistantInput, defaultPrompt: 'Keep me', avatarSeed: 'saved-face' });
|
|
1988
|
+
const peer = service.createAssistant({ ...assistantInput, name: 'Peer' });
|
|
1989
|
+
const messages = service.contactMessages(own.id).messages;
|
|
1990
|
+
const revision = service.snapshot().revision;
|
|
1991
|
+
service.close();
|
|
1992
|
+
const db = new (require('better-sqlite3'))(path.join(directory, 'assistants.sqlite'));
|
|
1993
|
+
db.prepare('UPDATE assistant_v2 SET enabled=0 WHERE assistant_id=?').run(own.id);
|
|
1994
|
+
db.close();
|
|
1995
|
+
try {
|
|
1996
|
+
service = setup(directory);
|
|
1997
|
+
const restored = service.snapshot();
|
|
1998
|
+
expect(restored.revision).toBe(revision + 1);
|
|
1999
|
+
expect(restored.assistants.find((item) => item.id === own.id)).toMatchObject({ ...own, enabled: true, revision: own.revision + 1, updatedAt: expect.any(Number) });
|
|
2000
|
+
expect(restored.assistants.find((item) => item.id === peer.id)).toEqual(peer);
|
|
2001
|
+
expect(service.contactMessages(own.id).messages).toEqual(messages);
|
|
2002
|
+
service.close();
|
|
2003
|
+
service = setup(directory);
|
|
2004
|
+
expect(service.snapshot()).toEqual(restored);
|
|
2005
|
+
expect((await settleSend(service, own.id, { messageID: 'restored-enabled', text: 'hello' })).settled.status).toBe('complete');
|
|
2006
|
+
} finally { service.close(); fs.rmSync(directory, { recursive: true, force: true }); }
|
|
2007
|
+
});
|
|
2008
|
+
|
|
2009
|
+
it('rolls back all legacy disabled contact normalization when a stored row rejects the write', () => {
|
|
2010
|
+
const directory = root();
|
|
2011
|
+
let service = setup(directory);
|
|
2012
|
+
const own = service.createAssistant(assistantInput);
|
|
2013
|
+
service.createAssistant({ ...assistantInput, name: 'Peer' });
|
|
2014
|
+
const before = service.snapshot();
|
|
2015
|
+
const messages = service.contactMessages(own.id).messages;
|
|
2016
|
+
service.close();
|
|
2017
|
+
const db = new (require('better-sqlite3'))(path.join(directory, 'assistants.sqlite'));
|
|
2018
|
+
try {
|
|
2019
|
+
db.exec(`UPDATE assistant_v2 SET enabled=0;
|
|
2020
|
+
CREATE TRIGGER reject_restore BEFORE UPDATE OF enabled ON assistant_v2
|
|
2021
|
+
WHEN OLD.name='Peer' BEGIN SELECT RAISE(ABORT, 'restore failed'); END`);
|
|
2022
|
+
expect(() => setup(directory)).toThrow('restore failed');
|
|
2023
|
+
expect(db.prepare('SELECT enabled, revision FROM assistant_v2 ORDER BY name').all()).toEqual([{ enabled: 0, revision: 1 }, { enabled: 0, revision: 1 }]);
|
|
2024
|
+
expect(Number(db.prepare("SELECT value FROM assistant_meta WHERE key='revision'").get().value)).toBe(before.revision);
|
|
2025
|
+
db.exec('DROP TRIGGER reject_restore');
|
|
2026
|
+
service = setup(directory);
|
|
2027
|
+
expect(service.snapshot().assistants.every((item) => item.enabled)).toBe(true);
|
|
2028
|
+
expect(service.contactMessages(own.id).messages).toEqual(messages);
|
|
2029
|
+
} finally { db.close(); service.close(); fs.rmSync(directory, { recursive: true, force: true }); }
|
|
1789
2030
|
});
|
|
1790
2031
|
|
|
1791
2032
|
it('defaults assistants to continuous mode and persists a mode patch', async () => {
|
|
@@ -3121,6 +3362,157 @@ describe('assistants service', () => {
|
|
|
3121
3362
|
busyService.close();
|
|
3122
3363
|
});
|
|
3123
3364
|
|
|
3365
|
+
it.each(['complete', 'error', 'cancelled'])('consumes a %s watch across later rounds and restart until explicitly watched again', async (status) => {
|
|
3366
|
+
const directory = root();
|
|
3367
|
+
let time = 20_000;
|
|
3368
|
+
const resumes = [];
|
|
3369
|
+
const options = {
|
|
3370
|
+
clock: () => time,
|
|
3371
|
+
runContactTurn: async ({ tools, userText, allowWatchSession }) => {
|
|
3372
|
+
if (userText.includes('Internal assigned-session resume')) {
|
|
3373
|
+
expect(allowWatchSession).toBe(!userText.includes('status: cancelled'));
|
|
3374
|
+
expect(userText).toContain('watchState: released');
|
|
3375
|
+
expect(userText).toContain('watchMode: one_shot');
|
|
3376
|
+
expect(userText).toContain(`rewatchAllowed: ${allowWatchSession}`);
|
|
3377
|
+
expect(userText).toContain('Later messages or execution rounds in this session will NOT automatically notify you');
|
|
3378
|
+
expect(userText).toContain('Only a successful new watch_session call rearms monitoring');
|
|
3379
|
+
resumes.push(userText);
|
|
3380
|
+
return { text: '结果已汇报', bubbles: ['结果已汇报'] };
|
|
3381
|
+
}
|
|
3382
|
+
const result = await tools.find((tool) => tool.name === 'watch_session').execute('watch', { sessionID: 'ses_once' });
|
|
3383
|
+
expect(result.isError).not.toBe(true);
|
|
3384
|
+
return { text: '在听', bubbles: ['在听'], cards: [result.details.card] };
|
|
3385
|
+
},
|
|
3386
|
+
};
|
|
3387
|
+
const client = { get: async ({ sessionID }) => ({ data: { id: sessionID, directory, status: { type: 'busy' } } }) };
|
|
3388
|
+
let service = setup(directory, client, options);
|
|
3389
|
+
try {
|
|
3390
|
+
const assistant = service.createAssistant(assistantInput);
|
|
3391
|
+
await settleSend(service, assistant.id, { messageID: 'watch_once', parts: [{ type: 'text', text: '监听这个会话' }] });
|
|
3392
|
+
expect(service.reportAssignedSessionSettle('ses_once', status)).toBe(true);
|
|
3393
|
+
await service.whenContactTurnSettled(assignedSessionResumeMessageID(assistant.id, 'ses_once', status, time));
|
|
3394
|
+
expect(resumes).toHaveLength(1);
|
|
3395
|
+
for (let index = 0; index < 50; index += 1) {
|
|
3396
|
+
time += 1;
|
|
3397
|
+
expect(service.processEvent({ type: 'session.execution.started', properties: { sessionID: 'ses_once' } })).toBe(false);
|
|
3398
|
+
expect(service.reportAssignedSessionSettle('ses_once', index % 2 ? 'error' : 'complete')).toBe(false);
|
|
3399
|
+
}
|
|
3400
|
+
expect(service.snapshot().assistants[0].assignedSessionIDs).toEqual([]);
|
|
3401
|
+
expect(service.contactMessages(assistant.id).messages.flatMap((message) => message.parts).find((part) => part.type === 'card').status).toBe(status);
|
|
3402
|
+
service.close();
|
|
3403
|
+
service = setup(directory, client, options);
|
|
3404
|
+
time += 1;
|
|
3405
|
+
expect(service.processEvent({ type: 'session.execution.started', properties: { sessionID: 'ses_once' } })).toBe(false);
|
|
3406
|
+
expect(service.reportAssignedSessionSettle('ses_once', 'cancelled')).toBe(false);
|
|
3407
|
+
await new Promise(setImmediate);
|
|
3408
|
+
expect(resumes).toHaveLength(1);
|
|
3409
|
+
await settleSend(service, assistant.id, { messageID: 'watch_again', parts: [{ type: 'text', text: '再监听一次' }] });
|
|
3410
|
+
expect(service.snapshot().assistants[0].assignedSessionIDs).toEqual(['ses_once']);
|
|
3411
|
+
expect(service.reportAssignedSessionSettle('ses_once', 'complete')).toBe(true);
|
|
3412
|
+
await service.whenContactTurnSettled(assignedSessionResumeMessageID(assistant.id, 'ses_once', 'complete', time));
|
|
3413
|
+
expect(resumes).toHaveLength(2);
|
|
3414
|
+
} finally {
|
|
3415
|
+
service.close();
|
|
3416
|
+
fs.rmSync(directory, { recursive: true, force: true });
|
|
3417
|
+
}
|
|
3418
|
+
});
|
|
3419
|
+
|
|
3420
|
+
it('a background result may explicitly rewatch only its own worker without reopening mutation tools', async () => {
|
|
3421
|
+
const directory = root();
|
|
3422
|
+
let time = 30_000;
|
|
3423
|
+
let resumed = false;
|
|
3424
|
+
const service = setup(directory, {
|
|
3425
|
+
get: async ({ sessionID }) => ({ data: { id: sessionID, directory, status: { type: 'busy' } } }),
|
|
3426
|
+
}, {
|
|
3427
|
+
clock: () => time,
|
|
3428
|
+
runContactTurn: async (input) => {
|
|
3429
|
+
if (!resumed) {
|
|
3430
|
+
resumed = true;
|
|
3431
|
+
expect(input.allowWatchSession).toBe(true);
|
|
3432
|
+
const watch = input.tools.find((tool) => tool.name === 'watch_session');
|
|
3433
|
+
const denied = await watch.execute('other', { sessionID: 'ses_unrelated' });
|
|
3434
|
+
expect(denied.details.error).toBe('validation_error');
|
|
3435
|
+
let round = 0;
|
|
3436
|
+
return realRunContactTurn({
|
|
3437
|
+
...input,
|
|
3438
|
+
createChatCompletion: async ({ body }) => {
|
|
3439
|
+
if (++round === 1) {
|
|
3440
|
+
const names = body.tools.map((tool) => tool.function.name);
|
|
3441
|
+
expect(names).toContain('watch_session');
|
|
3442
|
+
expect(names).not.toContain('assign_session');
|
|
3443
|
+
expect(names).not.toContain('stop_session');
|
|
3444
|
+
expect(names).not.toContain('bash');
|
|
3445
|
+
return toolBatch([['watch_session', { sessionID: 'ses_rewatch' }]]);
|
|
3446
|
+
}
|
|
3447
|
+
return { text: '还需要观察,重新监听一次。' };
|
|
3448
|
+
},
|
|
3449
|
+
});
|
|
3450
|
+
}
|
|
3451
|
+
return { text: '观察完成。', bubbles: ['观察完成。'] };
|
|
3452
|
+
},
|
|
3453
|
+
});
|
|
3454
|
+
try {
|
|
3455
|
+
const assistant = service.createAssistant(assistantInput);
|
|
3456
|
+
service.appendContactCard(assistant.id, { cardType: 'session', sessionID: 'ses_rewatch', directory, status: 'busy' });
|
|
3457
|
+
const priorDb = new (require('better-sqlite3'))(path.join(directory, 'assistants.sqlite'));
|
|
3458
|
+
try {
|
|
3459
|
+
priorDb.prepare('UPDATE assistant_contact_watch SET message_id=? WHERE session_id=?').run('msg_old_delivery', 'ses_rewatch');
|
|
3460
|
+
} finally { priorDb.close(); }
|
|
3461
|
+
service.reportAssignedSessionSettle('ses_rewatch', 'complete');
|
|
3462
|
+
const settled = await service.whenContactTurnSettled(assignedSessionResumeMessageID(assistant.id, 'ses_rewatch', 'complete', time));
|
|
3463
|
+
expect(settled.error).toBeUndefined();
|
|
3464
|
+
expect(settled.status).toBe('complete');
|
|
3465
|
+
expect(service.snapshot().assistants[0].assignedSessionIDs).toEqual(['ses_rewatch']);
|
|
3466
|
+
const db = new (require('better-sqlite3'))(path.join(directory, 'assistants.sqlite'));
|
|
3467
|
+
try {
|
|
3468
|
+
expect(db.prepare('SELECT message_id FROM assistant_contact_watch WHERE session_id=?').get('ses_rewatch').message_id).toBeNull();
|
|
3469
|
+
} finally { db.close(); }
|
|
3470
|
+
time += 1;
|
|
3471
|
+
expect(service.processEvent({ type: 'session.idle', properties: { sessionID: 'ses_rewatch' } })).toBe(true);
|
|
3472
|
+
await service.whenContactTurnSettled(assignedSessionResumeMessageID(assistant.id, 'ses_rewatch', 'complete', time));
|
|
3473
|
+
expect(service.snapshot().assistants[0].assignedSessionIDs).toEqual([]);
|
|
3474
|
+
expect(service.contactMessages(assistant.id).messages.some((message) => message.text === '观察完成。')).toBe(true);
|
|
3475
|
+
} finally {
|
|
3476
|
+
service.close();
|
|
3477
|
+
fs.rmSync(directory, { recursive: true, force: true });
|
|
3478
|
+
}
|
|
3479
|
+
});
|
|
3480
|
+
|
|
3481
|
+
it('an explicit terminal-baseline watch waits for a new round without replaying the old result', async () => {
|
|
3482
|
+
const directory = root();
|
|
3483
|
+
let time = 40_000;
|
|
3484
|
+
const resumes = [];
|
|
3485
|
+
const service = setup(directory, {
|
|
3486
|
+
get: async ({ sessionID }) => ({ data: { id: sessionID, directory, status: { type: 'idle' }, time: { completed: 1 } } }),
|
|
3487
|
+
}, {
|
|
3488
|
+
clock: () => time,
|
|
3489
|
+
runContactTurn: async ({ userText, tools }) => {
|
|
3490
|
+
if (userText.includes('Internal assigned-session resume')) {
|
|
3491
|
+
resumes.push(userText);
|
|
3492
|
+
return { text: '新一轮结果', bubbles: ['新一轮结果'] };
|
|
3493
|
+
}
|
|
3494
|
+
const result = await tools.find((tool) => tool.name === 'watch_session').execute('watch', { sessionID: 'ses_baseline' });
|
|
3495
|
+
expect(result.details.watched.status).toBe('complete');
|
|
3496
|
+
return { text: '继续观察', bubbles: ['继续观察'], cards: [result.details.card] };
|
|
3497
|
+
},
|
|
3498
|
+
});
|
|
3499
|
+
try {
|
|
3500
|
+
const assistant = service.createAssistant(assistantInput);
|
|
3501
|
+
await settleSend(service, assistant.id, { messageID: 'baseline', parts: [{ type: 'text', text: '继续监听这个会话' }] });
|
|
3502
|
+
expect(service.reportAssignedSessionSettle('ses_baseline', 'complete')).toBe(false);
|
|
3503
|
+
expect(resumes).toHaveLength(0);
|
|
3504
|
+
time += 1;
|
|
3505
|
+
expect(service.processEvent({ type: 'session.execution.started', properties: { sessionID: 'ses_baseline' } })).toBe(true);
|
|
3506
|
+
expect(service.reportAssignedSessionSettle('ses_baseline', 'complete')).toBe(true);
|
|
3507
|
+
await service.whenContactTurnSettled(assignedSessionResumeMessageID(assistant.id, 'ses_baseline', 'complete', time));
|
|
3508
|
+
expect(resumes).toHaveLength(1);
|
|
3509
|
+
expect(service.processEvent({ type: 'session.execution.started', properties: { sessionID: 'ses_baseline' } })).toBe(false);
|
|
3510
|
+
} finally {
|
|
3511
|
+
service.close();
|
|
3512
|
+
fs.rmSync(directory, { recursive: true, force: true });
|
|
3513
|
+
}
|
|
3514
|
+
});
|
|
3515
|
+
|
|
3124
3516
|
it('stop_session calls real session.abort and surfaces abort failures', async () => {
|
|
3125
3517
|
const directory = root();
|
|
3126
3518
|
const project = path.join(directory, 'app');
|
|
@@ -3316,7 +3708,7 @@ describe('assistants service', () => {
|
|
|
3316
3708
|
}
|
|
3317
3709
|
});
|
|
3318
3710
|
|
|
3319
|
-
it('reads and persists defaultPrompt via get_assistant_settings /
|
|
3711
|
+
it('reads and persists defaultPrompt via get_assistant_settings / update_assistant_settings', async () => {
|
|
3320
3712
|
const directory = root();
|
|
3321
3713
|
const tips = [];
|
|
3322
3714
|
const harnessTurns = [];
|
|
@@ -3338,19 +3730,18 @@ describe('assistants service', () => {
|
|
|
3338
3730
|
defaultPrompt: 'Base persona',
|
|
3339
3731
|
providerID: 'p',
|
|
3340
3732
|
modelID: 'm',
|
|
3341
|
-
enabled: true,
|
|
3342
3733
|
});
|
|
3343
3734
|
expect(result.content[0].text).toContain('Base persona');
|
|
3344
3735
|
phase = 'write';
|
|
3345
3736
|
return { text: 'Current default prompt loaded.', bubbles: ['Current default prompt loaded.'] };
|
|
3346
3737
|
}
|
|
3347
3738
|
if (phase === 'write') {
|
|
3348
|
-
const update = tools.find((tool) => tool.name === '
|
|
3349
|
-
const unchanged = await update.execute('call_same', {
|
|
3350
|
-
expect(unchanged.details).toMatchObject({ updated: false,
|
|
3739
|
+
const update = tools.find((tool) => tool.name === 'update_assistant_settings');
|
|
3740
|
+
const unchanged = await update.execute('call_same', { settings: { defaultPrompt: 'Base persona' } });
|
|
3741
|
+
expect(unchanged.details).toMatchObject({ updated: false, settings: { defaultPrompt: 'Base persona' } });
|
|
3351
3742
|
const beforeRevision = service.snapshot().assistants.find((item) => item.id === assistant.id).revision;
|
|
3352
|
-
const saved = await update.execute('call_set', {
|
|
3353
|
-
expect(saved.details).toMatchObject({ updated: true, defaultPrompt: 'Reply only in Chinese.' });
|
|
3743
|
+
const saved = await update.execute('call_set', { settings: { defaultPrompt: 'Reply only in Chinese.' } });
|
|
3744
|
+
expect(saved.details).toMatchObject({ updated: true, settings: { defaultPrompt: 'Reply only in Chinese.' } });
|
|
3354
3745
|
expect(saved.terminate).toBe(false);
|
|
3355
3746
|
const after = service.snapshot().assistants.find((item) => item.id === assistant.id);
|
|
3356
3747
|
expect(after.defaultPrompt).toBe('Reply only in Chinese.');
|
|
@@ -3389,7 +3780,7 @@ describe('assistants service', () => {
|
|
|
3389
3780
|
expect(harnessTurns.some((turn) => turn.phase === 'next' && turn.defaultPrompt === 'Reply only in Chinese.')).toBe(true);
|
|
3390
3781
|
expect(harnessTurns[0].toolNames).toEqual(expect.arrayContaining([
|
|
3391
3782
|
'get_assistant_settings',
|
|
3392
|
-
'
|
|
3783
|
+
'update_assistant_settings',
|
|
3393
3784
|
]));
|
|
3394
3785
|
service.close();
|
|
3395
3786
|
});
|
|
@@ -3399,7 +3790,7 @@ describe('assistants service', () => {
|
|
|
3399
3790
|
const service = setup(directory, {}, {
|
|
3400
3791
|
runContactTurn: async ({ tools }) => {
|
|
3401
3792
|
const get = tools.find((tool) => tool.name === 'get_assistant_settings');
|
|
3402
|
-
const update = tools.find((tool) => tool.name === '
|
|
3793
|
+
const update = tools.find((tool) => tool.name === 'update_assistant_settings');
|
|
3403
3794
|
const seen = await get.execute('call_peer_get', { to: 'OpenCode 配置助手' });
|
|
3404
3795
|
expect(seen.details.settings).toMatchObject({
|
|
3405
3796
|
name: 'OpenCode 配置助手',
|
|
@@ -3407,12 +3798,11 @@ describe('assistants service', () => {
|
|
|
3407
3798
|
});
|
|
3408
3799
|
const saved = await update.execute('call_peer_set', {
|
|
3409
3800
|
to: 'OpenCode 配置助手',
|
|
3410
|
-
|
|
3801
|
+
settings: { defaultPrompt: 'new peer persona' },
|
|
3411
3802
|
});
|
|
3412
3803
|
expect(saved.details).toMatchObject({
|
|
3413
3804
|
updated: true,
|
|
3414
|
-
defaultPrompt: 'new peer persona',
|
|
3415
|
-
name: 'OpenCode 配置助手',
|
|
3805
|
+
settings: { defaultPrompt: 'new peer persona', name: 'OpenCode 配置助手' },
|
|
3416
3806
|
});
|
|
3417
3807
|
return { text: 'Updated peer.', bubbles: ['Updated peer.'] };
|
|
3418
3808
|
},
|
|
@@ -3432,28 +3822,28 @@ describe('assistants service', () => {
|
|
|
3432
3822
|
service.close();
|
|
3433
3823
|
});
|
|
3434
3824
|
|
|
3435
|
-
it('retries
|
|
3825
|
+
it('retries update_assistant_settings once on revision_conflict and reports persistent conflict', async () => {
|
|
3436
3826
|
const directory = root();
|
|
3437
3827
|
let host;
|
|
3438
3828
|
let mode = 'retry_success';
|
|
3439
3829
|
const service = setup(directory, {}, {
|
|
3440
3830
|
runContactTurn: async ({ tools }) => {
|
|
3441
|
-
const update = tools.find((tool) => tool.name === '
|
|
3831
|
+
const update = tools.find((tool) => tool.name === 'update_assistant_settings');
|
|
3442
3832
|
if (mode === 'retry_success') {
|
|
3443
3833
|
// Concurrent name write lands during the pre-CAS yield → first CAS conflicts, retry succeeds.
|
|
3444
|
-
const pending = update.execute('call_retry', {
|
|
3834
|
+
const pending = update.execute('call_retry', { settings: { defaultPrompt: 'After conflict retry' } });
|
|
3445
3835
|
await service.updateAssistant(host.id, {
|
|
3446
3836
|
expectedRevision: service.snapshot().assistants.find((item) => item.id === host.id).revision,
|
|
3447
3837
|
name: 'ConcurrentRename',
|
|
3448
3838
|
});
|
|
3449
3839
|
const result = await pending;
|
|
3450
|
-
expect(result.details).toMatchObject({ updated: true, defaultPrompt: 'After conflict retry' });
|
|
3840
|
+
expect(result.details).toMatchObject({ updated: true, settings: { defaultPrompt: 'After conflict retry' } });
|
|
3451
3841
|
expect(result.details.error).toBeUndefined();
|
|
3452
3842
|
mode = 'retry_fail';
|
|
3453
3843
|
return { text: 'Retried ok.', bubbles: ['Retried ok.'] };
|
|
3454
3844
|
}
|
|
3455
3845
|
// Two concurrent writes during attempt 0 and attempt 1 yields → both CAS fail.
|
|
3456
|
-
const pending = update.execute('call_fail', {
|
|
3846
|
+
const pending = update.execute('call_fail', { settings: { defaultPrompt: 'Should fail' } });
|
|
3457
3847
|
await service.updateAssistant(host.id, {
|
|
3458
3848
|
expectedRevision: service.snapshot().assistants.find((item) => item.id === host.id).revision,
|
|
3459
3849
|
name: 'BumpOne',
|
|
@@ -4055,6 +4445,157 @@ describe('assistants service', () => {
|
|
|
4055
4445
|
service.close();
|
|
4056
4446
|
});
|
|
4057
4447
|
|
|
4448
|
+
it.each(['session.execution.succeeded', 'session.idle', 'session.status'])('waits for background descendants before the assigned result and receipt: %s', async (type) => {
|
|
4449
|
+
const directory = root();
|
|
4450
|
+
let active = { ses_nested: { type: 'running' } };
|
|
4451
|
+
const resumes = vi.fn(async () => ({ bubbles: ['最终报告已写入。'] }));
|
|
4452
|
+
const service = setup(directory, {
|
|
4453
|
+
active: async () => active,
|
|
4454
|
+
get: async ({ sessionID }) => ({ data: {
|
|
4455
|
+
id: sessionID,
|
|
4456
|
+
parentID: sessionID === 'ses_nested' ? 'ses_child' : sessionID === 'ses_child' ? 'ses_root' : undefined,
|
|
4457
|
+
status: { type: 'idle' },
|
|
4458
|
+
} }),
|
|
4459
|
+
messages: async () => ({ data: [
|
|
4460
|
+
{ id: 'msg_final', type: 'assistant', time: { created: 20, completed: 21 }, finish: 'stop', content: [{ type: 'text', text: '最终报告已写入 vault。' }] },
|
|
4461
|
+
{ id: 'msg_old', type: 'assistant', time: { created: 10, completed: 11 }, finish: 'stop', content: [{ type: 'text', text: '已交给后台子 agent,正在处理。' }] },
|
|
4462
|
+
] }),
|
|
4463
|
+
}, { clock: () => 5_000, runContactTurn: resumes });
|
|
4464
|
+
try {
|
|
4465
|
+
const assistant = service.createAssistant(assistantInput);
|
|
4466
|
+
service.appendContactCard(assistant.id, { cardType: 'session', sessionID: 'ses_root', directory, title: 'Research', status: 'busy' });
|
|
4467
|
+
service.processEvent({ type, properties: { sessionID: 'ses_root', status: { type: 'idle' } } });
|
|
4468
|
+
await service.reconcile();
|
|
4469
|
+
expect(service.snapshot().assistants[0].assignedSessionIDs).toEqual(['ses_root']);
|
|
4470
|
+
expect(resumes).not.toHaveBeenCalled();
|
|
4471
|
+
expect(service.contactMessages(assistant.id).messages.flatMap((message) => message.cards)).toEqual([expect.objectContaining({ status: 'busy' })]);
|
|
4472
|
+
|
|
4473
|
+
active = {};
|
|
4474
|
+
service.processEvent({ type: 'session.execution.succeeded', properties: { sessionID: 'ses_nested' } });
|
|
4475
|
+
await vi.waitFor(() => expect(resumes).toHaveBeenCalledTimes(1));
|
|
4476
|
+
await service.whenContactTurnSettled(assignedSessionResumeMessageID(assistant.id, 'ses_root', 'complete', 5_000));
|
|
4477
|
+
expect(resumes.mock.calls[0][0].userText).toContain('最终报告已写入 vault。');
|
|
4478
|
+
expect(resumes.mock.calls[0][0].userText).not.toContain('正在处理');
|
|
4479
|
+
expect(service.snapshot().assistants[0].assignedSessionIDs).toEqual([]);
|
|
4480
|
+
const cards = service.contactMessages(assistant.id).messages.flatMap((message) => message.cards);
|
|
4481
|
+
expect(cards).toHaveLength(2);
|
|
4482
|
+
expect(cards[1]).toMatchObject({ cardType: 'session', sessionID: 'ses_root', status: 'complete' });
|
|
4483
|
+
service.processEvent({ type, properties: { sessionID: 'ses_root', status: { type: 'idle' } } });
|
|
4484
|
+
await service.reconcile();
|
|
4485
|
+
expect(resumes).toHaveBeenCalledTimes(1);
|
|
4486
|
+
expect(service.contactMessages(assistant.id).messages.flatMap((message) => message.cards)).toHaveLength(2);
|
|
4487
|
+
} finally {
|
|
4488
|
+
service.close();
|
|
4489
|
+
fs.rmSync(directory, { recursive: true, force: true });
|
|
4490
|
+
}
|
|
4491
|
+
});
|
|
4492
|
+
|
|
4493
|
+
it('watch_session baselines an idle parent with an active background child as busy', async () => {
|
|
4494
|
+
const directory = root();
|
|
4495
|
+
let active = { ses_child: { type: 'running' } };
|
|
4496
|
+
const resumes = [];
|
|
4497
|
+
const prompt = vi.fn();
|
|
4498
|
+
const service = setup(directory, {
|
|
4499
|
+
active: async () => active,
|
|
4500
|
+
prompt,
|
|
4501
|
+
get: async ({ sessionID }) => ({ id: sessionID, parentID: sessionID === 'ses_child' ? 'ses_root' : undefined, directory, status: { type: 'idle' } }),
|
|
4502
|
+
messages: async () => ({ data: [{ id: 'msg_done', type: 'assistant', finish: 'stop', time: { completed: 10 }, content: [{ type: 'text', text: 'Done' }] }] }),
|
|
4503
|
+
}, {
|
|
4504
|
+
clock: () => 5_000,
|
|
4505
|
+
runContactTurn: async ({ tools, userText }) => {
|
|
4506
|
+
if (userText.includes('Internal assigned-session resume')) {
|
|
4507
|
+
resumes.push(userText);
|
|
4508
|
+
return { bubbles: ['Done'] };
|
|
4509
|
+
}
|
|
4510
|
+
const result = await tools.find((tool) => tool.name === 'watch_session').execute('watch', { sessionID: 'ses_root' });
|
|
4511
|
+
expect(result.details.watched.status).toBe('busy');
|
|
4512
|
+
return { bubbles: ['Watching'], cards: [result.details.card] };
|
|
4513
|
+
},
|
|
4514
|
+
});
|
|
4515
|
+
try {
|
|
4516
|
+
const assistant = service.createAssistant(assistantInput);
|
|
4517
|
+
await settleSend(service, assistant.id, { messageID: 'watch_family', parts: [{ type: 'text', text: 'Watch this session' }] });
|
|
4518
|
+
await service.reconcile();
|
|
4519
|
+
expect(service.snapshot().assistants[0].assignedSessionIDs).toEqual(['ses_root']);
|
|
4520
|
+
expect(resumes).toHaveLength(0);
|
|
4521
|
+
active = {};
|
|
4522
|
+
service.processEvent({ type: 'session.execution.succeeded', properties: { sessionID: 'ses_child' } });
|
|
4523
|
+
await service.whenContactTurnSettled(assignedSessionResumeMessageID(assistant.id, 'ses_root', 'complete', 5_000));
|
|
4524
|
+
expect(resumes).toHaveLength(1);
|
|
4525
|
+
expect(prompt).not.toHaveBeenCalled();
|
|
4526
|
+
} finally {
|
|
4527
|
+
service.close();
|
|
4528
|
+
fs.rmSync(directory, { recursive: true, force: true });
|
|
4529
|
+
}
|
|
4530
|
+
});
|
|
4531
|
+
|
|
4532
|
+
it('does not probe family activity for 50 ordinary streaming deltas', async () => {
|
|
4533
|
+
const directory = root();
|
|
4534
|
+
const active = vi.fn(async () => ({}));
|
|
4535
|
+
const service = setup(directory, { active });
|
|
4536
|
+
try {
|
|
4537
|
+
await service.reconcile();
|
|
4538
|
+
const assistant = service.createAssistant(assistantInput);
|
|
4539
|
+
service.appendContactCard(assistant.id, { cardType: 'session', sessionID: 'ses_stream', directory, status: 'busy' });
|
|
4540
|
+
for (let index = 0; index < 50; index += 1) {
|
|
4541
|
+
service.processEvent({ type: 'session.text.delta', data: { sessionID: 'ses_stream', delta: 'x' } });
|
|
4542
|
+
}
|
|
4543
|
+
expect(active).not.toHaveBeenCalled();
|
|
4544
|
+
} finally {
|
|
4545
|
+
service.close();
|
|
4546
|
+
fs.rmSync(directory, { recursive: true, force: true });
|
|
4547
|
+
}
|
|
4548
|
+
});
|
|
4549
|
+
|
|
4550
|
+
it.each([null, { error: { status: 503 } }, { ses_child: { type: 'unknown' } }])('keeps the watch and receipt pending when activity is unknown: %j', async (unknown) => {
|
|
4551
|
+
const directory = root();
|
|
4552
|
+
const active = vi.fn(async () => unknown);
|
|
4553
|
+
const resumes = vi.fn(async () => ({ bubbles: ['Done'] }));
|
|
4554
|
+
const service = setup(directory, { active }, { clock: () => 5_000, runContactTurn: resumes });
|
|
4555
|
+
try {
|
|
4556
|
+
const assistant = service.createAssistant(assistantInput);
|
|
4557
|
+
service.appendContactCard(assistant.id, { cardType: 'session', sessionID: 'ses_unknown', directory, status: 'busy' });
|
|
4558
|
+
service.processEvent({ type: 'session.execution.succeeded', properties: { sessionID: 'ses_unknown' } });
|
|
4559
|
+
await service.reconcile();
|
|
4560
|
+
expect(service.snapshot().assistants[0].assignedSessionIDs).toEqual(['ses_unknown']);
|
|
4561
|
+
expect(resumes).not.toHaveBeenCalled();
|
|
4562
|
+
expect(service.contactMessages(assistant.id).messages.flatMap((message) => message.cards)).toHaveLength(1);
|
|
4563
|
+
active.mockResolvedValue({});
|
|
4564
|
+
service.processEvent({ type: 'session.execution.succeeded', properties: { sessionID: 'ses_unknown' } });
|
|
4565
|
+
await vi.waitFor(() => expect(resumes).toHaveBeenCalledTimes(1));
|
|
4566
|
+
await service.whenContactTurnSettled(assignedSessionResumeMessageID(assistant.id, 'ses_unknown', 'complete', 5_000));
|
|
4567
|
+
} finally {
|
|
4568
|
+
service.close();
|
|
4569
|
+
fs.rmSync(directory, { recursive: true, force: true });
|
|
4570
|
+
}
|
|
4571
|
+
});
|
|
4572
|
+
|
|
4573
|
+
it('recovery waits for an idle watched child and its active descendants, without waiting for its unrelated parent', async () => {
|
|
4574
|
+
const directory = root();
|
|
4575
|
+
let active = { ses_nested: { type: 'running' }, ses_parent: { type: 'running' } };
|
|
4576
|
+
const resumes = vi.fn(async () => ({ bubbles: ['Done'] }));
|
|
4577
|
+
const service = setup(directory, {
|
|
4578
|
+
active: async () => active,
|
|
4579
|
+
get: async ({ sessionID }) => ({ id: sessionID, parentID: sessionID === 'ses_nested' ? 'ses_child' : sessionID === 'ses_child' ? 'ses_parent' : undefined, status: { type: 'idle' } }),
|
|
4580
|
+
messages: async () => ({ data: [{ id: 'msg_done', type: 'assistant', finish: 'stop', time: { completed: 10 }, content: [{ type: 'text', text: 'Done' }] }] }),
|
|
4581
|
+
}, { clock: () => 5_000, runContactTurn: resumes });
|
|
4582
|
+
try {
|
|
4583
|
+
const assistant = service.createAssistant(assistantInput);
|
|
4584
|
+
service.appendContactCard(assistant.id, { cardType: 'session', sessionID: 'ses_child', directory, status: 'busy' });
|
|
4585
|
+
await service.reconcile();
|
|
4586
|
+
expect(resumes).not.toHaveBeenCalled();
|
|
4587
|
+
expect(service.snapshot().assistants[0].assignedSessionIDs).toEqual(['ses_child']);
|
|
4588
|
+
active = { ses_parent: { type: 'running' } };
|
|
4589
|
+
service.processEvent({ type: 'session.execution.succeeded', properties: { sessionID: 'ses_nested' } });
|
|
4590
|
+
await service.whenContactTurnSettled(assignedSessionResumeMessageID(assistant.id, 'ses_child', 'complete', 5_000));
|
|
4591
|
+
expect(resumes).toHaveBeenCalledTimes(1);
|
|
4592
|
+
expect(service.contactMessages(assistant.id).messages.flatMap((message) => message.cards)).toHaveLength(2);
|
|
4593
|
+
} finally {
|
|
4594
|
+
service.close();
|
|
4595
|
+
fs.rmSync(directory, { recursive: true, force: true });
|
|
4596
|
+
}
|
|
4597
|
+
});
|
|
4598
|
+
|
|
4058
4599
|
it('subscribes assigned sessions so idle updates the card, resumes contact LLM, and skips canned settle bubbles', async () => {
|
|
4059
4600
|
const directory = root();
|
|
4060
4601
|
let time = 5_000;
|
|
@@ -4133,10 +4674,15 @@ describe('assistants service', () => {
|
|
|
4133
4674
|
const assistant = first.createAssistant(assistantInput);
|
|
4134
4675
|
first.appendContactCard(assistant.id, { cardType: 'session', sessionID: 'ses_shared', directory, title: 'Cleanup', status: 'busy' });
|
|
4135
4676
|
expect(first.processEvent({ type: 'session.idle', properties: { sessionID: 'ses_shared' } })).toBe(true);
|
|
4136
|
-
expect(second.processEvent({ type: 'session.idle', properties: { sessionID: 'ses_shared' } })).toBe(
|
|
4137
|
-
await vi.waitFor(() => expect(
|
|
4677
|
+
expect(second.processEvent({ type: 'session.idle', properties: { sessionID: 'ses_shared' } })).toBe(true);
|
|
4678
|
+
await vi.waitFor(() => expect(resumes).toHaveLength(1));
|
|
4679
|
+
await first.reconcile();
|
|
4680
|
+
await second.reconcile();
|
|
4138
4681
|
expect(resumes).toHaveLength(1);
|
|
4139
4682
|
expect(first.contactMessages(assistant.id).messages.filter((message) => message.text === '清理完成。')).toHaveLength(1);
|
|
4683
|
+
expect(second.processEvent({ type: 'session.execution.started', properties: { sessionID: 'ses_shared' } })).toBe(false);
|
|
4684
|
+
expect(second.processEvent({ type: 'session.error', properties: { sessionID: 'ses_shared', error: { name: 'UnknownError' } } })).toBe(false);
|
|
4685
|
+
expect(resumes).toHaveLength(1);
|
|
4140
4686
|
} finally {
|
|
4141
4687
|
first.close();
|
|
4142
4688
|
second.close();
|
|
@@ -4183,7 +4729,7 @@ describe('assistants service', () => {
|
|
|
4183
4729
|
}
|
|
4184
4730
|
});
|
|
4185
4731
|
|
|
4186
|
-
it('lets an assigned-session follow-up stay silent
|
|
4732
|
+
it('lets an assigned-session follow-up stay silent with only its completion receipt and no spoken notification', async () => {
|
|
4187
4733
|
const directory = root();
|
|
4188
4734
|
let time = 7_000;
|
|
4189
4735
|
const onContactTurnComplete = vi.fn();
|
|
@@ -4207,7 +4753,9 @@ describe('assistants service', () => {
|
|
|
4207
4753
|
const settled = await service.whenContactTurnSettled(assignedSessionResumeMessageID(assistant.id, 'ses_quiet', 'complete', 7_000));
|
|
4208
4754
|
expect(settled).toMatchObject({ status: 'complete', silent: true });
|
|
4209
4755
|
expect(backgroundTurns[0]).toMatchObject({ readOnly: true, allowSilent: true });
|
|
4210
|
-
|
|
4756
|
+
const messages = service.contactMessages(assistant.id).messages;
|
|
4757
|
+
expect(messages).toHaveLength(before + 1);
|
|
4758
|
+
expect(messages.at(-1).cards).toEqual([expect.objectContaining({ sessionID: 'ses_quiet', status: 'complete' })]);
|
|
4211
4759
|
expect(onContactTurnComplete).not.toHaveBeenCalled();
|
|
4212
4760
|
expect(service.snapshot().assistants[0].working).toBe(false);
|
|
4213
4761
|
} finally {
|
|
@@ -4297,7 +4845,72 @@ describe('assistants service', () => {
|
|
|
4297
4845
|
service.close();
|
|
4298
4846
|
});
|
|
4299
4847
|
|
|
4300
|
-
it('
|
|
4848
|
+
it('does not let a Host on another OpenCode settle a shared worker as missing', async () => {
|
|
4849
|
+
const directory = root();
|
|
4850
|
+
const resumes = vi.fn(async () => ({ text: '会话记录已找不到', bubbles: ['会话记录已找不到'] }));
|
|
4851
|
+
const completed = vi.fn(async () => ({ text: 'Actual worker result', bubbles: ['Actual worker result'] }));
|
|
4852
|
+
let running = true;
|
|
4853
|
+
const owner = setup(directory, {
|
|
4854
|
+
active: async () => running ? { ses_running: { type: 'running' } } : {},
|
|
4855
|
+
messages: async () => ({ data: [{
|
|
4856
|
+
id: 'msg_result', type: 'assistant', finish: 'stop', time: { completed: 1 },
|
|
4857
|
+
content: [{ type: 'text', text: 'Actual worker result' }],
|
|
4858
|
+
}] }),
|
|
4859
|
+
}, { clock: () => 9_000, runContactTurn: completed });
|
|
4860
|
+
const other = setup(directory, {
|
|
4861
|
+
get: async () => ({ error: { status: 404 } }),
|
|
4862
|
+
messages: async () => ({ error: { status: 404 } }),
|
|
4863
|
+
}, { runContactTurn: resumes });
|
|
4864
|
+
try {
|
|
4865
|
+
const assistant = owner.createAssistant(assistantInput);
|
|
4866
|
+
owner.appendContactCard(assistant.id, {
|
|
4867
|
+
cardType: 'session', sessionID: 'ses_running', directory, title: 'Still running', status: 'busy',
|
|
4868
|
+
});
|
|
4869
|
+
await other.reconcile();
|
|
4870
|
+
await owner.reconcile();
|
|
4871
|
+
expect(owner.snapshot().assistants[0].assignedSessionIDs).toEqual(['ses_running']);
|
|
4872
|
+
expect(owner.contactMessages(assistant.id).messages.find((message) => message.parts[0]?.type === 'card')?.parts[0].status).toBe('busy');
|
|
4873
|
+
expect(resumes).not.toHaveBeenCalled();
|
|
4874
|
+
expect(completed).not.toHaveBeenCalled();
|
|
4875
|
+
|
|
4876
|
+
running = false;
|
|
4877
|
+
await owner.reconcile();
|
|
4878
|
+
await owner.whenContactTurnSettled(assignedSessionResumeMessageID(assistant.id, 'ses_running', 'complete', 9_000));
|
|
4879
|
+
await other.reconcile();
|
|
4880
|
+
await owner.reconcile();
|
|
4881
|
+
expect(owner.snapshot().assistants[0].assignedSessionIDs).toEqual([]);
|
|
4882
|
+
expect(completed).toHaveBeenCalledTimes(1);
|
|
4883
|
+
expect(resumes).not.toHaveBeenCalled();
|
|
4884
|
+
} finally {
|
|
4885
|
+
owner.close();
|
|
4886
|
+
other.close();
|
|
4887
|
+
fs.rmSync(directory, { recursive: true, force: true });
|
|
4888
|
+
}
|
|
4889
|
+
});
|
|
4890
|
+
|
|
4891
|
+
it.each([
|
|
4892
|
+
{ error: { status: 404 } },
|
|
4893
|
+
{ _tag: 'SessionNotFoundError' },
|
|
4894
|
+
{ _tag: 'MessageNotFoundError' },
|
|
4895
|
+
])('keeps the watch when the message page is missing: %j', async (missing) => {
|
|
4896
|
+
const directory = root();
|
|
4897
|
+
const resumes = vi.fn();
|
|
4898
|
+
const service = setup(directory, { messages: async () => missing }, { runContactTurn: resumes });
|
|
4899
|
+
try {
|
|
4900
|
+
const assistant = service.createAssistant(assistantInput);
|
|
4901
|
+
service.appendContactCard(assistant.id, {
|
|
4902
|
+
cardType: 'session', sessionID: 'ses_unknown', directory, title: 'Unknown result', status: 'busy',
|
|
4903
|
+
});
|
|
4904
|
+
await service.reconcile();
|
|
4905
|
+
expect(service.snapshot().assistants[0].assignedSessionIDs).toEqual(['ses_unknown']);
|
|
4906
|
+
expect(resumes).not.toHaveBeenCalled();
|
|
4907
|
+
} finally {
|
|
4908
|
+
service.close();
|
|
4909
|
+
fs.rmSync(directory, { recursive: true, force: true });
|
|
4910
|
+
}
|
|
4911
|
+
});
|
|
4912
|
+
|
|
4913
|
+
it('keeps a missing assigned session pending and reconciles an unrelated assistant error without settle bubbles', async () => {
|
|
4301
4914
|
const directory = root();
|
|
4302
4915
|
let time = 9_000;
|
|
4303
4916
|
const resumes = [];
|
|
@@ -4344,23 +4957,20 @@ describe('assistants service', () => {
|
|
|
4344
4957
|
status: 'busy',
|
|
4345
4958
|
});
|
|
4346
4959
|
await service.reconcile();
|
|
4347
|
-
await
|
|
4348
|
-
service.whenContactTurnSettled(assignedSessionResumeMessageID(assistant.id, 'ses_gone', 'complete', 9_000)),
|
|
4349
|
-
service.whenContactTurnSettled(assignedSessionResumeMessageID(assistant.id, 'ses_fail', 'error', 9_000)),
|
|
4350
|
-
]);
|
|
4960
|
+
await service.whenContactTurnSettled(assignedSessionResumeMessageID(assistant.id, 'ses_fail', 'error', 9_000));
|
|
4351
4961
|
const cards = service.contactMessages(assistant.id).messages
|
|
4352
4962
|
.filter((message) => message.parts.some((part) => part.type === 'card'))
|
|
4353
4963
|
.map((message) => message.parts[0]);
|
|
4354
|
-
expect(cards.find((card) => card.sessionID === 'ses_gone')?.status).toBe('
|
|
4964
|
+
expect(cards.find((card) => card.sessionID === 'ses_gone')?.status).toBe('busy');
|
|
4355
4965
|
expect(cards.find((card) => card.sessionID === 'ses_fail')?.status).toBe('error');
|
|
4356
4966
|
const texts = service.contactMessages(assistant.id).messages.map((message) => message.text);
|
|
4357
4967
|
expect(texts.filter((text) => text === 'oc.settle.complete')).toHaveLength(0);
|
|
4358
4968
|
expect(texts.filter((text) => text === 'oc.settle.error')).toHaveLength(0);
|
|
4359
|
-
expect(texts).
|
|
4360
|
-
expect(resumes.some((text) => text.includes('ses_gone')
|
|
4969
|
+
expect(texts).toContain('失败摘要');
|
|
4970
|
+
expect(resumes.some((text) => text.includes('ses_gone'))).toBe(false);
|
|
4361
4971
|
expect(resumes.some((text) => text.includes('ses_fail') && text.includes('error'))).toBe(true);
|
|
4362
4972
|
expect(service.snapshot().assistants[0]).toMatchObject({
|
|
4363
|
-
assignedSessionIDs: [],
|
|
4973
|
+
assignedSessionIDs: ['ses_gone'],
|
|
4364
4974
|
working: false,
|
|
4365
4975
|
});
|
|
4366
4976
|
const resumeCount = resumes.length;
|
|
@@ -4602,11 +5212,229 @@ describe('assistants service', () => {
|
|
|
4602
5212
|
service.close();
|
|
4603
5213
|
});
|
|
4604
5214
|
|
|
5215
|
+
it('wakes the recipient once for a peer DM without inserting a user row or calling OpenCode', async () => {
|
|
5216
|
+
const turns = [];
|
|
5217
|
+
const onContactTurnComplete = vi.fn();
|
|
5218
|
+
const service = setup(root(), {}, {
|
|
5219
|
+
onContactTurnComplete,
|
|
5220
|
+
runContactTurn: async (turn) => {
|
|
5221
|
+
turns.push(turn);
|
|
5222
|
+
return { bubbles: ['收到了,你好!'] };
|
|
5223
|
+
},
|
|
5224
|
+
});
|
|
5225
|
+
try {
|
|
5226
|
+
const sender = service.createAssistant({ ...assistantInput, name: '小小白' });
|
|
5227
|
+
const recipient = service.createAssistant({ ...assistantInput, name: '大白' });
|
|
5228
|
+
const delivered = service.deliverPeerMessage(sender.id, {
|
|
5229
|
+
messageID: 'peer_greeting', toAssistantID: recipient.id, text: '老大你好',
|
|
5230
|
+
});
|
|
5231
|
+
expect(delivered.admitted).toBe(true);
|
|
5232
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
5233
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
5234
|
+
expect(turns).toHaveLength(1);
|
|
5235
|
+
expect(turns[0]).toMatchObject({ assistant: { id: recipient.id }, readOnly: true });
|
|
5236
|
+
expect(turns[0].userText).toContain('小小白');
|
|
5237
|
+
expect(turns[0].userText).toContain('老大你好');
|
|
5238
|
+
expect(service.contactMessages(recipient.id).messages.map((message) => message.role)).toEqual(['assistant', 'peer', 'assistant']);
|
|
5239
|
+
expect(service.contactMessages(recipient.id).messages.at(-1).text).toBe('收到了,你好!');
|
|
5240
|
+
expect(onContactTurnComplete).toHaveBeenCalledWith(expect.objectContaining({ assistantID: recipient.id, status: 'complete' }));
|
|
5241
|
+
const message = turns[0].tools.find((tool) => tool.name === 'message_assistant');
|
|
5242
|
+
if (message) {
|
|
5243
|
+
const blocked = await message.execute('loop', { toAssistantID: sender.id, text: 'ping back' });
|
|
5244
|
+
expect(blocked.details.error).toBe('validation_error');
|
|
5245
|
+
}
|
|
5246
|
+
expect(service.contactMessages(sender.id).messages).toHaveLength(1);
|
|
5247
|
+
} finally { service.close(); }
|
|
5248
|
+
});
|
|
5249
|
+
|
|
5250
|
+
it('replays the same peer admission once, rejects conflicting identities and does not wake old inbox rows on restart', async () => {
|
|
5251
|
+
const directory = root();
|
|
5252
|
+
const runContactTurn = vi.fn(async () => ({ bubbles: ['你好'] }));
|
|
5253
|
+
const service = setup(directory, {}, { runContactTurn });
|
|
5254
|
+
const sender = service.createAssistant({ ...assistantInput, name: 'Sender' });
|
|
5255
|
+
const recipient = service.createAssistant({ ...assistantInput, name: 'Recipient' });
|
|
5256
|
+
const input = { messageID: 'peer_once', toAssistantID: recipient.id, text: 'hello' };
|
|
5257
|
+
try {
|
|
5258
|
+
service.deliverPeerMessage(sender.id, input);
|
|
5259
|
+
expect(service.deliverPeerMessage(sender.id, input).replayed).toBe(true);
|
|
5260
|
+
expect(() => service.deliverPeerMessage(sender.id, { ...input, text: 'changed' })).toThrow('idempotency_conflict');
|
|
5261
|
+
expect(() => service.deliverPeerMessage(recipient.id, { ...input, toAssistantID: sender.id })).toThrow('idempotency_conflict');
|
|
5262
|
+
await service.whenContactTurnSettled(input.messageID);
|
|
5263
|
+
expect(service.deliverPeerMessage(sender.id, input).replayed).toBe(true);
|
|
5264
|
+
expect(runContactTurn).toHaveBeenCalledTimes(1);
|
|
5265
|
+
} finally { service.close(); }
|
|
5266
|
+
const cold = setup(directory, {}, { runContactTurn });
|
|
5267
|
+
try {
|
|
5268
|
+
expect(cold.deliverPeerMessage(sender.id, input).replayed).toBe(true);
|
|
5269
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
5270
|
+
expect(runContactTurn).toHaveBeenCalledTimes(1);
|
|
5271
|
+
expect(cold.contactMessages(recipient.id).messages.map((message) => message.role)).toEqual(['assistant', 'peer', 'assistant']);
|
|
5272
|
+
} finally { cold.close(); }
|
|
5273
|
+
});
|
|
5274
|
+
|
|
5275
|
+
it('rolls back failed peer persistence without waking the recipient and permits a clean retry', async () => {
|
|
5276
|
+
const directory = root();
|
|
5277
|
+
const runContactTurn = vi.fn(async () => ({ bubbles: ['hello'] }));
|
|
5278
|
+
const service = setup(directory, {}, { runContactTurn });
|
|
5279
|
+
const db = new (require('better-sqlite3'))(path.join(directory, 'assistants.sqlite'));
|
|
5280
|
+
try {
|
|
5281
|
+
const sender = service.createAssistant({ ...assistantInput, name: 'Sender' });
|
|
5282
|
+
const recipient = service.createAssistant({ ...assistantInput, name: 'Recipient' });
|
|
5283
|
+
db.exec(`CREATE TRIGGER fail_peer_part BEFORE INSERT ON assistant_contact_part
|
|
5284
|
+
WHEN NEW.message_id='peer_rollback' BEGIN SELECT RAISE(ABORT, 'peer write failed'); END`);
|
|
5285
|
+
const input = { messageID: 'peer_rollback', toAssistantID: recipient.id, text: 'hello' };
|
|
5286
|
+
expect(() => service.deliverPeerMessage(sender.id, input)).toThrow('peer write failed');
|
|
5287
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
5288
|
+
expect(runContactTurn).not.toHaveBeenCalled();
|
|
5289
|
+
expect(service.contactMessages(recipient.id).messages).toHaveLength(1);
|
|
5290
|
+
expect(service.snapshot().assistants.find((item) => item.id === recipient.id).working).toBe(false);
|
|
5291
|
+
db.exec('DROP TRIGGER fail_peer_part');
|
|
5292
|
+
service.deliverPeerMessage(sender.id, input);
|
|
5293
|
+
expect(await service.whenContactTurnSettled(input.messageID)).toMatchObject({ status: 'complete' });
|
|
5294
|
+
expect(runContactTurn).toHaveBeenCalledTimes(1);
|
|
5295
|
+
} finally { db.close(); service.close(); }
|
|
5296
|
+
});
|
|
5297
|
+
|
|
5298
|
+
it('serializes peer replies with user turns, keeps captured models and leaves watches armed', async () => {
|
|
5299
|
+
let release;
|
|
5300
|
+
let started;
|
|
5301
|
+
const gate = new Promise((resolve) => { release = resolve; });
|
|
5302
|
+
const ready = new Promise((resolve) => { started = resolve; });
|
|
5303
|
+
const turns = [];
|
|
5304
|
+
const directory = root();
|
|
5305
|
+
const service = setup(directory, {}, {
|
|
5306
|
+
runContactTurn: async (turn) => {
|
|
5307
|
+
turns.push(turn);
|
|
5308
|
+
if (turn.userText === 'hold') { started(); await gate; }
|
|
5309
|
+
return { bubbles: [turn.userText === 'hold' ? 'first reply' : 'peer reply'] };
|
|
5310
|
+
},
|
|
5311
|
+
});
|
|
5312
|
+
try {
|
|
5313
|
+
const sender = service.createAssistant({ ...assistantInput, name: 'Sender' });
|
|
5314
|
+
const recipient = service.createAssistant({ ...assistantInput, name: 'Recipient' });
|
|
5315
|
+
const user = await service.send(recipient.id, { messageID: 'user_hold', parts: [{ type: 'text', text: 'hold' }] });
|
|
5316
|
+
await ready;
|
|
5317
|
+
service.appendContactCard(recipient.id, { cardType: 'session', sessionID: 'ses_still_watched', directory, title: 'Work', status: 'busy' });
|
|
5318
|
+
const peer = service.deliverPeerMessage(sender.id, { messageID: 'peer_queued', toAssistantID: recipient.id, text: 'hello' });
|
|
5319
|
+
const current = service.snapshot().assistants.find((item) => item.id === recipient.id);
|
|
5320
|
+
await service.updateAssistant(recipient.id, { expectedRevision: current.revision, modelID: 'new-model' });
|
|
5321
|
+
expect(turns).toHaveLength(1);
|
|
5322
|
+
release();
|
|
5323
|
+
await service.whenContactTurnSettled(user.messageID);
|
|
5324
|
+
await service.whenContactTurnSettled(peer.messageID);
|
|
5325
|
+
expect(turns).toHaveLength(2);
|
|
5326
|
+
expect(turns[0].history.some((item) => item.content.includes('hello'))).toBe(false);
|
|
5327
|
+
expect(turns[1].history.some((item) => item.content === 'first reply')).toBe(true);
|
|
5328
|
+
expect(turns[1].assistant.modelID).toBe('m');
|
|
5329
|
+
const db = new (require('better-sqlite3'))(path.join(directory, 'assistants.sqlite'));
|
|
5330
|
+
try { expect(db.prepare('SELECT resume_allowed FROM assistant_contact_watch WHERE session_id=?').get('ses_still_watched').resume_allowed).toBe(1); }
|
|
5331
|
+
finally { db.close(); }
|
|
5332
|
+
} finally { release(); service.close(); }
|
|
5333
|
+
});
|
|
5334
|
+
|
|
5335
|
+
it('runs peer replies through the real harness with no mutation or relay tools', async () => {
|
|
5336
|
+
const createChatCompletion = vi.fn(async (input) => {
|
|
5337
|
+
const names = (input.body.tools || []).map((tool) => tool.function.name);
|
|
5338
|
+
expect(names).not.toContain('message_assistant');
|
|
5339
|
+
expect(names).not.toContain('assign_session');
|
|
5340
|
+
expect(names).not.toContain('load_tools');
|
|
5341
|
+
expect(names).not.toContain('bash');
|
|
5342
|
+
expect(input.body.messages.some((message) => typeof message.content === 'string' && message.content.includes('老大你好'))).toBe(true);
|
|
5343
|
+
return { completion: { choices: [{ message: { content: '小小白你好,收到啦。' } }] } };
|
|
5344
|
+
});
|
|
5345
|
+
const service = setup(root(), {}, { runContactTurn: realRunContactTurn, createChatCompletion });
|
|
5346
|
+
try {
|
|
5347
|
+
const sender = service.createAssistant({ ...assistantInput, name: '小小白' });
|
|
5348
|
+
const recipient = service.createAssistant({ ...assistantInput, name: '大白' });
|
|
5349
|
+
const peer = service.deliverPeerMessage(sender.id, { toAssistantID: recipient.id, text: '老大你好' });
|
|
5350
|
+
expect(await service.whenContactTurnSettled(peer.messageID)).toMatchObject({ status: 'complete' });
|
|
5351
|
+
expect(createChatCompletion).toHaveBeenCalledTimes(1);
|
|
5352
|
+
expect(service.contactMessages(recipient.id).messages.at(-1).text).toBe('小小白你好,收到啦。');
|
|
5353
|
+
expect(service.contactMessages(sender.id).messages).toHaveLength(1);
|
|
5354
|
+
} finally { service.close(); }
|
|
5355
|
+
});
|
|
5356
|
+
|
|
5357
|
+
it('settles a failed peer reply as an error without deleting the delivered message', async () => {
|
|
5358
|
+
const onContactTurnComplete = vi.fn();
|
|
5359
|
+
const service = setup(root(), {}, {
|
|
5360
|
+
onContactTurnComplete,
|
|
5361
|
+
runContactTurn: async () => { throw new Error('model unavailable'); },
|
|
5362
|
+
});
|
|
5363
|
+
try {
|
|
5364
|
+
const sender = service.createAssistant({ ...assistantInput, name: 'Sender' });
|
|
5365
|
+
const recipient = service.createAssistant({ ...assistantInput, name: 'Recipient' });
|
|
5366
|
+
const peer = service.deliverPeerMessage(sender.id, { toAssistantID: recipient.id, text: 'hello' });
|
|
5367
|
+
expect(await service.whenContactTurnSettled(peer.messageID)).toMatchObject({ status: 'error', error: 'model unavailable' });
|
|
5368
|
+
const messages = service.contactMessages(recipient.id).messages;
|
|
5369
|
+
expect(messages.find((message) => message.messageID === peer.messageID)?.role).toBe('peer');
|
|
5370
|
+
expect(messages.at(-1)).toMatchObject({ role: 'assistant', status: 'error', text: 'model unavailable' });
|
|
5371
|
+
expect(service.snapshot().assistants.find((item) => item.id === recipient.id).working).toBe(false);
|
|
5372
|
+
expect(onContactTurnComplete).toHaveBeenCalledWith(expect.objectContaining({ assistantID: recipient.id, status: 'error', body: 'model unavailable' }));
|
|
5373
|
+
} finally { service.close(); }
|
|
5374
|
+
});
|
|
5375
|
+
|
|
5376
|
+
it('cancels an aborted peer reply and drops its late output', async () => {
|
|
5377
|
+
let release;
|
|
5378
|
+
let started;
|
|
5379
|
+
const gate = new Promise((resolve) => { release = resolve; });
|
|
5380
|
+
const ready = new Promise((resolve) => { started = resolve; });
|
|
5381
|
+
const service = setup(root(), {}, { runContactTurn: async () => { started(); await gate; return { bubbles: ['late reply'] }; } });
|
|
5382
|
+
try {
|
|
5383
|
+
const sender = service.createAssistant({ ...assistantInput, name: 'Sender' });
|
|
5384
|
+
const recipient = service.createAssistant({ ...assistantInput, name: 'Recipient' });
|
|
5385
|
+
const peer = service.deliverPeerMessage(sender.id, { toAssistantID: recipient.id, text: 'hello' });
|
|
5386
|
+
await ready;
|
|
5387
|
+
await service.abort(recipient.id, { sessionID: null, sessionGeneration: 0 });
|
|
5388
|
+
release();
|
|
5389
|
+
expect(await service.whenContactTurnSettled(peer.messageID)).toMatchObject({ status: 'cancelled' });
|
|
5390
|
+
expect(service.contactMessages(recipient.id).messages.at(-1).role).toBe('peer');
|
|
5391
|
+
} finally { release(); service.close(); }
|
|
5392
|
+
});
|
|
5393
|
+
|
|
5394
|
+
it.each(['wipe', 'remove'])('does not run a queued peer reply after recipient %s', async (action) => {
|
|
5395
|
+
const runContactTurn = vi.fn(async () => ({ bubbles: ['should not run'] }));
|
|
5396
|
+
const service = setup(root(), {}, { runContactTurn });
|
|
5397
|
+
try {
|
|
5398
|
+
const sender = service.createAssistant({ ...assistantInput, name: 'Sender' });
|
|
5399
|
+
const recipient = service.createAssistant({ ...assistantInput, name: 'Recipient' });
|
|
5400
|
+
const peer = service.deliverPeerMessage(sender.id, { toAssistantID: recipient.id, text: 'hello' });
|
|
5401
|
+
if (action === 'wipe') service.resetContact(recipient.id);
|
|
5402
|
+
if (action === 'remove') service.removeAssistant(recipient.id, recipient.revision);
|
|
5403
|
+
expect(await service.whenContactTurnSettled(peer.messageID)).toMatchObject({ status: 'cancelled' });
|
|
5404
|
+
expect(runContactTurn).not.toHaveBeenCalled();
|
|
5405
|
+
} finally { service.close(); }
|
|
5406
|
+
});
|
|
5407
|
+
|
|
5408
|
+
it('does not republish a peer reply after its inbox message is wiped mid-turn', async () => {
|
|
5409
|
+
let release;
|
|
5410
|
+
let started;
|
|
5411
|
+
const gate = new Promise((resolve) => { release = resolve; });
|
|
5412
|
+
const ready = new Promise((resolve) => { started = resolve; });
|
|
5413
|
+
const service = setup(root(), {}, {
|
|
5414
|
+
runContactTurn: async ({ onBubbleDelta }) => {
|
|
5415
|
+
started(); await gate;
|
|
5416
|
+
onBubbleDelta(0, 'late reply', true);
|
|
5417
|
+
return { bubbles: ['late reply'] };
|
|
5418
|
+
},
|
|
5419
|
+
});
|
|
5420
|
+
try {
|
|
5421
|
+
const sender = service.createAssistant({ ...assistantInput, name: 'Sender' });
|
|
5422
|
+
const recipient = service.createAssistant({ ...assistantInput, name: 'Recipient' });
|
|
5423
|
+
const peer = service.deliverPeerMessage(sender.id, { toAssistantID: recipient.id, text: 'hello' });
|
|
5424
|
+
await ready;
|
|
5425
|
+
service.resetContact(recipient.id);
|
|
5426
|
+
release();
|
|
5427
|
+
expect(await service.whenContactTurnSettled(peer.messageID)).toMatchObject({ status: 'cancelled' });
|
|
5428
|
+
expect(service.contactMessages(recipient.id).messages).toEqual([]);
|
|
5429
|
+
} finally { release(); service.close(); }
|
|
5430
|
+
});
|
|
5431
|
+
|
|
4605
5432
|
it('sends a read-only peer message from message_assistant without promptAsync', async () => {
|
|
4606
5433
|
const prompts = [];
|
|
4607
5434
|
const directory = root();
|
|
4608
5435
|
const service = setup(directory, { promptAsync: async (input) => { prompts.push(input); return { response: { status: 204 } }; } }, {
|
|
4609
|
-
runContactTurn: async ({ tools }) => {
|
|
5436
|
+
runContactTurn: async ({ tools, assistant }) => {
|
|
5437
|
+
if (assistant.name === 'PeerQA') return { bubbles: ['收到带话'] };
|
|
4610
5438
|
const message = tools.find((tool) => tool.name === 'message_assistant');
|
|
4611
5439
|
const result = await message.execute('call_1', { to: 'PeerQA', text: 'hello-from-assistant 写好了' });
|
|
4612
5440
|
return {
|
|
@@ -4623,6 +5451,8 @@ describe('assistants service', () => {
|
|
|
4623
5451
|
messageID: 'client_message_assistant',
|
|
4624
5452
|
parts: [{ type: 'text', text: '给 PeerQA 说一声 hello-from-assistant 写好了' }],
|
|
4625
5453
|
});
|
|
5454
|
+
const peerMessage = service.contactMessages(recipient.id).messages.find((message) => message.role === 'peer');
|
|
5455
|
+
await service.whenContactTurnSettled(peerMessage.messageID);
|
|
4626
5456
|
expect(prompts).toEqual([]);
|
|
4627
5457
|
expect(service.contactMessages(recipient.id).messages).toEqual([expect.objectContaining({
|
|
4628
5458
|
role: 'assistant', text: "Hi, I'm PeerQA. How can I help you?",
|
|
@@ -4631,7 +5461,7 @@ describe('assistants service', () => {
|
|
|
4631
5461
|
fromAssistantID: sender.id,
|
|
4632
5462
|
fromAssistantName: 'DeepSeekQA',
|
|
4633
5463
|
text: 'hello-from-assistant 写好了',
|
|
4634
|
-
})]);
|
|
5464
|
+
}), expect.objectContaining({ role: 'assistant', text: '收到带话' })]);
|
|
4635
5465
|
const senderPage = service.contactMessages(sender.id);
|
|
4636
5466
|
expect(senderPage.messages.some((message) => message.role === 'user')).toBe(true);
|
|
4637
5467
|
expect(senderPage.messages.some((message) => message.text.includes('Sent to PeerQA'))).toBe(true);
|
|
@@ -4639,7 +5469,7 @@ describe('assistants service', () => {
|
|
|
4639
5469
|
service.close();
|
|
4640
5470
|
});
|
|
4641
5471
|
|
|
4642
|
-
it('delivers a
|
|
5472
|
+
it('delivers a peer DM and replies in the recipient contact transcript without OpenCode', async () => {
|
|
4643
5473
|
const prompts = [];
|
|
4644
5474
|
const directory = root();
|
|
4645
5475
|
const service = setup(directory, { promptAsync: async (input) => { prompts.push(input); return { response: { status: 204 } }; } });
|
|
@@ -4657,8 +5487,9 @@ describe('assistants service', () => {
|
|
|
4657
5487
|
toAssistantID: recipient.id,
|
|
4658
5488
|
});
|
|
4659
5489
|
expect(prompts).toEqual([]);
|
|
5490
|
+
await service.whenContactTurnSettled(delivered.messageID);
|
|
4660
5491
|
const page = service.contactMessages(recipient.id);
|
|
4661
|
-
expect(page.messages).toHaveLength(
|
|
5492
|
+
expect(page.messages).toHaveLength(3);
|
|
4662
5493
|
expect(page.messages[1]).toMatchObject({
|
|
4663
5494
|
role: 'peer',
|
|
4664
5495
|
fromAssistantID: sender.id,
|
|
@@ -4671,7 +5502,7 @@ describe('assistants service', () => {
|
|
|
4671
5502
|
service.close();
|
|
4672
5503
|
});
|
|
4673
5504
|
|
|
4674
|
-
it('rejects self-DMs and
|
|
5505
|
+
it('rejects self-DMs and keeps peer replies distinct from composer turns', async () => {
|
|
4675
5506
|
const service = setup();
|
|
4676
5507
|
const sender = service.createAssistant({ ...assistantInput, name: 'Sender' });
|
|
4677
5508
|
const recipient = service.createAssistant({ ...assistantInput, name: 'Recipient' });
|
|
@@ -4679,14 +5510,15 @@ describe('assistants service', () => {
|
|
|
4679
5510
|
toAssistantID: sender.id,
|
|
4680
5511
|
text: 'loop',
|
|
4681
5512
|
})).toThrow('validation_error');
|
|
4682
|
-
service.deliverPeerMessage(sender.id, { toAssistantID: recipient.id, text: 'ping' });
|
|
5513
|
+
const peer = service.deliverPeerMessage(sender.id, { toAssistantID: recipient.id, text: 'ping' });
|
|
5514
|
+
await service.whenContactTurnSettled(peer.messageID);
|
|
4683
5515
|
const sent = await settleSend(service, recipient.id, {
|
|
4684
5516
|
messageID: 'after-peer',
|
|
4685
5517
|
parts: [{ type: 'text', text: 'hello' }],
|
|
4686
5518
|
});
|
|
4687
5519
|
expect(sent.admitted).toBe(true);
|
|
4688
5520
|
const roles = service.contactMessages(recipient.id).messages.map((message) => message.role);
|
|
4689
|
-
expect(roles).toEqual(['assistant', 'peer', 'user', 'assistant']);
|
|
5521
|
+
expect(roles).toEqual(['assistant', 'peer', 'assistant', 'user', 'assistant']);
|
|
4690
5522
|
service.close();
|
|
4691
5523
|
});
|
|
4692
5524
|
|
|
@@ -4870,7 +5702,7 @@ describe('assistants service', () => {
|
|
|
4870
5702
|
service.close();
|
|
4871
5703
|
});
|
|
4872
5704
|
|
|
4873
|
-
it('
|
|
5705
|
+
it('persists contact errors across restart until a new message overwrites them', async () => {
|
|
4874
5706
|
const directory = root();
|
|
4875
5707
|
const events = [];
|
|
4876
5708
|
const service = setup(directory, {}, {
|
|
@@ -4891,6 +5723,7 @@ describe('assistants service', () => {
|
|
|
4891
5723
|
expect(page.messages.map((message) => ({ role: message.role, status: message.status, text: message.text }))).toEqual([
|
|
4892
5724
|
{ role: 'assistant', status: 'complete', text: greetingText },
|
|
4893
5725
|
{ role: 'user', status: 'complete', text: 'hi' },
|
|
5726
|
+
{ role: 'assistant', status: 'error', text: 'No connected model' },
|
|
4894
5727
|
]);
|
|
4895
5728
|
expect(service.snapshot().assistants[0]).toMatchObject({ working: false, activeContactTurn: null });
|
|
4896
5729
|
expect(events).toContainEqual(expect.objectContaining({
|
|
@@ -4901,15 +5734,81 @@ describe('assistants service', () => {
|
|
|
4901
5734
|
const restarted = setup(directory, {}, {
|
|
4902
5735
|
runContactTurn: async () => ({ text: 'ok', bubbles: ['ok'] }),
|
|
4903
5736
|
});
|
|
4904
|
-
expect(restarted.contactMessages(assistant.id).messages.map((message) => message.text)).toEqual([greetingText, 'hi']);
|
|
5737
|
+
expect(restarted.contactMessages(assistant.id).messages.map((message) => message.text)).toEqual([greetingText, 'hi', 'No connected model']);
|
|
4905
5738
|
await restarted.send(assistant.id, {
|
|
4906
5739
|
messageID: 'after_failure', parts: [{ type: 'text', text: 'again' }],
|
|
4907
5740
|
});
|
|
5741
|
+
expect(restarted.contactMessages(assistant.id).messages.some((message) => message.status === 'error')).toBe(false);
|
|
4908
5742
|
await restarted.whenContactTurnSettled('after_failure');
|
|
4909
5743
|
expect(restarted.contactMessages(assistant.id).messages.map((message) => message.text)).toEqual([greetingText, 'hi', 'again', 'ok']);
|
|
4910
5744
|
restarted.close();
|
|
4911
5745
|
});
|
|
4912
5746
|
|
|
5747
|
+
it.each(['throw', 'empty'])('persists %s background failures and clears them on the next assistant update', async (failure) => {
|
|
5748
|
+
const directory = root();
|
|
5749
|
+
let failBackground = true;
|
|
5750
|
+
const service = setup(directory, {}, {
|
|
5751
|
+
listProjects: async () => [{ id: 'project', path: directory }],
|
|
5752
|
+
upsertScheduledTask: async (_project, task) => ({ task: { ...task, id: 'task' } }),
|
|
5753
|
+
syncScheduledTaskProject: async () => {},
|
|
5754
|
+
runContactTurn: async ({ readOnly, tools }) => {
|
|
5755
|
+
if (!readOnly) {
|
|
5756
|
+
await tools.find((tool) => tool.name === 'schedule_task').execute('schedule', {
|
|
5757
|
+
name: 'Daily task', prompt: 'Research', time: '18:00', timezone: 'UTC',
|
|
5758
|
+
});
|
|
5759
|
+
return { bubbles: ['Scheduled'] };
|
|
5760
|
+
}
|
|
5761
|
+
if (failBackground) {
|
|
5762
|
+
if (failure === 'throw') throw new Error('Model unavailable');
|
|
5763
|
+
return { bubbles: [] };
|
|
5764
|
+
}
|
|
5765
|
+
return { bubbles: ['Recovered'] };
|
|
5766
|
+
},
|
|
5767
|
+
});
|
|
5768
|
+
try {
|
|
5769
|
+
const owner = service.createAssistant(assistantInput);
|
|
5770
|
+
await settleSend(service, owner.id, { messageID: 'schedule', parts: [{ type: 'text', text: 'Schedule' }] });
|
|
5771
|
+
const run = { id: 'failure', projectId: 'project', taskId: 'task', taskName: 'Task', status: 'success', sessionId: 'worker', directory, startedAt: Date.now(), finishedAt: Date.now() };
|
|
5772
|
+
await service.reportScheduledTaskResult(run);
|
|
5773
|
+
expect(service.contactMessages(owner.id).messages.at(-1)).toMatchObject({
|
|
5774
|
+
status: 'error', text: failure === 'throw' ? 'Model unavailable' : 'Assistant returned no text',
|
|
5775
|
+
});
|
|
5776
|
+
failBackground = false;
|
|
5777
|
+
await service.reportScheduledTaskResult({ ...run, id: 'recovery' });
|
|
5778
|
+
expect(service.contactMessages(owner.id).messages.at(-1)?.text).toBe('Recovered');
|
|
5779
|
+
expect(service.contactMessages(owner.id).messages.some((message) => message.status === 'error')).toBe(false);
|
|
5780
|
+
expect(service.contactMessages(owner.id).messages.filter((message) => message.role === 'user')).toHaveLength(1);
|
|
5781
|
+
} finally {
|
|
5782
|
+
service.close();
|
|
5783
|
+
}
|
|
5784
|
+
});
|
|
5785
|
+
|
|
5786
|
+
it('keeps live error feedback and settles activity when persisting the error fails', async () => {
|
|
5787
|
+
const directory = root();
|
|
5788
|
+
const events = [];
|
|
5789
|
+
const service = setup(directory, {}, {
|
|
5790
|
+
onContactTurnEvent: (event) => events.push(event),
|
|
5791
|
+
runContactTurn: async () => { throw new Error('Model unavailable'); },
|
|
5792
|
+
});
|
|
5793
|
+
const db = new (require('better-sqlite3'))(path.join(directory, 'assistants.sqlite'));
|
|
5794
|
+
const warning = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
5795
|
+
try {
|
|
5796
|
+
const owner = service.createAssistant(assistantInput);
|
|
5797
|
+
db.exec(`CREATE TRIGGER reject_error BEFORE INSERT ON assistant_contact_message
|
|
5798
|
+
WHEN NEW.status = 'error' BEGIN SELECT RAISE(ABORT, 'write failed'); END;`);
|
|
5799
|
+
const result = await settleSend(service, owner.id, { messageID: 'failed_write', parts: [{ type: 'text', text: 'Hi' }] });
|
|
5800
|
+
expect(result.settled).toMatchObject({ status: 'error', error: 'Model unavailable' });
|
|
5801
|
+
expect(service.snapshot().assistants[0]).toMatchObject({ working: false, activeContactTurn: null });
|
|
5802
|
+
expect(service.contactMessages(owner.id).messages.map((message) => message.text)).toEqual([greetingText, 'Hi']);
|
|
5803
|
+
expect(events).toContainEqual(expect.objectContaining({ type: 'yuri:contact-turn-end', properties: expect.objectContaining({ status: 'error' }) }));
|
|
5804
|
+
expect(warning).toHaveBeenCalledWith('[assistants] contact error could not be persisted');
|
|
5805
|
+
} finally {
|
|
5806
|
+
warning.mockRestore();
|
|
5807
|
+
db.close();
|
|
5808
|
+
service.close();
|
|
5809
|
+
}
|
|
5810
|
+
});
|
|
5811
|
+
|
|
4913
5812
|
it('bumps revision when queued contact turn becomes running', async () => {
|
|
4914
5813
|
const directory = root();
|
|
4915
5814
|
let release;
|
|
@@ -5052,10 +5951,11 @@ describe('assistants service', () => {
|
|
|
5052
5951
|
&& event.properties?.status === 'error'
|
|
5053
5952
|
&& event.properties?.turnID === 'client_no_provider'
|
|
5054
5953
|
))).toBe(true);
|
|
5055
|
-
// User row stays; error feedback is
|
|
5954
|
+
// User row stays; error feedback is also recoverable without the live event.
|
|
5056
5955
|
expect(service.contactMessages(assistant.id).messages.map((message) => ({ role: message.role, status: message.status }))).toEqual([
|
|
5057
5956
|
{ role: 'assistant', status: 'complete' },
|
|
5058
5957
|
{ role: 'user', status: 'complete' },
|
|
5958
|
+
{ role: 'assistant', status: 'error' },
|
|
5059
5959
|
]);
|
|
5060
5960
|
service.close();
|
|
5061
5961
|
});
|