Skip to content
Merged
4 changes: 3 additions & 1 deletion scripts/test-suite-lock.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ export function snapshotSessionsTree(dir) {
const record = (p) => {
try {
const st = statSync(p);
out.set(path.relative(dir, p), [st.mtimeMs, st.size]);
// posix-normalize the relpath key: Windows path.relative yields '\\',
// and the diff, banner and tests all compare '/'-separated paths (#4599).
out.set(path.relative(dir, p).split(path.sep).join("/"), [st.mtimeMs, st.size]);
} catch {
/* vanished mid-walk — skip */
}
Expand Down
15 changes: 15 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2587,6 +2587,21 @@ export function effectiveTokenCount(session: Session, msgs: CoreMessage[], inbou
&& payloadUpperNow >= session.stats.lastInputTokens * SIDE_LANE_ANCHOR_IMPOSSIBLE_RATIO
&& payloadUpperNow >= session.stats.lastUsagePayloadUpper! * SIDE_LANE_ANCHOR_IMPOSSIBLE_RATIO
&& payloadUpperNow - session.stats.lastInputTokens > SIDE_LANE_ANCHOR_MIN_ABS_DELTA;
// #5135: pending compress credit proves THIS session executed its own fold
// since the last settle — applyRanges (stream.ts) already netted the
// savings out of lastInputTokens while lastUsageGradeTokens still carries
// the pre-fold report. Until processTurn materializes the fold and clears
// the credit, the resent history still contains the covered originals, so
// the raw inbound upper bound legitimately towers over the last OUTBOUND
// snapshot on long sessions — letting anchorImpossible stand would refuse
// this session's own honest anchor and hand the meter the stale pre-fold
// grade (false pressure-T2 right after a successful T1). Credit can only
// be minted by this conversation's own folds (foreign usage samples write
// none), so it exempts exactly this window; the #4805/#4849 anchor-
// pollution refusal stays fully armed when credit == 0. #4383's cycle-stale guard
// still wins (#4383: newer unreported sends ride their own measurements);
// zero is a valid drained reading (kernel treats 0 as unknown).
if (!anchorStale && session.stats.lastInputTokensSource === "usage" && (session.stats.compressCreditTokens ?? 0) > 0 && session.stats.lastInputTokens >= 0) return { tokens: session.stats.lastInputTokens, source: "usage" };
if (!anchorStale && !anchorImpossible && session.stats.lastInputTokens > 0 && (session.stats.lastInputTokensSource === "usage" || session.stats.lastInputTokensSource === "overflow-arm") && staleSmallAnchorFallback(session.stats, session.stats.lastInputTokens) === null) return { tokens: session.stats.lastInputTokens, source: "usage" };
// #1820: right after a preflight rebuild the usage-grade baseline above is
// momentarily absent (the rebuild request's own report hasn't landed yet,
Expand Down
42 changes: 32 additions & 10 deletions tests/e2e/e2e-native-dsh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,29 +369,51 @@ function writeBiliConfig(ctx: Ctx): void {

// Graceful-stop the hermetic native proxy (SIGTERM — the server flushes
// dirty sessions on shutdown) so persisted state is observable on disk.
async function stopProxiesGracefully(ctx: Ctx): Promise<void> {
const instancesDir = path.join(ctx.xdg.state, "billion-context", "instances");
function liveInstancePids(instancesDir: string): number[] {
const out: number[] = [];
try {
for (const f of fs.readdirSync(instancesDir)) {
try {
const rec = JSON.parse(
fs.readFileSync(path.join(instancesDir, f), "utf8"),
) as { pid?: number };
if (typeof rec.pid === "number" && rec.pid > 0) {
try {
process.kill(rec.pid, "SIGTERM");
} catch {
/* already gone */
}
}
if (typeof rec.pid === "number" && rec.pid > 0) out.push(rec.pid);
} catch {
/* unreadable record */
}
}
} catch {
/* no instances dir */
}
await new Promise((r) => setTimeout(r, 500));
return out;
}

async function stopProxiesGracefully(ctx: Ctx): Promise<void> {
const instancesDir = path.join(ctx.xdg.state, "billion-context", "instances");
for (const pid of liveInstancePids(instancesDir)) {
try {
process.kill(pid, "SIGTERM");
} catch {
/* already gone */
}
}
// Wait for actual EXIT, not a blind sleep: the product drains keep-alives
// BEFORE flushing sessions on SIGTERM (SHUTDOWN_DRAIN_GRACE_MS = 10s
// backstop), so a fixed short read can land pre-flush and flake
// ("proxy must persist the session", #4599). Exit ⇒ flush completed.
const deadline = Date.now() + 20_000;
for (;;) {
const alive = liveInstancePids(instancesDir).filter((pid) => {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
});
if (alive.length === 0 || Date.now() > deadline) break;
await new Promise((r) => setTimeout(r, 100));
}
}

function teardown(ctx: Ctx): void {
Expand Down
6 changes: 5 additions & 1 deletion tests/e2e/e2e-native-opencode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,11 @@ async function stopProxiesGracefully(ctx: Ctx): Promise<void> {
}
}
}
await new Promise((r) => setTimeout(r, 500));
// Wait for actual EXIT, not a blind sleep: the product drains keep-alives
// BEFORE flushing sessions on SIGTERM (SHUTDOWN_DRAIN_GRACE_MS = 10s
// backstop), so a fixed short read can land pre-flush and flake
// ("proxy must persist the session", #4599). Exit ⇒ flush completed.
await awaitProxyExit(ctx, 20_000);
}

/** Wait (bounded) for the parent-watched proxy to exit so the next run starts
Expand Down
42 changes: 32 additions & 10 deletions tests/e2e/e2e-native-pi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,29 +363,51 @@ async function startCtx(): Promise<Ctx> {

// Graceful-stop the hermetic native proxy (SIGTERM — the server flushes
// dirty sessions on shutdown) so persisted state is observable on disk.
async function stopProxiesGracefully(ctx: Ctx): Promise<void> {
const instancesDir = path.join(ctx.xdg.state, "billion-context", "instances");
function liveInstancePids(instancesDir: string): number[] {
const out: number[] = [];
try {
for (const f of fs.readdirSync(instancesDir)) {
try {
const rec = JSON.parse(
fs.readFileSync(path.join(instancesDir, f), "utf8"),
) as { pid?: number };
if (typeof rec.pid === "number" && rec.pid > 0) {
try {
process.kill(rec.pid, "SIGTERM");
} catch {
/* already gone */
}
}
if (typeof rec.pid === "number" && rec.pid > 0) out.push(rec.pid);
} catch {
/* unreadable record */
}
}
} catch {
/* no instances dir */
}
await new Promise((r) => setTimeout(r, 500));
return out;
}

