Skip to content

Commit f09d987

Browse files
lidge-junHQ1995claude
committed
fix(devin): retain bounded conversation trajectories
Refines #6488 with credential/host/conversation isolation, active-safe retention and cancellation/retry regression coverage. Co-authored-by: Hanqing Zhao <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent f85925d commit f09d987

12 files changed

Lines changed: 453 additions & 13 deletions

File tree

‎devlog/_plan/261004_cache_identity/020_devin.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ Carry all source runtime and regression behavior, with deliberate replacement of
2121
Run `bun test tests/providers/devin-*.test.ts tests/adapters/adapter-inner-send-budget-wiring.test.ts`, including new tests and existing originals, then typecheck, structure/privacy/layout/ratchet and docs build. Each conditional path above has a fixture that activates it and checks wire UUIDs or claim IDs. Actual native/live cache-hit measurements remain unrun. Publish draft pending broader hosted/native evidence, inspect exact-head checks and repair in-scope defects.
2222

2323
Reflection refinements: parent-only request with populated parsed._clientThreadId must remain unnamed; direct fallback without a parent and each alias are exercised. Double release after reacquisition must not deactivate the new claim (release closure has a released flag). Named reset/signature retry wire requests retain the same trajectory within the live claim.
24+
25+
Implementation record: `trajectory.ts` contains the bounded claim owner; adapter resolves own identity (including existing `_codexOwnThreadId` for internal handoffs) and releases in `finally`; chat serializes the optional #15.1 field. Source regression tests were run before implementation (20 pass, four expected failures) and passed after implementation. Final focused Devin family plus adapter send-budget suite: 444 pass, zero fail across 21 files. Existing reset/signature tests now assert named wire-ID continuity. Typecheck, structure, privacy and 27 layout/ratchet checks passed; docs build passed 561 pages. No full/changed suite, native client or live cache experiment was run.

‎docs-site/src/content/docs/reference/adapters.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,13 @@ configuration that names the old id is rewritten at startup.
577577
- Uses `runTurn` rather than the ordinary fetch/parse path. Requests and server events are encoded
578578
with manual protobuf framing in `devin/cloud-direct/wire.ts`; the ordinary `buildRequest` /
579579
`parseStream` path is disabled.
580+
- Named conversations reuse a trajectory across sequential turns, scoped to the resolved credential
581+
and tenant host. Own-thread identity takes precedence over session markers (`session_id`,
582+
`session-id`, or `x-session-affinity`); a shared parent alone is not a conversation identity.
583+
Overlapping turns receive distinct IDs. The proxy retains at most 256 entries in memory,
584+
evicts only inactive entries, and releases claims after completion, failure, or cancellation.
585+
Restarting the proxy clears retention. Unnamed calls continue allocating an ID per request.
586+
This supports continuity but does not guarantee an upstream cache hit or a particular saving.
580587
- Reasoning continuity carries provider signatures across turns. If Cognition refuses a signed
581588
Anthropic replay before visible output, Devin retries once with the signature withheld and the
582589
thinking text preserved.

‎scripts/test-layout/layout.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,7 @@
828828
"devin-quota.test.ts": "providers",
829829
"devin-output-budget.test.ts": "providers",
830830
"devin-prompt-cache.test.ts": "providers",
831+
"devin-trajectory.test.ts": "providers",
831832
"devin-provider-merge-migration.test.ts": "providers",
832833
"devin-stated-reset-hardening.test.ts": "providers",
833834
"devin-stated-reset-retry.test.ts": "providers",

‎src/adapters/devin.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { DEVIN_DEFAULT_API_SERVER, resolveDevinApiServer } from "../oauth/devin"
1818
import { devinAssistantReasoning, encodeDevinSignature, hasAnthropicSignature } from "./devin/reasoning-signature";
1919
import { SendBudgetExhaustedError } from "../lib/upstream-retry";
2020
import { devinContextOverflowEvent, isDevinHistoryOverflow } from "./devin/context-overflow";
21+
import { claimDevinTrajectory, type DevinTrajectoryClaim } from "./devin/trajectory";
2122

