Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
1494a70
test(server): a thread should keep one pending summary pass
ScriptedAlchemy Nov 16, 2026
2097a4f
fix(server): keep one pending conversation-summary pass per thread
ScriptedAlchemy Nov 16, 2026
c9a88d1
test(server): roll up covered roots before a later uncovered stretch
ScriptedAlchemy Nov 16, 2026
dcf1aa9
fix(server): roll up roots that end before the first summary gap
ScriptedAlchemy Nov 16, 2026
01fd8a8
test(server): a bad summary chunk should not stall the thread
ScriptedAlchemy Nov 16, 2026
b0a0f8c
fix(server): skip a summary chunk after two empty or invalid replies
ScriptedAlchemy Nov 16, 2026
fdac97c
docs: clarify why roll-ups stop at the first summary gap
ScriptedAlchemy Nov 16, 2026
d764f74
test(server): a summary pass that steps aside is pruned, and a retryi…
ScriptedAlchemy Nov 16, 2026
83991b3
fix(server): a superseded summary pass is cancelled, and only a fresh…
ScriptedAlchemy Nov 16, 2026
bb93b94
test: empty and unreadable model replies carry a typed kind that summ…
ScriptedAlchemy Nov 16, 2026
cb1974b
fix(server): summaries classify an unusable reply by its typed kind, …
ScriptedAlchemy Nov 16, 2026
2977ca7
test(server): a skipped summary stretch needs other stretches to succ…
ScriptedAlchemy Nov 16, 2026
8b1ece2
fix(server): an outage skips no summaries, a skip expires with the mo…
ScriptedAlchemy Nov 16, 2026
d71b7de
docs: say what a skipped summary stretch means and that roll-ups pass it
ScriptedAlchemy Nov 16, 2026
c34e8a3
test(server): three summaries that fail together do not stop a thread…
ScriptedAlchemy Nov 16, 2026
cfca0cf
fix(server): summaries that fail together become suspects, so a threa…
ScriptedAlchemy Nov 16, 2026
3ef3439
test(server): a skipped summary stretch survives a full failure ledger
ScriptedAlchemy Nov 16, 2026
d9f05c4
fix(server): trimming the summary failure ledger keeps skipped stretches
ScriptedAlchemy Nov 16, 2026
184c45f
test(server): a Claude error result is not labelled an unreadable reply
ScriptedAlchemy Nov 16, 2026
41016cf
fix(server): only a Claude reply the model gave is labelled unreadable
ScriptedAlchemy Nov 16, 2026
5510143
style(server): keep the retention comment on the retention constant
ScriptedAlchemy Nov 16, 2026
5e6bfce
test(server): a Claude run that exits 1 out of structured output retr…
ScriptedAlchemy Nov 16, 2026
b30622f
fix(server): a Claude run that exits 1 out of structured output retri…
ScriptedAlchemy Nov 16, 2026
7edb723
test(server): after an outage, a summary stretch is charged only for …
ScriptedAlchemy Nov 16, 2026
df1b5d2
fix(server): a summary stretch is charged only for its own failure be…
ScriptedAlchemy Nov 16, 2026
93c6449
test(server): a summary pass with only suspect stretches makes one mo…
ScriptedAlchemy Nov 16, 2026
b7cebf9
fix(server): a summary pass tries one suspect stretch while nothing h…
ScriptedAlchemy Nov 16, 2026
da79eae
test(server): a summary stretch that fails twice is skipped even when…
ScriptedAlchemy Nov 16, 2026
d5f3a58
fix(server): skipped summary stretches leave room in the failure ledg…
ScriptedAlchemy Nov 16, 2026
a451a1d
docs: state when a summary failure counts and how an outage interacts…
ScriptedAlchemy Nov 16, 2026
66d133e
test(server): summary failures that should be worked around, retried …
ScriptedAlchemy Nov 16, 2026
bd4d473
fix(server): summary failures have a way around, a way out, and a vis…
ScriptedAlchemy Nov 16, 2026
7224a93
docs: shorten the summary skip paragraph to when a failure counts, wh…
ScriptedAlchemy Nov 16, 2026
70217d4
test(server): summary passes that fail beside a success, or race a re…
ScriptedAlchemy Nov 16, 2026
69ba707
fix(server): unlabelled summary failures can be skipped, and a re-ena…
ScriptedAlchemy Nov 16, 2026
237c21f
test(server): a pass whose first summary succeeds and the rest time o…
ScriptedAlchemy Nov 16, 2026
a67d769
fix(server): a timeout counts toward a summary skip only when a later…
ScriptedAlchemy Nov 16, 2026
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
70 changes: 67 additions & 3 deletions apps/server/src/orchestration-v2/EffectOutbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export const OrchestrationEffectStatusV2 = Schema.Literals([
]);
export type OrchestrationEffectStatusV2 = typeof OrchestrationEffectStatusV2.Type;

