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
2 changes: 1 addition & 1 deletion actions/setup/js/fixtures/routing-history/aw2-reducer.json

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions actions/setup/js/routing_history.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ function receiptBound(receipt, run) {
);
}

/**
* The base model of a pair: everything before `@` (or the whole string if no `@` is present).
* A suspension covers every effort of its base.
* @param {string} pairName
*/
function baseOf(pairName) {
const at = pairName.lastIndexOf("@");
return at >= 0 ? pairName.slice(0, at) : pairName;
}

/**
* Fold one outcome into a pair, as the bandit's record() does. Returns whether it counted.
* @param {HistoryState} state
Expand Down Expand Up @@ -201,6 +211,45 @@ function recordOutcome(state, scope, pairName, ok, at, cost, events) {
}
return false;
}
// A suspension covers every effort of its base model, so another effort of a suspended base
// settles the suspended sibling(s) when it runs (e.g. after the original effort left the rank).
const targetBase = baseOf(pairName);
/** @type {Array<{ record: PairRecord, suspended: Suspension }>} */
const siblings = [];
for (const [name, candidate] of Object.entries(scope.pairs)) {
if (name !== pairName && candidate.suspended !== null && baseOf(name) === targetBase) {
siblings.push({ record: candidate, suspended: candidate.suspended });
}
}
if (siblings.length > 0) {
if (ok) {
for (const { record, suspended: siblingSuspended } of siblings) {
const pending = Math.min(siblingSuspended.pending, record.history.length);
record.history = record.history.slice(0, record.history.length - pending);
record.suspended = null;
record.streak = 0;
}
pair.history.push(entry());
pair.streak = 0;
scope.last_success_seq = state.seq;
events.push([pairName, "back"]);
return true;
}
const dueSiblings = siblings.filter(({ suspended: siblingSuspended }) => scope.last_success_seq > siblingSuspended.since_seq);
if (dueSiblings.length > 0) {
for (const { record, suspended: siblingSuspended } of dueSiblings) {
const pending = Math.min(siblingSuspended.pending, record.history.length);
record.history = record.history.slice(record.history.length - pending);
record.suspended = null;
record.streak = 0;
}
pair.history.push(entry());
pair.streak = 0;
events.push([pairName, "broken"]);
return true;
}
return false;
}
pair.history.push(entry());
if (ok) {
pair.streak = 0;
Expand Down
126 changes: 126 additions & 0 deletions actions/setup/js/routing_history.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,129 @@ describe("routing history state", () => {
expect(averages([partial, full])).toEqual({ credits: 10.0, modelCredits: 10.0, bills: 1 });
});
});

