Skip to content

Commit 550c107

Browse files
feat(diagnostics): slow write-transaction logging — commit-time site+duration attribution for #362 (historian-publish, recomp, lease-guarded writes, clear-session, marker-drain, migration-runner; 1s threshold, log-only)
Co-authored-by: Alfonso <[email protected]>
2 parents 45b54af + 6a9d480 commit 550c107

8 files changed

Lines changed: 113 additions & 10 deletions

File tree

‎packages/plugin/src/features/magic-context/dreamer/lease.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { mkdtempSync, rmSync } from "node:fs";
55
import { tmpdir } from "node:os";
66
import { join } from "node:path";
77
import { $ } from "bun";
8+
import * as logger from "../../../shared/logger";
89
import { Database } from "../../../shared/sqlite";
910
import { closeQuietly } from "../../../shared/sqlite-helpers";
1011
import { runMigrations } from "../migrations";
@@ -150,6 +151,61 @@ describe("dreamer lease (atomic CAS)", () => {
150151
closeQuietly(db);
151152
});
152153

154+
it("logs a slow guarded write after COMMIT with its lease domain", () => {
155+
const db = makeDb();
156+
const events: string[] = [];
157+
const realExec = db.exec.bind(db);
158+
const execSpy = spyOn(db, "exec").mockImplementation((sql: string) => {
159+
events.push(sql);
160+
return realExec(sql);
161+
});
162+
const logSpy = spyOn(logger, "log").mockImplementation((message: string) => {
163+
events.push(`log:${message}`);
164+
});
165+
const nowSpy = spyOn(performance, "now")
166+
.mockImplementationOnce(() => 0)
167+
.mockImplementationOnce(() => 1_001);
168+
try {
169+
expect(acquireLease(db, "holder-a", "memory:proj")).toBe(true);
170+
db.prepare("CREATE TABLE guarded_write_timing (value TEXT)").run();
171+
runLeaseGuardedWrite(db, "holder-a", "memory:proj", () => {
172+
events.push("body");
173+
db.prepare("INSERT INTO guarded_write_timing (value) VALUES ('committed')").run();
174+
});
175+
176+
const commitIndex = events.indexOf("COMMIT");
177+
const logIndex = events.findIndex((event) => event.startsWith("log:"));
178+
expect(commitIndex).toBeGreaterThanOrEqual(0);
179+
expect(logIndex).toBeGreaterThan(commitIndex);
180+
expect(logSpy).toHaveBeenCalledWith(
181+
expect.stringContaining("site=lease-guarded-write:memory:proj"),
182+
);
183+
expect(logSpy.mock.calls[0]?.[0]).toMatch(/held=1001\.0ms/);
184+
} finally {
185+
nowSpy.mockRestore();
186+
logSpy.mockRestore();
187+
execSpy.mockRestore();
188+
closeQuietly(db);
189+
}
190+
});
191+
192+
it("does not log a guarded write below the slow-write threshold", () => {
193+
const db = makeDb();
194+
const logSpy = spyOn(logger, "log").mockImplementation(() => {});
195+
const nowSpy = spyOn(performance, "now")
196+
.mockImplementationOnce(() => 0)
197+
.mockImplementationOnce(() => 999);
198+
try {
199+
expect(acquireLease(db, "holder-a", "memory:proj")).toBe(true);
200+
runLeaseGuardedWrite(db, "holder-a", "memory:proj", () => {});
201+
expect(logSpy).not.toHaveBeenCalled();
202+
} finally {
203+
nowSpy.mockRestore();
204+
logSpy.mockRestore();
205+
closeQuietly(db);
206+
}
207+
});
208+
153209
it("allows exactly one winner across separate DB handles", () => {
154210
const dir = mkdtempSync(join(tmpdir(), "mc-dream-lease-handles-"));
155211
const path = join(dir, "context.db");

‎packages/plugin/src/features/magic-context/dreamer/lease.ts‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Database } from "../../../shared/sqlite";
2+
import { logSlowWriteTransaction } from "../../../shared/write-transaction-timing";
23
import { deleteDreamState, getDreamState, setDreamState } from "./storage-dream-state";
34

45
const LEASE_DURATION_MS = 2 * 60 * 1000; // 2 minutes — renewed periodically during task execution
@@ -105,13 +106,17 @@ export function leaseOwnershipMatches(
105106
// = false under WAL snapshot isolation and both write — double-acquiring the
106107
// lease and spawning duplicate dreamer workers. busy_timeout (set in
107108
// initializeDatabase) makes the loser wait rather than throw SQLITE_BUSY.
108-
function runImmediate<T>(db: Database, body: () => T): T {
109+
function runImmediate<T>(db: Database, body: () => T, site?: string): T {
110+
const transactionStartedAt = site === undefined ? undefined : performance.now();
109111
db.exec("BEGIN IMMEDIATE");
110112
let committed = false;
111113
try {
112114
const result = body();
113115
db.exec("COMMIT");
114116
committed = true;
117+
if (site !== undefined && transactionStartedAt !== undefined) {
118+
logSlowWriteTransaction(site, transactionStartedAt);
119+
}
115120
return result;
116121
} finally {
117122
if (!committed) {
@@ -191,15 +196,19 @@ export function runLeaseGuardedWrite<T>(
191196
leaseKey: string,
192197
fn: () => T,
193198
): T {
194-
return runImmediate(db, () => {
195-
// The lease is checked after BEGIN IMMEDIATE has acquired SQLite's write
196-
// lock. That removes the deferred-transaction gap where another process
197-
// could steal the lease after a peek but before the durable mutation.
198-
if (!peekLeaseHolderAndExpiry(db, holderId, leaseKey)) {
199-
throw new Error("Dream lease lost before guarded write");
200-
}
201-
return fn();
202-
});
199+
return runImmediate(
200+
db,
201+
() => {
202+
// The lease is checked after BEGIN IMMEDIATE has acquired SQLite's write
203+
// lock. That removes the deferred-transaction gap where another process
204+
// could steal the lease after a peek but before the durable mutation.
205+
if (!peekLeaseHolderAndExpiry(db, holderId, leaseKey)) {
206+
throw new Error("Dream lease lost before guarded write");
207+
}
208+
return fn();
209+
},
210+
`lease-guarded-write:${leaseKey}`,
211+
);
203212
}
204213

205214
/** Renewal beat interval. The lease TTL is LEASE_DURATION_MS (2×), so a single

‎packages/plugin/src/features/magic-context/migrations.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { extractTiersFromInner } from "../../hooks/magic-context/compartment-parser";
22
import { log } from "../../shared/logger";
33
import type { Database } from "../../shared/sqlite";
4+
import { logSlowWriteTransaction } from "../../shared/write-transaction-timing";
45
import { ensureColumn, healAllNullColumns } from "./storage-schema-helpers";
56
import { bumpEpochsForWorkspaceMemberSet } from "./workspaces";
67

@@ -2970,6 +2971,7 @@ export function runMigrations(db: Database): void {
29702971
let migration: Migration | undefined;
29712972
let currentVersion = 0;
29722973
try {
2974+
const transactionStartedAt = performance.now();
29732975
const applied = db
29742976
.transaction(() => {
29752977
currentVersion = getCurrentVersion(db);
@@ -3004,6 +3006,7 @@ export function runMigrations(db: Database): void {
30043006
return true;
30053007
})
30063008
.immediate();
3009+
logSlowWriteTransaction("migration-runner", transactionStartedAt);
30073010

30083011
if (!applied || !migration) break;
30093012
if (migration.version <= 61) touchedLegacyAuthorityBatch = true;
@@ -3040,7 +3043,9 @@ export function runMigrations(db: Database): void {
30403043

30413044
if (touchedLegacyAuthorityBatch) {
30423045
try {
3046+
const transactionStartedAt = performance.now();
30433047
db.transaction(() => installLatestAuthorityTriggers(db)).immediate();
3048+
logSlowWriteTransaction("migration-runner", transactionStartedAt);
30443049
} catch (error) {
30453050
throw new Error(
30463051
`Migration authority-trigger postcondition failed: ${error instanceof Error ? error.message : String(error)}. Database may need manual repair.`,

‎packages/plugin/src/features/magic-context/storage-meta-session.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Buffer } from "node:buffer";
22
import { getHarness } from "../../shared/harness";
33
import { piModelRefToCanonical } from "../../shared/harness-provider-map";
44
import type { Database } from "../../shared/sqlite";
5+
import { logSlowWriteTransaction } from "../../shared/write-transaction-timing";
56
import { resolveIsSubagentFromOpenCodeDb } from "./resolve-subagent-fallback";
67
import {
78
BOOLEAN_META_KEYS,
@@ -212,7 +213,9 @@ export function retryPendingSessionCleanups(
212213
}
213214

214215
export function clearSession(db: Database, sessionId: string): void {
216+
const transactionStartedAt = performance.now();
215217
db.transaction(() => {
216218
deleteSessionScopedRows(db, [sessionId]);
217219
})();
220+
logSlowWriteTransaction("clear-session", transactionStartedAt);
218221
}

‎packages/plugin/src/hooks/magic-context/compaction-marker-manager.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { log, sessionLog } from "../../shared/logger";
4040
import type { Database } from "../../shared/sqlite";
4141
import { Database as SqliteDb } from "../../shared/sqlite";
4242
import { closeQuietly } from "../../shared/sqlite-helpers";
43+
import { logSlowWriteTransaction } from "../../shared/write-transaction-timing";
4344

4445
/** Static placeholder. The real session-history comes from transform injection. */
4546
export const MARKER_SUMMARY_TEXT =
@@ -56,12 +57,14 @@ function persistMarkerStateAndDropReplacedTag(
5657
state: PersistedCompactionMarkerState | null,
5758
replacedSummaryMessageId: string | null,
5859
): void {
60+
const transactionStartedAt = performance.now();
5961
db.transaction(() => {
6062
setPersistedCompactionMarkerState(db, sessionId, state);
6163
if (replacedSummaryMessageId !== null) {
6264
dropMarkerSummaryTag(db, sessionId, replacedSummaryMessageId);
6365
}
6466
})();
67+
logSlowWriteTransaction("marker-drain", transactionStartedAt);
6568
}
6669

6770
/**

‎packages/plugin/src/hooks/magic-context/compartment-runner-incremental.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import { insertUserMemoryCandidates } from "../../features/magic-context/user-me
5353
import { normalizeSDKResponse } from "../../shared";
5454
import { describeError } from "../../shared/error-message";
5555
import { sessionLog } from "../../shared/logger";
56+
import { logSlowWriteTransaction } from "../../shared/write-transaction-timing";
5657
import { updateCompactionMarkerAfterPublication } from "./compaction-marker-manager";
5758
import { buildCompartmentAgentPrompt } from "./compartment-prompt";
5859
import { queueDropsForCompartmentalizedMessages } from "./compartment-runner-drop-queue";
@@ -629,6 +630,7 @@ export async function runCompartmentAgent(deps: CompartmentRunnerDeps): Promise<
629630
return;
630631
}
631632
let published = false;
633+
const transactionStartedAt = performance.now();
632634
db.exec("BEGIN IMMEDIATE");
633635
try {
634636
if (!isCompartmentLeaseHeld(db, sessionId, holderId)) {
@@ -719,6 +721,7 @@ export async function runCompartmentAgent(deps: CompartmentRunnerDeps): Promise<
719721
}
720722
db.exec("COMMIT");
721723
published = true;
724+
logSlowWriteTransaction("historian-publish", transactionStartedAt);
722725
} finally {
723726
if (!published) {
724727
try {

‎packages/plugin/src/hooks/magic-context/compartment-runner-recomp.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { getErrorMessage } from "../../shared/error-message";
2626
import { getHarness } from "../../shared/harness";
2727
import { sessionLog } from "../../shared/logger";
2828
import type { Database } from "../../shared/sqlite";
29+
import { logSlowWriteTransaction } from "../../shared/write-transaction-timing";
2930
import { updateCompactionMarkerAfterPublication } from "./compaction-marker-manager";
3031
import { buildCompartmentAgentPrompt } from "./compartment-prompt";
3132
import { queueDropsForCompartmentalizedMessages } from "./compartment-runner-drop-queue";
@@ -91,6 +92,7 @@ export function promoteRecompStagingWithM0Mutation(
9192
facts: Array<{ category: string; content: string }>;
9293
} | null {
9394
const now = Date.now();
95+
const transactionStartedAt = performance.now();
9496
db.exec("BEGIN IMMEDIATE");
9597
let finished = false;
9698
try {
@@ -127,6 +129,7 @@ export function promoteRecompStagingWithM0Mutation(
127129

128130
db.exec("COMMIT");
129131
finished = true;
132+
logSlowWriteTransaction("historian-publish:recomp", transactionStartedAt);
130133
return { compartments: staging.compartments, facts: staging.facts };
131134
} finally {
132135
if (!finished) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { log } from "./logger";
2+
3+
/**
4+
* A one-second threshold catches transactions that can noticeably hold back
5+
* sibling writers without turning normal SQLite work into log noise.
6+
*/
7+
export const SLOW_WRITE_TRANSACTION_THRESHOLD_MS = 1_000;
8+
9+
/**
10+
* Log a slow write only after its COMMIT has completed. The timing diagnostic is
11+
* deliberately best-effort so it can never change the transaction's behavior.
12+
*/
13+
export function logSlowWriteTransaction(site: string, startedAt: number): void {
14+
try {
15+
const durationMs = performance.now() - startedAt;
16+
if (durationMs < SLOW_WRITE_TRANSACTION_THRESHOLD_MS) return;
17+
log(`[magic-context] slow write transaction: site=${site} held=${durationMs.toFixed(1)}ms`);
18+
} catch {
19+
// Timing diagnostics must never make a committed write appear to fail.
20+
}
21+
}

0 commit comments

Comments
 (0)