@openchamber/web 1.18.3 → 1.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/assets/{CodeMirrorCommentWidgets-DbUi6I9s.js → CodeMirrorCommentWidgets-DAvopJvy.js} +1 -1
- package/dist/assets/{CommitSection-Jc2_hNPE.js → CommitSection-D9cV-qR9.js} +1 -1
- package/dist/assets/{DiagramEditor-PCrYHBko.js → DiagramEditor-B9NCadU7.js} +1 -1
- package/dist/assets/DiagramView-LKGBwf4G.js +1 -0
- package/dist/assets/{DiffView-DEGlNjMH.js → DiffView-BWNq-0ov.js} +2 -2
- package/dist/assets/FilesView-DB-Q0Gkz.js +14 -0
- package/dist/assets/{GitView-C-n17voy.js → GitView-DsWXqWx3.js} +3 -3
- package/dist/assets/{JsonTreeView-BYcgdTOQ.js → JsonTreeView-h4dHASlq.js} +1 -1
- package/dist/assets/MarkdownImageGallery-n0ikH-xm.js +1 -0
- package/dist/assets/MarkdownRendererImpl-CT2mjx-s.js +12 -0
- package/dist/assets/OnboardingScreen-CDjgzxEe.js +1 -0
- package/dist/assets/{PatchDiff-Bn7bfgUv.js → PatchDiff-jPKTJje5.js} +1 -1
- package/dist/assets/{PierreDiffViewer-D1fWvrUn.js → PierreDiffViewer-D_Gt4A_6.js} +1 -1
- package/dist/assets/PlanView-UvuxIl9-.js +4 -0
- package/dist/assets/SessionSwitcherDropdown-xKU4LRuQ.js +1 -0
- package/dist/assets/SettingsView-3B9VN6Ys.js +28 -0
- package/dist/assets/SettingsWindow-CqQNDKNd.js +1 -0
- package/dist/assets/{ToolOutputDialog-BtIJkFno.js → ToolOutputDialog-PmjlAxVE.js} +6 -6
- package/dist/assets/{ToolPartDiffPreview-i3PQo6_0.js → ToolPartDiffPreview-B34Xw9wq.js} +1 -1
- package/dist/assets/{VirtualizedFileDiff-DkcusvMa.js → VirtualizedFileDiff-B37yB9jZ.js} +3 -3
- package/dist/assets/{WalkthroughView-Cc5Hd6P5.js → WalkthroughView-Bxun3k66.js} +2 -2
- package/dist/assets/{computeVirtualFileMetrics-xPena3W2.js → computeVirtualFileMetrics-DYFv_yZS.js} +2 -2
- package/dist/assets/contextPanelEmbeddedChat-BO1wRpaN.js +1 -0
- package/dist/assets/de-DbV6CLxv.js +9 -0
- package/dist/assets/document-attachments-DFAWlG3U.js +31 -0
- package/dist/assets/es-CVDlyzZ4.js +15 -0
- package/dist/assets/fr-Czc-p0g-.js +13 -0
- package/dist/assets/index-1OhiMDBz.css +1 -0
- package/dist/assets/index-BSvmTvZl.js +1 -0
- package/dist/assets/index-DzriVon2.js +7 -0
- package/dist/assets/{isDiffPlainText-Dudf-dWx.js → isDiffPlainText-DwHKxDYc.js} +1 -1
- package/dist/assets/{isFilePlainText-DS4QV1dd.js → isFilePlainText-Dwq6ILxo.js} +1 -1
- package/dist/assets/ja-dwc9NM_S.js +15 -0
- package/dist/assets/ko-Ds1DZ8FX.js +15 -0
- package/dist/assets/{lua-BgMRiT3U.js → lua-VAEuO923.js} +1 -1
- package/dist/assets/main-BF51IOVF.js +921 -0
- package/dist/assets/{main-C0DyIwTb.js → main-D7UPARut.js} +2 -2
- package/dist/assets/markdownCore-BgeBDwD3.js +2 -0
- package/dist/assets/{miniChat-CGfrxKpJ.js → miniChat-BISXSGOo.js} +2 -2
- package/dist/assets/mobile-BnYxVuvw.js +2 -0
- package/dist/assets/{patchFileDiff-C5D1OPCa.js → patchFileDiff-BEDPDypx.js} +2 -2
- package/dist/assets/pl-CLiTO7i-.js +15 -0
- package/dist/assets/pt-BR-GIimJgpK.js +15 -0
- package/dist/assets/renderElectronMiniChatApp-BaOy72Gi.js +2 -0
- package/dist/assets/{renderMobileApp-B5HA4qbi.js → renderMobileApp-BiMbzPq9.js} +7 -7
- package/dist/assets/runtimeEndpointReset-BMBRhIbf.js +15 -0
- package/dist/assets/scheduledTasksApi-01zzfzqH.js +1 -0
- package/dist/assets/{shikiHighlight-CmanopUB.js → shikiHighlight-DXYW46vW.js} +1 -1
- package/dist/assets/uk-DLfgD1ki.js +15 -0
- package/dist/assets/{useAppFontEffects-sHmU5b8u.js → useAppFontEffects-CbzHzFop.js} +139 -142
- package/dist/assets/{useInlineCommentController-gt56WkxJ.js → useInlineCommentController-CegKdDXi.js} +2 -2
- package/dist/assets/{vendor-base-ui-D_V6hAcE.js → vendor-base-ui-BYlZTzrw.js} +1 -1
- package/dist/assets/vendor-codemirror-autocomplete-DzPz9Wgq.js +1 -0
- package/dist/assets/vendor-codemirror-commands-B7H_j0iU.js +1 -0
- package/dist/assets/{vendor-codemirror-lang-angular-CgY7_gQ1.js → vendor-codemirror-lang-angular-DEPs2M5t.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-cpp-B5Ta4enQ.js → vendor-codemirror-lang-cpp-DhkseaMo.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-css-VgNqFSH-.js → vendor-codemirror-lang-css-O0cZX78y.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-go-LxLz62pp.js → vendor-codemirror-lang-go-fMdQYcON.js} +1 -1
- package/dist/assets/vendor-codemirror-lang-html-CfgK872x.js +1 -0
- package/dist/assets/{vendor-codemirror-lang-java-Bksi1PH4.js → vendor-codemirror-lang-java-CQj4S3zl.js} +1 -1
- package/dist/assets/vendor-codemirror-lang-javascript-Cc2ZYbgX.js +13 -0
- package/dist/assets/{vendor-codemirror-lang-jinja-C7npuDD7.js → vendor-codemirror-lang-jinja-BmZZv8g-.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-json-ryCw7nab.js → vendor-codemirror-lang-json-CMBS2tG9.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-less-BxsZJD6N.js → vendor-codemirror-lang-less-C6Eo8hVS.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-liquid-De6qch10.js → vendor-codemirror-lang-liquid-DSTR47-H.js} +1 -1
- package/dist/assets/vendor-codemirror-lang-markdown-BPGOH9ye.js +1 -0
- package/dist/assets/{vendor-codemirror-lang-php-Cm1es33p.js → vendor-codemirror-lang-php-DD9udfPH.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-python-N414WYK-.js → vendor-codemirror-lang-python-HahnQaAS.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-rust-C_WuUStH.js → vendor-codemirror-lang-rust-nLTovbjv.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-sass-8UHTCAOM.js → vendor-codemirror-lang-sass-pL2Yc72v.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-sql-CV3sMA5D.js → vendor-codemirror-lang-sql-BVBnan19.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-vue-DzvC1Dhk.js → vendor-codemirror-lang-vue-BqGzJVm1.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-wast-jlr-LDdA.js → vendor-codemirror-lang-wast-B0_m5DqU.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-xml-CrO63OVp.js → vendor-codemirror-lang-xml-Dj3voadT.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-yaml-dxi6CISW.js → vendor-codemirror-lang-yaml-yu-9Z5u7.js} +1 -1
- package/dist/assets/vendor-codemirror-language-BgPj_McA.js +3 -0
- package/dist/assets/{vendor-codemirror-language-data-Dihw9MtL.js → vendor-codemirror-language-data-CiAYDJXN.js} +2 -2
- package/dist/assets/vendor-codemirror-search-BmS-GmcX.js +2 -0
- package/dist/assets/{vendor-codemirror-state-CNWM3Q8V.js → vendor-codemirror-state-C9awkmn-.js} +5 -5
- package/dist/assets/vendor-codemirror-view-CiLROUVV.js +4 -0
- package/dist/assets/vendor-replit-codemirror-vim-B2DJlAhT.js +6 -0
- package/dist/assets/vendor-replit-codemirror-vim-core-FHzKdNUS.js +34 -0
- package/dist/assets/vendor-zod-adaMkgkm.js +39 -0
- package/dist/assets/{web-components-BL-Ur2Tb.js → web-components-DCLjCO9n.js} +1 -1
- package/dist/assets/zh-CN-DkeP5awK.js +15 -0
- package/dist/assets/zh-TW-BocaFm3d.js +15 -0
- package/dist/index.html +4 -4
- package/dist/mini-chat.html +3 -3
- package/dist/mobile.html +3 -3
- package/package.json +1 -1
- package/server/index.js +116 -3
- package/server/lib/agent-memory/actions.js +243 -0
- package/server/lib/agent-memory/actions.test.js +343 -0
- package/server/lib/agent-memory/feature-flag.js +19 -0
- package/server/lib/agent-memory/feature-flag.test.js +38 -0
- package/server/lib/agent-memory/project-resolution.js +56 -0
- package/server/lib/agent-memory/project-resolution.test.js +85 -0
- package/server/lib/agent-memory/routes.http.test.js +270 -0
- package/server/lib/agent-memory/routes.js +169 -0
- package/server/lib/agent-memory/runtime.js +427 -0
- package/server/lib/agent-memory/runtime.test.js +344 -0
- package/server/lib/agent-memory/threat-patterns.js +61 -0
- package/server/lib/agent-memory/threat-patterns.test.js +61 -0
- package/server/lib/agent-tool/DOCUMENTATION.md +14 -0
- package/server/lib/agent-tool/runtime.js +63 -13
- package/server/lib/agent-tool/runtime.test.js +85 -2
- package/server/lib/context-obligatory/runtime.js +34 -3
- package/server/lib/context-obligatory/runtime.test.js +114 -0
- package/server/lib/dev-tunnel/client.js +20 -1
- package/server/lib/dev-tunnel/tunnel.test.js +9 -0
- package/server/lib/fs/DOCUMENTATION.md +3 -0
- package/server/lib/fs/routes.js +149 -32
- package/server/lib/fs/routes.test.js +225 -0
- package/server/lib/git/DOCUMENTATION.md +8 -0
- package/server/lib/git/service.js +121 -67
- package/server/lib/git/service.test.js +129 -0
- package/server/lib/github/DOCUMENTATION.md +14 -3
- package/server/lib/github/pr-status.js +188 -83
- package/server/lib/github/pr-status.test.js +182 -0
- package/server/lib/github/routes.js +36 -30
- package/server/lib/markdown-image-grants/DOCUMENTATION.md +36 -0
- package/server/lib/markdown-image-grants/routes.js +340 -0
- package/server/lib/markdown-image-grants/routes.test.js +221 -0
- package/server/lib/notifications/template-runtime.js +2 -3
- package/server/lib/openchamber-control/action-resolution.test.js +65 -0
- package/server/lib/openchamber-control/actions.js +78 -0
- package/server/lib/openchamber-control/service.js +7 -0
- package/server/lib/openchamber-sessions/routes.js +14 -0
- package/server/lib/opencode/DOCUMENTATION.md +6 -4
- package/server/lib/opencode/claude-cli-auth.js +60 -0
- package/server/lib/opencode/claude-cli-auth.test.js +54 -0
- package/server/lib/opencode/feature-routes-runtime.js +22 -0
- package/server/lib/opencode/plugins.js +12 -3
- package/server/lib/opencode/plugins.test.js +18 -0
- package/server/lib/opencode/proxy-agent-wiring.test.js +152 -0
- package/server/lib/opencode/proxy.js +116 -6
- package/server/lib/opencode/proxy.test.js +158 -1
- package/server/lib/opencode/routes.js +4 -1
- package/server/lib/opencode/server-startup-runtime.js +22 -1
- package/server/lib/opencode/server-startup-runtime.test.js +44 -0
- package/server/lib/opencode/settings-helpers.js +11 -0
- package/server/lib/opencode/settings-helpers.test.js +35 -0
- package/server/lib/opencode/settings-runtime.js +51 -0
- package/server/lib/opencode/shared.js +120 -16
- package/server/lib/opencode/shared.test.js +185 -1
- package/server/lib/project-context/DOCUMENTATION.md +158 -0
- package/server/lib/project-context/routes.http.test.js +264 -0
- package/server/lib/project-context/routes.js +237 -0
- package/server/lib/project-context/runtime.js +667 -0
- package/server/lib/project-context/runtime.test.js +498 -0
- package/server/lib/quota/DOCUMENTATION.md +16 -1
- package/server/lib/quota/providers/claude/auth.js +113 -0
- package/server/lib/quota/providers/claude/auth.test.js +117 -0
- package/server/lib/quota/providers/claude/index.js +151 -0
- package/server/lib/quota/providers/claude/index.test.js +147 -0
- package/server/lib/quota/providers/claude/transforms.js +127 -0
- package/server/lib/quota/providers/claude/transforms.test.js +82 -0
- package/server/lib/quota/providers/command-code.js +90 -0
- package/server/lib/quota/providers/command-code.test.js +72 -0
- package/server/lib/quota/providers/index.js +22 -2
- package/server/lib/quota/providers/index.test.js +10 -1
- package/server/lib/quota/utils/formatters.js +2 -1
- package/server/lib/scheduled-tasks/runtime.js +21 -2
- package/server/lib/session-knowledge/DOCUMENTATION.md +87 -0
- package/server/lib/session-knowledge/routes.js +107 -0
- package/server/lib/session-knowledge/runtime.js +331 -0
- package/server/lib/session-knowledge/runtime.test.js +275 -0
- package/server/lib/skills-catalog/DOCUMENTATION.md +22 -22
- package/server/lib/skills-catalog/curated-sources.js +1 -1
- package/server/lib/skills-catalog/curated-sources.test.js +10 -0
- package/server/lib/small-model/index.js +8 -0
- package/server/lib/small-model/index.test.js +31 -1
- package/server/lib/small-model/routes.js +3 -1
- package/dist/assets/DiagramView-CndEcZNu.js +0 -1
- package/dist/assets/FilesView-CzUgIvUm.js +0 -14
- package/dist/assets/MarkdownImageGallery-DzHwkVIH.js +0 -1
- package/dist/assets/MarkdownRendererImpl-DUcIcWgs.js +0 -12
- package/dist/assets/OnboardingScreen-BmN0lhk4.js +0 -1
- package/dist/assets/PlanView-CWM0iyFk.js +0 -4
- package/dist/assets/SessionSwitcherDropdown-Cez-9xRh.js +0 -1
- package/dist/assets/SettingsView-Bl1drwPw.js +0 -28
- package/dist/assets/SettingsWindow-DL5axjDZ.js +0 -1
- package/dist/assets/contextPanelEmbeddedChat-tQekJUAF.js +0 -1
- package/dist/assets/de-u7gSnqWs.js +0 -9
- package/dist/assets/document-attachments-CX_YaDsu.js +0 -30
- package/dist/assets/es-T9O7aAOr.js +0 -15
- package/dist/assets/fr-Oor7ewbE.js +0 -13
- package/dist/assets/index-CVNuER6G.css +0 -1
- package/dist/assets/index-Crz1YOFl.js +0 -1
- package/dist/assets/index-iFY_N5aO.js +0 -7
- package/dist/assets/ja-DHpqbVmr.js +0 -15
- package/dist/assets/ko-CwLkS3nx.js +0 -15
- package/dist/assets/main-CAxX6DAN.js +0 -915
- package/dist/assets/markdownCore-DhfzuGXu.js +0 -2
- package/dist/assets/markdownImageAssets-BTNlRyjd.js +0 -1
- package/dist/assets/mobile-DEmhwNcV.js +0 -2
- package/dist/assets/pl-CcZILN8q.js +0 -15
- package/dist/assets/pt-BR-DYHhVK6o.js +0 -15
- package/dist/assets/renderElectronMiniChatApp-Dpbswpom.js +0 -2
- package/dist/assets/runtimeEndpointReset-cbahJ3hD.js +0 -13
- package/dist/assets/scheduledTasksApi-BxgGWTL8.js +0 -1
- package/dist/assets/uk-BA2kZR38.js +0 -15
- package/dist/assets/vendor-codemirror-autocomplete-BjbBRsUA.js +0 -1
- package/dist/assets/vendor-codemirror-commands-ARAOgp1x.js +0 -1
- package/dist/assets/vendor-codemirror-lang-html-B3Qe3OpS.js +0 -1
- package/dist/assets/vendor-codemirror-lang-javascript-Bbw3D7r0.js +0 -13
- package/dist/assets/vendor-codemirror-lang-markdown-FGQydhTc.js +0 -1
- package/dist/assets/vendor-codemirror-language-DUOssd4j.js +0 -3
- package/dist/assets/vendor-codemirror-search-8kyQdB3f.js +0 -2
- package/dist/assets/vendor-codemirror-view-DSzLbZiS.js +0 -4
- package/dist/assets/vendor-replit-codemirror-vim-B0Ecqnlh.js +0 -39
- package/dist/assets/zh-CN-dlOQK6v9.js +0 -15
- package/dist/assets/zh-TW-BnqQ-WL5.js +0 -15
- package/server/lib/quota/providers/claude.js +0 -107
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
|
|
2
|
+
import fsPromises from 'node:fs/promises';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
|
|
6
|
+
import { createProjectContextRuntime, parsePlanMarkdown } from './runtime.js';
|
|
7
|
+
|
|
8
|
+
const PROJECT_ID = 'path_dGVzdA';
|
|
9
|
+
|
|
10
|
+
let projectsDirPath;
|
|
11
|
+
let runtime;
|
|
12
|
+
let idCounter;
|
|
13
|
+
|
|
14
|
+
const legacyConfigPath = () => path.join(projectsDirPath, `${PROJECT_ID}.json`);
|
|
15
|
+
const contextPath = () => path.join(projectsDirPath, PROJECT_ID, 'context.json');
|
|
16
|
+
const plansDir = () => path.join(projectsDirPath, PROJECT_ID, 'plans');
|
|
17
|
+
|
|
18
|
+
const writeJson = async (filePath, value) => {
|
|
19
|
+
await fsPromises.mkdir(path.dirname(filePath), { recursive: true });
|
|
20
|
+
await fsPromises.writeFile(filePath, JSON.stringify(value, null, 2), 'utf8');
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const readJson = async (filePath) => JSON.parse(await fsPromises.readFile(filePath, 'utf8'));
|
|
24
|
+
|
|
25
|
+
beforeEach(async () => {
|
|
26
|
+
projectsDirPath = await fsPromises.mkdtemp(path.join(os.tmpdir(), 'oc-project-context-'));
|
|
27
|
+
idCounter = 0;
|
|
28
|
+
runtime = createProjectContextRuntime({
|
|
29
|
+
fsPromises,
|
|
30
|
+
path,
|
|
31
|
+
projectsDirPath,
|
|
32
|
+
createId: () => `plan-${++idCounter}`,
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
afterEach(async () => {
|
|
37
|
+
await fsPromises.rm(projectsDirPath, { recursive: true, force: true });
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe('projectId validation', () => {
|
|
41
|
+
test('rejects traversal and empty ids', async () => {
|
|
42
|
+
await expect(runtime.readContext('../escape')).rejects.toThrow('unsupported characters');
|
|
43
|
+
await expect(runtime.readContext('a/b')).rejects.toThrow('unsupported characters');
|
|
44
|
+
await expect(runtime.readContext('')).rejects.toThrow('projectId is required');
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('readContext', () => {
|
|
49
|
+
test('missing file is authoritative empty', async () => {
|
|
50
|
+
expect(await runtime.readContext(PROJECT_ID)).toEqual({
|
|
51
|
+
version: 2,
|
|
52
|
+
notes: [],
|
|
53
|
+
todos: [],
|
|
54
|
+
plans: [],
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('malformed stored context fails instead of reading as empty', async () => {
|
|
59
|
+
await fsPromises.mkdir(path.dirname(contextPath()), { recursive: true });
|
|
60
|
+
await fsPromises.writeFile(contextPath(), '{ not json', 'utf8');
|
|
61
|
+
await expect(runtime.readContext(PROJECT_ID)).rejects.toThrow('malformed');
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('drops malformed todo and plan entries without failing the read', async () => {
|
|
65
|
+
await writeJson(contextPath(), {
|
|
66
|
+
version: 2,
|
|
67
|
+
notes: [{ id: 'n1', body: 'kept', createdAt: 1, updatedAt: 1, source: 'manual' }],
|
|
68
|
+
todos: [{ id: 'a', text: 'ok', completed: false, createdAt: 1 }, { id: '', text: 'no id' }, { text: 'no id' }],
|
|
69
|
+
plans: [
|
|
70
|
+
{ id: 'p1', file: 'a.md', title: 'A', createdAt: 2 },
|
|
71
|
+
{ id: 'p2', file: '../escape.md', title: 'Bad', createdAt: 3 },
|
|
72
|
+
{ id: 'p3', file: 'no-extension', createdAt: 4 },
|
|
73
|
+
],
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const context = await runtime.readContext(PROJECT_ID);
|
|
77
|
+
expect(context.notes.map((note) => note.body)).toEqual(['kept']);
|
|
78
|
+
expect(context.todos.map((todo) => todo.id)).toEqual(['a']);
|
|
79
|
+
expect(context.plans.map((plan) => plan.id)).toEqual(['p1']);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('clamps a note body to the maximum length', async () => {
|
|
83
|
+
await writeJson(contextPath(), {
|
|
84
|
+
version: 2,
|
|
85
|
+
notes: [{ id: 'n1', body: 'x'.repeat(5000), createdAt: 1, updatedAt: 1 }],
|
|
86
|
+
todos: [],
|
|
87
|
+
plans: [],
|
|
88
|
+
});
|
|
89
|
+
expect((await runtime.readContext(PROJECT_ID)).notes[0].body).toHaveLength(3000);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('converts a version 1 string note into a single entry', async () => {
|
|
93
|
+
await writeJson(contextPath(), { version: 1, notes: 'legacy blob', todos: [], plans: [] });
|
|
94
|
+
|
|
95
|
+
const notes = (await runtime.readContext(PROJECT_ID)).notes;
|
|
96
|
+
expect(notes).toHaveLength(1);
|
|
97
|
+
expect(notes[0].body).toBe('legacy blob');
|
|
98
|
+
expect(notes[0].source).toBe('manual');
|
|
99
|
+
expect(notes[0].pinned).toBe(false);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('an empty version 1 string converts to no notes at all', async () => {
|
|
103
|
+
await writeJson(contextPath(), { version: 1, notes: ' ', todos: [], plans: [] });
|
|
104
|
+
expect((await runtime.readContext(PROJECT_ID)).notes).toEqual([]);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test('newest note is listed first', async () => {
|
|
108
|
+
await writeJson(contextPath(), {
|
|
109
|
+
version: 2,
|
|
110
|
+
notes: [
|
|
111
|
+
{ id: 'old', body: 'old', createdAt: 1, updatedAt: 1 },
|
|
112
|
+
{ id: 'new', body: 'new', createdAt: 9, updatedAt: 9 },
|
|
113
|
+
],
|
|
114
|
+
todos: [],
|
|
115
|
+
plans: [],
|
|
116
|
+
});
|
|
117
|
+
expect((await runtime.readContext(PROJECT_ID)).notes.map((note) => note.id)).toEqual(['new', 'old']);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe('legacy migration', () => {
|
|
122
|
+
test('moves the three keys out of the client-owned config and preserves the rest', async () => {
|
|
123
|
+
await fsPromises.mkdir(plansDir(), { recursive: true });
|
|
124
|
+
await fsPromises.writeFile(path.join(plansDir(), '10-old.md'), '# Old plan\n\nbody here', 'utf8');
|
|
125
|
+
await writeJson(legacyConfigPath(), {
|
|
126
|
+
projectPath: '/tmp/test',
|
|
127
|
+
'setup-worktree': ['bun install'],
|
|
128
|
+
projectActions: [{ id: 'a', name: 'Dev', command: 'bun dev' }],
|
|
129
|
+
projectNotes: 'legacy notes',
|
|
130
|
+
projectTodos: [{ id: 't1', text: 'legacy todo', completed: true, createdAt: 5 }],
|
|
131
|
+
projectPlanFiles: [{ id: 'p1', path: path.join(plansDir(), '10-old.md'), createdAt: 10 }],
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const context = await runtime.readContext(PROJECT_ID);
|
|
135
|
+
expect(context.notes.map((note) => note.body)).toEqual(['legacy notes']);
|
|
136
|
+
expect(context.todos).toEqual([{ id: 't1', text: 'legacy todo', completed: true, createdAt: 5 }]);
|
|
137
|
+
expect(context.plans).toEqual([{ id: 'p1', file: '10-old.md', title: 'Old plan', createdAt: 10, pinned: false }]);
|
|
138
|
+
|
|
139
|
+
const remaining = await readJson(legacyConfigPath());
|
|
140
|
+
expect(remaining).toEqual({
|
|
141
|
+
projectPath: '/tmp/test',
|
|
142
|
+
'setup-worktree': ['bun install'],
|
|
143
|
+
projectActions: [{ id: 'a', name: 'Dev', command: 'bun dev' }],
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test('recovers a plan whose recorded path points outside the plans directory', async () => {
|
|
148
|
+
const strayPath = path.join(projectsDirPath, 'stray.md');
|
|
149
|
+
await fsPromises.writeFile(strayPath, '# Stray\n\nrecovered', 'utf8');
|
|
150
|
+
await writeJson(legacyConfigPath(), {
|
|
151
|
+
projectPlanFiles: [{ id: 'p1', path: strayPath, createdAt: 10 }],
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
const context = await runtime.readContext(PROJECT_ID);
|
|
155
|
+
expect(context.plans).toEqual([{ id: 'p1', file: 'stray.md', title: 'Stray', createdAt: 10, pinned: false }]);
|
|
156
|
+
expect(await fsPromises.readFile(path.join(plansDir(), 'stray.md'), 'utf8')).toContain('recovered');
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test('drops a link whose markdown no longer exists anywhere', async () => {
|
|
160
|
+
await writeJson(legacyConfigPath(), {
|
|
161
|
+
projectNotes: 'kept',
|
|
162
|
+
projectPlanFiles: [{ id: 'gone', path: path.join(plansDir(), 'missing.md'), createdAt: 10 }],
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const context = await runtime.readContext(PROJECT_ID);
|
|
166
|
+
expect(context.notes.map((note) => note.body)).toEqual(['kept']);
|
|
167
|
+
expect(context.plans).toEqual([]);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test('does not run when the legacy config holds no context keys', async () => {
|
|
171
|
+
await writeJson(legacyConfigPath(), { 'setup-worktree': ['bun install'] });
|
|
172
|
+
|
|
173
|
+
expect(await runtime.readContext(PROJECT_ID)).toEqual({ version: 2, notes: [], todos: [], plans: [] });
|
|
174
|
+
await expect(fsPromises.access(contextPath())).rejects.toThrow();
|
|
175
|
+
expect(await readJson(legacyConfigPath())).toEqual({ 'setup-worktree': ['bun install'] });
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test('is idempotent across repeated reads', async () => {
|
|
179
|
+
await writeJson(legacyConfigPath(), { projectNotes: 'once', projectTodos: [] });
|
|
180
|
+
|
|
181
|
+
const first = await runtime.readContext(PROJECT_ID);
|
|
182
|
+
const second = await runtime.readContext(PROJECT_ID);
|
|
183
|
+
expect(second).toEqual(first);
|
|
184
|
+
expect(await readJson(legacyConfigPath())).toEqual({});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test('concurrent reads converge on the same migrated content', async () => {
|
|
188
|
+
await writeJson(legacyConfigPath(), { projectNotes: 'concurrent', projectTodos: [] });
|
|
189
|
+
|
|
190
|
+
const results = await Promise.all([
|
|
191
|
+
runtime.readContext(PROJECT_ID),
|
|
192
|
+
runtime.readContext(PROJECT_ID),
|
|
193
|
+
runtime.readContext(PROJECT_ID),
|
|
194
|
+
]);
|
|
195
|
+
for (const result of results) {
|
|
196
|
+
expect(result.notes.map((note) => note.body)).toEqual(['concurrent']);
|
|
197
|
+
}
|
|
198
|
+
expect((await readJson(contextPath())).notes[0].body).toBe('concurrent');
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
describe('todos', () => {
|
|
203
|
+
test('round-trips through disk', async () => {
|
|
204
|
+
await runtime.saveTodos(PROJECT_ID, [{ id: 't1', text: 'do it', completed: false, createdAt: 1 }]);
|
|
205
|
+
|
|
206
|
+
expect((await runtime.readContext(PROJECT_ID)).todos).toEqual([
|
|
207
|
+
{ id: 't1', text: 'do it', completed: false, createdAt: 1 },
|
|
208
|
+
]);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
test('preserves notes and plans it does not write', async () => {
|
|
212
|
+
const { note } = await runtime.createNote(PROJECT_ID, { body: 'keep me' });
|
|
213
|
+
const { plan } = await runtime.createPlan(PROJECT_ID, { title: 'Keep me', body: 'x' });
|
|
214
|
+
|
|
215
|
+
await runtime.saveTodos(PROJECT_ID, [{ id: 't1', text: 'todo', createdAt: 1 }]);
|
|
216
|
+
|
|
217
|
+
const context = await runtime.readContext(PROJECT_ID);
|
|
218
|
+
expect(context.notes.map((entry) => entry.id)).toEqual([note.id]);
|
|
219
|
+
expect(context.plans.map((entry) => entry.id)).toEqual([plan.id]);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test('serializes concurrent writes without losing one', async () => {
|
|
223
|
+
await Promise.all([
|
|
224
|
+
runtime.saveTodos(PROJECT_ID, [{ id: '1', text: 'one', createdAt: 1 }]),
|
|
225
|
+
runtime.saveTodos(PROJECT_ID, [{ id: '2', text: 'two', createdAt: 2 }]),
|
|
226
|
+
]);
|
|
227
|
+
|
|
228
|
+
expect((await runtime.readContext(PROJECT_ID)).todos).toHaveLength(1);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test('clamps oversized todo text', async () => {
|
|
232
|
+
await runtime.saveTodos(PROJECT_ID, [{ id: 't1', text: 'z'.repeat(300), createdAt: 1 }]);
|
|
233
|
+
expect((await runtime.readContext(PROJECT_ID)).todos[0].text).toHaveLength(120);
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
describe('notes', () => {
|
|
238
|
+
test('create returns the stored note and prepends it', async () => {
|
|
239
|
+
const first = await runtime.createNote(PROJECT_ID, { body: 'first' });
|
|
240
|
+
const second = await runtime.createNote(PROJECT_ID, { body: 'second' });
|
|
241
|
+
|
|
242
|
+
expect(first.note.source).toBe('manual');
|
|
243
|
+
expect(first.note.pinned).toBe(false);
|
|
244
|
+
expect(second.context.notes.map((note) => note.body)).toEqual(['second', 'first']);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test('create records provenance for a note distilled from a chat selection', async () => {
|
|
248
|
+
const { note } = await runtime.createNote(PROJECT_ID, {
|
|
249
|
+
body: 'insight',
|
|
250
|
+
source: 'selection',
|
|
251
|
+
origin: { sessionId: 'ses_1', messageId: 'msg_1' },
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
expect(note.source).toBe('selection');
|
|
255
|
+
expect(note.origin).toEqual({ sessionId: 'ses_1', messageId: 'msg_1' });
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
test('create drops an origin with no session', async () => {
|
|
259
|
+
const { note } = await runtime.createNote(PROJECT_ID, { body: 'x', origin: { messageId: 'msg_1' } });
|
|
260
|
+
expect(note.origin).toBeUndefined();
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test('create rejects an empty body', async () => {
|
|
264
|
+
await expect(runtime.createNote(PROJECT_ID, { body: ' ' })).rejects.toThrow('body is required');
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
test('create clamps an oversized body', async () => {
|
|
268
|
+
const { note } = await runtime.createNote(PROJECT_ID, { body: 'y'.repeat(4000) });
|
|
269
|
+
expect(note.body).toHaveLength(3000);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
test('update patches the body and bumps updatedAt without touching createdAt', async () => {
|
|
273
|
+
const { note } = await runtime.createNote(PROJECT_ID, { body: 'before' });
|
|
274
|
+
await new Promise((resolve) => setTimeout(resolve, 2));
|
|
275
|
+
|
|
276
|
+
const result = await runtime.updateNote(PROJECT_ID, note.id, { body: 'after' });
|
|
277
|
+
expect(result.note.body).toBe('after');
|
|
278
|
+
expect(result.note.createdAt).toBe(note.createdAt);
|
|
279
|
+
expect(result.note.updatedAt).toBeGreaterThan(note.updatedAt);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
test('pinning alone leaves the body and updatedAt untouched', async () => {
|
|
283
|
+
const { note } = await runtime.createNote(PROJECT_ID, { body: 'body' });
|
|
284
|
+
|
|
285
|
+
const result = await runtime.updateNote(PROJECT_ID, note.id, { pinned: true });
|
|
286
|
+
expect(result.note.pinned).toBe(true);
|
|
287
|
+
expect(result.note.body).toBe('body');
|
|
288
|
+
expect(result.note.updatedAt).toBe(note.updatedAt);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
test('update rejects an empty patch', async () => {
|
|
292
|
+
const { note } = await runtime.createNote(PROJECT_ID, { body: 'body' });
|
|
293
|
+
await expect(runtime.updateNote(PROJECT_ID, note.id, {})).rejects.toThrow('body or pinned is required');
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
test('update rejects blanking the body', async () => {
|
|
297
|
+
const { note } = await runtime.createNote(PROJECT_ID, { body: 'body' });
|
|
298
|
+
await expect(runtime.updateNote(PROJECT_ID, note.id, { body: ' ' })).rejects.toThrow('body is required');
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
test('update returns null for an unknown note', async () => {
|
|
302
|
+
expect(await runtime.updateNote(PROJECT_ID, 'missing', { body: 'x' })).toBeNull();
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
test('delete removes only the requested note', async () => {
|
|
306
|
+
const keep = await runtime.createNote(PROJECT_ID, { body: 'keep' });
|
|
307
|
+
const drop = await runtime.createNote(PROJECT_ID, { body: 'drop' });
|
|
308
|
+
|
|
309
|
+
const result = await runtime.deleteNote(PROJECT_ID, drop.note.id);
|
|
310
|
+
expect(result.deleted).toBe(true);
|
|
311
|
+
expect(result.context.notes.map((note) => note.id)).toEqual([keep.note.id]);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
test('deleting an unknown note reports no deletion', async () => {
|
|
315
|
+
const result = await runtime.deleteNote(PROJECT_ID, 'missing');
|
|
316
|
+
expect(result.deleted).toBe(false);
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
test('refuses to grow past the note limit', async () => {
|
|
320
|
+
const notes = Array.from({ length: 200 }, (_unused, index) => ({
|
|
321
|
+
id: `n${index}`,
|
|
322
|
+
body: `note ${index}`,
|
|
323
|
+
createdAt: index,
|
|
324
|
+
updatedAt: index,
|
|
325
|
+
}));
|
|
326
|
+
await writeJson(contextPath(), { version: 2, notes, todos: [], plans: [] });
|
|
327
|
+
|
|
328
|
+
await expect(runtime.createNote(PROJECT_ID, { body: 'one too many' })).rejects.toThrow('at most 200 notes');
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
test('concurrent creates all survive', async () => {
|
|
332
|
+
await Promise.all([
|
|
333
|
+
runtime.createNote(PROJECT_ID, { body: 'a' }),
|
|
334
|
+
runtime.createNote(PROJECT_ID, { body: 'b' }),
|
|
335
|
+
runtime.createNote(PROJECT_ID, { body: 'c' }),
|
|
336
|
+
]);
|
|
337
|
+
|
|
338
|
+
const bodies = (await runtime.readContext(PROJECT_ID)).notes.map((note) => note.body);
|
|
339
|
+
expect(bodies.sort()).toEqual(['a', 'b', 'c']);
|
|
340
|
+
});
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
describe('plans', () => {
|
|
344
|
+
test('create writes markdown and returns a readable plan', async () => {
|
|
345
|
+
const { plan } = await runtime.createPlan(PROJECT_ID, { title: 'My Plan', body: 'step one' });
|
|
346
|
+
expect(plan.file).toMatch(/^\d+-my-plan\.md$/);
|
|
347
|
+
|
|
348
|
+
const read = await runtime.readPlan(PROJECT_ID, plan.id);
|
|
349
|
+
expect(read.title).toBe('My Plan');
|
|
350
|
+
expect(read.body).toBe('step one');
|
|
351
|
+
expect(read.raw).toBe('# My Plan\n\nstep one');
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
test('newest plan is listed first', async () => {
|
|
355
|
+
const first = await runtime.createPlan(PROJECT_ID, { title: 'First', body: 'a' });
|
|
356
|
+
await new Promise((resolve) => setTimeout(resolve, 2));
|
|
357
|
+
const second = await runtime.createPlan(PROJECT_ID, { title: 'Second', body: 'b' });
|
|
358
|
+
|
|
359
|
+
const context = await runtime.readContext(PROJECT_ID);
|
|
360
|
+
expect(context.plans.map((entry) => entry.id)).toEqual([second.plan.id, first.plan.id]);
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
test('reading an unknown plan returns null rather than throwing', async () => {
|
|
364
|
+
expect(await runtime.readPlan(PROJECT_ID, 'nope')).toBeNull();
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
test('reading a plan whose markdown was deleted returns null', async () => {
|
|
368
|
+
const { plan } = await runtime.createPlan(PROJECT_ID, { title: 'Doomed', body: 'x' });
|
|
369
|
+
await fsPromises.rm(path.join(plansDir(), plan.file));
|
|
370
|
+
|
|
371
|
+
expect(await runtime.readPlan(PROJECT_ID, plan.id)).toBeNull();
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
test('delete removes both the entry and the markdown', async () => {
|
|
375
|
+
const { plan } = await runtime.createPlan(PROJECT_ID, { title: 'Bye', body: 'x' });
|
|
376
|
+
|
|
377
|
+
const result = await runtime.deletePlan(PROJECT_ID, plan.id);
|
|
378
|
+
expect(result.deleted).toBe(true);
|
|
379
|
+
expect(result.context.plans).toEqual([]);
|
|
380
|
+
await expect(fsPromises.access(path.join(plansDir(), plan.file))).rejects.toThrow();
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
test('deleting an unknown plan reports no deletion and keeps state', async () => {
|
|
384
|
+
const { plan } = await runtime.createPlan(PROJECT_ID, { title: 'Stay', body: 'x' });
|
|
385
|
+
|
|
386
|
+
const result = await runtime.deletePlan(PROJECT_ID, 'missing');
|
|
387
|
+
expect(result.deleted).toBe(false);
|
|
388
|
+
expect(result.context.plans.map((entry) => entry.id)).toEqual([plan.id]);
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
test('plans created in the same millisecond do not collide on a file name', async () => {
|
|
392
|
+
const created = await Promise.all([
|
|
393
|
+
runtime.createPlan(PROJECT_ID, { title: 'Same', body: 'a' }),
|
|
394
|
+
runtime.createPlan(PROJECT_ID, { title: 'Same', body: 'b' }),
|
|
395
|
+
]);
|
|
396
|
+
|
|
397
|
+
const files = new Set(created.map((entry) => entry.plan.file));
|
|
398
|
+
expect(files.size).toBe(2);
|
|
399
|
+
const context = await runtime.readContext(PROJECT_ID);
|
|
400
|
+
expect(context.plans).toHaveLength(2);
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
test('update rewrites the markdown verbatim and re-derives the manifest title', async () => {
|
|
404
|
+
const { plan } = await runtime.createPlan(PROJECT_ID, { title: 'Old', body: 'first' });
|
|
405
|
+
|
|
406
|
+
const result = await runtime.updatePlan(PROJECT_ID, plan.id, { raw: '# New title\n\n- step\n- step two\n' });
|
|
407
|
+
expect(result.plan.title).toBe('New title');
|
|
408
|
+
expect(result.plan.file).toBe(plan.file);
|
|
409
|
+
|
|
410
|
+
expect(await fsPromises.readFile(path.join(plansDir(), plan.file), 'utf8')).toBe('# New title\n\n- step\n- step two\n');
|
|
411
|
+
expect((await runtime.readContext(PROJECT_ID)).plans[0].title).toBe('New title');
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
test('update keeps the file name when the title changes', async () => {
|
|
415
|
+
const { plan } = await runtime.createPlan(PROJECT_ID, { title: 'Original', body: 'x' });
|
|
416
|
+
|
|
417
|
+
await runtime.updatePlan(PROJECT_ID, plan.id, { raw: '# Totally different\n\nx' });
|
|
418
|
+
|
|
419
|
+
const context = await runtime.readContext(PROJECT_ID);
|
|
420
|
+
expect(context.plans[0].file).toBe(plan.file);
|
|
421
|
+
expect(context.plans).toHaveLength(1);
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
test('update returns null for an unknown plan without writing anything', async () => {
|
|
425
|
+
expect(await runtime.updatePlan(PROJECT_ID, 'missing', { raw: '# X' })).toBeNull();
|
|
426
|
+
await expect(fsPromises.readdir(plansDir())).rejects.toThrow();
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
test('update refuses to recreate markdown deleted underneath it', async () => {
|
|
430
|
+
const { plan } = await runtime.createPlan(PROJECT_ID, { title: 'Gone', body: 'x' });
|
|
431
|
+
await fsPromises.rm(path.join(plansDir(), plan.file));
|
|
432
|
+
|
|
433
|
+
expect(await runtime.updatePlan(PROJECT_ID, plan.id, { raw: '# Resurrected' })).toBeNull();
|
|
434
|
+
await expect(fsPromises.access(path.join(plansDir(), plan.file))).rejects.toThrow();
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
test('update rejects a non-string payload', async () => {
|
|
438
|
+
const { plan } = await runtime.createPlan(PROJECT_ID, { title: 'A', body: 'x' });
|
|
439
|
+
await expect(runtime.updatePlan(PROJECT_ID, plan.id, {})).rejects.toThrow('raw is required');
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
test('update does not disturb notes or todos', async () => {
|
|
443
|
+
await runtime.createNote(PROJECT_ID, { body: 'keep me' });
|
|
444
|
+
await runtime.saveTodos(PROJECT_ID, [{ id: 't1', text: 'keep', completed: false, createdAt: 1 }]);
|
|
445
|
+
const { plan } = await runtime.createPlan(PROJECT_ID, { title: 'A', body: 'x' });
|
|
446
|
+
|
|
447
|
+
await runtime.updatePlan(PROJECT_ID, plan.id, { raw: '# B\n\ny' });
|
|
448
|
+
|
|
449
|
+
const context = await runtime.readContext(PROJECT_ID);
|
|
450
|
+
expect(context.notes.map((note) => note.body)).toEqual(['keep me']);
|
|
451
|
+
expect(context.todos).toHaveLength(1);
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
test('pinning a plan leaves its title and file alone', async () => {
|
|
455
|
+
const { plan } = await runtime.createPlan(PROJECT_ID, { title: 'Pin me', body: 'x' });
|
|
456
|
+
|
|
457
|
+
const result = await runtime.setPlanPinned(PROJECT_ID, plan.id, true);
|
|
458
|
+
expect(result.plan).toEqual({ ...plan, pinned: true });
|
|
459
|
+
expect((await runtime.readContext(PROJECT_ID)).plans[0].pinned).toBe(true);
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
test('pinning an unknown plan returns null', async () => {
|
|
463
|
+
expect(await runtime.setPlanPinned(PROJECT_ID, 'missing', true)).toBeNull();
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
test('editing a plan preserves its pin state', async () => {
|
|
467
|
+
const { plan } = await runtime.createPlan(PROJECT_ID, { title: 'A', body: 'x' });
|
|
468
|
+
await runtime.setPlanPinned(PROJECT_ID, plan.id, true);
|
|
469
|
+
|
|
470
|
+
const result = await runtime.updatePlan(PROJECT_ID, plan.id, { raw: '# B\n\ny' });
|
|
471
|
+
expect(result.plan.pinned).toBe(true);
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
test('an untitled body still produces a titled markdown file', async () => {
|
|
475
|
+
const { plan } = await runtime.createPlan(PROJECT_ID, { title: '', body: '' });
|
|
476
|
+
const read = await runtime.readPlan(PROJECT_ID, plan.id);
|
|
477
|
+
expect(read.title).toBe('Plan');
|
|
478
|
+
expect(read.body).toBe('');
|
|
479
|
+
});
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
describe('parsePlanMarkdown', () => {
|
|
483
|
+
test('reads the leading heading as the title', () => {
|
|
484
|
+
expect(parsePlanMarkdown('# Title\n\nbody')).toEqual({ title: 'Title', body: 'body' });
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
test('falls back to the first non-empty line', () => {
|
|
488
|
+
expect(parsePlanMarkdown('\n\njust text\nmore')).toEqual({ title: 'just text', body: 'just text\nmore' });
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
test('normalizes CRLF input', () => {
|
|
492
|
+
expect(parsePlanMarkdown('# Title\r\n\r\nbody')).toEqual({ title: 'Title', body: 'body' });
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
test('empty input yields the default title', () => {
|
|
496
|
+
expect(parsePlanMarkdown('')).toEqual({ title: 'Plan', body: '' });
|
|
497
|
+
});
|
|
498
|
+
});
|
|
@@ -8,6 +8,7 @@ This module fetches quota and usage signals for supported providers in the web s
|
|
|
8
8
|
- `packages/web/server/lib/quota/routes.js`: Express route registration for quota endpoints.
|
|
9
9
|
- `packages/web/server/lib/quota/providers/index.js`: provider registry, configured-provider list, and provider dispatcher.
|
|
10
10
|
- `packages/web/server/lib/quota/providers/google/`: Google-specific auth, API, and transform modules.
|
|
11
|
+
- `packages/web/server/lib/quota/providers/claude/`: Claude credential discovery, usage transforms, and rate-limit handling.
|
|
11
12
|
- `packages/web/server/lib/quota/utils/`: shared auth, transform, and formatting helpers.
|
|
12
13
|
|
|
13
14
|
## Supported provider IDs (dispatcher)
|
|
@@ -16,8 +17,9 @@ These provider IDs are currently dispatchable via `fetchQuotaForProvider(provide
|
|
|
16
17
|
|
|
17
18
|
| Provider ID | Display name | Module | Auth aliases/keys |
|
|
18
19
|
| --- | --- | --- | --- |
|
|
19
|
-
| `claude` | Claude | `providers/claude
|
|
20
|
+
| `claude` | Claude | `providers/claude/` | Claude Code Keychain entry, Claude Code credentials file, OpenCode `auth.json` (`anthropic`, `claude`), `CLAUDE_CODE_OAUTH_TOKEN` |
|
|
20
21
|
| `codex` | Codex | `providers/codex.js` | `openai`, `codex`, `chatgpt` |
|
|
22
|
+
| `command-code` | Command Code | `providers/command-code.js` | `command-code` OAuth/API credential in OpenCode `auth.json`, or `COMMAND_CODE_API_KEY` |
|
|
21
23
|
| `cursor` | Cursor | `providers/cursor.js` | Environment/token files, OpenChamber-managed credentials, or explicit one-time Cursor import |
|
|
22
24
|
| `crof` | CrofAI | `providers/crof.js` | `crof` (API key under `key` or `token`) |
|
|
23
25
|
| `deepseek` | DeepSeek | `providers/deepseek.js` | `deepseek` (API key under `key` or `token`) |
|
|
@@ -51,8 +53,21 @@ Provider modules must export `providerId`, `providerName`, `aliases`, `isConfigu
|
|
|
51
53
|
|
|
52
54
|
Ollama Cloud and Cursor credentials are explicitly managed through Settings. OpenCode Go usage uses `GET https://opencode.ai/zen/go/v1/usage` with the `opencode-go` API key from OpenCode `auth.json` as a bearer token. The server validates managed credentials before atomic `0600` writes and never returns secrets through its API. OpenChamber never scans browser cookie stores or automatically reads Cursor storage; Cursor import is an explicit one-time user action and never modifies Cursor's database.
|
|
53
55
|
|
|
56
|
+
Command Code usage resolves account scope through `GET /alpha/whoami`, then reads server-backed credit balances and five-hour/weekly limits from `GET /alpha/billing/credits?orgId=...`. Personal accounts return `org: null` and use `/alpha/billing/credits` without an `orgId`; organization accounts include their organization id. Web/Electron and VS Code read the standard `command-code` OpenCode auth entry (including OAuth `access`) or `COMMAND_CODE_API_KEY`; credentials remain in the owning runtime and are never returned to shared UI.
|
|
57
|
+
|
|
54
58
|
On the first OpenCode Go usage refresh after upgrading, OpenChamber deletes the obsolete `quota/opencode-go.json` credential file without reading its cookie value.
|
|
55
59
|
|
|
60
|
+
## Claude credential and limit semantics
|
|
61
|
+
|
|
62
|
+
Claude quota reports the subscription limits Claude Code itself is bound by, read from `GET https://api.anthropic.com/api/oauth/usage`.
|
|
63
|
+
|
|
64
|
+
- **Credential sources**, in priority order: the macOS Keychain entry `Claude Code-credentials`, then `${CLAUDE_CONFIG_DIR:-~/.claude}/.credentials.json` (the Linux/WSL location), then the OpenCode `auth.json` entry, then `CLAUDE_CODE_OAUTH_TOKEN`. The Keychain wins on macOS because the credentials file there is a leftover Claude Code no longer updates.
|
|
65
|
+
- **All sources are read-only.** OpenChamber never writes to Claude Code's credential store and never refreshes the OAuth token, because Anthropic does not support two live refresh tokens for one `client_id` — refreshing here would sign the user out of Claude Code. Credentials are read fresh per request so a Claude Code refresh is picked up immediately; an expired token yields an explicit "open Claude Code to sign in again" error rather than a bare 401.
|
|
66
|
+
- **Limits come from the `limits` array**, keyed by `kind`: `session` maps to the `5h` window, `weekly_all` to `7d`, and `weekly_scoped` to a per-model `7d` window named by `scope.model.display_name`. The legacy `five_hour`/`seven_day` fields are only a fallback; `seven_day_sonnet`/`seven_day_opus` are no longer populated by Anthropic. Unrecognized limit kinds and Anthropic's rotating internal code names (`nimbus_quill`, `tangelo`, ...) are ignored rather than guessed at.
|
|
67
|
+
- **Extra usage** is reported as the `extra_usage` window from `spend`, only while `spend.enabled` is true, with a money `valueLabel`.
|
|
68
|
+
- **Rate limiting**: Anthropic returns 429 aggressively. The last successful usage payload is cached in memory and reserved during a cooldown (`Retry-After`, else five minutes, capped at one hour). The cache is keyed by a hash of the access and refresh tokens, so switching accounts drops it instead of showing the previous account's numbers.
|
|
69
|
+
- **Runtime parity**: Web/Electron and VS Code preserve the last successful Claude values during the same bounded 429 cooldown. Quota dispatchers also coalesce concurrent refreshes for the same provider in each runtime, while requests for different providers remain parallel.
|
|
70
|
+
|
|
56
71
|
## Add a new provider (quick steps)
|
|
57
72
|
1. Choose module shape based on complexity:
|
|
58
73
|
- Simple providers: create `packages/web/server/lib/quota/providers/<provider>.js`.
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Claude credential discovery.
|
|
3
|
+
*
|
|
4
|
+
* Claude Code is the primary source: on macOS it keeps its OAuth tokens in the
|
|
5
|
+
* login Keychain, elsewhere in a credentials file. OpenCode's own `auth.json`
|
|
6
|
+
* entry is the fallback for users who signed into Anthropic through OpenCode
|
|
7
|
+
* instead of Claude Code.
|
|
8
|
+
*
|
|
9
|
+
* Every source is read-only. Claude rotates a Keychain/credentials entry from
|
|
10
|
+
* under us whenever Claude Code refreshes, so credentials are read fresh per
|
|
11
|
+
* request rather than cached; a stale cached token would outlive the record it
|
|
12
|
+
* came from.
|
|
13
|
+
*
|
|
14
|
+
* @module quota/providers/claude/auth
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { execFileSync } from 'child_process';
|
|
18
|
+
import os from 'os';
|
|
19
|
+
import path from 'path';
|
|
20
|
+
|
|
21
|
+
import { readAuthFile } from '../../../opencode/auth.js';
|
|
22
|
+
import { asObject, asNonEmptyString, normalizeTimestamp, getAuthEntry, normalizeAuthEntry, readJsonFile } from '../../utils/index.js';
|
|
23
|
+
|
|
24
|
+
const KEYCHAIN_SERVICE = 'Claude Code-credentials';
|
|
25
|
+
const OPENCODE_AUTH_ALIASES = ['anthropic', 'claude'];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @typedef {object} ClaudeCredential
|
|
29
|
+
* @property {string} accessToken
|
|
30
|
+
* @property {string|null} refreshToken
|
|
31
|
+
* @property {number|null} expiresAt Epoch milliseconds, when the source reports it.
|
|
32
|
+
* @property {string|null} planLabel Subscription tier reported by Claude Code, e.g. `max`.
|
|
33
|
+
* @property {'keychain'|'credentials-file'|'opencode-auth'|'env'} source
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
const claudeConfigDirectory = () => {
|
|
37
|
+
const override = asNonEmptyString(process.env.CLAUDE_CONFIG_DIR);
|
|
38
|
+
return override ? path.resolve(override) : path.join(os.homedir(), '.claude');
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Claude Code writes one JSON blob holding both its own OAuth tokens
|
|
43
|
+
* (`claudeAiOauth`) and unrelated MCP server tokens. Only the former is read.
|
|
44
|
+
*/
|
|
45
|
+
const parseClaudeCodeBlob = (blob, source) => {
|
|
46
|
+
const oauth = asObject(asObject(blob)?.claudeAiOauth);
|
|
47
|
+
const accessToken = asNonEmptyString(oauth?.accessToken);
|
|
48
|
+
if (!accessToken) return null;
|
|
49
|
+
return {
|
|
50
|
+
accessToken,
|
|
51
|
+
refreshToken: asNonEmptyString(oauth.refreshToken),
|
|
52
|
+
expiresAt: normalizeTimestamp(oauth.expiresAt),
|
|
53
|
+
planLabel: asNonEmptyString(oauth.subscriptionType),
|
|
54
|
+
source
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const readKeychainCredential = () => {
|
|
59
|
+
if (process.platform !== 'darwin') return null;
|
|
60
|
+
let raw;
|
|
61
|
+
try {
|
|
62
|
+
raw = execFileSync('security', ['find-generic-password', '-s', KEYCHAIN_SERVICE, '-w'], {
|
|
63
|
+
encoding: 'utf8',
|
|
64
|
+
timeout: 10_000,
|
|
65
|
+
stdio: ['ignore', 'pipe', 'ignore']
|
|
66
|
+
});
|
|
67
|
+
} catch {
|
|
68
|
+
// No entry, or the user denied Keychain access. Both mean "try the next source".
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
return parseClaudeCodeBlob(JSON.parse(raw.trim()), 'keychain');
|
|
73
|
+
} catch {
|
|
74
|
+
console.warn('Claude quota: Keychain credentials are not valid JSON');
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const readCredentialsFile = () =>
|
|
80
|
+
parseClaudeCodeBlob(readJsonFile(path.join(claudeConfigDirectory(), '.credentials.json')), 'credentials-file');
|
|
81
|
+
|
|
82
|
+
const readOpenCodeCredential = () => {
|
|
83
|
+
const entry = normalizeAuthEntry(getAuthEntry(readAuthFile(), OPENCODE_AUTH_ALIASES));
|
|
84
|
+
const accessToken = asNonEmptyString(entry?.access) ?? asNonEmptyString(entry?.token);
|
|
85
|
+
if (!accessToken) return null;
|
|
86
|
+
return {
|
|
87
|
+
accessToken,
|
|
88
|
+
refreshToken: asNonEmptyString(entry.refresh),
|
|
89
|
+
expiresAt: normalizeTimestamp(entry.expires),
|
|
90
|
+
planLabel: null,
|
|
91
|
+
source: 'opencode-auth'
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const readEnvCredential = () => {
|
|
96
|
+
const accessToken = asNonEmptyString(process.env.CLAUDE_CODE_OAUTH_TOKEN);
|
|
97
|
+
if (!accessToken) return null;
|
|
98
|
+
return { accessToken, refreshToken: null, expiresAt: null, planLabel: null, source: 'env' };
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* First credential a source can produce, in priority order.
|
|
103
|
+
*
|
|
104
|
+
* The Keychain wins over the credentials file because on macOS the file is a
|
|
105
|
+
* leftover that Claude Code no longer updates.
|
|
106
|
+
*
|
|
107
|
+
* @returns {ClaudeCredential|null}
|
|
108
|
+
*/
|
|
109
|
+
export const loadClaudeCredential = () =>
|
|
110
|
+
readKeychainCredential()
|
|
111
|
+
?? readCredentialsFile()
|
|
112
|
+
?? readOpenCodeCredential()
|
|
113
|
+
?? readEnvCredential();
|