Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

fix(coding-agent): unblock cold chat openings during catalog scans (#2299)

Florian Brandcfb2d3fe35c7
diff --git a/packages/coding-agent/.changes/perf-cold-chat-ledger-queue.md b/packages/coding-agent/.changes/perf-cold-chat-ledger-queue.md
new file mode 100644
index 0000000000..6e3375824a
--- /dev/null
+++ b/packages/coding-agent/.changes/perf-cold-chat-ledger-queue.md
@@ -0,0 +1,2 @@
+- Fixed cold chat openings waiting behind saved-session catalog scans ([#2259](https://github.com/PrimeIntellect-ai/prime-agent/pull/2259)).
+- Fixed background scheduled-job scans slowing down agents with large saved chats ([#2259](https://github.com/PrimeIntellect-ai/prime-agent/pull/2259)).
diff --git a/packages/coding-agent/src/modes/daemon/daemon-supervisor.ts b/packages/coding-agent/src/modes/daemon/daemon-supervisor.ts
index 88874deab8..b77a9e5b14 100644
--- a/packages/coding-agent/src/modes/daemon/daemon-supervisor.ts
+++ b/packages/coding-agent/src/modes/daemon/daemon-supervisor.ts
@@ -961,7 +961,7 @@ export class DaemonSupervisor {
pendingCancelRoots.add(canonicalSessionPath(context.sessionFile));
}
}
- const infos = await this.rlmSpawnLedger().family();
+ const infos = await this.rlmSpawnLedger().family(SESSION_SCHEDULED_JOBS_FILENAME);
const infoByPath = new Map(infos.map((info) => [canonicalSessionPath(info.path), info] as const));
const storeBySessionId = new Map<string, AgentCronJobStore>();
const infoBySessionId = new Map<string, SessionInfo>();
diff --git a/packages/coding-agent/src/modes/daemon/rlm-ledger.ts b/packages/coding-agent/src/modes/daemon/rlm-ledger.ts
index dcad64224a..bc9ce07bdc 100644
--- a/packages/coding-agent/src/modes/daemon/rlm-ledger.ts
+++ b/packages/coding-agent/src/modes/daemon/rlm-ledger.ts
@@ -16,7 +16,7 @@ import { basename, dirname, join, resolve } from "node:path";
import { EventLog } from "../../core/event-log.js";
import { canonicalSessionPath } from "../../core/session-lease.js";
import { getSessionArtifactPathForFile, readSessionInfo, type SessionInfo } from "../../core/session-manager.js";
-import { readFirstLineSync } from "../../utils/file-lines.js";
+import { readFirstLineSync, readLinesAsBuffers } from "../../utils/file-lines.js";
/**
* Daemon-owned RLM spawn ledger.
@@ -40,6 +40,7 @@ export const RLM_LEDGER_DIR = "rlm-ledger";
/** Bounded read: a ledger beyond these limits fails closed loudly. */
export const RLM_LEDGER_MAX_BYTES = 32 * 1024 * 1024;
export const RLM_LEDGER_MAX_RECORDS = 100_000;
+const SESSION_HEADER_PROBE_MAX_BYTES = 1024 * 1024;
export type RlmLedgerDeleteReason = "user" | "parent-teardown" | "revoked" | "gc";
@@ -291,8 +292,8 @@ function edgeKey(childId: string, child: string): string {
}
/**
- * Per-sessions-dir spawn ledger. All operations are serialized on an internal
- * queue; the first operation lazily seeds a missing ledger from the existing
+ * Per-sessions-dir spawn ledger. Topology reads and writes are serialized on an
+ * internal queue; the first operation lazily seeds a missing ledger from the existing
* per-parent registries (memoized; a seeding failure degrades to an empty
* ledger and is never fail-closed).
*/
@@ -391,17 +392,19 @@ export class RlmSpawnLedger {
* readdir of *.jsonl roots as depth-0 rows plus live ledger edges, both
* reconciled by stat (a dead parent or child drops the edge). Depths are
* verified parent+1 between ledger-known depths; a contradictory edge is
- * dropped and logged, never fails the whole family.
+ * dropped and logged, never fails the whole family. With metadataArtifact,
+ * rows without that artifact retain topology but omit transcript display fields.
*/
- family(): Promise<SessionInfo[]> {
- return this.enqueue(() => this.familyUnlocked());
+ family(metadataArtifact?: string): Promise<SessionInfo[]> {
+ // Capture ordered topology first; transcript metadata must not block worker roster reads or appends.
+ return this.enqueue(() => this.liveEdgesUnlocked()).then((alive) => this.familyUnlocked(alive, metadataArtifact));
}
/** Same-parent rows for a child session path, including the child itself. */
siblings(sessionPath: string): Promise<SessionInfo[]> {
return this.enqueue(async () => {
const target = canonicalSessionPath(sessionPath);
- const family = await this.familyUnlocked();
+ const family = await this.familyUnlocked(await this.liveEdgesUnlocked());
const edges = [...this.replaySyncCached().values()].filter((edge) => !edge.deleted);
const parentByChild = new Map(
edges.map((edge) => [canonicalSessionPath(edge.child), canonicalSessionPath(edge.parent)]),
@@ -521,12 +524,9 @@ export class RlmSpawnLedger {
return alive;
}
- private async familyUnlocked(): Promise<SessionInfo[]> {
+ private async familyUnlocked(alive: RlmLedgerEdge[], metadataArtifact?: string): Promise<SessionInfo[]> {
// One replay, one stat snapshot: byChild comes from the same alive set that emits child rows,
// so a child whose dead edge was reconciled away degrades to a root row instead of vanishing.
- let alive: RlmLedgerEdge[] = await this.liveEdgesUnlocked(
- [...this.replaySyncCached().values()].filter((candidate) => !candidate.deleted),
- );
const byChild = new Map<string, RlmLedgerEdge>();
for (const edge of alive) {
byChild.set(canonicalSessionPath(edge.child), edge);
@@ -565,7 +565,7 @@ export class RlmSpawnLedger {
});
const rows: SessionInfo[] = [];
for (const rootPath of rootPaths) {
- rows.push(await this.sessionRow(rootPath, 0, undefined, undefined));
+ rows.push(await this.sessionRow(rootPath, 0, undefined, undefined, metadataArtifact));
}
for (const edge of alive) {
rows.push(
@@ -574,6 +574,7 @@ export class RlmSpawnLedger {
edge.depth,
canonicalSessionPath(edge.parent),
edge.name,
+ metadataArtifact,
),
);
}
@@ -585,14 +586,45 @@ export class RlmSpawnLedger {
depth: number,
parentPath: string | undefined,
name: string | undefined,
+ metadataArtifact?: string,
): Promise<SessionInfo> {
+ let id = basename(path, ".jsonl");
+ let loadMetadata = true;
+ if (metadataArtifact !== undefined) {
+ // Imported filenames need not match the session id. If the bounded header
+ // probe is inconclusive, keep the ordinary read so schedules are not hidden.
+ let bytesRead = 0;
+ try {
+ for await (const line of readLinesAsBuffers(path, { end: SESSION_HEADER_PROBE_MAX_BYTES - 1 })) {
+ bytesRead += line.length + 1;
+ if (bytesRead >= SESSION_HEADER_PROBE_MAX_BYTES) break;
+ const text = line.toString("utf8").trim();
+ if (!text) continue;
+ let header: { type?: unknown; id?: unknown } | null;
+ try {
+ header = JSON.parse(text);
+ } catch {
+ continue;
+ }
+ if (header?.type === "session" && typeof header.id === "string") id = header.id;
+ loadMetadata = await stat(join(getSessionArtifactPathForFile(path, id), metadataArtifact)).then(
+ () => true,
+ () => false,
+ );
+ break;
+ }
+ } catch {
+ // Unreadable probes use the ordinary best-effort metadata read.
+ }
+ }
+
// Display-grade fields are best-effort from the ordinary session-info
// read; topology (path, depth, parent) comes EXCLUSIVELY from the
// ledger: header-claimed parentSessionPath/rlmDepth (e.g. fork headers)
// are stripped, never passed through. For roots the ledger carries no
// name, so the name comes from this read — writer-owned display data,
// not authority.
- const info = await readSessionInfo(path).catch(() => null);
+ const info = loadMetadata ? await readSessionInfo(path).catch(() => null) : null;
if (info) {
const { parentSessionPath: _headerParent, rlmDepth: _headerDepth, ...display } = info;
return {
@@ -604,7 +636,7 @@ export class RlmSpawnLedger {
}
return {
path,
- id: basename(path, ".jsonl"),
+ id,
cwd: "",
...(name ? { name } : {}),
...(parentPath ? { parentSessionPath: parentPath } : {}),
diff --git a/packages/coding-agent/test/suite/regressions/1343-cold-chat-ledger-queue.test.ts b/packages/coding-agent/test/suite/regressions/1343-cold-chat-ledger-queue.test.ts
new file mode 100644
index 0000000000..8ef6fb16e2
--- /dev/null
+++ b/packages/coding-agent/test/suite/regressions/1343-cold-chat-ledger-queue.test.ts
@@ -0,0 +1,56 @@
+import { join } from "node:path";
+import { describe, expect, it, vi } from "vitest";
+import { canonicalSessionPath } from "../../../src/core/session-lease.js";
+import * as sessionManagerModule from "../../../src/core/session-manager.js";
+import { RlmSpawnLedger } from "../../../src/modes/daemon/rlm-ledger.js";
+import { createHarness } from "../harness.js";
+import { createDeferred } from "../scheduling.js";
+
+describe("cold chat opens during a saved-session scan", () => {
+ it("keeps roster reads and appends moving while preserving the scan's captured topology", async () => {
+ const harness = await createHarness({ persistSession: true });
+ const parent = harness.sessionManager;
+ parent.appendSessionInfo("parent");
+ parent.flushNow();
+ const parentFile = canonicalSessionPath(parent.getSessionFile()!);
+ const child = sessionManagerModule.SessionManager.create(harness.tempDir, join(harness.tempDir, "children"));
+ child.appendSessionInfo("child");
+ child.flushNow();
+ const childFile = canonicalSessionPath(child.getSessionFile()!);
+ const ledger = new RlmSpawnLedger(harness.tempDir, join(harness.tempDir, "sessions"));
+ const edge = { childId: "child", parent: parentFile, child: childFile, depth: 1, name: "before" };
+ await ledger.appendSpawn(edge);
+ const readSessionInfo = sessionManagerModule.readSessionInfo;
+ const started = createDeferred();
+ const release = createDeferred();
+ const readSpy = vi.spyOn(sessionManagerModule, "readSessionInfo").mockImplementation(async (...args) => {
+ started.resolve();
+ await release.promise;
+ return readSessionInfo(...args);
+ });
+ const scan = ledger.family();
+ try {
+ await started.promise;
+ let rosterReady = false;
+ const roster = ledger.liveEdges().then((edges) => {
+ rosterReady = true;
+ return edges;
+ });
+ await vi.waitFor(() => expect(rosterReady).toBe(true));
+ await expect(roster).resolves.toEqual([edge]);
+ await ledger.appendRename({ childId: "child", child: childFile, name: "after" });
+ await expect(ledger.liveEdges()).resolves.toEqual([{ ...edge, name: "after" }]);
+ release.resolve();
+ expect((await scan).find((row) => row.path === childFile)).toMatchObject({ name: "before", rlmDepth: 1 });
+ expect((await ledger.family()).find((row) => row.path === childFile)).toMatchObject({
+ name: "after",
+ rlmDepth: 1,
+ });
+ } finally {
+ release.resolve();
+ await scan;
+ readSpy.mockRestore();
+ await harness.cleanup();
+ }
+ });
+});
diff --git a/packages/coding-agent/test/suite/regressions/2259-scheduled-catalog-metadata.test.ts b/packages/coding-agent/test/suite/regressions/2259-scheduled-catalog-metadata.test.ts
new file mode 100644
index 0000000000..3715f77d17
--- /dev/null
+++ b/packages/coding-agent/test/suite/regressions/2259-scheduled-catalog-metadata.test.ts
@@ -0,0 +1,127 @@
+import { readFileSync, realpathSync, renameSync, writeFileSync } from "node:fs";
+import { join } from "node:path";
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { type AgentCronJob, AgentCronJobStore } from "../../../src/core/cron-jobs.js";
+import * as sessionManager from "../../../src/core/session-manager.js";
+import { DaemonSupervisor } from "../../../src/modes/daemon/daemon-supervisor.js";
+import type { RlmSpawnLedger } from "../../../src/modes/daemon/rlm-ledger.js";
+import { createHarness, type Harness } from "../harness.js";
+
+interface SupervisorHarness {
+ defaultSessionConfig: { agentDir: string };
+ rlmSpawnLedger(): RlmSpawnLedger;
+ collectPassiveScheduledJobs(): Promise<
+ Array<{ rootSessionFile: string; job: AgentCronJob; info: sessionManager.SessionInfo }>
+ >;
+ findWorkerBySessionFile(path: string): unknown;
+}
+const harnesses: Harness[] = [];
+afterEach(() => {
+ vi.restoreAllMocks();
+ for (const harness of harnesses.splice(0)) harness.cleanup();
+});
+async function createSupervisorHarness(): Promise<SupervisorHarness> {
+ const harness = await createHarness();
+ harnesses.push(harness);
+ const directory = harness.tempDir;
+ return new DaemonSupervisor(join(directory, "daemon.sock"), {
+ defaultSessionConfig: { agentDir: directory, cwd: directory },
+ descriptorDir: join(directory, "workers"),
+ }) as unknown as SupervisorHarness;
+}
+describe("scheduled catalog metadata scans", () => {
+ it.each(["", "\n \t\r\n", "\n".repeat(65_536), "malformed JSON\n", "\n".repeat(1024 * 1024)])(
+ "only scans scheduled transcripts while retaining ancestry, imported session ids, and archived filtering (case %#)",
+ async (prefix) => {
+ const supervisor = await createSupervisorHarness();
+ const directory = realpathSync(supervisor.defaultSessionConfig.agentDir);
+ const [parent, child, archived, unrelated] = ["parent", "child", "archived", "unrelated"].map((name) => {
+ const manager = sessionManager.SessionManager.create(directory, join(directory, "sessions"));
+ manager.newSession({ parentSession: join(directory, "stale-parent.jsonl"), rlmDepth: 9 });
+ manager.appendSessionInfo(name);
+ manager.appendMessage({ role: "user", content: name, timestamp: 1 });
+ if (name === "archived") manager.appendSessionState({ status: "archived" });
+ manager.flushNow();
+ return manager;
+ });
+ const parentFile = parent!.getSessionFile()!;
+ const childFile = join(directory, "sessions", "imported-child.jsonl");
+ renameSync(child!.getSessionFile()!, childFile);
+ writeFileSync(childFile, prefix + readFileSync(childFile, "utf8"));
+ await supervisor.rlmSpawnLedger().appendSpawn({
+ childId: "sub-11111111",
+ parent: parentFile,
+ child: childFile,
+ depth: 1,
+ name: "ledger-child",
+ });
+ const store = AgentCronJobStore.forSessionArtifacts();
+ for (const manager of [child!, archived!]) {
+ store.registerSessionArtifact(manager.getSessionId(), manager.getSessionArtifactDir()!);
+ store.createHeartbeat({
+ activeSessionId: manager.getSessionId(),
+ sessionId: manager.getSessionId(),
+ sessionFile: manager === child ? childFile : manager.getSessionFile()!,
+ cwd: directory,
+ scheduleText: "every 1h",
+ prompt: "continue",
+ });
+ }
+ const readInfo = vi.spyOn(sessionManager, "readSessionInfo");
+ const jobs = await supervisor.collectPassiveScheduledJobs();
+ expect(jobs).toHaveLength(1);
+ expect(jobs[0]).toMatchObject({
+ rootSessionFile: parentFile,
+ info: {
+ id: child!.getSessionId(),
+ name: "ledger-child",
+ firstMessage: "child",
+ parentSessionPath: parentFile,
+ rlmDepth: 1,
+ },
+ });
+ expect(readInfo).toHaveBeenCalledTimes(2);
+ expect(readInfo).not.toHaveBeenCalledWith(parentFile);
+ expect(readInfo).not.toHaveBeenCalledWith(unrelated!.getSessionFile());
+ const late = unrelated!;
+ store.registerSessionArtifact(late.getSessionId(), late.getSessionArtifactDir()!);
+ store.createHeartbeat({
+ activeSessionId: late.getSessionId(),
+ sessionId: late.getSessionId(),
+ sessionFile: late.getSessionFile()!,
+ cwd: directory,
+ scheduleText: "every 1h",
+ prompt: "continue",
+ });
+ expect(await supervisor.collectPassiveScheduledJobs()).toHaveLength(2);
+ expect(readInfo).toHaveBeenCalledWith(late.getSessionFile());
+ vi.spyOn(supervisor, "findWorkerBySessionFile").mockImplementation((path) =>
+ path === parentFile ? {} : undefined,
+ );
+ expect(await supervisor.collectPassiveScheduledJobs()).toMatchObject([{ info: { id: late.getSessionId() } }]);
+ },
+ );
+
+ it.each(["\n", "x"])("falls back to full metadata for oversized header probes (case %#)", async (prefix) => {
+ const supervisor = await createSupervisorHarness();
+ const directory = realpathSync(supervisor.defaultSessionConfig.agentDir);
+ const manager = sessionManager.SessionManager.create(directory, join(directory, "sessions"));
+ manager.appendMessage({ role: "user", content: "hello", timestamp: 1 });
+ manager.flushNow();
+ const file = manager.getSessionFile()!;
+ writeFileSync(file, `${prefix.repeat(2 * 1024 * 1024)}\n${readFileSync(file, "utf8")}`);
+ const readInfo = vi.spyOn(sessionManager, "readSessionInfo");
+ const store = AgentCronJobStore.forSessionArtifacts();
+ store.registerSessionArtifact(manager.getSessionId(), manager.getSessionArtifactDir()!);
+ store.createHeartbeat({
+ activeSessionId: manager.getSessionId(),
+ sessionId: manager.getSessionId(),
+ sessionFile: file,
+ cwd: directory,
+ scheduleText: "every 1h",
+ prompt: "continue",
+ });
+ expect(await supervisor.collectPassiveScheduledJobs()).toMatchObject([{ info: { id: manager.getSessionId() } }]);
+ expect(readInfo).toHaveBeenCalledWith(file);
+ });
+});