async function stopProxiesGracefully(ctx: Ctx): Promise<void> {
const instancesDir = path.join(ctx.xdg.state, "billion-context", "instances");
for (const pid of liveInstancePids(instancesDir)) {
try {
process.kill(pid, "SIGTERM");
} catch {
/* already gone */
}
}
// Wait for actual EXIT, not a blind sleep: the product drains keep-alives
// BEFORE flushing sessions on SIGTERM (SHUTDOWN_DRAIN_GRACE_MS = 10s
// backstop), so a fixed short read can land pre-flush and flake
// ("proxy must persist the session", #4599). Exit ⇒ flush completed.
const deadline = Date.now() + 20_000;
for (;;) {
const alive = liveInstancePids(instancesDir).filter((pid) => {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
});
if (alive.length === 0 || Date.now() > deadline) break;
await new Promise((r) => setTimeout(r, 100));
}
}

function teardown(ctx: Ctx): void {
Expand Down
6 changes: 5 additions & 1 deletion tests/e2e/e2e-subagent-sessions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,11 @@ async function stopProxiesGracefully(ctx: Ctx): Promise<void> {
}
}
}
await new Promise((r) => setTimeout(r, 500));
// Wait for actual EXIT, not a blind sleep: the product drains keep-alives
// BEFORE flushing sessions on SIGTERM (SHUTDOWN_DRAIN_GRACE_MS = 10s
// backstop), so a fixed short read can land pre-flush and flake
// ("proxy must persist the session", #4599). Exit ⇒ flush completed.
await awaitProxyExit(ctx, 20_000);
}

async function awaitProxyExit(ctx: Ctx, timeoutMs = 10_000): Promise<void> {
Expand Down
Loading
Loading