Skip to content

Commit da53f0f

Browse files
github-actions[bot]devin-ai-integration[bot]
authored andcommitted
fix(server): retry devin session/load while the session lock is held
Production swe-2 threads showed 5 'session already open in another process' failures when a second devin acp process resumed a session still held by another process. Devin marks the error session_locked + retryable, so the ACP runtime now supports a bounded sessionLoadRetry (3 retries, 1.5s apart, gated on a per-provider predicate) and Devin opts in for session-locked failures only. Exhausted retries surface a clean actionable error instead of the raw JSON-RPC message. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent f27bafe commit da53f0f

6 files changed

Lines changed: 225 additions & 20 deletions

File tree

‎apps/server/scripts/acp-mock-agent.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const emitLateUpdateAfterCancel = process.env.T3_ACP_EMIT_LATE_UPDATE_AFTER_CANC
4646
const omitXAiPromptCompleteStopReason =
4747
process.env.T3_ACP_OMIT_XAI_PROMPT_COMPLETE_STOP_REASON === "1";
4848
const failLoadSession = process.env.T3_ACP_FAIL_LOAD_SESSION === "1";
49+
const busyLoadSessionCount = Number(process.env.T3_ACP_BUSY_LOAD_SESSIONS ?? "0");
4950
const emitLoadReplay = process.env.T3_ACP_EMIT_LOAD_REPLAY === "1";
5051
const hangLoadSessionAfterReplay = process.env.T3_ACP_HANG_LOAD_SESSION_AFTER_REPLAY === "1";
5152
const delayLoadSessionAfterReplay = process.env.T3_ACP_DELAY_LOAD_SESSION_AFTER_REPLAY === "1";
@@ -90,6 +91,9 @@ let currentReasoning = "medium";
9091
let currentContext = "272k";
9192
let currentFast = false;
9293
let promptCount = 0;
94+
let busyLoadSessionsRemaining = Number.isFinite(busyLoadSessionCount)
95+
? Math.max(0, Math.floor(busyLoadSessionCount))
96+
: 0;
9397
let overlappingFirstPromptId: string | undefined;
9498
const cancelledSessions = new Set<string>();
9599

