Skip to content

feat(bin): add OpenRouter backend to typed dispatch resolution - #7524

Closed
siddhanthpranavrao wants to merge 3 commits into
kunchenguid:mainfrom
siddhanthpranavrao:fm/dispatch-openrouter
Closed

siddhanthpranavrao wants to merge 3 commits into
kunchenguid:mainfrom
siddhanthpranavrao:fm/dispatch-openrouter

Conversation

@siddhanthpranavrao

Copy link
Copy Markdown
Contributor

Intent

Let's create a very simple open router adapter and let's create a pull request in the actual GitHub repository. Let's make it very clean. Let's get it reviewed also. Let's test it out also to see if it's working. When opening a pull request, please open a pull request from my personal GitHub account, not from the company account.

Context: the typed dispatch resolver (bin/fm-dispatch-resolve.sh) only talks to TypeSafe's own API behind TYPESAFE_API_KEY. The captain has an OpenRouter account and no TypeSafe account. OpenRouter serves the same Jev model through a dedicated decisions endpoint, POST https://openrouter.ai/api/alpha/decisions, which was probed live from this Mac with the resolver's exact request body (model "typesafe/jev-1.13", state + one Choice question with instructions and criteria): it answered HTTP 200 in about 600 ms with the exact response shape the resolver already validates (answers.rule.choice, answers.rule.probabilities summing to 1, answers.rule.confidence, usage.input_tokens/output_tokens, plus extra fields id, provider, cost that must be tolerated). The chat/completions endpoint rejects the model ("decisions model ... use /api/alpha/decisions"). Cost per call is about two thousandths of a cent.

The pull request needs to be very cleanly open with proper descriptions and testing in place, so that the PR, when it's audited by Kun Chen, he has all the information and it is very, very properly built into his repository, not some patchy fix, a long term fix which anyone can use out there. Let's get reviewed, test it out thoroughly. The credentials are sitting in my aero-lens repository in the .env file. Use that, test it out, confirm the roles with me, so that the roles are set up properly, about how the routing is going to be done based on the model effort and the amount of quota left. And after that we will open the PR using my personal GitHub account and ping me the link.

What Changed

  • bin/fm-dispatch-resolve.sh reads OPENROUTER_API_KEY from the environment or $FM_HOME/.env through fmx_env_get when no TYPESAFE_API_KEY is present, and POSTs model typesafe/jev-1.13 to https://openrouter.ai/api/alpha/decisions with the unchanged request body and response validation. A TypeSafe key from either source always wins. Only the selected key is kept, it still reaches curl as a header on a file descriptor, and both key variables plus any caller-exported API_KEY_PRIVATE are unset before child processes run. The output's model: line now carries a backend=<name> word.
  • bin/fm-bootstrap.sh treats an OpenRouter key like a TypeSafe key: it activates resolver-field validation of config/crew-dispatch.json and scrubs the key from child environments.
  • Tests and docs cover the new backend: tests/fm-dispatch-resolve.test.sh adds a fake-curl case for endpoint selection, key precedence, secret handling, and an OpenRouter-shaped response with extra fields; tests/fm-bootstrap.test.sh adds the .env and environment OpenRouter cases; docs/configuration.md, docs/verification/dispatch-resolve.md, the home-layout skill, bin/fm-env-lib.sh, and bin/fm-test-run.sh comments document the backend table, precedence, and live-run verification.

Risk Assessment

✅ Low: Both prior fixes are verified in source (API_KEY_PRIVATE is unset at the top so a caller export cannot survive reassignment, and the backend is rendered as one model-line word via a jq --arg with the RESULT JSON field removed); the OpenRouter path reuses the identical request body, response validation, fd-3 bearer header, and precedence rules, with behavior-driven tests through the public argv/env interface and no live network, and docs match the code.

Testing

Ran the two targeted suites (tests/fm-dispatch-resolve.test.sh with the new OpenRouter selection, precedence and child-env cases, and tests/fm-bootstrap.test.sh for the OpenRouter-gated resolver-field validation); both pass. Then drove the real resolver in a disposable fm-lab FM_HOME against the live OpenRouter decisions endpoint with the aero-lens key and the shipped example rules: three briefs (typo, fresh-news, big refactor) each resolved to the expected rule with real quota-axi evidence, the .env key path worked, a bogus TYPESAFE_API_KEY in the environment correctly took precedence over the real OpenRouter .env key (typesafe 401 error outcome, OpenRouter never called), no keys produced the unchanged off line, and a bad OpenRouter key produced a 401 error outcome with exit 0. A curl shim on PATH proved the real network call carried no TYPESAFE/OPENROUTER/API_KEY_PRIVATE variables in its environment. The bootstrap gating scenario was not driven live; it rests on the test suite only. Evidence is one redacted transcript; the lab home was removed and the worktree is clean.

  • Live validation: ✅ go - 6 of 7 scenarios driven live against the product
