fix(core): keep nested permission metadata JSON-encodable (#66391)

This commit is contained in:
Filip Hejmowski 2026-11-26 15:02:12 +01:00 • committed by GitHub
parent f91949daee
commit b5ae67aecb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 28 additions and 9 deletions

View file

@ -1041,7 +1041,7 @@ const layer = Layer.effect(
if (Option.isNone(at)) return yield* new InvalidError({ reason: `Invalid time: ${trigger.at}` })
if (at.value <= now) return yield* new InvalidError({ reason: `Time is in the past: ${trigger.at}` })
const stored: Trigger = RELATIVE.test(trigger.at.trim())
? { type: "at", at: new Date(at.value).toISOString(), tz: trigger.tz }
? { ...trigger, at: new Date(at.value).toISOString() }
: trigger
return { trigger: stored, due: at.value }
}

View file

@ -1,6 +1,7 @@
export * as Permission from "./permission.js"
import { makeLocationNode } from "@opencode/util/effect/app-node"
import { isRecord } from "@opencode/util/record"
import { Context, Deferred, Effect, Layer, Schema, Scope } from "effect"
import { Permission } from "@opencode/schema/permission"
import type { Tool } from "@opencode/schema/tool"
@ -246,11 +247,7 @@ const layer = Layer.effect(
action: input.action,
resources: input.resources,
save: input.save,
// Tools copy optional inputs straight into metadata; undefined values are not JSON and would
// fail the HttpApi response encoder when clients list pending requests.
metadata:
input.metadata &&
Object.fromEntries(Object.entries(input.metadata).filter(([, value]) => value !== undefined)),
metadata: input.metadata && withoutUndefined(input.metadata),
source: row(input.source),
message,
}
@ -412,6 +409,16 @@ const layer = Layer.effect(
}),
)
// Tools copy optional inputs straight into metadata, nested objects included; undefined values are not JSON
// and would fail the HttpApi response encoder when clients list pending requests.
function withoutUndefined(record: Record<string, unknown>): Record<string, unknown> {
return Object.fromEntries(
Object.entries(record)
.filter(([, value]) => value !== undefined)
.map(([key, value]) => [key, isRecord(value) ? withoutUndefined(value) : value]),
)
}
export const node = makeLocationNode({
service: Service,
layer,

View file

@ -650,9 +650,18 @@ describe("Permission", () => {
const { service, request } = yield* waitForRequest({
action: "glob",
resources: ["/project"],
metadata: { root: "/project", path: undefined, hidden: undefined, limit: undefined },
metadata: {
root: "/project",
path: undefined,
hidden: undefined,
limit: undefined,
trigger: { type: "at", at: "2026-11-25T12:00:00.000Z", tz: undefined },
},
})
expect(request.metadata).toStrictEqual({
root: "/project",
trigger: { type: "at", at: "2026-11-25T12:00:00.000Z" },
})
expect(request.metadata).toStrictEqual({ root: "/project" })
expect(yield* service.forSession(request.sessionID)).toEqual([request])
// HttpApi encodes JSON success bodies through Schema.toCodecJson, which rejects undefined values in Unknown.
const encode = Schema.encodeEffect(Schema.toCodecJson(Schema.Struct({ data: Schema.Array(Permission.Request) })))
@ -663,7 +672,10 @@ describe("Permission", () => {
sessionID: request.sessionID,
action: "glob",
resources: ["/project"],
metadata: { root: "/project" },
metadata: {
root: "/project",
trigger: { type: "at", at: "2026-11-25T12:00:00.000Z" },
},
},
],
})