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
146 changes: 143 additions & 3 deletions .github/workflows/test-model-routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ on:
paths:
- '.github/workflows/test-model-routing.yml'
- 'containers/api-proxy/Dockerfile'
- 'containers/api-proxy/automatic-model-eligibility.js'
- 'containers/api-proxy/model-information.coding-tiers.generated.json'
- 'containers/api-proxy/routing-*'
- 'scripts/ci/assert-model-routing-selection.cjs'
- 'containers/api-proxy/fixtures/routing-contract/**'
- 'containers/api-proxy/management.js'
- 'containers/api-proxy/guards/model-policy-guard.js'
Expand Down Expand Up @@ -195,7 +198,17 @@ jobs:
SCRIPT
)"
sudo --preserve-env=COPILOT_GITHUB_TOKEN,GITHUB_WORKSPACE,GITHUB_RUN_ID,GITHUB_RUN_ATTEMPT,GITHUB_REPOSITORY,GITHUB_WORKFLOW_REF,HOME,PATH \
node dist/cli.js --config "${ROUTING_DIR}/awf-config.yml" -- bash -euc "$ROUTED_COMMAND"
node dist/cli.js --config "${ROUTING_DIR}/awf-config.yml" \
--proxy-logs-dir "${ROUTING_DIR}/logs" \
-- bash -euc "$ROUTED_COMMAND"
node scripts/ci/assert-model-routing-selection.cjs \
--file "${ROUTING_DIR}/logs/api-proxy-logs/model-routing.jsonl" \
--source router \
--provider copilot \
--model github-copilot/claude-haiku-5.5 \
--wire-model claude-haiku-5.5 \
--forbid-text "Reply with exactly ROUTED_OK." \
--label ROUTED_COPILOT_AUDIT

- name: Run an effort-routed Copilot inference through AWF
env:
Expand Down Expand Up @@ -316,7 +329,114 @@ jobs:
SCRIPT
)"
sudo --preserve-env=COPILOT_GITHUB_TOKEN,GITHUB_WORKSPACE,GITHUB_RUN_ID,GITHUB_RUN_ATTEMPT,GITHUB_REPOSITORY,GITHUB_WORKFLOW_REF,HOME,PATH \
node dist/cli.js --config "${ROUTING_DIR}/awf-config.yml" -- bash -euc "$ROUTED_COMMAND"
node dist/cli.js --config "${ROUTING_DIR}/awf-config.yml" \
--proxy-logs-dir "${ROUTING_DIR}/logs" \
-- bash -euc "$ROUTED_COMMAND"
node scripts/ci/assert-model-routing-selection.cjs \
--file "${ROUTING_DIR}/logs/api-proxy-logs/model-routing.jsonl" \
--source router \
--provider copilot \
--model github-copilot/gpt-5.6-luna \
--wire-model gpt-5.6-luna \
--endpoint /responses \
--forbid-text "Reply with exactly ROUTED_OK." \
--label ROUTED_COPILOT_EFFORT_AUDIT

- name: Run a fallback Copilot inference through AWF
env:
COPILOT_GITHUB_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
ROUTING_DIR="${RUNNER_TEMP}/model-routing-fallback"
mkdir -m 700 -p "$ROUTING_DIR"
cat > "${ROUTING_DIR}/conversation.json" <<'JSON'
[{"role":"user","parts":[{"text":"Reply with exactly FALLBACK_OK."}]}]
JSON
cat > "${ROUTING_DIR}/awf-config.yml" <<YAML
experimental:
modelRouting: true
network:
allowDomains:
- api.githubcopilot.com
apiProxy:
enabled: true
allowedModels:
- github-copilot/claude-haiku-4.5
routing:
objective:
goal: cost
mode: balanced
task:
conversationFile: ${ROUTING_DIR}/conversation.json
container:
images:
agent: ${AGENT_IMAGE}
apiProxy: ${API_PROXY_IMAGE}
squid: ${SQUID_IMAGE}
router: ${ROUTER_IMAGE}
YAML

