fix(server): skip settlement when PR lookup fails

This commit is contained in:
Theo Browne committed 2026-08-13 16:32:04 -07:00
1 parent a0becb39e3
commit fd5a6b032a
3 files changed
+61 -2

No files matched your search

@@ -2,6 +2,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
import {
ProjectId,
ProviderInstanceId,
SourceControlProviderError,
ThreadId,
TurnId,
type ChangeRequest,
@@ -163,3 +164,54 @@ it.effect(
}),
),
);
it.effect("does not settle a stale thread when its PR state lookup fails", () =>
Effect.scoped(
Effect.gen(function* () {
const thread = makeThread({ id: "lookup-failed", branch: "feature/open-pr" });
const snapshot = {
snapshotSequence: 1,
projects: [project],
threads: [thread],
updatedAt: "2020-01-02T00:00:00.000Z",
} satisfies OrchestrationShellSnapshot;
const dispatched = yield* Ref.make<ReadonlyArray<OrchestrationCommand>>([]);
const provider = {
listChangeRequests: () =>
Effect.fail(
new SourceControlProviderError({
provider: "github",
operation: "listChangeRequests",
cwd: project.workspaceRoot,
detail: "temporary lookup failure",
}),
),
} as unknown as SourceControlProvider.SourceControlProvider["Service"];
const dependencies = Layer.mergeAll(
Layer.succeed(OrchestrationEngineService, {
readEvents: () => Stream.empty,
dispatch: (command) =>
Ref.update(dispatched, (commands) => [...commands, command]).pipe(
Effect.as({ sequence: 1 }),
),
streamDomainEvents: Stream.empty,
latestSequence: Effect.succeed(0),
} satisfies OrchestrationEngineShape),
Layer.succeed(ProjectionSnapshotQuery, {
getShellSnapshot: () => Effect.succeed(snapshot),
} as unknown as ProjectionSnapshotQueryShape),
ServerSettingsService.layerTest(),
Layer.mock(SourceControlProviderRegistry)({
resolve: () => Effect.succeed(provider),
}),
NodeServices.layer,
);
const reactor = yield* makeThreadSettlementReactor.pipe(Effect.provide(dependencies));
yield* reactor.start();
yield* reactor.drain;
expect(yield* Ref.get(dispatched)).toEqual([]);
}),
),
);
@@ -33,6 +33,7 @@ const RECONCILE_INTERVAL = Duration.minutes(1);
const CHANGE_REQUEST_LOOKUP_TTL = Duration.minutes(2);
const CHANGE_REQUEST_LOOKUP_FAILURE_TTL = Duration.seconds(20);
const MAX_BRANCH_LOOKUPS_PER_RECONCILE = 20;
const CHANGE_REQUEST_LOOKUP_FAILED = Symbol("CHANGE_REQUEST_LOOKUP_FAILED");
function workspaceCwd(
thread: Pick<OrchestrationThreadShell, "projectId" | "worktreePath">,
@@ -120,7 +121,7 @@ export const makeThreadSettlementReactor = Effect.gen(function* () {
cwdLength: cwd.length,
branch,
errorTag: error._tag,
}).pipe(Effect.as(null)),
}).pipe(Effect.as(CHANGE_REQUEST_LOOKUP_FAILED)),
),
);
@@ -129,7 +130,7 @@ export const makeThreadSettlementReactor = Effect.gen(function* () {
)(function* (input: {
readonly thread: OrchestrationThreadShell;
readonly cwd: string | undefined;
}): Effect.fn.Return<ChangeRequestState | null> {
}): Effect.fn.Return<ChangeRequestState | null | typeof CHANGE_REQUEST_LOOKUP_FAILED> {
const branch = input.thread.branch;
if (branch === null || input.cwd === undefined) return null;
return yield* lookupBranchChangeRequestState(input.cwd, branch);
@@ -190,6 +191,9 @@ export const makeThreadSettlementReactor = Effect.gen(function* () {
cwd: cwdByThreadId.get(thread.id),
}).pipe(
Effect.flatMap((changeRequestState) => {
// A failed lookup is unknown, not evidence that no PR exists.
// Fail closed so a transient provider outage cannot hide live work.
if (changeRequestState === CHANGE_REQUEST_LOOKUP_FAILED) return Effect.void;
const reason = resolveAutomaticSettlementReason(thread, {
now: nowIso,
autoSettleAfterDays: settings.threadAutoSettleAfterDays,
+3
View File
@@ -200,6 +200,9 @@ export { ThreadEnvMode } from "./environment.ts";
export const MIN_THREAD_AUTO_SETTLE_AFTER_DAYS = 1;
export const MAX_THREAD_AUTO_SETTLE_AFTER_DAYS = 90;
// Replaces the former client-local sidebar setting. Its value is intentionally
// not migrated because different clients can hold conflicting preferences;
// the first server-owned value keeps the prior three-day default.
export const ThreadAutoSettleAfterDays = Schema.Number.check(
Schema.isBetween({
minimum: MIN_THREAD_AUTO_SETTLE_AFTER_DAYS,