Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/fold-reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,9 @@ export interface ReconcileOptions {
* cleans the resend — benign churn that must not feed the #2193 destroy
* streak. #2815/#2789. */
markerStripped?: number;
/** Inbound wire items before codec expansion; auxiliary requests can
* project a few items into dozens of cores without carrying history. */
inboundMessageCount?: number;
}

/** Entry point wired into the four wire sites, immediately BEFORE the
Expand Down Expand Up @@ -1490,7 +1493,7 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt
// evidence is taken from such a pass; skipping is NOT recovery either, so
// the episode state is left exactly as found (the resets above stay
// reserved for true episode boundaries: reconcile off / no folds at all).
if (msgs.length < SIDE_REQUEST_MAX_MSGS) {
if (msgs.length < SIDE_REQUEST_MAX_MSGS || (opts.inboundMessageCount !== undefined && opts.inboundMessageCount < SIDE_REQUEST_MAX_MSGS)) {
return { kind: "noop", missing: 0, claims: 0, byPos: 0, byTool: 0, byTwin: 0, bySim: 0, byOrder: 0, byNorm: 0, byTurn: 0, byImage: 0, byShape: 0, reaped: 0, unmatched: 0 };
}
if (!session.metadata) return { kind: "noop", missing: 0, claims: 0, byPos: 0, byTool: 0, byTwin: 0, bySim: 0, byOrder: 0, byNorm: 0, byTurn: 0, byImage: 0, byShape: 0, reaped: 0, unmatched: 0 };
Expand Down
3 changes: 2 additions & 1 deletion src/server/prepare-anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export async function prepareAnthropic(
): Promise<Prepared> {
const sessionId = session.id;
const stream = parsed.stream === true;
const inboundMessageCount = Array.isArray(parsed.messages) ? parsed.messages.length : 1;
++session.stats.requests;
const injectTools = opts.compress.injectTool && !pluginMode;
// #3417: lane decisions (profile + per-wire observation) own the
Expand Down Expand Up @@ -287,7 +288,7 @@ export async function prepareAnthropic(
// [#1921] re-anchor fold coverage onto churned-but-same messages
// before the #1195 snapshot, so covered ids surviving a client
// re-serialization stay covered (src/fold-reconcile.ts).
reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts) });
reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount });
noteSystemPromptFingerprint(session, clientSystem, { sessionId, log });
// #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside
// processTurn may deactivate fully-drifted blocks, erasing them.
Expand Down
3 changes: 2 additions & 1 deletion src/server/prepare-google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export async function prepareGoogle(
): Promise<Prepared> {
const sessionId = session.id;
++session.stats.requests;
const inboundMessageCount = Array.isArray(parsed.contents) ? parsed.contents.length : 1;
let googleClientSystem = "";
let sysNotes: string[] = [];
let googleOutboundSystem: string | undefined;
Expand Down Expand Up @@ -172,7 +173,7 @@ export async function prepareGoogle(
// [#1921] re-anchor fold coverage onto churned-but-same messages
// before the #1195 snapshot, so covered ids surviving a client
// re-serialization stay covered (src/fold-reconcile.ts).
reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts) });
reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount });
if (!isTitleGen) noteSystemPromptFingerprint(session, systemText, { sessionId, log });
// #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside
// processTurn may deactivate fully-drifted blocks, erasing them.
Expand Down
3 changes: 2 additions & 1 deletion src/server/prepare-openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export async function prepareOpenai(
): Promise<Prepared> {
const sessionId = session.id;
const stream = parsed.stream === true;
const inboundMessageCount = Array.isArray(parsed.messages) ? parsed.messages.length : 1;
++session.stats.requests;
let openaiSystemText = "";
let sysNotes: string[] = [];
Expand Down Expand Up @@ -180,7 +181,7 @@ export async function prepareOpenai(
// [#1921] re-anchor fold coverage onto churned-but-same messages
// before the #1195 snapshot, so covered ids surviving a client
// re-serialization stay covered (src/fold-reconcile.ts).
reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts) });
reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount });
if (!isTitleGen) noteSystemPromptFingerprint(session, systemText, { sessionId, log });
// #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside
// processTurn may deactivate fully-drifted blocks, erasing them.
Expand Down
3 changes: 2 additions & 1 deletion src/server/prepare-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export async function prepareResponses(
decide?: DecideConfig,
): Promise<Prepared> {
const sessionId = session.id;
const inboundMessageCount = Array.isArray(parsed.input) ? parsed.input.length : 1;
const stream = parsed.stream === true;
++session.stats.requests;
// #3222: #651's reasoning-drop plan, computed on the INBOUND history before
Expand Down Expand Up @@ -278,7 +279,7 @@ export async function prepareResponses(
// [#1921] re-anchor fold coverage onto churned-but-same messages
// before the #1195 snapshot, so covered ids surviving a client
// re-serialization stay covered (src/fold-reconcile.ts).
reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts) });
reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount });
if (!isCompactionTrigger) {
noteSystemPromptFingerprint(session, responsesClientSystem, { sessionId, log });
// #2867: the mode-independent twin — lets the ledger classify a
Expand Down
26 changes: 26 additions & 0 deletions tests/fold-drift.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,34 @@ import assert from "node:assert/strict";
import { createCore, createInitialState, defaultConfig, type CoreMessage } from "acp-kernel";
import type { Session } from "../src/session.ts";
import { foldCoverage } from "../src/session.ts";
import { responsesToCore } from "acp-kernel/wire";
import { reconcileFoldCoverage, METADATA_DRIFT_ESCALATED, type FoldAnchor } from "../src/fold-reconcile.ts";

test("projected short requests preserve real kernel folds until the main history returns", () => {
const { session, stableResend, coveredIds } = driftFixture();
const core = createCore();
const config = defaultConfig(200_000);
const opts = { mode: "repair" as const };
reconcileFoldCoverage(session, stableResend, opts);
const blockIds = session.state.blocks.map((b) => b.blockId);
const input = Array.from({ length: 6 }, (_, i) => ({
type: "message" as const, role: "user" as const,
content: Array.from({ length: 13 }, (_, j) => ({ type: "input_text" as const, text: `short utility ${i} part ${j}` })),
}));
const { msgs } = responsesToCore({ input });
assert.ok(msgs.length >= 10);
for (let i = 0; i < 4; i++) {
reconcileFoldCoverage(session, msgs, { ...opts, inboundMessageCount: input.length });
session.state = core.processTurn({ messages: msgs, state: session.state, config, tokenCount: 0 }).state;
assert.deepEqual(session.state.blocks.map((b) => b.blockId), blockIds, "sync may deactivate but must retain every fold");
}
reconcileFoldCoverage(session, stableResend, opts);
session.state = core.processTurn({ messages: stableResend, state: session.state, config, tokenCount: 0 }).state;
assert.deepEqual(session.state.blocks.map((b) => b.blockId), blockIds);
assert.ok(session.state.blocks.every((b) => b.active));
assert.deepEqual(session.state.blocks[0].effectiveMessageIds, coveredIds);
});

function makeSession(): Session {
return {
id: `test-${Math.random().toString(36).slice(2)}`,
Expand Down
67 changes: 67 additions & 0 deletions tests/fold-reconcile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import { simhashHex } from "../src/affinity-simhash.ts";
import { defaultCountTokens } from "acp-kernel";
import type { CoreMessage } from "acp-kernel";
import { responsesToCore, openaiToCore, anthropicToCore, googleToCore } from "acp-kernel/wire";
import type { Session } from "../src/session.ts";
import { SessionStore, _setStoreForTest } from "../src/persist.ts";
import { conflictEventsOf } from "../src/conflict-watch.js";
Expand Down Expand Up @@ -652,6 +653,72 @@ describe("reconcileFoldCoverage coverage evidence + side-request guard (#2202)",
assert.equal(conflict?.kind, "orphan-reap");
});

test("short wire requests expanded into many cores never reap main folds", () => {
const ids = Array.from({ length: 12 }, (_, i) => `main${i}`);
const session = blockSession("main-fold", ids);
const originals = ids.map((id, i) => msg(id, "user", `main conversation evidence ${i}`));
reconcileFoldCoverage(session, originals, makeOpts());
const before = JSON.stringify({ state: session.state, metadata: session.metadata });
// A short Responses input can project to dozens of cores. The wire
// shape, rather than projection granularity, decides whether its
// missing history can be treated as evidence of owner deletion.
const input = Array.from({ length: 6 }, (_, i) => ({
type: "message" as const, role: "user" as const,
content: Array.from({ length: 13 }, (_, j) => ({ type: "input_text" as const, text: `utility ${i} part ${j}` })),
}));
const { msgs } = responsesToCore({ input });
assert.ok(msgs.length >= 10, `fixture must defeat the core-count guard, got ${msgs.length}`);
const opts = { ...makeOpts(), inboundMessageCount: input.length };
const passes = Array.from({ length: 4 }, () => {
const result = reconcileFoldCoverage(session, msgs, opts);
return { result, snapshot: JSON.stringify({ state: session.state, metadata: session.metadata }) };
});
assert.equal(session.state.blocks.length, 1, "four short wire passes must retain the main fold");
for (const [i, { result, snapshot }] of passes.entries()) {
assert.equal(result.kind, "noop", `short wire pass ${i + 1} takes no deletion evidence`);
assert.equal(result.reaped, 0);
assert.equal(snapshot, before);
}
const next = reconcileFoldCoverage(session, originals, makeOpts());
assert.equal(next.missing, 0);
assert.equal(session.state.blocks[0].blockId, "main-fold");
});

test("sibling-wire short multi-part passes never reap main folds (#3844 openai/anthropic/google)", () => {
const textParts = (n: number, tag: string) =>
Array.from({ length: n }, (_, j) => ({ type: "text" as const, text: `${tag} part ${j} words` }));
const wires = [
{ name: "openai", raw: 3, msgs: openaiToCore({ messages: Array.from({ length: 3 }, (_, i) => ({ role: "user" as const, content: textParts(5, `oai-${i}`) })) }).msgs },
{ name: "anthropic", raw: 3, msgs: anthropicToCore({ messages: Array.from({ length: 3 }, (_, i) => ({ role: "user" as const, content: textParts(5, `ant-${i}`) })) }).msgs },
{ name: "google", raw: 3, msgs: googleToCore({ contents: Array.from({ length: 3 }, (_, i) => ({ role: "user", parts: Array.from({ length: 5 }, (_, j) => ({ text: `ggl-${i} part ${j} words` })) })) }).msgs },
];
for (const w of wires) {
assert.ok(w.msgs.length >= 10, `${w.name}: fixture must defeat the core-count guard, got ${w.msgs.length}`);
const ctrlIds = Array.from({ length: 12 }, (_, i) => `c${i}`);
const ctrl = blockSession("main-fold", ctrlIds);
const ctrlOriginals = ctrlIds.map((id, i) => msg(id, "user", `control evidence ${i}`));
reconcileFoldCoverage(ctrl, ctrlOriginals, makeOpts());
for (let i = 0; i < 4; i++) reconcileFoldCoverage(ctrl, w.msgs, makeOpts());
assert.equal(ctrl.state.blocks.length, 0, `${w.name}: uncounted expanded short passes feed deletion evidence and reap the main fold (pre-fix behavior)`);
const ids = Array.from({ length: 12 }, (_, i) => `main${i}`);
const session = blockSession("main-fold", ids);
const originals = ids.map((id, i) => msg(id, "user", `main conversation evidence ${i}`));
reconcileFoldCoverage(session, originals, makeOpts());
const before = JSON.stringify({ state: session.state, metadata: session.metadata });
const opts = { ...makeOpts(), inboundMessageCount: w.raw };
for (let i = 0; i < 4; i++) {
const result = reconcileFoldCoverage(session, w.msgs, opts);
assert.equal(result.kind, "noop", `${w.name}: short wire pass ${i + 1} takes no deletion evidence`);
assert.equal(result.reaped, 0);
assert.equal(JSON.stringify({ state: session.state, metadata: session.metadata }), before);
}
assert.equal(session.state.blocks.length, 1, `${w.name}: four short wire passes retain the main fold`);
const next = reconcileFoldCoverage(session, originals, makeOpts());
assert.equal(next.missing, 0);
assert.equal(session.state.blocks[0].blockId, "main-fold");
}
});

test("short passes do not skew the next pass's alignment", () => {
const ids = Array.from({ length: 10 }, (_, i) => `k${i}`);
const session = blockSession("blk4", ids);
Expand Down
53 changes: 52 additions & 1 deletion tests/preflight-fail-fast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ process.env.NODE_ENV = "test";
// exponential backoff — these tests are about the post-retry behavior.
process.env.BILI_REPLAY_RETRY_MAX = "1";

import { defaultConfig, type Config } from "acp-kernel";
import { createCore, defaultConfig, type Config } from "acp-kernel";
import { startServer } from "../src/server.ts";
import { type CompressSettings, type ProxyOptions } from "../src/config.ts";
import { SessionStore, _setStoreForTest } from "../src/persist.ts";
import { METADATA_FOLD_COVERAGE } from "../src/fold-reconcile.ts";
import { getSession } from "../src/session.ts";
import { _setForTest as setRegistryForTest } from "../src/registry.ts";

// #301: when preflight (overflow) compression cannot proceed — the summary
Expand Down Expand Up @@ -116,6 +118,55 @@ function startProxy(upstreamPort: number, models: Record<string, { context: numb
} as ProxyOptions);
}

test("Responses short wire projection preserves main folds through the full request pipeline", async () => {
const calls: Call[] = [];
const upstream = makeUpstreamOk(calls);
upstream.listen(0, "127.0.0.1");
await once(upstream, "listening");
const upstreamPort = (upstream.address() as { port: number }).port;
const proxy = await startProxy(upstreamPort, { "gpt-short-wire": { context: 400_000 } });
await once(proxy, "listening");
const proxyPort = (proxy.address() as { port: number }).port;
const post = async (input: unknown[]) => {
const response = await fetch(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/responses`, {
method: "POST", headers: { "content-type": "application/json", "x-acp-session": "short-wire-pipeline" },
body: JSON.stringify({ model: "gpt-short-wire", stream: true, max_output_tokens: 1024, input }),
});
const body = await response.text();
assert.equal(response.status, 200, body.slice(0, 300));
};
try {
const main = Array.from({ length: 24 }, (_, i) => ({ type: "message", role: i % 2 ? "assistant" : "user", content: `main turn ${i} ${"x".repeat(2000)}` }));
await post(main);
const session = getSession("short-wire-pipeline");
const messages = session.lastMessages;
assert.ok(messages && messages.length >= 24);
session.state = createCore().applyCompression({
ranges: [{ startRef: "m00001", endRef: "m00007", summary: "Compressed main history: seven early turns preserved the investigation, implementation decisions, and verified test evidence." }],
messages, state: session.state, config: defaultConfig(400_000),
}).state;
assert.equal(session.state.blocks.length, 1);
await post(main);
const blockIds = session.state.blocks.map((b) => b.blockId);
const coverage = structuredClone(session.metadata[METADATA_FOLD_COVERAGE]);
assert.ok(coverage);
const short = Array.from({ length: 6 }, (_, i) => ({
type: "message", role: "user",
content: Array.from({ length: 13 }, (_, j) => ({ type: "input_text", text: `utility ${i} part ${j}` })),
}));
for (let i = 0; i < 4; i++) await post(short);
assert.deepEqual(session.metadata[METADATA_FOLD_COVERAGE], coverage, "auxiliary projection must not accumulate deletion evidence");
assert.deepEqual(session.state.blocks.map((b) => b.blockId), blockIds);
await post(main);
assert.deepEqual(session.state.blocks.map((b) => b.blockId), blockIds);
assert.ok(session.state.blocks.every((b) => b.active), "returning main history reactivates every retained fold");
} finally {
proxy.close(); proxy.closeIdleConnections?.();
upstream.close(); upstream.closeIdleConnections?.();
await Promise.allSettled([once(proxy, "close"), once(upstream, "close")]);
}
});

test("e2e #301: overflow + summary upstream 429 → structured 503, over-window payload NOT forwarded", async () => {
const calls: Call[] = [];
// The preflight summarization call hits the same rate-limited upstream as
Expand Down
Loading