2223
/**
2324
* Combine two usage frames from one turn by keeping the larger count per field.
@@ -743,6 +744,7 @@ export function createDevinAdapter(
743744
let contextWindow: number | undefined;
744745
let messages: ChatHistoryItem[] = [];
745746
let tools: ToolDef[] | undefined;
747+
let trajectory: DevinTrajectoryClaim | undefined;
746748

747749
const closeOpenTool = () => {
748750
if (!openToolId) return;
@@ -751,6 +753,12 @@ export function createDevinAdapter(
751753
};
752754

753755
try {
756+
const conversation = incoming.headers.get("thread-id")?.trim() || parsed._codexOwnThreadId?.trim()
757+
|| incoming.headers.get("session_id")?.trim() || incoming.headers.get("session-id")?.trim()
758+
|| incoming.headers.get("x-session-affinity")?.trim()
759+
|| (!incoming.headers.has("x-codex-parent-thread-id") ? parsed._clientThreadId : undefined);
760+
trajectory = claimDevinTrajectory(apiKey, host, conversation);
761+
const trajectoryId = trajectory.trajectoryId;
754762
// Read the selected UID's catalog row, not the picker's collapsed base.
755763
contextWindow = resolveDevinContextWindow(provider, modelUid, catalog?.byUid.get(modelUid));
756764
messages = mapOcxMessagesToDevin(parsed);
@@ -780,6 +788,7 @@ export function createDevinAdapter(
780788
messages,
781789
tools,
782790
cascadeId,
791+
trajectoryId,
783792
completionOpts: {
784793
...(maxOutputTokens !== undefined ? { maxOutputTokens } : {}),
785794
...(typeof parsed.options.temperature === "number" ? { temperature: parsed.options.temperature } : {}),
@@ -961,6 +970,8 @@ export function createDevinAdapter(
961970
...(error instanceof CloudChatError && error.code ? { code: error.code } : {}),
962971
...(usage ? { usage } : {}),
963972
});
973+
} finally {
974+
trajectory?.release();
964975
}
965976
},
966977
};

‎src/adapters/devin/cloud-direct/chat.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,8 @@ export type CloudChatEvent =
536536

537537
interface BuildArgs {
538538
apiKey: string;
539+
/** CortexTrajectoryReference #15 id; minted per request when absent. */
540+
trajectoryId?: string;
539541
userJwt?: string;
540542
modelUid: string;
541543
messages: ChatHistoryItem[];
@@ -779,9 +781,9 @@ function buildGetChatMessageRequest(args: BuildArgs): Buffer {
779781
// native client and CLIProxyAPIPlus, which places it outside its tools gate.
780782
encodeMessage(13, encodeVarintField(1, PROMPT_CACHE_EPHEMERAL)),
781783
// #15 CortexTrajectoryReference: { id, 1, 4 }. Present on every verified
782-
// request.
784+
// request. A supplied id retains continuity across sequential named turns.
783785
encodeMessage(15, Buffer.concat([
784-
encodeString(1, crypto.randomUUID()),
786+
encodeString(1, args.trajectoryId ?? crypto.randomUUID()),
785787
encodeVarintField(2, 1),
786788
encodeVarintField(3, 4),
787789
])),
@@ -1105,6 +1107,8 @@ export interface CloudChatRequest {
11051107
tools?: ToolDef[];
11061108
/** Cascade ID — reuse across turns of the same conversation. */
11071109
cascadeId?: string;
1110+
/** Trajectory ID (#15) — reuse across sequential turns of one conversation. */
1111+
trajectoryId?: string;
11081112
/** Optional sampling overrides. */
11091113
completionOpts?: BuildArgs['completionOpts'];
11101114
/** Override request_type (default = 5, CASCADE). */
@@ -1255,6 +1259,7 @@ export async function* streamChatEvents(req: CloudChatRequest): AsyncGenerator<C
12551259
tools: req.tools,
12561260
cascadeId: sessionIds.cascadeId,
12571261
sessionId: sessionIds.sessionId,
1262+
trajectoryId: req.trajectoryId,
12581263
requestId: BigInt(Date.now()),
12591264
triggerId: crypto.randomUUID(),
12601265
requestType: req.requestType,

‎src/adapters/devin/trajectory.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { devinCacheIdentity } from "./cloud-direct/chat";
2+
3+
const TRAJECTORY_MAX = 256;
4+
const trajectories = new Map<string, { id: string; active: boolean }>();
5+
6+
export interface DevinTrajectoryClaim {
7+
trajectoryId?: string;
8+
release(): void;
9+
}
10+
11+
/** One process-local trajectory per named conversation, never shared by live turns. */
12+
export function claimDevinTrajectory(
13+
apiKey: string,
14+
host: string,
15+
conversation: string | null | undefined,
16+
): DevinTrajectoryClaim {
17+
if (!conversation) return { release() {} };
18+
// Structured framing separates host and conversation even if either contains a delimiter.
19+
// Only the fixed digest and UUID enter the retained map, never the token or conversation.
20+
const key = new Bun.CryptoHasher("sha256")
21+
.update(JSON.stringify([devinCacheIdentity(apiKey, host), conversation])).digest("hex");
22+
let entry = trajectories.get(key);
23+
if (entry?.active) return { trajectoryId: crypto.randomUUID(), release() {} };
24+
if (!entry) {
25+
if (trajectories.size >= TRAJECTORY_MAX) {
26+
const inactive = [...trajectories].find(([, candidate]) => !candidate.active);
27+
if (!inactive) return { trajectoryId: crypto.randomUUID(), release() {} };
28+
trajectories.delete(inactive[0]);
29+
}
30+
entry = { id: crypto.randomUUID(), active: true };
31+
}
32+
entry.active = true;
33+
trajectories.delete(key);
34+
trajectories.set(key, entry);
35+
const claimed = entry;
36+
let released = false;
37+
return {
38+
trajectoryId: claimed.id,
39+
release() {
40+
if (released) return;
41+
released = true;
42+
claimed.active = false;
43+
},
44+
};
45+
}

‎structure/providers-and-adapters.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ rewrite rules and the routed-id settlement.
196196
| `src/adapters/image.ts`, `src/adapters/anthropic-image-guard.ts`, `src/adapters/anthropic-image-normalize.ts`, `src/adapters/anthropic-image-codec.ts` | Image conversion for adapter ingress and Anthropic-specific normalization/limits. An image's ladder position is pinned to its own identity (content hash + media type), so appending a newer image cannot re-encode older ones and bust Anthropic's prompt prefix cache (#4532). |
197197
| `src/adapters/run-turn-queue.ts`, `src/adapters/tool-catalog-nudge.ts`, `src/adapters/identity.ts`, `src/adapters/upstream-http-error.ts` | Shared adapter execution support: turn queueing, tool-catalog nudging, client identity, upstream error normalization. |
198198

199+
`src/adapters/devin/trajectory.ts` owns up to 256 process-local conversation trajectories, keyed by a digest of resolved credential identity, tenant host and named conversation. Own-thread identity takes precedence over session aliases; a parent-only request does not borrow its parent's trajectory. Sequential turns reuse an inactive entry across request-scoped adapters; overlapping turns and a store full of active entries receive fresh unretained IDs. Only inactive entries are evicted, and a claim's idempotent release runs in the adapter's `finally` on success, error and cancellation. The optional trajectory travels through `src/adapters/devin/cloud-direct/chat.ts` to protobuf field #15.1; unnamed calls retain per-request allocation. A named claim remains held through the turn's existing reset/signature retries. This is identifier continuity, not proof of upstream cache hits.
200+
199201
Devin pairs a late signature only with immediately preceding unsigned thinking in one assistant message; a call between them breaks the pair. While a signed attempt is held for an optional unsigned retry, a timer emits plain heartbeats even when the upstream stalls. The held queue is capped at 1,024 events or approximately 1 MiB of UTF-16 reasoning, signature, and signature-type payload; signature types are individually capped at 4 KiB before UTF-8 decoding, and crossing either queue cap releases the events and disables that retry. Held usage frames merge per cumulative field, and the refused attempt's usage is added to the retry. A signed `invalid_argument` refusal is offered the unsigned retry before the history-overflow classifier sees any final refusal. If the send budget withholds that retry, the original refusal reaches the classifier and the recovery is recorded as withheld.
200202

201203
Devin family selection in `src/adapters/devin/live-models.ts` counts the anchor's nonzero targets once and visits each candidate's own axes. Missing axes have order zero; extra nonzero axes, including prototype names such as `toString`, count as mismatches. Effort precedence and family confinement remain unchanged; `tests/providers/devin-family-resolution.test.ts` covers the sparse-scoring contract.

‎tests/fixtures/test-layout-expected.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@
831831
"devin-quota.test.ts": "providers",
832832
"devin-output-budget.test.ts": "providers",
833833
"devin-prompt-cache.test.ts": "providers",
834+
"devin-trajectory.test.ts": "providers",
834835
"devin-provider-merge-migration.test.ts": "providers",
835836
"devin-stated-reset-hardening.test.ts": "providers",
836837
"devin-stated-reset-retry.test.ts": "providers",

‎tests/providers/devin-adapter-reset-wait.test.ts‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { tmpdir } from "node:os";
2020
import { join } from "node:path";
2121
import { createDevinAdapter } from "../../src/adapters/devin";
2222
import { parseCatalogBuffer, setCachedCatalogForTests } from "../../src/adapters/devin/cloud-direct/catalog";
23-
import { encodeMessage, encodeString, encodeVarintField } from "../../src/adapters/devin/cloud-direct/wire";
23+
import { encodeMessage, encodeString, encodeVarintField, iterFields } from "../../src/adapters/devin/cloud-direct/wire";
2424
import { createTranslatorBudget } from "../../src/lib/translator-budget";
2525
import { saveCredential } from "../../src/oauth/store";
2626
import type { AdapterEvent } from "../../src/types";
@@ -35,6 +35,7 @@ const previousFetch = globalThis.fetch;
3535
const previousWait = process.env.OPENCODEX_DEVIN_STATED_RESET_WAIT_MS;
3636
let chatPosts = 0;
3737
let seenUrls: string[] = [];
38+
let trajectories: string[] = [];
3839

3940
function seed(tenantHost = host): void {
4041
const buffer = Buffer.concat([encodeMessage(1, Buffer.concat([
@@ -65,16 +66,22 @@ function refusalResponse(message: string): Response {
6566
function stubTransport(message: string): void {
6667
chatPosts = 0;
6768
seenUrls = [];
68-
globalThis.fetch = (async (input: RequestInfo | URL) => {
69+
trajectories = [];
70+
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
6971
// The seeded catalog cache means GetChatMessage is the only RPC this turn
7072
// performs; count it anyway so a replay is visible as a second send.
7173
seenUrls.push(String(input));
72-
if (String(input).endsWith(new URL(CHAT_URL).pathname)) chatPosts += 1;
74+
if (String(input).endsWith(new URL(CHAT_URL).pathname)) {
75+
chatPosts += 1;
76+
const request = Buffer.from(await (init!.body as Blob).arrayBuffer()).subarray(5);
77+
const reference = [...iterFields(request)].find(field => field.num === 15)!.value as Buffer;
78+
trajectories.push(([...iterFields(reference)].find(field => field.num === 1)!.value as Buffer).toString());
79+
}
7380
return refusalResponse(message);
7481
}) as typeof fetch;
7582
}
7683

77-
async function runOneTurn(abortSignal: AbortSignal = AbortSignal.timeout(5_000), comboAttempt = false): Promise<AdapterEvent[]> {
84+
async function runOneTurn(abortSignal: AbortSignal = AbortSignal.timeout(5_000), comboAttempt = false, headers = new Headers()): Promise<AdapterEvent[]> {
7885
const adapter = createDevinAdapter({ adapter: "devin", apiKey, baseUrl: host });
7986
const events: AdapterEvent[] = [];
8087
await adapter.runTurn!({
@@ -83,7 +90,7 @@ async function runOneTurn(abortSignal: AbortSignal = AbortSignal.timeout(5_000),
8390
context: { messages: [{ role: "user", content: "hi", timestamp: 1 }] },
8491
options: {},
8592
}, {
86-
headers: new Headers(),
93+
headers,
8794
translatorBudget: createTranslatorBudget(),
8895
abortSignal,
8996
comboAttempt,
@@ -137,14 +144,21 @@ describe("devin adapter stated-reset wait", () => {
137144
process.env.OPENCODEX_DEVIN_STATED_RESET_WAIT_MS = "3000";
138145
stubTransport("Your limit will reset in 1 second");
139146

140-
const events = await runOneTurn(AbortSignal.timeout(30_000));
147+
const headers = new Headers({ "session-id": crypto.randomUUID() });
148+
const events = await runOneTurn(AbortSignal.timeout(5_000), false, headers);
141149

142150
expect(chatPosts).toBe(3);
143151
expect(events.some(event => event.type === "heartbeat" && event.preflightReady === true)).toBe(true);
144152
const error = events.find((event): event is Extract<AdapterEvent, { type: "error" }> => event.type === "error");
145153
expect(error).toMatchObject({ status: 429, errorType: "rate_limit_error", code: "resource_exhausted" });
146154
expect(error?.message).toContain("retry after ~1s");
147155
expect(events.some(event => event.type === "done")).toBe(false);
156+
expect(trajectories).toHaveLength(3);
157+
expect(new Set(trajectories).size).toBe(1);
158+
// Once retries finish, the next named turn reacquires the same retained id.
159+
await runOneTurn(AbortSignal.timeout(3_000), true, headers);
160+
expect(trajectories).toHaveLength(4);
161+
expect(trajectories[3]).toBe(trajectories[0]);
148162
});
149163

150164
test("a combo child returns the stated reset immediately despite a standalone wait allowance", async () => {

‎tests/providers/devin-anthropic-signature-fallback.test.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe("Devin Anthropic signature fallback", () => {
4444
return { thinking: byNum.get(11), signature: byNum.get(12) };
4545
}
4646

47-
async function run(signature: string, modelId: string, observed?: AdapterEvent[], userText = "go", meta: Pick<IncomingMeta, "sendBudget" | "onRecoveryWithheld"> = {}): Promise<AdapterEvent[]> {
47+
async function run(signature: string, modelId: string, observed?: AdapterEvent[], userText = "go", meta: Partial<Pick<IncomingMeta, "sendBudget" | "onRecoveryWithheld" | "headers" | "abortSignal">> = {}): Promise<AdapterEvent[]> {
4848
const parsed = parseRequest({
4949
model: `devin/${modelId}`,
5050
input: [
@@ -101,12 +101,22 @@ describe("Devin Anthropic signature fallback", () => {
101101

102102
test("a refused signed Claude turn is retried once with the signature withheld", async () => {
103103
responses = ["refuse", "ok"];
104-
const events = await run(encodeDevinSignature("EpcBClaude", "anthropic"), "claude-opus-5-5-medium");
104+
const headers = new Headers({ "thread-id": crypto.randomUUID() });
105+
const signature = encodeDevinSignature("EpcBClaude", "anthropic");
106+
const events = await run(signature, "claude-opus-5-5-medium", undefined, "go", { headers, abortSignal: AbortSignal.timeout(3_000) });
105107
expect(requests).toHaveLength(2);
106108
expect(assistantSignature(requests[0]!)).toEqual({ thinking: "summarised thought", signature: "EpcBClaude" });
107109
expect(assistantSignature(requests[1]!)).toEqual({ thinking: "summarised thought", signature: undefined });
108110
expect(events.some(e => e.type === "error")).toBe(false);
109111
expect(events).toContainEqual({ type: "text_delta", text: "ok" });
112+
const trajectoryOf = (request: Buffer) => {
113+
const reference = [...iterFields(request)].find(field => field.num === 15)!.value as Buffer;
114+
return ([...iterFields(reference)].find(field => field.num === 1)!.value as Buffer).toString();
115+
};
116+
expect(trajectoryOf(requests[1]!)).toBe(trajectoryOf(requests[0]!));
117+
await run(signature, "claude-opus-5-5-medium", undefined, "go", { headers, abortSignal: AbortSignal.timeout(3_000) });
118+
expect(requests).toHaveLength(3);
119+
expect(trajectoryOf(requests[2]!)).toBe(trajectoryOf(requests[0]!));
110120
});
111121

112122
test("a signed refusal at 95% of the catalog window retries unsigned before overflow classification", async () => {

0 commit comments

Comments
 (0)