fix(artifacts): explain artifact API rate limits and replay once on a short Retry-After (#42882)

* fix(artifacts): explain artifact API rate limits and retry once on a short Retry-After

A 429 from the artifact API now carries the server's message and its reset
time in local time to the CLI and the desktop share toast; older servers'
bare 429s say the request was rate limited and when to retry. A Retry-After
of 60s or less replays the request once with the same Idempotency-Key;
longer waits fail fast and keep the pending create intent so a later share
reuses its key.

* fix(artifacts): cancel a rate-limit replay when the account changed during the wait

A Retry-After replay waited up to 60s and then resent with the token captured
before the wait, so a sign-out, account switch or revoked session in between
could still create, update or delete an artifact under the old account. Every
artifact request now passes a check that runs immediately before the replay:
the profile, its cloud link and share lifecycle must be unchanged and, for a
stored session, the session must still be signed in. A request without the
check never replays.

The auth-context builders move unchanged into artifact-cloud-auth-context.ts to
keep the service under its line budget.

* fix(artifacts): cancel a rate-limit replay once sign-out has started

Sign-out advances the session-owner generation before it awaits remote logout,
but only marks the session signed out and unlinks the profile afterwards. A
Retry-After replay that came due during that wait still passed the account
checks and resent with the old token. The replay check now also requires the
session generation captured with the request to be unchanged; token rotation
does not advance it, so normal refreshes still replay.

* refactor(cli): keep the artifact timeout floor in the request-timeout module

Main grew the CLI client to its 300-line budget, so the artifact timeout floor
now lives beside the other per-method timeout helpers and the client reaches
them through its existing import, adding no module to the CLI's load path.

* fix(artifacts): keep one rate-limit wait per operation across a 401 session refresh

A 401 makes the session refresh re-run the whole artifact operation, and each
run got a fresh 429 wait-and-replay. So 429 -> 401 -> 429 -> 200 sent four
POSTs with the same Idempotency-Key, and a reconciled create plus its update
could wait twice and outrun the CLI's 210s request timeout.

The allowance is now created once per operation, before the refresh re-run,
and every request in the operation draws from it. 429 -> 401 -> 200 still
finishes in three calls; 429 -> 401 -> 429 surfaces the second 429 without
another wait. The CLI timeout is now the sum of the most requests one share
can send (five), the single wait and both session refreshes, still 210s.

* test(artifacts): drop timeout tests that pinned production constants and timing

Addresses FARTIFACT429-C4-B1: remove the CLI artifact timeout arithmetic test and
the slow re-run timing/work-count test, plus the imports and stub latency/update
helpers only they used. No production code changes.

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Jinjing
2026-11-16 22:35:27 -08:00
committed by GitHub
co-authored by Claude
parent 80974be81c
commit 75f07139fa
14 changed files with 1230 additions and 109 deletions
+5 -5
View File
@@ -40,7 +40,7 @@ import {
} from '../../shared/orchestration/orchestration-runtime-capabilities'
import { RemoteRuntimeCompatGate } from './connection/remote-runtime-compat-gate'
import { OrchestrationRequestEvidence } from '../orchestration/identity/orchestration-compatibility-envelope'
import { getTimeoutMsParam, isWaitingCheck } from './connection/runtime-request-timeout'
import * as requestTimeout from './connection/runtime-request-timeout'
import {
isWorkerStartTimeoutWithinTimerLimit,
resolveWorkerStartClientTimeoutMs,
@@ -201,7 +201,7 @@ export class RuntimeClient {
// Give server-side waits time to finish before the client transport times out.
private resolveMethodTimeoutMs(method: string, params?: unknown): number {
if (method === 'orchestration.workerStart') {
const requestedValue = getTimeoutMsParam(params)
const requestedValue = requestTimeout.getTimeoutMsParam(params)
const requested = typeof requestedValue === 'number' ? requestedValue : Number(requestedValue)
if (!isWorkerStartTimeoutWithinTimerLimit(requested)) {
throw new RuntimeClientError(
@@ -217,15 +217,15 @@ export class RuntimeClient {
return Math.max(resolveWorkerStartClientTimeoutMs(), this.requestTimeoutMs)
}
if (
(method === 'orchestration.check' && isWaitingCheck(params)) ||
(method === 'orchestration.check' && requestTimeout.isWaitingCheck(params)) ||
method === 'terminal.wait'
) {
const inner = Number(getTimeoutMsParam(params))
const inner = Number(requestTimeout.getTimeoutMsParam(params))
if (Number.isFinite(inner) && inner > 0) {
return Math.max(inner + LONG_POLL_CLIENT_GRACE_MS, this.requestTimeoutMs)
}
}
return this.requestTimeoutMs
return Math.max(requestTimeout.artifactMethodTimeoutMs(method), this.requestTimeoutMs)
}
async getCliStatus(): Promise<RuntimeRpcSuccess<CliStatusResult>> {
@@ -1,3 +1,5 @@
import { ARTIFACT_CLI_REQUEST_TIMEOUT_MS } from '../../../shared/artifacts/artifacts'
export function isWaitingCheck(params: unknown): boolean {
return (
typeof params === 'object' &&
@@ -13,3 +15,8 @@ export function getTimeoutMsParam(params: unknown): unknown {
}
return (params as { timeoutMs?: unknown }).timeoutMs
}
/** The desktop may wait once on an artifact API rate limit before replaying, so allow for it. */
export function artifactMethodTimeoutMs(method: string): number {
return method.startsWith('artifacts.') ? ARTIFACT_CLI_REQUEST_TIMEOUT_MS : 0
}
@@ -0,0 +1,147 @@
import { createHash } from 'node:crypto'
import { ensureActiveOrcaProfile } from '../orca-profiles/profile-index-store'
import type { ActiveOrcaProfileState } from '../orca-profiles/profile-index-store'
import {
readCloudSession,
takeCloudSessionTicket
} from '../orca-profiles/cloud-session/profile-cloud-session-owner'
import {
type ArtifactShareScope,
captureArtifactShareLifecycle,
isArtifactShareLifecycleCurrent
} from './artifact-share-record-store'
export type ArtifactRateLimitReplay = {
/** Takes the operation's one 429 wait-and-replay; false once any of its requests used it. */
claim: () => boolean
/** Run before the delayed replay: the token must still belong to a signed-in, unchanged account. */
assertCanReplay: () => void
}
/**
* One 429 wait-and-replay for a whole artifact operation. Create it before the 401
* session refresh, which re-runs the operation; a re-run must not earn another wait.
*/
export type ArtifactRateLimitAllowance = { claim: () => boolean }
export function createArtifactRateLimitAllowance(): ArtifactRateLimitAllowance {
let claimed = false
return {
claim: () => {
if (claimed) {
return false
}
claimed = true
return true
}
}
}
export type ArtifactAuthContext = {
profileId: string
scope: ArtifactShareScope
assertCurrent: () => void
rateLimitReplay: ArtifactRateLimitReplay
}
function tokenFingerprint(token: string): string {
return createHash('sha256').update(token).digest('hex')
}
function authContext(
active: ActiveOrcaProfileState,
scope: ArtifactShareScope,
userDataPath: string,
allowance: ArtifactRateLimitAllowance,
expectedCloud?: { userId: string; profileId: string; organizationId: string }
): ArtifactAuthContext {
const lifecycleGeneration = captureArtifactShareLifecycle(active.profile.id, userDataPath)
// Sign-in and sign-out advance this before any network wait; token rotation does not.
const sessionGeneration = takeCloudSessionTicket(active.profile.id, userDataPath).generation
const assertCurrent = (): void => {
const current = ensureActiveOrcaProfile(userDataPath)
const cloudCurrent =
!expectedCloud ||
(current.profile.cloud?.userId === expectedCloud.userId &&
current.profile.cloud.cloudProfileId === expectedCloud.profileId &&
(current.profile.cloud.activeOrgId ?? '') === expectedCloud.organizationId)
if (
current.profile.id !== active.profile.id ||
!cloudCurrent ||
!isArtifactShareLifecycleCurrent(active.profile.id, userDataPath, lifecycleGeneration)
) {
throw new Error('The signed-in Orca account changed while the artifact request was running.')
}
}
return {
profileId: active.profile.id,
scope,
assertCurrent,
rateLimitReplay: {
claim: allowance.claim,
assertCanReplay: () => {
assertCurrent()
// Sign-out fences the session generation before awaiting remote logout, and a revoked
// session signs out without unlinking, so neither is visible to assertCurrent yet.
if (
expectedCloud &&
(takeCloudSessionTicket(active.profile.id, userDataPath).generation !==
sessionGeneration ||
readCloudSession(active.profile.id, userDataPath).status !== 'found')
) {
throw new Error(
'Signed out of Orca while waiting to retry the artifact request. Sign in and try again.'
)
}
}
}
}
}
export function storedSessionAuthContext(
active: ActiveOrcaProfileState,
apiOrigin: string,
userDataPath: string,
allowance: ArtifactRateLimitAllowance
): ArtifactAuthContext {
if (!active.profile.cloud) {
throw new Error('The active Orca profile is not linked to a cloud account.')
}
return authContext(
active,
{
cloudUserId: active.profile.cloud.userId,
cloudProfileId: active.profile.cloud.cloudProfileId,
cloudOrganizationId: active.profile.cloud.activeOrgId ?? '',
apiOrigin
},
userDataPath,
allowance,
{
userId: active.profile.cloud.userId,
profileId: active.profile.cloud.cloudProfileId,
organizationId: active.profile.cloud.activeOrgId ?? ''
}
)
}
export function explicitTokenAuthContext(
active: ActiveOrcaProfileState,
apiOrigin: string,
token: string,
userDataPath: string,
allowance: ArtifactRateLimitAllowance
): ArtifactAuthContext {
const fingerprint = tokenFingerprint(token)
return authContext(
active,
{
cloudUserId: `token:${fingerprint}`,
cloudProfileId: `token:${fingerprint}`,
cloudOrganizationId: `token:${fingerprint}`,
apiOrigin
},
userDataPath,
allowance
)
}
@@ -0,0 +1,233 @@
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
vi.mock('electron', () => ({
app: { isPackaged: false },
safeStorage: { isEncryptionAvailable: () => false }
}))
import type { OrcaProfileCloudSummary } from '../../shared/orca-profiles/orca-profiles'
import { ensureActiveOrcaProfile } from '../orca-profiles/profile-index-store'
import { linkOrcaProfileToCloud } from '../orca-profiles/cloud-auth/profile-cloud-index'
import { saveOrcaCloudSession } from '../orca-profiles/cloud-session/profile-cloud-session-store'
import { getArtifactShareRecord } from './artifact-share-record-store'
import { ArtifactCloudService } from './artifact-cloud-service'
const createdPaths: string[] = []
const apiUrl = 'http://localhost:3000'
const cloud: OrcaProfileCloudSummary = {
cloudProfileId: 'cloud-a',
userId: 'user-a',
email: 'a@example.com',
linkedAt: 1
}
const scope = {
cloudUserId: cloud.userId,
cloudProfileId: cloud.cloudProfileId,
cloudOrganizationId: '',
apiOrigin: apiUrl
}
const writeRequest = {
sourceKey: '/repo/report.html',
content: '<h1>Hi</h1>',
contentType: 'text/html' as const,
fileName: 'report.html',
apiUrl
}
function artifactBody(): Record<string, unknown> {
return {
artifact: {
version: 1,
slug: 'artifact-a',
title: null,
originalFileName: 'report.html',
sourceContentType: 'text/html',
renderedContentType: 'text/html',
createdAt: '2026-08-07T12:00:00.000Z',
updatedAt: '2026-08-07T12:00:00.000Z',
expiresAt: '2026-09-07T00:00:00.000Z',
byteSize: 12,
deletedAt: null
},
shareUrl: 'https://share.onorca.dev/a/artifact-a'
}
}
const created = (): Response => Response.json({ ...artifactBody(), editToken: 'edit-secret' })
const rejected = (): Response => Response.json({ code: 'unauthorized' }, { status: 401 })
const limited = (seconds: number): Response =>
Response.json(
{
message: 'Too many artifact requests.',
scope: 'account',
limit: 20,
windowSeconds: 3600,
retryAfterSeconds: seconds
},
{ status: 429, headers: { 'retry-after': String(seconds) } }
)
type Sent = { method: string; token: string | null; key: string | null }
type CloudStub = {
sent: Sent[]
refreshes: number
}
/** Serves artifact responses in order and answers every refresh with the next session. */
function stubCloud(responses: (() => Response)[]): CloudStub {
const stub: CloudStub = { sent: [], refreshes: 0 }
vi.stubGlobal(
'fetch',
vi.fn(async (url: string | URL, init: RequestInit) => {
if (String(url).endsWith('/v1/desktop/auth/refresh')) {
stub.refreshes++
return Response.json({
accessToken: `access-${stub.refreshes + 1}`,
refreshToken: `refresh-${stub.refreshes + 1}`,
expiresAt: Date.now() + 60 * 60 * 1000,
cloud
})
}
if (!String(url).includes('/v1/artifacts')) {
throw new Error(`unexpected request to ${String(url)}`)
}
const headers = new Headers(init.headers)
stub.sent.push({
method: init.method ?? 'GET',
token: headers.get('authorization'),
key: headers.get('idempotency-key')
})
const next = responses.shift()
if (!next) {
throw new Error('unexpected extra artifact request')
}
return next()
})
)
return stub
}
function saveSession(profileId: string, userDataPath: string, expiresInMs: number): void {
saveOrcaCloudSession(profileId, userDataPath, {
accessToken: 'access-1',
refreshToken: 'refresh-1',
expiresAt: Date.now() + expiresInMs,
capabilities: { flags: {}, refreshedAt: Date.now() }
})
}
async function signIn(): Promise<{
userDataPath: string
profileId: string
artifacts: ArtifactCloudService
}> {
const userDataPath = await mkdtemp(join(tmpdir(), 'orca-artifact-auth-recovery-'))
createdPaths.push(userDataPath)
const profileId = ensureActiveOrcaProfile(userDataPath).profile.id
linkOrcaProfileToCloud(profileId, cloud, userDataPath)
saveSession(profileId, userDataPath, 60 * 60 * 1000)
return { userDataPath, profileId, artifacts: new ArtifactCloudService(userDataPath, () => true) }
}
/** Runs fake time forward in small steps until `outcome` settles. */
async function settle<T>(outcome: Promise<T>): Promise<T> {
let done = false
void outcome.then(
() => (done = true),
() => (done = true)
)
for (let step = 0; step < 10_000 && !done; step++) {
await vi.advanceTimersByTimeAsync(100)
}
expect(done).toBe(true)
return outcome
}
beforeEach(() => {
vi.useFakeTimers({ toFake: ['Date', 'setTimeout', 'clearTimeout'] })
vi.setSystemTime('2026-08-07T12:00:00.000Z')
vi.stubEnv('ORCA_CLOUD_API_URL', apiUrl)
vi.stubEnv('ORCA_CLOUD_CLIENT_ID', 'orca-test')
vi.stubEnv('ORCA_CLOUD_ALLOW_PLAINTEXT_SESSION', '1')
})
afterEach(async () => {
vi.useRealTimers()
vi.unstubAllGlobals()
vi.unstubAllEnvs()
vi.restoreAllMocks()
await Promise.all(
createdPaths.splice(0).map((path) => rm(path, { recursive: true, force: true }))
)
})
describe('a rate-limited artifact request that needs a session refresh', () => {
it('surfaces the second 429 after the refresh instead of waiting and replaying again', async () => {
const signin = await signIn()
const cloudStub = stubCloud([() => limited(5), rejected, () => limited(5), created])
const outcome = await settle(
signin.artifacts.share(writeRequest).catch((caught: unknown) => caught)
)
expect(outcome).toMatchObject({
statusCode: 429,
code: 'artifact_rate_limited',
data: { retryAfterSeconds: 5, scope: 'account', limit: 20, windowSeconds: 3600 }
})
expect(String(outcome)).toContain('Too many artifact requests.')
expect(vi.getTimerCount()).toBe(0)
expect(cloudStub.refreshes).toBe(1)
expect(cloudStub.sent.map(({ method, token }) => [method, token])).toEqual([
['POST', 'Bearer access-1'],
['POST', 'Bearer access-1'],
['POST', 'Bearer access-2']
])
expect(new Set(cloudStub.sent.map(({ key }) => key)).size).toBe(1)
expect(cloudStub.sent[0]?.key).toBeTruthy()
})
it('still finishes with the refreshed session when it is no longer limited', async () => {
const signin = await signIn()
const cloudStub = stubCloud([() => limited(5), rejected, created])
const outcome = await settle(signin.artifacts.share(writeRequest))
expect(outcome).toMatchObject({ status: 'ok', value: { shareUrl: artifactBody().shareUrl } })
expect(cloudStub.refreshes).toBe(1)
expect(cloudStub.sent.map(({ method, token }) => [method, token])).toEqual([
['POST', 'Bearer access-1'],
['POST', 'Bearer access-1'],
['POST', 'Bearer access-2']
])
expect(new Set(cloudStub.sent.map(({ key }) => key)).size).toBe(1)
expect(
getArtifactShareRecord(signin.profileId, signin.userDataPath, writeRequest.sourceKey, scope)
).toMatchObject({ slug: 'artifact-a', editToken: 'edit-secret' })
})
it('does not renew the wait when a reconciled create is re-run after its update got a 401', async () => {
const signin = await signIn()
// A lost create response leaves a pending create for the next publish to reconcile.
stubCloud([
() => {
throw new TypeError('fetch failed')
}
])
await expect(signin.artifacts.publish(writeRequest)).rejects.toThrow('fetch failed')
const changed = { ...writeRequest, content: '<h1>Changed</h1>' }
const cloudStub = stubCloud([() => limited(5), created, rejected, () => limited(5)])
const outcome = await settle(
signin.artifacts.publish(changed).catch((caught: unknown) => caught)
)
expect(outcome).toMatchObject({ statusCode: 429, code: 'artifact_rate_limited' })
expect(vi.getTimerCount()).toBe(0)
expect(cloudStub.sent.map(({ method }) => method)).toEqual(['POST', 'POST', 'PUT', 'POST'])
})
})
@@ -0,0 +1,273 @@
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
vi.mock('electron', () => ({
app: { isPackaged: false },
safeStorage: { isEncryptionAvailable: () => false }
}))
import type { OrcaProfileCloudSummary } from '../../shared/orca-profiles/orca-profiles'
import { ensureActiveOrcaProfile } from '../orca-profiles/profile-index-store'
import {
linkOrcaProfileToCloud,
unlinkOrcaProfileFromCloud
} from '../orca-profiles/cloud-auth/profile-cloud-index'
import { signOutCloudSession } from '../orca-profiles/cloud-session/profile-cloud-session-owner'
import { saveOrcaCloudSession } from '../orca-profiles/cloud-session/profile-cloud-session-store'
import { signOutCurrentOrcaProfile } from '../orca-profiles/cloud-auth/profile-cloud-service'
import { getArtifactShareRecord } from './artifact-share-record-store'
import { ArtifactCloudService } from './artifact-cloud-service'
const createdPaths: string[] = []
const apiUrl = 'http://localhost:3000'
const cloudA: OrcaProfileCloudSummary = {
cloudProfileId: 'cloud-a',
userId: 'user-a',
email: 'a@example.com',
linkedAt: 1
}
const cloudB: OrcaProfileCloudSummary = {
cloudProfileId: 'cloud-b',
userId: 'user-b',
email: 'b@example.com',
linkedAt: 2
}
const writeRequest = {
sourceKey: '/repo/report.html',
content: '<h1>Hi</h1>',
contentType: 'text/html' as const,
fileName: 'report.html',
apiUrl
}
type Signin = { userDataPath: string; profileId: string; artifacts: ArtifactCloudService }
function artifactBody(): Record<string, unknown> {
return {
artifact: {
version: 1,
slug: 'artifact-a',
title: null,
originalFileName: 'report.html',
sourceContentType: 'text/html',
renderedContentType: 'text/html',
createdAt: '2026-08-07T12:00:00.000Z',
updatedAt: '2026-08-07T12:00:00.000Z',
expiresAt: '2026-09-07T00:00:00.000Z',
byteSize: 12,
deletedAt: null
},
shareUrl: 'https://share.onorca.dev/a/artifact-a'
}
}
const created = (): Response => Response.json({ ...artifactBody(), editToken: 'edit-secret' })
const updated = (): Response => Response.json(artifactBody())
const deleted = (): Response => new Response(null, { status: 204 })
const limited = (): Response =>
Response.json(
{ message: 'Too many artifact requests.', retryAfterSeconds: 30 },
{ status: 429, headers: { 'retry-after': '30' } }
)
/** Serves `responses` in order and records each request's method. */
function stubFetch(...responses: Response[]): { methods: string[] } {
const methods: string[] = []
vi.stubGlobal(
'fetch',
vi.fn(async (_url: string, init: RequestInit) => {
methods.push(init.method ?? 'GET')
const next = responses.shift()
if (!next) {
throw new Error('unexpected extra artifact request')
}
return next
})
)
return { methods }
}
async function signIn(): Promise<Signin> {
const userDataPath = await mkdtemp(join(tmpdir(), 'orca-artifact-replay-authority-'))
createdPaths.push(userDataPath)
const profileId = ensureActiveOrcaProfile(userDataPath).profile.id
linkOrcaProfileToCloud(profileId, cloudA, userDataPath)
saveOrcaCloudSession(profileId, userDataPath, {
accessToken: 'access-a',
refreshToken: 'refresh-a',
expiresAt: Date.now() + 60 * 60 * 1000,
capabilities: { flags: {}, refreshedAt: Date.now() }
})
return { userDataPath, profileId, artifacts: new ArtifactCloudService(userDataPath, () => true) }
}
function signOutInApp({ profileId, userDataPath }: Signin): void {
signOutCloudSession(profileId, userDataPath)
unlinkOrcaProfileFromCloud(profileId, userDataPath)
}
function switchAccount(signin: Signin): void {
signOutInApp(signin)
linkOrcaProfileToCloud(signin.profileId, cloudB, signin.userDataPath)
}
// A server-revoked session signs out without unlinking the profile.
function revokeSession({ profileId, userDataPath }: Signin): void {
signOutCloudSession(profileId, userDataPath)
}
// The wait is scheduled after the 429 body is read; act only once it exists.
async function untilRetryScheduled(): Promise<void> {
for (let turn = 0; turn < 1000 && vi.getTimerCount() === 0; turn++) {
await new Promise((resolve) => setImmediate(resolve))
}
expect(vi.getTimerCount()).toBe(1)
}
/** Starts `run`, changes the account during the 429 wait, then lets the wait end. */
async function changeAccountDuringWait<T>(
run: () => Promise<T>,
change: () => void
): Promise<unknown> {
const outcome = run().catch((caught: unknown) => caught)
await untilRetryScheduled()
change()
await vi.advanceTimersByTimeAsync(30_000)
return outcome
}
beforeEach(() => {
vi.useFakeTimers({ toFake: ['Date', 'setTimeout', 'clearTimeout'] })
vi.setSystemTime('2026-08-07T12:00:00.000Z')
vi.stubEnv('ORCA_CLOUD_API_URL', apiUrl)
vi.stubEnv('ORCA_CLOUD_CLIENT_ID', 'orca-test')
vi.stubEnv('ORCA_CLOUD_ALLOW_PLAINTEXT_SESSION', '1')
})
afterEach(async () => {
vi.useRealTimers()
vi.unstubAllGlobals()
vi.unstubAllEnvs()
vi.restoreAllMocks()
await Promise.all(
createdPaths.splice(0).map((path) => rm(path, { recursive: true, force: true }))
)
})
describe('a rate-limited artifact request whose account changes during the wait', () => {
it('does not replay while the real sign-out is still waiting on remote logout', async () => {
const signin = await signIn()
const artifactMethods: string[] = []
const artifactResponses = [limited(), created()]
let finishLogout: (response: Response) => void = () => {}
let logoutRequests = 0
vi.stubGlobal(
'fetch',
vi.fn(async (url: string | URL, init: RequestInit) => {
if (String(url).includes('/v1/artifacts')) {
artifactMethods.push(init.method ?? 'GET')
const next = artifactResponses.shift()
if (!next) {
throw new Error('unexpected extra artifact request')
}
return next
}
logoutRequests++
return new Promise<Response>((resolve) => {
finishLogout = resolve
})
})
)
const outcome = signin.artifacts.share(writeRequest).catch((caught: unknown) => caught)
await untilRetryScheduled()
const signingOut = signOutCurrentOrcaProfile(signin.userDataPath)
await vi.advanceTimersByTimeAsync(30_000)
expect(String(await outcome)).toContain('Signed out of Orca while waiting to retry')
expect(artifactMethods).toEqual(['POST'])
expect(logoutRequests).toBe(1)
finishLogout(Response.json({}))
await expect(signingOut).resolves.toMatchObject({ status: 'signed-out' })
expect(artifactMethods).toEqual(['POST'])
expect(
getArtifactShareRecord(signin.profileId, signin.userDataPath, writeRequest.sourceKey, {
cloudUserId: cloudA.userId,
cloudProfileId: cloudA.cloudProfileId,
cloudOrganizationId: '',
apiOrigin: apiUrl
})
).toBeNull()
})
it('does not create the artifact after the user signs out', async () => {
const signin = await signIn()
const fetches = stubFetch(limited(), created())
const outcome = await changeAccountDuringWait(
() => signin.artifacts.share(writeRequest),
() => signOutInApp(signin)
)
expect(outcome).toBeInstanceOf(Error)
expect(String(outcome)).toContain('account changed')
expect(fetches.methods).toEqual(['POST'])
})
it('does not apply the update under the previous account after a switch', async () => {
const signin = await signIn()
const fetches = stubFetch(created(), limited(), updated())
await signin.artifacts.share(writeRequest)
const outcome = await changeAccountDuringWait(
() => signin.artifacts.update(writeRequest),
() => switchAccount(signin)
)
expect(String(outcome)).toContain('account changed')
expect(fetches.methods).toEqual(['POST', 'PUT'])
})
it('does not delete with a session the server revoked during the wait', async () => {
const signin = await signIn()
const fetches = stubFetch(created(), limited(), deleted())
await signin.artifacts.share(writeRequest)
const outcome = await changeAccountDuringWait(
() => signin.artifacts.unshare({ sourceKey: writeRequest.sourceKey, apiUrl }),
() => revokeSession(signin)
)
expect(String(outcome)).toContain('Signed out of Orca while waiting to retry')
expect(fetches.methods).toEqual(['POST', 'DELETE'])
})
it('does not delete by id under the previous account after a switch', async () => {
const signin = await signIn()
const fetches = stubFetch(limited(), deleted())
const outcome = await changeAccountDuringWait(
() => signin.artifacts.delete('artifact-a', { apiUrl }),
() => switchAccount(signin)
)
expect(String(outcome)).toContain('account changed')
expect(fetches.methods).toEqual(['DELETE'])
})
it('still replays once when the same account stays signed in', async () => {
const signin = await signIn()
const fetches = stubFetch(created(), limited(), updated())
await signin.artifacts.share(writeRequest)
const outcome = await changeAccountDuringWait(
() => signin.artifacts.update(writeRequest),
() => {}
)
expect(outcome).toMatchObject({ status: 'ok' })
expect(fetches.methods).toEqual(['POST', 'PUT', 'PUT'])
})
})
@@ -0,0 +1,251 @@
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
vi.mock('electron', () => ({
app: { isPackaged: false },
safeStorage: { isEncryptionAvailable: () => false }
}))
import { ensureActiveOrcaProfile } from '../orca-profiles/profile-index-store'
import { linkOrcaProfileToCloud } from '../orca-profiles/cloud-auth/profile-cloud-index'
import { mapRuntimeError } from '../runtime/rpc/responses/rpc-response'
import { ArtifactCloudService } from './artifact-cloud-service'
const createdPaths: string[] = []
const apiUrl = 'http://localhost:3000'
const now = new Date('2026-08-07T12:00:00.000Z')
const resetAt = '2026-08-07T12:45:00.000Z'
const writeRequest = {
sourceKey: '/repo/report.html',
content: '<h1>Hi</h1>',
contentType: 'text/html' as const,
fileName: 'report.html',
apiUrl,
authToken: 'token-a'
}
function createdResponse(): Response {
return Response.json({
artifact: {
version: 1,
slug: 'artifact-a',
title: null,
originalFileName: 'report.html',
sourceContentType: 'text/html',
renderedContentType: 'text/html',
createdAt: now.toISOString(),
updatedAt: now.toISOString(),
expiresAt: '2026-09-07T00:00:00.000Z',
byteSize: 12,
deletedAt: null
},
shareUrl: 'https://share.onorca.dev/a/artifact-a',
editToken: 'edit-secret'
})
}
function rateLimited(retryAfter: string | null, body?: Record<string, unknown>): Response {
return new Response(body ? JSON.stringify(body) : null, {
status: 429,
headers: {
...(retryAfter === null ? {} : { 'retry-after': retryAfter }),
...(body ? { 'content-type': 'application/json' } : {})
}
})
}
function limitBody(retryAfterSeconds: number): Record<string, unknown> {
return {
message: 'You have shared 20 artifacts this hour.',
scope: 'account',
limit: 20,
windowSeconds: 3600,
retryAfterSeconds,
resetAt
}
}
/** Serves `responses` in order and records each request's idempotency key. */
function stubFetch(...responses: Response[]): { keys: (string | null)[] } {
const keys: (string | null)[] = []
vi.stubGlobal(
'fetch',
vi.fn(async (_url: string, init: RequestInit) => {
keys.push(new Headers(init.headers).get('idempotency-key'))
const next = responses.shift()
if (!next) {
throw new Error('unexpected extra artifact request')
}
return next
})
)
return { keys }
}
async function service(): Promise<ArtifactCloudService> {
const userDataPath = await mkdtemp(join(tmpdir(), 'orca-artifact-rate-limit-'))
createdPaths.push(userDataPath)
const active = ensureActiveOrcaProfile(userDataPath)
linkOrcaProfileToCloud(
active.profile.id,
{ cloudProfileId: 'cloud-a', userId: 'user-a', email: 'a@example.com', linkedAt: 1 },
userDataPath
)
return new ArtifactCloudService(userDataPath, () => true)
}
// The wait is scheduled after the 429 body is read; advance only once it exists.
async function untilRetryScheduled(): Promise<void> {
for (let turn = 0; turn < 1000 && vi.getTimerCount() === 0; turn++) {
await new Promise((resolve) => setImmediate(resolve))
}
expect(vi.getTimerCount()).toBe(1)
}
async function shareFailure(artifacts: ArtifactCloudService): Promise<Error> {
const error: unknown = await artifacts.share(writeRequest).catch((caught: unknown) => caught)
if (!(error instanceof Error)) {
throw new Error('expected the share to fail')
}
return error
}
function localTime(iso: string): string {
return new Date(iso).toLocaleTimeString(undefined, { hour: 'numeric', minute: '2-digit' })
}
beforeEach(() => {
vi.useFakeTimers({ toFake: ['Date', 'setTimeout', 'clearTimeout'] })
vi.setSystemTime(now)
})
afterEach(async () => {
vi.useRealTimers()
vi.unstubAllGlobals()
vi.restoreAllMocks()
await Promise.all(
createdPaths.splice(0).map((path) => rm(path, { recursive: true, force: true }))
)
})
describe('artifact share under a rate limit', () => {
it('fails fast with the server message and local reset time when the wait is long', async () => {
const artifacts = await service()
const fetches = stubFetch(rateLimited('2700', limitBody(2700)))
const error = await shareFailure(artifacts)
expect(fetches.keys).toHaveLength(1)
expect(error).toMatchObject({
code: 'artifact_rate_limited',
data: { scope: 'account', limit: 20, windowSeconds: 3600, retryAfterSeconds: 2700, resetAt }
})
const message = error.message
expect(message).toContain('You have shared 20 artifacts this hour.')
expect(message).toContain(`Try again after`)
expect(message).toContain(localTime(resetAt))
expect(message).not.toContain(resetAt)
})
it('waits out a short Retry-After and replays once with the same Idempotency-Key', async () => {
const artifacts = await service()
const fetches = stubFetch(rateLimited('30', limitBody(30)), createdResponse())
const shared = artifacts.share(writeRequest)
await untilRetryScheduled()
await vi.advanceTimersByTimeAsync(29_999)
expect(fetches.keys).toHaveLength(1)
await vi.advanceTimersByTimeAsync(1)
await expect(shared).resolves.toMatchObject({
status: 'ok',
value: { shareUrl: 'https://share.onorca.dev/a/artifact-a' }
})
expect(fetches.keys).toHaveLength(2)
expect(fetches.keys[0]).toBeTruthy()
expect(fetches.keys[1]).toBe(fetches.keys[0])
})
it('gives up after one replay when the server is still limiting', async () => {
const artifacts = await service()
const fetches = stubFetch(rateLimited('10', limitBody(10)), rateLimited('10', limitBody(10)))
const shared = artifacts.share(writeRequest).catch((caught: unknown) => caught)
await untilRetryScheduled()
await vi.advanceTimersByTimeAsync(10_000)
expect(await shared).toMatchObject({ code: 'artifact_rate_limited' })
expect(vi.getTimerCount()).toBe(0)
expect(fetches.keys).toHaveLength(2)
})
it('reuses the pending Idempotency-Key when the user shares again after a rate limit', async () => {
const artifacts = await service()
const fetches = stubFetch(rateLimited('600', limitBody(600)), createdResponse())
await expect(artifacts.share(writeRequest)).rejects.toMatchObject({ statusCode: 429 })
await expect(artifacts.share(writeRequest)).resolves.toBeTruthy()
expect(fetches.keys).toHaveLength(2)
expect(fetches.keys[1]).toBe(fetches.keys[0])
})
it('reports an older server bare 429 as rate limited without retrying', async () => {
const artifacts = await service()
const fetches = stubFetch(rateLimited(null))
await expect(artifacts.share(writeRequest)).rejects.toThrow(
'Artifact sharing is rate limited. Try again later.'
)
expect(fetches.keys).toHaveLength(1)
})
it('uses an older server Retry-After header for the wait it reports', async () => {
const artifacts = await service()
const fetches = stubFetch(rateLimited('120'))
await expect(artifacts.share(writeRequest)).rejects.toThrow(
'Artifact sharing is rate limited. Try again in 2 minutes.'
)
expect(fetches.keys).toHaveLength(1)
})
it('reads an HTTP-date Retry-After and replays when it is within a minute', async () => {
const artifacts = await service()
const retryAt = new Date(now.getTime() + 5_000).toUTCString()
const fetches = stubFetch(rateLimited(retryAt), createdResponse())
const shared = artifacts.share(writeRequest)
await untilRetryScheduled()
await vi.advanceTimersByTimeAsync(5_000)
await expect(shared).resolves.toBeTruthy()
expect(fetches.keys).toHaveLength(2)
})
it('keeps server text from writing control sequences into the terminal', async () => {
const artifacts = await service()
stubFetch(rateLimited('900', { message: 'Slow down\u001b[2J\u0007 please' }))
await expect(artifacts.share(writeRequest)).rejects.toThrow(
'Slow down [2J please. Try again in 15 minutes.'
)
})
it('reaches RPC callers with its own code, message and reset details', async () => {
const artifacts = await service()
stubFetch(rateLimited('2700', limitBody(2700)))
const error = await shareFailure(artifacts)
const response = mapRuntimeError('request-1', { runtimeId: 'runtime-1' }, error)
expect(response.error).toMatchObject({
code: 'artifact_rate_limited',
message: error.message,
data: { resetAt, retryAfterSeconds: 2700 }
})
})
})
@@ -0,0 +1,130 @@
import { ARTIFACT_RATE_LIMITED_CODE } from '../../shared/artifacts/artifacts'
import { OrcaCloudRequestError } from '../orca-profiles/cloud-auth/profile-cloud-client'
const MAX_SERVER_MESSAGE_LENGTH = 300
export type ArtifactRateLimitDetails = {
retryAfterSeconds: number | null
resetAt: string | null
scope: 'account' | 'network' | null
limit: number | null
windowSeconds: number | null
}
/** A 429 from the artifact API; `message` is written for people, `data` for programs. */
export class ArtifactCloudRateLimitError extends OrcaCloudRequestError {
readonly code = ARTIFACT_RATE_LIMITED_CODE
readonly data: ArtifactRateLimitDetails
constructor(message: string, data: ArtifactRateLimitDetails, errorCode?: string) {
super(429, errorCode)
this.name = 'ArtifactCloudRateLimitError'
this.message = message
this.data = data
}
}
/** Retry-After is delay-seconds or an HTTP-date (RFC 9110 §10.2.3). */
export function parseRetryAfterSeconds(header: string | null, now = Date.now()): number | null {
const value = header?.trim()
if (!value) {
return null
}
if (/^\d+$/.test(value)) {
return Number(value)
}
const at = Date.parse(value)
return Number.isNaN(at) ? null : Math.max(0, Math.ceil((at - now) / 1000))
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null
}
function nonNegativeNumber(value: unknown): number | null {
return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : null
}
// Server text reaches a terminal; drop control characters so it cannot rewrite the screen.
function sanitizeServerMessage(value: unknown): string | null {
if (typeof value !== 'string') {
return null
}
const text = value
// oxlint-disable-next-line no-control-regex -- matching control bytes is the point.
.replace(/[\u0000-\u001f\u007f-\u009f]/g, ' ')
.replace(/\s+/g, ' ')
.trim()
if (!text) {
return null
}
return text.length > MAX_SERVER_MESSAGE_LENGTH
? `${text.slice(0, MAX_SERVER_MESSAGE_LENGTH - 1)}…`
: text
}
function formatLocalResetTime(resetAt: Date, now: Date): string {
const sameDay = resetAt.toDateString() === now.toDateString()
return new Intl.DateTimeFormat(undefined, {
...(sameDay ? {} : { month: 'short', day: 'numeric' }),
hour: 'numeric',
minute: '2-digit'
}).format(resetAt)
}
function formatWait(seconds: number): string {
if (seconds < 60) {
return seconds === 1 ? '1 second' : `${seconds} seconds`
}
const minutes = Math.ceil(seconds / 60)
if (minutes < 60) {
return minutes === 1 ? '1 minute' : `${minutes} minutes`
}
const hours = Math.ceil(minutes / 60)
return hours === 1 ? '1 hour' : `${hours} hours`
}
function withSentenceEnd(text: string): string {
return /[.!?…]$/.test(text) ? text : `${text}.`
}
export type ArtifactRateLimitResponse = {
error: ArtifactCloudRateLimitError
/** Seconds the server asked us to wait; null when it gave no usable hint. */
retryAfterSeconds: number | null
}
/** Reads both the structured 429 body and the bare 429 older servers send. */
export async function readArtifactRateLimitResponse(
response: Response,
now = new Date()
): Promise<ArtifactRateLimitResponse> {
const raw: unknown = await response.json().catch(() => null)
const body = isRecord(raw) ? raw : {}
const bodyRetryAfter = nonNegativeNumber(body.retryAfterSeconds)
const retryAfterSeconds =
parseRetryAfterSeconds(response.headers.get('retry-after'), now.getTime()) ??
(bodyRetryAfter === null ? null : Math.ceil(bodyRetryAfter))
const resetDate = typeof body.resetAt === 'string' ? new Date(body.resetAt) : null
const resetAt = resetDate && !Number.isNaN(resetDate.getTime()) ? resetDate : null
const serverMessage = sanitizeServerMessage(body.message)
const when = resetAt
? `Try again after ${formatLocalResetTime(resetAt, now)}.`
: retryAfterSeconds !== null
? `Try again in ${formatWait(retryAfterSeconds)}.`
: 'Try again later.'
const message = `${withSentenceEnd(serverMessage ?? 'Artifact sharing is rate limited')} ${when}`
const scope = body.scope === 'account' || body.scope === 'network' ? body.scope : null
const error = new ArtifactCloudRateLimitError(
message,
{
retryAfterSeconds,
resetAt: resetAt?.toISOString() ?? null,
scope,
limit: nonNegativeNumber(body.limit),
windowSeconds: nonNegativeNumber(body.windowSeconds)
},
typeof body.code === 'string' ? body.code : undefined
)
return { error, retryAfterSeconds }
}
+58 -6
View File
@@ -1,5 +1,11 @@
import type { ArtifactWriteRequest } from '../../shared/artifacts/artifacts'
import {
ARTIFACT_CLOUD_REQUEST_TIMEOUT_MS,
ARTIFACT_RATE_LIMIT_MAX_RETRY_DELAY_SECONDS,
type ArtifactWriteRequest
} from '../../shared/artifacts/artifacts'
import { OrcaCloudRequestError } from '../orca-profiles/cloud-auth/profile-cloud-client'
import { readArtifactRateLimitResponse } from './artifact-cloud-rate-limit'
import type { ArtifactRateLimitReplay } from './artifact-cloud-auth-context'
export type ArtifactWriteBody = {
content: string
@@ -17,13 +23,22 @@ export function artifactWriteBody(request: ArtifactWriteRequest): ArtifactWriteB
}
}
export async function artifactRequest<T>(
type ArtifactRequestOptions = {
method?: string
body?: unknown
editToken?: string
idempotencyKey?: string
/** The operation's single 429 replay; without it a 429 never replays. */
rateLimitReplay?: ArtifactRateLimitReplay
}
function sendArtifactRequest(
apiUrl: string,
token: string,
path: string,
options: { method?: string; body?: unknown; editToken?: string; idempotencyKey?: string } = {}
): Promise<T> {
const response = await fetch(`${apiUrl}/v1/artifacts${path}`, {
options: ArtifactRequestOptions
): Promise<Response> {
return fetch(`${apiUrl}/v1/artifacts${path}`, {
method: options.method ?? 'GET',
headers: {
authorization: `Bearer ${token}`,
@@ -33,8 +48,45 @@ export async function artifactRequest<T>(
},
body: options.body ? JSON.stringify(options.body) : undefined,
redirect: 'error',
signal: AbortSignal.timeout(20_000)
signal: AbortSignal.timeout(ARTIFACT_CLOUD_REQUEST_TIMEOUT_MS)
})
}
// A POST without an Idempotency-Key could create a second artifact if the first one landed.
function canReplay(options: ArtifactRequestOptions): boolean {
return (
options.rateLimitReplay !== undefined &&
((options.method ?? 'GET') !== 'POST' || Boolean(options.idempotencyKey))
)
}
export async function artifactRequest<T>(
apiUrl: string,
token: string,
path: string,
options: ArtifactRequestOptions = {}
): Promise<T> {
let response = await sendArtifactRequest(apiUrl, token, path, options)
if (response.status === 429) {
const limited = await readArtifactRateLimitResponse(response)
const delay = limited.retryAfterSeconds
// One replay with the same Idempotency-Key for a short wait, per operation; never loop on 429.
if (
delay === null ||
delay > ARTIFACT_RATE_LIMIT_MAX_RETRY_DELAY_SECONDS ||
!canReplay(options) ||
!options.rateLimitReplay?.claim()
) {
throw limited.error
}
await new Promise((resolve) => setTimeout(resolve, delay * 1000))
// A sign-out or account switch during the wait must not create, update or delete under the old token.
options.rateLimitReplay?.assertCanReplay()
response = await sendArtifactRequest(apiUrl, token, path, options)
if (response.status === 429) {
throw (await readArtifactRateLimitResponse(response)).error
}
}
if (!response.ok) {
const body = (await response.json().catch(() => null)) as { code?: string } | null
throw new OrcaCloudRequestError(response.status, body?.code)
+27 -95
View File
@@ -1,4 +1,4 @@
import { createHash, randomUUID } from 'node:crypto'
import { randomUUID } from 'node:crypto'
import type {
ArtifactCloudOperation,
ArtifactCloudOptions,
@@ -19,33 +19,32 @@ import {
resolveArtifactCloudApiUrl
} from './artifact-cloud-config'
import {
type ArtifactShareScope,
captureArtifactShareLifecycle,
getArtifactShareRecord,
isArtifactShareLifecycleCurrent,
refreshArtifactShareRecordExpiration,
removeArtifactShareRecords
} from './artifact-share-record-store'
import type { ActiveOrcaProfileState } from '../orca-profiles/profile-index-store'
import { artifactRequest, artifactWriteBody } from './artifact-cloud-request'
import { ArtifactPublisher } from './artifact-publisher'
import { OrcaCloudRequestError } from '../orca-profiles/cloud-auth/profile-cloud-client'
type ArtifactAuthContext = {
profileId: string
scope: ArtifactShareScope
assertCurrent: () => void
}
import {
type ArtifactAuthContext,
createArtifactRateLimitAllowance,
explicitTokenAuthContext,
storedSessionAuthContext
} from './artifact-cloud-auth-context'
async function deleteArtifactRequest(
apiUrl: string,
token: string,
path: string,
auth: ArtifactAuthContext,
editToken?: string
): Promise<void> {
try {
await artifactRequest<void>(apiUrl, token, path, {
method: 'DELETE',
rateLimitReplay: auth.rateLimitReplay,
...(editToken ? { editToken } : {})
})
} catch (error) {
@@ -59,84 +58,6 @@ async function deleteArtifactRequest(
}
}
function tokenFingerprint(token: string): string {
return createHash('sha256').update(token).digest('hex')
}
function authContext(
active: ActiveOrcaProfileState,
scope: ArtifactShareScope,
userDataPath: string,
expectedCloud?: { userId: string; profileId: string; organizationId: string }
): ArtifactAuthContext {
const lifecycleGeneration = captureArtifactShareLifecycle(active.profile.id, userDataPath)
return {
profileId: active.profile.id,
scope,
assertCurrent: () => {
const current = ensureActiveOrcaProfile(userDataPath)
const cloudCurrent =
!expectedCloud ||
(current.profile.cloud?.userId === expectedCloud.userId &&
current.profile.cloud.cloudProfileId === expectedCloud.profileId &&
(current.profile.cloud.activeOrgId ?? '') === expectedCloud.organizationId)
if (
current.profile.id !== active.profile.id ||
!cloudCurrent ||
!isArtifactShareLifecycleCurrent(active.profile.id, userDataPath, lifecycleGeneration)
) {
throw new Error(
'The signed-in Orca account changed while the artifact request was running.'
)
}
}
}
}
function storedSessionAuthContext(
active: ActiveOrcaProfileState,
apiOrigin: string,
userDataPath: string
): ArtifactAuthContext {
if (!active.profile.cloud) {
throw new Error('The active Orca profile is not linked to a cloud account.')
}
return authContext(
active,
{
cloudUserId: active.profile.cloud.userId,
cloudProfileId: active.profile.cloud.cloudProfileId,
cloudOrganizationId: active.profile.cloud.activeOrgId ?? '',
apiOrigin
},
userDataPath,
{
userId: active.profile.cloud.userId,
profileId: active.profile.cloud.cloudProfileId,
organizationId: active.profile.cloud.activeOrgId ?? ''
}
)
}
function explicitTokenAuthContext(
active: ActiveOrcaProfileState,
apiOrigin: string,
token: string,
userDataPath: string
): ArtifactAuthContext {
const fingerprint = tokenFingerprint(token)
return authContext(
active,
{
cloudUserId: `token:${fingerprint}`,
cloudProfileId: `token:${fingerprint}`,
cloudOrganizationId: `token:${fingerprint}`,
apiOrigin
},
userDataPath
)
}
export class ArtifactCloudService {
private readonly publisher: ArtifactPublisher
@@ -149,9 +70,11 @@ export class ArtifactCloudService {
}
list(options: ArtifactListOptions): Promise<ArtifactCloudOperation<ArtifactListPage>> {
return this.withAuth(options, async (token, apiUrl) => {
return this.withAuth(options, async (token, apiUrl, auth) => {
const query = options.cursor ? `?cursor=${encodeURIComponent(options.cursor)}` : ''
return artifactRequest<ArtifactListPage>(apiUrl, token, query)
return artifactRequest<ArtifactListPage>(apiUrl, token, query, {
rateLimitReplay: auth.rateLimitReplay
})
})
}
@@ -211,7 +134,8 @@ export class ArtifactCloudService {
{
method: 'PUT',
editToken: record.editToken,
body: artifactWriteBody(request)
body: artifactWriteBody(request),
rateLimitReplay: auth.rateLimitReplay
}
)
auth.assertCurrent()
@@ -246,7 +170,7 @@ export class ArtifactCloudService {
}
return this.publisher.runForSlug(record.slug, auth, async () => {
auth.assertCurrent()
await deleteArtifactRequest(apiUrl, token, `/${record.slug}`, record.editToken)
await deleteArtifactRequest(apiUrl, token, `/${record.slug}`, auth, record.editToken)
auth.assertCurrent()
removeArtifactShareRecords(auth.profileId, this.userDataPath, auth.scope, {
sourceKey: request.sourceKey,
@@ -261,7 +185,7 @@ export class ArtifactCloudService {
return this.withAuth(options, (token, apiUrl, auth) =>
this.publisher.runForSlug(id, auth, async () => {
auth.assertCurrent()
await deleteArtifactRequest(apiUrl, token, `/${encodeURIComponent(id)}`)
await deleteArtifactRequest(apiUrl, token, `/${encodeURIComponent(id)}`, auth)
auth.assertCurrent()
removeArtifactShareRecords(auth.profileId, this.userDataPath, auth.scope, { slug: id })
})
@@ -275,6 +199,8 @@ export class ArtifactCloudService {
const apiUrl = resolveArtifactCloudApiUrl(options.apiUrl)
const active = ensureActiveOrcaProfile(this.userDataPath)
prepareArtifactCloudUse(active.profile, this.userDataPath)
// Outlives the 401 re-run below, so the whole operation gets one rate-limit wait.
const rateLimitAllowance = createArtifactRateLimitAllowance()
if (options.authToken?.trim()) {
if (!allowsArtifactCloudAuthOverride()) {
throw new Error(
@@ -282,7 +208,13 @@ export class ArtifactCloudService {
)
}
const token = options.authToken.trim()
const auth = explicitTokenAuthContext(active, apiUrl, token, this.userDataPath)
const auth = explicitTokenAuthContext(
active,
apiUrl,
token,
this.userDataPath,
rateLimitAllowance
)
const value = await operation(token, apiUrl, auth)
auth.assertCurrent()
return {
@@ -299,7 +231,7 @@ export class ArtifactCloudService {
active,
this.userDataPath,
async (session) => {
const auth = storedSessionAuthContext(active, apiUrl, this.userDataPath)
const auth = storedSessionAuthContext(active, apiUrl, this.userDataPath, rateLimitAllowance)
const value = await operation(session.accessToken, apiUrl, auth)
auth.assertCurrent()
return value
+6 -2
View File
@@ -9,6 +9,7 @@ import {
type ArtifactWriteBody,
artifactWriteBody
} from './artifact-cloud-request'
import type { ArtifactRateLimitReplay } from './artifact-cloud-auth-context'
import {
type ArtifactCreateIntent,
getArtifactCreateIntent,
@@ -53,6 +54,7 @@ type ArtifactPublishAuthContext = {
profileId: string
scope: ArtifactShareScope
assertCurrent: () => void
rateLimitReplay: ArtifactRateLimitReplay
}
function artifactOperationQueueKey(
@@ -188,7 +190,8 @@ export class ArtifactPublisher {
const item = await artifactRequest<ArtifactListItem>(apiUrl, token, `/${record.slug}`, {
method: 'PUT',
editToken: record.editToken,
body: artifactWriteBody(request)
body: artifactWriteBody(request),
rateLimitReplay: auth.rateLimitReplay
})
auth.assertCurrent()
refreshArtifactShareRecordExpiration(
@@ -227,7 +230,8 @@ export class ArtifactPublisher {
response = await artifactRequest<ArtifactCreateResponse>(apiUrl, token, '', {
method: 'POST',
body: intent.body,
idempotencyKey: intent.idempotencyKey
idempotencyKey: intent.idempotencyKey,
rateLimitReplay: auth.rateLimitReplay
})
} catch (error) {
// A replay may outlive its server receipt, so never abandon a possibly committed artifact.
@@ -26,6 +26,7 @@ import { wslDistroHoldRpcFailure } from '../../../../shared/remote-runtime/wsl-h
import { LINEAR_ERROR_CODES } from '../../../../shared/linear/agent-access'
import { AGENT_SESSION_RPC_ERROR_CODES } from '../../../../shared/agent-session/lifecycle/operations/agent-session-host-authority'
import { ARTIFACT_SHARING_DISABLED_CODE } from '../../../../shared/artifacts/artifact-sharing-gate'
import { ARTIFACT_RATE_LIMITED_CODE } from '../../../../shared/artifacts/artifacts'
import { AGENT_SKILL_SHARING_DISABLED_CODE } from '../../../../shared/skills/sharing/agent-skill-sharing-gate'
import { SHARING_UNAVAILABLE_ON_HOST_CODE } from '../../../../shared/remote-runtime/host-status/sharing-unavailable-on-host'
import {
@@ -188,6 +189,8 @@ const STRUCTURED_RUNTIME_PASSTHROUGH_CODES: ReadonlySet<string> = new Set([
PROJECT_RUNTIME_REPAIR_REQUIRED_CODE,
GIT_DIFF_TOO_LARGE_CODE,
ARTIFACT_SHARING_DISABLED_CODE,
// The reset time and limit ride in data; the message already names them for people.
ARTIFACT_RATE_LIMITED_CODE,
AGENT_SKILL_SHARING_DISABLED_CODE,
SHARING_UNAVAILABLE_ON_HOST_CODE,
AGENT_SKILL_NOT_SHAREABLE_CODE,
@@ -0,0 +1,58 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
const mocks = vi.hoisted(() => ({
call: vi.fn(),
toastError: vi.fn()
}))
vi.mock('sonner', () => ({ toast: { error: mocks.toastError, success: vi.fn() } }))
vi.mock('@/runtime/rpc/calls/runtime-rpc-call', () => ({ callRuntimeRpc: mocks.call }))
vi.mock('@/store', () => ({
useAppStore: {
getState: () => ({ orcaProfileAuthStatus: { state: 'connected' } })
}
}))
import { RuntimeRpcCallError } from '@/runtime/rpc/calls/runtime-rpc-result'
import { publishArtifactFromSurface } from './artifact-publish-flow'
const request = {
sourceKey: '/repo/report.html',
content: '<h1>Hi</h1>',
contentType: 'text/html' as const,
fileName: 'report.html'
}
function rpcFailure(code: string, message: string): RuntimeRpcCallError {
return new RuntimeRpcCallError({
id: 'request-1',
ok: false,
error: { code, message },
_meta: { runtimeId: 'runtime-1' }
})
}
afterEach(() => {
vi.clearAllMocks()
})
describe('publishArtifactFromSurface', () => {
it('shows the rate limit and when to retry instead of a bare failure', async () => {
const message = 'You have shared 20 artifacts this hour. Try again after 3:45 PM.'
mocks.call.mockRejectedValue(rpcFailure('artifact_rate_limited', message))
await expect(publishArtifactFromSurface(async () => request)).resolves.toBeNull()
expect(mocks.toastError).toHaveBeenCalledWith('Could not share artifact', {
description: message
})
})
it('keeps internal error text out of the toast for other failures', async () => {
mocks.call.mockRejectedValue(rpcFailure('runtime_error', 'orca_cloud_request_failed_500'))
await publishArtifactFromSurface(async () => request)
expect(mocks.toastError).toHaveBeenCalledWith('Could not share artifact', undefined)
})
})
@@ -7,11 +7,13 @@ import type {
import {
ARTIFACT_MAX_CONTENT_BYTES,
ARTIFACT_MAX_REQUEST_BYTES,
ARTIFACT_RATE_LIMITED_CODE,
artifactContentByteLength,
artifactWriteRequestByteLength
} from '../../../../shared/artifacts/artifacts'
import { translate } from '@/i18n/i18n'
import { callRuntimeRpc } from '@/runtime/rpc/calls/runtime-rpc-call'
import { RuntimeRpcCallError } from '@/runtime/rpc/calls/runtime-rpc-result'
import { useAppStore } from '@/store'
const LOCAL_RUNTIME = { kind: 'local' } as const
@@ -94,7 +96,10 @@ export async function publishArtifactFromSurface(
),
error instanceof ArtifactPublishPreparationError
? { description: artifactPreparationErrorDescription(error.code) }
: undefined
: // The desktop already wrote the limit and its local reset time into the message.
error instanceof RuntimeRpcCallError && error.code === ARTIFACT_RATE_LIMITED_CODE
? { description: error.message }
: undefined
)
}
return null
+26
View File
@@ -7,6 +7,32 @@ export const ARTIFACT_CLI_MAX_RPC_BYTES = 800 * 1024
/** Allows JSON escaping while staying below the cloud API's 11 MiB body budget. */
export const ARTIFACT_MAX_REQUEST_BYTES = 11 * 1024 * 1024
/** RPC error code for an artifact API 429; `data` carries the reset time and limit. */
export const ARTIFACT_RATE_LIMITED_CODE = 'artifact_rate_limited'
/** Per-request budget for one call to the artifact cloud API. */
export const ARTIFACT_CLOUD_REQUEST_TIMEOUT_MS = 20_000
/** A 429 asking for a longer wait fails fast; a longer hold would block the file's publish queue. */
export const ARTIFACT_RATE_LIMIT_MAX_RETRY_DELAY_SECONDS = 60
/** Most artifact API requests one share can send: create, its rate-limit replay and the
* follow-up update, then create and update again after a 401 session refresh. */
export const ARTIFACT_MAX_CLOUD_REQUESTS_PER_OPERATION = 5
/** One session refresh request (CLOUD_REFRESH_TIMEOUT_MS): the expiry refresh before the
* operation and the 401 refresh during it. The refresh's own 5xx/lost-response replay is
* cloud-auth's failure path and is not counted. */
const ARTIFACT_SESSION_REFRESH_BUDGET_MS = 20_000
/** Covers a share's worst case: every request it can send, the operation's single
* rate-limit wait, and both session refreshes. */
export const ARTIFACT_CLI_REQUEST_TIMEOUT_MS =
ARTIFACT_MAX_CLOUD_REQUESTS_PER_OPERATION * ARTIFACT_CLOUD_REQUEST_TIMEOUT_MS +
ARTIFACT_RATE_LIMIT_MAX_RETRY_DELAY_SECONDS * 1000 +
2 * ARTIFACT_SESSION_REFRESH_BUDGET_MS +
10_000
export function artifactContentByteLength(content: string): number {
return new TextEncoder().encode(content).byteLength
}