Skip to content

Commit 651a653

Browse files
committed
refactor: minor audit-driven cleanups
- I-1 already landed in the LRU commit (singleSession resolveProjectIdentity threaded through transform.ts). - I-2: Reuse sessionMetaEarly read in system-prompt-hash.ts. The handler was calling getOrCreateSessionMeta twice per system-prompt transform — once early for isSubagent detection, again later for hash comparison. Nothing between the two reads mutates session_meta for that session, so the second read is redundant. Now short-circuits to the early read. - M-2: Broaden hasSuccessfulTask's post-task exclusion set in dreamer/ runner.ts. The filter only excluded "smart-notes" from the success gate, but `user memories` and `key files` post-task phases also run unconditionally after the main task loop. A project whose configured tasks (consolidate/verify/archive-stale/...) all failed but whose post-task phases succeeded would have last_dream_at advanced, silently suppressing re-scheduling of the failed tasks. Now all three post-task phase names are excluded from the success gate. Addresses audit findings I-2 and M-2.
1 parent bad700c commit 651a653

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

packages/plugin/src/features/magic-context/dreamer/runner.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,9 +730,15 @@ export async function runDream(args: {
730730
});
731731
// Only update dream timestamps when at least one task succeeded — failed runs
732732
// should not block re-scheduling for the project.
733-
// Only count configured dream tasks for success — smart-note evaluation is supplementary
734-
// and should not mask failures of real tasks like consolidate/verify/archive-stale
735-
const hasSuccessfulTask = result.tasks.some((t) => !t.error && t.name !== "smart-notes");
733+
//
734+
// Only count configured dream tasks (consolidate / verify / archive-stale /
735+
// improve / maintain-docs) for success. Post-task phases (smart-notes,
736+
// user memories, key files) run unconditionally after the main task loop
737+
// and must NOT mask failures of the configured tasks — otherwise a
738+
// successful key-file evaluation would suppress re-scheduling a project
739+
// whose consolidate/verify/archive tasks all failed.
740+
const POST_TASK_NAMES = new Set(["smart-notes", "user memories", "key files"]);
741+
const hasSuccessfulTask = result.tasks.some((t) => !t.error && !POST_TASK_NAMES.has(t.name));
736742
if (hasSuccessfulTask) {
737743
setDreamState(args.db, `last_dream_at:${args.projectIdentity}`, String(result.finishedAt));
738744
setDreamState(args.db, "last_dream_at", String(result.finishedAt));

packages/plugin/src/hooks/magic-context/system-prompt-hash.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,16 @@ export function createSystemPromptHashHandler(deps: {
359359
// causing precision loss on read-back and infinite hash-change flushes.
360360
const currentHash = new Bun.CryptoHasher("md5").update(systemContent).digest("hex");
361361

362-
let sessionMeta: import("../../features/magic-context/types").SessionMeta | undefined;
363-
try {
364-
sessionMeta = getOrCreateSessionMeta(deps.db, sessionId);
365-
} catch (error) {
366-
sessionLog(sessionId, "system-prompt-hash DB update failed:", error);
362+
// Reuse sessionMetaEarly from Step 1 — no code path between that read
363+
// and here mutates session_meta for this session, so a second DB read
364+
// would return identical data. If Step 1's read failed (sessionMetaEarly
365+
// is undefined), bail rather than re-attempting: we already logged the
366+
// error and can't make an informed hash-change decision without the
367+
// previous hash.
368+
if (!sessionMetaEarly) {
367369
return;
368370
}
369-
371+
const sessionMeta = sessionMetaEarly;
370372
const previousHash = sessionMeta.systemPromptHash;
371373
if (previousHash !== "" && previousHash !== "0" && previousHash !== currentHash) {
372374
sessionLog(

0 commit comments

Comments
 (0)