Skip to content

An open-source, faster Devin alternative

TypeScript

web: keep the index age current through 304s with Work-As-Of; mock answers 304

Igor Zalutski via Computer392cdc8d1ce2
diff --git a/web/src/api/mock-work.ts b/web/src/api/mock-work.ts
index 5c9dd128..b9091de7 100644
--- a/web/src/api/mock-work.ts
+++ b/web/src/api/mock-work.ts
@@ -419,3 +419,25 @@ export function workMock(
}
return undefined
}
+
+/**
+ * The open-items poll as the server answers it, headers included: an ETag
+ * over the page without `asOf`, a 304 when the caller's ETag still matches,
+ * and the index's `Work-As-Of` on both.
+ */
+export function workOpenItemsResponse(ifNoneMatch: string | null): Response {
+ const page = workMock(`${PREFIX}/items?stage=open`, 'GET', undefined) as Json
+ const { asOf, ...rest } = page
+ const etag = `"${hash(JSON.stringify(rest))}"`
+ const headers = new Headers({ ETag: etag })
+ if (typeof asOf === 'string') headers.set('Work-As-Of', asOf)
+ if (ifNoneMatch === etag) return new Response(null, { status: 304, headers })
+ headers.set('Content-Type', 'application/json')
+ return new Response(JSON.stringify(page), { status: 200, headers })
+}
+
+function hash(s: string): string {
+ let h = 5381
+ for (let i = 0; i < s.length; i++) h = (h * 33) ^ s.charCodeAt(i)
+ return (h >>> 0).toString(16)
+}
diff --git a/web/src/managed-agents/work/Board.tsx b/web/src/managed-agents/work/Board.tsx
index 55660653..b2a6f75b 100644
--- a/web/src/managed-agents/work/Board.tsx
+++ b/web/src/managed-agents/work/Board.tsx
@@ -23,6 +23,7 @@ import { timeAgo } from '../computer-sessions/transcript'
import {
getClosedItems,
getOpenItems,
+ keepOpenItems,
useOrg,
workKeys,
type Item,
@@ -70,10 +71,10 @@ function useOpenItems() {
queryFn: async () => {
const prev = qc.getQueryData<OpenItemsPoll>(key)
const next = await getOpenItems(prev?.etag ?? null)
- if (next) return next
- if (prev) return prev
+ if (prev) return keepOpenItems(prev, next)
+ if (!('notModified' in next)) return next
// A 304 with nothing cached cannot happen (no ETag was sent); ask again.
- return (await getOpenItems(null))!
+ return (await getOpenItems(null)) as OpenItemsPoll
},
refetchInterval: OPEN_POLL_MS,
retry: (count, error) =>
diff --git a/web/src/managed-agents/work/api.test.ts b/web/src/managed-agents/work/api.test.ts
index a22178e3..c3e00b5d 100644
--- a/web/src/managed-agents/work/api.test.ts
+++ b/web/src/managed-agents/work/api.test.ts
@@ -1,5 +1,10 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
-import { getOpenItems, putRepository } from './api'
+import {
+ getOpenItems,
+ keepOpenItems,
+ putRepository,
+ type OpenItemsPoll,
+} from './api'
const page = {
repository: null,
@@ -30,12 +35,60 @@ describe('open items poll', () => {
'/api/managed-agents/computer/work/items?stage=open',
)
- expect(await getOpenItems('"h1"')).toBeNull()
+ expect(await getOpenItems('"h1"')).toEqual({
+ notModified: true,
+ asOf: null,
+ })
const init = fetch.mock.calls[1][1] as RequestInit
expect((init.headers as Record<string, string>)['If-None-Match']).toBe(
'"h1"',
)
})
+
+ it('reads Work-As-Of on a 200 and on a 304', async () => {
+ const fetch = vi
+ .fn()
+ .mockResolvedValueOnce(
+ new Response(JSON.stringify({ ...page, asOf: 'body' }), {
+ status: 200,
+ headers: { ETag: '"h1"', 'Work-As-Of': '2026-11-19T10:00:00Z' },
+ }),
+ )
+ .mockResolvedValueOnce(
+ new Response(null, {
+ status: 304,
+ headers: { 'Work-As-Of': '2026-11-19T10:15:00Z' },
+ }),
+ )
+ vi.stubGlobal('fetch', fetch)
+
+ const first = (await getOpenItems(null)) as OpenItemsPoll
+ expect(first.page.asOf).toBe('2026-11-19T10:00:00Z')
+ const next = await getOpenItems('"h1"')
+ expect(next).toEqual({ notModified: true, asOf: '2026-11-19T10:15:00Z' })
+
+ const kept = keepOpenItems(first, next)
+ expect(kept.etag).toBe('"h1"')
+ expect(kept.page.items).toBe(first.page.items)
+ expect(kept.page.asOf).toBe('2026-11-19T10:15:00Z')
+ // A 304 without the header keeps everything as it was.
+ expect(keepOpenItems(first, { notModified: true, asOf: null })).toBe(first)
+ })
+})
+
+describe('preview mock of the poll', () => {
+ it('answers 304 with Work-As-Of when the ETag still matches', async () => {
+ const { workOpenItemsResponse } = await import('@/api/mock-work')
+ const first = workOpenItemsResponse(null)
+ expect(first.status).toBe(200)
+ const etag = first.headers.get('ETag')
+ expect(etag).toBeTruthy()
+ expect(first.headers.get('Work-As-Of')).toBeTruthy()
+ const again = workOpenItemsResponse(etag)
+ expect(again.status).toBe(304)
+ expect(again.headers.get('Work-As-Of')).toBeTruthy()
+ expect(workOpenItemsResponse('"other"').status).toBe(200)
+ })
})
describe('repository setting', () => {
diff --git a/web/src/managed-agents/work/api.ts b/web/src/managed-agents/work/api.ts
index 42d3b1cb..2f9da90b 100644
--- a/web/src/managed-agents/work/api.ts
+++ b/web/src/managed-agents/work/api.ts
@@ -161,33 +161,49 @@ export function putRepository(choice: RepositoryChoice) {
/** What the open-items poll keeps: the page and the ETag it came with. */
export type OpenItemsPoll = { etag: string | null; page: OpenItems }
+/** A 304: the page is unchanged, but the index was read again at `asOf`. */
+export type OpenItemsUnchanged = { notModified: true; asOf: string | null }
+
+// The server sends how fresh its index is on every answer, 200 or 304, so a
+// quiet board does not drift into "As of N ago" while nothing changes.
+const AS_OF_HEADER = 'Work-As-Of'
+
/**
* All open items. With the ETag of the last answer the server says 304 when
- * nothing changed (head and attention unchanged); that returns null, and the
- * caller keeps what it has.
+ * nothing changed; that returns only the new `asOf`, and the caller keeps the
+ * page it has (`keepOpenItems`).
*/
export async function getOpenItems(
etag: string | null,
-): Promise<OpenItemsPoll | null> {
+): Promise<OpenItemsPoll | OpenItemsUnchanged> {
const path = `${PREFIX}/items?stage=open`
+ const init = etag ? { headers: { 'If-None-Match': etag } } : {}
+ let res: Response
if (import.meta.env.VITE_PREVIEW === '1') {
- return {
- etag: null,
- page: await apiFetch(path, undefined, OpenItemsSchema),
- }
+ const { workOpenItemsResponse } = await import('@/api/mock-work')
+ res = workOpenItemsResponse(etag)
+ } else {
+ res = await apiFetchResponse(path, init, '/api')
}
- const res = await apiFetchResponse(
- path,
- etag ? { headers: { 'If-None-Match': etag } } : {},
- '/api',
- )
- if (res.status === 304) return null
+ const asOf = res.headers.get(AS_OF_HEADER)
+ if (res.status === 304) return { notModified: true, asOf }
+ const page = validate(OpenItemsSchema, await res.json(), path)
return {
etag: res.headers.get('ETag'),
- page: validate(OpenItemsSchema, await res.json(), path),
+ page: asOf ? { ...page, asOf } : page,
}
}
+/** The poll's next value: a new page, or the old one with the new `asOf`. */
+export function keepOpenItems(
+ prev: OpenItemsPoll,
+ next: OpenItemsPoll | OpenItemsUnchanged,
+): OpenItemsPoll {
+ if (!('notModified' in next)) return next
+ if (!next.asOf || next.asOf === prev.page.asOf) return prev
+ return { ...prev, page: { ...prev.page, asOf: next.asOf } }
+}
+
export function getClosedItems(cursor: string | undefined, limit = 50) {
const params = new URLSearchParams({ stage: 'closed', limit: String(limit) })
if (cursor) params.set('cursor', cursor)