mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 10:18:46 +03:00
chore: upgrade effect to 4.1.0 and fix a SQLite lock-up on interrupt (#60651)
This commit is contained in:
committed by
GitHub
Unverified
parent
12f7e1b59a
commit
c6056f3931
@@ -1043,10 +1043,10 @@
|
||||
"@astrojs/starlight": "0.41.11",
|
||||
"@cloudflare/workers-types": "4.20251008.0",
|
||||
"@corvu/drawer": "0.2.4",
|
||||
"@effect/opentelemetry": "4.0.6",
|
||||
"@effect/platform-node": "4.0.6",
|
||||
"@effect/platform-node-shared": "4.0.6",
|
||||
"@effect/sql-sqlite-bun": "4.0.6",
|
||||
"@effect/opentelemetry": "4.1.0",
|
||||
"@effect/platform-node": "4.1.0",
|
||||
"@effect/platform-node-shared": "4.1.0",
|
||||
"@effect/sql-sqlite-bun": "4.1.0",
|
||||
"@kobalte/core": "0.13.13",
|
||||
"@lydell/node-pty": "1.2.0-beta.12",
|
||||
"@npmcli/arborist": "9.4.0",
|
||||
@@ -1083,7 +1083,7 @@
|
||||
"dompurify": "3.4.17",
|
||||
"drizzle-kit": "1.0.0-rc.5-ab785fc",
|
||||
"drizzle-orm": "1.0.0-rc.5-169397b",
|
||||
"effect": "4.0.6",
|
||||
"effect": "4.1.0",
|
||||
"fast-check": "4.9.0",
|
||||
"fuzzysort": "3.1.0",
|
||||
"get-east-asian-width": "1.6.0",
|
||||
@@ -1530,13 +1530,13 @@
|
||||
|
||||
"@drizzle-team/brocli": ["@drizzle-team/brocli@0.12.0", "", {}, "sha512-mlUE+rZ8CatQekLhnaiN91Iemdd+e2gFKooGlnRB3oPTL3VghLfX24dx7HrzMNeC1JrIB/0kpsfyty3f5HNfxQ=="],
|
||||
|
||||
"@effect/opentelemetry": ["@effect/opentelemetry@4.0.6", "", { "peerDependencies": { "@opentelemetry/api": ">=1.9.0 <2.0.0", "@opentelemetry/api-logs": ">=0.203.0 <0.300.0", "@opentelemetry/resources": ">=2.0.0 <3.0.0", "@opentelemetry/sdk-logs": ">=0.203.0 <0.300.0", "@opentelemetry/sdk-metrics": ">=2.0.0 <3.0.0", "@opentelemetry/sdk-trace-base": ">=2.0.0 <3.0.0", "@opentelemetry/sdk-trace-node": ">=2.0.0 <3.0.0", "@opentelemetry/sdk-trace-web": ">=2.0.0 <3.0.0", "@opentelemetry/semantic-conventions": ">=1.33.0 <2.0.0", "effect": "^4.0.6" }, "optionalPeers": ["@opentelemetry/api", "@opentelemetry/api-logs", "@opentelemetry/resources", "@opentelemetry/sdk-logs", "@opentelemetry/sdk-metrics", "@opentelemetry/sdk-trace-base", "@opentelemetry/sdk-trace-node", "@opentelemetry/sdk-trace-web"] }, "sha512-xhLR1EN4ZNaKM7Bx59SCFiRNduFzgH8LZXDrCpPN8MpQ93V0GQcFHTBah5T3EXO2v+5HTEacdTgetIA0Creqvw=="],
|
||||
"@effect/opentelemetry": ["@effect/opentelemetry@4.1.0", "", { "peerDependencies": { "@opentelemetry/api": ">=1.9.0 <2.0.0", "@opentelemetry/api-logs": ">=0.203.0 <0.300.0", "@opentelemetry/resources": ">=2.0.0 <3.0.0", "@opentelemetry/sdk-logs": ">=0.203.0 <0.300.0", "@opentelemetry/sdk-metrics": ">=2.0.0 <3.0.0", "@opentelemetry/sdk-trace-base": ">=2.0.0 <3.0.0", "@opentelemetry/sdk-trace-node": ">=2.0.0 <3.0.0", "@opentelemetry/sdk-trace-web": ">=2.0.0 <3.0.0", "@opentelemetry/semantic-conventions": ">=1.33.0 <2.0.0", "effect": "^4.1.0" }, "optionalPeers": ["@opentelemetry/api", "@opentelemetry/api-logs", "@opentelemetry/resources", "@opentelemetry/sdk-logs", "@opentelemetry/sdk-metrics", "@opentelemetry/sdk-trace-base", "@opentelemetry/sdk-trace-node", "@opentelemetry/sdk-trace-web"] }, "sha512-j9bNzQaoQvcnimBFMG0i3piEW20E8HjkXAQHTJN/XqG2e4uJ9zCv53/HCNzD7+ooFn5S4nwQy/BOXeNReMIe+A=="],
|
||||
|
||||
"@effect/platform-node": ["@effect/platform-node@4.0.6", "", { "dependencies": { "@effect/platform-node-shared": "^4.0.6", "undici": "^8.12.2" }, "peerDependencies": { "effect": "^4.0.6", "redis": ">=5.0.0 <7.0.0" } }, "sha512-gXvGHAy0/8PZbWvMMguloDUyeSP+rzYEo3FSEqUuGduxo9ORcGioDxPI+GOLCIFfam7VuNkmSUxBOiRMIrMrBw=="],
|
||||
"@effect/platform-node": ["@effect/platform-node@4.1.0", "", { "dependencies": { "@effect/platform-node-shared": "^4.1.0", "undici": "^8.12.2" }, "peerDependencies": { "effect": "^4.1.0", "redis": ">=5.0.0 <7.0.0" } }, "sha512-v+X7moGVF9k5oMVvz0slA+NgtEod+W+oygeI285MDJgSlUCW3JcqElj+cD3rlijFZtpKhC7RLkU1GuCxjGHTpA=="],
|
||||
|
||||
"@effect/platform-node-shared": ["@effect/platform-node-shared@4.0.6", "", { "dependencies": { "@types/ws": "^8.18.2", "ws": "^8.22.0" }, "peerDependencies": { "effect": "^4.0.6" } }, "sha512-0PhyC4uG30xGtsajnxjkcBWfTjNRzUh4ZKl1Kvo/MNyCTb+W2O+fBzf7wCRCoqt+nLudiq4V7vX40Q8XIBuMaQ=="],
|
||||
"@effect/platform-node-shared": ["@effect/platform-node-shared@4.1.0", "", { "dependencies": { "@types/ws": "^8.18.2", "ws": "^8.22.0" }, "peerDependencies": { "effect": "^4.1.0" } }, "sha512-uYQfFRoxWLo8fIhiSeBcmz/R1ooOfE3HF3cWHFgxz82tZMk2Wn02BGABcdLmaAch9VpDLbtWnuhSxNyBsGMUFg=="],
|
||||
|
||||
"@effect/sql-sqlite-bun": ["@effect/sql-sqlite-bun@4.0.6", "", { "peerDependencies": { "effect": "^4.0.6" } }, "sha512-IUf0d6NrrG+m/yKqziGyE9xQO5cOPzyDcb98S39OpdQMKHBZjVtx+TN+nz3b8Spc6AvfKjFYTcvXSjSzgjDRug=="],
|
||||
"@effect/sql-sqlite-bun": ["@effect/sql-sqlite-bun@4.1.0", "", { "peerDependencies": { "effect": "^4.1.0" } }, "sha512-V05HFjGmSnzIvXuxiHABw67SNf70dNbIvhUfyM7woDrgOu0JlqA8BTIhrwEPy0+GPwyvS5ALolcyNOMRgPVxyA=="],
|
||||
|
||||
"@electron-internal/extract-zip": ["@electron-internal/extract-zip@1.0.5", "", {}, "sha512-+bqFCP98pLI0Tt0XQo1TmlXtwjWchISndDOxCkEcIuUgXWpBnLyRI+2DU+mesvnMMX6L1XDqYNA0lXNDHd/yiA=="],
|
||||
|
||||
@@ -3090,7 +3090,7 @@
|
||||
|
||||
"ee-first": ["ee-first@1.1.1", "", {}, "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow=="],
|
||||
|
||||
"effect": ["effect@4.0.6", "", {}, "sha512-Tc3mkzuLR6t6P9dhLt0D2GrxYtmU5td4XEJEiXCmILz53IRIfJyHsKZWjTA3xEjiwpNTtXBgI4iqYPxMztBDrw=="],
|
||||
"effect": ["effect@4.1.0", "", {}, "sha512-lGx2MJJyrXA8RZb2UpQ/0Nkb5Utaimby7oD92+18noBxLPE85IFOvptdzau8dvcxAOwfwHo9iqapbKeFzqQM/Q=="],
|
||||
|
||||
"ejs": ["ejs@3.1.10", "", { "dependencies": { "jake": "^10.8.5" }, "bin": { "ejs": "bin/cli.js" } }, "sha512-UeJmFfOrAQS8OJWPZ4qtgHyWExa088/MtK5UEyoJGFH67cDEXkZSviOiKRCZ4Xij0zxI3JECgYs3oKx+AizQBA=="],
|
||||
|
||||
|
||||
+5
-5
@@ -45,10 +45,10 @@
|
||||
"packages/stats/*"
|
||||
],
|
||||
"catalog": {
|
||||
"@effect/opentelemetry": "4.0.6",
|
||||
"@effect/platform-node": "4.0.6",
|
||||
"@effect/platform-node-shared": "4.0.6",
|
||||
"@effect/sql-sqlite-bun": "4.0.6",
|
||||
"@effect/opentelemetry": "4.1.0",
|
||||
"@effect/platform-node": "4.1.0",
|
||||
"@effect/platform-node-shared": "4.1.0",
|
||||
"@effect/sql-sqlite-bun": "4.1.0",
|
||||
"@npmcli/arborist": "9.4.0",
|
||||
"@types/bun": "1.4.3",
|
||||
"@types/cross-spawn": "6.0.6",
|
||||
@@ -79,7 +79,7 @@
|
||||
"dompurify": "3.4.17",
|
||||
"drizzle-kit": "1.0.0-rc.5-ab785fc",
|
||||
"drizzle-orm": "1.0.0-rc.5-169397b",
|
||||
"effect": "4.0.6",
|
||||
"effect": "4.1.0",
|
||||
"fast-check": "4.9.0",
|
||||
"cross-spawn": "7.0.6",
|
||||
"hono": "4.13.13",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { HttpRecorder } from "@opencode/http-recorder"
|
||||
import { NodeSocket } from "@effect/platform-node"
|
||||
import { NodeWS } from "@effect/platform-node"
|
||||
import { Effect, Layer } from "effect"
|
||||
import { Socket } from "effect/socket"
|
||||
import * as path from "node:path"
|
||||
@@ -100,7 +100,7 @@ export const recordedTests = (options: RecordedTestsOptions) =>
|
||||
...recorderOptions,
|
||||
directory: FIXTURES_DIR,
|
||||
metadata: recorderMetadata,
|
||||
}).pipe(Layer.provide(NodeSocket.layerWebSocketConstructorWS))
|
||||
}).pipe(Layer.provide(NodeWS.layerWebSocketConstructor))
|
||||
const layer = Layer.mergeAll(
|
||||
requestExecutor,
|
||||
LLMClient.layer.pipe(Layer.provide(requestExecutor)),
|
||||
|
||||
@@ -392,7 +392,7 @@ export const LineComment = Persistence.struct({
|
||||
export type LineComment = typeof LineComment.Type
|
||||
|
||||
export const CommentStore = Persistence.struct({
|
||||
comments: Schema.Record(Schema.String, Schema.mutableKey(Persistence.array(LineComment))),
|
||||
comments: Schema.Record(Schema.String, Persistence.entry(Persistence.array(LineComment))),
|
||||
})
|
||||
|
||||
export type CommentStore = typeof CommentStore.Type
|
||||
|
||||
@@ -41,7 +41,7 @@ type State = typeof StateSchema.Type
|
||||
|
||||
const SessionsSchema = Schema.Record(
|
||||
Schema.String,
|
||||
Schema.mutableKey(Persistence.fallback(Schema.UndefinedOr(StateSchema), () => undefined)),
|
||||
Persistence.entry(Persistence.fallback(Schema.UndefinedOr(StateSchema), () => undefined)),
|
||||
)
|
||||
|
||||
const Current = Persistence.struct({ session: SessionsSchema })
|
||||
|
||||
@@ -43,8 +43,10 @@ migration rules remain explicit in their schemas.
|
||||
each field's optionality and codec. It does not add defaults or error recovery.
|
||||
- `Persistence.record(valueSchema)` creates a mutable string-keyed record, defaulting
|
||||
missing or invalid records to a fresh `{}`. Its value schema determines entry
|
||||
recovery: pass `Persistence.optional(valueSchema)` to discard only invalid entries,
|
||||
or `Persistence.fallback(valueSchema, factory)` to replace those entries.
|
||||
recovery: pass `valueSchema.pipe(Schema.catchDecoding(() => Effect.succeed(Option.none())))`
|
||||
to discard only invalid entries, or `Persistence.fallback(valueSchema, factory)` to
|
||||
replace those entries. Don't pass `Persistence.optional(valueSchema)`: Effect rejects
|
||||
optional record values, and the record throws when it is built.
|
||||
- `Persistence.array(schema)` defaults to an empty mutable array and discards
|
||||
invalid entries individually. Valid entries still pass through their codecs.
|
||||
- Recovery is not a substitute for an explicit historical shape transformation.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect, Predicate, Schema, SchemaGetter } from "effect"
|
||||
import { Effect, Option, Predicate, Schema, SchemaGetter } from "effect"
|
||||
import { Persistence } from "./schema"
|
||||
|
||||
describe("persistence schemas", () => {
|
||||
@@ -87,7 +87,9 @@ describe("persistence schemas", () => {
|
||||
})
|
||||
|
||||
const decode = Schema.decodeUnknownSync(schema)
|
||||
expect(decode({ general: { expanded: true, theme: "dark", nested: { keep: true } }, other: { flag: true } })).toEqual({
|
||||
expect(
|
||||
decode({ general: { expanded: true, theme: "dark", nested: { keep: true } }, other: { flag: true } }),
|
||||
).toEqual({
|
||||
general: { mode: "full", theme: "dark", nested: { keep: true } },
|
||||
other: { flag: true },
|
||||
})
|
||||
@@ -169,7 +171,7 @@ describe("persistence schemas", () => {
|
||||
first.changed = true
|
||||
expect(decode(undefined)).toEqual({})
|
||||
|
||||
const recover = Persistence.record(Persistence.optional(Schema.Boolean))
|
||||
const recover = Persistence.record(Schema.Boolean.pipe(Schema.catchDecoding(() => Effect.succeed(Option.none()))))
|
||||
const state = Schema.decodeUnknownSync(recover)({ valid: true, invalid: "true" })
|
||||
expect(state).toEqual({ valid: true })
|
||||
expect(Schema.encodeSync(recover)(state)).toEqual({ valid: true })
|
||||
|
||||
@@ -97,8 +97,18 @@ export function struct<const Fields extends Schema.Struct.Fields>(fields: Fields
|
||||
return Schema.Struct(fields).mapFields(Struct.map(Schema.mutableKey))
|
||||
}
|
||||
|
||||
// Record values can't have an optional encoded side (as `fallback` does), so each entry decodes from
|
||||
// `Unknown` and keeps its own recovery.
|
||||
export function entry<S extends Schema.ConstraintCodec<unknown, unknown>>(schema: S) {
|
||||
return Schema.mutableKey(
|
||||
Schema.Unknown.pipe(
|
||||
Schema.decodeTo(schema, { decode: SchemaGetter.passthrough(), encode: SchemaGetter.passthrough() }),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
export function record<S extends Schema.ConstraintCodec<unknown, unknown>>(schema: S) {
|
||||
const entries = Schema.Record(Schema.String, Schema.mutableKey(schema))
|
||||
const entries = Schema.Record(Schema.String, entry(schema))
|
||||
|
||||
return fallback(entries, () => Schema.decodeUnknownSync(entries)({}))
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import { Global } from "@opencode/util/global"
|
||||
import { AppProcess } from "@opencode/util/process"
|
||||
import { expect, spyOn, test } from "bun:test"
|
||||
import { Effect, FileSystem, Layer, PlatformError, Stream } from "effect"
|
||||
import { ChildProcess, ChildProcessSpawner } from "effect/process"
|
||||
import { ChildProcessSpawner } from "effect/process"
|
||||
import { existsSync, readdirSync, readFileSync } from "node:fs"
|
||||
import path from "node:path"
|
||||
import { Updater } from "../src/services/updater"
|
||||
@@ -16,7 +16,10 @@ const it = testEffect(NodeServices.layer)
|
||||
declare const OPENCODE_CLI_NAME: string | undefined
|
||||
|
||||
function fixture(
|
||||
respond: (command: ChildProcess.StandardCommand) => Partial<AppProcess.RunResult> & {
|
||||
respond: (command: {
|
||||
readonly command: string
|
||||
readonly args: ReadonlyArray<string>
|
||||
}) => Partial<AppProcess.RunResult> & {
|
||||
error?: AppProcess.AppProcessError
|
||||
} = () => ({}),
|
||||
name = "@opencode/cli",
|
||||
@@ -110,11 +113,12 @@ function fixture(
|
||||
run: (command) =>
|
||||
Effect.suspend(() => {
|
||||
if (command._tag !== "StandardCommand") return Effect.die("Unexpected piped install command")
|
||||
const lookup = command.command === "git" && command.args[0] === "--exec-path"
|
||||
if (!lookup) commands.push([command.command, ...command.args])
|
||||
const args = command.args.map(String)
|
||||
const lookup = command.command === "git" && args[0] === "--exec-path"
|
||||
if (!lookup) commands.push([command.command, ...args])
|
||||
const result = lookup
|
||||
? { stdout: Buffer.from(`${path.join(git, "mingw64", "libexec", "git-core")}\n`) }
|
||||
: respond(command)
|
||||
: respond({ command: command.command, args })
|
||||
if (result.error) return Effect.fail(result.error)
|
||||
return Effect.succeed({
|
||||
command: command.command,
|
||||
|
||||
@@ -18,27 +18,27 @@ export const scan = Effect.fn("SkillScan.scan")(function* (root: string) {
|
||||
entries.filter((entry) => !entry.name.startsWith(".") && !FSUtil.ignoredDirectories.includes(entry.name)),
|
||||
(entry) => {
|
||||
const target = path.join(item.directory, entry.name)
|
||||
if (entry.type !== "symlink") return Effect.succeed({ name: entry.name, path: target, type: entry.type })
|
||||
if (entry.type !== "SymbolicLink") return Effect.succeed({ name: entry.name, path: target, type: entry.type })
|
||||
return fs.stat(target).pipe(
|
||||
Effect.map((info) => (info.type === "Directory" ? "directory" : info.type === "File" ? "file" : "other")),
|
||||
Effect.orElseSucceed(() => "other" as const),
|
||||
Effect.map((info) => info.type),
|
||||
Effect.orElseSucceed(() => "Unknown" as const),
|
||||
Effect.map((type) => ({ name: entry.name, path: target, type })),
|
||||
)
|
||||
},
|
||||
)
|
||||
const skill = children.find((child) => child.type === "file" && child.name === "SKILL.md")
|
||||
const skill = children.find((child) => child.type === "File" && child.name === "SKILL.md")
|
||||
if (skill) {
|
||||
files.push(skill.path)
|
||||
continue
|
||||
}
|
||||
if (item.depth === 0)
|
||||
files.push(
|
||||
...children.filter((child) => child.type === "file" && child.name.endsWith(".md")).map((child) => child.path),
|
||||
...children.filter((child) => child.type === "File" && child.name.endsWith(".md")).map((child) => child.path),
|
||||
)
|
||||
if (item.depth < MAX_DEPTH)
|
||||
queue.push(
|
||||
...children
|
||||
.filter((child) => child.type === "directory")
|
||||
.filter((child) => child.type === "Directory")
|
||||
.map((child) => ({ directory: child.path, depth: item.depth + 1 })),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
export * as Sqlite from "./sqlite.js"
|
||||
|
||||
import { Context, Effect, Fiber, Scope, Semaphore, Stream } from "effect"
|
||||
import { Context, Effect, type Scope, Semaphore, Stream } from "effect"
|
||||
import { identity } from "effect/Function"
|
||||
import { SqlClient, Statement } from "effect/sql"
|
||||
import type { Connection } from "effect/sql/SqlConnection"
|
||||
@@ -61,14 +61,9 @@ export const makeClient = <
|
||||
Effect.gen(function* () {
|
||||
const semaphore = yield* Semaphore.make(1)
|
||||
const acquirer = semaphore.withPermits(1)(Effect.succeed(connection))
|
||||
const transactionAcquirer = Effect.uninterruptibleMask((restore) => {
|
||||
const fiber = Fiber.getCurrent()!
|
||||
const scope = Context.getUnsafe(fiber.context, Scope.Scope)
|
||||
return Effect.as(
|
||||
Effect.tap(restore(semaphore.take(1)), () => Scope.addFinalizer(scope, semaphore.release(1))),
|
||||
connection,
|
||||
)
|
||||
})
|
||||
const transactionAcquirer = Effect.uninterruptibleMask((restore) =>
|
||||
Effect.as(restore(semaphore.takeScoped(1)), connection),
|
||||
)
|
||||
const transformRows = options.transformResultNames
|
||||
? Statement.defaultTransforms(options.transformResultNames).array
|
||||
: undefined
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Context, Effect, Exit, Fiber, Layer, Scope, Semaphore } from "effect"
|
||||
import { Context, Effect, Exit, Layer, Semaphore } from "effect"
|
||||
import { Reactivity } from "effect/reactivity"
|
||||
import { SqlClient, Statement } from "effect/sql"
|
||||
import type { Connection } from "effect/sql/SqlConnection"
|
||||
@@ -170,14 +170,9 @@ const make = (options: Config) =>
|
||||
|
||||
const semaphore = yield* Semaphore.make(1)
|
||||
const acquirer = semaphore.withPermits(1)(Effect.succeed(connection))
|
||||
const transactionAcquirer = Effect.uninterruptibleMask((restore) => {
|
||||
const fiber = Fiber.getCurrent()!
|
||||
const scope = Context.getUnsafe(fiber.context, Scope.Scope)
|
||||
return Effect.as(
|
||||
Effect.tap(restore(semaphore.take(1)), () => Scope.addFinalizer(scope, semaphore.release(1))),
|
||||
connection,
|
||||
)
|
||||
})
|
||||
const transactionAcquirer = Effect.uninterruptibleMask((restore) =>
|
||||
Effect.as(restore(semaphore.takeScoped(1)), connection),
|
||||
)
|
||||
|
||||
const client = Object.assign(
|
||||
(yield* SqlClient.make({
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// The platform barrel also exposes Redis and its optional native hash loader, which workerd cannot resolve.
|
||||
import { NodeWS } from "@effect/platform-node/NodeSocket"
|
||||
import { WebSocket as NodeWebSocket } from "@effect/platform-node/NodeWS"
|
||||
import { HttpProxyAgent } from "http-proxy-agent"
|
||||
import { HttpsProxyAgent } from "https-proxy-agent"
|
||||
import { Layer } from "effect"
|
||||
@@ -75,7 +75,7 @@ const layer = Layer.succeed(Socket.WebSocketConstructor, (url, input) => {
|
||||
// Reject redirects before headers can cross an origin boundary; the caller safely falls back to HTTP.
|
||||
followRedirects: false,
|
||||
}
|
||||
return config.protocols ? new NodeWS.WebSocket(url, config.protocols, native) : new NodeWS.WebSocket(url, native)
|
||||
return config.protocols ? new NodeWebSocket(url, config.protocols, native) : new NodeWebSocket(url, native)
|
||||
})
|
||||
|
||||
export const WebSocketConstructor = { layer, proxy } as const
|
||||
|
||||
@@ -8,11 +8,8 @@ import { Failed, NotFound, WrongKind, type FileInfo, type FilesImpl, type FileTy
|
||||
/**
|
||||
* The host filesystem binding. Deliberately raw node:fs rather than effect's
|
||||
* FileSystem service or FSUtil: the contract needs lstat semantics (stat
|
||||
* reports "symlink") and typed directory entries, and effect's node
|
||||
* FileSystem provides neither — its stat always follows symlinks and
|
||||
* readDirectory returns names only. FSUtil hits the same gap and its
|
||||
* readDirectoryEntries already bypasses to raw node readdir internally.
|
||||
* Nothing above the environment seam touches node:fs.
|
||||
* reports "symlink"), and effect's node FileSystem stat always follows
|
||||
* symlinks. Nothing above the environment seam touches node:fs.
|
||||
*/
|
||||
export const makeLocalDriver = (spawner: ChildProcessSpawner["Service"]): Driver => {
|
||||
const overrides: FilesImpl = {
|
||||
|
||||
@@ -183,13 +183,14 @@ const baseLayer = Layer.effect(
|
||||
Effect.map((items) =>
|
||||
items
|
||||
.flatMap((item) => {
|
||||
if (item.type !== "file" && item.type !== "directory") return []
|
||||
if (item.type !== "File" && item.type !== "Directory") return []
|
||||
const type = item.type === "Directory" ? "directory" : "file"
|
||||
const absolute = path.join(directory, item.name)
|
||||
const relative = path.relative(location.directory, absolute) || "."
|
||||
return [
|
||||
Entry.make({
|
||||
path: RelativePath.make(relative + (item.type === "directory" ? path.sep : "")),
|
||||
type: item.type,
|
||||
path: RelativePath.make(relative + (type === "directory" ? path.sep : "")),
|
||||
type,
|
||||
}),
|
||||
]
|
||||
})
|
||||
|
||||
@@ -21,9 +21,9 @@ export const discover = Effect.fn("PluginSourceDirectory.discover")(function* (
|
||||
const targets = yield* Effect.forEach(children, (entry) =>
|
||||
Effect.gen(function* () {
|
||||
const source = entry.target.endsWith(".ts") || entry.target.endsWith(".js")
|
||||
if (entry.type === "file" && source) return Option.some(entry.target)
|
||||
if (entry.type === "directory") return Option.some(entry.target)
|
||||
if (entry.type !== "symlink") return Option.none<string>()
|
||||
if (entry.type === "File" && source) return Option.some(entry.target)
|
||||
if (entry.type === "Directory") return Option.some(entry.target)
|
||||
if (entry.type !== "SymbolicLink") return Option.none<string>()
|
||||
if (source && (yield* fs.isFile(entry.target))) return Option.some(entry.target)
|
||||
if (yield* fs.isDir(entry.target)) return Option.some(entry.target)
|
||||
return Option.none<string>()
|
||||
|
||||
@@ -58,7 +58,7 @@ export const discover = Effect.fn("ProjectIcon.discover")(function* (fs: FSUtil.
|
||||
const root = yield* fs.realPath(directory)
|
||||
const children = yield* fs.readDirectoryEntries(root).pipe(Effect.orElseSucceed(() => []))
|
||||
const catalogs = children
|
||||
.filter((e) => e.type === "directory" && !e.name.startsWith("."))
|
||||
.filter((e) => e.type === "Directory" && !e.name.startsWith("."))
|
||||
.map((e) => e.name)
|
||||
.toSorted()
|
||||
.slice(0, 128)
|
||||
@@ -102,7 +102,7 @@ export const discover = Effect.fn("ProjectIcon.discover")(function* (fs: FSUtil.
|
||||
windows ? Math.abs(Number(file.match(/^icon-(\d+)\.png$/i)?.[1]) - 128) : /128|256/.test(file) ? 0 : 1
|
||||
const files = entries
|
||||
?.flatMap((e) =>
|
||||
e.type === "file" &&
|
||||
e.type === "File" &&
|
||||
(windows ? /^icon-\d+\.png$/i.test(e.name) : IMAGES.has(path.extname(e.name).toLowerCase()))
|
||||
? [e.name]
|
||||
: [],
|
||||
|
||||
@@ -196,9 +196,9 @@ const readFileAttachment = Effect.fn("SessionPrompt.readFileAttachment")(functio
|
||||
return {
|
||||
bytes: Buffer.from(
|
||||
entries
|
||||
.filter((entry) => entry.type === "file" || entry.type === "directory")
|
||||
.sort((a, b) => (a.type === b.type ? a.name.localeCompare(b.name) : a.type === "directory" ? -1 : 1))
|
||||
.map((entry) => entry.name + (entry.type === "directory" ? path.sep : ""))
|
||||
.filter((entry) => entry.type === "File" || entry.type === "Directory")
|
||||
.sort((a, b) => (a.type === b.type ? a.name.localeCompare(b.name) : a.type === "Directory" ? -1 : 1))
|
||||
.map((entry) => entry.name + (entry.type === "Directory" ? path.sep : ""))
|
||||
.join("\n"),
|
||||
),
|
||||
source: { type: "uri" as const, uri },
|
||||
|
||||
@@ -144,7 +144,12 @@ export const make = Effect.gen(function* () {
|
||||
const streamFailure = Option.getOrUndefined(Exit.findErrorOption(stream))
|
||||
const streamInterrupted = Exit.hasInterrupts(stream)
|
||||
// Cancelled before the start snapshot existed: record nothing, as when the capture preceded the request.
|
||||
if (streamInterrupted && !publisher.hasStarted() && !pendingStartSnapshot.pollUnsafe())
|
||||
if (
|
||||
Exit.isFailure(stream) &&
|
||||
streamInterrupted &&
|
||||
!publisher.hasStarted() &&
|
||||
!pendingStartSnapshot.pollUnsafe()
|
||||
)
|
||||
return yield* Effect.failCause(stream.cause)
|
||||
if (!overflowFailure && publisher.hasStarted()) yield* publisher.streamed()
|
||||
if (streamInterrupted) yield* interruptTools
|
||||
|
||||
@@ -93,7 +93,7 @@ export const cleanup = Effect.fn("Shell.cleanup")(function* () {
|
||||
const global = yield* Global.Service
|
||||
const directory = path.join(global.data, DIRECTORY)
|
||||
const projects = yield* fs.readDirectoryEntries(directory).pipe(
|
||||
Effect.map((entries) => entries.filter((entry) => entry.type === "directory")),
|
||||
Effect.map((entries) => entries.filter((entry) => entry.type === "Directory")),
|
||||
Effect.orElseSucceed(() => []),
|
||||
)
|
||||
const files = yield* Effect.forEach(
|
||||
@@ -102,7 +102,7 @@ export const cleanup = Effect.fn("Shell.cleanup")(function* () {
|
||||
fs.readDirectoryEntries(path.join(directory, project.name)).pipe(
|
||||
Effect.map((entries) =>
|
||||
entries.flatMap((entry) =>
|
||||
entry.type === "file" && /^sh_[0-9a-f]{12}.*\.out$/.test(entry.name)
|
||||
entry.type === "File" && /^sh_[0-9a-f]{12}.*\.out$/.test(entry.name)
|
||||
? [path.join(directory, project.name, entry.name)]
|
||||
: [],
|
||||
),
|
||||
|
||||
@@ -2,7 +2,8 @@ import { describe, expect } from "bun:test"
|
||||
import fs from "node:fs/promises"
|
||||
import os from "node:os"
|
||||
import path from "node:path"
|
||||
import { Deferred, Effect, Exit, PlatformError, Stream } from "effect"
|
||||
import { inspect } from "node:util"
|
||||
import { Cause, Deferred, Effect, Exit, PlatformError, Redacted, Stream } from "effect"
|
||||
import { ChildProcess, ChildProcessSpawner } from "effect/process"
|
||||
import { CrossSpawnSpawner } from "@opencode/util/cross-spawn-spawner"
|
||||
import { LayerNode } from "@opencode/util/effect/layer-node"
|
||||
@@ -404,6 +405,58 @@ describe("cross-spawn spawner", () => {
|
||||
expect(Exit.isFailure(exit) ? true : exit.value !== ChildProcessSpawner.ExitCode(0)).toBe(true)
|
||||
}),
|
||||
)
|
||||
|
||||
fx.effect(
|
||||
"rejects NUL characters without exposing Redacted values",
|
||||
Effect.gen(function* () {
|
||||
const secret = Redacted.make("hunter2\0")
|
||||
const commands = [
|
||||
() => ChildProcess.make("echo", [secret]),
|
||||
() => ChildProcess.make`echo ${secret}`,
|
||||
() => ChildProcess.make("echo", [], { env: { SECRET: secret } }),
|
||||
]
|
||||
for (const command of commands) {
|
||||
const exit = yield* Effect.exit(Effect.scoped(Effect.flatMap(command(), (handle) => handle.exitCode)))
|
||||
expect(Exit.isFailure(exit)).toBe(true)
|
||||
if (Exit.isSuccess(exit)) continue
|
||||
expect(Cause.hasDies(exit.cause)).toBe(false)
|
||||
const error = Cause.squash(exit.cause)
|
||||
expect(PlatformError.isPlatformError(error) && error.reason._tag).toBe("BadArgument")
|
||||
expect(inspect(exit, { depth: Infinity })).not.toContain("hunter2")
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
fx.live(
|
||||
"reports a duplex write failure to a later reader without an uncaught error",
|
||||
Effect.gen(function* () {
|
||||
if (process.platform === "win32") return
|
||||
const uncaught: Array<unknown> = []
|
||||
const onUncaught = (error: unknown) => uncaught.push(error)
|
||||
yield* Effect.acquireRelease(
|
||||
Effect.sync(() => process.on("uncaughtException", onUncaught)),
|
||||
() => Effect.sync(() => process.off("uncaughtException", onUncaught)),
|
||||
)
|
||||
// Under `bun test` (1.4.4) the process's first child with an extra fd never emits "spawn", so spawn one without.
|
||||
yield* Effect.scoped(Effect.flatMap(ChildProcess.make("true"), (warmUp) => warmUp.exitCode))
|
||||
|
||||
// The child closes fd3 before signalling readiness on stdout.
|
||||
const handle = yield* ChildProcess.make("sh", ["-c", "exec 3>&-; echo ready; exec sleep 10"], {
|
||||
additionalFds: { fd3: { type: "duplex" } },
|
||||
})
|
||||
yield* Stream.runDrain(Stream.take(handle.stdout, 1))
|
||||
|
||||
// Write before anyone reads fd3, and start reading only after the write has failed.
|
||||
const writeError = yield* Effect.flip(
|
||||
Stream.run(Stream.make(new TextEncoder().encode("request")), handle.getInputFd(3)),
|
||||
)
|
||||
const readError = yield* Effect.flip(Stream.runDrain(handle.getOutputFd(3)).pipe(Effect.timeout("2 seconds")))
|
||||
|
||||
expect(writeError._tag).toBe("PlatformError")
|
||||
expect(uncaught).toEqual([])
|
||||
expect(readError._tag).toBe("PlatformError")
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
describe("pipeline", () => {
|
||||
|
||||
@@ -12,7 +12,7 @@ export function countingSpawner(fail?: (args: readonly string[]) => boolean) {
|
||||
Effect.map(ChildProcessSpawner.ChildProcessSpawner, (spawner) =>
|
||||
ChildProcessSpawner.make((command) => {
|
||||
state.spawns++
|
||||
if (fail && command._tag === "StandardCommand" && fail(command.args))
|
||||
if (fail && command._tag === "StandardCommand" && fail(command.args.map(String)))
|
||||
return Effect.fail(PlatformError.systemError({ _tag: "Busy", module: "ChildProcess", method: "spawn" }))
|
||||
return spawner.spawn(command)
|
||||
}),
|
||||
|
||||
@@ -136,11 +136,12 @@ const fakeAzureCli = Effect.fn(function* (respond: (args: readonly string[]) =>
|
||||
...processes,
|
||||
run: (command) => {
|
||||
if (command._tag !== "StandardCommand") return processes.run(command)
|
||||
commands.push([...command.args])
|
||||
const value = respond(command.args)
|
||||
const args = command.args.map(String)
|
||||
commands.push(args)
|
||||
const value = respond(args)
|
||||
if (value instanceof Error) return Effect.fail(new AppProcess.AppProcessError({ command: "az", cause: value }))
|
||||
return Effect.succeed({
|
||||
command: `az ${command.args.join(" ")}`,
|
||||
command: `az ${args.join(" ")}`,
|
||||
exitCode: 0,
|
||||
stdout: Buffer.from(JSON.stringify(value)),
|
||||
stderr: Buffer.alloc(0),
|
||||
|
||||
@@ -125,7 +125,7 @@ const testEnvironment = Layer.effect(
|
||||
yield* files.mkdir(toggleDirectory).pipe(Effect.orDie)
|
||||
const spawner = ChildProcessSpawner.make((command) =>
|
||||
Effect.gen(function* () {
|
||||
const script = command._tag === "StandardCommand" ? (command.args.at(-1) ?? command.command) : "pipeline"
|
||||
const script = command._tag === "StandardCommand" ? String(command.args.at(-1) ?? command.command) : "pipeline"
|
||||
shellState.runs.push(script)
|
||||
if (shellState.hangMs > 0) yield* Effect.sleep(shellState.hangMs)
|
||||
const outBytes = new TextEncoder().encode(shellState.stdout)
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect, Fiber, Layer, References } from "effect"
|
||||
import { SqlClient } from "effect/sql"
|
||||
import { sqliteLayer as bunSqliteLayer } from "@opencode/core/database/sqlite.bun"
|
||||
import { sqliteLayer as workerdSqliteLayer } from "@opencode/core/database/sqlite.workerd"
|
||||
import { makeDurableObjectStorage } from "./fixture/durable-object-storage"
|
||||
|
||||
// The holder keeps the connection across yields, so the queued fiber parks in
|
||||
// the semaphore's waiter queue and is woken when the holder lets go. The sweep
|
||||
// moves the interrupt across that wait and the gap between the woken fiber
|
||||
// taking the connection's only permit and registering its release. A leaked
|
||||
// permit makes every later query wait forever.
|
||||
const sweep = (
|
||||
layer: () => Layer.Layer<SqlClient.SqlClient>,
|
||||
queue: (sql: SqlClient.SqlClient) => Effect.Effect<unknown, unknown>,
|
||||
) =>
|
||||
Effect.runPromise(
|
||||
Effect.gen(function* () {
|
||||
const leaked: Array<number> = []
|
||||
for (let delay = 0; delay < 31; delay++) {
|
||||
yield* Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
const sql = yield* SqlClient.SqlClient
|
||||
const holder = yield* Effect.forkChild(
|
||||
Effect.scoped(Effect.andThen(sql.reserve, Effect.andThen(Effect.yieldNow, Effect.yieldNow))),
|
||||
)
|
||||
const queued = yield* queue(sql).pipe(
|
||||
Effect.provideService(References.MaxOpsBeforeYield, 3),
|
||||
Effect.forkChild,
|
||||
)
|
||||
for (let i = 0; i < delay; i++) yield* Effect.yieldNow
|
||||
yield* Fiber.interrupt(queued)
|
||||
yield* Fiber.join(holder)
|
||||
const next = yield* Effect.forkChild(sql`SELECT 3`, { startImmediately: true })
|
||||
if (next.pollUnsafe() === undefined) {
|
||||
leaked.push(delay)
|
||||
yield* Fiber.interrupt(next)
|
||||
}
|
||||
}).pipe(Effect.provide(layer())),
|
||||
)
|
||||
}
|
||||
return leaked
|
||||
}),
|
||||
)
|
||||
|
||||
describe("sqlite transaction interrupts", () => {
|
||||
test("bun client releases the connection when a queued transaction is interrupted", async () => {
|
||||
const leaked = await sweep(
|
||||
() => bunSqliteLayer({ filename: ":memory:", disableWAL: true }),
|
||||
(sql) => sql.withTransaction(sql`SELECT 2`),
|
||||
)
|
||||
expect(leaked).toEqual([])
|
||||
})
|
||||
|
||||
test("bun client releases the connection when a queued reserve is interrupted", async () => {
|
||||
const leaked = await sweep(
|
||||
() => bunSqliteLayer({ filename: ":memory:", disableWAL: true }),
|
||||
(sql) => Effect.scoped(Effect.andThen(sql.reserve, Effect.yieldNow)),
|
||||
)
|
||||
expect(leaked).toEqual([])
|
||||
})
|
||||
|
||||
test("workerd client releases the connection when a queued reserve is interrupted", async () => {
|
||||
const leaked = await sweep(
|
||||
() => workerdSqliteLayer({ storage: makeDurableObjectStorage() }),
|
||||
(sql) => Effect.scoped(Effect.andThen(sql.reserve, Effect.yieldNow)),
|
||||
)
|
||||
expect(leaked).toEqual([])
|
||||
})
|
||||
|
||||
test("workerd client releases the connection when a queued transaction is interrupted", async () => {
|
||||
const leaked = await sweep(
|
||||
() => workerdSqliteLayer({ storage: makeDurableObjectStorage() }),
|
||||
(sql) => sql.withTransaction(sql`SELECT 2`),
|
||||
)
|
||||
expect(leaked).toEqual([])
|
||||
})
|
||||
})
|
||||
@@ -424,7 +424,7 @@ it.effect("falls back to execDefaults over the connection spawner for unprovided
|
||||
const spawner = makeSpawner((command) =>
|
||||
Effect.sync(() => {
|
||||
if (command._tag === "StandardCommand") {
|
||||
spawned.push(command.args.at(3) ?? command.command)
|
||||
spawned.push(String(command.args.at(3) ?? command.command))
|
||||
}
|
||||
return makeHandle({
|
||||
pid: ProcessId(1),
|
||||
|
||||
@@ -63,7 +63,11 @@ const responseFromSnapshot = (request: HttpClientRequest.HttpClientRequest, snap
|
||||
export const redactedErrorRequest = (
|
||||
request: HttpClientRequest.HttpClientRequest,
|
||||
redactedUrl = redactUrl(request.url),
|
||||
) => HttpClientRequest.make(request.method)(redactedUrl)
|
||||
) => {
|
||||
// Received requests can carry any method string, which HttpClientRequest.make does not accept.
|
||||
const base = HttpClientRequest.get(redactedUrl)
|
||||
return HttpClientRequest.makeWith(request.method, base.url, base.urlParams, base.hash, base.headers, base.body)
|
||||
}
|
||||
const transportError = (request: HttpClientRequest.HttpClientRequest, description: string, redactedUrl?: string) =>
|
||||
new HttpClientError.HttpClientError({
|
||||
reason: new HttpClientError.TransportError({ request: redactedErrorRequest(request, redactedUrl), description }),
|
||||
|
||||
Generated
+6719
-1678
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,6 @@
|
||||
// Effect gives shared anonymous schemas encounter-order names (`Union_3`). Replace those names
|
||||
// with a hash of their canonical shape and sort components so unrelated additions stay local.
|
||||
// Effect gives shared anonymous schemas encounter-order names (`Union_3`), and does the same for the encoded side of a
|
||||
// named schema when it differs between uses (`Event.VersionEncoded_37`). Replace those names with a hash of their
|
||||
// canonical shape and sort components so unrelated additions stay local.
|
||||
export function stabilizeOpenApi(source: object) {
|
||||
const document = source as {
|
||||
components: { schemas: Record<string, unknown> }
|
||||
@@ -9,7 +10,9 @@ export function stabilizeOpenApi(source: object) {
|
||||
const families = ["Union", "Objects", "Arrays"].filter((name) => `${name}_` in schemas)
|
||||
const anonymous = new Map(
|
||||
Object.keys(schemas)
|
||||
.filter((name) => families.some((family) => new RegExp(`^${family}_\\d*$`).test(name)))
|
||||
.filter(
|
||||
(name) => families.some((family) => new RegExp(`^${family}_\\d*$`).test(name)) || /^.+Encoded_\d+$/.test(name),
|
||||
)
|
||||
.map((name) => [name, schemas[name]]),
|
||||
)
|
||||
|
||||
@@ -69,5 +72,71 @@ export function stabilizeOpenApi(source: object) {
|
||||
result.components.schemas = Object.fromEntries(
|
||||
[...stable].sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0)),
|
||||
)
|
||||
return collapseCopies(result)
|
||||
}
|
||||
|
||||
// Effect names a second AST copy of an identified schema `<Name>_<N>`; event streams reach their schemas without the
|
||||
// JSON codec that REST bodies get. Fold a copy into `<Name>` only when the two are deeply equal, treating a reference
|
||||
// to another copy as equal to its original only when that copy folds too. Any real difference keeps its own name.
|
||||
function collapseCopies<Document extends { components: { schemas: Record<string, unknown> } }>(document: Document) {
|
||||
const schemas = document.components.schemas
|
||||
const prefix = "#/components/schemas/"
|
||||
const copies = new Map<string, string>()
|
||||
for (const name of Object.keys(schemas)) {
|
||||
const original = /^(.+)_\d+$/.exec(name)?.[1]
|
||||
if (original !== undefined && Object.hasOwn(schemas, original)) copies.set(name, original)
|
||||
}
|
||||
const sameReference = (left: string, right: string) => {
|
||||
if (left === right) return true
|
||||
if (!left.startsWith(prefix) || !right.startsWith(prefix)) return false
|
||||
const a = left.slice(prefix.length)
|
||||
const b = right.slice(prefix.length)
|
||||
return copies.get(a) === b || copies.get(b) === a
|
||||
}
|
||||
const equal = (left: unknown, right: unknown): boolean => {
|
||||
if (Array.isArray(left) || Array.isArray(right)) {
|
||||
return (
|
||||
Array.isArray(left) &&
|
||||
Array.isArray(right) &&
|
||||
left.length === right.length &&
|
||||
left.every((item, index) => equal(item, right[index]))
|
||||
)
|
||||
}
|
||||
if (typeof left !== "object" || left === null || typeof right !== "object" || right === null) return left === right
|
||||
const entries = new Map<string, unknown>(Object.entries(left))
|
||||
const others = new Map<string, unknown>(Object.entries(right))
|
||||
if (entries.size !== others.size) return false
|
||||
return [...entries].every(([key, a]) => {
|
||||
if (!others.has(key)) return false
|
||||
const b = others.get(key)
|
||||
if (key === "$ref" && typeof a === "string" && typeof b === "string") return sameReference(a, b)
|
||||
return equal(a, b)
|
||||
})
|
||||
}
|
||||
let changed = true
|
||||
while (changed) {
|
||||
changed = false
|
||||
for (const [copy, original] of copies) {
|
||||
if (equal(schemas[copy], schemas[original])) continue
|
||||
copies.delete(copy)
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
if (copies.size === 0) return document
|
||||
const rewrite = (node: unknown): unknown => {
|
||||
if (Array.isArray(node)) return node.map(rewrite)
|
||||
if (typeof node !== "object" || node === null) return node
|
||||
return Object.fromEntries(
|
||||
Object.entries(node).map(([key, value]) => {
|
||||
if (key !== "$ref" || typeof value !== "string" || !value.startsWith(prefix)) return [key, rewrite(value)]
|
||||
const name = value.slice(prefix.length)
|
||||
return [key, `${prefix}${copies.get(name) ?? name}`]
|
||||
}),
|
||||
)
|
||||
}
|
||||
const result = rewrite(document) as Document
|
||||
result.components.schemas = Object.fromEntries(
|
||||
Object.entries(result.components.schemas).filter(([name]) => !copies.has(name)),
|
||||
)
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -68,6 +68,59 @@ test("includes reference siblings in anonymous component hashes", () => {
|
||||
expect(Object.keys(result.components.schemas).filter((name) => name.startsWith("Arrays_"))).toHaveLength(2)
|
||||
})
|
||||
|
||||
test("content-addresses encounter-order names of encoded variants", () => {
|
||||
const generate = (one: string, two: string) =>
|
||||
stabilizeOpenApi({
|
||||
components: {
|
||||
schemas: {
|
||||
[one]: { type: "number", enum: [1] },
|
||||
[two]: { type: "number", enum: [2] },
|
||||
Event: { anyOf: [{ $ref: `#/components/schemas/${one}` }, { $ref: `#/components/schemas/${two}` }] },
|
||||
},
|
||||
},
|
||||
}) as { components: { schemas: Record<string, unknown> } }
|
||||
|
||||
const result = generate("Event.VersionEncoded_47", "Event.VersionEncoded_37")
|
||||
expect(result).toEqual(generate("Event.VersionEncoded_3", "Event.VersionEncoded_9"))
|
||||
expect(
|
||||
Object.keys(result.components.schemas).filter((name) => /^Event\.VersionEncoded_[a-f0-9]{12}$/.test(name)),
|
||||
).toHaveLength(2)
|
||||
})
|
||||
|
||||
test("folds numbered copies of a named component only when they are deeply equal", () => {
|
||||
const ref = (name: string) => ({ $ref: `#/components/schemas/${name}` })
|
||||
const result = stabilizeOpenApi({
|
||||
components: {
|
||||
schemas: {
|
||||
Info: { type: "object", properties: { content: ref("Content") }, required: ["content"] },
|
||||
Info_1: { required: ["content"], properties: { content: ref("Content_1") }, type: "object" },
|
||||
Content: { type: "string" },
|
||||
Content_1: { type: "string" },
|
||||
Field: { type: "object", properties: { value: ref("Value") } },
|
||||
Field_1: { type: "object", properties: { value: ref("Value_1") } },
|
||||
Value: { type: "string", enum: ["a", "b"] },
|
||||
Value_1: { type: "string", enum: ["b", "a"] },
|
||||
},
|
||||
},
|
||||
paths: { "/info": { schema: ref("Info_1") }, "/field": { schema: ref("Field_1") } },
|
||||
}) as {
|
||||
components: { schemas: Record<string, unknown> }
|
||||
paths: Record<string, { schema: { $ref: string } }>
|
||||
}
|
||||
|
||||
expect(Object.keys(result.components.schemas).sort()).toEqual([
|
||||
"Content",
|
||||
"Field",
|
||||
"Field_1",
|
||||
"Info",
|
||||
"Value",
|
||||
"Value_1",
|
||||
])
|
||||
expect(result.paths["/info"].schema).toEqual(ref("Info"))
|
||||
expect(result.paths["/field"].schema).toEqual(ref("Field_1"))
|
||||
expect(result.components.schemas.Field_1).toEqual({ type: "object", properties: { value: ref("Value_1") } })
|
||||
})
|
||||
|
||||
test("preserves authored synthetic-looking names without an anonymous family root", () => {
|
||||
const result = stabilizeOpenApi({
|
||||
components: { schemas: { Union_2: { type: "string" } } },
|
||||
|
||||
@@ -52,7 +52,7 @@ export const ToastShow = Event.ephemeral({
|
||||
export const SessionSelect = Event.ephemeral({
|
||||
type: "tui.session.select",
|
||||
schema: {
|
||||
sessionID: SessionID.annotate({ description: "Session ID to navigate to" }),
|
||||
sessionID: SessionID.annotateKey({ description: "Session ID to navigate to" }),
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@@ -1,7 +1,19 @@
|
||||
import { isArrayNonEmpty } from "effect/Array"
|
||||
import * as NodeSink from "@effect/platform-node/NodeSink"
|
||||
import * as NodeStream from "@effect/platform-node/NodeStream"
|
||||
import { Deferred, Effect, Exit, FileSystem, Layer, Path, PlatformError, Predicate, Sink, Stream } from "effect"
|
||||
import {
|
||||
Deferred,
|
||||
Effect,
|
||||
Exit,
|
||||
FileSystem,
|
||||
Layer,
|
||||
Path,
|
||||
PlatformError,
|
||||
Predicate,
|
||||
Redacted,
|
||||
Sink,
|
||||
Stream,
|
||||
} from "effect"
|
||||
import type { Scope } from "effect"
|
||||
import { ChildProcess } from "effect/process"
|
||||
import {
|
||||
@@ -36,6 +48,31 @@ const killGroups = () =>
|
||||
}
|
||||
})
|
||||
|
||||
const unwrap = (value: string | Redacted.Redacted<string>): string =>
|
||||
Redacted.isRedacted(value) ? Redacted.value(value) : value
|
||||
|
||||
// Node's own NUL check throws an error that includes the value, so NUL is rejected here to keep Redacted values
|
||||
// out of errors.
|
||||
const findNulCharacter = (command: ChildProcess.StandardCommand): string | undefined => {
|
||||
if (command.command.includes("\0")) return "The command contains a NUL character"
|
||||
const index = command.args.findIndex((arg) => unwrap(arg).includes("\0"))
|
||||
if (index !== -1) return `Argument ${index + 1} contains a NUL character`
|
||||
for (const [name, value] of Object.entries(command.options.env ?? {})) {
|
||||
if (value !== undefined && unwrap(value).includes("\0")) {
|
||||
return `The environment variable ${name} contains a NUL character`
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
// Buffered writes can fail after the sink has been interrupted and removed its error listener. Keep a fallback until
|
||||
// close, without replacing the sink listener that propagates errors while it is active.
|
||||
const handleInputErrors = (writable: NodeJS.WritableStream) => {
|
||||
const onError = () => {}
|
||||
writable.on("error", onError)
|
||||
writable.once("close", () => writable.off("error", onError))
|
||||
}
|
||||
|
||||
const toError = (err: unknown): Error => (err instanceof globalThis.Error ? err : new globalThis.Error(String(err)))
|
||||
|
||||
const toTag = (err: NodeJS.ErrnoException): PlatformError.SystemErrorTag => {
|
||||
@@ -129,8 +166,15 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
|
||||
return path.resolve(opts.cwd)
|
||||
})
|
||||
|
||||
const env = (opts: ChildProcess.CommandOptions) =>
|
||||
opts.extendEnv ? { ...globalThis.process.env, ...opts.env } : opts.env
|
||||
const env = (opts: ChildProcess.CommandOptions) => {
|
||||
if (opts.env === undefined) return opts.extendEnv ? globalThis.process.env : undefined
|
||||
const result: Record<string, string | undefined> = opts.extendEnv ? { ...globalThis.process.env } : {}
|
||||
for (const [key, value] of Object.entries(opts.env)) {
|
||||
if (value === undefined) delete result[key]
|
||||
else result[key] = unwrap(value)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
const input = (x: ChildProcess.CommandInput | undefined): NodeChildProcess.IOType | undefined =>
|
||||
Stream.isStream(x) ? "pipe" : x
|
||||
@@ -203,37 +247,64 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
|
||||
const ins = new Map<number, Sink.Sink<void, Uint8Array, never, PlatformError.PlatformError>>()
|
||||
const outs = new Map<number, Stream.Stream<Uint8Array, PlatformError.PlatformError>>()
|
||||
|
||||
const setupInput = Effect.fnUntraced(function* (
|
||||
fd: number,
|
||||
node: NodeChildProcess.ChildProcess["stdio"][number],
|
||||
source: Stream.Stream<Uint8Array, PlatformError.PlatformError> | undefined,
|
||||
) {
|
||||
let sink: Sink.Sink<void, Uint8Array, never, PlatformError.PlatformError> = Sink.drain
|
||||
if (node && "write" in node) {
|
||||
handleInputErrors(node)
|
||||
sink = NodeSink.fromWritable({
|
||||
evaluate: () => node,
|
||||
onError: (err) => toPlatformError(`fromWritable(fd${fd})`, toError(err), command),
|
||||
endOnDone: true,
|
||||
})
|
||||
}
|
||||
if (source) yield* Effect.forkScoped(Stream.run(source, sink))
|
||||
ins.set(fd, sink)
|
||||
})
|
||||
|
||||
const setupOutput = (
|
||||
fd: number,
|
||||
node: NodeChildProcess.ChildProcess["stdio"][number],
|
||||
sink: Sink.Sink<Uint8Array, Uint8Array, never, PlatformError.PlatformError> | undefined,
|
||||
) => {
|
||||
let stream: Stream.Stream<Uint8Array, PlatformError.PlatformError> = Stream.empty
|
||||
if (node && "read" in node) {
|
||||
const tap = new PassThrough()
|
||||
// Retain errors raised before anyone reads, e.g. a failed write on a duplex fd, so they neither go uncaught
|
||||
// nor leave later readers hanging.
|
||||
let error: unknown = undefined
|
||||
tap.on("error", (cause) => {
|
||||
error ??= cause
|
||||
})
|
||||
node.on("error", (err) => tap.destroy(toError(err)))
|
||||
node.pipe(tap)
|
||||
const onError = (err: unknown) => toPlatformError(`fromReadable(fd${fd})`, toError(err), command)
|
||||
stream = Stream.suspend(() =>
|
||||
error === undefined ? NodeStream.fromReadable({ evaluate: () => tap, onError }) : Stream.fail(onError(error)),
|
||||
)
|
||||
}
|
||||
if (sink) stream = Stream.transduce(stream, sink)
|
||||
outs.set(fd, stream)
|
||||
}
|
||||
|
||||
for (const x of extra) {
|
||||
const node = proc.stdio[x.fd]
|
||||
switch (x.config.type) {
|
||||
case "input": {
|
||||
let sink: Sink.Sink<void, Uint8Array, never, PlatformError.PlatformError> = Sink.drain
|
||||
if (node && "write" in node) {
|
||||
sink = NodeSink.fromWritable({
|
||||
evaluate: () => node,
|
||||
onError: (err) => toPlatformError(`fromWritable(fd${x.fd})`, toError(err), command),
|
||||
endOnDone: true,
|
||||
})
|
||||
}
|
||||
if (x.config.stream) yield* Effect.forkScoped(Stream.run(x.config.stream, sink))
|
||||
ins.set(x.fd, sink)
|
||||
case "input":
|
||||
yield* setupInput(x.fd, node, x.config.stream)
|
||||
break
|
||||
}
|
||||
case "output": {
|
||||
let stream: Stream.Stream<Uint8Array, PlatformError.PlatformError> = Stream.empty
|
||||
if (node && "read" in node) {
|
||||
const tap = new PassThrough()
|
||||
node.on("error", (err) => tap.destroy(toError(err)))
|
||||
node.pipe(tap)
|
||||
stream = NodeStream.fromReadable({
|
||||
evaluate: () => tap,
|
||||
onError: (err) => toPlatformError(`fromReadable(fd${x.fd})`, toError(err), command),
|
||||
})
|
||||
}
|
||||
if (x.config.sink) stream = Stream.transduce(stream, x.config.sink)
|
||||
outs.set(x.fd, stream)
|
||||
case "output":
|
||||
setupOutput(x.fd, node, x.config.sink)
|
||||
break
|
||||
case "duplex":
|
||||
// Keep writing possible after the child closes its write side.
|
||||
if (node && "allowHalfOpen" in node) node.allowHalfOpen = true
|
||||
setupOutput(x.fd, node, x.config.sink)
|
||||
yield* setupInput(x.fd, node, x.config.stream)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,6 +322,7 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
|
||||
Effect.suspend(() => {
|
||||
let sink: Sink.Sink<void, unknown, never, PlatformError.PlatformError> = Sink.drain
|
||||
if (Predicate.isNotNull(proc.stdin)) {
|
||||
handleInputErrors(proc.stdin)
|
||||
sink = NodeSink.fromWritable({
|
||||
evaluate: () => proc.stdin!,
|
||||
onError: (err) => toPlatformError("fromWritable(stdin)", toError(err), command),
|
||||
@@ -302,11 +374,17 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
|
||||
Effect.callback<Spawned, PlatformError.PlatformError>((resume) => {
|
||||
const closed = Deferred.makeUnsafe<readonly [code: number | null, signal: NodeJS.Signals | null]>()
|
||||
const exited = Deferred.makeUnsafe<readonly [code: number | null, signal: NodeJS.Signals | null]>()
|
||||
const proc = launch(command.command, command.args, opts)
|
||||
const proc = launch(command.command, command.args.map(unwrap), opts)
|
||||
let settle = () => {}
|
||||
let end = false
|
||||
let exit: readonly [code: number | null, signal: NodeJS.Signals | null] | undefined
|
||||
proc.on("error", (err) => {
|
||||
// Node copies the arguments into `spawnargs`.
|
||||
if (command.args.some(Redacted.isRedacted)) {
|
||||
Object.assign(err, {
|
||||
spawnargs: [command.command, ...command.args.map((arg) => (Redacted.isRedacted(arg) ? "<redacted>" : arg))],
|
||||
})
|
||||
}
|
||||
resume(Effect.fail(toPlatformError("spawn", err, command)))
|
||||
})
|
||||
proc.on("exit", (...args) => {
|
||||
@@ -452,6 +530,12 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
|
||||
function* (command) {
|
||||
switch (command._tag) {
|
||||
case "StandardCommand": {
|
||||
const nul = findNulCharacter(command)
|
||||
if (nul !== undefined) {
|
||||
return yield* Effect.fail(
|
||||
PlatformError.badArgument({ module: "ChildProcess", method: "spawn", description: nul }),
|
||||
)
|
||||
}
|
||||
const sin = stdin(command.options)
|
||||
const sout = stdio(command.options, "stdout")
|
||||
const serr = stdio(command.options, "stderr")
|
||||
@@ -567,12 +651,14 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
|
||||
)
|
||||
continue
|
||||
}
|
||||
const name = ChildProcess.fdName(fd) as `fd${number}`
|
||||
const existing = next.options.additionalFds?.[name]
|
||||
handle = spawnCommand(
|
||||
ChildProcess.make(next.command, next.args, {
|
||||
...next.options,
|
||||
additionalFds: {
|
||||
...next.options.additionalFds,
|
||||
[ChildProcess.fdName(fd) as `fd${number}`]: { type: "input", stream },
|
||||
[name]: existing?.type === "duplex" ? { ...existing, stream } : { type: "input", stream },
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import path, { dirname, isAbsolute, join, relative, sep } from "path"
|
||||
import { realpathSync } from "fs"
|
||||
import { readdir } from "fs/promises"
|
||||
import { lookup } from "mime-types"
|
||||
import { Context, Effect, FileSystem, Layer, Schema } from "effect"
|
||||
import type { PlatformError } from "effect/PlatformError"
|
||||
@@ -22,11 +21,6 @@ export namespace FSUtil {
|
||||
|
||||
export type Error = PlatformError | FileSystemError
|
||||
|
||||
export interface DirEntry {
|
||||
readonly name: string
|
||||
readonly type: "file" | "directory" | "symlink" | "other"
|
||||
}
|
||||
|
||||
export interface UpOptions {
|
||||
readonly targets: string[]
|
||||
readonly start: string
|
||||
@@ -45,7 +39,6 @@ export namespace FSUtil {
|
||||
readonly writeJson: (path: string, data: unknown, mode?: number) => Effect.Effect<void, Error>
|
||||
readonly ensureDir: (path: string) => Effect.Effect<void, Error>
|
||||
readonly writeWithDirs: (path: string, content: string | Uint8Array, mode?: number) => Effect.Effect<void, Error>
|
||||
readonly readDirectoryEntries: (path: string) => Effect.Effect<DirEntry[], Error>
|
||||
readonly resolve: (path: string) => Effect.Effect<string>
|
||||
readonly findUp: (target: string, start: string, stop?: string) => Effect.Effect<string[], Error>
|
||||
readonly up: (options: UpOptions) => Effect.Effect<string[], Error>
|
||||
@@ -57,7 +50,7 @@ export namespace FSUtil {
|
||||
export class Service extends Context.Service<Service, Interface>()("@opencode/FSUtil") {}
|
||||
|
||||
// Exported so simulation can wrap this layer and override the methods that
|
||||
// bypass the injected FileSystem (readDirectoryEntries, scan, globUp).
|
||||
// bypass the injected FileSystem (scan, globUp).
|
||||
export const layer = Layer.effect(
|
||||
Service,
|
||||
Effect.gen(function* () {
|
||||
@@ -85,21 +78,6 @@ export namespace FSUtil {
|
||||
return info?.type === "File"
|
||||
})
|
||||
|
||||
const readDirectoryEntries = Effect.fn("FileSystem.readDirectoryEntries")(function* (dirPath: string) {
|
||||
return yield* Effect.tryPromise({
|
||||
try: async () => {
|
||||
const entries = await readdir(dirPath, { withFileTypes: true })
|
||||
return entries.map(
|
||||
(e): DirEntry => ({
|
||||
name: e.name,
|
||||
type: e.isDirectory() ? "directory" : e.isSymbolicLink() ? "symlink" : e.isFile() ? "file" : "other",
|
||||
}),
|
||||
)
|
||||
},
|
||||
catch: (cause) => new FileSystemError({ method: "readDirectoryEntries", cause }),
|
||||
})
|
||||
})
|
||||
|
||||
const resolve = Effect.fn("FileSystem.resolve")(function* (input: string) {
|
||||
const resolved = path.resolve(windowsPath(input))
|
||||
// Sandboxes deny reads outside granted paths; canonicalize what we can see.
|
||||
@@ -218,7 +196,6 @@ export namespace FSUtil {
|
||||
readFileStringSafe,
|
||||
isDir,
|
||||
isFile,
|
||||
readDirectoryEntries,
|
||||
resolve,
|
||||
readJson,
|
||||
writeJson,
|
||||
|
||||
Reference in New Issue
Block a user