Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

fix(setup): exclude observer child sessions from token usage (#6764)

Afloat07ef098249b5
diff --git a/src/services/context/ObservationCompiler.ts b/src/services/context/ObservationCompiler.ts
index c180692e1..6246a84df 100644
--- a/src/services/context/ObservationCompiler.ts
+++ b/src/services/context/ObservationCompiler.ts
@@ -285,7 +285,7 @@ export function querySummariesMulti(
}
/** Claude Code cuts a longer encoded project directory name to this length and appends a hash. */
-const CLAUDE_PROJECT_DIR_NAME_MAX_LENGTH = 200;
+export const CLAUDE_PROJECT_DIR_NAME_MAX_LENGTH = 200;
/** Claude Code's 32-bit string hash over UTF-16 code units, in base 36: the suffix of a cut name. */
function claudeProjectDirNameHash(cwd: string): string {
diff --git a/src/services/prompt-import/token-usage.ts b/src/services/prompt-import/token-usage.ts
index 74664df24..74fddb16a 100644
--- a/src/services/prompt-import/token-usage.ts
+++ b/src/services/prompt-import/token-usage.ts
@@ -35,7 +35,7 @@ import { basename, dirname, join } from 'path';
import { OBSERVER_SESSIONS_DIR } from '../../shared/paths.js';
import { logger } from '../../utils/logger.js';
import { streamRows } from '../sqlite/stream-rows.js';
-import { cwdToDashed } from '../context/ObservationCompiler.js';
+import { CLAUDE_PROJECT_DIR_NAME_MAX_LENGTH, cwdToDashed } from '../context/ObservationCompiler.js';
import { calculateTokenEconomics, combineTokenEconomics, type ObservationTokenFields } from '../context/TokenCalculator.js';
import type { TokenEconomics } from '../context/types.js';
import { PROMPT_SCAN_WINDOW_DAYS } from './index.js';
@@ -277,7 +277,8 @@ const openCodeUsage: UsageReader = {
export async function scanTokenUsage(options: TokenUsageScanOptions): Promise<{ windowStart: string; windowDays: number; agents: AgentTokenUsage[] }> {
const homeDirectory = options.homeDirectory ?? homedir();
const windowDays = options.windowDays ?? PROMPT_SCAN_WINDOW_DAYS;
- const excludedFolders = new Set((options.excludedClaudeCwds ?? [OBSERVER_SESSIONS_DIR]).map(cwdToDashed));
+ const excludedPrefixes = (options.excludedClaudeCwds ?? [OBSERVER_SESSIONS_DIR])
+ .map(cwd => cwdToDashed(cwd).slice(0, CLAUDE_PROJECT_DIR_NAME_MAX_LENGTH));
const usageReaders: Partial<Record<PromptAgentId, UsageReader>> = {
'claude-code': claudeCodeUsage(), codex: codexUsage, 'gemini-cli': geminiCliUsage, opencode: openCodeUsage,
};
@@ -288,7 +289,9 @@ export async function scanTokenUsage(options: TokenUsageScanOptions): Promise<{
// history covers the window, including files too old to read for tokens.
const usageSources = reader.id === 'claude-code' ? sources.filter(source => {
const projectFolder = basename(source.holdsPrompts ? dirname(source.path) : dirname(dirname(dirname(source.path))));
- return !excludedFolders.has(projectFolder);
+ // SDK children work in observer-sessions/<id>. Long names retain only
+ // the encoded prefix; their hash changes with each child's working directory.
+ return !excludedPrefixes.some(prefix => projectFolder === prefix || projectFolder.startsWith(`${prefix}-`));
}) : sources;
const usageReader = usageReaders[reader.id];
const usage: UsageTally = { tokens: 0, firstRecordAt: null };
diff --git a/tests/services/prompt-import/token-coverage-exclusions.test.ts b/tests/services/prompt-import/token-coverage-exclusions.test.ts
index 369456328..51b7ee582 100644
--- a/tests/services/prompt-import/token-coverage-exclusions.test.ts
+++ b/tests/services/prompt-import/token-coverage-exclusions.test.ts
@@ -72,6 +72,44 @@ describe('token coverage uses the same source exclusions as token counts', () =>
expect(included.agents[0]).toMatchObject({ tokens: 200, historyStartsBeforeWindow: true });
});
+ for (const { label, cwd, project } of [
+ { label: 'observer session', cwd: OBSERVER_CWD, project: `${OBSERVER_PROJECT}-123` },
+ { label: 'standalone observer session', cwd: OBSERVER_CWD, project: `${OBSERVER_PROJECT}--7` },
+ {
+ label: 'shortened observer session',
+ cwd: '/srv/' + 'deep/'.repeat(50) + 'observer-sessions',
+ project: '-srv-' + 'deep-'.repeat(39) + '-nfas40',
+ },
+ {
+ label: 'observer session at the directory-name length boundary',
+ cwd: '/' + 'd'.repeat(199),
+ project: '-' + 'd'.repeat(199) + '-tsfua0',
+ },
+ ]) {
+ it.each(['current.jsonl', 'session/subagents/agent.jsonl'])(`excludes ${label} children from tokens and coverage: %s`, async (file) => {
+ setup();
+ transcript(`${project}/${file}`, 'observer-current', NOW - DAY);
+ transcript(`${project}/old.jsonl`, 'observer-old', SINCE - DAY, SINCE - DAY);
+
+ const usage = await scan([cwd]);
+ expect(usage.agents[0]).toMatchObject({
+ tokens: 100, historyStartsBeforeWindow: false, firstRecordAt: new Date(NOW - DAY).toISOString(),
+ });
+ expect(summarizeTokenUsage(usage, null)).toMatchObject({ coverage: 'partial', persona: null });
+
+ const included = await scan([]);
+ expect(included.agents[0]).toMatchObject({ tokens: 200, historyStartsBeforeWindow: true });
+ });
+ }
+
+ it('keeps personal project names that merely extend the observer directory name', async () => {
+ setup();
+ transcript(`${OBSERVER_PROJECT}2/current.jsonl`, 'personal-neighbor', NOW - DAY);
+ transcript(`${OBSERVER_PROJECT}2/old.jsonl`, 'personal-neighbor-old', SINCE - DAY, SINCE - DAY);
+ const usage = await scan();
+ expect(usage.agents[0]).toMatchObject({ tokens: 200, historyStartsBeforeWindow: true });
+ });
+
it('still recognizes old personal transcripts as evidence of a complete window', async () => {
setup();
transcript('-home-test-app/old.jsonl', 'personal-old', SINCE - DAY, SINCE - DAY);