@@ -6,8 +6,9 @@ import { acquireLease, releaseLease } from "../features/magic-context/dreamer/le
66import { openOpenCodeDb } from "../features/magic-context/dreamer/open-opencode-db" ;
77import {
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