# Shortlisting only a model absent from the bundled router tables forces
# a healthy-router no_route fallback through the bundled coding-tiers table.
ROUTED_COMMAND="$(cat <<'SCRIPT'
set -euo pipefail
test -n "${COPILOT_API_URL:-}" || { echo "COPILOT_API_URL is missing" >&2; exit 1; }
reflect="$(curl --fail --silent --show-error --max-time 30 "${COPILOT_API_URL}/reflect")"
plan="$(node -e '
const assert = require("node:assert/strict");
const routing = JSON.parse(process.argv[1]).routing;
assert.equal(routing?.status, "selected", "/reflect did not report a fallback selection: " + JSON.stringify(routing));
const s = routing.selection;
assert.equal(s.provider, "copilot");
assert.equal(s.model, "github-copilot/claude-haiku-4.5");
assert.equal(s.wire_model, "claude-haiku-4.5");
assert.equal(s.effort, null);
assert.equal(s.endpoint, "/chat/completions");
console.error("FALLBACK_SELECTION=" + JSON.stringify(s));
console.log(JSON.stringify({
endpoint: s.endpoint,
body: {
model: s.wire_model,
messages: [{ role: "user", content: "Reply with exactly FALLBACK_OK." }],
max_tokens: 128,
stream: false,
},
}));
' "$reflect")"
endpoint="$(node -e 'process.stdout.write(JSON.parse(process.argv[1]).endpoint)' "$plan")"
body="$(node -e 'process.stdout.write(JSON.stringify(JSON.parse(process.argv[1]).body))' "$plan")"
response="$(curl --fail --silent --show-error --max-time 120 \
-H "Content-Type: application/json" \
--data "$body" \
"${COPILOT_API_URL}${endpoint}")"
node -e '
const assert = require("node:assert/strict");
const result = JSON.parse(process.argv[1]);
const text = result.choices?.[0]?.message?.content;
assert.equal(typeof text, "string", "Copilot response contained no message text: " + JSON.stringify(result).slice(0, 500));
assert.match(text, /FALLBACK_OK/, "Copilot did not return the expected fallback smoke response: " + JSON.stringify(result).slice(0, 500));
console.log("FALLBACK_COPILOT_INFERENCE=passed model=claude-haiku-4.5 endpoint=" + process.argv[2]);
' "$response" "$endpoint"
SCRIPT
)"
sudo --preserve-env=COPILOT_GITHUB_TOKEN,GITHUB_WORKSPACE,GITHUB_RUN_ID,GITHUB_RUN_ATTEMPT,GITHUB_REPOSITORY,GITHUB_WORKFLOW_REF,HOME,PATH \
node dist/cli.js --config "${ROUTING_DIR}/awf-config.yml" \
--proxy-logs-dir "${ROUTING_DIR}/logs" \
-- bash -euc "$ROUTED_COMMAND"
node scripts/ci/assert-model-routing-selection.cjs \
--file "${ROUTING_DIR}/logs/api-proxy-logs/model-routing.jsonl" \
--source fallback \
--provider copilot \
--model github-copilot/claude-haiku-4.5 \
--wire-model claude-haiku-4.5 \
--endpoint /chat/completions \
--effort null \
--fallback-reason no_route \
--fallback-tier lightweight \
--expect-classifier-attempts 0 \
--expect-completed-request \
--forbid-text "Reply with exactly FALLBACK_OK." \
--label FALLBACK_COPILOT_AUDIT

- name: Translate cross-endpoint Copilot sub-agent requests through AWF
env:
Expand Down Expand Up @@ -572,6 +692,9 @@ jobs:
assert.equal(selection.endpoint, reflected.endpoint);
assert.equal(selection.selected_effort, reflected.effort);
assert.equal(selection.selected_provider, reflected.provider);
assert.equal(selection.selection_source, "router");
assert.equal(selection.fallback_reason, null);
assert.equal(selection.fallback_tier, null);
assert.equal(selection.conversation_sha256, digest);
assert.deepEqual(selection.objective, { goal: "cost", mode: "balanced" });
assert.equal(selection.provider, "copilot");
Expand Down Expand Up @@ -752,7 +875,15 @@ jobs:
SCRIPT
)"
sudo --preserve-env=COPILOT_GITHUB_TOKEN,GITHUB_WORKSPACE,GITHUB_RUN_ID,GITHUB_RUN_ATTEMPT,GITHUB_REPOSITORY,GITHUB_WORKFLOW_REF,HOME,PATH \
node dist/cli.js --config "${ROUTING_DIR}/awf-config.yml" -- bash -euc "$ROUTED_COMMAND"
node dist/cli.js --config "${ROUTING_DIR}/awf-config.yml" \
--proxy-logs-dir "${ROUTING_DIR}/logs" \
-- bash -euc "$ROUTED_COMMAND"
node scripts/ci/assert-model-routing-selection.cjs \
--file "${ROUTING_DIR}/logs/api-proxy-logs/model-routing.jsonl" \
--source router \
--provider copilot \
--forbid-text "Reply with exactly ROUTED_OK." \
--label ROUTED_COPILOT_CLI_AUDIT

