Skip to content

Commit 8bb958e

Browse files
committed
fix(responses): bind continuation to route owner
1 parent 03735ec commit 8bb958e

8 files changed

Lines changed: 917 additions & 69 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import type {
2+
OcxProviderContinuationOwner,
3+
OcxReasoningReplayIdentity,
4+
} from "../types";
5+
6+
const bounded = (value: unknown, max: number): value is string =>
7+
typeof value === "string" && value.length > 0 && value.length <= max;
8+
9+
/** Validate proxy-authored continuation ownership before trusting persisted state. */
10+
export function isValidProviderContinuationOwner(
11+
value: unknown,
12+
): value is OcxProviderContinuationOwner {
13+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
14+
const owner = value as Record<string, unknown>;
15+
return owner.version === 1
16+
&& bounded(owner.providerName, 256)
17+
&& typeof owner.providerDestinationIdentity === "string"
18+
&& /^destination:[0-9a-f]{64}$/.test(owner.providerDestinationIdentity)
19+
&& bounded(owner.adapterName, 128)
20+
&& bounded(owner.modelId, 512)
21+
&& typeof owner.credentialIdentity === "string"
22+
&& /^(key|oauth|codex):[0-9a-f]{64}$/.test(owner.credentialIdentity);
23+
}
24+
25+
/** Reuse the exact process-local route identity already established for reasoning replay. */
26+
export function providerContinuationOwnerFromReplayIdentity(
27+
identity: Readonly<OcxReasoningReplayIdentity> | undefined,
28+
): OcxProviderContinuationOwner | undefined {
29+
if (!identity) return undefined;
30+
const owner: OcxProviderContinuationOwner = {
31+
version: 1,
32+
providerName: identity.providerName,
33+
providerDestinationIdentity: identity.providerDestinationIdentity,
34+
adapterName: identity.adapterName,
35+
modelId: identity.modelId,
36+
credentialIdentity: identity.credentialIdentity,
37+
};
38+
return isValidProviderContinuationOwner(owner) ? owner : undefined;
39+
}
40+
41+
export function sameProviderContinuationOwner(
42+
left: OcxProviderContinuationOwner,
43+
right: OcxProviderContinuationOwner,
44+
): boolean {
45+
return left.version === right.version
46+
&& left.providerName === right.providerName
47+
&& left.providerDestinationIdentity === right.providerDestinationIdentity
48+
&& left.adapterName === right.adapterName
49+
&& left.modelId === right.modelId
50+
&& left.credentialIdentity === right.credentialIdentity;
51+
}
52+
53+
/** Cursor hashes this non-secret namespace with the client thread id. */
54+
export function providerContinuationRouteScope(owner: OcxProviderContinuationOwner): string {
55+
return JSON.stringify([
56+
"provider-continuation-v1",
57+
owner.providerName,
58+
owner.providerDestinationIdentity,
59+
owner.adapterName,
60+
owner.modelId,
61+
owner.credentialIdentity,
62+
]);
63+
}

‎src/responses/spill-store.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { createHash, randomBytes } from "node:crypto";
1818
import { join } from "node:path";
1919
import { getConfigDir } from "../config";
2020
import { forgetEphemeralSecretPath, forgetHardenedSecretPath, hardenSecretDir, hardenSecretPath } from "../lib/windows-secret-acl";
21+
import { isValidProviderContinuationOwner } from "./provider-continuation";
2122
import type { OcxProviderContinuationState } from "../types";
2223

2324
export const RESPONSE_SPILL_VERSION = 1;
@@ -287,7 +288,11 @@ function validPayload(value: unknown, responseId: string): value is ResponseSpil
287288
}
288289
if (payload.providers !== undefined) {
289290
if (!payload.providers || typeof payload.providers !== "object" || Array.isArray(payload.providers)) return false;
290-
for (const providerState of Object.values(payload.providers)) {
291+
const providers = payload.providers as Record<string, unknown>;
292+
if (providers.__ocxOwner !== undefined
293+
&& !isValidProviderContinuationOwner(providers.__ocxOwner)) return false;
294+
for (const [provider, providerState] of Object.entries(providers)) {
295+
if (provider === "__ocxOwner") continue;
291296
if (!providerState || typeof providerState !== "object" || Array.isArray(providerState)) return false;
292297
}
293298
}

‎src/responses/state.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,17 @@ export function previousResponseReplayPrefixLength(body: unknown): number {
12161216
return replayedInputPrefixLengths.get(body) ?? 0;
12171217
}
12181218

1219+
/** Copy proxy-private replay provenance to an internal clone with the same materialized input. */
1220+
export function copyPreviousResponseReplayProvenance(source: unknown, target: unknown): void {
1221+
if (!source || typeof source !== "object" || Array.isArray(source)) return;
1222+
if (!target || typeof target !== "object" || Array.isArray(target)) return;
1223+
const prefixLength = replayedInputPrefixLengths.get(source);
1224+
if (!prefixLength) return;
1225+
const input = (target as { input?: unknown }).input;
1226+
if (!Array.isArray(input) || prefixLength > input.length) return;
1227+
replayedInputPrefixLengths.set(target, prefixLength);
1228+
}
1229+
12191230
/** True when a stale or foreign previous_response_id was removed from this exact request body. */
12201231
export function previousResponseScopeMismatch(body: unknown): boolean {
12211232
return !!body && typeof body === "object" && replayScopeMismatches.has(body as object);

0 commit comments

Comments
 (0)