const SUMMARY_PASS_SUPERSEDED = "Superseded by a pending summary pass for the same thread.";
/** Receipts deduplicate retries, and restart re-enqueues need far less than a week. Retain failed rows to keep deleted-thread worktrees. */
export const SETTLED_EFFECT_RETENTION = Duration.days(7);
const PRUNE_BATCH_SIZE = 500;
Expand Down Expand Up @@ -406,6 +407,17 @@ export const layer: Layer.Layer<EffectOutboxV2, never, SqlClient.SqlClient> = La
).pipe(Effect.uninterruptible);
};
// Retry backoff must block later effects so turns cannot race a pending rollback.
// A pending pass for the same thread re-reads the whole thread, so this row has nothing left to do.
const summaryPassHasPendingSuccessor = sql`
orchestration_v2_effect_outbox.effect_type = 'conversation-summary.generate'
AND EXISTS (
SELECT 1 FROM orchestration_v2_effect_outbox AS pending_summary
WHERE pending_summary.thread_id = orchestration_v2_effect_outbox.thread_id
AND pending_summary.effect_type = 'conversation-summary.generate'
AND pending_summary.status = 'pending'
)
`;

const claimableCandidatePredicate = (
availableBefore?: string,
excludeRestartContinuations = false,
Expand Down Expand Up @@ -505,14 +517,29 @@ export const layer: Layer.Layer<EffectOutboxV2, never, SqlClient.SqlClient> = La
${DateTime.formatIso(effect.availableAt ?? now)},
${nowIso},
${nowIso}
WHERE ${effect.request.type !== "project-knowledge.scrub-deliveries" || effect.request.afterId !== undefined ? sql`1 = 1` : sql`1 = 0`}
WHERE (
${effect.request.type !== "project-knowledge.scrub-deliveries" || effect.request.afterId !== undefined ? sql`1 = 1` : sql`1 = 0`}
OR NOT EXISTS (
SELECT 1 FROM orchestration_v2_effect_outbox AS pending
WHERE pending.thread_id = ${effect.threadId}
AND pending.effect_type = 'project-knowledge.scrub-deliveries'
AND pending.status = 'pending'
AND json_extract(pending.payload_json, '$.afterId') IS NULL
)
)
-- A running pass may enqueue its successor. Another pending pass would rehash the thread.
-- A retrying pass can still exhaust its attempts, so it does not stand in for this one.
AND ${
effect.request.type === "conversation-summary.generate"
? sql`NOT EXISTS (
SELECT 1 FROM orchestration_v2_effect_outbox AS pending_summary
WHERE pending_summary.thread_id = ${effect.threadId}
AND pending_summary.effect_type = 'conversation-summary.generate'
AND pending_summary.status = 'pending'
AND pending_summary.attempt_count = 0
)`
: sql`1 = 1`
}
ON CONFLICT(effect_id) DO NOTHING
`,
{ concurrency: 1, discard: true },
Expand Down Expand Up @@ -614,6 +641,19 @@ export const layer: Layer.Layer<EffectOutboxV2, never, SqlClient.SqlClient> = La
AND effect_type IN ${sql.in(PROCESS_BOUND_EFFECT_TYPES)}
RETURNING effect_id
`;
// The successor was enqueued before the crash, so resuming this row too would run two passes.
const coveredRows = yield* sql<{ readonly effect_id: string }>`
UPDATE orchestration_v2_effect_outbox
SET
status = 'cancelled',
lease_owner = NULL,
completed_at = ${now},
updated_at = ${now},
last_error = ${SUMMARY_PASS_SUPERSEDED}
WHERE status = 'running'
AND ${summaryPassHasPendingSuccessor}
RETURNING effect_id
`;
const requeuedRows = yield* sql<{ readonly effect_id: string }>`
UPDATE orchestration_v2_effect_outbox
SET
Expand All @@ -626,8 +666,12 @@ export const layer: Layer.Layer<EffectOutboxV2, never, SqlClient.SqlClient> = La
AND effect_type IN ${sql.in(REPLAY_SAFE_EFFECT_TYPES_AFTER_PROCESS_LOSS)}
RETURNING effect_id
`;
if (requeuedRows.length > 0) yield* notifyAvailable(requeuedRows.length);
return { requeued: requeuedRows.length, cancelled: cancelledRows.length };
if (requeuedRows.length > 0 || coveredRows.length > 0)
yield* notifyAvailable(requeuedRows.length + coveredRows.length);
return {
requeued: requeuedRows.length,
cancelled: cancelledRows.length + coveredRows.length,
};
}).pipe(
Effect.mapError(
(cause) => new EffectOutboxError({ operation: "reconcile-process-loss", cause }),
Expand Down Expand Up @@ -756,6 +800,26 @@ export const layer: Layer.Layer<EffectOutboxV2, never, SqlClient.SqlClient> = La
const availableAt = DateTime.formatIso(
DateTime.add(now, { milliseconds: Math.max(0, delayMs) }),
);
const yielded = yield* sql<{ readonly effect_id: string }>`
UPDATE orchestration_v2_effect_outbox
SET
status = 'cancelled',
lease_owner = NULL,
completed_at = ${nowIso},
updated_at = ${nowIso},
last_error = ${SUMMARY_PASS_SUPERSEDED}
WHERE effect_id = ${effectId}
AND status = 'running'
AND lease_owner = ${workerId}
AND ${summaryPassHasPendingSuccessor}
RETURNING effect_id
`;
if (yielded.length === 1) {
cancellationSignals.delete(effectId);
yield* signalSettled([effectId]);
yield* notifyAvailable();
return true;
}
const rows = yield* sql<{ readonly effect_id: string }>`
UPDATE orchestration_v2_effect_outbox
SET
Expand Down
139 changes: 139 additions & 0 deletions apps/server/src/orchestration-v2/EffectWorker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { modelEvaluationEffects } from "../modelEvaluations/modelEvaluationSched
import {
ConversationSummaryPassError,
ConversationSummaryRuntime,
conversationSummaryEffect,
} from "../project/conversationSummaries/conversationSummarySchedule.ts";
import {
DeliveryScrubRunner,
Expand Down Expand Up @@ -201,6 +202,144 @@ it.effect("retries a failed summary pass before scheduling its continuation", ()
}),
);

it.effect("keeps at most one pending summary pass per thread", () =>
Effect.gen(function* () {
const threadA = ThreadId.make("thread:summary-one-pending");
const threadB = ThreadId.make("thread:summary-other");
const first = conversationSummaryEffect({
threadId: threadA,
runId: RunId.make("run:summary-one-pending-a"),
});
const second = conversationSummaryEffect({
threadId: threadA,
runId: RunId.make("run:summary-one-pending-b"),
});
const other = conversationSummaryEffect({
threadId: threadB,
runId: RunId.make("run:summary-one-pending-c"),
});
const layer = EffectOutbox.layer.pipe(Layer.provide(SqlitePersistence.layerMemory));
yield* Effect.gen(function* () {
const outbox = yield* EffectOutbox.EffectOutboxV2;
yield* outbox.enqueue([first, second, other]);
assert.isTrue(Option.isSome(yield* outbox.get(first.id)));
assert.isTrue(Option.isNone(yield* outbox.get(second.id)));
assert.isTrue(Option.isSome(yield* outbox.get(other.id)));

const workerId = "summary-dedupe";
const claimed = yield* outbox.claimNext({ workerId, lane: "maintenance" });
assert.deepEqual(
Option.map(claimed, (effect) => effect.id),
Option.some(first.id),
);
const otherClaimed = yield* outbox.claimNext({ workerId, lane: "maintenance" });
assert.deepEqual(
Option.map(otherClaimed, (effect) => effect.id),
Option.some(other.id),
);
assert.isTrue(yield* outbox.succeed({ effectId: other.id, workerId }));

const continuation = conversationSummaryEffect({
threadId: threadA,
runId: RunId.make("run:summary-one-pending-a"),
pass: 1,
});
const third = conversationSummaryEffect({
threadId: threadA,
runId: RunId.make("run:summary-one-pending-d"),
});
yield* outbox.enqueue([continuation, third]);
assert.isTrue(Option.isSome(yield* outbox.get(continuation.id)));
assert.isTrue(Option.isNone(yield* outbox.get(third.id)));

const retried = yield* outbox.retry({
effectId: first.id,
workerId,
error: "OpenCode did not finish generating in time.",
delayMs: 0,
});
assert.isTrue(retried);
assert.equal(
Option.map(yield* outbox.get(first.id), (row) => row.status).pipe(Option.getOrNull),
"cancelled",
);
const pendingContinuation = yield* outbox.get(continuation.id);
assert.isTrue(Option.isSome(pendingContinuation));
if (Option.isSome(pendingContinuation))
assert.equal(pendingContinuation.value.status, "pending");

const resumed = yield* outbox.claimNext({ workerId, lane: "maintenance" });
assert.deepEqual(
Option.map(resumed, (effect) => effect.id),
Option.some(continuation.id),
);
const successor = conversationSummaryEffect({
threadId: threadA,
runId: RunId.make("run:summary-one-pending-a"),
pass: 2,
});
yield* outbox.enqueue([successor]);
assert.deepEqual(yield* outbox.reconcileAfterProcessLoss, { requeued: 0, cancelled: 1 });
assert.equal(
Option.map(yield* outbox.get(continuation.id), (row) => row.status).pipe(Option.getOrNull),
"cancelled",
);
const pendingSuccessor = yield* outbox.get(successor.id);
assert.isTrue(Option.isSome(pendingSuccessor));
if (Option.isSome(pendingSuccessor)) assert.equal(pendingSuccessor.value.status, "pending");

yield* TestClock.adjust(
Duration.sum(EffectOutbox.SETTLED_EFFECT_RETENTION, Duration.days(1)),
);
yield* outbox.pruneSettled;
assert.isTrue(Option.isNone(yield* outbox.get(first.id)));
assert.isTrue(Option.isNone(yield* outbox.get(continuation.id)));
}).pipe(Effect.provide(layer));
}),
);

it.effect("hands a later run's summary pass to a fresh row when the queued pass is retrying", () =>
Effect.gen(function* () {
const threadId = ThreadId.make("thread:summary-retrying");
const first = conversationSummaryEffect({
threadId,
runId: RunId.make("run:summary-retrying-a"),
});
const later = conversationSummaryEffect({
threadId,
runId: RunId.make("run:summary-retrying-b"),
});
const layer = EffectOutbox.layer.pipe(Layer.provide(SqlitePersistence.layerMemory));
yield* Effect.gen(function* () {
const outbox = yield* EffectOutbox.EffectOutboxV2;
const workerId = "summary-retrying";
yield* outbox.enqueue([first]);
yield* outbox.claimNext({ workerId, lane: "maintenance" });
assert.isTrue(
yield* outbox.retry({ effectId: first.id, workerId, error: "Timed out.", delayMs: 0 }),
);

// The retrying row may exhaust its attempts, so it cannot stand in for this run's pass.
yield* outbox.enqueue([later]);
assert.isTrue(Option.isSome(yield* outbox.get(later.id)));

const retried = yield* outbox.claimNext({ workerId, lane: "maintenance" });
assert.deepEqual(
Option.map(retried, (effect) => effect.id),
Option.some(first.id),
);
assert.isTrue(
yield* outbox.retry({ effectId: first.id, workerId, error: "Timed out.", delayMs: 0 }),
);
const next = yield* outbox.claimNext({ workerId, lane: "maintenance" });
assert.deepEqual(
Option.map(next, (effect) => effect.id),
Option.some(later.id),
);
}).pipe(Effect.provide(layer));
}),
);

it.effect.each(["turn", "maintenance"] as const)(
"wakes the %s lane when the other lane is waiting first",
(lane) =>
Expand Down
Loading
Loading