describe("sibling-effort suspension settlement", () => {
const scopeKey = "copilot/cost/shared";
const gen = "9f2c41d0";
const makeRun = (number, at, pair, outcome, role = "probe") => ({
workflow: "pr-sous-chef",
run: { id: String(45_000_000_000 + number), number, attempt: 1 },
at,
generation: gen,
mode: "live",
scope: scopeKey,
observation: { outcome },
pair,
cost: { credits: 10.0, model_credits: 10.0, tokens: null },
receipt: {
workflow: "pr-sous-chef",
run: { id: String(45_000_000_000 + number), attempt: 1 },
generation: gen,
mode: "live",
scope: scopeKey,
role,
selected: pair,
incumbent: { before: "github-copilot/model-b@high", after: "github-copilot/model-b@high" },
},
prepare: { explore_allowed: true },
});

const makeSuspendedPair = (history, sinceSeq, pending, seen) => ({
history,
credits: 10.0,
model_credits: 10.0,
bills: history.length,
streak: pending,
tests: history.length,
suspended: { since_seq: sinceSeq, pending },
seen,
summary: null,
});

it("preserves prior successes on restore and drops them on confirmed break, emitting settlement events", () => {
const baseState = {
...emptyState("pr-sous-chef"),
seq: 6,
generation: { hash: gen, since_run: 1 },
last_run: 6,
scopes: {
[scopeKey]: {
incumbent: "github-copilot/model-b@high",
last_role: "serve",
last_success_seq: 6,
pairs: {
"github-copilot/model-a@low": makeSuspendedPair(
[
[1, "2026-11-01T00:00:00Z", true, 10.0, 10.0, null],
[2, "2026-11-02T00:00:00Z", true, 10.0, 10.0, null],
[3, "2026-11-03T00:00:00Z", false, 10.0, 10.0, null],
[4, "2026-11-04T00:00:00Z", false, 10.0, 10.0, null],
[5, "2026-11-05T00:00:00Z", false, 10.0, 10.0, null],
],
5,
3,
"2026-11-05T00:00:00Z"
),
},
},
},
};

const okEvents = [];
const okResult = applyRun(baseState, [], makeRun(7, "2026-11-07T00:00:00Z", "github-copilot/model-a@high", "success"), { events: okEvents });
const okScope = okResult.state.scopes[scopeKey];
expect(okEvents).toEqual([["github-copilot/model-a@high", "back"]]);
expect(okScope.last_success_seq).toBe(7);
expect(okScope.pairs["github-copilot/model-a@low"].suspended).toBeNull();
expect(okScope.pairs["github-copilot/model-a@low"].streak).toBe(0);
expect(okScope.pairs["github-copilot/model-a@low"].history.map(e => e[0])).toEqual([1, 2]);
expect(okScope.pairs["github-copilot/model-a@high"].history.map(e => [e[0], e[2]])).toEqual([[7, true]]);

const failEvents = [];
const failResult = applyRun(baseState, [], makeRun(7, "2026-11-07T00:00:00Z", "github-copilot/model-a@high", "failure"), { events: failEvents });
const failScope = failResult.state.scopes[scopeKey];
expect(failEvents).toEqual([["github-copilot/model-a@high", "broken"]]);
expect(failScope.last_success_seq).toBe(6);
expect(failScope.pairs["github-copilot/model-a@low"].suspended).toBeNull();
expect(failScope.pairs["github-copilot/model-a@low"].streak).toBe(0);
expect(failScope.pairs["github-copilot/model-a@low"].history.map(e => e[0])).toEqual([3, 4, 5]);
expect(failScope.pairs["github-copilot/model-a@high"].history.map(e => [e[0], e[2]])).toEqual([[7, false]]);
expect(failScope.pairs["github-copilot/model-a@high"].streak).toBe(0);
});

it("settles only due same-base siblings on failure and never touches other providers or model prefixes", () => {
const state = {
...emptyState("pr-sous-chef"),
seq: 10,
generation: { hash: gen, since_run: 1 },
last_run: 10,
scopes: {
[scopeKey]: {
incumbent: "github-copilot/model-b@high",
last_role: "serve",
last_success_seq: 6,
pairs: {
"github-copilot/model-a@low": makeSuspendedPair([[3, "2026-11-03T00:00:00Z", false, 10.0, 10.0, null]], 5, 3, "2026-11-03T00:00:00Z"),
"github-copilot/model-a@medium": makeSuspendedPair([[8, "2026-11-04T00:00:00Z", false, 10.0, 10.0, null]], 8, 1, "2026-11-04T00:00:00Z"),
"openai/model-a@low": makeSuspendedPair([[4, "2026-11-03T00:00:00Z", false, 10.0, 10.0, null]], 4, 1, "2026-11-03T00:00:00Z"),
"github-copilot/model-a-mini@low": makeSuspendedPair([[5, "2026-11-03T00:00:00Z", false, 10.0, 10.0, null]], 5, 1, "2026-11-03T00:00:00Z"),
},
},
},
};

const failResult = applyRun(state, [], makeRun(11, "2026-11-08T00:00:00Z", "github-copilot/model-a@high", "failure"));
const failPairs = failResult.state.scopes[scopeKey].pairs;
expect(failPairs["github-copilot/model-a@low"].suspended).toBeNull();
expect(failPairs["github-copilot/model-a@medium"].suspended).toEqual({ since_seq: 8, pending: 1 });
expect(failPairs["openai/model-a@low"].suspended).toEqual({ since_seq: 4, pending: 1 });
expect(failPairs["github-copilot/model-a-mini@low"].suspended).toEqual({ since_seq: 5, pending: 1 });

const okResult = applyRun(state, [], makeRun(11, "2026-11-08T00:00:00Z", "github-copilot/model-a@high", "success"));
const okPairs = okResult.state.scopes[scopeKey].pairs;
expect(okPairs["github-copilot/model-a@low"].suspended).toBeNull();
expect(okPairs["github-copilot/model-a@medium"].suspended).toBeNull();
expect(okPairs["openai/model-a@low"].suspended).toEqual({ since_seq: 4, pending: 1 });
expect(okPairs["github-copilot/model-a-mini@low"].suspended).toEqual({ since_seq: 5, pending: 1 });
});
});
Loading