Public: anyone can read this; the crew can change it.

/api/v1/forge/view can be followed as an event stream, so an outside agent stops polling #15278

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/forge:torque/15264-api-v1-forge-view-can-be-followed-as-an → main 6 commits fork 4 threads · 4 unresolved

  • Merged. torque/15264-api-v1-forge-view-can-be-followed-as-an was merged into main as bdec6be.

Review

Sign in to review this change.
10 files changed +360 −10
docs/features/forge-view.md +7 −2
@@ -25,7 +25,12 @@ composed, so its numbers are the ones home and the board show.
2525 - **Cheap to leave on.** The answer is built at most once per two seconds per caller (`VIEW_TTL_MS`), carries an
2626 `ETag` that ignores `at`, and a poll that holds it gets a 304. Lists are bounded: queued and running runs at 50 (the `LIMIT 50` of `listActiveRuns`, which `crewNow` reads), the
2727 rows waiting on a person by what `crewNow` finds, and apps at 200 (`VIEW_APPS`; `counts.apps` is still the true number). Poll every
28 2–5 s; there is no stream, deliberately, so an always-on view adds no connection to hold (#7559).
28 2–5 s, or follow the stream below.
29 - **Stream.** `Accept: text/event-stream` on the same path sends the same body (same caller, same visibility) as a
30 `view` event: once on connect, then when it has changed (the `at` is ignored), at most once per `VIEW_GAP_MS`
31 (2 s), woken by the forge's event bus. A `: ping` comment goes every 25 s. The stream ends when its token is
32 revoked or expires, and when the account is suspended; each send re-reads the caller, so a changed role shows as on the next poll. A client that goes away frees its place. At
33 most 3 streams per token (a browser session or account counts as its own credential); the 4th gets a 429.
2934
3035 **MCP.** The `forge_view` tool (read-only, no arguments) answers the same body for the same caller, from the same two-second build.
3136
@@ -37,4 +42,4 @@ forge depends on it.
3742 **Terminal.** The palette's **Forge view** opens a screen with the view's counts and the Foundry address (when there
3843 is a door); Enter opens Foundry on the web.
3944
40 Not done: a push stream over HTTP (outside callers still poll `/api/v1/forge/view` with ETag; the terminal screen is live: it re-reads when the in-process event feed moves, at most once per refresh gap, with no timer), and anything that writes.
45 Not done: anything that writes. (The terminal screen is live too: it re-reads when the in-process event feed moves, at most once per refresh gap, with no timer.)
src/actions/forge_view.ts +2 −2
@@ -69,11 +69,11 @@ export function forgeView(ctx: Pick<Ctx, "app">, user: User): ForgeView {
6969 }
7070
7171 /** The view as JSON text for this reader, reused for `VIEW_TTL_MS`. */
72 export function forgeViewText(ctx: Pick<Ctx, "app">, user: User, now = Date.now()): string {
72 export function forgeViewText(ctx: Pick<Ctx, "app">, user: User, now = Date.now(), ttl = VIEW_TTL_MS): string {
7373 let mine = kept.get(ctx.app);
7474 if (mine === undefined) kept.set(ctx.app, (mine = new Map()));
7575 const held = mine.get(user.id);
76 if (held !== undefined && now - held.at < VIEW_TTL_MS) return held.text;
76 if (held !== undefined && now - held.at < ttl) return held.text;
7777 const text = JSON.stringify({ ...forgeView(ctx, user), at: new Date(now).toISOString() });
7878 if (mine.size > 200) mine.clear();
7979 mine.set(user.id, { at: now, text });
src/contract/openapi/routes/getApiV1ForgeView.ts +2 −2
@@ -4,9 +4,9 @@ export const route: Route = {
44 method: "get",
55 path: "/api/v1/forge/view",
66 summary: "The forge as one read-only picture for a reader that draws it",
7 detail: "The forge as one read-only picture for a reader that draws it: { counts: { apps, running, queued, waiting }, apps: [{ app, slug, access, running, queued }], runs: [the rows of /api/v1/crew/now; each run's state is its station], health: as /api/v1/health answers this caller, at } for the apps the caller may read. Any token scope reads it; it is built at most once per two seconds per caller and answers 304 to a held ETag, so polling it is cheap",
7 detail: "The forge as one read-only picture for a reader that draws it: { counts: { apps, running, queued, waiting }, apps: [{ app, slug, access, running, queued }], runs: [the rows of /api/v1/crew/now; each run's state is its station], health: as /api/v1/health answers this caller, at } for the apps the caller may read. Any token scope reads it; it is built at most once per two seconds per caller and answers 304 to a held ETag, so polling it is cheap. With Accept: text/event-stream it is a stream of the same body as `view` events, sent when it changes at most once per two seconds, with a `: ping` comment every 25 s; it ends when the token is revoked, and a token holds at most 3",
88 auth: true,
99 role: "member",
10 refusals: { unauthorized: "no valid credentials were sent" },
10 refusals: { unauthorized: "no valid credentials were sent", too_many: "this token already holds 3 view streams (429)" },
1111 refuses: [401],
1212 };
src/core/tokens.ts +14 −3
@@ -81,7 +81,7 @@ export function verifyToken(
8181 db: Database,
8282 presented: string,
8383 ): Result<
84 { user: User; scope: TokenScope; name: string; proposal?: Proposal; run?: RunScope; via?: string; contribute?: ContributeScope },
84 { user: User; scope: TokenScope; name: string; id: string; proposal?: Proposal; run?: RunScope; via?: string; contribute?: ContributeScope },
8585 "invalid_token"
8686 > {
8787 const row = db
@@ -141,6 +141,7 @@ export function verifyToken(
141141 return ok({
142142 user,
143143 name: row.name,
144 id: row.id,
144145 scope: row.scope,
145146 proposal: {
146147 tokenId: row.id,
@@ -157,6 +158,7 @@ export function verifyToken(
157158 return ok({
158159 user,
159160 name: row.name,
161 id: row.id,
160162 scope: row.scope,
161163 run: {
162164 repoId: row.r_repo,
@@ -170,6 +172,7 @@ export function verifyToken(
170172 return ok({
171173 user,
172174 name: row.name,
175 id: row.id,
173176 scope: row.scope,
174177 contribute: {
175178 tokenId: row.id,
@@ -179,8 +182,8 @@ export function verifyToken(
179182 },
180183 });
181184 // The forge's own agent, speaking for this person (`core/via_token.ts`).
182 if (row.v_via !== null) return ok({ user, name: row.name, scope: row.scope, via: row.v_via });
183 return ok({ user, name: row.name, scope: row.scope });
185 if (row.v_via !== null) return ok({ user, name: row.name, id: row.id, scope: row.scope, via: row.v_via });
186 return ok({ user, name: row.name, id: row.id, scope: row.scope });
184187 }
185188
186189 export function listTokens(db: Database, userId: string): TokenInfo[] {
@@ -298,3 +301,11 @@ export function renewToken(
298301 .run(expiresAt, tokenId).changes > 0
299302 );
300303 }
304
305 /**
306 * Whether a token still works: on file and not past `expires_at`, the rule `verifyToken` applies.
307 */
308 export function tokenLive(db: Database, tokenId: string): boolean {
309 const row = db.query<{ expires_at: string | null }, [string]>("SELECT expires_at FROM token WHERE id = ?1").get(tokenId);
310 return row !== null && (row.expires_at === null || Date.parse(row.expires_at) > Date.now());
311 }
src/http/api/forge_view.ts +3 −1
@@ -8,6 +8,7 @@ import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.t
88 import type { Handler } from "../context.ts";
99 import { holds, weakEtag } from "../etag.ts";
1010 import { requireScope } from "../respond.ts";
11 import { viewStream, wantsViewStream } from "../view_stream.ts";
1112 import type { Router } from "../router.ts";
1213
1314 /**
@@ -36,10 +37,11 @@ export function forgeViewRoutes(router: Router<Handler>): void {
3637 const denied = requireScope(ctx, "ro");
3738 if (denied) return denied;
3839 const user = ctx.auth.user!;
40 if (wantsViewStream(ctx.request)) return viewStream(ctx);
3941 const text = timePhase(ctx.request, "view", () => recentView(user.id, () => forgeViewText(ctx, user)));
4042 // The tag leaves out the moment it was built, so an unchanged forge is a 304.
4143 const etag = weakEtag(text.replace(/,"at":"[^"]*"\}$/, "}"));
42 const headers = { etag, "cache-control": "private, max-age=2", vary: "authorization, cookie" };
44 const headers = { etag, "cache-control": "private, max-age=2", vary: "authorization, cookie, accept" };
4345 if (holds(ctx.request, etag)) return new Response(null, { status: 304, headers });
4446 return new Response(text, { headers: { "content-type": "application/json", ...headers } });
4547 });
src/http/context.ts +2
@@ -28,6 +28,8 @@ export interface Auth {
2828 readonly noticeWaiting?: true;
2929 /** Set for a token: the name its owner gave it, never its secret. */
3030 readonly tokenName?: string;
31 /** Set for a token: its id, so what it opens can end when it is revoked. */
32 readonly tokenId?: string;
3133 /** Set for a proposal token: all it may do (`http/proposal_gate.ts`). */
3234 readonly proposal?: Proposal;
3335 /** Set for a crew run's token: the repositories it may reach
src/http/server.ts +2
@@ -783,6 +783,7 @@ async function resolveAuth(app: App, request: Request): Promise<Auth> {
783783 scope: verified.value.scope,
784784 via: "token",
785785 tokenName: verified.value.name,
786 tokenId: verified.value.id,
786787 ...(verified.value.proposal ? { proposal: verified.value.proposal } : {}),
787788 ...(verified.value.run ? { run: verified.value.run } : {}),
788789 ...(verified.value.contribute ? { contribute: verified.value.contribute } : {}),
@@ -810,6 +811,7 @@ async function resolveAuth(app: App, request: Request): Promise<Auth> {
810811 scope: verified.value.scope,
811812 via: "token",
812813 tokenName: verified.value.name,
814 tokenId: verified.value.id,
813815 ...(verified.value.proposal
814816 ? { proposal: verified.value.proposal }
815817 : {}),
src/http/view_stream.ts added +122
@@ -0,0 +1,122 @@
1 /**
2 * The forge view as an event stream (`GET /api/v1/forge/view` with
3 * `Accept: text/event-stream`): the same role-aware body a poll gets
4 * (`forgeViewText`), sent as a `view` event when it has changed, never more
5 * than once per `VIEW_GAP_MS`. The forge's own bus says when something may
6 * have moved; there is no timer but the heartbeat `eventStream` already has
7 * and a slow check that the caller and token still stand.
8 *
9 * A stream ends with its token (revoked, expired, its person suspended) and
10 * is held at most `STREAMS_PER_TOKEN` at a time per credential.
11 */
12 import { forgeViewText, VIEW_TTL_MS } from "../actions/forge_view.ts";
13 import { tokenLive } from "../core/tokens.ts";
14 import { getUserById, isActive } from "../core/users.ts";
15 import { heldOf } from "../perf/held.ts";
16 import { during } from "../perf/jobs.ts";
17 import type { Handler } from "./context.ts";
18 import { eventStream } from "./sse.ts";
19 import { tooManyStrangers } from "./strangers.ts";
20
21 type Ctx = Parameters<Handler>[0];
22
23 /** The least time between two views sent on one stream. */
24 export const VIEW_GAP_MS = VIEW_TTL_MS;
25 /**
26 * The gap a stream keeps: `FORGE_VIEW_GAP_MS` (0 to `VIEW_GAP_MS`) shortens it, for tests; else
27 * `VIEW_GAP_MS`.
28 */
29 function gapMs(): number {
30 const n = Number(process.env.FORGE_VIEW_GAP_MS ?? "");
31 return Number.isInteger(n) && n >= 0 && n <= VIEW_GAP_MS ? n : VIEW_GAP_MS;
32 }
33 /** Streams one credential (a token, else a browser, else an account) may hold. */
34 export const STREAMS_PER_TOKEN = 3;
35 /** How often a stream re-checks its caller and token when nothing else wakes it. */
36 const TOKEN_CHECK_MS = 30_000;
37
38 const heldByApp = new WeakMap<object, Map<string, number>>();
39
40 /** This forge's open streams by credential key, named in its held registry. */
41 function heldIn(app: object): Map<string, number> {
42 let m = heldByApp.get(app);
43 if (m === undefined) {
44 const made = new Map<string, number>();
45 heldOf(app).add("view streams", () => {
46 let n = 0;
47 for (const v of made.values()) n += v;
48 return n;
49 });
50 heldByApp.set(app, (m = made));
51 }
52 return m;
53 }
54
55 /** Whose count a stream is: the token, else the browser session, else the account. */
56 const keyOf = (ctx: Ctx): string =>
outdated

includes accepts text/event-stream;q=0. Parse the Accept header properly.

57 ctx.auth.tokenId !== undefined
58 ? `token:${ctx.auth.tokenId}`
59 : ctx.auth.sessionId !== undefined
60 ? `session:${ctx.auth.sessionId}`
outdated

user is captured once at connect. Re-read the account on each send so a role change or suspension takes effect on an open stream, as it would on the next poll.

61 : `user:${ctx.auth.user!.id}`;
62
63 /** The view's text without the moment it was built, so "changed" ignores `at`. */
64 const bodyOf = (text: string): string => text.replace(/,"at":"[^"]*"\}$/, "}");
65
66 export function wantsViewStream(request: Request): boolean {
67 return (request.headers.get("accept") ?? "").split(",").some((range) => {
68 const [type, ...params] = range.split(";").map((p) => p.trim().toLowerCase());
69 if (type !== "text/event-stream") return false;
70 const q = params.find((p) => p.startsWith("q="));
71 return q === undefined || Number(q.slice(2)) > 0;
72 });
73 }
74
75 export function viewStream(ctx: Ctx): Response {
76 const gap = gapMs();
77 const key = keyOf(ctx);
78 const held = heldIn(ctx.app);
79 if ((held.get(key) ?? 0) >= STREAMS_PER_TOKEN)
80 return tooManyStrangers({ retryAfter: 30, why: `at most ${STREAMS_PER_TOKEN} view streams per token` });
81 return eventStream(ctx.app, ctx.request, (emit, close) => {
82 held.set(key, (held.get(key) ?? 0) + 1);
83 let last: string | undefined;
84 let sentAt = Number.NEGATIVE_INFINITY;
85 let timer: ReturnType<typeof setTimeout> | null = null;
86 let done = false;
87 const send = (): void => {
outdated

This only checks that the row exists, so an expired token keeps its stream. Apply the expiry rule verifyToken uses. It also runs a DB read on every bus event for every stream: move the check to the timer or to once per send.

88 timer = null;
89 if (done) return;
90 // Whoever this is now, as the next poll would find them: a lapsed token,
91 // a suspended or removed account, ends the stream.
92 const user = getUserById(ctx.app.db, ctx.auth.user!.id);
93 if (!user || !isActive(user) || (ctx.auth.tokenId !== undefined && !tokenLive(ctx.app.db, ctx.auth.tokenId)))
94 return close();
95 const text = forgeViewText(ctx, user, Date.now(), gap);
96 const body = bodyOf(text);
97 sentAt = Date.now();
98 if (body === last) return;
99 last = body;
100 emit("view", JSON.parse(text), String(sentAt));
101 };
102 const soon = (): void => {
103 if (timer !== null || done) return;
104 timer = setTimeout(() => during("view-stream-send", send), Math.max(40, sentAt + gap - Date.now()));
105 };
106 // The bus only says something may have moved; who the caller is and
107 // whether the token lives are asked in `send`, at most once per gap.
108 const unsubscribe = ctx.app.bus.subscribe(soon, "ordered");
109 const check = setInterval(() => during("view-stream-token-check", send), TOKEN_CHECK_MS);
110 send();
111 return () => {
112 if (done) return;
113 done = true;
114 unsubscribe();
115 if (timer !== null) clearTimeout(timer);
116 clearInterval(check);
117 const n = (held.get(key) ?? 1) - 1;
118 if (n <= 0) held.delete(key);
119 else held.set(key, n);
120 };
121 });
122 }
test/forge-view-stream.test.ts added +204
@@ -0,0 +1,204 @@
1 /**
2 * GET /api/v1/forge/view as an event stream: the poll's body, sent on change, ended by revocation,
3 * bounded.
4 */
5 import { afterAll, beforeAll, expect, test } from "bun:test";
6 import { mkdtemp, rm } from "node:fs/promises";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { createApp, type App } from "../src/app.ts";
10 import { createRepo } from "../src/actions/repos.ts";
11 import { createApp as createAppRecord } from "../src/core/apps.ts";
12 import { createUser } from "../src/core/users.ts";
13 import { createToken, deleteToken } from "../src/core/tokens.ts";
14 import { serve, type Servers } from "../src/http/server.ts";
15 import { heldOf } from "../src/perf/held.ts";
16 import { STREAMS_PER_TOKEN } from "../src/http/view_stream.ts";
17 import type { User } from "../src/core/users.ts";
18 import { pollUntil } from "./helpers/poll.ts";
19 import { seedAdmin } from "./helpers/admin.ts";
20
21 let dir: string;
22 let app: App;
23 let servers: Servers;
24 let userId: string;
25 let admin: User;
26 let token: { id: string; token: string };
27
28 beforeAll(async () => {
29 process.env.FORGE_VIEW_GAP_MS = "50";
30 dir = await mkdtemp(join(tmpdir(), "fv-stream-"));
31 app = createApp({ dataDir: dir, port: 0 });
32 servers = await serve(app);
33 const made = await seedAdmin(app, { username: "ada", email: "[email protected]" });
34 if (!made.ok) throw new Error(made.error);
35 admin = made.value;
36 userId = admin.id;
37 token = createToken(app.db, userId, "viewer", "ro");
38 });
39 afterAll(async () => {
40 delete process.env.FORGE_VIEW_GAP_MS;
41 servers.stop();
42 app.close();
43 await rm(dir, { recursive: true, force: true });
44 });
45
46 /** Streams this forge holds open now, across every credential. */
47 const openStreams = () => heldOf(app).read().find((h) => h.name === "view streams")?.size ?? 0;
48
49 /** Resolves null after `ms`, on the abort clock. */
50 const after = (ms: number) => new Promise<null>((r) => AbortSignal.timeout(ms).addEventListener("abort", () => r(null)));
51
52 const open = (secret: string, signal?: AbortSignal) =>
53 fetch(`${servers.url}/api/v1/forge/view`, { headers: { authorization: `Bearer ${secret}`, accept: "text/event-stream" }, ...(signal ? { signal } : {}) });
54
55 /** Reads frames until `n` `view` events are in, or the stream ends/times out. */
56 async function views(res: Response, n: number, ms = 8000): Promise<{ data: any[]; ended: boolean }> {
57 const reader = res.body!.getReader();
58 const decoder = new TextDecoder();
59 let buf = "";
60 const deadline = Date.now() + ms;
61 let ended = false;
62 const count = () => buf.split("\n\n").filter((f) => f.includes("event: view")).length;
63 while (count() < n) {
64 if (Date.now() >= deadline) break;
65 const r = await Promise.race([reader.read(), after(Math.max(1, deadline - Date.now()))]);
66 if (r === null) break;
67 if (r.done) {
68 ended = true;
69 break;
70 }
71 buf += decoder.decode(r.value, { stream: true });
72 }
73 reader.releaseLock();
74 const data = buf
75 .split("\n\n")
76 .filter((f) => f.includes("event: view"))
77 .map((f) => JSON.parse(f.split("\n").find((l) => l.startsWith("data: "))!.slice(6)));
78 return { data, ended };
79 }
80
81 test("two events across one change; the first is the poll's body", async () => {
82 const repo = await createRepo(app, admin, { name: "thing", visibility: "public" });
83 if (!repo.ok) throw new Error(String(repo.error));
84 const abort = new AbortController();
85 const res = await open(token.token, abort.signal);
86 expect(res.status).toBe(200);
87 expect(res.headers.get("content-type")).toContain("text/event-stream");
88 const pending = views(res, 2);
89 createAppRecord(app.db, { repoId: repo.value.id, slug: "one", access: "public" });
90 app.bus.append({ kind: "run.started" });
91 const { data } = await pending;
92 expect(data.length).toBe(2);
93 expect(data[1].counts.apps).toBe(data[0].counts.apps + 1);
94 expect(Object.keys(data[0]).sort()).toEqual(["apps", "counts", "health", "runs", "at"].sort());
95 abort.abort();
96 });
97
98 test("a stream ends when its token is revoked, and leaves nothing held", async () => {
99 const t = createToken(app.db, userId, "short", "ro");
100 const res = await open(t.token);
101 const first = await views(res, 1);
102 expect(first.data.length).toBe(1);
103 // The reader was released; read on for the end of the stream.
104 expect(openStreams()).toBe(1);
105 const reader = res.body!.getReader();
106 deleteToken(app.db, userId, t.id);
107 app.bus.append({ kind: "token.deleted", payload: { tokenId: t.id } });
108 const end = await Promise.race([
109 (async () => {
110 for (;;) if ((await reader.read()).done) return "ended";
111 })(),
112 after(4000).then(() => "open"),
113 ]);
114 expect(end).toBe("ended");
115 expect(openStreams()).toBe(0);
116 });
117
118 test("at most three streams per token; an abort frees a place", async () => {
119 const t = createToken(app.db, userId, "many", "ro");
120 const aborts = Array.from({ length: STREAMS_PER_TOKEN }, () => new AbortController());
121 const open3 = await Promise.all(aborts.map((a) => open(t.token, a.signal)));
122 for (const r of open3) expect(r.status).toBe(200);
123 expect(openStreams()).toBe(STREAMS_PER_TOKEN);
124 const fourth = await open(t.token);
125 expect(fourth.status).toBe(429);
126 aborts[0]!.abort();
127 await pollUntil(() => openStreams() < STREAMS_PER_TOKEN, "an aborted stream gives its place back", 5000);
128 expect(openStreams()).toBe(STREAMS_PER_TOKEN - 1);
129 const again = new AbortController();
130 expect((await open(t.token, again.signal)).status).toBe(200);
131 again.abort();
132 for (const a of aborts) a.abort();
133 await pollUntil(() => openStreams() === 0, "every stream let go", 5000);
134 expect(openStreams()).toBe(0);
135 });
136
137 test("without the header the route still answers JSON", async () => {
138 const res = await fetch(`${servers.url}/api/v1/forge/view`, { headers: { authorization: `Bearer ${token.token}` } });
139 expect(res.headers.get("content-type")).toContain("application/json");
140 });
141
142 test("a stream ends when its token has expired, though the row is still on file", async () => {
143 const t = createToken(app.db, userId, "brief", "ro", { expiresAt: new Date(Date.now() + 60_000).toISOString() });
144 const res = await open(t.token);
145 expect((await views(res, 1)).data.length).toBe(1);
146 const reader = res.body!.getReader();
147 app.db.query("UPDATE token SET expires_at = ?2 WHERE id = ?1").run(t.id, new Date(Date.now() - 1000).toISOString());
148 expect(app.db.query("SELECT 1 FROM token WHERE id = ?1").get(t.id)).not.toBeNull();
149 app.bus.append({ kind: "run.started" });
150 const end = await Promise.race([
151 (async () => {
152 for (;;) if ((await reader.read()).done) return "ended";
153 })(),
154 after(4000).then(() => "open"),
155 ]);
156 expect(end).toBe("ended");
157 await pollUntil(() => openStreams() === 0, "the expired stream let go", 5000);
158 });
159
160 test("a caller demoted mid-stream stops seeing what a poll would hide", async () => {
161 const made = await createUser(app.db, { username: "bob", email: "[email protected]", origin: "seed", isAdmin: true, trusted: true });
162 if (!made.ok) throw new Error(made.error);
163 const boss = await createUser(app.db, { username: "boss", email: "[email protected]", origin: "seed", isAdmin: true, trusted: true });
164 if (!boss.ok) throw new Error(boss.error);
165 const secret = createToken(app.db, boss.value.id, "boss", "ro");
166 const repo = await createRepo(app, made.value, { name: "hidden", visibility: "private" });
167 if (!repo.ok) throw new Error(String(repo.error));
168 createAppRecord(app.db, { repoId: repo.value.id, slug: "hidden", access: "private" });
169 const abort = new AbortController();
170 const pending = views(await open(secret.token, abort.signal), 2);
171 await pollUntil(() => openStreams() === 1, "the stream is open", 5000);
172 app.db.query("UPDATE user SET is_admin = 0 WHERE id = ?1").run(boss.value.id);
173 app.bus.append({ kind: "run.started" });
174 const { data } = await pending;
175 expect(data.length).toBe(2);
176 expect(data[0].apps.some((a: any) => a.app === "bob/hidden")).toBe(true);
177 expect(data[1].apps.some((a: any) => a.app === "bob/hidden")).toBe(false);
178 abort.abort();
179 });
180
181 test("a suspended caller's stream ends", async () => {
182 const made = await createUser(app.db, { username: "cy", email: "[email protected]", origin: "seed", trusted: true });
183 if (!made.ok) throw new Error(made.error);
184 const t = createToken(app.db, made.value.id, "cy", "ro");
185 const res = await open(t.token);
186 expect((await views(res, 1)).data.length).toBe(1);
187 const reader = res.body!.getReader();
188 app.db.query("UPDATE user SET suspended_at = ?2 WHERE id = ?1").run(made.value.id, new Date().toISOString());
189 app.bus.append({ kind: "run.started" });
190 const end = await Promise.race([
191 (async () => {
192 for (;;) if ((await reader.read()).done) return "ended";
193 })(),
194 after(4000).then(() => "open"),
195 ]);
196 expect(end).toBe("ended");
197 });
198
199 test("an Accept that refuses the stream (q=0) still gets JSON", async () => {
200 const res = await fetch(`${servers.url}/api/v1/forge/view`, {
201 headers: { authorization: `Bearer ${token.token}`, accept: "text/event-stream;q=0, application/json" },
202 });
203 expect(res.headers.get("content-type")).toContain("application/json");
204 });
test/job-slices.test.ts +2
@@ -265,6 +265,8 @@ const UNTIMED: Record<string, string> = {
265265 "git-pace": "a one-shot timer in git/pace.ts that only resolves the promise a paced pack write waits on: no work of its own, armed per pack chunk while a page response is in flight; git-pace.test.ts drives the pacer with a fake clock",
266266 "drill-watch": "a /proc read of the drill child's memory and a state write every WATCH_MS, only while a restore drill runs in its own process; drill-job tests drive runDrillInChild directly",
267267 "meter-watch": "one indexed read of held-full windows every 15 minutes, and a status fetch only while one is held full; meter-watch.test.ts drives it with a fake fetcher; the fixture holds no window",
268 "view-stream-send": "re-reads the forge view for one open stream, at most once per VIEW_GAP_MS, only while an agent follows the view; the view is cached and forge-view-stream.test.ts drives it",
269 "view-stream-token-check": "one user read and one token read every 30 s per open stream; forge-view-stream.test.ts drives revocation",
268270 desk: "expires notes past their time: one indexed delete on the desk table every 30 s (actions/desk.ts); the fixture has no notes",
269271 ledger: "walks the cost ledger's hash chain at boot and once a day (#12273): file reads proportional to the ledger, off the request path; the fixture has no ledger",
270272 "full-disk-probe": "one 64 KiB write every 2 s, only while the disk is full; the full-disk test drives probeFull directly",

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs