Skip to content

Commit 9109062

Browse files
fix: mask endpoint ids by route position; keep the status when the body stalls
On identifier-first routers (skills, memory) the first subpath segment is masked whatever it is spelled like; a body that times out after the server answered reports that answer's status class with the status. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_011AhtJPr3nmT1Po92ZQk4VE
1 parent d7ff8d3 commit 9109062

2 files changed

Lines changed: 68 additions & 10 deletions

File tree

‎packages/opencode/src/altimate/workspace/api-client.ts‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,23 +178,35 @@ async function creds(): Promise<{ url: string; instance: string; apiKey: string
178178
// `workspace_api_error` (or `workspace_auth_expired` on 401/403) with the endpoint masked of
179179
// ids; the link lifecycle functions below report linked / created / switched / unlinked on the
180180
// success response and `workspace_link_failed` with a reason class otherwise.
181-
/** The static segments of the routes the workspace clients call. Anything not in this list —
182-
* numeric ids, uuids, memory ids, skill public ids however they are spelled — is replaced by
183-
* ":id" in telemetry. A new static word that is missing here is masked too, which only costs
184-
* detail; the list must never be widened with a dynamic value. */
181+
/** Endpoint labels mask by ROUTE POSITION, not by spelling. The routers whose subpaths start
182+
* with a static word are listed here; on every other base (``/skills``, ``/datamates/memory``)
183+
* the first subpath segment is an identifier and is masked whatever it is spelled like — a
184+
* public id equal to a route word must not reach telemetry. Segments after that, and the
185+
* segments of the base itself, are kept only when they are known static words; an unknown
186+
* static word is masked too, which only costs detail. A new router with a static first segment
187+
* is added here; a new identifier-first router needs nothing. */
188+
const STATIC_FIRST_SEGMENT_BASES = new Set(["/datamate-project-bindings", "/datamates", "/users"])
185189
const ROUTE_WORDS = new Set([
186190
"datamate-project-bindings", "bind", "by-remote", "by-path", "datamates", "users", "me",
187191
"memory", "blocks", "skills", "files", "list", "attach", "publish", "sync", "status", "search",
188192
"versions", "lessons", "learned-lessons", "content", "download",
189193
])
190194

191-
/** "METHOD /base/path" with every dynamic segment replaced by ":id" (see ROUTE_WORDS). */
195+
/** "METHOD /base/path" with every dynamic segment replaced by ":id" (see the note above). */
192196
export function endpointOf(method: string, basePath: string, subpath: string): string {
193-
const masked = `${basePath}${subpath}`
197+
const word = (seg: string) => (seg === "" || ROUTE_WORDS.has(seg) ? seg : ":id")
198+
const base = basePath.split("/").map(word).join("/")
199+
let first = true
200+
const sub = subpath
194201
.split("/")
195-
.map((seg) => (seg === "" || ROUTE_WORDS.has(seg) ? seg : ":id"))
202+
.map((seg) => {
203+
if (seg === "") return seg
204+
const idPosition = first && !STATIC_FIRST_SEGMENT_BASES.has(basePath)
205+
first = false
206+
return idPosition ? ":id" : word(seg)
207+
})
196208
.join("/")
197-
return `${method} ${masked}`
209+
return `${method} ${base}${sub}`
198210
}
199211

200212
function workspaceIdIn(body: unknown): number | undefined {
@@ -384,7 +396,15 @@ async function req<T>(
384396
// The abort fires equally when it kills the fetch OR the body read. (m8)
385397
const name = (err as { name?: string } | undefined)?.name
386398
if (name === "AbortError") {
387-
reportApiError({ endpoint, kind: "timeout", workspaceId: bodyWorkspaceId })
399+
// A timeout after the server answered (the body stalled) keeps the answer's status:
400+
// a 500 whose body never arrives is still a 5xx row; a 2xx whose body never arrives
401+
// stays a timeout, with the status attached.
402+
reportApiError({
403+
endpoint,
404+
...(answered !== undefined ? { status: answered } : {}),
405+
kind: answered === undefined ? "timeout" : answered >= 500 ? "5xx" : answered >= 400 ? "4xx" : "timeout",
406+
workspaceId: bodyWorkspaceId,
407+
})
388408
throw new WorkspaceApiError(
389409
`Request to ${target} timed out after ${Math.round(timeoutMs / 1000)}s`,
390410
)

‎packages/opencode/test/altimate/workspace/link-telemetry.test.ts‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,15 @@ describe("workspace link telemetry", () => {
223223
expect(endpointOf("POST", "/datamates", "/42/skills/pub_Ab12Cd/attach")).toBe(
224224
"POST /datamates/:id/skills/:id/attach",
225225
)
226-
// Spelling is not evidence: a public id that looks like a route word is still an id.
226+
// Position, not spelling: on an identifier-first router the first segment is an id whatever
227+
// it looks like, including a public id that equals a route word.
227228
expect(endpointOf("GET", "/skills", "/pub-many")).toBe("GET /skills/:id")
229+
expect(endpointOf("GET", "/skills", "/status")).toBe("GET /skills/:id")
230+
expect(endpointOf("PUT", "/skills", "/status/datamates")).toBe("PUT /skills/:id/datamates")
231+
expect(endpointOf("PATCH", "/datamates/memory", "/list")).toBe("PATCH /datamates/memory/:id")
232+
expect(endpointOf("POST", "/datamate-project-bindings", "/bind")).toBe("POST /datamate-project-bindings/bind")
233+
expect(endpointOf("GET", "/users", "/me")).toBe("GET /users/me")
234+
expect(endpointOf("GET", "/unknown-router", "/anything/else")).toBe("GET /:id/:id/:id")
228235
expect(endpointOf("GET", "/datamates", "/42/skills/pub_Ab12Cd/files/readme.md")).toBe(
229236
"GET /datamates/:id/skills/:id/files/:id",
230237
)
@@ -234,6 +241,37 @@ describe("workspace link telemetry", () => {
234241
expect(of("workspace_api_error")).toEqual([expect.objectContaining({ status_class: "response", status: 200 })])
235242
})
236243

244+
test("a body that stalls after the server answered keeps the answer's status class", async () => {
245+
globalThis.fetch = (async (_input: string | URL | Request, init?: RequestInit) =>
246+
new Response(
247+
new ReadableStream({
248+
start(controller) {
249+
init?.signal?.addEventListener("abort", () =>
250+
controller.error(Object.assign(new Error("aborted"), { name: "AbortError" })),
251+
)
252+
},
253+
}),
254+
{ status: 500 },
255+
)) as typeof globalThis.fetch
256+
await expect(altimateRequest("GET", "/x", { base: "/datamates", timeoutMs: 200 })).rejects.toThrow(/timed out/)
257+
expect(of("workspace_api_error")).toEqual([expect.objectContaining({ status_class: "5xx", status: 500 })])
258+
259+
events = []
260+
globalThis.fetch = (async (_input: string | URL | Request, init?: RequestInit) =>
261+
new Response(
262+
new ReadableStream({
263+
start(controller) {
264+
init?.signal?.addEventListener("abort", () =>
265+
controller.error(Object.assign(new Error("aborted"), { name: "AbortError" })),
266+
)
267+
},
268+
}),
269+
{ status: 200 },
270+
)) as typeof globalThis.fetch
271+
await expect(altimateRequest("GET", "/x", { base: "/datamates", timeoutMs: 200 })).rejects.toThrow(/timed out/)
272+
expect(of("workspace_api_error")).toEqual([expect.objectContaining({ status_class: "timeout", status: 200 })])
273+
})
274+
237275
test("a body that fails after the server answered keeps the answer's status class", async () => {
238276
// Oversized 500: readBounded() refuses the body before the status branch; the row is still a 5xx.
239277
globalThis.fetch = (async (_input: string | URL | Request, _init?: RequestInit) =>

0 commit comments

Comments
 (0)