Skip to content

Commit 61ec04c

Browse files
committed
fix(historian): preserve child privacy windows
1 parent 876860e commit 61ec04c

8 files changed

Lines changed: 117 additions & 33 deletions

File tree

‎packages/plugin/src/features/magic-context/dreamer/retrospective-orphan-sweep.test.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import {
88
CURATE_CHILD_TITLE,
99
HISTORIAN_CHILD_TITLE,
1010
historianOrphanStaleMs,
11+
historianOrphanSweepTitleMatches,
1112
MAINTAIN_DOCS_CHILD_TITLE,
12-
orphanSweepTitleMatches,
13+
privacyOrphanSweepTitleMatches,
1314
REFRESH_PRIMERS_CHILD_TITLE,
1415
RETROSPECTIVE_CHILD_TITLE,
1516
retrospectiveOrphanStaleMs,
@@ -57,10 +58,14 @@ describe("retrospectiveOrphanStaleMs", () => {
5758
});
5859
});
5960

60-
describe("orphanSweepTitleMatches", () => {
61-
test("preserves historian children when keep_subagents is enabled", () => {
62-
expect(orphanSweepTitleMatches(false).exact).toContain(HISTORIAN_CHILD_TITLE);
63-
expect(orphanSweepTitleMatches(true).exact).not.toContain(HISTORIAN_CHILD_TITLE);
61+
describe("orphan sweep title groups", () => {
62+
test("keeps historian and privacy titles in separate groups", () => {
63+
const privacyTitles = privacyOrphanSweepTitleMatches();
64+
const historianTitles = historianOrphanSweepTitleMatches();
65+
66+
expect(privacyTitles.exact).toContain(RETROSPECTIVE_CHILD_TITLE);
67+
expect(privacyTitles.exact).not.toContain(HISTORIAN_CHILD_TITLE);
68+
expect(historianTitles).toEqual({ exact: [HISTORIAN_CHILD_TITLE], prefixes: [] });
6469
});
6570
});
6671

‎packages/plugin/src/features/magic-context/dreamer/retrospective-orphan-sweep.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { DreamTaskName } from "./task-registry";
77
type OpencodeClient = PluginContext["client"];
88

99
/**
10-
* Privacy backstop for dreamer children that carry raw user or project text.
10+
* Privacy backstop for internal children that carry raw user or project text.
1111
*
1212
* These child sessions normally delete themselves in `finally`, but a hard
1313
* SIGKILL/OOM BETWEEN session-create and that delete would leave their prompts
@@ -16,6 +16,8 @@ type OpencodeClient = PluginContext["client"];
1616
* CONCURRENCY: `session.delete` has no cross-process "active session" lease (OC
1717
* peer confirmed), so the ONLY safe filter is AGE — a child older than any
1818
* legitimate run cannot belong to a live run on another OpenCode process.
19+
* Callers sweep dreamer/privacy titles and historian titles separately because
20+
* their maximum legitimate runtimes are calculated from different budgets.
1921
* OpenCode sets `title` + `time_created` immediately at create (not lazily), so
2022
* the age gate is airtight. 404 on delete = already-swept = success.
2123
*/
@@ -55,8 +57,7 @@ export const PRIVACY_SENSITIVE_CHILD_TITLE_MATCHES: PrivacySensitiveChildTitleMa
5557
};
5658
const PRIMARY_AND_SUGGESTED_CALLS_PER_CANDIDATE = 2;
5759

58-
export function orphanSweepTitleMatches(keepSubagents: boolean): PrivacySensitiveChildTitleMatches {
59-
if (!keepSubagents) return PRIVACY_SENSITIVE_CHILD_TITLE_MATCHES;
60+
export function privacyOrphanSweepTitleMatches(): PrivacySensitiveChildTitleMatches {
6061
return {
6162
exact: PRIVACY_SENSITIVE_CHILD_TITLE_MATCHES.exact.filter(
6263
(title) => title !== HISTORIAN_CHILD_TITLE,
@@ -65,6 +66,10 @@ export function orphanSweepTitleMatches(keepSubagents: boolean): PrivacySensitiv
6566
};
6667
}
6768

69+
export function historianOrphanSweepTitleMatches(): PrivacySensitiveChildTitleMatches {
70+
return { exact: [HISTORIAN_CHILD_TITLE], prefixes: [] };
71+
}
72+
6873
/** Stale threshold from task timeout(s): max(60min, maxTimeout×3) — comfortably
6974
* past every swept child type so a live child is never swept. */
7075
export function retrospectiveOrphanStaleMs(

‎packages/plugin/src/hooks/magic-context/empty-task-output.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,28 @@ describe("annotateEmptyTaskOutput", () => {
2222
expect(taskOutput.output).toBe("completed");
2323
expect(toolOutput.output).toBe("");
2424
});
25+
26+
test("leaves bare empty and non-completed task results unchanged", () => {
27+
const outputs = [
28+
{ output: "" },
29+
{ output: '<task id="error" state="error"><task_result></task_result></task>' },
30+
{ output: '<task id="aborted" state="aborted"><task_result></task_result></task>' },
31+
{ output: '<task id="running" state="running"><task_result></task_result></task>' },
32+
];
33+
34+
for (const output of outputs) annotateEmptyTaskOutput("task", output);
35+
36+
expect(outputs.every(({ output }) => !output.includes(EMPTY_TASK_OUTPUT_SENTINEL))).toBe(
37+
true,
38+
);
39+
});
40+
41+
test("does not throw when a completed empty task output is frozen", () => {
42+
const output = Object.freeze({
43+
output: '<task id="frozen" state="completed"><task_result></task_result></task>',
44+
});
45+
46+
expect(() => annotateEmptyTaskOutput("task", output)).not.toThrow();
47+
expect(output.output).not.toContain(EMPTY_TASK_OUTPUT_SENTINEL);
48+
});
2549
});

‎packages/plugin/src/hooks/magic-context/empty-task-output.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ export function annotateEmptyTaskOutput(tool: string, output: unknown): void {
88
if (tool !== "task" || !isRecord(output)) return;
99
if (typeof output.output !== "string") return;
1010
if (output.output.includes(EMPTY_TASK_OUTPUT_SENTINEL)) return;
11-
if (output.output.trim().length > 0 && !EMPTY_COMPLETED_TASK_RESULT.test(output.output)) {
12-
return;
13-
}
11+
if (!EMPTY_COMPLETED_TASK_RESULT.test(output.output)) return;
1412

15-
output.output += `\n${EMPTY_TASK_OUTPUT_SENTINEL}
16-
The subagent completed without a final text response. Its provider may have emitted reasoning only; inspect the child session and retry with a low-reasoning model or variant.`;
13+
Reflect.set(
14+
output,
15+
"output",
16+
`${output.output}\n${EMPTY_TASK_OUTPUT_SENTINEL}
17+
The subagent completed without a final text response. Context-fill truncation may have omitted its final output, or its provider may have emitted reasoning only; inspect the child session and retry with a low-reasoning model or variant.`,
18+
);
1719
}

‎packages/plugin/src/index-refresh.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ describe("plugin model-limit cache warmup", () => {
1414
});
1515
});
1616

17+
describe("historian timer fallback budget", () => {
18+
test("counts a string fallback as one normalized model", () => {
19+
const source = readFileSync(join(import.meta.dir, "index.ts"), "utf8");
20+
21+
expect(source).toMatch(
22+
/resolveFallbackChain\(\s*pluginConfig\.historian\?\.fallback_models,?\s*\)\.length/,
23+
);
24+
expect(source).not.toContain("pluginConfig.historian?.fallback_models?.length");
25+
});
26+
});
27+
1728
describe("buildHiddenAgentConfig", () => {
1829
test("clamps maxSteps overrides above the hard cap", () => {
1930
const config = buildHiddenAgentConfig("prompt", ["read"], 40, { maxSteps: 100_000 });

‎packages/plugin/src/index.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import { setKeepSubagents } from "./shared/keep-subagents";
6464
import { log } from "./shared/logger";
6565
import { refreshModelLimitsFromApi } from "./shared/models-dev-cache";
6666
import { createPromptSurfaceRuntime } from "./shared/prompt-surface-runtime";
67+
import { resolveFallbackChain } from "./shared/resolve-fallbacks";
6768
import { MagicContextRpcServer } from "./shared/rpc-server";
6869
import { closeQuietly } from "./shared/sqlite-helpers";
6970
import { setStoragePrivatePermissionEnforcement } from "./shared/storage-permissions";
@@ -368,7 +369,9 @@ const server: Plugin = async (ctx) => {
368369
client: ctx.client,
369370
dreamerConfig: dreamerRunnable ? pluginConfig.dreamer : undefined,
370371
historianTimeoutMs: pluginConfig.historian_timeout_ms,
371-
historianFallbackCount: pluginConfig.historian?.fallback_models?.length ?? 0,
372+
historianFallbackCount: resolveFallbackChain(
373+
pluginConfig.historian?.fallback_models,
374+
).length,
372375
keepSubagents: pluginConfig.keep_subagents,
373376
language: pluginConfig.language,
374377
transformMode: pluginConfig.transform_mode,

‎packages/plugin/src/plugin/dream-timer.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,33 @@ describe("dream-timer message-history maintenance (static)", () => {
119119
});
120120
});
121121

122+
describe("dream-timer privacy orphan sweep ordering (static)", () => {
123+
const source = readFileSync(join(import.meta.dir, "dream-timer.ts"), "utf8");
124+
const sweep = source.slice(
125+
source.indexOf("async function sweepProject("),
126+
source.indexOf("async function runCompiledSmartNoteSweep("),
127+
);
128+
129+
test("derives privacy timeouts before task scheduling can reject", () => {
130+
const timeoutIndex = sweep.indexOf("const privacySweepTimeouts = runtimeConfigs");
131+
const schedulerIndex = sweep.indexOf("await runDueTasksForProject(");
132+
133+
expect(timeoutIndex).toBeGreaterThan(0);
134+
expect(timeoutIndex).toBeLessThan(schedulerIndex);
135+
});
136+
137+
test("sweeps historian and privacy titles with separate stale windows", () => {
138+
const orphanSweep = source.slice(
139+
source.indexOf("async function sweepOrphanedChildSessions("),
140+
source.indexOf("async function runCompiledSmartNoteSweep("),
141+
);
142+
const sweepCalls = orphanSweep.match(/sweepOrphanedRetrospectiveChildren\(\{/g) ?? [];
143+
144+
expect(sweepCalls).toHaveLength(2);
145+
expect(orphanSweep).not.toContain("staleMs: Math.max(");
146+
});
147+
});
148+
122149
describe("dream-timer git commit backlog drain (static)", () => {
123150
const source = readFileSync(join(import.meta.dir, "dream-timer.ts"), "utf8");
124151

‎packages/plugin/src/plugin/dream-timer.ts‎

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import { acquireLease, releaseLease } from "../features/magic-context/dreamer/le
66
import { openOpenCodeDb } from "../features/magic-context/dreamer/open-opencode-db";
77
import {
88
historianOrphanStaleMs,
9-
orphanSweepTitleMatches,
9+
historianOrphanSweepTitleMatches,
1010
PRIVACY_SENSITIVE_CHILD_TASKS,
11+
privacyOrphanSweepTitleMatches,
1112
retrospectiveOrphanStaleMs,
1213
sweepOrphanedRetrospectiveChildren,
1314
} from "../features/magic-context/dreamer/retrospective-orphan-sweep";
@@ -425,7 +426,15 @@ async function sweepProject(
425426
return;
426427
}
427428

428-
let privacySweepTimeouts: Array<number | undefined> = [];
429+
// Resolve the privacy age budget before any awaited scheduler work. If
430+
// scheduling rejects, the orphan sweep must still protect a legitimately
431+
// long-running child with its configured timeout rather than the 60m floor.
432+
const runtimeConfigs = buildDreamTaskRuntimeConfigs(dreamerConfig, reg.language);
433+
const privacySweepTimeouts = runtimeConfigs
434+
.filter((config) =>
435+
(PRIVACY_SENSITIVE_CHILD_TASKS as readonly string[]).includes(config.task),
436+
)
437+
.map((config) => config.timeoutMinutes);
429438
try {
430439
await runCompiledSmartNoteSweep(reg, db);
431440

@@ -434,7 +443,6 @@ async function sweepProject(
434443
// runs due tasks grouped by conflict-domain under keyed leases. The
435444
// executor runs in THIS registration's own checkout (not a sibling
436445
// worktree the shared git:<sha> identity might resolve to).
437-
const runtimeConfigs = buildDreamTaskRuntimeConfigs(dreamerConfig, reg.language);
438446
const executor = createDreamTaskExecutor({
439447
client: reg.client,
440448
sessionDirectory: reg.directory,
@@ -470,14 +478,6 @@ async function sweepProject(
470478
if (ran > 0) {
471479
log(`[dreamer] timer tick (${origin}) ${reg.projectIdentity} — ran ${ran} task(s)`);
472480
}
473-
474-
// PRIVACY backstop: remove crash-orphaned children carrying raw user or
475-
// project text only after the longest swept task's timeout has elapsed.
476-
// OpenCode-only (Pi subprocess children die with their process); skip
477-
// when no opencode.db.
478-
privacySweepTimeouts = runtimeConfigs
479-
.filter((c) => (PRIVACY_SENSITIVE_CHILD_TASKS as readonly string[]).includes(c.task))
480-
.map((c) => c.timeoutMinutes);
481481
} catch (error) {
482482
log(`[dreamer] timer-triggered task scheduling failed for ${reg.projectIdentity}:`, error);
483483
}
@@ -491,22 +491,29 @@ async function sweepOrphanedChildSessions(
491491
const opencodeDb = openOpenCodeDb();
492492
if (!opencodeDb) return;
493493

494-
const titleMatches = orphanSweepTitleMatches(reg.keepSubagents === true);
495-
496494
try {
495+
// Dreamer privacy titles use task timeout ×3. Historian children have a
496+
// separate retry/fallback budget; sharing the maximum made raw dreamer
497+
// sessions linger for hours. keep_subagents exempts historian only.
497498
await sweepOrphanedRetrospectiveChildren({
498499
opencodeDb,
499500
client: reg.client,
500501
sessionDirectory: reg.directory,
501-
staleMs: Math.max(
502-
retrospectiveOrphanStaleMs(privacySweepTimeouts),
503-
historianOrphanStaleMs(
502+
staleMs: retrospectiveOrphanStaleMs(privacySweepTimeouts),
503+
titleMatches: privacyOrphanSweepTitleMatches(),
504+
});
505+
if (reg.keepSubagents !== true) {
506+
await sweepOrphanedRetrospectiveChildren({
507+
opencodeDb,
508+
client: reg.client,
509+
sessionDirectory: reg.directory,
510+
staleMs: historianOrphanStaleMs(
504511
reg.historianTimeoutMs ?? DEFAULT_HISTORIAN_TIMEOUT_MS,
505512
reg.historianFallbackCount ?? 0,
506513
),
507-
),
508-
titleMatches,
509-
});
514+
titleMatches: historianOrphanSweepTitleMatches(),
515+
});
516+
}
510517
} catch (error) {
511518
log(`[dreamer] child-session orphan sweep failed for ${reg.projectIdentity}:`, error);
512519
} finally {

0 commit comments

Comments
 (0)