-
Notifications
You must be signed in to change notification settings - Fork 2
perf+fix: 大历史量下仪表盘不再卡顿;setup 不再吞掉你的 Claude 配置 #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1298638
perf(discovery): memoize session scans by (path, mtime, size)
moose-lab 0dc4fd1
fix(runtime): keep the legacy env-var marker when persisting session …
moose-lab b888d4b
fix(cli): never destroy a corrupt Claude settings file; tolerate stan…
moose-lab c90875e
fix(cli): avoid check-then-use races in settings backup and load
moose-lab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { copyFileSync, mkdirSync, readFileSync, writeFileSync } from "fs"; | ||
| import { dirname } from "path"; | ||
|
|
||
| export type ClaudeSettings = Record<string, unknown>; | ||
|
|
||
| function isFileMissing(err: unknown): boolean { | ||
| return (err as NodeJS.ErrnoException)?.code === "ENOENT"; | ||
| } | ||
|
|
||
| /** | ||
| * Reads ~/.claude/settings.json without destroying it (IM-18). A parse | ||
| * failure used to fall back to `{}` and the setup commands then overwrote | ||
| * the user's file with a devlog-only object — corrupt settings must abort | ||
| * instead, and writes keep a .bak of the previous content. | ||
| */ | ||
| export function loadClaudeSettings( | ||
| path: string, | ||
| ): | ||
| | { ok: true; settings: ClaudeSettings } | ||
| | { ok: false; error: string } { | ||
| let raw: string; | ||
| try { | ||
| raw = readFileSync(path, "utf-8"); | ||
| } catch (err) { | ||
| if (isFileMissing(err)) { | ||
| return { ok: true, settings: {} }; | ||
| } | ||
| return { | ||
| ok: false, | ||
| error: `Could not read ${path}: ${err instanceof Error ? err.message : String(err)}`, | ||
| }; | ||
| } | ||
| try { | ||
| const parsed = JSON.parse(raw) as unknown; | ||
| if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) { | ||
| return { ok: true, settings: parsed as ClaudeSettings }; | ||
| } | ||
| return { ok: false, error: `${path} is not a JSON object` }; | ||
| } catch (err) { | ||
| return { | ||
| ok: false, | ||
| error: `Could not parse ${path}: ${err instanceof Error ? err.message : String(err)}`, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| export function writeClaudeSettingsWithBackup( | ||
| path: string, | ||
| settings: ClaudeSettings, | ||
| ): void { | ||
| mkdirSync(dirname(path), { recursive: true }); | ||
| try { | ||
| copyFileSync(path, `${path}.bak`); | ||
| } catch (err) { | ||
| // No existing file means nothing to back up. | ||
| if (!isFileMissing(err)) throw err; | ||
| } | ||
| writeFileSync(path, JSON.stringify(settings, null, 2) + "\n", "utf-8"); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { test } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { | ||
| loadClaudeSettings, | ||
| writeClaudeSettingsWithBackup, | ||
| } from "../../cli/lib/claude-settings"; | ||
|
|
||
| /** | ||
| * Regression tests for IM-18 (REVIEW-2026-06-10): a corrupt | ||
| * ~/.claude/settings.json was parsed to `{}` and silently overwritten with | ||
| * a devlog-only object — destroying the user's hooks, model config, etc. | ||
| */ | ||
|
|
||
| function tempPath(): { dir: string; path: string; cleanup: () => void } { | ||
| const dir = mkdtempSync(join(tmpdir(), "devlog-settings-")); | ||
| return { | ||
| dir, | ||
| path: join(dir, "settings.json"), | ||
| cleanup: () => rmSync(dir, { recursive: true, force: true }), | ||
| }; | ||
| } | ||
|
|
||
| test("corrupt settings files abort instead of resolving to {} (IM-18)", () => { | ||
| const { path, cleanup } = tempPath(); | ||
| try { | ||
| writeFileSync(path, "{ definitely not json"); | ||
| const result = loadClaudeSettings(path); | ||
| assert.equal(result.ok, false); | ||
| } finally { | ||
| cleanup(); | ||
| } | ||
| }); | ||
|
|
||
| test("missing files load as empty settings; valid files round-trip", () => { | ||
| const { path, cleanup } = tempPath(); | ||
| try { | ||
| const missing = loadClaudeSettings(path); | ||
| assert.deepEqual(missing, { ok: true, settings: {} }); | ||
|
|
||
| writeFileSync(path, JSON.stringify({ model: "opus" })); | ||
| const loaded = loadClaudeSettings(path); | ||
| assert.equal(loaded.ok, true); | ||
| if (loaded.ok) assert.equal(loaded.settings.model, "opus"); | ||
| } finally { | ||
| cleanup(); | ||
| } | ||
| }); | ||
|
|
||
| test("writes keep a .bak of the previous content (IM-18)", () => { | ||
| const { path, cleanup } = tempPath(); | ||
| try { | ||
| writeFileSync(path, JSON.stringify({ precious: true })); | ||
| writeClaudeSettingsWithBackup(path, { precious: true, statusLine: {} }); | ||
|
|
||
| const backup = JSON.parse(readFileSync(`${path}.bak`, "utf-8")); | ||
| assert.deepEqual(backup, { precious: true }); | ||
| const current = JSON.parse(readFileSync(path, "utf-8")); | ||
| assert.equal(current.precious, true); | ||
| assert.ok("statusLine" in current); | ||
| } finally { | ||
| cleanup(); | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { test } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { appendFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { | ||
| clearDiscoveryCache, | ||
| discoverProjects, | ||
| getDiscoveryCacheStats, | ||
| } from "../discovery"; | ||
|
|
||
| /** | ||
| * Regression tests for IM-23 (REVIEW-2026-06-10): every dashboard poll | ||
| * re-parsed every JSONL under ~/.claude/projects serially — multi-second | ||
| * CPU+IO per request at a few thousand sessions. Scans are now memoized by | ||
| * (path, mtime, size) and only changed files are re-read. | ||
| */ | ||
|
|
||
| function line(text: string, timestamp: string): string { | ||
| return ( | ||
| JSON.stringify({ | ||
| type: "assistant", | ||
| timestamp, | ||
| message: { role: "assistant", content: [{ type: "text", text }] }, | ||
| }) + "\n" | ||
| ); | ||
| } | ||
|
|
||
| function makeProjectsDir(): { dir: string; file: string; cleanup: () => void } { | ||
| const dir = mkdtempSync(join(tmpdir(), "devlog-discovery-")); | ||
| const projectDir = join(dir, "-tmp-proj-a"); | ||
| mkdirSync(projectDir); | ||
| const file = join(projectDir, "session-1.jsonl"); | ||
| writeFileSync(file, line("hello", "2026-06-01T10:00:00.000Z")); | ||
| return { dir, file, cleanup: () => rmSync(dir, { recursive: true, force: true }) }; | ||
| } | ||
|
|
||
| test("unchanged files are served from the scan cache (IM-23)", async () => { | ||
| const { dir, cleanup } = makeProjectsDir(); | ||
| try { | ||
| clearDiscoveryCache(); | ||
|
|
||
| const first = await discoverProjects(dir); | ||
| assert.equal(first.length, 1); | ||
| assert.equal(first[0].sessions[0].meta.assistantTurns, 1); | ||
| const afterFirst = getDiscoveryCacheStats(); | ||
| assert.equal(afterFirst.misses, 1); | ||
| assert.equal(afterFirst.hits, 0); | ||
|
|
||
| const second = await discoverProjects(dir); | ||
| assert.equal(second[0].sessions[0].meta.assistantTurns, 1); | ||
| const afterSecond = getDiscoveryCacheStats(); | ||
| assert.equal(afterSecond.misses, 1); | ||
| assert.equal(afterSecond.hits, 1); | ||
| } finally { | ||
| cleanup(); | ||
| } | ||
| }); | ||
|
|
||
| test("changed files are rescanned and fresh metadata returned", async () => { | ||
| const { dir, file, cleanup } = makeProjectsDir(); | ||
| try { | ||
| clearDiscoveryCache(); | ||
| await discoverProjects(dir); | ||
|
|
||
| appendFileSync(file, line("more work", "2026-06-01T11:00:00.000Z")); | ||
|
|
||
| const after = await discoverProjects(dir); | ||
| assert.equal(after[0].sessions[0].meta.assistantTurns, 2); | ||
| const stats = getDiscoveryCacheStats(); | ||
| assert.equal(stats.misses, 2); | ||
| } finally { | ||
| cleanup(); | ||
| } | ||
| }); | ||
|
|
||
| test("cache entries for deleted files are pruned", async () => { | ||
| const { dir, file, cleanup } = makeProjectsDir(); | ||
| try { | ||
| clearDiscoveryCache(); | ||
| await discoverProjects(dir); | ||
| assert.equal(getDiscoveryCacheStats().entries, 1); | ||
|
|
||
| rmSync(file); | ||
| await discoverProjects(dir); | ||
| assert.equal(getDiscoveryCacheStats().entries, 0); | ||
| } finally { | ||
| cleanup(); | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { test } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { | ||
| getPersistedSessionAuthMode, | ||
| resolveSessionRuntimeAuthConfig, | ||
| resolveStoredSessionRuntimeAuthConfig, | ||
| } from "../session-runtime-auth"; | ||
|
|
||
| /** | ||
| * Regression test for IM-7 (REVIEW-2026-06-10): legacy agent-api-key | ||
| * sessions were persisted as "anthropic-api-key", losing the env-var | ||
| * marker — the next stored-config resolution treated them as browser BYOK | ||
| * and threw "API key is required" although preflight had passed. | ||
| */ | ||
|
|
||
| test("legacy env-var sessions survive a persist/resolve round-trip (IM-7)", () => { | ||
| const original = resolveSessionRuntimeAuthConfig({ | ||
| session_auth_mode: "agent-api-key", | ||
| agent_api_key_env_var: "MY_BACKEND_KEY", | ||
| }); | ||
| assert.equal(original.usesLegacyEnvVar, true); | ||
| assert.equal(original.agentApiKeyEnvVar, "MY_BACKEND_KEY"); | ||
|
|
||
| const persistedMode = getPersistedSessionAuthMode(original); | ||
| assert.equal(persistedMode, "agent-api-key"); | ||
|
|
||
| // Simulate the next turn: stored row resolved with no transient key. | ||
| const restored = resolveStoredSessionRuntimeAuthConfig({ | ||
| session_auth_mode: persistedMode, | ||
| agent_api_key_env_var: "MY_BACKEND_KEY", | ||
| }); | ||
| assert.equal(restored.usesLegacyEnvVar, true); | ||
| assert.equal(restored.agentApiKeyEnvVar, "MY_BACKEND_KEY"); | ||
| assert.equal(restored.anthropicApiKey, null); | ||
| }); | ||
|
|
||
| test("browser BYOK sessions persist their bare mode", () => { | ||
| const config = resolveSessionRuntimeAuthConfig({ | ||
| session_auth_mode: "anthropic-api-key", | ||
| anthropic_api_key: "sk-ant-test", | ||
| }); | ||
| assert.equal(config.usesLegacyEnvVar, false); | ||
| assert.equal(getPersistedSessionAuthMode(config), "anthropic-api-key"); | ||
|
|
||
| const localCli = resolveSessionRuntimeAuthConfig({ | ||
| session_auth_mode: "local-cli", | ||
| }); | ||
| assert.equal(getPersistedSessionAuthMode(localCli), "local-cli"); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.