Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

⌘B on Harness's Jev key, for everyone without an OpenRouter key (#1174)

Dee3e8752be7eed
diff --git a/backend/src/config/env.ts b/backend/src/config/env.ts
index 0e6d8852d..eaca88551 100644
--- a/backend/src/config/env.ts
+++ b/backend/src/config/env.ts
@@ -210,6 +210,14 @@ const envSchema = z.object({
CURSOR_LLM_API_KEY: z.string().optional(),
CURSOR_LLM_MODEL: z.string().default('DeepSeek-V4-Flash-0731'),
+ // Harness's own OpenRouter key, spent on ⌘B's Jev decisions for people with no key of their own
+ // (routes/jevRoute.ts). Optional here, but the route fails CLOSED without it. Give it its own key with
+ // a credit limit on OpenRouter: that limit is the budget, the route's per-account caps only spread it.
+ JEV_OPENROUTER_API_KEY: z.string().optional(),
+ // Decisions a day across every account, on top of each account's own cap: at ~$0.00017 each, the default is
+ // about $34 a day. Past it ⌘B says Harness's Jev budget has run out until the next UTC day.
+ JEV_DAILY_DECISIONS: z.coerce.number().int().positive().default(200_000),
+
// TTL for the in-memory subdomain→machineId cache on the public app-proxy hot path
// (avoids a Mongo lookup per request). Bounds staleness after a cross-instance app teardown.
// 60 min default (mapping is stable; a same-instance delete evicts immediately).
diff --git a/backend/src/lib/bus.ts b/backend/src/lib/bus.ts
index c1ac43cd0..87d02e144 100644
--- a/backend/src/lib/bus.ts
+++ b/backend/src/lib/bus.ts
@@ -901,6 +901,18 @@ export async function getCommanderJoinGeneration(machineId: string): Promise<num
// here would also swallow the difference between "absent" and "Redis is down" for anyone reading logs.
// The key is a digest of the token — see `ssoProfileCache.ts` — and the value holds no credential.
+/**
+ * A fixed-window counter shared by every process and pod: the count after this one. The window is in the
+ * key, so the key's expiry only cleans up. ⌘B's Jev caps (routes/jevRoute.ts) count here, because a
+ * process-local count multiplies by workers × pods and resets on every deploy.
+ */
+export async function redisCount(key: string, ttlMs: number): Promise<number> {
+ const results = await pub.multi().set(key, '0', 'PX', Math.max(1, Math.floor(ttlMs)), 'NX').incr(key).exec()
+ const counted = results?.[1]
+ if (!counted || counted[0]) throw counted?.[0] ?? new Error('redis count failed')
+ return Number(counted[1])
+}
+
export const redisSsoProfileStore = {
get: (key: string): Promise<string | null> => pub.get(key),
async set(key: string, value: string, ttlMs: number): Promise<void> {
diff --git a/backend/src/routes/index.ts b/backend/src/routes/index.ts
index 7f400a81d..2eaa3514c 100644
--- a/backend/src/routes/index.ts
+++ b/backend/src/routes/index.ts
@@ -9,4 +9,5 @@ export * from './mobile.js'
export * from './apps.js'
export * from './analytics.js'
export * from './agentRoute.js'
+export * from './jevRoute.js'
export * from './store.js'
diff --git a/backend/src/routes/jevRoute.test.ts b/backend/src/routes/jevRoute.test.ts
new file mode 100644
index 000000000..9d728b1d8
--- /dev/null
+++ b/backend/src/routes/jevRoute.test.ts
@@ -0,0 +1,251 @@
+// POST /api/route/jev — ⌘B's Jev decisions on Harness's own key.
+//
+// Pinned here: that the route spends the key only on Jev (the model is ours, not the caller's), that
+// it fails closed without a key, that one account cannot spend without bound, and that what comes back
+// is only answers to the questions that were asked.
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+import Fastify, { type FastifyInstance } from 'fastify'
+
+const authenticateAccessToken = vi.hoisted(() => vi.fn())
+vi.mock('../lib/ssoAuth.js', async (importActual) => ({
+ ...(await importActual<typeof import('../lib/ssoAuth.js')>()),
+ authenticateAccessToken,
+}))
+
+import { jevAllowance, jevRouteRoutes, localJevCounter, parseJevAnswers, JEV_ROUTE_PATH, type JevBody, type JevCounter } from './jevRoute.js'
+import { errorHandler } from '../middlewares/errorHandler.js'
+import { registerAuthMiddleware, shouldSkipAuth } from '../middlewares/authMiddleware.js'
+import { env } from '../config/env.js'
+import { logger } from '../utils/logger.js'
+
+const body = (over: Partial<JevBody> = {}): JevBody => ({
+ state: { message: 'fix the login redirect' },
+ questions: {
+ target: { type: 'choice', instructions: 'Which session?', criteria: { s0: 'auth work', new: 'None of them' } },
+ topic: { type: 'choice', instructions: 'Does it name a subject?', criteria: { yes: 'Yes', no: 'No' } },
+ },
+ ...over,
+})
+
+async function build(count?: JevCounter): Promise<FastifyInstance> {
+ const app = Fastify()
+ app.setErrorHandler(errorHandler)
+ registerAuthMiddleware(app, authenticateAccessToken)
+ await app.register(jevRouteRoutes, count ? { count } : {})
+ await app.ready()
+ return app
+}
+
+/** The largest decision the CLI's router builds (cli/src/lib/routeDecide.ts): 40 sessions and "new", 40
+ * projects, 40 agents, every label cut to 600 characters, a 16,000-character message and a previous session. */
+function largest(message = 'x'.repeat(16_000)): JevBody {
+ const options = (prefix: string) => Object.fromEntries(Array.from({ length: 40 }, (_, i) => [`${prefix}${i}`, 'é'.repeat(599) + '…']))
+ return {
+ state: { message, previous_message_went_to: 'n'.repeat(300) },
+ questions: {
+ target: { type: 'choice', instructions: 'i'.repeat(760), criteria: { ...options('s'), new: 'None of them: no session does this kind of work' } },
+ topic: { type: 'choice', instructions: 'i'.repeat(160), criteria: { yes: 'y'.repeat(100), no: 'n'.repeat(140) } },
+ project: { type: 'choice', instructions: 'If this becomes new work, which project folder does it belong in?', criteria: options('p') },
+ agent: { type: 'choice', instructions: 'Which agent is best suited to do this work?', criteria: options('a') },
+ },
+ }
+}
+
+const post = (app: FastifyInstance, payload: unknown, auth = true) =>
+ app.inject({ method: 'POST', url: JEV_ROUTE_PATH, payload: payload as object, headers: auth ? { authorization: 'Bearer sso-token' } : {} })
+
+const jevSays = (answer: unknown, status = 200) =>
+ vi.spyOn(globalThis, 'fetch').mockImplementation(async () =>
+ new Response(typeof answer === 'string' ? answer : JSON.stringify(answer), { status, headers: { 'content-type': 'application/json' } }))
+
+const good = {
+ answers: {
+ target: { choice: 's0', probabilities: { s0: 0.9, new: 0.1 } },
+ topic: { choice: 'yes', probabilities: { yes: 0.97, no: 0.03 } },
+ },
+ usage: { cost: 0.00017, input_tokens: 4000 },
+}
+
+describe('POST /api/route/jev', () => {
+ beforeEach(() => {
+ vi.restoreAllMocks()
+ authenticateAccessToken.mockReset()
+ authenticateAccessToken.mockResolvedValue({ sub: 'u1', email: '[email protected]', role: 'user', autonomousEnv: 'prod' })
+ env.JEV_OPENROUTER_API_KEY = 'or-key'
+ })
+
+ it('is gated by the ordinary SSO middleware', async () => {
+ expect(shouldSkipAuth(JEV_ROUTE_PATH)).toBe(false)
+ const app = await build()
+ expect((await post(app, body(), false)).statusCode).toBe(401)
+ })
+
+ it("asks Jev with Harness's key and returns only the answers", async () => {
+ const spy = jevSays(good)
+ const app = await build()
+ const res = await post(app, body())
+ expect(res.statusCode).toBe(200)
+ expect(res.json().data).toEqual({ answers: good.answers })
+ const [url, init] = spy.mock.calls[0] as [string, RequestInit]
+ expect(url).toBe('https://openrouter.ai/api/alpha/decisions')
+ const headers = init.headers as Record<string, string>
+ expect(headers.Authorization).toBe('Bearer or-key')
+ expect(headers['X-Title']).toBe('Harness router')
+ const sent = JSON.parse(init.body as string)
+ expect(sent).toEqual({ model: 'typesafe/jev-1.13', ...body() })
+ })
+
+ it('never lets a caller pick the model', async () => {
+ const spy = jevSays(good)
+ const app = await build()
+ expect((await post(app, { ...body(), model: 'openai/gpt-5-pro' })).statusCode).toBe(200)
+ expect(JSON.parse((spy.mock.calls[0] as [string, RequestInit])[1].body as string).model).toBe('typesafe/jev-1.13')
+ })
+
+ it('fails closed with no key', async () => {
+ env.JEV_OPENROUTER_API_KEY = undefined
+ const spy = jevSays(good)
+ const app = await build()
+ const res = await post(app, body())
+ expect(res.statusCode).toBe(503)
+ expect(res.json().error.code).toBe('JEV_NOT_CONFIGURED')
+ expect(spy).not.toHaveBeenCalled()
+ })
+
+ it('names running out of credit apart from other failures', async () => {
+ const app = await build()
+ jevSays({ error: 'no credit' }, 402)
+ expect((await post(app, body())).json().error.code).toBe('JEV_CREDITS')
+ vi.restoreAllMocks()
+ jevSays({ error: 'busy' }, 429)
+ expect((await post(app, body())).json().error.code).toBe('JEV_BUSY')
+ vi.restoreAllMocks()
+ jevSays({ error: 'boom' }, 500)
+ expect((await post(app, body())).statusCode).toBe(502)
+ vi.restoreAllMocks()
+ jevSays('not json')
+ expect((await post(app, body())).statusCode).toBe(502)
+ vi.restoreAllMocks()
+ vi.spyOn(globalThis, 'fetch').mockRejectedValue(new Error('down') as never)
+ expect((await post(app, body())).json().error.code).toBe('JEV_UNAVAILABLE')
+ })
+
+ it('takes every decision the router can build: none, one session, the largest, a long paste', async () => {
+ const app = await build()
+ // A desk with no sessions offers only "new". The first ⌘B of a new user is exactly this.
+ jevSays({ answers: { target: { choice: 'new', probabilities: { new: 1 } } } })
+ const first = body({ questions: { target: { type: 'choice', instructions: 'Which session?', criteria: { new: 'None of them' } } } })
+ expect((await post(app, first)).json().data.answers.target.choice).toBe('new')
+ vi.restoreAllMocks()
+ const spy = jevSays(good)
+ expect((await post(app, largest())).statusCode).toBe(200)
+ // 16,000 characters of pasted JSON: 21k once JSON escapes it, and still one message.
+ const paste = '{"a":"b\n","c":[1,"\t"]}\n'.repeat(Math.floor(16_000 / 26))
+ expect(paste.length).toBeLessThanOrEqual(16_000)
+ expect(JSON.stringify(paste).length).toBeGreaterThan(17_000)
+ expect((await post(app, largest(paste))).statusCode).toBe(200)
+ expect(spy).toHaveBeenCalledTimes(2)
+ })
+
+ it('refuses decisions bigger than the router builds', async () => {
+ const spy = jevSays(good)
+ const app = await build()
+ const big = largest()
+ expect((await post(app, { ...big, state: { message: 'x'.repeat(16_701), previous_message_went_to: 'n'.repeat(300) } })).statusCode).toBe(400)
+ expect((await post(app, { ...big, state: { a: '1', b: '2', c: '3', d: '4', e: '5' } })).statusCode).toBe(400)
+ const many = Object.fromEntries(Array.from({ length: 49 }, (_, i) => [`s${i}`, 'option']))
+ expect((await post(app, body({ questions: { target: { type: 'choice', instructions: 'Which?', criteria: many } } }))).statusCode).toBe(400)
+ expect((await post(app, body({ questions: { target: { type: 'choice', instructions: 'Which?', criteria: { s0: 'x'.repeat(641) } } } }))).statusCode).toBe(400)
+ expect((await post(app, body({ questions: { target: { type: 'choice', instructions: 'x'.repeat(1_501), criteria: { s0: 'a' } } } }))).statusCode).toBe(400)
+ expect((await post(app, body({ questions: { ...big.questions, extra: big.questions.topic } }))).statusCode).toBe(400)
+ expect((await post(app, body({ questions: { target: { type: 'choice', instructions: 'Which?', criteria: {} } } }))).statusCode).toBe(400)
+ expect((await post(app, { state: { message: 'hi' }, questions: {} })).statusCode).toBe(400)
+ expect(spy).not.toHaveBeenCalled()
+ })
+
+ it('caps each account per minute, counted where it is told to count', async () => {
+ jevSays(good)
+ const count = vi.fn(localJevCounter())
+ const app = await build(count)
+ for (let i = 0; i < 30; i++) expect((await post(app, body())).statusCode).toBe(200)
+ expect((await post(app, body())).statusCode).toBe(429)
+ expect(count.mock.calls[0]![0]).toMatch(/^jev:m:u1:\d+$/)
+ })
+
+ it('stops everyone at the daily ceiling, as out of credit', async () => {
+ const spy = jevSays(good)
+ const app = await build(async (key) => (key.startsWith('jev:all:') ? 200_001 : 1))
+ const res = await post(app, body())
+ expect(res.statusCode).toBe(503)
+ expect(res.json().error.code).toBe('JEV_CREDITS')
+ expect(spy).not.toHaveBeenCalled()
+ })
+
+ it('still caps when the shared count fails, in this process', async () => {
+ jevSays(good)
+ vi.spyOn(logger, 'error').mockImplementation(() => logger)
+ const app = await build(async () => { throw new Error('redis down') })
+ for (let i = 0; i < 30; i++) expect((await post(app, body())).statusCode).toBe(200)
+ expect((await post(app, body())).statusCode).toBe(429)
+ })
+
+ it('never logs the words', async () => {
+ const lines: unknown[] = []
+ for (const level of ['info', 'error', 'warn', 'debug'] as const) {
+ vi.spyOn(logger, level).mockImplementation((...args: unknown[]) => { lines.push(args); return logger })
+ }
+ const app = await build()
+ const secret = { state: { message: 'the payroll export for ACME' }, questions: { target: { type: 'choice' as const, instructions: 'Which?', criteria: { s0: 'the ACME migration', new: 'none' } } } }
+ jevSays(good)
+ await post(app, secret)
+ vi.mocked(globalThis.fetch).mockRejectedValueOnce(new Error('down'))
+ await post(app, secret)
+ jevSays('not json')
+ await post(app, secret)
+ expect(lines.length).toBeGreaterThan(0)
+ expect(JSON.stringify(lines)).not.toMatch(/ACME|payroll/)
+ })
+})
+
+describe('jevAllowance', () => {
+ it('allows 30 a minute and 1,000 a day per account, under the shared ceiling', async () => {
+ let now = 0
+ const count = localJevCounter(() => now)
+ let allowed = 0
+ // Thirty a minute for an hour is 1,800 asks; the day stops it at 1,000.
+ for (let minute = 0; minute < 60; minute++, now += 60_000) {
+ for (let i = 0; i < 31; i++) if (await jevAllowance(count, 'u1', now, 10_000) === null) allowed++
+ }
+ expect(allowed).toBe(1_000)
+ expect(await jevAllowance(count, 'u2', now, 10_000)).toBeNull()
+ expect(await jevAllowance(count, 'u1', now + 24 * 60 * 60_000, 10_000)).toBeNull()
+ })
+
+ it("never spends the shared ceiling on an account's refused asks", async () => {
+ const count = localJevCounter(() => 0)
+ for (let i = 0; i < 100; i++) await jevAllowance(count, 'loop', 0, 40)
+ expect(await jevAllowance(count, 'u2', 0, 40)).toBeNull()
+ for (let i = 0; i < 9; i++) await jevAllowance(count, `u${i + 3}`, 0, 40)
+ expect(await jevAllowance(count, 'last', 0, 40)).toBe('everyone')
+ })
+})
+
+describe('parseJevAnswers', () => {
+ const { questions } = body()
+
+ it('keeps only answers to questions asked, with options offered', () => {
+ expect(parseJevAnswers({
+ answers: {
+ target: { choice: 's0', probabilities: { s0: 0.8, new: 0.2, s9: 0.5 } },
+ topic: { choice: 'maybe', probabilities: { maybe: 1 } },
+ extra: { choice: 'x', probabilities: { x: 1 } },
+ },
+ }, questions)).toEqual({ target: { choice: 's0', probabilities: { s0: 0.8, new: 0.2 } } })
+ })
+
+ it('is null when nothing usable came back', () => {
+ expect(parseJevAnswers(null, questions)).toBeNull()
+ expect(parseJevAnswers({ answers: 'no' }, questions)).toBeNull()
+ expect(parseJevAnswers({ answers: { target: { choice: 's0' } } }, questions)).toBeNull()
+ })
+})
diff --git a/backend/src/routes/jevRoute.ts b/backend/src/routes/jevRoute.ts
new file mode 100644
index 000000000..8bc00c586
--- /dev/null
+++ b/backend/src/routes/jevRoute.ts
@@ -0,0 +1,237 @@
+/**
+ * ⌘B's Jev decisions on Harness's own OpenRouter key.
+ *
+ * POST /api/route/jev SSO access token, { state, questions } → { answers }
+ *
+ * WHY. ⌘B asks Jev 1.13 (OpenRouter's Decisions API) where a typed task goes. The CLI used to need the
+ * person's own OpenRouter key, so for nearly everyone ⌘B said "no OpenRouter key" and sent nothing. A
+ * decision costs about $0.00017 (docs/design/2026-10-09-auto-router.md), so the owner chose to pay for
+ * them (2026-10-11). The key cannot ship in the CLI: this repository is public and the CLI runs on every
+ * user's computer. It stays here. A person with a key of their own still uses it directly
+ * (cli/src/lib/jev/jevClient.ts); this route is for everyone else.
+ *
+ * WHAT IS FIXED HERE. The model and the endpoint: a caller sends the decision, never a model name, so
+ * this key buys Jev decisions and nothing else. The size of a decision is bounded just above what the
+ * router builds (cli/src/lib/routeDecide.ts: a message of up to 16,000 characters, and up to four
+ * questions of at most 41 options of at most 600 characters), which is what bounds its price.
+ *
+ * WHAT IS NOT KEPT. The message and the session descriptions are relayed and dropped. The log carries the
+ * account, the time, the cost and the token count, never the words. Every request is OpenRouter-titled
+ * "Harness router": an unnamed app burned $60 on 2026-10-10 before anyone could tell which one it was.
+ *
+ * THE BUDGET is the key's credit limit on OpenRouter, with a daily ceiling across every account
+ * (JEV_DAILY_DECISIONS) under it. Each account is capped too, so one account or one runaway client cannot
+ * spend everyone's share: a person presses ⌘B, a loop does not. The caps count in Redis, shared by every
+ * process and pod; a process-local count would multiply by both and reset on every deploy.
+ */
+import type { FastifyInstance } from 'fastify'
+import { z } from 'zod'
+import { env } from '../config/env.js'
+import { validateBody } from '../middlewares/validation.js'
+import { sendSuccess, sendError } from '../utils/response.js'
+import { logger } from '../utils/logger.js'
+
+export const JEV_ROUTE_PATH = '/api/route/jev'
+
+const ENDPOINT = 'https://openrouter.ai/api/alpha/decisions'
+const MODEL = 'typesafe/jev-1.13'
+/** The CLI waits 8 s for Jev; one hop more, and this gives up first. */
+const TIMEOUT_MS = 7_000
+const MAX_REPLY_CHARS = 128_000
+
+/** The router's message is at most 16,000 characters; the previous session's name rides beside it. */
+const MAX_STATE_CHARS = 17_000
+const MAX_STATE_KEYS = 4
+/** The router asks target, topic, project and agent; 41 options at most (40 and "new"), each cut to 600. */
+const MAX_QUESTIONS = 4
+const MAX_OPTIONS = 48
+const MAX_OPTION_CHARS = 640
+const MAX_INSTRUCTION_CHARS = 1_500
+/** Bytes, for a request at those bounds in any script: enforced before the body is parsed. */
+const MAX_BODY_BYTES = 512 * 1024
+
+/** Per account: a burst of quick ⌘B presses passes, a client in a loop does not get far. */
+const MINUTE_MS = 60_000
+const PER_MINUTE = 30
+const DAY_MS = 24 * 60 * 60_000
+const PER_DAY = 1_000
+
+/** The count after this one, in a fixed window named by the key; the ttl only cleans up. */
+export type JevCounter = (key: string, ttlMs: number) => Promise<number>
+
+/** A count in this process alone: tests, and the fallback when the shared one fails. */
+export function localJevCounter(now: () => number = Date.now): JevCounter {
+ const counts = new Map<string, { count: number; expiresAt: number }>()
+ return async (key, ttlMs) => {
+ const at = now()
+ for (const [old, entry] of counts) if (entry.expiresAt <= at) counts.delete(old)
+ const entry = counts.get(key) ?? { count: 0, expiresAt: at + ttlMs }
+ entry.count++
+ counts.set(key, entry)
+ return entry.count
+ }
+}
+
+/**
+ * Whether one more decision may be asked: null when it may, or who is over. The account's caps are counted
+ * first, so asks refused for one account never spend the day's shared ceiling.
+ */
+export async function jevAllowance(count: JevCounter, userId: string, now: number, ceiling: number): Promise<'account' | 'everyone' | null> {
+ const minute = Math.floor(now / MINUTE_MS), day = Math.floor(now / DAY_MS)
+ if (await count(`jev:m:${userId}:${minute}`, MINUTE_MS) > PER_MINUTE) return 'account'
+ if (await count(`jev:d:${userId}:${day}`, DAY_MS) > PER_DAY) return 'account'
+ if (await count(`jev:all:${day}`, DAY_MS) > ceiling) return 'everyone'
+ return null
+}
+
+const own = (record: Record<string, unknown>, key: string): boolean => Object.hasOwn(record, key)
+
+const choiceSchema = z.object({
+ type: z.literal('choice'),
+ instructions: z.string().min(1).max(MAX_INSTRUCTION_CHARS),
+ // One option is a real question: a desk with no sessions offers only "new".
+ criteria: z
+ .record(z.string().min(1).max(64), z.string().max(MAX_OPTION_CHARS))
+ .refine((criteria) => {
+ const count = Object.keys(criteria).length
+ return count >= 1 && count <= MAX_OPTIONS
+ }, `between 1 and ${MAX_OPTIONS} options`),
+})
+
+const jevBodySchema = z.object({
+ // Characters as typed, not as JSON escapes them: a pasted 16,000-character stack trace is one message.
+ state: z
+ .record(z.string().max(64), z.string())
+ .refine((state) => Object.keys(state).length <= MAX_STATE_KEYS, 'state has too many fields')
+ .refine((state) => Object.values(state).reduce((sum, value) => sum + value.length, 0) <= MAX_STATE_CHARS, 'state is too long'),
+ questions: z
+ .record(z.string().min(1).max(64), choiceSchema)
+ .refine((questions) => {
+ const count = Object.keys(questions).length
+ return count >= 1 && count <= MAX_QUESTIONS
+ }, `between 1 and ${MAX_QUESTIONS} questions`),
+})
+
+export type JevBody = z.infer<typeof jevBodySchema>
+
+export interface JevAnswer {
+ choice: string
+ probabilities: Record<string, number>
+}
+
+/**
+ * Jev's answers, kept only where they answer a question that was asked with one of its own options.
+ * Null when nothing usable came back.
+ */
+export function parseJevAnswers(raw: unknown, questions: JevBody['questions']): Record<string, JevAnswer> | null {
+ const answers = (raw as { answers?: unknown } | null)?.answers
+ if (!answers || typeof answers !== 'object') return null
+ const out: Record<string, JevAnswer> = {}
+ for (const [id, value] of Object.entries(answers as Record<string, unknown>)) {
+ const question = own(questions, id) ? questions[id] : undefined
+ const answer = value as { choice?: unknown; probabilities?: unknown } | null
+ if (!question || typeof answer?.choice !== 'string' || !own(question.criteria, answer.choice)) continue
+ if (!answer.probabilities || typeof answer.probabilities !== 'object') continue
+ const probabilities: Record<string, number> = {}
+ for (const [option, p] of Object.entries(answer.probabilities as Record<string, unknown>)) {
+ if (own(question.criteria, option) && typeof p === 'number' && Number.isFinite(p)) probabilities[option] = p
+ }
+ out[id] = { choice: answer.choice, probabilities }
+ }
+ return Object.keys(out).length > 0 ? out : null
+}
+
+export interface JevRouteOptions {
+ /** Shared by every process (server.ts passes Redis); this process's own count when absent. */
+ count?: JevCounter
+ now?: () => number
+}
+
+/**
+ * ORDINARY SSO GATING, as /api/route/agents: the daemon signs in as a person and presents that access
+ * token; the middleware puts the account on `request.user`, which the caps count.
+ */
+export async function jevRouteRoutes(app: FastifyInstance, options: JevRouteOptions = {}): Promise<void> {
+ const now = options.now ?? Date.now
+ const local = localJevCounter(now)
+ const shared = options.count ?? local
+ // Redis down must not mean uncapped: count in this process until it answers again.
+ const count: JevCounter = (key, ttlMs) => shared(key, ttlMs).catch((err: unknown) => {
+ logger.error('jev-route: shared count failed, counting in this process', { error: err instanceof Error ? err.message : String(err) })
+ return local(key, ttlMs)
+ })
+
+ app.post<{ Body: JevBody }>(JEV_ROUTE_PATH, { bodyLimit: MAX_BODY_BYTES, preHandler: [validateBody(jevBodySchema)] }, async (req, reply) => {
+ // Fail CLOSED: an absent key must never read as "decide some other way" on a path that spends credit.
+ if (!env.JEV_OPENROUTER_API_KEY) {
+ logger.error('jev-route: JEV_OPENROUTER_API_KEY not configured — refusing')
+ sendError(reply, 'Jev is not set up on this server', 'JEV_NOT_CONFIGURED', 503)
+ return
+ }
+ const userId = req.user?.sub ?? ''
+ const over = await jevAllowance(count, userId, now(), env.JEV_DAILY_DECISIONS)
+ if (over === 'account') {
+ sendError(reply, 'Too many ⌘B decisions; try again in a minute', 'RATE_LIMITED', 429)
+ return
+ }
+ if (over === 'everyone') {
+ logger.error('jev-route: daily ceiling reached', { ceiling: env.JEV_DAILY_DECISIONS })
+ sendError(reply, "Harness's Jev budget for today has run out", 'JEV_CREDITS', 503)
+ return
+ }
+
+ const started = Date.now()
+ const { state, questions } = req.body
+ // A CLI that gave up has nobody waiting on the answer: stop paying for it.
+ const gone = new AbortController()
+ const onClose = () => { if (!reply.raw.writableFinished) gone.abort() }
+ reply.raw.once('close', onClose)
+ try {
+ const res = await fetch(ENDPOINT, {
+ method: 'POST',
+ signal: AbortSignal.any([AbortSignal.timeout(TIMEOUT_MS), gone.signal]),
+ headers: {
+ Authorization: `Bearer ${env.JEV_OPENROUTER_API_KEY}`,
+ 'Content-Type': 'application/json',
+ 'HTTP-Referer': 'https://harness.autonomous.ai',
+ 'X-Title': 'Harness router',
+ },
+ body: JSON.stringify({ model: MODEL, state, questions }),
+ })
+ if (!res.ok) {
+ await res.body?.cancel()
+ logger.error('jev-route: OpenRouter refused', { userId, status: res.status, ms: Date.now() - started })
+ // Out of credit is the one the owner must act on, and the CLI tells the person it is ours.
+ if (res.status === 402) sendError(reply, 'Harness is out of Jev credit', 'JEV_CREDITS', 503)
+ else if (res.status === 429) sendError(reply, 'Jev is busy', 'JEV_BUSY', 503)
+ else sendError(reply, 'Jev did not answer', 'JEV_UNAVAILABLE', 502)
+ return
+ }
+ const text = await res.text()
+ let parsed: unknown = null
+ if (text.length <= MAX_REPLY_CHARS) {
+ try { parsed = JSON.parse(text) } catch { /* reported below */ }
+ }
+ const answers = parseJevAnswers(parsed, questions)
+ if (!answers) {
+ logger.error('jev-route: unusable answer', { userId, chars: text.length, ms: Date.now() - started })
+ sendError(reply, 'Jev did not answer', 'JEV_UNAVAILABLE', 502)
+ return
+ }
+ const usage = (parsed as { usage?: { cost?: unknown; input_tokens?: unknown } }).usage
+ logger.info('jev-route', {
+ userId,
+ ms: Date.now() - started,
+ questions: Object.keys(questions).length,
+ cost: typeof usage?.cost === 'number' ? usage.cost : undefined,
+ tokens: typeof usage?.input_tokens === 'number' ? usage.input_tokens : undefined,
+ })
+ sendSuccess(reply, { answers })
+ } catch (err) {
+ logger.error('jev-route failed', { userId, ms: Date.now() - started, gone: gone.signal.aborted, error: err instanceof Error ? err.name : String(err) })
+ sendError(reply, 'Jev did not answer', 'JEV_UNAVAILABLE', 502)
+ } finally {
+ reply.raw.off('close', onClose)
+ }
+ })
+}
diff --git a/backend/src/server.ts b/backend/src/server.ts
index 8a569b8ec..a6f0813b3 100644
--- a/backend/src/server.ts
+++ b/backend/src/server.ts
@@ -22,7 +22,7 @@ import { experimentalSettingsRoutes } from './routes/experimentalSettings.js'
import { describeDaemonsSwitch } from './lib/daemonsSwitch.js'
import { handleObserverUpgrade } from './lib/observerWs.js'
import { deviceAuthRoutes } from './routes/deviceAuth.js'
-import { healthRoutes, authRoutes, userRoutes, machineRoutes, planRoutes, gridRoutes, deviceRoutes, mobileRoutes, appRoutes, analyticsRoutes, agentRouteRoutes, storeRoutes } from './routes/index.js'
+import { healthRoutes, authRoutes, userRoutes, machineRoutes, planRoutes, gridRoutes, deviceRoutes, mobileRoutes, appRoutes, analyticsRoutes, agentRouteRoutes, jevRouteRoutes, storeRoutes } from './routes/index.js'
import { startSubdomainProxy, startMeshProxy } from './lib/subdomainProxy.js'
import { handleDeviceUpgrade } from './lib/deviceWs.js'
import { handleWebUpgrade } from './lib/webWs.js'
@@ -30,7 +30,7 @@ import { handleManagerUpgrade } from './lib/managerWs.js'
import { handleAdapterUpgrade } from './lib/adapterWs.js'
import { logger } from './utils/logger.js'
import { startTurnCredentialRefresh } from './lib/turnCredentials.js'
-import { closeBus, redisSsoProfileStore } from './lib/bus.js'
+import { closeBus, redisCount, redisSsoProfileStore } from './lib/bus.js'
import { useSharedSsoProfileStore } from './lib/ssoAuth.js'
import { drainAllSockets, openSocketCount, RELEASE_UPGRADE_SLOT, type SlotSocket } from './lib/wsServer.js'
@@ -166,6 +166,7 @@ async function start(): Promise<void> {
await app.register(appRoutes)
await app.register(analyticsRoutes)
await app.register(agentRouteRoutes) // which agent a task belongs to — the prompt lives here, not in the CLI
+ await app.register(jevRouteRoutes, { count: redisCount }) // ⌘B's Jev decisions on Harness's key, for people with none of their own
await app.register(cursorRoutes) // standalone STT + LLM for the Cursor desktop app (self-contained)
// The cabled dial's transcription, authenticated by the caller's SSO token rather than a shared secret.
// Absent from the auth middleware's skip-list on purpose: that absence IS the gate.
diff --git a/cli/src/lib/jev/jevClient.spec.ts b/cli/src/lib/jev/jevClient.spec.ts
index b71d2cd67..fffcbf080 100644
--- a/cli/src/lib/jev/jevClient.spec.ts
+++ b/cli/src/lib/jev/jevClient.spec.ts
@@ -6,7 +6,7 @@ import { join } from 'node:path'
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { resolveOpenRouterKey } from '../openrouter.js'
-import { createJevDecide, JevUnavailable, resolveJevKey, type JevChoice } from './jevClient.js'
+import { createJevDecide, JevUnavailable, resolveJevKey, type AccountBackend, type JevChoice } from './jevClient.js'
// The env key and `ori login`'s file are openrouter.ts's to read, and its spec's to test: here they are none,
// unless a test says otherwise.
@@ -115,7 +115,7 @@ describe('asking Jev', () => {
expect(costs).toEqual([[0.000167, 3986], [0.0001, 0]])
})
- it('asks nothing without a key', async () => {
+ it('asks nothing without a key or a backend to ask', async () => {
const fetch = fetching()
const failure = await createJevDecide({ key: async () => null, fetch })(STATE, QUESTIONS).catch((error: unknown) => error)
expect(failure).toBeInstanceOf(JevUnavailable)
@@ -136,6 +136,14 @@ describe('asking Jev', () => {
await expect(createJevDecide()(STATE, QUESTIONS)).rejects.toMatchObject({ code: 'NO_KEY' })
})
+ it("prefers the person's own key over Harness's", async () => {
+ const fetch = fetching()
+ const backend = vi.fn<AccountBackend>(async () => ({ status: 200, body: {} }))
+ await createJevDecide({ key: async () => 'sk-or-fixture', backend, fetch })(STATE, QUESTIONS)
+ expect(backend).not.toHaveBeenCalled()
+ expect(fetch.mock.calls[0]![0]).toBe('https://openrouter.ai/api/alpha/decisions')
+ })
+
it.each([
[401, 'AUTH', 'OpenRouter refused the key'],
[403, 'AUTH', 'OpenRouter refused the key'],
@@ -183,3 +191,55 @@ describe('asking Jev', () => {
expect(await createJevDecide({ key: async () => 'sk-or-fixture', fetch: fetching('{"answers":{}}') })(STATE, QUESTIONS)).toEqual({})
})
})
+
+describe("asking Jev on Harness's key", () => {
+ const QUESTIONS: Record<string, JevChoice> = { target: { type: 'choice', instructions: 'Which?', criteria: { s0: 'billing', new: 'none' } } }
+ const STATE = { message: 'why was the card charged twice' }
+ const ANSWERS = { target: { choice: 's0', probabilities: { s0: 0.9, new: 0.1 } } }
+ const answering = (status: number, body: Record<string, unknown>) => vi.fn<AccountBackend>(async () => ({ status, body }))
+ const decide = (backend: AccountBackend, onCost?: (usd: number, tokens: number) => void) =>
+ createJevDecide({ key: async () => null, backend, onCost })
+
+ it('posts the state and the questions to the backend the core signs, and reads the answers', async () => {
+ const backend = answering(200, { success: true, data: { answers: ANSWERS } })
+ const onCost = vi.fn()
+ expect(await decide(backend, onCost)(STATE, QUESTIONS)).toEqual(ANSWERS)
+ // No model and no key from here: the backend fixes both.
+ expect(backend).toHaveBeenCalledWith('POST', '/api/route/jev', { state: STATE, questions: QUESTIONS })
+ // Harness pays, so nothing is charged to the person.
+ expect(onCost).not.toHaveBeenCalled()
+ })
+
+ it.each([
+ [401, { success: false, error: { code: 'NOT_SIGNED_IN' } }, 'NO_KEY', 'sign in to Harness to use ⌘B'],
+ [401, { success: false, error: { code: 'UNAUTHORIZED' } }, 'AUTH', 'Harness could not confirm your sign-in'],
+ [403, {}, 'AUTH', 'Harness could not confirm your sign-in'],
+ [502, { success: false, error: { code: 'AUTH_UNAVAILABLE' } }, 'UNAVAILABLE', 'Harness could not refresh your sign-in'],
+ [429, { success: false, error: { code: 'RATE_LIMITED' } }, 'BUSY', 'too many ⌘B decisions, try again in a minute'],
+ [503, { success: false, error: { code: 'JEV_CREDITS' } }, 'CREDITS', "Harness's Jev budget has run out"],
+ [503, { success: false, error: { code: 'JEV_BUSY' } }, 'BUSY', 'Jev is busy'],
+ [504, { success: false, error: { code: 'BACKEND_TIMEOUT' } }, 'UNAVAILABLE', "Harness's server did not answer in time"],
+ [502, { success: false, error: { code: 'BACKEND_UNREACHABLE' } }, 'UNAVAILABLE', "Harness's server cannot be reached"],
+ [503, { error: 'SERVICE_UNAVAILABLE' }, 'UNAVAILABLE', "Harness's server cannot be reached"],
+ [404, {}, 'UNAVAILABLE', "Harness's server cannot decide yet"],
+ [503, { success: false, error: { code: 'JEV_NOT_CONFIGURED' } }, 'UNAVAILABLE', "Harness's server answered 503"],
+ [502, { error: 7 }, 'UNAVAILABLE', "Harness's server answered 502"],
+ ])('names a %i (%j) as %s', async (status, body, code, message) => {
+ await expect(decide(answering(status, body))(STATE, QUESTIONS)).rejects.toMatchObject({ code, message })
+ })
+
+ it('refuses a reply with no answers', async () => {
+ await expect(decide(answering(200, { success: true, data: {} }))(STATE, QUESTIONS)).rejects.toMatchObject({ code: 'INVALID' })
+ await expect(decide(answering(200, {}))(STATE, QUESTIONS)).rejects.toMatchObject({ code: 'INVALID' })
+ })
+
+ it('stops waiting when the caller does: its slot is not held by a slow backend', async () => {
+ const never = vi.fn<AccountBackend>(() => new Promise(() => {}))
+ const caller = new AbortController()
+ const asked = decide(never)(STATE, QUESTIONS, caller.signal)
+ caller.abort(new Error('the window closed'))
+ await expect(asked).rejects.toThrow('the window closed')
+ // Already gone before it asked.
+ await expect(decide(never)(STATE, QUESTIONS, caller.signal)).rejects.toThrow('the window closed')
+ })
+})
diff --git a/cli/src/lib/jev/jevClient.ts b/cli/src/lib/jev/jevClient.ts
index 8c28e8286..1d3f5b330 100644
--- a/cli/src/lib/jev/jevClient.ts
+++ b/cli/src/lib/jev/jevClient.ts
@@ -5,9 +5,12 @@
* One POST answers several typed questions at once — which session, which project, which agent — in
* about 0.6 s, for about $0.0002 on a desk of thirty sessions (measured 2026-10-10).
*
- * The key is the person's OpenRouter key: OPENROUTER_API_KEY, the account `ori login` saved, or the
- * credentials file at ~/.config/typesafe/credentials (or $TYPESAFE_CREDENTIALS). It is read when a
- * decision is asked for and never logged.
+ * A person with an OpenRouter key of their own asks with it: OPENROUTER_API_KEY, the account `ori login`
+ * saved, or the credentials file at ~/.config/typesafe/credentials (or $TYPESAFE_CREDENTIALS). It is read
+ * when a decision is asked for and never logged. Everyone else asks on Harness's key, through the backend
+ * (backend/src/routes/jevRoute.ts): the owner chose to pay for these decisions (2026-10-11), and the key
+ * cannot ship here because this code is public. That request is the core's to sign (`core.account.backend`):
+ * the router holds no credential of its own.
*/
import { readFile } from 'node:fs/promises'
import { homedir } from 'node:os'
@@ -28,6 +31,13 @@ export class JevUnavailable extends Error {
constructor(readonly code: 'NO_KEY' | 'AUTH' | 'CREDITS' | 'BUSY' | 'UNAVAILABLE' | 'INVALID', message: string) { super(message) }
}
+/** A read or write of the account's backend, signed in by the core (`CoreApi['account']['backend']`). A
+ * failure is an answer, never a throw: 401 signed out, 502 unreachable, 504 too slow. */
+export type AccountBackend = (method: string, path: string, body?: unknown) => Promise<{ status: number; body: Record<string, unknown> }>
+
+/** Harness's Jev, on the backend. */
+export const HOSTED_JEV_PATH = '/api/route/jev'
+
/** The key, or null when this computer has none. */
export async function resolveJevKey(environment: NodeJS.ProcessEnv = process.env): Promise<string | null> {
const known = await resolveOpenRouterKey()
@@ -43,19 +53,79 @@ export async function resolveJevKey(environment: NodeJS.ProcessEnv = process.env
return null
}
+/** What a reply's answers say: each one that is a choice with probabilities. */
+function readAnswers(answers: unknown): Record<string, JevChoiceAnswer> {
+ if (!answers || typeof answers !== 'object') throw new JevUnavailable('INVALID', 'Jev gave no answers')
+ const out: Record<string, JevChoiceAnswer> = {}
+ for (const [id, raw] of Object.entries(answers as Record<string, unknown>)) {
+ const answer = raw as { choice?: unknown; probabilities?: unknown } | null
+ if (typeof answer?.choice !== 'string' || !answer.probabilities || typeof answer.probabilities !== 'object') continue
+ out[id] = { choice: answer.choice, probabilities: answer.probabilities as Record<string, number> }
+ }
+ return out
+}
+
+async function readJson(response: Response): Promise<unknown> {
+ const text = await response.text()
+ if (text.length > MAX_REPLY_BYTES) throw new JevUnavailable('INVALID', 'Jev answered too much')
+ try { return JSON.parse(text) } catch { throw new JevUnavailable('INVALID', 'Jev answered something that is not JSON') }
+}
+
+/** The backend's error code: the route's `{ error: { code } }`, or the core's own `{ error: 'CODE' }`. */
+function errorCode(body: Record<string, unknown>): string | undefined {
+ const error = body.error
+ if (typeof error === 'string') return error
+ const code = (error as { code?: unknown } | null | undefined)?.code
+ return typeof code === 'string' ? code : undefined
+}
+
+/** Why Harness's Jev did not answer, in the router's words. */
+function hostedRefusal(status: number, code: string | undefined): JevUnavailable {
+ if (code === 'NOT_SIGNED_IN') return new JevUnavailable('NO_KEY', 'sign in to Harness to use ⌘B')
+ if (status === 401 || status === 403) return new JevUnavailable('AUTH', 'Harness could not confirm your sign-in')
+ // Signed in, but the refresh could not be made just now: not a reason to tell anyone to sign in.
+ if (code === 'AUTH_UNAVAILABLE') return new JevUnavailable('UNAVAILABLE', 'Harness could not refresh your sign-in')
+ if (status === 429) return new JevUnavailable('BUSY', 'too many ⌘B decisions, try again in a minute')
+ if (code === 'JEV_CREDITS') return new JevUnavailable('CREDITS', "Harness's Jev budget has run out")
+ if (code === 'JEV_BUSY') return new JevUnavailable('BUSY', 'Jev is busy')
+ if (code === 'BACKEND_TIMEOUT') return new JevUnavailable('UNAVAILABLE', "Harness's server did not answer in time")
+ if (code === 'BACKEND_UNREACHABLE' || code === 'SERVICE_UNAVAILABLE') return new JevUnavailable('UNAVAILABLE', "Harness's server cannot be reached")
+ // An older backend, before the route: the CLI self-updates on its own schedule and can arrive first.
+ if (status === 404) return new JevUnavailable('UNAVAILABLE', "Harness's server cannot decide yet")
+ return new JevUnavailable('UNAVAILABLE', `Harness's server answered ${status}`)
+}
+
+/** [work], or the reason [signal] gives once it fires: the caller's slot is freed when it stops waiting. */
+function bounded<T>(work: Promise<T>, signal: AbortSignal): Promise<T> {
+ if (signal.aborted) return Promise.reject(signal.reason)
+ return new Promise<T>((resolve, reject) => {
+ const stop = () => reject(signal.reason)
+ signal.addEventListener('abort', stop, { once: true })
+ work.then(resolve, reject).finally(() => signal.removeEventListener('abort', stop))
+ })
+}
+
export function createJevDecide(options: {
key?: () => Promise<string | null>
+ /** The account's backend, signed in by the core: Harness's key, for a computer with none of its own. */
+ backend?: AccountBackend
fetch?: typeof fetch
- /** What each answer cost, as OpenRouter reports it (USD) — what a person's key is charged. */
+ /** What each answer cost, as OpenRouter reports it (USD) — what a person's own key is charged. */
onCost?: (usd: number, inputTokens: number) => void
} = {}): JevDecide {
return async (state, questions, signal) => {
const key = await (options.key ?? resolveJevKey)()
- if (!key) throw new JevUnavailable('NO_KEY', 'no OpenRouter key on this computer')
const timeout = AbortSignal.timeout(TIMEOUT_MS)
+ const abort = signal ? AbortSignal.any([signal, timeout]) : timeout
+ if (!key) {
+ if (!options.backend) throw new JevUnavailable('NO_KEY', 'no OpenRouter key on this computer')
+ const answer = await bounded(options.backend('POST', HOSTED_JEV_PATH, { state, questions }), abort)
+ if (answer.status !== 200) throw hostedRefusal(answer.status, errorCode(answer.body))
+ return readAnswers((answer.body.data as { answers?: unknown } | undefined)?.answers)
+ }
const response = await (options.fetch ?? fetch)(ENDPOINT, {
method: 'POST',
- signal: signal ? AbortSignal.any([signal, timeout]) : timeout,
+ signal: abort,
headers: {
Authorization: `Bearer ${key}`, 'Content-Type': 'application/json',
'HTTP-Referer': 'https://harness.autonomous.ai', 'X-Title': 'Harness router',
@@ -69,20 +139,10 @@ export function createJevDecide(options: {
if (response.status === 429) throw new JevUnavailable('BUSY', 'OpenRouter is busy')
throw new JevUnavailable('UNAVAILABLE', `OpenRouter answered ${response.status}`)
}
- const text = await response.text()
- if (text.length > MAX_REPLY_BYTES) throw new JevUnavailable('INVALID', 'Jev answered too much')
- let parsed: unknown
- try { parsed = JSON.parse(text) } catch { throw new JevUnavailable('INVALID', 'Jev answered something that is not JSON') }
- const answers = (parsed as { answers?: unknown } | null)?.answers
- if (!answers || typeof answers !== 'object') throw new JevUnavailable('INVALID', 'Jev gave no answers')
+ const parsed = await readJson(response)
+ const answers = readAnswers((parsed as { answers?: unknown } | null)?.answers)
const usage = (parsed as { usage?: { cost?: unknown; input_tokens?: unknown } }).usage
if (typeof usage?.cost === 'number') options.onCost?.(usage.cost, typeof usage.input_tokens === 'number' ? usage.input_tokens : 0)
- const out: Record<string, JevChoiceAnswer> = {}
- for (const [id, raw] of Object.entries(answers as Record<string, unknown>)) {
- const answer = raw as { choice?: unknown; probabilities?: unknown } | null
- if (typeof answer?.choice !== 'string' || !answer.probabilities || typeof answer.probabilities !== 'object') continue
- out[id] = { choice: answer.choice, probabilities: answer.probabilities as Record<string, number> }
- }
- return out
+ return answers
}
}
diff --git a/cli/src/services/router.spec.ts b/cli/src/services/router.spec.ts
index fc784c77b..07ea92924 100644
--- a/cli/src/services/router.spec.ts
+++ b/cli/src/services/router.spec.ts
@@ -258,11 +258,16 @@ describe('the router', () => {
})
describe('the router this computer runs', () => {
- it('asks Jev through the person\'s key, and logs as [route]', () => {
+ it('asks Jev through the person\'s key or the account\'s backend, and logs as [route]', async () => {
const log = vi.spyOn(console, 'log').mockImplementation(() => {})
- const made = defaultRouter()
+ const core = fakeCore()
+ const backend = vi.spyOn(core.account, 'backend').mockResolvedValue({ status: 200, body: {} })
+ const made = defaultRouter(core)
// No key or fetch of its own: the client finds the person's key when it is asked, and not before.
- expect(createJevDecide).toHaveBeenCalledWith({ onCost: expect.any(Function) })
+ expect(createJevDecide).toHaveBeenCalledWith({ backend: expect.any(Function), onCost: expect.any(Function) })
+ // Harness's key is the core's to sign for: the router holds no credential.
+ await vi.mocked(createJevDecide).mock.calls[0]![0]!.backend!('POST', '/api/route/jev', { state: {} })
+ expect(backend).toHaveBeenCalledWith('POST', '/api/route/jev', { state: {} })
expect(made.jev).toEqual(expect.any(Function))
expect(Object.keys(made).sort()).toEqual(['jev', 'log'])
made.log!('session — 2 sessions')
diff --git a/cli/src/services/router.ts b/cli/src/services/router.ts
index 8defce8ed..eaa45bb04 100644
--- a/cli/src/services/router.ts
+++ b/cli/src/services/router.ts
@@ -10,7 +10,8 @@
* holds no session list of its own, and reaches no other machine: a client that can see a session on
* another computer can route to it.
*
- * It asks Jev through the person's OpenRouter key (lib/jev/); the decision is lib/routeDecide.ts.
+ * It asks Jev through the person's own OpenRouter key, or on Harness's key through the backend for a person
+ * with none (lib/jev/); the decision is lib/routeDecide.ts.
*/
import type { CoreApi, ServiceRequests } from '../core/api.js'
import { createJevDecide } from '../lib/jev/jevClient.js'
@@ -65,14 +66,21 @@ export function routeInput(payload: Record<string, unknown>): RouteInput | null
export type Router = RouteDeps
-/** Jev through the person's key, and the router's log. */
-export function defaultRouter(): Router {
+/** Jev, on the person's key or on Harness's through the account's backend, which the core signs; and the
+ * router's log. */
+export function defaultRouter(core: CoreApi): Router {
const log = (line: string): void => console.log(`[route] ${line}`)
- // Every answer's price, as OpenRouter charged it: the person pays for these with their own key.
- return { jev: createJevDecide({ onCost: (usd, tokens) => log(`jev cost $${usd.toFixed(6)} for ${tokens} tokens`) }), log }
+ return {
+ jev: createJevDecide({
+ backend: (method, path, body) => core.account.backend(method, path, body),
+ // Every answer's price on the person's own key, as OpenRouter charged it. On Harness's key the backend logs it.
+ onCost: (usd, tokens) => log(`jev cost $${usd.toFixed(6)} for ${tokens} tokens`),
+ }),
+ log,
+ }
}
-export function startRouter(_core: CoreApi, router: Router = defaultRouter()): ServiceRequests {
+export function startRouter(core: CoreApi, router: Router = defaultRouter(core)): ServiceRequests {
/** Decisions in flight, each with the connection that asked: what the limits count. */
const pending = new Map<object, unknown>()
return {
diff --git a/cli/src/services/routerProcess.spec.ts b/cli/src/services/routerProcess.spec.ts
index 2286952b1..5e646bfbf 100644
--- a/cli/src/services/routerProcess.spec.ts
+++ b/cli/src/services/routerProcess.spec.ts
@@ -1,6 +1,7 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { runServiceProcess, type ServiceProcessOptions } from './process.js'
import { runRouterService } from './routerProcess.js'
+import type { CoreApi } from '../core/api.js'
// The real default reaches a real socket: never in a test. The real router it starts by default builds Jev's
// client, and asks it nothing until a request comes.
@@ -25,6 +26,24 @@ describe('the router in its own process', () => {
expect(await options!.requests.route_decide!({ text: 'go' }, { local: true, owner: true })).toEqual({ decided: 'session', id: 'a1', via: 'jev' })
})
+ it("asks the core to sign Harness's backend calls, while it is connected", async () => {
+ let options: ServiceProcessOptions | null = null
+ let core: CoreApi | null = null
+ runRouterService({
+ dataDir: '/data', socketPath: '/data/daemon-1.sock', machineId: 'm', token: 't',
+ run: (given) => { options = given; return { stop: vi.fn() } },
+ start: (given) => { core = given; return {} },
+ })
+ // Not connected yet: an answer, never a throw.
+ expect(await core!.account.backend('POST', '/api/route/jev', { state: {} })).toEqual({ status: 503, body: { error: 'SERVICE_UNAVAILABLE' } })
+ const query = vi.fn(async () => ({ status: 200, body: { success: true } }))
+ options!.onConnected!({ query } as never)
+ expect(await core!.account.backend('POST', '/api/route/jev', { state: {} })).toEqual({ status: 200, body: { success: true } })
+ expect(query).toHaveBeenCalledWith('backend', { method: 'POST', path: '/api/route/jev', body: { state: {} } })
+ options!.onDisconnected!()
+ expect((await core!.account.backend('POST', '/api/route/jev')).status).toBe(503)
+ })
+
it('runs as a real service by default', () => {
runRouterService({ dataDir: '/data', socketPath: '/data/daemon-1.sock', machineId: 'm', token: 't' })
expect(runServiceProcess).toHaveBeenCalledWith(expect.objectContaining({
diff --git a/cli/src/services/routerProcess.ts b/cli/src/services/routerProcess.ts
index 5cc57cd78..77dca6d45 100644
--- a/cli/src/services/routerProcess.ts
+++ b/cli/src/services/routerProcess.ts
@@ -1,10 +1,10 @@
/**
* The router in its own process (`harness __service router`): the same handler as in the core's process
- * (services/router.ts). It asks the core nothing; the OpenRouter key it reads and the Jev calls it makes
- * are this process's alone.
+ * (services/router.ts). The person's own OpenRouter key and the Jev calls made with it are this process's
+ * alone; a call on Harness's key goes through the core, which holds the sign-in (`backend`).
*/
import { startRouter } from './router.js'
-import { runServiceProcess, type ServiceProcess } from './process.js'
+import { runServiceProcess, type CoreConnection, type ServiceProcess } from './process.js'
import { processCoreApi } from './processCoreApi.js'
export interface RouterServiceOptions {
@@ -19,11 +19,16 @@ export interface RouterServiceOptions {
}
export function runRouterService(options: RouterServiceOptions): ServiceProcess {
+ let core: CoreConnection | null = null
+ const ask = (query: string, payload: Record<string, unknown>): Promise<Record<string, unknown>> =>
+ core ? core.query(query, payload) : Promise.reject(new Error('not connected to the core'))
return (options.run ?? runServiceProcess)({
name: 'router',
socketPath: options.socketPath,
machineId: options.machineId,
token: options.token,
- requests: (options.start ?? startRouter)(processCoreApi(options.dataDir, 'router')),
+ requests: (options.start ?? startRouter)(processCoreApi(options.dataDir, 'router', { ask })),
+ onConnected: (connection) => { core = connection },
+ onDisconnected: () => { core = null },
})
}
diff --git a/docs/design/2026-10-09-auto-router.md b/docs/design/2026-10-09-auto-router.md
index 02aaf3651..29b90aaa7 100644
--- a/docs/design/2026-10-09-auto-router.md
+++ b/docs/design/2026-10-09-auto-router.md
@@ -16,7 +16,7 @@ task goes and does it, without a list and without a question:
permission mode last chosen for that agent), in the project and with the agent
the router chose. A line says which. A project that cannot be read opens New
Harness on the task instead of guessing.
-- **Nothing decided** — Jev cannot be reached (no key, offline, no credit) or the
+- **Nothing decided** — Jev cannot be reached (signed out, offline, out of credit) or the
router fails: the box says so and sends nothing.
There is no undo; the owner chose instant sends. Esc while the session's pane comes
@@ -75,9 +75,20 @@ elsewhere), so the bar stands. The log
records the runner-up with every decision, to set these numbers from real use. The session the person last sent
to (within ten minutes) is told to Jev, so a short follow-up finds it.
-The key is the person's OpenRouter key (`OPENROUTER_API_KEY`, `ori login`, or
-`~/.config/typesafe/credentials`). The router logs OpenRouter's reported cost of
-every answer.
+**Harness pays** (owner, 2026-10-11): at about $0.00017 a decision, a person who presses ⌘B a hundred
+times a day costs about $0.50 a month. A computer with no OpenRouter key of its own asks the backend's
+`POST /api/route/jev` (`backend/src/routes/jevRoute.ts`) through `core.account.backend`, so the core signs
+it and the router holds no credential. The backend asks Jev with Harness's key (`JEV_OPENROUTER_API_KEY`).
+The key never ships in the CLI, because this repository is public. The backend fixes the model, bounds a
+decision just above the largest the router builds (about 73 KB, 4 questions of 41 options), caps each
+account at 30 a minute and 1,000 a day and everyone at `JEV_DAILY_DECISIONS` a day (counted in Redis,
+shared by every process and pod), and logs the cost but never the words. The budget is the key's credit
+limit on OpenRouter; the OpenRouter account should keep prompt logging and training off, since every
+person's message and session descriptions now go out under it.
+
+A person with a key of their own (`OPENROUTER_API_KEY`, `ori login`, or
+`~/.config/typesafe/credentials`) still asks Jev directly with it, one hop shorter. The router logs
+OpenRouter's reported cost of every answer on that key.
## What was measured