Files
OmniRoute/tests/unit/responses-continuation-store.test.ts
Markus Hartung 0f402a84a4 feat(responses): virtualize previous_response_id continuation regardless of upstream support (#10262)
* feat(responses): virtualize previous_response_id continuation regardless of upstream support

OmniRoute now exposes OpenAI-compatible previous_response_id/store
continuation to clients unconditionally, even when the selected upstream
provider has no native Responses-API state support. Reconstruction happens
server-side in handleChatImplementation, before any downstream validation
or provider translation: OmniRoute resolves the response id back to the
full input/output it previously produced, prepends it to the client's
delta, and forwards the full reconstructed history upstream exactly as it
does today. Client<->OmniRoute traffic shrinks to the new delta only;
OmniRoute<->provider traffic is unchanged.

Storage reuses the existing call-log pipeline artifact (already gated by
call_log_pipeline_enabled, already retained/cleaned up by the existing
call-log lifecycle) instead of duplicating conversation content into a
second store -- only a lightweight call_logs.response_id index is new.
Every lookup is scoped by api_key_id so one client can never resolve
another client's stored conversation, and any unresolvable/missing/
size-limit-omitted state fails closed with OpenAI's own
previous_response_not_found contract.

Stacked on feat/openai-responses-store-toggle (#10121).

* fix(db): re-export responsesContinuationStore from the localDb barrel

check-db-rules requires every db/ module to be re-exported (or explicitly
allowlisted as intentionally-internal) for discoverability. Missed this
when the module was first added.

* fix(db): renumber previous_response_id index migration to 154

The migration was numbered 153, but release/v3.8.50 already carries
153_radar_local_model_state.sql. The emngrating runner's collision guard
throws on two live .sql files sharing a numeric prefix, so the refreshed
merge would fail DB startup. Renumber to the next free slot (154).

Co-authored-by: diegosouzapw <diegosouza.pw@gmail.com>

* docs(db): sync migration count to 149 across llm.txt mirrors

The responses-continuation store adds one migration, so the docs'
migration count is now 149 (was 148). Update README/AGENTS/llm.txt and
regenerate the i18n llm.txt mirrors to keep check:docs-all green.

Co-authored-by: diegosouzapw <diegosouza.pw@gmail.com>

* fix(responses-continuation): respect preserve mode, drop dead export

- Un-export ResponsesContinuationState: it's never imported outside
  responsesContinuationStore.ts, its own defining file. Fixes the
  check:dead-code regression (410 > baseline 409).
- Scope the previous_response_id virtualization interception in chat.ts to
  skip entirely when responsesPreviousResponseIdMode=preserve. The
  interception ran unconditionally before target/connection selection,
  ahead of applyResponsesPreviousResponseIdPolicy (chatCore.ts) -- the
  existing per-target enforcement point for this setting -- so "preserve"
  (the explicit, connection-independent contract for "let the upstream
  resolve previous_response_id natively") was silently unreachable: the
  field was already deleted and replaced with locally-reconstructed input
  by the time that policy ran. This also broke Codex's own executor, which
  relies on an untouched previous_response_id to delegate history
  resolution upstream (see stripOrphanedCodexFunctionCallOutputs in
  codex.ts). "auto" and "strip" modes are unaffected -- virtualization is
  a strict improvement over their old "drop the field, hope the client
  resent everything" behavior.
- Add a regression test exercising the actual chat.ts handler (not just
  the policy helper in isolation): confirms mode=preserve now proceeds to
  normal routing instead of the virtualization's previous_response_not_found
  rejection, and that default/auto mode's existing virtualization behavior
  is unchanged. Verified the test fails for the right reason against
  pre-fix chat.ts.

Addresses PR review feedback.

---------

Co-authored-by: adevwithpurpose <adevwithpurpose@users.noreply.github.com>
Co-authored-by: hartmark <hartmark@users.noreply.github.com>
Co-authored-by: diegosouzapw <diegosouza.pw@gmail.com>
2026-08-17 08:22:17 -03:00

160 lines
5.1 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
// OmniRoute-native `previous_response_id` virtualization: resolvePreviousResponseState
// resolves a response id back to the full input/output a prior call produced by
// reading the already-persisted call-log artifact, so a later request can be
// reconstructed to full history server-side without duplicating conversation
// content into a second store.
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-responses-continuation-"));
process.env.DATA_DIR = TEST_DATA_DIR;
const core = await import("../../src/lib/db/core.ts");
const store = await import("../../src/lib/db/responsesContinuationStore.ts");
test.after(() => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
});
function insertCallLog(row: {
id: string;
responseId: string | null;
apiKeyId: string | null;
detailState: string;
artifactRelPath: string | null;
}) {
const db = core.getDbInstance();
db.prepare(
`INSERT INTO call_logs
(id, timestamp, method, path, status, model, provider, account, duration,
tokens_in, tokens_out, api_key_id, detail_state, artifact_relpath, response_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
).run(
row.id,
new Date().toISOString(),
"POST",
"/v1/responses",
200,
"gpt-5.4-pro",
"openai",
"acc1",
100,
10,
20,
row.apiKeyId,
row.detailState,
row.artifactRelPath,
row.responseId
);
}
function writeArtifact(relPath: string, pipeline: Record<string, unknown>) {
const absPath = path.join(TEST_DATA_DIR, "call_logs", relPath);
fs.mkdirSync(path.dirname(absPath), { recursive: true });
fs.writeFileSync(
absPath,
JSON.stringify({
schemaVersion: 5,
summary: {},
requestBody: null,
responseBody: null,
error: null,
pipeline,
})
);
}
test("resolvePreviousResponseState reconstructs input/output from the call-log artifact", () => {
insertCallLog({
id: "log-1",
responseId: "resp_abc",
apiKeyId: "key-1",
detailState: "ready",
artifactRelPath: "2026-01-01/log-1.json",
});
writeArtifact("2026-01-01/log-1.json", {
providerRequest: { body: { input: [{ type: "message", role: "user", content: "hi" }] } },
clientResponse: {
id: "resp_abc",
output: [{ type: "message", role: "assistant", content: "hello" }],
},
});
const result = store.resolvePreviousResponseState("resp_abc", "key-1");
assert.deepEqual(result, {
input: [{ type: "message", role: "user", content: "hi" }],
output: [{ type: "message", role: "assistant", content: "hello" }],
});
});
test("resolvePreviousResponseState returns null for an unknown response id", () => {
const result = store.resolvePreviousResponseState("resp_does_not_exist", "key-1");
assert.equal(result, null);
});
test("resolvePreviousResponseState never crosses tenants (scoped by api_key_id)", () => {
insertCallLog({
id: "log-2",
responseId: "resp_tenant_a",
apiKeyId: "key-a",
detailState: "ready",
artifactRelPath: "2026-01-01/log-2.json",
});
writeArtifact("2026-01-01/log-2.json", {
providerRequest: { body: { input: [{ role: "user", content: "secret" }] } },
clientResponse: { id: "resp_tenant_a", output: [{ role: "assistant", content: "reply" }] },
});
assert.equal(store.resolvePreviousResponseState("resp_tenant_a", "key-b"), null);
assert.equal(store.resolvePreviousResponseState("resp_tenant_a", null), null);
assert.notEqual(store.resolvePreviousResponseState("resp_tenant_a", "key-a"), null);
});
test("resolvePreviousResponseState returns null when the artifact is missing on disk", () => {
insertCallLog({
id: "log-3",
responseId: "resp_missing_file",
apiKeyId: "key-1",
detailState: "ready",
artifactRelPath: "2026-01-01/does-not-exist.json",
});
assert.equal(store.resolvePreviousResponseState("resp_missing_file", "key-1"), null);
});
test("resolvePreviousResponseState fails closed when the pipeline payload was size-limit-omitted", () => {
insertCallLog({
id: "log-4",
responseId: "resp_omitted",
apiKeyId: "key-1",
detailState: "ready",
artifactRelPath: "2026-01-01/log-4.json",
});
// A size-limit-omitted payload is replaced with a placeholder string, not
// an object -- resolvePreviousResponseState must never try to reconstruct
// from it and silently drop history.
writeArtifact("2026-01-01/log-4.json", {
providerRequest: { body: "[omitted: call log artifact size limit exceeded]" },
clientResponse: { id: "resp_omitted", output: [] },
});
assert.equal(store.resolvePreviousResponseState("resp_omitted", "key-1"), null);
});
test("resolvePreviousResponseState returns null when detail logging was never captured for this row", () => {
insertCallLog({
id: "log-5",
responseId: "resp_no_detail",
apiKeyId: "key-1",
detailState: "none",
artifactRelPath: null,
});
assert.equal(store.resolvePreviousResponseState("resp_no_detail", "key-1"), null);
});