Scenario Result Live Evidence
OpenRouter-only operator: with OPENROUTER_API_KEY in the environment the resolver calls openrouter.ai/api/alpha/decisions, tolerates id/provider/cost, picks the right rule for three different briefs w… ✅ pass live live-openrouter-transcript.md sections or-typo, or-news, or-refactor
OPENROUTER_API_KEY read from $FM_HOME/.env (no environment key) activates the OpenRouter backend ✅ pass live live-openrouter-transcript.md section or-dotenv
Secret hygiene: the selected key is absent from curl's argv and from the child environment even when the caller exports a stale API_KEY_PRIVATE ✅ pass live live-openrouter-transcript.md curl shim child-environment log (no LEAK lines, header via @/dev/fd/3); tests/fm-dispatch-resolve.test.sh child-env assertion
Precedence: a TYPESAFE_API_KEY in the environment wins over an OpenRouter key in .env, so typesafe.ai is called and OpenRouter never is ✅ pass live live-openrouter-transcript.md section precedence and its shim log (POST https://api.typesafe.ai/v1/systemone only)
No keys: resolver prints the unchanged 'dispatch-resolve: off' line, nothing on stdout, exit 0 ✅ pass live live-openrouter-transcript.md section off
Adversarial: a bad OpenRouter key is an error outcome (http 401) with exit 0, no crash, no key echoed ✅ pass live live-openrouter-transcript.md section or-badkey; grep for the key across evidence returns no files
Bootstrap gates resolver-field validation on an OpenRouter key (env or .env) and bootstrap children never inherit it ⏸️ untested no Not driven against the live product. fm-bootstrap.sh requires a full primary launch, which was not performed in this run; only the unit suite tests/fm-bootstrap.test.sh (OpenRouter cases at lines 1220…
Evidence: Live OpenRouter resolver transcript (7 runs, curl shim logs, key redacted)
# Live OpenRouter dispatch-resolve transcript (2026-10-19, disposable FM_HOME lab, docs/examples/crew-dispatch.json, real quota-axi, aero-lens OPENROUTER_API_KEY, key redacted)

## or-typo
`` `
dispatch-resolve:
  status: clear
  model: typesafe/jev-1.13-20260917 backend=openrouter   latency_ms: 930   tokens: 550/53
  rule: rule_2 (The task is a trivial mechanical edit such as a rote rename,)   confidence: 1
  probabilities: rule_1=0 rule_2=1 rule_3=0 default=0
  note: rule matched
  candidate: claude:haiku  provider=claude  scope=all_models  remaining=65%  spendPriority=0.5641  runway=through_reset  -> eligible
  profile: --harness 'claude' --model 'haiku' --effort 'low'
exit=0
-- stderr:
`` `

## or-news
`` `
dispatch-resolve:
  status: escalate
  model: typesafe/jev-1.13-20260917 backend=openrouter   latency_ms: 526   tokens: 549/53
  rule: rule_1 (The task depends on fresh news, current events, live public )   confidence: 1
  probabilities: rule_1=1 rule_2=0 rule_3=0 default=0
  reason: no rankable eligible candidate
  note: rule matched
  candidate: grok:-  provider=grok  -> eligible, unranked: provider grok unmeasured (unknown): disclosed uncertainty
exit=0
-- stderr:
`` `

## or-refactor
`` `
dispatch-resolve:
  status: clear
  model: typesafe/jev-1.13-20260917 backend=openrouter   latency_ms: 595   tokens: 566/53
  rule: rule_3 (The task is a big or ambiguous multi-file feature, a risky r)   confidence: 1
  probabilities: rule_1=0 rule_2=0 rule_3=1 default=0
  note: rule matched
  candidate: claude:claude-sonnet-5  provider=claude  scope=all_models  remaining=65%  spendPriority=0.5644  runway=through_reset  -> eligible
  candidate: codex:gpt-5.5  provider=codex  scope=all_models  remaining=99%  spendPriority=1.1912  runway=through_reset  -> eligible
  profile: --harness 'codex' --model 'gpt-5.5' --effort 'high'
exit=0
-- stderr:
`` `

## or-dotenv
`` `
dispatch-resolve:
  status: clear
  model: typesafe/jev-1.13-20260917 backend=openrouter   latency_ms: 920   tokens: 550/53
  rule: rule_2 (The task is a trivial mechanical edit such as a rote rename,)   confidence: 1
  probabilities: rule_1=0 rule_2=1 rule_3=0 default=0
  note: rule matched
  candidate: claude:haiku  provider=claude  scope=all_models  remaining=65%  spendPriority=0.5673  runway=through_reset  -> eligible
  profile: --harness 'claude' --model 'haiku' --effort 'low'
exit=0
-- stderr:
`` `

## precedence
`` `
dispatch-resolve:
  status: error
  reason: http 401 after 559 ms: {"detail":{"error_type":"authentication_error","message":"Cannot authenticate with the server. Please check your API key and try again."}}
exit=0
-- stderr:
dispatch-resolve: error (http 401 after 559 ms: {"detail":{"error_type":"authentication_error","message":"Cannot authenticate with the server. Please check your API key and try again."}})
`` `

## off
`` `
exit=0
-- stderr:
dispatch-resolve: off (TYPESAFE_API_KEY absent from the environment and <LAB>/.env)
`` `

## or-badkey
`` `
dispatch-resolve:
  status: error
  reason: http 401 after 510 ms: {"error":{"message":"User not found.","code":401}}
exit=0
-- stderr:
dispatch-resolve: error (http 401 after 510 ms: {"error":{"message":"User not found.","code":401}})
`` `

## curl shim child-environment log (S1: .env OpenRouter key with exported stale API_KEY_PRIVATE in caller env; no LEAK: lines)
`` `
argv: -sS --max-time 5 -o /var/folders/4l/h32kk4fn7dsc9wpqkk6vg0mw0000gn/T/tmp.O49gQCbgDA -w %{http_code} -X POST https://openrouter.ai/api/alpha/decisions -H Content-Type: application/json -H @/dev/fd/3 --data-binary @-
`` `

## curl shim log (S2: bogus TYPESAFE_API_KEY in env beats real OpenRouter key in .env)
`` `
argv: -sS --max-time 5 -o /var/folders/4l/h32kk4fn7dsc9wpqkk6vg0mw0000gn/T/tmp.VHS6tZdp6q -w %{http_code} -X POST https://api.typesafe.ai/v1/systemone -H Content-Type: application/json -H @/dev/fd/3 --data-binary @-
`` `
Evidence: OpenRouter-only clear resolution (trivial edit brief)
dispatch-resolve:
status: clear
model: typesafe/jev-1.13-20260917 backend=openrouter latency_ms: 930 tokens: 550/53
rule: rule_2 (The task is a trivial mechanical edit such as a rote rename,) confidence: 1
probabilities: rule_1=0 rule_2=1 rule_3=0 default=0
note: rule matched
candidate: claude:haiku provider=claude scope=all_models remaining=65% spendPriority=0.5641 runway=through_reset -> eligible
profile: --harness 'claude' --model 'haiku' --effort 'low'
exit=0
Evidence: Precedence: bogus TYPESAFE_API_KEY beats real OpenRouter .env key (curl shim argv)
argv: -sS --max-time 5 -o <tmp> -w %{http_code} -X POST https://api.typesafe.ai/v1/systemone -H Content-Type: application/json -H @/dev/fd/3 --data-binary @-
status: error reason: http 401 after 559 ms: authentication_error exit=0

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

🔧 **Review** - 4 issues found → auto-fixed ✅
  • ⚠️ bin/fm-dispatch-resolve.sh:150 - The opt-in gate now also reads OPENROUTER_API_KEY, but the off line still says only "TYPESAFE_API_KEY absent from the environment and $FM_HOME/.env". An OpenRouter-only operator whose .env line is misspelled or blank gets a message naming a key they were never told to set, which misleads the diagnosis. Remedy: name both keys in the message (e.g. "TYPESAFE_API_KEY and OPENROUTER_API_KEY absent ...") and update the matching sentence in the script header and docs/configuration.md if they quote it.
  • ⚠️ bin/fm-dispatch-resolve.sh:141 - The pre-existing code defends the secret copy with export -n TYPESAFE_API_KEY_PRIVATE so an exported same-named variable in the caller's environment cannot carry the key into child processes. The new selected-key variable API_KEY_PRIVATE (assigned at lines 141/146 and sent to curl at line 350) has no such guard: if the calling environment happens to export API_KEY_PRIVATE (a generic name), bash keeps the export attribute on reassignment and the selected key is inherited by curl, jq, quota-axi and the never-send check children, contradicting the "absent from child environments" contract the docs now claim for the selected key. Remedy: add API_KEY_PRIVATE (and for hygiene BACKEND/MODEL/URL) to the export -n line at line 85, or unset API_KEY_PRIVATE before the selection block.
  • ⚠️ bin/fm-dispatch-resolve.sh:526 - Simplification: the change adds a new backend: field to the resolver's output line and the RESULT JSON (lines 488 and 526) plus matching test assertions. The stated intent asks for a very simple OpenRouter adapter; backend identification is already observable from the response's model value (jev-1.13.0 vs typesafe/jev-1.13-...), so no intent requirement needs a second label. If the maintainer prefers a minimal diff, remove the field and its assertions; if the explicit label is wanted for operators, keep it as is. This is a product/output decision for the author.
  • ℹ️ docs/configuration.md:1346 - The section heading "## Typed dispatch resolution (.env TYPESAFE_API_KEY)" and the surrounding anchor text still name only the TypeSafe key while the body now documents two keys. Cosmetic, but Kun Chen reading the ToC will not see OpenRouter is supported. Suggest "(.env TYPESAFE_API_KEY or OPENROUTER_API_KEY)"; check that no other doc links depend on the exact heading anchor before renaming.

🔧 Fix applied.
✅ Re-checked - no issues remain.

✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 6 of 7 scenarios driven live against the product
Scenario Result Live Evidence
OpenRouter-only operator: with OPENROUTER_API_KEY in the environment the resolver calls openrouter.ai/api/alpha/decisions, tolerates id/provider/cost, picks the right rule for three different briefs w… ✅ pass live live-openrouter-transcript.md sections or-typo, or-news, or-refactor
OPENROUTER_API_KEY read from $FM_HOME/.env (no environment key) activates the OpenRouter backend ✅ pass live live-openrouter-transcript.md section or-dotenv
Secret hygiene: the selected key is absent from curl's argv and from the child environment even when the caller exports a stale API_KEY_PRIVATE ✅ pass live live-openrouter-transcript.md curl shim child-environment log (no LEAK lines, header via @/dev/fd/3); tests/fm-dispatch-resolve.test.sh child-env assertion
Precedence: a TYPESAFE_API_KEY in the environment wins over an OpenRouter key in .env, so typesafe.ai is called and OpenRouter never is ✅ pass live live-openrouter-transcript.md section precedence and its shim log (POST https://api.typesafe.ai/v1/systemone only)
No keys: resolver prints the unchanged 'dispatch-resolve: off' line, nothing on stdout, exit 0 ✅ pass live live-openrouter-transcript.md section off
Adversarial: a bad OpenRouter key is an error outcome (http 401) with exit 0, no crash, no key echoed ✅ pass live live-openrouter-transcript.md section or-badkey; grep for the key across evidence returns no files
Bootstrap gates resolver-field validation on an OpenRouter key (env or .env) and bootstrap children never inherit it ⏸️ untested no Not driven against the live product. fm-bootstrap.sh requires a full primary launch, which was not performed in this run; only the unit suite tests/fm-bootstrap.test.sh (OpenRouter cases at lines 1220…
  • bash tests/fm-dispatch-resolve.test.sh (all 24 cases pass, including the new OpenRouter selection/precedence/child-env case)
  • bash tests/fm-bootstrap.test.sh (passes; OpenRouter .env and environment key gate resolver-field validation, no child inherits the key)
  • Live: FM_HOME=&lt;lab&gt; OPENROUTER_API_KEY=&lt;aero-lens key&gt; bin/fm-dispatch-resolve.sh &lt;brief&gt; --project aero-lens for a typo brief, a fresh-news brief and a multi-file refactor brief against docs/examples/crew-dispatch.json and the real quota-axi
  • Live: OPENROUTER_API_KEY only in $FM_HOME/.env, with API_KEY_PRIVATE=stale-caller-export exported in the caller env and a PATH curl shim logging env var presence before exec'ing /usr/bin/curl
  • Live: TYPESAFE_API_KEY=ts-bogus-key in env plus real OpenRouter key in .env (precedence; shim shows api.typesafe.ai called, OpenRouter not called)
  • Live: no key anywhere (off line, empty stdout, exit 0)
  • Live: OPENROUTER_API_KEY=sk-or-v1-bogus (http 401 error outcome, exit 0)
  • grep -l &lt;key&gt; evidence/* returns zero files
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

fm-dispatch-resolve.sh now accepts OPENROUTER_API_KEY as a second opt-in
key. When TYPESAFE_API_KEY is absent from both the environment and .env,
the resolver sends the same request body to OpenRouter's decisions endpoint
with model typesafe/jev-1.13. TYPESAFE_API_KEY keeps winning when both
keys are present. Secret handling is unchanged: only the selected key is
kept, both source variables are unset before any child process, and the
key reaches curl only as a header on a file descriptor. The model line now
names the backend that answered.

Bootstrap treats either key as activating typed resolver validation.
@siddhanthpranavrao

Copy link
Copy Markdown
Contributor Author

Closing: OpenRouter support for typed dispatch resolution landed in #7149 (FM_DISPATCH_PROVIDER=openrouter + FM_DISPATCH_MODEL), which covers this change. Verified live on our homes with typesafe/jev-1.13 through the decisions endpoint. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant