@yuriai/web 0.2.31 → 0.2.32-beta.10
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 +19 -0
- package/dist/assets/{AgentAvatar-DLAQkFQe.js → AgentAvatar-DLzcNrQ0.js} +1 -1
- package/dist/assets/AssistantContactStatus-Bf1yg0yx.js +3 -0
- package/dist/assets/{AssistantUnreadBadge-BynxNQMk.js → AssistantUnreadBadge-BUFyxUrT.js} +1 -1
- package/dist/assets/{AssistantView-CDZyA9ft.js → AssistantView-CELFybSr.js} +1 -1
- package/dist/assets/{DiagramView-DdVFGHQQ.js → DiagramView-DV4P3-Wg.js} +1 -1
- package/dist/assets/{FadeInOnReveal-C9a7XnAh.js → FadeInOnReveal-BRrU2ivL.js} +1 -1
- package/dist/assets/{GuiPluginSurface-v8r-Hex5.js → GuiPluginSurface-NOHaJGy2.js} +1 -1
- package/dist/assets/{GuiPluginWindow-Ba1HfrjQ.js → GuiPluginWindow-BykPaQeY.js} +1 -1
- package/dist/assets/Icon-OJf0qBKf.js +1 -0
- package/dist/assets/{ImageLoadingPlaceholder-Dcy5lc7F.js → ImageLoadingPlaceholder-DlQbo9CM.js} +1 -1
- package/dist/assets/{LoadingImage-BxjJ_Xyj.js → LoadingImage-sZ5omN5L.js} +1 -1
- package/dist/assets/MarkdownRenderer-BFR9FWQL.js +2 -0
- package/dist/assets/MarkdownRendererImpl-iZFUMI3M.js +10 -0
- package/dist/assets/{MarkstreamRendererImpl-EpBnVy9J.css → MarkstreamRendererImpl-C6aiJnjl.css} +1 -1
- package/dist/assets/MarkstreamRendererImpl-DjC_Y4LS.js +3 -0
- package/dist/assets/{MobileDetailNavigation-o54juFXD.js → MobileDetailNavigation-BOZpHdI2.js} +1 -1
- package/dist/assets/{MobileResizableSheet-sV9XR-0k.js → MobileResizableSheet-DuAi_epR.js} +1 -1
- package/dist/assets/{MobileShareBridge-oqBZ_f6M.js → MobileShareBridge-D1DrqLWf.js} +1 -1
- package/dist/assets/MobileTabPageHeader-BT9gWugr.js +2 -0
- package/dist/assets/{MultiRunWindow-7W_86vL6.js → MultiRunWindow-C0GYNaUU.js} +1 -1
- package/dist/assets/{OnboardingScreen-CnkijSLE.js → OnboardingScreen-CqsI0632.js} +1 -1
- package/dist/assets/{OverlayScrollbar-CCtBTJQj.js → OverlayScrollbar-CZD1yU06.js} +1 -1
- package/dist/assets/SettingsView-BYO8LY5A.js +29 -0
- package/dist/assets/{SettingsWindow-C0HiNvXh.js → SettingsWindow-CM0Ph9WD.js} +1 -1
- package/dist/assets/SetupTourDialog-BYlSBPKZ.js +1 -0
- package/dist/assets/SetupTourDialog-DrL_4ODI.css +1 -0
- package/dist/assets/{TerminalView-y36H_fIe.js → TerminalView-DnyEAShU.js} +1 -1
- package/dist/assets/{ToolOutputDialog-BfjBeqGS.js → ToolOutputDialog-BWONfWze.js} +1 -1
- package/dist/assets/{VoiceSettings-BONwzGgR.js → VoiceSettings-Ru_0ySgM.js} +2 -2
- package/dist/assets/{YuriLogo-BA5FWp-g.js → YuriLogo-CMUoGUHc.js} +1 -1
- package/dist/assets/YuriWordmark-DHYo0FlR.js +1 -0
- package/dist/assets/{appThemeRegistry-BsLMp3bc.js → appThemeRegistry-BMlAlm0m.js} +1 -1
- package/dist/assets/{button-CBjOhOI0.js → button-C-gRcs_2.js} +1 -1
- package/dist/assets/{capgoAdapter-DjlyVxy_.js → capgoAdapter-GdNfjwXs.js} +1 -1
- package/dist/assets/checkbox-BrJBzET8.js +1 -0
- package/dist/assets/{collapsible-B0j5IQ0T.js → collapsible-B7esGYLd.js} +1 -1
- package/dist/assets/{context-menu-C4e_bpls.js → context-menu-DWn1nSZC.js} +1 -1
- package/dist/assets/desktopHosts-BLU1tGSX.js +1 -0
- package/dist/assets/{desktopRelayRestore-AE3_TnQz.js → desktopRelayRestore-2BIIs8mZ.js} +1 -1
- package/dist/assets/{diagram-WPmk0Qh6.js → diagram-Bnra5_6g.js} +1 -1
- package/dist/assets/{dialog-Dx3IT7dQ.js → dialog-3UseH0hM.js} +1 -1
- package/dist/assets/{es-JU--16Gd.js → es-J7a_sZ1u.js} +2 -2
- package/dist/assets/{fileMentionTouchSelection-Bizsisaj.js → fileMentionTouchSelection-ByaQJcTS.js} +1 -1
- package/dist/assets/{fr-BUZcRuMk.js → fr-BYxUMKWp.js} +2 -2
- package/dist/assets/{gitApi-CNgEj6X5.js → gitApi-BdwvoQFb.js} +1 -1
- package/dist/assets/historyImportQueries-C8VK9R5_.js +1 -0
- package/dist/assets/host-z9PU8q1e.js +1 -0
- package/dist/assets/{input-CEJGM-TS.js → input-CmLlZdeD.js} +1 -1
- package/dist/assets/insertionBoundaries-BpQ6nLjP.js +1 -0
- package/dist/assets/{ja-BwtVLTXD.js → ja-DCdBiJmD.js} +2 -2
- package/dist/assets/{ko--sLsRFpA.js → ko-D-Nb_RJY.js} +2 -2
- package/dist/assets/main-Dosy2jXg.js +2 -0
- package/dist/assets/main-oBh-dk5-.js +169 -0
- package/dist/assets/{markdownHeightCache-D6iSNJG2.js → markdownHeightCache-xNkQdlqi.js} +1 -1
- package/dist/assets/{mcpQueries-DVSsZeuk.js → mcpQueries-C8zsTQqJ.js} +1 -1
- package/dist/assets/miniChat-CI8ryDGT.js +2 -0
- package/dist/assets/mobile-e_mTK3dh.js +2 -0
- package/dist/assets/{mobileBackNavigation-Dmbf0kw1.js → mobileBackNavigation-sB7c0Pi4.js} +1 -1
- package/dist/assets/multirun-DbmgWrsY.js +3 -0
- package/dist/assets/{native-config-BaRHaY4y.js → native-config-C_mwkFzh.js} +1 -1
- package/dist/assets/{pl-C4_4J5yA.js → pl-AmU4w3aM.js} +2 -2
- package/dist/assets/{projectMeta-rrpZdm8D.js → projectMeta-1iUbVl_M.js} +1 -1
- package/dist/assets/{pt-BR-DvNC5K1q.js → pt-BR-BI4ft-Fj.js} +2 -2
- package/dist/assets/pwa-BdUAGhLt.js +2 -0
- package/dist/assets/{queue-abort-optimistic-CSkERbr4.js → queue-abort-optimistic-BwDsaym5.js} +1 -1
- package/dist/assets/react-a6PrrNTm.js +1 -0
- package/dist/assets/{readiness-BnO7IaZV.js → readiness-D2N0DtZF.js} +1 -1
- package/dist/assets/{renderElectronMiniChatApp-Bq3iNKtr.js → renderElectronMiniChatApp-DV0cNt_O.js} +2 -2
- package/dist/assets/{renderMobileApp-DiNeMgwp.js → renderMobileApp-b7--jT5z.js} +7 -7
- package/dist/assets/{runtimeConfig-BHOiBANF.js → runtimeConfig-DxmJLWUO.js} +1 -1
- package/dist/assets/runtimeEndpointReset-jYAYa3Rh.js +149 -0
- package/dist/assets/{select-bKMw5JE8.js → select-J8r9SxHt.js} +1 -1
- package/dist/assets/sessionLookup-CUQqj2N4.js +1 -0
- package/dist/assets/{sortable-tabs-strip-3sKiA7T1.js → sortable-tabs-strip-CiP6RIQa.js} +1 -1
- package/dist/assets/{speechSettingsQueries-BDDxw8LP.js → speechSettingsQueries-B3ziGQvb.js} +1 -1
- package/dist/assets/src-GtPQ8f4o.css +2 -0
- package/dist/assets/{store-X3lU0obe.js → store-OjrIiXCx.js} +5 -5
- package/dist/assets/{textarea-EsaDD051.js → textarea-BHgcxJdC.js} +1 -1
- package/dist/assets/{tooltip-LLDL5nIZ.js → tooltip-Degmpxnd.js} +1 -1
- package/dist/assets/tour-DDI0Zg7l.js +1 -0
- package/dist/assets/types-QETHY-S7.js +2 -0
- package/dist/assets/{uk-Dvj0JjiC.js → uk-CbdByxxj.js} +2 -2
- package/dist/assets/{useAppFontEffects-wIWWE59b.js → useAppFontEffects-LADr3Zl1.js} +53 -53
- package/dist/assets/useDesktopWindowControlsLayout-BVNnRHtc.js +1 -0
- package/dist/assets/{useDictation-DlEHSN6P.js → useDictation-DFVJpRbm.js} +1 -1
- package/dist/assets/{useEffectiveDirectory-DzqpWUC3.js → useEffectiveDirectory-8tqfGdV5.js} +1 -1
- package/dist/assets/{useMobileNavigationStore-BWLhXqci.js → useMobileNavigationStore-CLUFv-TZ.js} +13 -13
- package/dist/assets/usePluginsStore-B_SIDwsK.js +1 -0
- package/dist/assets/useStreamingTextThrottle-VhgGU_te.js +1 -0
- package/dist/assets/{useWorkerHighlightedLines-DMS69wKa.js → useWorkerHighlightedLines-jlxakDGs.js} +1 -1
- package/dist/assets/{vendor-markstream-react-DfLaSG3W.js → vendor-markstream-react-6zxyQLCD.js} +1 -1
- package/dist/assets/{zh-CN-2Y6u8t24.js → zh-CN-ufEVDbXp.js} +2 -2
- package/dist/assets/{zh-TW-Z5wVv4Fb.js → zh-TW-DTBlZ4YC.js} +2 -2
- package/dist/index.html +8 -8
- package/dist/mini-chat.html +8 -8
- package/dist/mobile.html +8 -8
- package/package.json +1 -1
- package/server/index.js +3 -0
- package/server/lib/client-auth/pairing.js +2 -0
- package/server/lib/client-auth/pairing.test.js +29 -0
- package/server/lib/config-sync/DOCUMENTATION.md +8 -4
- package/server/lib/config-sync/index.js +1 -1
- package/server/lib/config-sync/magpie-auth.js +10 -4
- package/server/lib/config-sync/magpie-auth.test.js +4 -0
- package/server/lib/config-sync/magpie.js +28 -22
- package/server/lib/config-sync/magpie.test.js +127 -2
- package/server/lib/config-sync/plugins.js +44 -0
- package/server/lib/config-sync/plugins.test.js +64 -0
- package/server/lib/config-sync/tar.js +61 -1
- package/server/lib/content-search/DOCUMENTATION.md +9 -0
- package/server/lib/content-search/routes.js +18 -1
- package/server/lib/content-search/routes.test.js +21 -0
- package/server/lib/content-search/runtime.js +7 -3
- package/server/lib/content-search/runtime.test.js +26 -0
- package/server/lib/gui-customization/DOCUMENTATION.md +21 -1
- package/server/lib/gui-customization/session-skill.js +2 -2
- package/server/lib/gui-customization/session-skill.test.js +3 -1
- package/server/lib/gui-customization/skill/SKILL.md +5 -1
- package/server/lib/gui-customization/skill/references/global-pages.md +1 -1
- package/server/lib/gui-customization/skill/references/gui-api.md +4 -1
- package/server/lib/gui-customization/skill/references/icons.md +134 -0
- package/server/lib/gui-customization/skill/references/plugin-styles.md +112 -0
- package/server/lib/gui-customization/skill/references/ui-surfaces.md +2 -0
- package/server/lib/gui-customization/skill/references/work-status.md +110 -0
- package/server/lib/history-import/AGENT_CONNECT_LICENSE +21 -0
- package/server/lib/history-import/DOCUMENTATION.md +223 -0
- package/server/lib/history-import/dev-proxy.integration.test.js +56 -0
- package/server/lib/history-import/events.js +322 -0
- package/server/lib/history-import/events.test.js +110 -0
- package/server/lib/history-import/installations.js +39 -0
- package/server/lib/history-import/installations.test.js +24 -0
- package/server/lib/history-import/native.js +49 -0
- package/server/lib/history-import/native.test.js +42 -0
- package/server/lib/history-import/routes.js +48 -0
- package/server/lib/history-import/routes.test.js +55 -0
- package/server/lib/history-import/service.js +374 -0
- package/server/lib/history-import/service.test.js +538 -0
- package/server/lib/history-import/settings.js +146 -0
- package/server/lib/history-import/settings.test.js +88 -0
- package/server/lib/history-import/sources.js +184 -0
- package/server/lib/history-import/sources.test.js +190 -0
- package/server/lib/history-import/sqlite.js +187 -0
- package/server/lib/history-import/store.js +58 -0
- package/server/lib/history-import/store.test.js +84 -0
- package/server/lib/history-import/targets.js +24 -0
- package/server/lib/host-update/DOCUMENTATION.md +6 -0
- package/server/lib/host-update/routes.test.js +1 -1
- package/server/lib/notifications/native-stream.relay.test.js +1 -0
- package/server/lib/opencode/DOCUMENTATION.md +14 -0
- package/server/lib/opencode/core-routes.js +1 -0
- package/server/lib/opencode/feature-routes-runtime.js +11 -1
- package/server/lib/opencode/plugin-routes.test.js +1 -0
- package/server/lib/opencode/plugins.js +1 -1
- package/server/lib/opencode/plugins.test.js +3 -2
- package/server/lib/opencode/proxy.js +29 -4
- package/server/lib/opencode/proxy.test.js +85 -1
- package/server/lib/opencode/server-utils-runtime.js +2 -0
- package/server/lib/opencode/settings-runtime.js +8 -6
- package/server/lib/opencode/settings-runtime.test.js +44 -0
- package/server/lib/opencode/yuri-routes.js +1 -1
- package/server/lib/relay/DOCUMENTATION.md +1 -1
- package/server/lib/relay/host-client.js +3 -1
- package/server/lib/relay/host-client.test.js +1 -0
- package/server/lib/relay/reasoning-projection.e2e.test.ts +1 -0
- package/server/lib/relay/relay-server.e2e.test.ts +32 -17
- package/server/lib/relay/service.js +2 -0
- package/server/lib/relay/tunnel-host.js +26 -1
- package/server/lib/relay/tunnel-host.test.js +27 -3
- package/server/lib/remote-desktop/runtime.test.js +2 -2
- package/server/lib/ui-auth/DOCUMENTATION.md +3 -0
- package/server/lib/ui-auth/ui-auth.js +27 -0
- package/server/lib/ui-auth/ui-auth.test.js +42 -1
- package/server/plugin-gui-proxy.test.js +1 -1
- package/dist/assets/AssistantContactStatus-DZGGspw4.js +0 -3
- package/dist/assets/Icon-Bh3TaKoN.js +0 -1
- package/dist/assets/MarkdownRenderer-CS6Pia5D.js +0 -2
- package/dist/assets/MarkdownRendererImpl-Bdwe3qUr.js +0 -10
- package/dist/assets/MarkstreamRendererImpl-CCQj9IkL.js +0 -3
- package/dist/assets/MobileTabPageHeader-CptcDWNS.js +0 -2
- package/dist/assets/SettingsView-CkJpYIMe.js +0 -29
- package/dist/assets/SetupTourDialog-BcEbaEO3.js +0 -1
- package/dist/assets/SetupTourDialog-CUzqT2sw.css +0 -1
- package/dist/assets/YuriWordmark-Cr1vkcue.js +0 -1
- package/dist/assets/desktopHosts-CMglnUdS.js +0 -1
- package/dist/assets/host-CBfSyi4l.js +0 -1
- package/dist/assets/insertionBoundaries-CgYf6gys.js +0 -1
- package/dist/assets/main-B8FcBwPM.js +0 -169
- package/dist/assets/main-DrqtyqDc.js +0 -2
- package/dist/assets/miniChat-CgeCl4RY.js +0 -2
- package/dist/assets/mobile-DAhJKHUq.js +0 -2
- package/dist/assets/multirun-BbWnHeaY.js +0 -3
- package/dist/assets/pwa-Dm_DJEr9.js +0 -2
- package/dist/assets/react-Bwr5-XPi.js +0 -1
- package/dist/assets/runtimeEndpointReset-DGxT24oE.js +0 -149
- package/dist/assets/sessionLookup-DGNxfDsP.js +0 -1
- package/dist/assets/src-PTWgTO6W.css +0 -2
- package/dist/assets/tour-Db44qwfs.js +0 -1
- package/dist/assets/types-Cl7nTpsb.js +0 -2
- package/dist/assets/useDesktopWindowControlsLayout-CdUr9mCZ.js +0 -1
- package/dist/assets/usePluginsStore-aZl0WTK8.js +0 -1
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
4
|
+
import { parse } from 'jsonc-parser';
|
|
5
|
+
import { readConfigLayers, writeConfig } from '../opencode/config-files.js';
|
|
6
|
+
import { digest, fail } from './events.js';
|
|
7
|
+
import { resolveImportDirectory } from './targets.js';
|
|
8
|
+
|
|
9
|
+
const MAX_RULE_BYTES = 512 * 1024;
|
|
10
|
+
const roots = { claude: ['CLAUDE.md', '.claude/rules', '.claude/skills'], codex: ['AGENTS.md', '.agents/skills', '.codex/skills'],
|
|
11
|
+
cursor: ['.cursorrules', '.cursor/rules', '.cursor/skills'], dsh: ['AGENTS.md', '.agents/skills', '.dsh/skills'] };
|
|
12
|
+
|
|
13
|
+
async function readOptional(file) {
|
|
14
|
+
try { const stat = await fs.lstat(file);
|
|
15
|
+
if (stat.isSymbolicLink() || !stat.isFile()) fail('unsafe_settings_path');
|
|
16
|
+
if (stat.size > MAX_RULE_BYTES) fail('settings_too_large');
|
|
17
|
+
return await fs.readFile(file, 'utf8');
|
|
18
|
+
} catch (error) { if (error.code === 'ENOENT') return null; throw error; }
|
|
19
|
+
}
|
|
20
|
+
async function* ruleFiles(base, relative, depth = 0) {
|
|
21
|
+
if (depth > 8) fail('settings_too_large');
|
|
22
|
+
const file = path.join(base, relative); let stat;
|
|
23
|
+
try { stat = await fs.lstat(file); } catch (error) { if (error.code === 'ENOENT') return; throw error; }
|
|
24
|
+
if (stat.isSymbolicLink()) fail('unsafe_settings_path');
|
|
25
|
+
if (stat.isDirectory()) {
|
|
26
|
+
for (const entry of await fs.readdir(file)) yield* ruleFiles(base, path.join(relative, entry), depth + 1);
|
|
27
|
+
} else if (stat.isFile() && (/\.(?:md|mdc)$/i.test(relative) || path.basename(relative) === '.cursorrules')) yield relative;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Destination writes cannot follow symlinks out of the approved project. The
|
|
31
|
+
// same check runs at preview and immediately before each write.
|
|
32
|
+
async function targetPath(directory, relative) {
|
|
33
|
+
if (!relative || path.isAbsolute(relative) || relative.split(/[\\/]/).includes('..')) fail('unsafe_settings_path');
|
|
34
|
+
const real = await resolveImportDirectory(directory, true);
|
|
35
|
+
if (real !== directory) fail('target_changed');
|
|
36
|
+
let current = real;
|
|
37
|
+
for (const segment of relative.split(path.sep)) {
|
|
38
|
+
current = path.join(current, segment);
|
|
39
|
+
try { if ((await fs.lstat(current)).isSymbolicLink()) fail('unsafe_settings_path'); }
|
|
40
|
+
catch (error) { if (error.code !== 'ENOENT') throw error; }
|
|
41
|
+
}
|
|
42
|
+
return current;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export async function planSettings(source, sourceDirectory, directory) {
|
|
46
|
+
if (!path.isAbsolute(sourceDirectory)) return [];
|
|
47
|
+
let sourceRoot;
|
|
48
|
+
try { sourceRoot = await fs.realpath(sourceDirectory); }
|
|
49
|
+
catch (error) { if (error.code === 'ENOENT') return []; throw error; }
|
|
50
|
+
const result = [];
|
|
51
|
+
for (const root of roots[source]) {
|
|
52
|
+
for await (const relative of ruleFiles(sourceRoot, root)) {
|
|
53
|
+
if (result.length >= 200) fail('settings_too_large');
|
|
54
|
+
const content = await readOptional(path.join(sourceRoot, relative));
|
|
55
|
+
const skill = /(?:^|[/\\])skills[/\\]/.test(relative);
|
|
56
|
+
// Never load supporting scripts/config files as executable capabilities.
|
|
57
|
+
if (skill && path.basename(relative) !== 'SKILL.md') continue;
|
|
58
|
+
const scope = path.dirname(relative);
|
|
59
|
+
const target = skill ? path.join('.opencode', 'skills', `imported-${source}-${digest(`${sourceRoot}:${scope}`).slice(0, 12)}`, 'SKILL.md')
|
|
60
|
+
: path.join('.opencode', 'imported-rules', `${source}-${digest(`${sourceRoot}:${relative}`).slice(0, 16)}.md`);
|
|
61
|
+
const destination = await targetPath(directory, target);
|
|
62
|
+
const existing = await readOptional(destination);
|
|
63
|
+
// OpenCode already discovers a root AGENTS.md/CLAUDE.md in-place.
|
|
64
|
+
const native = sourceRoot === directory && ['AGENTS.md', 'CLAUDE.md'].includes(relative);
|
|
65
|
+
const scoped = /\b(?:globs|paths|alwaysApply)\s*:/.test(content);
|
|
66
|
+
const activated = !skill && !native && !scoped;
|
|
67
|
+
result.push({ key: digest(`${directory}:${sourceRoot}:${source}:${relative}`), source, sourceDirectory: sourceRoot, relative,
|
|
68
|
+
directory, target, kind: skill ? 'skill' : 'rule', content, hash: digest(content),
|
|
69
|
+
beforeHash: existing === null ? null : digest(existing), status: native || existing === content ? 'unchanged' : existing === null ? 'create' : 'conflict',
|
|
70
|
+
activated, scoped });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function configSnapshot(directory) {
|
|
77
|
+
const candidates = ['opencode.json', 'opencode.jsonc', path.join('.opencode', 'opencode.json'), path.join('.opencode', 'opencode.jsonc')];
|
|
78
|
+
let file = await targetPath(directory, path.join('.opencode', 'opencode.jsonc')); let text = null;
|
|
79
|
+
for (const relative of candidates) {
|
|
80
|
+
const candidate = await targetPath(directory, relative); const content = await readOptional(candidate);
|
|
81
|
+
if (content !== null) { file = candidate; text = content; }
|
|
82
|
+
}
|
|
83
|
+
const errors = []; const config = text === null ? {} : parse(text.replace(/^\uFEFF/, ''), errors, { allowTrailingComma: true, allowEmptyContent: true }) ?? {};
|
|
84
|
+
if (errors.length || !config || typeof config !== 'object' || Array.isArray(config)
|
|
85
|
+
|| config.instructions !== undefined && (!Array.isArray(config.instructions) || config.instructions.some((entry) => typeof entry !== 'string'))) fail('invalid_target_config');
|
|
86
|
+
// Preserve effective inherited instructions, not only the touched document.
|
|
87
|
+
const layers = readConfigLayers(directory);
|
|
88
|
+
const instructions = layers.mergedConfig.instructions ?? [];
|
|
89
|
+
if (!Array.isArray(instructions) || instructions.some((entry) => typeof entry !== 'string')) fail('invalid_target_config');
|
|
90
|
+
const hash = digest({ file, text, instructions });
|
|
91
|
+
return { file, config, instructions, hash };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export async function settingsConfigFingerprint(directory) { return (await configSnapshot(directory)).hash; }
|
|
95
|
+
|
|
96
|
+
export async function applySettings(item, decision, configHash) {
|
|
97
|
+
if (!['keep', 'replace', 'copy'].includes(decision)) fail('invalid_selection');
|
|
98
|
+
if (item.status === 'unchanged' || item.status === 'conflict' && decision === 'keep') return { status: 'skipped' };
|
|
99
|
+
const sourceText = await readOptional(path.join(item.sourceDirectory, item.relative));
|
|
100
|
+
if (sourceText === null || digest(sourceText) !== item.hash) fail('source_changed');
|
|
101
|
+
let relative = item.target;
|
|
102
|
+
if (item.status === 'conflict' && decision === 'copy') {
|
|
103
|
+
relative = item.kind === 'skill' ? path.join(path.dirname(path.dirname(item.target)), `${path.basename(path.dirname(item.target))}-${item.hash.slice(0, 8)}`, 'SKILL.md')
|
|
104
|
+
: item.target.replace(/\.md$/, `-${item.hash.slice(0, 8)}.md`);
|
|
105
|
+
}
|
|
106
|
+
const destination = await targetPath(item.directory, relative);
|
|
107
|
+
const before = await readOptional(destination);
|
|
108
|
+
const expected = relative === item.target ? item.beforeHash : null;
|
|
109
|
+
if (before !== null && digest(before) === item.hash) return { status: 'skipped' };
|
|
110
|
+
if ((before === null ? null : digest(before)) !== expected) fail('target_changed');
|
|
111
|
+
const snapshot = item.activated ? await configSnapshot(item.directory) : null;
|
|
112
|
+
if (snapshot && snapshot.hash !== configHash) fail('target_changed');
|
|
113
|
+
await fs.mkdir(path.dirname(destination), { recursive: true, mode: 0o700 });
|
|
114
|
+
// Recheck ancestors after mkdir and before creating the exclusive new file.
|
|
115
|
+
await targetPath(item.directory, relative);
|
|
116
|
+
let backup;
|
|
117
|
+
if (before !== null) {
|
|
118
|
+
backup = `${destination}.yuri-${digest(before).slice(0, 12)}.backup`;
|
|
119
|
+
try { await fs.writeFile(backup, before, { flag: 'wx', mode: 0o600 }); }
|
|
120
|
+
catch (error) { if (error.code !== 'EEXIST') throw error; }
|
|
121
|
+
}
|
|
122
|
+
const temporary = `${destination}.${randomUUID()}.tmp`;
|
|
123
|
+
try {
|
|
124
|
+
await fs.writeFile(temporary, item.content, { flag: 'wx', mode: 0o600 });
|
|
125
|
+
// Do not overwrite a file changed since preview or while preparing the write.
|
|
126
|
+
const current = await readOptional(destination);
|
|
127
|
+
if ((current === null ? null : digest(current)) !== expected) fail('target_changed');
|
|
128
|
+
if (before === null) { await fs.link(temporary, destination); await fs.unlink(temporary); }
|
|
129
|
+
else await fs.rename(temporary, destination);
|
|
130
|
+
if (snapshot) {
|
|
131
|
+
const fresh = await configSnapshot(item.directory);
|
|
132
|
+
if (fresh.hash !== configHash) fail('target_changed');
|
|
133
|
+
const instruction = relative.split(path.sep).join('/');
|
|
134
|
+
writeConfig({ ...fresh.config, instructions: [...new Set([...fresh.instructions, instruction])] }, fresh.file);
|
|
135
|
+
}
|
|
136
|
+
} catch (error) {
|
|
137
|
+
// Compensate only our own unchanged content. Never erase a concurrent edit.
|
|
138
|
+
const current = await readOptional(destination);
|
|
139
|
+
if (current !== null && digest(current) === item.hash) {
|
|
140
|
+
if (before === null) await fs.unlink(destination);
|
|
141
|
+
else await fs.writeFile(destination, before, { mode: 0o600 });
|
|
142
|
+
}
|
|
143
|
+
throw error;
|
|
144
|
+
} finally { await fs.rm(temporary, { force: true }); }
|
|
145
|
+
return { status: 'imported', configHash: item.activated ? await settingsConfigFingerprint(item.directory) : configHash, backup: Boolean(backup) };
|
|
146
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { afterEach, beforeEach, expect, test, vi } from 'vitest';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { parse } from 'jsonc-parser';
|
|
6
|
+
import { applySettings, planSettings, settingsConfigFingerprint } from './settings.js';
|
|
7
|
+
|
|
8
|
+
let temporary; let source; let target;
|
|
9
|
+
const save = async (base, relative, content) => { const file = path.join(base, relative); await fs.mkdir(path.dirname(file), { recursive: true }); await fs.writeFile(file, content); return file; };
|
|
10
|
+
beforeEach(async () => {
|
|
11
|
+
temporary = await fs.mkdtemp(path.join(os.tmpdir(), 'yuri-import-settings-'));
|
|
12
|
+
temporary = await fs.realpath(temporary);
|
|
13
|
+
source = path.join(temporary, 'source'); target = path.join(temporary, 'target');
|
|
14
|
+
await fs.mkdir(source); await fs.mkdir(target);
|
|
15
|
+
vi.stubEnv('OPENCODE_CONFIG_DIR', path.join(temporary, 'config')); vi.stubEnv('OPENCODE_CONFIG', '');
|
|
16
|
+
});
|
|
17
|
+
afterEach(async () => { vi.unstubAllEnvs(); await fs.rm(temporary, { recursive: true, force: true }); });
|
|
18
|
+
|
|
19
|
+
test('merges text rules through the existing JSONC owner, preserves inherited instructions/comments and leaves sources untouched', async () => {
|
|
20
|
+
const original = '# Project rules\nPreserve types.\n';
|
|
21
|
+
await save(source, 'CLAUDE.md', original);
|
|
22
|
+
await save(target, 'opencode.json', '{"instructions":["base.md"]}');
|
|
23
|
+
await save(target, '.opencode/opencode.jsonc', '{\n // keep this comment\n "model": "example/model",\n}');
|
|
24
|
+
const [item] = await planSettings('claude', source, target);
|
|
25
|
+
const result = await applySettings(item, 'keep', await settingsConfigFingerprint(target));
|
|
26
|
+
expect(result.status).toBe('imported');
|
|
27
|
+
const text = await fs.readFile(path.join(target, '.opencode/opencode.jsonc'), 'utf8');
|
|
28
|
+
expect(text).toContain('// keep this comment');
|
|
29
|
+
expect(parse(text)).toMatchObject({ model: 'example/model', instructions: ['base.md', item.target.split(path.sep).join('/')] });
|
|
30
|
+
expect(await fs.readFile(path.join(source, 'CLAUDE.md'), 'utf8')).toBe(original);
|
|
31
|
+
expect(await fs.readFile(path.join(target, item.target), 'utf8')).toBe(original);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('native in-place root instructions are skipped and conditional Cursor rules are archived without global activation', async () => {
|
|
35
|
+
await save(source, 'AGENTS.md', 'already native');
|
|
36
|
+
expect((await planSettings('codex', source, source))[0].status).toBe('unchanged');
|
|
37
|
+
await save(source, '.cursor/rules/test.mdc', '---\nglobs: "src/**/*.ts"\n---\nUse strict types.');
|
|
38
|
+
const [item] = await planSettings('cursor', source, target);
|
|
39
|
+
expect(item.scoped).toBe(true); expect(item.activated).toBe(false);
|
|
40
|
+
expect((await applySettings(item, 'keep', await settingsConfigFingerprint(target))).status).toBe('imported');
|
|
41
|
+
await expect(fs.access(path.join(target, '.opencode/opencode.jsonc'))).rejects.toBeDefined();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('does not copy credentials, hooks, MCP, plugins or supporting executable skill files', async () => {
|
|
45
|
+
await save(source, '.claude/settings.json', '{"hooks":{"PreToolUse":"do not execute"}}');
|
|
46
|
+
await save(source, '.claude/skills/test/SKILL.md', '---\nname: test\ndescription: Fixture\n---\nFixture text.');
|
|
47
|
+
await save(source, '.claude/skills/test/run.sh', 'echo never run');
|
|
48
|
+
await save(source, '.claude/auth.json', 'fixture-not-a-real-secret');
|
|
49
|
+
const items = await planSettings('claude', source, target);
|
|
50
|
+
expect(items).toHaveLength(1); expect(items[0].kind).toBe('skill');
|
|
51
|
+
await applySettings(items[0], 'keep', await settingsConfigFingerprint(target));
|
|
52
|
+
expect(await fs.readdir(path.dirname(path.join(target, items[0].target)))).toEqual(['SKILL.md']);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('conflicts default to keep, explicit replace makes a backup and keep-both writes a separate file', async () => {
|
|
56
|
+
await save(source, 'CLAUDE.md', 'new'); let [item] = await planSettings('claude', source, target);
|
|
57
|
+
await save(target, item.target, 'old'); [item] = await planSettings('claude', source, target);
|
|
58
|
+
expect(item.status).toBe('conflict');
|
|
59
|
+
const fingerprint = await settingsConfigFingerprint(target);
|
|
60
|
+
expect((await applySettings(item, 'keep', fingerprint)).status).toBe('skipped');
|
|
61
|
+
expect(await fs.readFile(path.join(target, item.target), 'utf8')).toBe('old');
|
|
62
|
+
expect((await applySettings(item, 'copy', fingerprint)).status).toBe('imported');
|
|
63
|
+
expect(await fs.readFile(path.join(target, item.target), 'utf8')).toBe('old');
|
|
64
|
+
[item] = await planSettings('claude', source, target);
|
|
65
|
+
expect((await applySettings(item, 'replace', await settingsConfigFingerprint(target))).status).toBe('imported');
|
|
66
|
+
expect((await fs.readdir(path.dirname(path.join(target, item.target)))).some((name) => name.endsWith('.backup'))).toBe(true);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('source changes, target changes and malformed target config cannot silently overwrite files', async () => {
|
|
70
|
+
await save(source, 'CLAUDE.md', 'rules'); let [item] = await planSettings('claude', source, target);
|
|
71
|
+
const fingerprint = await settingsConfigFingerprint(target);
|
|
72
|
+
await save(source, 'CLAUDE.md', 'changed');
|
|
73
|
+
await expect(applySettings(item, 'keep', fingerprint)).rejects.toThrow('source_changed');
|
|
74
|
+
[item] = await planSettings('claude', source, target);
|
|
75
|
+
await save(target, 'opencode.json', '{"model":"user-edit"}');
|
|
76
|
+
await expect(applySettings(item, 'keep', fingerprint)).rejects.toThrow('target_changed');
|
|
77
|
+
await save(target, 'opencode.json', '{broken');
|
|
78
|
+
await expect(settingsConfigFingerprint(target)).rejects.toThrow('invalid_target_config');
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('symlink source settings and symlink destinations fail closed', async () => {
|
|
82
|
+
await save(temporary, 'outside.md', 'outside');
|
|
83
|
+
await fs.symlink(path.join(temporary, 'outside.md'), path.join(source, 'CLAUDE.md'));
|
|
84
|
+
await expect(planSettings('claude', source, target)).rejects.toThrow('unsafe_settings_path');
|
|
85
|
+
await fs.unlink(path.join(source, 'CLAUDE.md')); await save(source, 'CLAUDE.md', 'rules');
|
|
86
|
+
await fs.symlink(source, path.join(target, '.opencode'));
|
|
87
|
+
await expect(planSettings('claude', source, target)).rejects.toThrow('unsafe_settings_path');
|
|
88
|
+
});
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import { createReadStream } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import { createInterface } from 'node:readline';
|
|
6
|
+
import * as zlib from 'node:zlib';
|
|
7
|
+
import { setImmediate } from 'node:timers/promises';
|
|
8
|
+
import { digest, fail, parseTranscript } from './events.js';
|
|
9
|
+
import { readCursorCli, readCursorEditor, readCursorEditorInventory } from './sqlite.js';
|
|
10
|
+
|
|
11
|
+
const MAX_BYTES = 64 * 1024 * 1024;
|
|
12
|
+
const MAX_FILES = 20000;
|
|
13
|
+
const sourceKinds = ['claude', 'codex', 'cursor', 'dsh'];
|
|
14
|
+
|
|
15
|
+
const namespaceFor = (source, file) => {
|
|
16
|
+
const parts = path.resolve(file).split(path.sep);
|
|
17
|
+
const marker = source === 'codex' ? ['sessions', 'archived_sessions'] : source === 'dsh' ? ['sessions'] : source === 'cursor' ? ['projects', 'chats', 'globalStorage'] : ['projects'];
|
|
18
|
+
const index = parts.findLastIndex((part) => marker.includes(part));
|
|
19
|
+
return digest(index > 0 ? parts.slice(0, index).join(path.sep) || path.sep : path.dirname(file));
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export function defaultSources(home = os.homedir(), env = process.env, platform = process.platform) {
|
|
23
|
+
const cursorUser = platform === 'darwin' ? path.join(home, 'Library/Application Support/Cursor/User')
|
|
24
|
+
: platform === 'win32' ? path.join(env.APPDATA || path.join(home, 'AppData/Roaming'), 'Cursor/User')
|
|
25
|
+
: path.join(env.XDG_CONFIG_HOME || path.join(home, '.config'), 'Cursor/User');
|
|
26
|
+
const codex = env.CODEX_HOME || path.join(home, '.codex');
|
|
27
|
+
return [
|
|
28
|
+
{ source: 'claude', path: path.join(env.CLAUDE_CONFIG_DIR || path.join(home, '.claude'), 'projects') },
|
|
29
|
+
{ source: 'codex', path: path.join(codex, 'sessions') }, { source: 'codex', path: path.join(codex, 'archived_sessions') },
|
|
30
|
+
{ source: 'cursor', path: path.join(env.CURSOR_DATA_DIR || path.join(home, '.cursor'), 'projects') },
|
|
31
|
+
{ source: 'cursor', path: path.join(env.CURSOR_CONFIG_DIR || path.join(home, '.cursor'), 'chats') },
|
|
32
|
+
{ source: 'cursor', path: path.join(cursorUser, 'globalStorage/state.vscdb') },
|
|
33
|
+
{ source: 'dsh', path: path.join(env.DSH_HOME || path.join(home, '.dsh'), 'sessions') },
|
|
34
|
+
];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function* walk(root, budget, depth = 0) {
|
|
38
|
+
if (depth > 12) fail('source_too_large');
|
|
39
|
+
let stat;
|
|
40
|
+
try { stat = await fs.lstat(root); } catch (error) { if (error.code === 'ENOENT') return; throw error; }
|
|
41
|
+
if (stat.isSymbolicLink()) return;
|
|
42
|
+
if (++budget.count > MAX_FILES) fail('too_many_files');
|
|
43
|
+
if (stat.isFile()) { yield root; return; }
|
|
44
|
+
if (!stat.isDirectory()) return;
|
|
45
|
+
const entries = await fs.readdir(root, { withFileTypes: true });
|
|
46
|
+
// DSH keeps old physical generations. Read only the highest version, with
|
|
47
|
+
// mtime as tie-breaker for compressed/plain variants of that generation.
|
|
48
|
+
const generations = entries.filter((entry) => entry.isFile() && /^session(?:\.v\d+)?\.jsonl(?:\.zstd)?$/.test(entry.name));
|
|
49
|
+
let latest;
|
|
50
|
+
for (const entry of generations) {
|
|
51
|
+
const version = Number(entry.name.match(/\.v(\d+)/)?.[1] ?? 0);
|
|
52
|
+
const modified = (await fs.stat(path.join(root, entry.name))).mtimeMs;
|
|
53
|
+
if (!latest || version > latest.version || version === latest.version && modified > latest.modified) latest = { name: entry.name, version, modified };
|
|
54
|
+
}
|
|
55
|
+
for (const entry of entries) {
|
|
56
|
+
if (['node_modules', '.git'].includes(entry.name) || entry.isSymbolicLink()) continue;
|
|
57
|
+
if (generations.some((candidate) => candidate.name === entry.name) && latest?.name !== entry.name) continue;
|
|
58
|
+
yield* walk(path.join(root, entry.name), budget, depth + 1);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export async function readJsonLines(file) {
|
|
63
|
+
const stat = await fs.lstat(file);
|
|
64
|
+
if (!stat.isFile() || stat.isSymbolicLink()) fail('invalid_source');
|
|
65
|
+
if (stat.size > MAX_BYTES) fail('source_too_large');
|
|
66
|
+
const compressed = /\.(?:zstd|zst)$/.test(file);
|
|
67
|
+
if (compressed && typeof zlib.createZstdDecompress !== 'function') fail('compression_unsupported');
|
|
68
|
+
const input = createReadStream(file);
|
|
69
|
+
const stream = compressed ? input.pipe(zlib.createZstdDecompress()) : input;
|
|
70
|
+
const lines = createInterface({ input: stream, crlfDelay: Infinity });
|
|
71
|
+
const entries = []; let bytes = 0;
|
|
72
|
+
// Forward source errors into the decompression stream (pipe does not do so).
|
|
73
|
+
const onError = (error) => stream.destroy(error);
|
|
74
|
+
if (stream !== input) input.on('error', onError);
|
|
75
|
+
try {
|
|
76
|
+
for await (const line of lines) {
|
|
77
|
+
bytes += Buffer.byteLength(line);
|
|
78
|
+
if (bytes > MAX_BYTES || line.length > 8 * 1024 * 1024 || entries.length >= 100000) fail('source_too_large');
|
|
79
|
+
if (!line.trim()) continue;
|
|
80
|
+
try { const entry = JSON.parse(line); if (!entry || typeof entry !== 'object' || Array.isArray(entry)) fail('invalid_source'); entries.push(entry); }
|
|
81
|
+
catch { fail('invalid_source'); }
|
|
82
|
+
if (entries.length % 128 === 0) await setImmediate();
|
|
83
|
+
}
|
|
84
|
+
} finally { lines.close(); stream.destroy(); input.destroy(); input.off('error', onError); }
|
|
85
|
+
return { entries, modified: stat.mtimeMs };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export async function readSource(descriptor) {
|
|
89
|
+
let session;
|
|
90
|
+
if (descriptor.format === 'cursor-editor') {
|
|
91
|
+
const sessions = await readCursorEditor(descriptor.path, descriptor.sessionID);
|
|
92
|
+
session = sessions.find((item) => item.id === descriptor.sessionID);
|
|
93
|
+
if (!session) fail('source_missing');
|
|
94
|
+
} else if (descriptor.format === 'cursor-cli') session = await readCursorCli(descriptor.path);
|
|
95
|
+
else {
|
|
96
|
+
const { entries } = await readJsonLines(descriptor.path);
|
|
97
|
+
const fallback = path.basename(descriptor.path).replace(/\.jsonl.*$/, '');
|
|
98
|
+
const child = descriptor.source === 'claude' && path.basename(path.dirname(descriptor.path)) === 'subagents';
|
|
99
|
+
session = parseTranscript(descriptor.source, child ? entries.map((entry) => ({ ...entry, isSidechain: false })) : entries, fallback);
|
|
100
|
+
if (child) { session.id = fallback; session.parentID = path.basename(path.dirname(path.dirname(descriptor.path))); }
|
|
101
|
+
}
|
|
102
|
+
return { ...session, source: descriptor.source, namespace: descriptor.namespace,
|
|
103
|
+
hash: digest({ version: 1, ...session }), descriptor };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export async function discoverSources({ sources = sourceKinds, roots, limitPerSource, includeCounts = false } = {}, defaults = defaultSources()) {
|
|
107
|
+
if (!Array.isArray(sources) || !sources.length || sources.some((source) => !sourceKinds.includes(source))) fail('invalid_source');
|
|
108
|
+
if (roots !== undefined && (!Array.isArray(roots) || roots.length > 16 || roots.some((root) => !sourceKinds.includes(root.source) || typeof root.path !== 'string' || !path.isAbsolute(root.path)))) fail('invalid_source');
|
|
109
|
+
if (limitPerSource !== undefined && (!Number.isInteger(limitPerSource) || limitPerSource < 1 || limitPerSource > 10)) fail('invalid_preview_limit');
|
|
110
|
+
if (typeof includeCounts !== 'boolean') fail('invalid_source');
|
|
111
|
+
const locations = roots?.length ? roots.filter((root) => sources.includes(root.source)) : defaults.filter((root) => sources.includes(root.source));
|
|
112
|
+
const sessions = []; const errors = []; const descriptors = new Map(); const positions = new Map(); const budget = { count: 0 };
|
|
113
|
+
const sampleCounts = Object.fromEntries(sourceKinds.map((source) => [source, 0]));
|
|
114
|
+
const candidateCounts = Object.fromEntries(sources.map((source) => [source, 0]));
|
|
115
|
+
const unknownCounts = new Set(); const countedFiles = new Set();
|
|
116
|
+
const perSourceLimit = limitPerSource ?? Infinity;
|
|
117
|
+
for (const root of locations) {
|
|
118
|
+
if (!includeCounts && sampleCounts[root.source] >= perSourceLimit) continue;
|
|
119
|
+
try {
|
|
120
|
+
for await (const file of walk(root.path, budget)) {
|
|
121
|
+
let format = 'jsonl';
|
|
122
|
+
if (root.source === 'cursor') {
|
|
123
|
+
if (path.basename(file) === 'state.vscdb') format = 'cursor-editor';
|
|
124
|
+
else if (path.basename(file) === 'store.db') format = 'cursor-cli';
|
|
125
|
+
else if (!file.includes(`${path.sep}agent-transcripts${path.sep}`) || !file.endsWith('.jsonl')) continue;
|
|
126
|
+
} else if (root.source === 'dsh') {
|
|
127
|
+
if (!/^session(?:\.v\d+)?\.jsonl(?:\.zstd)?$/.test(path.basename(file))) continue;
|
|
128
|
+
} else if (!/\.jsonl(?:\.zst|\.zstd)?$/.test(file)) continue;
|
|
129
|
+
if (includeCounts) {
|
|
130
|
+
const countedKey = `${root.source}:${path.resolve(file)}`;
|
|
131
|
+
if (countedFiles.has(countedKey)) continue;
|
|
132
|
+
countedFiles.add(countedKey);
|
|
133
|
+
if (format !== 'cursor-editor') candidateCounts[root.source]++;
|
|
134
|
+
}
|
|
135
|
+
if (sampleCounts[root.source] >= perSourceLimit && (!includeCounts || format !== 'cursor-editor')) continue;
|
|
136
|
+
const namespace = namespaceFor(root.source, file);
|
|
137
|
+
const modified = (await fs.stat(file)).mtimeMs;
|
|
138
|
+
const descriptor = { source: root.source, namespace, path: file, format, modified };
|
|
139
|
+
try {
|
|
140
|
+
const remaining = limitPerSource === undefined ? undefined : Math.max(0, limitPerSource - sampleCounts[root.source]);
|
|
141
|
+
let records;
|
|
142
|
+
if (format === 'cursor-editor' && includeCounts) {
|
|
143
|
+
const inventory = await readCursorEditorInventory(file, remaining);
|
|
144
|
+
candidateCounts[root.source] += inventory.count;
|
|
145
|
+
records = inventory.sessions;
|
|
146
|
+
if (inventory.error) errors.push({ source: root.source, file: path.basename(file), code: inventory.error.code ?? 'source_unreadable' });
|
|
147
|
+
} else records = format === 'cursor-editor' ? await readCursorEditor(file, undefined, remaining) : [await readSource(descriptor)];
|
|
148
|
+
for (const session of records) {
|
|
149
|
+
if (sampleCounts[root.source] >= perSourceLimit) break;
|
|
150
|
+
const key = digest(`${root.source}:${namespace}:${session.id}`);
|
|
151
|
+
const previous = descriptors.get(key);
|
|
152
|
+
if (previous && previous.modified >= modified) continue;
|
|
153
|
+
const item = { ...descriptor, sessionID: session.id, revision: {
|
|
154
|
+
source: root.source, namespace, id: session.id, parentID: session.parentID,
|
|
155
|
+
hash: session.hash ?? digest({ version: 1, ...session }), directory: session.directory,
|
|
156
|
+
} };
|
|
157
|
+
descriptors.set(key, item);
|
|
158
|
+
const excerpt = session.events.find((event) => event.kind === 'assistant-text' && event.text?.trim())
|
|
159
|
+
?? session.events.find((event) => event.kind === 'user' && event.text?.trim());
|
|
160
|
+
const summary = { key, source: root.source, id: session.id, title: session.title, directory: session.directory,
|
|
161
|
+
parentID: session.parentID, messages: session.events.length, warnings: session.warnings,
|
|
162
|
+
excerpt: excerpt?.text?.slice(0, 512).replace(/\s+/g, ' ').trim().slice(0, 180) ?? '',
|
|
163
|
+
updated: session.events.at(-1)?.time ?? 0 };
|
|
164
|
+
if (positions.has(key)) sessions[positions.get(key)] = summary;
|
|
165
|
+
else { if (sessions.length >= 5000) fail('too_many_sessions'); positions.set(key, sessions.length); sessions.push(summary); sampleCounts[root.source]++; }
|
|
166
|
+
}
|
|
167
|
+
} catch (error) {
|
|
168
|
+
if (includeCounts && format === 'cursor-editor') unknownCounts.add(root.source);
|
|
169
|
+
errors.push({ source: root.source, file: path.basename(file), code: error.code ?? 'source_unreadable' });
|
|
170
|
+
}
|
|
171
|
+
await setImmediate();
|
|
172
|
+
if (!includeCounts && sampleCounts[root.source] >= perSourceLimit) break;
|
|
173
|
+
}
|
|
174
|
+
} catch (error) {
|
|
175
|
+
if (includeCounts) unknownCounts.add(root.source);
|
|
176
|
+
errors.push({ source: root.source, code: error.code ?? 'source_unreadable' });
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
sessions.sort((a, b) => a.updated - b.updated || a.key.localeCompare(b.key));
|
|
180
|
+
const counts = includeCounts ? Object.fromEntries(sources.map((source) => [source, {
|
|
181
|
+
value: unknownCounts.has(source) ? null : candidateCounts[source], approximate: candidateCounts[source] > 0,
|
|
182
|
+
}])) : undefined;
|
|
183
|
+
return { sessions, errors, descriptors, counts };
|
|
184
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { afterEach, beforeEach, expect, test } from 'vitest';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { zstdCompressSync } from 'node:zlib';
|
|
6
|
+
import Database from 'better-sqlite3';
|
|
7
|
+
import { defaultSources, discoverSources, readSource, readJsonLines } from './sources.js';
|
|
8
|
+
import { readCursorEditor, readCursorEditorInventory, readCursorCli } from './sqlite.js';
|
|
9
|
+
|
|
10
|
+
let temporary;
|
|
11
|
+
const save = async (file, data) => { const name = path.join(temporary, file); await fs.mkdir(path.dirname(name), { recursive: true }); await fs.writeFile(name, data); return name; };
|
|
12
|
+
const lines = (items) => items.map((item) => JSON.stringify(item)).join('\n') + '\n';
|
|
13
|
+
beforeEach(async () => { temporary = await fs.mkdtemp(path.join(os.tmpdir(), 'yuri-source-test-')); });
|
|
14
|
+
afterEach(async () => { await fs.rm(temporary, { force: true, recursive: true }); });
|
|
15
|
+
|
|
16
|
+
test('discovers all four source kinds, isolates malformed files and picks the newest DSH generation', async () => {
|
|
17
|
+
const claude = await save('claude/projects/project/main.jsonl', lines([{ type: 'user', uuid: 'u', timestamp: '2026-01-01', sessionId: 'main', cwd: temporary, message: { content: 'hello claude' } }]));
|
|
18
|
+
await save('claude/projects/project/main/subagents/agent-child.jsonl', lines([{ type: 'user', isSidechain: true, uuid: 'c', timestamp: '2026-01-01', sessionId: 'main', cwd: temporary, message: { content: 'child' } }]));
|
|
19
|
+
await save('claude/projects/project/broken.jsonl', '{not json}\n');
|
|
20
|
+
await save('codex/sessions/rollout.jsonl', lines([{ type: 'session_meta', payload: { id: 'codex', cwd: temporary } }, { type: 'event_msg', payload: { type: 'user_message', message: 'hello codex' } }]));
|
|
21
|
+
await save('cursor/projects/project/agent-transcripts/cursor.jsonl', lines([{ role: 'user', content: 'hello cursor' }]));
|
|
22
|
+
await save('dsh/sessions/project/dsh/session.jsonl', lines([{ type: 'session', id: 'dsh', version: 0 }, { type: 'user/message', data: { source: { kind: 'user' }, content: 'old' } }]));
|
|
23
|
+
const dsh = lines([{ type: 'session', id: 'dsh', version: 4, cwd: temporary }, { type: 'user/message', seq: 0, data: { source: { kind: 'user' }, content: 'hello dsh' } }]);
|
|
24
|
+
await save('dsh/sessions/project/dsh/session.v4.jsonl.zstd', zstdCompressSync(Buffer.from(dsh)));
|
|
25
|
+
const result = await discoverSources({ roots: ['claude', 'codex', 'cursor', 'dsh'].map((source) => ({ source, path: path.join(temporary, source) })) });
|
|
26
|
+
expect(result.sessions.map((session) => session.source).sort()).toEqual(['claude', 'claude', 'codex', 'cursor', 'dsh']);
|
|
27
|
+
expect(result.errors).toEqual([{ source: 'claude', file: 'broken.jsonl', code: 'invalid_source' }]);
|
|
28
|
+
expect(result.sessions.find((session) => session.id === 'agent-child')).toMatchObject({ parentID: 'main', title: 'child' });
|
|
29
|
+
expect(result.sessions.find((session) => session.source === 'dsh').title).toBe('hello dsh');
|
|
30
|
+
for (const descriptor of result.descriptors.values()) {
|
|
31
|
+
const reread = await readSource(descriptor);
|
|
32
|
+
expect(descriptor.revision.hash).toBe(reread.hash);
|
|
33
|
+
expect(descriptor.revision.namespace).toBe(reread.namespace);
|
|
34
|
+
expect(descriptor.revision.id).toBe(reread.id);
|
|
35
|
+
}
|
|
36
|
+
const rediscovered = await discoverSources({ roots: [{ source: 'claude', path: claude }] });
|
|
37
|
+
expect(rediscovered.sessions[0].key).toBe(result.sessions.find((session) => session.id === 'main').key);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('symlinks are not followed and malformed compressed sources fail explicitly', async () => {
|
|
41
|
+
await save('outside/file.jsonl', lines([{ role: 'user', content: 'do not scan' }]));
|
|
42
|
+
await fs.mkdir(path.join(temporary, 'root'));
|
|
43
|
+
await fs.symlink(path.join(temporary, 'outside'), path.join(temporary, 'root/link'));
|
|
44
|
+
const result = await discoverSources({ roots: [{ source: 'claude', path: path.join(temporary, 'root') }] });
|
|
45
|
+
expect(result.sessions).toEqual([]);
|
|
46
|
+
const broken = await save('broken.jsonl.zstd', Buffer.from('bad compression'));
|
|
47
|
+
await expect(readJsonLines(broken)).rejects.toBeDefined();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('sample discovery stops reading at five sessions per source without counting the remaining history', async () => {
|
|
51
|
+
const roots = [];
|
|
52
|
+
for (let index = 0; index < 5; index++) roots.push({ source: 'claude', path: await save(`claude/projects/sample-${index}.jsonl`, lines([
|
|
53
|
+
{ type: 'user', sessionId: `claude-${index}`, message: { content: `Claude ${index}` } },
|
|
54
|
+
])) });
|
|
55
|
+
roots.push({ source: 'claude', path: await save('claude/projects/not-read.jsonl', 'invalid JSON must not be read by sample discovery') });
|
|
56
|
+
for (let index = 0; index < 12; index++) await save(`codex/sessions/sample-${index}.jsonl`, lines([
|
|
57
|
+
{ type: 'session_meta', payload: { id: `codex-${index}` } },
|
|
58
|
+
{ type: 'event_msg', payload: { type: 'user_message', message: `Codex ${index}` } },
|
|
59
|
+
]));
|
|
60
|
+
roots.push({ source: 'codex', path: path.join(temporary, 'codex') });
|
|
61
|
+
const sample = await discoverSources({ roots, limitPerSource: 5 });
|
|
62
|
+
expect(sample.sessions.filter((session) => session.source === 'claude')).toHaveLength(5);
|
|
63
|
+
expect(sample.sessions.filter((session) => session.source === 'codex')).toHaveLength(5);
|
|
64
|
+
expect(sample.descriptors.size).toBe(10);
|
|
65
|
+
expect(sample.errors).toEqual([]);
|
|
66
|
+
const counted = await discoverSources({ roots: [...roots, roots[0]], limitPerSource: 5, includeCounts: true });
|
|
67
|
+
expect(counted.counts.claude).toEqual({ value: 6, approximate: true });
|
|
68
|
+
expect(counted.counts.codex).toEqual({ value: 12, approximate: true });
|
|
69
|
+
expect(counted.sessions).toHaveLength(10);
|
|
70
|
+
expect(counted.errors).toEqual([]);
|
|
71
|
+
const full = await discoverSources({ roots });
|
|
72
|
+
expect(full.sessions).toHaveLength(17);
|
|
73
|
+
expect(full.errors).toEqual([{ source: 'claude', file: 'not-read.jsonl', code: 'invalid_source' }]);
|
|
74
|
+
await expect(discoverSources({ roots, limitPerSource: 11 })).rejects.toThrow('invalid_preview_limit');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('Cursor sample reads only bounded composer headers and never parses the remaining records', async () => {
|
|
78
|
+
const file = path.join(temporary, 'state.vscdb'); const db = new Database(file);
|
|
79
|
+
db.exec('CREATE TABLE cursorDiskKV (key TEXT PRIMARY KEY, value BLOB); CREATE TABLE composerHeaders (composerId TEXT, value TEXT)');
|
|
80
|
+
const put = db.prepare('INSERT INTO cursorDiskKV VALUES (?, ?)');
|
|
81
|
+
const head = db.prepare('INSERT INTO composerHeaders VALUES (?, ?)');
|
|
82
|
+
for (let index = 0; index < 5; index++) {
|
|
83
|
+
head.run(`sample-${index}`, JSON.stringify({ name: `Sample ${index}` }));
|
|
84
|
+
put.run(`composerData:sample-${index}`, JSON.stringify({ conversation: [{ type: 1, text: 'preview' }] }));
|
|
85
|
+
}
|
|
86
|
+
head.run('not-read', 'invalid JSON must not be parsed'); db.close();
|
|
87
|
+
expect(await readCursorEditor(file, undefined, 5)).toHaveLength(5);
|
|
88
|
+
const inventory = await readCursorEditorInventory(file, 5);
|
|
89
|
+
expect(inventory.count).toBe(6);
|
|
90
|
+
expect(inventory.sessions).toHaveLength(5);
|
|
91
|
+
expect((await readCursorEditorInventory(file, 0)).sessions).toEqual([]);
|
|
92
|
+
const failedPreview = await readCursorEditorInventory(file, 7);
|
|
93
|
+
expect(failedPreview.count).toBe(6);
|
|
94
|
+
expect(failedPreview.error).toBeDefined();
|
|
95
|
+
expect(failedPreview.sessions).toEqual([]);
|
|
96
|
+
const catalog = await discoverSources({ sources: ['cursor'], roots: [{ source: 'cursor', path: file }], limitPerSource: 10, includeCounts: true });
|
|
97
|
+
expect(catalog.counts.cursor).toEqual({ value: 6, approximate: true });
|
|
98
|
+
expect(catalog.errors).toHaveLength(1);
|
|
99
|
+
await expect(readCursorEditor(file)).rejects.toBeDefined();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('unreadable index counts stay unknown, while an empty source remains a successful zero', async () => {
|
|
103
|
+
const file = await save('cursor/globalStorage/state.vscdb', 'not a SQLite database');
|
|
104
|
+
const result = await discoverSources({ sources: ['cursor', 'dsh'], roots: [
|
|
105
|
+
{ source: 'cursor', path: file }, { source: 'dsh', path: path.join(temporary, 'missing') },
|
|
106
|
+
], limitPerSource: 5, includeCounts: true });
|
|
107
|
+
expect(result.counts.cursor.value).toBeNull();
|
|
108
|
+
expect(result.counts.dsh).toEqual({ value: 0, approximate: false });
|
|
109
|
+
expect(result.errors.some((error) => error.source === 'cursor')).toBe(true);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test('preview subtitles use real assistant text, omit tool/thinking content, and stay bounded', async () => {
|
|
113
|
+
const file = await save('claude/projects/example.jsonl', lines([
|
|
114
|
+
{ type: 'user', sessionId: 'example', message: { content: 'The conversation title' } },
|
|
115
|
+
{ type: 'assistant', message: { content: [{ type: 'thinking', thinking: 'not a preview subtitle' }, { type: 'text', text: `Actual answer\nwith context ${'x'.repeat(500)}` }] } },
|
|
116
|
+
]));
|
|
117
|
+
const result = await discoverSources({ roots: [{ source: 'claude', path: file }], limitPerSource: 5 });
|
|
118
|
+
expect(result.sessions[0].excerpt).toMatch(/^Actual answer with context /);
|
|
119
|
+
expect(result.sessions[0].excerpt).toHaveLength(180);
|
|
120
|
+
expect(result.sessions[0].excerpt).not.toContain('not a preview subtitle');
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test('Cursor editor reads a WAL-consistent private snapshot without changing source files', async () => {
|
|
124
|
+
const file = path.join(temporary, 'state.vscdb'); const db = new Database(file);
|
|
125
|
+
try {
|
|
126
|
+
db.pragma('journal_mode = WAL');
|
|
127
|
+
db.exec('CREATE TABLE cursorDiskKV (key TEXT PRIMARY KEY, value BLOB); CREATE TABLE composerHeaders (composerId TEXT, value TEXT)');
|
|
128
|
+
const head = { name: 'Cursor example', createdAt: 10, workspaceIdentifier: { uri: { fsPath: temporary } } };
|
|
129
|
+
db.prepare('INSERT INTO composerHeaders VALUES (?, ?)').run('composer', JSON.stringify(head));
|
|
130
|
+
const put = db.prepare('INSERT INTO cursorDiskKV VALUES (?, ?)');
|
|
131
|
+
put.run('composerData:composer', JSON.stringify({ ...head, fullConversationHeadersOnly: [{ type: 1, bubbleId: 'u' }, { type: 2, bubbleId: 'a' }] }));
|
|
132
|
+
put.run('bubbleId:composer:u', JSON.stringify({ type: 1, text: 'question', createdAt: 10 }));
|
|
133
|
+
put.run('bubbleId:composer:a', JSON.stringify({ type: 2, text: 'answer', createdAt: 20 }));
|
|
134
|
+
const snapshot = async () => Promise.all((await fs.readdir(temporary)).sort().map(async (name) => { const stat = await fs.stat(path.join(temporary, name), { bigint: true }); return [name, stat.size, stat.mtimeNs]; }));
|
|
135
|
+
const before = await snapshot();
|
|
136
|
+
const sessions = await readCursorEditor(file);
|
|
137
|
+
const inventory = await readCursorEditorInventory(file, 1);
|
|
138
|
+
expect(inventory.count).toBe(1);
|
|
139
|
+
expect(inventory.sessions).toHaveLength(1);
|
|
140
|
+
expect(sessions[0]).toMatchObject({ id: 'composer', directory: temporary, title: 'Cursor example' });
|
|
141
|
+
expect(sessions[0].events.map((event) => event.text)).toEqual(['question', 'answer']);
|
|
142
|
+
expect(await snapshot()).toEqual(before);
|
|
143
|
+
} finally { db.close(); }
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test('Cursor CLI reads ordered protobuf blob references and tool results', async () => {
|
|
147
|
+
const file = path.join(temporary, 'store.db'); const db = new Database(file);
|
|
148
|
+
db.exec('CREATE TABLE meta (key TEXT, value TEXT); CREATE TABLE blobs (id TEXT PRIMARY KEY, data BLOB)');
|
|
149
|
+
const ids = ['aa', 'bb', 'cc']; const root = Buffer.concat(ids.map((id) => Buffer.from([0x0a, 1, Number.parseInt(id, 16)])));
|
|
150
|
+
const put = db.prepare('INSERT INTO blobs VALUES (?, ?)');
|
|
151
|
+
put.run('root', root);
|
|
152
|
+
put.run('aa', Buffer.from(JSON.stringify({ role: 'user', content: '<user_query>question</user_query>' })));
|
|
153
|
+
put.run('bb', Buffer.from(JSON.stringify({ role: 'assistant', content: [{ type: 'tool-call', toolCallId: 'call', toolName: 'shell', args: { command: 'echo never execute' } }] })));
|
|
154
|
+
put.run('cc', Buffer.from(JSON.stringify({ role: 'tool', content: [{ type: 'tool-result', toolCallId: 'call', result: 'result' }] })));
|
|
155
|
+
db.prepare('INSERT INTO meta VALUES (?, ?)').run('0', Buffer.from(JSON.stringify({ agentId: 'cli', latestRootBlobId: 'root', name: 'CLI' })).toString('hex'));
|
|
156
|
+
db.close();
|
|
157
|
+
const session = await readCursorCli(file);
|
|
158
|
+
expect(session.events[0].text).toBe('question');
|
|
159
|
+
expect(session.events[1]).toMatchObject({ kind: 'tool', incomplete: false, output: 'result' });
|
|
160
|
+
expect(await fs.readdir(temporary)).toEqual(['store.db']);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test('touching an unchanged transcript does not change its import revision', async () => {
|
|
164
|
+
const file = await save('chat.jsonl', lines([{ type: 'user', message: { content: 'hello' } }]));
|
|
165
|
+
const descriptor = { source: 'claude', path: file, namespace: 'installation', format: 'jsonl' };
|
|
166
|
+
const before = await readSource(descriptor); await fs.utimes(file, new Date(), new Date());
|
|
167
|
+
expect((await readSource(descriptor)).hash).toBe(before.hash);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test('source discovery honors platform roots and environment overrides without reading credentials', () => {
|
|
171
|
+
const result = defaultSources('/home/example', { DSH_HOME: '/data/dsh', CODEX_HOME: '/data/codex' }, 'linux');
|
|
172
|
+
expect(result.find((root) => root.source === 'dsh').path).toBe('/data/dsh/sessions');
|
|
173
|
+
expect(result.filter((root) => root.source === 'codex').map((root) => root.path)).toEqual(['/data/codex/sessions', '/data/codex/archived_sessions']);
|
|
174
|
+
expect(JSON.stringify(result)).not.toMatch(/auth\.json|config\.toml/);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test('Codex active and archived copies share one identity across custom scan entry paths', async () => {
|
|
178
|
+
const transcript = (text) => lines([{ type: 'session_meta', payload: { id: 'same-thread', cwd: temporary } },
|
|
179
|
+
{ type: 'event_msg', payload: { type: 'user_message', message: text } }]);
|
|
180
|
+
const active = await save('codex/sessions/active.jsonl', transcript('old'));
|
|
181
|
+
const archived = await save('codex/archived_sessions/archived.jsonl', transcript('new'));
|
|
182
|
+
await fs.utimes(active, new Date(1000), new Date(1000));
|
|
183
|
+
await fs.utimes(archived, new Date(2000), new Date(2000));
|
|
184
|
+
const result = await discoverSources({ sources: ['codex'], roots: [{ source: 'codex', path: path.join(temporary, 'codex') }] });
|
|
185
|
+
expect(result.sessions).toHaveLength(1);
|
|
186
|
+
expect(result.sessions[0].title).toBe('new');
|
|
187
|
+
const direct = await discoverSources({ sources: ['codex'], roots: [{ source: 'codex', path: archived }] });
|
|
188
|
+
expect(direct.sessions[0].key).toBe(result.sessions[0].key);
|
|
189
|
+
expect((await readSource(result.descriptors.get(result.sessions[0].key))).events[0].text).toBe('new');
|
|
190
|
+
});
|