- name: Show Docker state on failure
if: failure()
Expand Down Expand Up @@ -941,8 +1072,17 @@ jobs:
)"
sudo --preserve-env=OPENAI_API_KEY,ANTHROPIC_API_KEY,ROUTING_PROVIDER,ROUTING_MODEL,ROUTING_WIRE_MODEL,GITHUB_WORKSPACE,GITHUB_RUN_ID,GITHUB_RUN_ATTEMPT,GITHUB_REPOSITORY,GITHUB_WORKFLOW_REF,HOME,PATH \
node dist/cli.js --config "${ROUTING_DIR}/awf-config.yml" \
--proxy-logs-dir "${ROUTING_DIR}/logs" \
--env ROUTING_PROVIDER="${ROUTING_PROVIDER}" --env ROUTING_MODEL="${ROUTING_MODEL}" --env ROUTING_WIRE_MODEL="${ROUTING_WIRE_MODEL}" \
-- bash -euc "$ROUTED_COMMAND"
node scripts/ci/assert-model-routing-selection.cjs \
--file "${ROUTING_DIR}/logs/api-proxy-logs/model-routing.jsonl" \
--source router \
--provider "${ROUTING_PROVIDER}" \
--model "${ROUTING_MODEL}" \
--wire-model "${ROUTING_WIRE_MODEL}" \
--forbid-text "Reply with exactly ROUTED_OK." \
--label "ROUTED_NATIVE_${ROUTING_PROVIDER}_AUDIT"

- name: Show Docker state on failure
if: failure()
Expand Down
192 changes: 192 additions & 0 deletions scripts/ci/assert-model-routing-selection.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
#!/usr/bin/env node
'use strict';

const assert = require('node:assert/strict');
const fs = require('node:fs');

const VALID_STAGES = new Set(['classification', 'selection', 'failure', 'request']);

function parseJsonl(text) {
const lines = text.trim().split('\n').filter(Boolean);
return lines.map((line, index) => {
try {
return JSON.parse(line);
} catch (error) {
throw new Error(`Invalid JSONL at line ${index + 1}: ${error.message}`);
}
});
}

