Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/services/capture.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Part } from "@opencode-ai/sdk";

import {
AUTOMATIC_CAPTURE_TIMEOUT_MS,
MAX_CAPTURE_ATTEMPTS,
buildCadenceBatches,
buildCaptureTurns,
buildSessionEndBatch,
Expand Down Expand Up @@ -284,4 +285,39 @@ describe("automatic conversation capture", () => {
]);
expect(readAttempts).toBe(3);
});
test("stops re-sending a batch after repeated failed writes", async () => {
let messages = conversation(1);
const writes: string[] = [];
const hook = createCaptureHook(
{ directory: "/repo", client: { session: { messages: async () => ({ data: messages }) } } },
{
canonical: "repo_test__0123456789abcdef",
user: "repo_test__0123456789abcdef",
project: "repo_test__0123456789abcdef",
projectId: "0123456789abcdef",
projectName: "test",
personalReads: [],
projectReads: [],
allReads: [],
},
{
captureEveryNTurns: 1,
memoryClient: {
ingestConversation: async (_id, _messages, _tags, _metadata, options) => {
writes.push(options?.customId ?? "");
return { success: false, error: "Timeout after 3250ms" };
},
},
},
);

for (let turn = 1; turn <= 6; turn += 1) {
messages = conversation(turn);
await hook.event({ event: { type: "session.idle", properties: { sessionID: "session-1" } } });
}

const firstBatch = writes.filter((id) => id === writes[0]).length;
expect(firstBatch).toBe(MAX_CAPTURE_ATTEMPTS);
expect(writes.length).toBeLessThan(21);
});
});
52 changes: 44 additions & 8 deletions src/services/capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@ import { isFullyPrivate, stripPrivateContent } from "./privacy.js";
import type { ResolvedTags } from "./tags.js";

export const AUTOMATIC_CAPTURE_TIMEOUT_MS = 3_000;
export const MAX_CAPTURE_ATTEMPTS = 3;

/**
* Bounds how often one batch is re-sent after failed writes. A write that timed
* out on the client may still have reached the server, so retrying it on every
* later turn stores the same conversation again each time.
*/
export class CaptureRetryBudget {
readonly #failures = new Map<string, number>();

constructor(private readonly maxAttempts = MAX_CAPTURE_ATTEMPTS) {}

/** Records a failed write. Returns true once the batch should stop retrying. */
recordFailure(captureId: string): boolean {
const failures = (this.#failures.get(captureId) ?? 0) + 1;
if (failures >= this.maxAttempts) {
this.#failures.delete(captureId);
return true;
}
this.#failures.set(captureId, failures);
return false;
}

recordSuccess(captureId: string): void {
this.#failures.delete(captureId);
}
}

interface CaptureMessageInfo {
id: string;
Expand Down Expand Up @@ -220,6 +247,7 @@ export function createCaptureHook(
const snapshots = new Map<string, CaptureTurn[]>();
const activeSessions = new Set<string>();
const completedCaptureIds = new Set<string>();
const retries = new CaptureRetryBudget();
const inFlight = new Map<string, Promise<void>>();

async function refreshSnapshot(sessionID: string): Promise<CaptureTurn[]> {
Expand Down Expand Up @@ -282,6 +310,7 @@ export function createCaptureHook(

if (result.success) {
completedCaptureIds.add(captureId);
retries.recordSuccess(captureId);
options?.onSaved?.();
log("[capture] conversation batch saved", {
sessionID,
Expand All @@ -292,14 +321,21 @@ export function createCaptureHook(
return true;
}

log("[capture] failed to save conversation batch", {
sessionID,
reason,
startTurn: batch.startTurn,
endTurn: batch.endTurn,
error: result.error,
});
return false;
const exhausted = retries.recordFailure(captureId);
if (exhausted) completedCaptureIds.add(captureId);
log(
exhausted
? "[capture] giving up on conversation batch after repeated failures"
: "[capture] failed to save conversation batch",
{
sessionID,
reason,
startTurn: batch.startTurn,
endTurn: batch.endTurn,
error: result.error,
},
);
return exhausted;
}

async function captureCadence(
Expand Down
Loading
Loading