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

The API's agent read names its Server-Timing phases: session, agent, render #4960

Merged opened this

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

Merged

scout/forge:scout/4946-the-api-s-agent-read-names-its-server-ti → main 1 commit fork

  • Merged. scout/4946-the-api-s-agent-read-names-its-server-ti was merged into main as d70f243.

Review

Sign in to review this change.
2 files changed +74 −5
src/http/api.ts +7 −5
@@ -6183,13 +6183,15 @@ export function apiRoutes(router: Router<Handler>): void {
61836183 });
61846184
61856185 router.add("GET", "/api/v1/agents/:username", (ctx) => {
6186 const agent = getAgentView(
6187 ctx.app,
6188 ctx.auth.user,
6189 ctx.params.username ?? "",
6186 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
6187 const agent = timePhase(ctx.request, "agent", () =>
6188 getAgentView(ctx.app, ctx.auth.user, ctx.params.username ?? ""),
61906189 );
61916190 if (!agent.ok) return respondAccess(agent.error, 404);
6192 return json(agentFor(agent.value, ctx.auth.user));
6191 const view = agent.value;
6192 return timePhase(ctx.request, "render", () =>
6193 json(agentFor(view, ctx.auth.user)),
6194 );
61936195 });
61946196
61956197 router.add("PATCH", "/api/v1/agents/:username", async (ctx) => {
test/api-agent-timing.test.ts added +67
@@ -0,0 +1,67 @@
1 /** The API agent read names its Server-Timing phases (#4946). */
2 import { afterAll, beforeAll, expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createApp, type App } from "../src/app.ts";
7 import { createAgent } from "../src/actions/crew.ts";
8 import { createSession } from "../src/core/sessions.ts";
9 import { createUser } from "../src/core/users.ts";
10 import { serve, type Servers } from "../src/http/server.ts";
11 import { quietLogs } from "./helpers/globals.ts";
12
13 quietLogs();
14
15 let dataDir: string;
16 let app: App;
17 let servers: Servers;
18 let cookie = "";
19
20 const cells = process.env["FORGE_CELLS"];
21
22 beforeAll(async () => {
23 process.env["FORGE_CELLS"] = "off";
24 dataDir = await mkdtemp(join(tmpdir(), "forge-api-agent-timing-"));
25 app = createApp({ dataDir, port: 0 });
26 servers = await serve(app);
27 const made = await createUser(app.db, {
28 origin: "seed",
29 trusted: true,
30 username: "alice",
31 email: "[email protected]",
32 password: "a-good-passphrase",
33 adminIfFirst: true,
34 });
35 if (!made.ok) throw new Error(String(made.error));
36 const alice = made.value;
37 cookie = `forge_session=${createSession(app.db, alice.id).token}`;
38 const bot = await createAgent(app, alice, { username: "bot" });
39 if (!bot.ok) throw new Error(String(bot.error));
40 });
41 afterAll(async () => {
42 await servers.stop();
43 app.close();
44 if (cells === undefined) delete process.env["FORGE_CELLS"];
45 else process.env["FORGE_CELLS"] = cells;
46 await rm(dataDir, { recursive: true, force: true });
47 });
48
49 async function phases(path: string): Promise<{ status: number; names: string[]; body: string }> {
50 const res = await fetch(`${servers.url}${path}`, { headers: { cookie } });
51 const body = await res.text();
52 const names = (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!);
53 return { status: res.status, names: names.slice(names.indexOf("app") + 1), body };
54 }
55
56 test("the agent read names session, agent, render in order", async () => {
57 const got = await phases("/api/v1/agents/bot");
58 expect(got.status).toBe(200);
59 expect(got.names).toEqual(["session", "agent", "render"]);
60 expect(JSON.parse(got.body).username).toBe("bot");
61 });
62
63 test("a missing agent is a 404 naming session and agent", async () => {
64 const got = await phases("/api/v1/agents/nope");
65 expect(got.status).toBe(404);
66 expect(got.names).toEqual(["session", "agent"]);
67 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs