mirror of
https://bitbucket.org/siakitem/my-pi.git
synced 2026-10-26 10:45:22 +00:00
feat(compaction): support SubAPI native remote compaction
- support sub-api/openai-responses through the public Responses endpoint - preserve explicit Authorization and support header-only authentication - retain Codex JWT requirements and native compaction failure fallback - restore SubAPI checkpoints and preserve portable model-switching summaries - add provider, authentication and checkpoint lifecycle regression tests - document compatibility requirements and the Sub2API protocol reference
This commit is contained in:
@@ -101,3 +101,205 @@ test("model switching after a native checkpoint projects the portable checkpoint
|
||||
assert.equal(projected.messages[1].content, "continue");
|
||||
await h.handlers.get("session_shutdown")![0]!({}, ctx);
|
||||
});
|
||||
|
||||
for (const provider of ["sub-api", "openai-codex"]) {
|
||||
test(`${provider} native compaction uses the selected model and restores its checkpoint`, async (t) => {
|
||||
const h = harness();
|
||||
piCompaction(h.pi);
|
||||
const branch: any[] = [{
|
||||
type: "message", id: "u1", parentId: null,
|
||||
message: { role: "user", content: "Remember the test decision.", timestamp: 1 },
|
||||
}];
|
||||
const ctx = context(branch);
|
||||
ctx.model = {
|
||||
provider, api: provider === "sub-api" ? "openai-responses" : "openai-codex-responses",
|
||||
id: "selected-model", input: ["text"], contextWindow: 100_000,
|
||||
baseUrl: provider === "sub-api" ? "https://gateway.example/v1" : "https://codex.example/codex",
|
||||
};
|
||||
const jwt = Buffer.from(JSON.stringify({
|
||||
"https://api.openai.com/auth": { chatgpt_account_id: "test-account" },
|
||||
})).toString("base64url");
|
||||
const apiKey = provider === "sub-api" ? "ordinary-test-key" : `header.${jwt}.signature`;
|
||||
ctx.modelRegistry.getApiKeyAndHeaders = async (model: unknown) => {
|
||||
assert.equal(model, ctx.model);
|
||||
return { ok: true, apiKey, headers: { "x-test-route": "preserved" } };
|
||||
};
|
||||
const requests: Array<{ url: string; headers: Headers; body: any }> = [];
|
||||
t.mock.method(globalThis, "fetch", async (url: string, init: RequestInit) => {
|
||||
requests.push({ url: String(url), headers: new Headers(init.headers), body: JSON.parse(String(init.body)) });
|
||||
const events = [
|
||||
{ type: "response.output_item.done", item: { type: "compaction", encrypted_content: "opaque-checkpoint" } },
|
||||
{ type: "response.completed", response: { usage: { input_tokens: 12, output_tokens: 3 } } },
|
||||
];
|
||||
return new Response(events.map((event) => `data: ${JSON.stringify(event)}\n\n`).join(""), {
|
||||
headers: { "content-type": "text/event-stream" },
|
||||
});
|
||||
});
|
||||
try {
|
||||
await h.handlers.get("context")![0]!({ messages: branch.map((entry) => entry.message) }, ctx);
|
||||
const headers: Record<string, string> = { "x-codex-beta-features": "existing-feature" };
|
||||
await h.handlers.get("before_provider_headers")![0]!({ headers }, ctx);
|
||||
assert.match(headers["x-codex-beta-features"]!, /existing-feature/);
|
||||
assert.notEqual(headers["x-codex-beta-features"], "existing-feature");
|
||||
await h.handlers.get("before_provider_request")![0]!({ payload: {
|
||||
model: "stale-cached-model", input: [{ role: "user", content: "STALE-RAW-HISTORY" }],
|
||||
store: true, stream: false,
|
||||
} }, ctx);
|
||||
const result = await h.handlers.get("session_before_compact")![0]!({
|
||||
reason: "manual", branchEntries: branch, signal: new AbortController().signal,
|
||||
preparation: { firstKeptEntryId: "u1" },
|
||||
}, ctx);
|
||||
assert.ok(result?.compaction, "native compaction should return a checkpoint");
|
||||
assert.equal(requests.length, 1);
|
||||
const request = requests[0]!;
|
||||
assert.equal(request.url, provider === "sub-api"
|
||||
? "https://gateway.example/v1/responses" : "https://codex.example/codex/responses");
|
||||
assert.equal(request.headers.get("authorization"), `Bearer ${apiKey}`);
|
||||
assert.equal(request.headers.get("x-test-route"), "preserved");
|
||||
assert.equal(request.headers.get("chatgpt-account-id"), provider === "sub-api" ? null : "test-account");
|
||||
assert.equal(request.body.model, "selected-model");
|
||||
assert.equal(request.body.stream, true);
|
||||
assert.equal(request.body.store, false);
|
||||
assert.deepEqual(request.body.input.at(-1), { type: "compaction_trigger" });
|
||||
assert.match(JSON.stringify(request.body.input), /Remember the test decision/);
|
||||
assert.doesNotMatch(JSON.stringify(request.body.input), /STALE-RAW-HISTORY/);
|
||||
const details = result.compaction.details;
|
||||
assert.equal(details.checkpoint, "native");
|
||||
assert.equal(details.modelKey, `${provider}:${ctx.model.api}:selected-model`);
|
||||
assert.ok(details.replacementHistory.some((item: any) => item.encrypted_content === "opaque-checkpoint"));
|
||||
assert.ok(details.portableSummary.length > 0);
|
||||
branch.push(
|
||||
{ type: "compaction", id: "c1", parentId: "u1", ...result.compaction },
|
||||
{ type: "message", id: "u2", parentId: "c1", message: { role: "user", content: "Continue after compaction.", timestamp: 2 } },
|
||||
);
|
||||
const projected = await h.handlers.get("context")![0]!({ messages: [] }, ctx);
|
||||
assert.doesNotMatch(JSON.stringify(projected.messages), /opaque-checkpoint/);
|
||||
const payload = await h.handlers.get("before_provider_request")![0]!({
|
||||
payload: { model: ctx.model.id, input: [], messages: [], previous_response_id: "stale-response" },
|
||||
}, ctx);
|
||||
assert.ok(payload.input.some((item: any) => item.encrypted_content === "opaque-checkpoint"));
|
||||
assert.match(JSON.stringify(payload.input), /Continue after compaction/);
|
||||
assert.equal(payload.previous_response_id, undefined);
|
||||
assert.equal(payload.messages, undefined);
|
||||
ctx.model = { ...ctx.model, id: "another-model" };
|
||||
const switched = await h.handlers.get("context")![0]!({ messages: [] }, ctx);
|
||||
assert.match(JSON.stringify(switched.messages), /Remember the test decision/);
|
||||
assert.equal(await h.handlers.get("before_provider_request")![0]!({ payload: { input: [] } }, ctx), undefined);
|
||||
} finally {
|
||||
await h.handlers.get("session_shutdown")![0]!({}, ctx);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
test("SubAPI native failures and cancellation return control to Pi without a checkpoint", async (t) => {
|
||||
const h = harness();
|
||||
piCompaction(h.pi);
|
||||
const branch: any[] = [{
|
||||
type: "message", id: "u1", parentId: null,
|
||||
message: { role: "user", content: "Test only.", timestamp: 1 },
|
||||
}];
|
||||
const ctx = context(branch);
|
||||
ctx.model = { provider: "sub-api", api: "openai-responses", id: "test-model", input: ["text"], baseUrl: "https://gateway.example/v1" };
|
||||
ctx.modelRegistry.getApiKeyAndHeaders = async () => ({ ok: true, apiKey: "test-key" });
|
||||
const responses = [
|
||||
new Response('{"error":{"message":"unsupported"}}', { status: 400 }),
|
||||
new Response('data: {"type":"response.completed","response":{}}\n\n', { headers: { "content-type": "text/event-stream" } }),
|
||||
];
|
||||
let calls = 0;
|
||||
t.mock.method(globalThis, "fetch", async (_url: string, init: RequestInit) => {
|
||||
init.signal?.throwIfAborted();
|
||||
calls++;
|
||||
return responses.shift()!;
|
||||
});
|
||||
try {
|
||||
for (let attempt = 0; attempt < 2; attempt++) {
|
||||
assert.equal(await h.handlers.get("session_before_compact")![0]!({
|
||||
reason: "manual", branchEntries: branch, preparation: { firstKeptEntryId: "u1" },
|
||||
signal: new AbortController().signal,
|
||||
}, ctx), undefined);
|
||||
}
|
||||
const controller = new AbortController();
|
||||
controller.abort();
|
||||
assert.equal(await h.handlers.get("session_before_compact")![0]!({
|
||||
reason: "manual", branchEntries: branch, preparation: { firstKeptEntryId: "u1" }, signal: controller.signal,
|
||||
}, ctx), undefined);
|
||||
assert.equal(calls, 2);
|
||||
assert.equal(branch.some((entry) => entry.type === "compaction"), false);
|
||||
} finally {
|
||||
await h.handlers.get("session_shutdown")![0]!({}, ctx);
|
||||
}
|
||||
});
|
||||
|
||||
test("other providers and SubAPI Chat Completions keep their provider requests unchanged", async () => {
|
||||
const h = harness();
|
||||
piCompaction(h.pi);
|
||||
const ctx = context();
|
||||
try {
|
||||
for (const model of [
|
||||
{ provider: "other", api: "openai-responses", id: "test" },
|
||||
{ provider: "sub-api", api: "openai-completions", id: "test" },
|
||||
]) {
|
||||
ctx.model = model;
|
||||
const headers: Record<string, string> = {};
|
||||
await h.handlers.get("before_provider_headers")![0]!({ headers }, ctx);
|
||||
assert.equal(headers["x-codex-beta-features"], undefined);
|
||||
assert.equal(await h.handlers.get("before_provider_request")![0]!({ payload: { input: [] } }, ctx), undefined);
|
||||
}
|
||||
} finally {
|
||||
await h.handlers.get("session_shutdown")![0]!({}, ctx);
|
||||
}
|
||||
});
|
||||
|
||||
test("native runtime delegates authentication to each provider without blocking SubAPI header-only auth", async (t) => {
|
||||
const cases: Array<{ name: string; provider?: string; auth: any; succeeds: boolean; authorization?: string; gatewayAuthorization?: string }> = [
|
||||
{ name: "explicit header", auth: { ok: true, apiKey: "dummy-key", headers: { Authorization: "Bearer explicit" } }, succeeds: true, authorization: "Bearer explicit" },
|
||||
{ name: "header only", auth: { ok: true, headers: { authorization: "Bearer header-only" } }, succeeds: true, authorization: "Bearer header-only" },
|
||||
{ name: "proxy header only", auth: { ok: true, headers: { "CF-AIG-Authorization": "Bearer proxy" } }, succeeds: true, gatewayAuthorization: "Bearer proxy" },
|
||||
{ name: "blank header", auth: { ok: true, headers: { Authorization: " " } }, succeeds: false },
|
||||
{ name: "missing credentials", auth: { ok: true }, succeeds: false },
|
||||
{ name: "registry failure", auth: { ok: false, error: "credentials unavailable" }, succeeds: false },
|
||||
{ name: "Codex header only", provider: "openai-codex", auth: { ok: true, headers: { Authorization: "Bearer header-only" } }, succeeds: false },
|
||||
{ name: "Codex invalid JWT", provider: "openai-codex", auth: { ok: true, apiKey: "invalid", headers: { Authorization: "Bearer header-only" } }, succeeds: false },
|
||||
];
|
||||
for (const scenario of cases) {
|
||||
await t.test(scenario.name, async (t) => {
|
||||
const h = harness();
|
||||
piCompaction(h.pi);
|
||||
const branch: any[] = [{
|
||||
type: "message", id: "u1", parentId: null,
|
||||
message: { role: "user", content: "Synthetic authentication test.", timestamp: 1 },
|
||||
}];
|
||||
const ctx = context(branch);
|
||||
const provider = scenario.provider ?? "sub-api";
|
||||
ctx.model = {
|
||||
provider, api: provider === "sub-api" ? "openai-responses" : "openai-codex-responses",
|
||||
id: "test-model", input: ["text"], baseUrl: "https://gateway.example/v1",
|
||||
};
|
||||
ctx.modelRegistry.getApiKeyAndHeaders = async () => scenario.auth;
|
||||
let calls = 0;
|
||||
t.mock.method(globalThis, "fetch", async (_url: string, init: RequestInit) => {
|
||||
calls++;
|
||||
const headers = new Headers(init.headers);
|
||||
assert.equal(headers.get("authorization"), scenario.authorization ?? null);
|
||||
assert.equal(headers.get("cf-aig-authorization"), scenario.gatewayAuthorization ?? null);
|
||||
const events = [
|
||||
{ type: "response.output_item.done", item: { type: "compaction", encrypted_content: "test-checkpoint" } },
|
||||
{ type: "response.completed", response: {} },
|
||||
];
|
||||
return new Response(events.map((event) => `data: ${JSON.stringify(event)}\n\n`).join(""), {
|
||||
headers: { "content-type": "text/event-stream" },
|
||||
});
|
||||
});
|
||||
try {
|
||||
const result = await h.handlers.get("session_before_compact")![0]!({
|
||||
reason: "manual", branchEntries: branch, preparation: { firstKeptEntryId: "u1" },
|
||||
signal: new AbortController().signal,
|
||||
}, ctx);
|
||||
assert.equal(Boolean(result?.compaction), scenario.succeeds);
|
||||
assert.equal(calls, scenario.succeeds ? 1 : 0);
|
||||
} finally {
|
||||
await h.handlers.get("session_shutdown")![0]!({}, ctx);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { buildCodexHeaders, mergeFeatureHeader } from "../src/native-compaction.ts";
|
||||
import { buildNativeCompactionTransport, isNativeCompactionModel } from "../src/native-provider.ts";
|
||||
|
||||
const subApi = {
|
||||
provider: "sub-api",
|
||||
api: "openai-responses",
|
||||
id: "gpt-test",
|
||||
baseUrl: "https://gateway.example/v1",
|
||||
};
|
||||
const params = { model: subApi, apiKey: "test-api-key", sessionId: "session-test" };
|
||||
|
||||
test("native compaction eligibility is limited to the supported provider/API pairs", () => {
|
||||
assert.equal(isNativeCompactionModel(subApi), true);
|
||||
assert.equal(isNativeCompactionModel({ provider: "openai-codex", api: "openai-codex-responses" }), true);
|
||||
for (const model of [
|
||||
null, {},
|
||||
{ ...subApi, provider: "another-gateway" },
|
||||
{ ...subApi, api: "openai-completions" },
|
||||
{ ...subApi, api: "openai-codex-responses" },
|
||||
{ provider: "openai-codex", api: "openai-responses" },
|
||||
]) assert.equal(isNativeCompactionModel(model), false);
|
||||
});
|
||||
|
||||
test("SubAPI uses its public Responses URL and API key without Codex account parsing", () => {
|
||||
const { url, headers } = buildNativeCompactionTransport({
|
||||
...params,
|
||||
headers: { "x-gateway-route": "test", "x-codex-beta-features": "existing-feature" },
|
||||
});
|
||||
assert.equal(url, "https://gateway.example/v1/responses");
|
||||
assert.equal(headers.get("authorization"), "Bearer test-api-key");
|
||||
assert.equal(headers.has("chatgpt-account-id"), false);
|
||||
assert.equal(headers.get("x-gateway-route"), "test");
|
||||
assert.equal(headers.get("content-type"), "application/json");
|
||||
assert.equal(headers.get("accept"), "text/event-stream");
|
||||
assert.equal(headers.get("session-id"), "session-test");
|
||||
assert.equal(headers.get("x-client-request-id"), "session-test");
|
||||
assert.equal(headers.get("x-codex-beta-features"), mergeFeatureHeader("existing-feature"));
|
||||
});
|
||||
|
||||
test("SubAPI URL normalization preserves prefixes and avoids duplicating responses", () => {
|
||||
for (const [baseUrl, expected] of [
|
||||
["https://gateway.example/v1/", "https://gateway.example/v1/responses"],
|
||||
["https://gateway.example/v1/responses", "https://gateway.example/v1/responses"],
|
||||
["https://gateway.example/v1/responses/", "https://gateway.example/v1/responses"],
|
||||
["https://gateway.example/prefix/v1?route=test", "https://gateway.example/prefix/v1/responses?route=test"],
|
||||
["http://localhost:8080", "http://localhost:8080/responses"],
|
||||
]) {
|
||||
assert.equal(buildNativeCompactionTransport({ ...params, model: { ...subApi, baseUrl } }).url, expected);
|
||||
}
|
||||
});
|
||||
|
||||
test("invalid SubAPI endpoints and unsupported providers fail without a Codex fallback", () => {
|
||||
for (const baseUrl of [undefined, "", " ", "/v1", "invalid", "file:///tmp/test", "https://user:secret@gateway.example/v1", "https://gateway.example/v1#fragment"]) {
|
||||
assert.throws(() => buildNativeCompactionTransport({ ...params, model: { ...subApi, baseUrl } }), /SubAPI/);
|
||||
}
|
||||
assert.throws(() => buildNativeCompactionTransport({
|
||||
...params, model: { ...subApi, provider: "other" },
|
||||
}), /not supported/);
|
||||
});
|
||||
|
||||
test("official Codex transport retains its URL, JWT account ID and headers", () => {
|
||||
const payload = Buffer.from(JSON.stringify({
|
||||
"https://api.openai.com/auth": { chatgpt_account_id: "test-account" },
|
||||
})).toString("base64url");
|
||||
const codexParams = {
|
||||
...params,
|
||||
model: { provider: "openai-codex", api: "openai-codex-responses", id: "gpt-test" },
|
||||
apiKey: `header.${payload}.signature`,
|
||||
headers: { "x-custom": "preserved" },
|
||||
};
|
||||
const transport = buildNativeCompactionTransport(codexParams);
|
||||
assert.equal(transport.url, "https://chatgpt.com/backend-api/codex/responses");
|
||||
assert.deepEqual([...transport.headers], [...buildCodexHeaders(codexParams)]);
|
||||
assert.equal(transport.headers.get("chatgpt-account-id"), "test-account");
|
||||
assert.throws(() => buildNativeCompactionTransport({ ...codexParams, apiKey: "ordinary-key" }), /account ID/);
|
||||
});
|
||||
|
||||
test("SubAPI preserves explicit Authorization ahead of its API key, case-insensitively", () => {
|
||||
for (const name of ["Authorization", "authorization", "AUTHORIZATION"]) {
|
||||
const { headers } = buildNativeCompactionTransport({
|
||||
...params, headers: { [name]: "Bearer explicit-token" },
|
||||
});
|
||||
assert.equal(headers.get("authorization"), "Bearer explicit-token");
|
||||
}
|
||||
});
|
||||
|
||||
test("SubAPI accepts header-only authentication without synthesizing a bearer token", () => {
|
||||
for (const name of ["Authorization", "CF-AIG-Authorization"]) {
|
||||
const { headers } = buildNativeCompactionTransport({
|
||||
...params, apiKey: undefined, headers: { [name]: "Bearer header-only-token" },
|
||||
});
|
||||
assert.equal(headers.get(name), "Bearer header-only-token");
|
||||
if (name === "CF-AIG-Authorization") assert.equal(headers.has("authorization"), false);
|
||||
}
|
||||
});
|
||||
|
||||
test("SubAPI rejects absent or blank credentials but can fall back to a real API key", () => {
|
||||
const candidates: Record<string, string>[] = [{}, { Authorization: " " }, { "cf-aig-authorization": " " }, { "x-api-key": "not-supported" }];
|
||||
for (const headers of candidates) {
|
||||
for (const apiKey of [undefined, "", " "]) {
|
||||
assert.throws(() => buildNativeCompactionTransport({ ...params, apiKey, headers }), /authentication/i);
|
||||
}
|
||||
}
|
||||
const result = buildNativeCompactionTransport({ ...params, headers: { Authorization: " " } });
|
||||
assert.equal(result.headers.get("authorization"), "Bearer test-api-key");
|
||||
});
|
||||
|
||||
test("official Codex still requires an API key even with explicit header credentials", () => {
|
||||
assert.throws(() => buildNativeCompactionTransport({
|
||||
...params, apiKey: undefined,
|
||||
model: { provider: "openai-codex", api: "openai-codex-responses" },
|
||||
headers: { Authorization: "Bearer header-only-token" },
|
||||
}), /Codex.*authentication/i);
|
||||
});
|
||||
Reference in New Issue
Block a user