feat(mobile): add safe Codex rate-limit resets (#9394)
* feat(mobile): add safe Codex rate-limit resets * fix(mobile): address reset credit review feedback * review: purge removed-account reset attempts, shared capability constant, rebase test mocks * review: preserve host compatibility and reset durability * fix(mobile): recover reset capability after cutover * fix(mobile): validate runtime capability payloads * fix(mobile): enforce capability payload contract * fix(mobile): route mock terminals to selected worktree * test(mobile): pin malformed probe retry behavior --------- Co-authored-by: Brennan Benson <79079362+brennanb2025@users.noreply.github.com>
This commit is contained in:
co-authored by
Brennan Benson
parent
402b5a1cc8
commit
4a71a0ecb2
@@ -27,6 +27,96 @@ describe('account RPC methods', () => {
|
||||
expect(runtime.refreshAccountsForMobile).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('forwards a client idempotency key when consuming a Codex reset credit', async () => {
|
||||
const idempotencyKey = '11111111-1111-4111-8111-111111111111'
|
||||
const expectedScope = {
|
||||
target: { runtime: 'host' as const, wslDistro: null },
|
||||
accountId: 'codex-account',
|
||||
accountRevision: 42,
|
||||
offerRevision: 'v1:offer'
|
||||
}
|
||||
const result = {
|
||||
outcome: 'reset',
|
||||
scope: expectedScope,
|
||||
snapshot: { claude: null, codex: null }
|
||||
}
|
||||
const consumeCodexRateLimitResetCredit = vi.fn().mockResolvedValue(result)
|
||||
const runtime = { consumeCodexRateLimitResetCredit } as unknown as OrcaRuntimeService
|
||||
const reset = method('accounts.consumeCodexResetCredit')
|
||||
if (isStreamingMethod(reset)) {
|
||||
throw new Error('accounts.consumeCodexResetCredit must be a request method')
|
||||
}
|
||||
|
||||
expect(reset.params?.parse({ idempotencyKey, expectedScope })).toEqual({
|
||||
idempotencyKey,
|
||||
expectedScope
|
||||
})
|
||||
expect(() => reset.params?.parse({ idempotencyKey: 'not-a-uuid', expectedScope })).toThrow()
|
||||
expect(() =>
|
||||
reset.params?.parse({
|
||||
idempotencyKey,
|
||||
expectedScope: {
|
||||
...expectedScope,
|
||||
target: { runtime: 'host', wslDistro: 'Ubuntu' }
|
||||
}
|
||||
})
|
||||
).toThrow()
|
||||
expect(() =>
|
||||
reset.params?.parse({
|
||||
idempotencyKey,
|
||||
expectedScope: {
|
||||
...expectedScope,
|
||||
target: { runtime: 'wsl', wslDistro: null }
|
||||
}
|
||||
})
|
||||
).toThrow()
|
||||
expect(() => reset.params?.parse({ idempotencyKey, expectedScope, extra: true })).toThrow()
|
||||
await expect(reset.handler({ idempotencyKey, expectedScope }, { runtime })).resolves.toBe(
|
||||
result
|
||||
)
|
||||
expect(consumeCodexRateLimitResetCredit).toHaveBeenCalledWith(idempotencyKey, expectedScope)
|
||||
})
|
||||
|
||||
it('forwards the exact WSL target when selecting a Codex account', async () => {
|
||||
const selectCodexAccountForTarget = vi
|
||||
.fn()
|
||||
.mockResolvedValue({ accounts: [], activeAccountId: null })
|
||||
const runtime = { selectCodexAccountForTarget } as unknown as OrcaRuntimeService
|
||||
const select = method('accounts.selectCodexForTarget')
|
||||
if (isStreamingMethod(select)) {
|
||||
throw new Error('accounts.selectCodexForTarget must be a request method')
|
||||
}
|
||||
const params = {
|
||||
accountId: null,
|
||||
target: { runtime: 'wsl' as const, wslDistro: 'Ubuntu' }
|
||||
}
|
||||
|
||||
expect(select.params?.parse(params)).toEqual(params)
|
||||
expect(
|
||||
select.params?.parse({
|
||||
accountId: null,
|
||||
target: { runtime: 'wsl', wslDistro: null }
|
||||
})
|
||||
).toEqual({ accountId: null, target: { runtime: 'wsl', wslDistro: null } })
|
||||
expect(() =>
|
||||
select.params?.parse({
|
||||
accountId: null,
|
||||
target: { runtime: 'host', wslDistro: 'Ubuntu' }
|
||||
})
|
||||
).toThrow()
|
||||
expect(() =>
|
||||
select.params?.parse({
|
||||
accountId: null,
|
||||
target: { runtime: 'wsl', wslDistro: ' ' }
|
||||
})
|
||||
).toThrow()
|
||||
await expect(select.handler(params, { runtime })).resolves.toEqual({
|
||||
accounts: [],
|
||||
activeAccountId: null
|
||||
})
|
||||
expect(selectCodexAccountForTarget).toHaveBeenCalledWith(null, params.target)
|
||||
})
|
||||
|
||||
it('uses a stale-aware refresh when a connection replays the subscription', async () => {
|
||||
const snapshot = { claude: null, codex: null }
|
||||
let cleanup: (() => void) | undefined
|
||||
|
||||
@@ -7,16 +7,55 @@ import { defineMethod, defineStreamingMethod, type RpcAnyMethod } from '../core'
|
||||
// registerSubscriptionCleanup's existing-key eviction path.
|
||||
let accountsSubscriptionSeq = 0
|
||||
|
||||
const CodexResetTarget = z.discriminatedUnion('runtime', [
|
||||
z.object({ runtime: z.literal('host'), wslDistro: z.null() }).strict(),
|
||||
// Why: reset scope must identify one exact WSL distro; null means all slots only for selection.
|
||||
z.object({ runtime: z.literal('wsl'), wslDistro: z.string().trim().min(1).max(255) }).strict()
|
||||
])
|
||||
|
||||
const CodexSelectionTarget = z.discriminatedUnion('runtime', [
|
||||
z.object({ runtime: z.literal('host'), wslDistro: z.null() }).strict(),
|
||||
z
|
||||
.object({
|
||||
runtime: z.literal('wsl'),
|
||||
// A null distro intentionally means all WSL selection slots.
|
||||
wslDistro: z.string().trim().min(1).max(255).nullable()
|
||||
})
|
||||
.strict()
|
||||
])
|
||||
|
||||
const SelectAccountParams = z.object({
|
||||
accountId: z
|
||||
.union([z.string().min(1, 'Missing accountId'), z.null()])
|
||||
.transform((v) => (v === null ? null : v))
|
||||
})
|
||||
|
||||
const SelectCodexAccountForTargetParams = SelectAccountParams.extend({
|
||||
target: CodexSelectionTarget
|
||||
})
|
||||
|
||||
const RemoveAccountParams = z.object({
|
||||
accountId: z.string().min(1, 'Missing accountId')
|
||||
})
|
||||
|
||||
const CodexResetExpectedScope = z
|
||||
.object({
|
||||
target: CodexResetTarget,
|
||||
accountId: z.string().min(1, 'Missing accountId').max(512),
|
||||
accountRevision: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
|
||||
offerRevision: z.string().startsWith('v1:', 'Invalid offerRevision').max(4_096)
|
||||
})
|
||||
.strict()
|
||||
|
||||
const ConsumeCodexResetCreditParams = z
|
||||
.object({
|
||||
// Why: the phone owns the logical attempt key so a lost response can be
|
||||
// retried without spending a finite earned credit twice.
|
||||
idempotencyKey: z.uuid('Invalid idempotencyKey'),
|
||||
expectedScope: CodexResetExpectedScope
|
||||
})
|
||||
.strict()
|
||||
|
||||
const AccountsUnsubscribeParams = z.object({
|
||||
subscriptionId: z
|
||||
.unknown()
|
||||
@@ -52,6 +91,20 @@ export const ACCOUNT_METHODS: readonly RpcAnyMethod[] = [
|
||||
params: SelectAccountParams,
|
||||
handler: async (params, { runtime }) => runtime.selectCodexAccount(params.accountId)
|
||||
}),
|
||||
defineMethod({
|
||||
// Why: old hosts silently strip unknown target fields from selectCodex.
|
||||
// A distinct RPC makes version skew fail before it can clear the host slot.
|
||||
name: 'accounts.selectCodexForTarget',
|
||||
params: SelectCodexAccountForTargetParams,
|
||||
handler: async (params, { runtime }) =>
|
||||
runtime.selectCodexAccountForTarget(params.accountId, params.target)
|
||||
}),
|
||||
defineMethod({
|
||||
name: 'accounts.consumeCodexResetCredit',
|
||||
params: ConsumeCodexResetCreditParams,
|
||||
handler: async (params, { runtime }) =>
|
||||
runtime.consumeCodexRateLimitResetCredit(params.idempotencyKey, params.expectedScope)
|
||||
}),
|
||||
defineMethod({
|
||||
name: 'accounts.removeClaude',
|
||||
params: RemoveAccountParams,
|
||||
|
||||
Reference in New Issue
Block a user