@@ -563,6 +567,17 @@ const program = Effect.gen(function* () {
563567
yield* agent.handleLoadSession((request) =>
564568
Effect.gen(function* () {
565569
const requestedSessionId = String(request.sessionId ?? sessionId);
570+
if (busyLoadSessionsRemaining > 0) {
571+
busyLoadSessionsRemaining -= 1;
572+
return yield* new AcpError.AcpRequestError({
573+
code: -32015,
574+
errorMessage: `Session '${requestedSessionId}' is already open in another process. Close the other instance before opening it here.`,
575+
data: {
576+
"cognition.ai/errorKind": "session_locked",
577+
"cognition.ai/retryable": true,
578+
},
579+
});
580+
}
566581
if (failLoadSession) {
567582
return yield* AcpError.AcpRequestError.internalError("Mock load session failure");
568583
}

‎apps/server/src/provider/Layers/DevinAdapter.test.ts‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import * as NodeURL from "node:url";
66

77
import * as NodeServices from "@effect/platform-node/NodeServices";
88
import { assert, it } from "@effect/vitest";
9+
import * as Cause from "effect/Cause";
910
import * as Crypto from "effect/Crypto";
1011
import * as Deferred from "effect/Deferred";
1112
import * as Effect from "effect/Effect";
13+
import * as Exit from "effect/Exit";
1214
import * as Fiber from "effect/Fiber";
1315
import * as FileSystem from "effect/FileSystem";
1416
import * as Layer from "effect/Layer";
17+
import * as Option from "effect/Option";
1518
import * as Schema from "effect/Schema";
1619
import * as Stream from "effect/Stream";
1720
import { ChildProcessSpawner } from "effect/unstable/process";
@@ -529,3 +532,93 @@ it.layer(devinAdapterTestLayer)("DevinAdapterLive", (it) => {
529532
}),
530533
);
531534
});
535+
536+
// `it.live` rather than the layer tester: the session-load retry schedule
537+
// sleeps on the real clock, which `it.effect`'s TestClock never advances.
538+
it.live("retries session/load while Devin reports the session locked", () =>
539+
Effect.gen(function* () {
540+
const threadId = ThreadId.make("devin-locked-resume-thread");
541+
const tempDir = yield* Effect.promise(() =>
542+
NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "devin-locked-resume-")),
543+
);
544+
const requestLogPath = NodePath.join(tempDir, "requests.ndjson");
545+
const wrapperPath = yield* Effect.promise(() =>
546+
makeMockDevinWrapper({
547+
T3_ACP_REQUEST_LOG_PATH: requestLogPath,
548+
T3_ACP_BUSY_LOAD_SESSIONS: "2",
549+
}),
550+
);
551+
const adapter = yield* makeTestAdapter(wrapperPath, {
552+
...process.env,
553+
WINDSURF_API_KEY: "ws-test",
554+
});
555+
556+
const session = yield* adapter.startSession({
557+
threadId,
558+
cwd: process.cwd(),
559+
runtimeMode: "auto",
560+
resumeCursor: { schemaVersion: 1, sessionId: "locked-devin-session" },
561+
});
562+
assert.equal(session.provider, "devin");
563+
assert.deepStrictEqual(session.resumeCursor, {
564+
schemaVersion: 1,
565+
sessionId: "locked-devin-session",
566+
});
567+
yield* adapter.stopSession(threadId);
568+
569+
const requests = yield* Effect.promise(() => readJsonLines(requestLogPath));
570+
assert.lengthOf(
571+
requests.filter((request) => request.method === "session/load"),
572+
3,
573+
"two locked attempts then the successful load",
574+
);
575+
}).pipe(Effect.provide(devinAdapterTestLayer)),
576+
);
577+
578+
it.live("maps an exhausted session lock to an actionable error", () =>
579+
Effect.gen(function* () {
580+
const threadId = ThreadId.make("devin-stuck-locked-resume-thread");
581+
const tempDir = yield* Effect.promise(() =>
582+
NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "devin-stuck-locked-")),
583+
);
584+
const requestLogPath = NodePath.join(tempDir, "requests.ndjson");
585+
const wrapperPath = yield* Effect.promise(() =>
586+
makeMockDevinWrapper({
587+
T3_ACP_REQUEST_LOG_PATH: requestLogPath,
588+
T3_ACP_BUSY_LOAD_SESSIONS: "9",
589+
}),
590+
);
591+
const adapter = yield* makeTestAdapter(wrapperPath, {
592+
...process.env,
593+
WINDSURF_API_KEY: "ws-test",
594+
});
595+
596+
const exit = yield* adapter
597+
.startSession({
598+
threadId,
599+
cwd: process.cwd(),
600+
runtimeMode: "auto",
601+
resumeCursor: { schemaVersion: 1, sessionId: "stuck-locked-session" },
602+
})
603+
.pipe(Effect.exit);
604+
assert.isTrue(Exit.isFailure(exit));
605+
if (Exit.isFailure(exit)) {
606+
const failure = Cause.findErrorOption(exit.cause);
607+
assert.isTrue(Option.isSome(failure));
608+
if (Option.isSome(failure)) {
609+
const error = failure.value;
610+
assert.equal(error._tag, "ProviderAdapterRequestError");
611+
if (error._tag === "ProviderAdapterRequestError") {
612+
assert.match(String(error.detail), /still open in another process/i);
613+
}
614+
}
615+
}
616+
617+
const requests = yield* Effect.promise(() => readJsonLines(requestLogPath));
618+
assert.lengthOf(
619+
requests.filter((request) => request.method === "session/load"),
620+
4,
621+
"initial attempt plus three retries before surfacing the lock error",
622+
);
623+
}).pipe(Effect.provide(devinAdapterTestLayer)),
624+
);