function assertModelRoutingRecords(records, options) {
const {
source,
provider,
model,
wireModel,
endpoint,
effort,
fallbackReason,
fallbackTier,
expectClassifierAttempts,
expectCompletedRequest = false,
forbidText,
} = options;

assert.ok(source === 'router' || source === 'fallback', `Unsupported --source value: ${source}`);
assert.ok(Array.isArray(records) && records.length > 0, 'model-routing.jsonl contained no records');

for (const record of records) {
assert.match(record._schema, /^model-routing\/v.+$/);
assert.equal(record.event, 'model_routing');
assert.ok(VALID_STAGES.has(record.stage), `Unexpected routing record stage: ${record.stage}`);
assert.ok(Number.isFinite(Date.parse(record.timestamp)), `Invalid timestamp: ${record.timestamp}`);
if (forbidText) {
assert.ok(!JSON.stringify(record).includes(forbidText), 'routing audit captured prompt text');
}
}

assert.ok(!records.some(record => record.stage === 'failure'), 'routing audit recorded a failure');

const selections = records.filter(record => record.stage === 'selection');
assert.equal(selections.length, 1, `expected exactly one selection record, found ${selections.length}`);
const selection = selections[0];

if (provider !== undefined) {
assert.equal(selection.selected_provider, provider);
}
if (model !== undefined) {
assert.equal(selection.selected_model, model);
}
if (wireModel !== undefined) {
assert.equal(selection.wire_model, wireModel);
}
if (endpoint !== undefined) {
assert.equal(selection.endpoint, endpoint);
}
if (effort !== undefined) {
const expectedEffort = effort === 'null' ? null : effort;
assert.equal(selection.selected_effort, expectedEffort);
}

if (source === 'router') {
assert.equal(selection.selection_source, 'router');
assert.equal(selection.fallback_reason, null);
assert.equal(selection.fallback_tier, null);
assert.equal(typeof selection.router?.name, 'string', 'router.name must be a string');
assert.ok(selection.router.name.length > 0, 'router.name must be non-empty');
assert.equal(typeof selection.router?.version, 'string', 'router.version must be a string');
assert.ok(selection.router.version.length > 0, 'router.version must be non-empty');
assert.ok(
Array.isArray(selection.ranked_choices) && selection.ranked_choices.length > 0,
'router selection must include non-empty ranked_choices',
);
assert.ok(
selection.ranked_choices.some(choice => choice.model === selection.selected_model),
`selected_model ${selection.selected_model} was not in ranked_choices`,
);
} else {
assert.equal(selection.selection_source, 'fallback');
assert.ok(typeof fallbackReason === 'string' && fallbackReason.length > 0, '--fallback-reason is required for fallback');
assert.ok(typeof fallbackTier === 'string' && fallbackTier.length > 0, '--fallback-tier is required for fallback');
assert.equal(selection.fallback_reason, fallbackReason);
assert.equal(selection.fallback_tier, fallbackTier);
assert.equal(selection.ranked_choices, null);
if (fallbackReason === 'no_route') {
assert.equal(typeof selection.router?.name, 'string', 'healthy router fallback must preserve router.name');
assert.ok(selection.router.name.length > 0, 'router.name must be non-empty');
assert.equal(typeof selection.router?.version, 'string', 'healthy router fallback must preserve router.version');
assert.ok(selection.router.version.length > 0, 'router.version must be non-empty');
}
}

if (expectClassifierAttempts !== undefined) {
assert.equal(selection.classifier_attempts, expectClassifierAttempts);
}

if (expectCompletedRequest) {
const completed = records.filter(record => record.stage === 'request' && record.outcome === 'completed');
assert.ok(completed.length >= 1, 'expected at least one completed inference request record');
const request = completed[0];
assert.equal(request.status, 200);
assert.equal(request.requested_model, selection.wire_model);
assert.equal(request.selected_model, selection.wire_model);
assert.equal(request.selected_endpoint, selection.endpoint);
assert.equal(request.routed, 'as_selected');
assert.deepEqual(request.deviations, []);
}

return selection;
}

function parseArgs(argv) {
const options = {};
const valueFlags = {
'--file': 'file',
'--source': 'source',
'--provider': 'provider',
'--model': 'model',
'--wire-model': 'wireModel',
'--endpoint': 'endpoint',
'--effort': 'effort',
'--fallback-reason': 'fallbackReason',
'--fallback-tier': 'fallbackTier',
'--expect-classifier-attempts': 'expectClassifierAttempts',
'--forbid-text': 'forbidText',
'--label': 'label',
};
for (let i = 0; i < argv.length; i++) {
const arg = argv[i];
if (arg === '--expect-completed-request') {
options.expectCompletedRequest = true;
continue;
}
if (Object.hasOwn(valueFlags, arg)) {
const value = argv[++i];
if (value === undefined) {
throw new Error(`Missing value for ${arg}`);
}
const key = valueFlags[arg];
if (key === 'expectClassifierAttempts') {
const parsed = Number(value);
if (!Number.isInteger(parsed) || parsed < 0) {
throw new Error(`Invalid integer for ${arg}: ${value}`);
}
options[key] = parsed;
} else {
options[key] = value;
}
continue;
}
throw new Error(`Unknown argument: ${arg}`);
}
return options;
}

function runCli(argv) {
const options = parseArgs(argv);
assert.ok(typeof options.file === 'string' && options.file.length > 0, '--file is required');
const records = parseJsonl(fs.readFileSync(options.file, 'utf8'));
const selection = assertModelRoutingRecords(records, options);
const label = options.label || 'ROUTING_SELECTION_AUDIT';
const details = selection.selection_source === 'fallback'
? `source=fallback reason=${selection.fallback_reason} tier=${selection.fallback_tier} model=${selection.wire_model}`
: `source=router model=${selection.wire_model}`;
console.log(`${label}=passed ${details}`);
return selection;
}

if (require.main === module) {
try {
runCli(process.argv.slice(2));
} catch (error) {
console.error(error?.stack || String(error));
process.exit(1);
}
}

module.exports = {
parseJsonl,
assertModelRoutingRecords,
parseArgs,
runCli,
};
Loading
Loading