Skip to content

Commit 0ba9043

Browse files
fix: attribute recovery-path unlinks, report bad 2xx bodies, mask every id
The no-local-row unlink carries the server-found workspace id; an empty, unparseable or oversized 2xx body reports status_class "response"; endpoint masking keeps only route words, so memory and skill public ids never reach telemetry; the plugin test isolates OPENCODE_DISABLE_TELEMETRY. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_011AhtJPr3nmT1Po92ZQk4VE
1 parent d94408d commit 0ba9043

4 files changed

Lines changed: 42 additions & 6 deletions

File tree

‎packages/opencode/src/altimate/telemetry/index.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,8 @@ export namespace Telemetry {
591591
timestamp: number
592592
session_id: string
593593
endpoint: string
594-
status_class: "4xx" | "5xx" | "network" | "timeout"
594+
/** `response`: the server answered 2xx with an empty, unparseable or oversized body. */
595+
status_class: "4xx" | "5xx" | "network" | "timeout" | "response"
595596
status?: number
596597
workspace_id?: number
597598
}

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,15 @@ 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-
function endpointOf(method: string, basePath: string, subpath: string): string {
182-
return `${method} ${basePath}${subpath}`.replace(/\/\d+(?=\/|$)/g, "/:id")
181+
/** "METHOD /base/path" with every dynamic segment replaced by ":id". Route words are lowercase
182+
* letters with hyphens or underscores (or a version like v3); anything else — numeric ids,
183+
* uuids, memory and skill public ids — is an identifier and must not reach telemetry. */
184+
export function endpointOf(method: string, basePath: string, subpath: string): string {
185+
const masked = `${basePath}${subpath}`
186+
.split("/")
187+
.map((seg) => (seg === "" || /^(v\d+|[a-z][a-z_-]*)$/.test(seg) ? seg : ":id"))
188+
.join("/")
189+
return `${method} ${masked}`
183190
}
184191

185192
function workspaceIdIn(body: unknown): number | undefined {
@@ -192,7 +199,7 @@ function workspaceIdIn(body: unknown): number | undefined {
192199
function reportApiError(input: {
193200
endpoint: string
194201
status?: number
195-
kind: "4xx" | "5xx" | "network" | "timeout"
202+
kind: "4xx" | "5xx" | "network" | "timeout" | "response"
196203
workspaceId?: number
197204
}) {
198205
const session_id = Telemetry.getContext().sessionId
@@ -372,7 +379,13 @@ async function req<T>(
372379
`Request to ${target} timed out after ${Math.round(timeoutMs / 1000)}s`,
373380
)
374381
}
375-
reportApiError({ endpoint, kind: "network", workspaceId: bodyWorkspaceId })
382+
// A WorkspaceApiError here came from readBounded(): the server answered, the body was
383+
// over the cap. Everything else is transport.
384+
reportApiError({
385+
endpoint,
386+
kind: err instanceof WorkspaceApiError ? "response" : "network",
387+
workspaceId: bodyWorkspaceId,
388+
})
376389
const msg = err instanceof Error ? err.message : String(err)
377390
throw new WorkspaceApiError(`Cannot reach ${target}: ${msg}`)
378391
} finally {
@@ -427,6 +440,7 @@ async function req<T>(
427440
// is treated as an empty body too — otherwise ``null as T`` reaches callers
428441
// and .foo throws in a way the typed switches can't classify. (m7 + CR)
429442
if (json == null && !opts.allowEmptyBody) {
443+
reportApiError({ endpoint, status: res.status, kind: "response", workspaceId: bodyWorkspaceId })
430444
throw new WorkspaceApiError(
431445
`Empty ${res.status} body from ${target} — expected JSON payload`,
432446
res.status,

‎packages/opencode/test/altimate/altimate-plugin.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ describe("buildCliContext", () => {
141141
const idPath = path.join(dir.path, "machine-id")
142142
fs.writeFileSync(idPath, VALID_UUID, "utf8")
143143
const savedCs = process.env.APPLICATIONINSIGHTS_CONNECTION_STRING
144+
const savedOpencodeOptOut = process.env.OPENCODE_DISABLE_TELEMETRY
145+
delete process.env.OPENCODE_DISABLE_TELEMETRY
144146
process.env.APPLICATIONINSIGHTS_CONNECTION_STRING =
145147
"InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://ingest.example.invalid/"
146148
const home = spyOn(os, "homedir").mockImplementation(() => dir.path)
@@ -159,6 +161,8 @@ describe("buildCliContext", () => {
159161
home.mockRestore()
160162
if (savedCs === undefined) delete process.env.APPLICATIONINSIGHTS_CONNECTION_STRING
161163
else process.env.APPLICATIONINSIGHTS_CONNECTION_STRING = savedCs
164+
if (savedOpencodeOptOut === undefined) delete process.env.OPENCODE_DISABLE_TELEMETRY
165+
else process.env.OPENCODE_DISABLE_TELEMETRY = savedOpencodeOptOut
162166
}
163167
})
164168

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ writeFileSync(
1515
JSON.stringify({ altimateUrl: API_URL, altimateInstanceName: "acme", altimateApiKey: "test-key" }),
1616
)
1717

18-
const { WorkspaceApi, linkFailureReason, WorkspaceApiError, NotFoundError, ForbiddenError, ConflictError } =
18+
const { WorkspaceApi, linkFailureReason, endpointOf, WorkspaceApiError, NotFoundError, ForbiddenError, ConflictError } =
1919
await import("../../../src/altimate/workspace/api-client")
2020
const { Telemetry } = await import("../../../src/altimate/telemetry")
2121

@@ -205,6 +205,23 @@ describe("workspace link telemetry", () => {
205205
expect(of("workspace_api_error")[0]).toEqual(expect.objectContaining({ status_class: "timeout" }))
206206
}, 40_000)
207207

208+
test("endpoints mask every dynamic segment, and a bad 2xx body is its own class", async () => {
209+
expect(endpointOf("GET", "/datamate-project-bindings", "/by-remote")).toBe(
210+
"GET /datamate-project-bindings/by-remote",
211+
)
212+
expect(endpointOf("GET", "/datamates", "/123/memory/8f2c0a1e-9b7d-4e11-a0c2-5d6e7f8a9b0c")).toBe(
213+
"GET /datamates/:id/memory/:id",
214+
)
215+
expect(endpointOf("POST", "/datamates", "/42/skills/pub_Ab12Cd/attach")).toBe(
216+
"POST /datamates/:id/skills/:id/attach",
217+
)
218+
expect(endpointOf("GET", "/dbt", "/v3/validate-credentials")).toBe("GET /dbt/v3/validate-credentials")
219+
220+
replies = [{ status: 200 }] // 2xx with an empty body where JSON was required
221+
await expect(WorkspaceApi.listDatamates()).rejects.toThrow(/Empty 200 body/)
222+
expect(of("workspace_api_error")).toEqual([expect.objectContaining({ status_class: "response", status: 200 })])
223+
})
224+
208225
test("linkFailureReason classifies every error the client throws", () => {
209226
expect(linkFailureReason(new ForbiddenError("f"))).toBe("auth")
210227
expect(linkFailureReason(new WorkspaceApiError("unauthorized", 401))).toBe("auth")

0 commit comments

Comments
 (0)