‎apps/server/src/provider/Layers/DevinAdapter.ts‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import {
5959
currentDevinModelIdFromConfigOptions,
6060
devinModelOptionValues,
6161
findDevinModelConfigOption,
62+
isDevinSessionLockedError,
6263
resolveDevinModeId,
6364
resolveDevinModelId,
6465
type DevinAcpRuntimeInput,
@@ -107,14 +108,24 @@ function isDevinSignInRequiredError(cause: unknown): boolean {
107108
}
108109

109110
function mapDevinError(threadId: ThreadId, method: string, cause: EffectAcpErrors.AcpError) {
110-
return isDevinSignInRequiredError(cause)
111-
? new ProviderAdapterRequestError({
112-
provider: PROVIDER,
113-
method,
114-
detail: DEVIN_SIGN_IN_REQUIRED_MESSAGE,
115-
cause,
116-
})
117-
: mapAcpToAdapterError(PROVIDER, threadId, method, cause);
111+
if (isDevinSignInRequiredError(cause)) {
112+
return new ProviderAdapterRequestError({
113+
provider: PROVIDER,
114+
method,
115+
detail: DEVIN_SIGN_IN_REQUIRED_MESSAGE,
116+
cause,
117+
});
118+
}
119+
if (cause._tag === "AcpRequestError" && isDevinSessionLockedError(cause)) {
120+
return new ProviderAdapterRequestError({
121+
provider: PROVIDER,
122+
method,
123+
detail:
124+
"This Devin session is still open in another process. Close the other T3 Code window or `devin acp` session holding it, then retry.",
125+
cause,
126+
});
127+
}
128+
return mapAcpToAdapterError(PROVIDER, threadId, method, cause);
118129
}
119130

120131
function isRecord(value: unknown): value is Record<string, unknown> {

‎apps/server/src/provider/acp/AcpSessionRuntime.ts‎

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as Layer from "effect/Layer";
1111
import * as Option from "effect/Option";
1212
import * as Queue from "effect/Queue";
1313
import * as Ref from "effect/Ref";
14+
import * as Schedule from "effect/Schedule";
1415
import * as Scope from "effect/Scope";
1516
import * as Semaphore from "effect/Semaphore";
1617
import * as Stream from "effect/Stream";
@@ -84,6 +85,17 @@ export interface AcpSessionRuntimeOptions {
8485
readonly resumeMethod?: "load" | "resume";
8586
readonly sessionLoadTimeout?: Duration.Input;
8687
readonly sessionLoadReplayIdleGap?: Duration.Input;
88+
/**
89+
* Retries `session/load` when `while` accepts the failure — e.g. an agent
90+
* reporting the session is still locked by another process but retryable.
91+
* Required so a caller cannot accidentally retry auth or invalid-params
92+
* failures.
93+
*/
94+
readonly sessionLoadRetry?: {
95+
readonly retries: number;
96+
readonly delay: Duration.Input;
97+
readonly while: (error: EffectAcpErrors.AcpRequestError) => boolean;
98+
};
8799
/** Native cancellation waits for the prompt response and the getEvents consumer to drain. */
88100
readonly cancelBehavior?: "interrupt" | "wait-for-prompt";
89101
readonly cancelTimeout?: Duration.Input;
@@ -771,18 +783,21 @@ export const make = (
771783
options.sessionLoadReplayIdleGap ?? defaultSessionLoadReplayIdleGap,
772784
);
773785

774-
yield* Ref.set(
775-
sessionLoadGateRef,
776-
Option.some({
777-
active: true,
778-
lastActivityAtMillis: undefined,
779-
idleGap: sessionLoadReplayIdleGap,
780-
initializeResult,
781-
}),
782-
);
783-
784786
sessionId = options.resumeSessionId;
785-
sessionSetupResult = yield* Effect.gen(function* () {
787+
const loadRetry = options.sessionLoadRetry;
788+
const loadSession = Effect.gen(function* () {
789+
// Re-armed per attempt so a stale replay timestamp from a failed
790+
// load cannot make a retried attempt resolve as `replay_idle`
791+
// before the real response arrives.
792+
yield* Ref.set(
793+
sessionLoadGateRef,
794+
Option.some({
795+
active: true,
796+
lastActivityAtMillis: undefined,
797+
idleGap: sessionLoadReplayIdleGap,
798+
initializeResult,
799+
}),
800+
);
786801
yield* logRequest({
787802
method: "session/load",
788803
payload: loadPayload,
@@ -831,7 +846,19 @@ export const make = (
831846
);
832847

833848
return loaded;
834-
}).pipe(Effect.ensuring(Ref.set(sessionLoadGateRef, Option.none())));
849+
});
850+
sessionSetupResult = yield* (
851+
loadRetry === undefined
852+
? loadSession
853+
: loadSession.pipe(
854+
Effect.retry({
855+
while: (error) => error._tag === "AcpRequestError" && loadRetry.while(error),
856+
schedule: Schedule.recurs(loadRetry.retries).pipe(
857+
Schedule.addDelay(() => Effect.succeed(loadRetry.delay)),
858+
),
859+
}),
860+
)
861+
).pipe(Effect.ensuring(Ref.set(sessionLoadGateRef, Option.none())));
835862
} else {
836863
const createPayload = {
837864
cwd: options.cwd,

‎apps/server/src/provider/acp/DevinAcpSupport.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
66
import { assert, it } from "@effect/vitest";
77
import * as Effect from "effect/Effect";
88
import * as FileSystem from "effect/FileSystem";
9+
import * as EffectAcpErrors from "effect-acp/errors";
910
import type * as EffectAcpSchema from "effect-acp/schema";
1011

1112
import type { AcpSessionModeState } from "./AcpRuntimeModel.ts";
@@ -18,6 +19,7 @@ import {
1819
devinCredentialsFilePaths,
1920
devinHasAmbientCredentials,
2021
devinModeFor,
22+
isDevinSessionLockedError,
2123
resolveDevinAuthMethodId,
2224
resolveDevinModeId,
2325
resolveDevinModelId,
@@ -37,6 +39,39 @@ const DEVIN_MODES = modeState([
3739
]);
3840

3941
it.layer(NodeServices.layer)("DevinAcpSupport", (it) => {
42+
it("recognizes Devin's retryable session-locked error", () => {
43+
assert.isTrue(
44+
isDevinSessionLockedError(
45+
new EffectAcpErrors.AcpRequestError({
46+
code: -32015,
47+
errorMessage:
48+
"Session 'tattered-ornament' is already open in another process. Close the other instance before opening it here.",
49+
data: {
50+
"cognition.ai/errorKind": "session_locked",
51+
"cognition.ai/retryable": true,
52+
},
53+
}),
54+
),
55+
);
56+
// Message-only fallback for builds that drop the structured data.
57+
assert.isTrue(
58+
isDevinSessionLockedError(
59+
new EffectAcpErrors.AcpRequestError({
60+
code: -32603,
61+
errorMessage: "Session 'x' is already open in another process.",
62+
}),
63+
),
64+
);
65+
assert.isFalse(
66+
isDevinSessionLockedError(
67+
new EffectAcpErrors.AcpRequestError({
68+
code: -32603,
69+
errorMessage: "Reached overall message rate limit. Please try again later.",
70+
}),
71+
),
72+
);
73+
});
74+
4075
it("maps T3 runtime and interaction modes onto Devin session modes", () => {
4176
assert.equal(devinModeFor("full-access", undefined), "bypass");
4277
assert.equal(devinModeFor("auto", undefined), "smart");

‎apps/server/src/provider/acp/DevinAcpSupport.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@ export const resolveDevinAuthMethodId = (input: {
102102
hasCredentials || !input.browserAuth ? undefined : DEVIN_BROWSER_AUTH_METHOD,
103103
);
104104

105+
/**
106+
* Whether a `session/load` failure is Devin's "session already open in another
107+
* process" lock conflict. Devin tags it `session_locked` + `retryable: true` in
108+
* the error data; the message match is a fallback for builds that drop `data`.
109+
*/
110+
export function isDevinSessionLockedError(error: EffectAcpErrors.AcpRequestError): boolean {
111+
const data = error.data;
112+
if (
113+
typeof data === "object" &&
114+
data !== null &&
115+
(data as Record<string, unknown>)["cognition.ai/errorKind"] === "session_locked"
116+
) {
117+
return true;
118+
}
119+
return /already open in another process/i.test(error.errorMessage);
120+
}
121+
105122
export function buildDevinAcpSpawnInput(
106123
devinSettings: DevinAcpRuntimeDevinSettings | null | undefined,
107124
cwd: string,
@@ -155,6 +172,13 @@ export const makeDevinAcpRuntime = (
155172
terminal: false,
156173
},
157174
cancelBehavior: "wait-for-prompt",
175+
sessionLoadRetry: {
176+
// The session lock releases as soon as the other `devin acp` process
177+
// exits, so short retries absorb the common crash/teardown overlap.
178+
retries: 3,
179+
delay: "1.5 seconds",
180+
while: isDevinSessionLockedError,
181+
},
158182
}).pipe(
159183
Layer.provide(
160184
Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, input.childProcessSpawner),

0 commit comments

Comments
 (0)