GeezerVison is showing github.com — fix: bound startup-path network waits for offline hosts · PJ-Zetier/opencode@aede901
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/core/src/config/plugin/instruction.ts +17-3Lines changed: 17 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -141,11 +141,15 @@ export const Plugin = define({ | 141 | 141 | // A failed fetch keeps the last good copy or skips the URL, so an unreachable URL | 142 | 142 | // never makes the whole instruction set unavailable. | 143 | 143 | const reconfigure = Effect.fn("ConfigInstructionPlugin.reconfigure")( | 144 | | - function* () { | | 144 | + function* (options?: { readonly fetch?: boolean }) { | 145 | 145 | const entries = yield* config.entries() | 146 | 146 | const sources = Array.from( | 147 | 147 | new Set(entries.flatMap((entry) => (entry.type === "document" ? (entry.info.instructions ?? []) : []))), | 148 | 148 | ) | | 149 | + if (options?.fetch === false) { | | 150 | + configured.sources = sources | | 151 | + return | | 152 | + } | 149 | 153 | const fetched = yield* Effect.forEach( | 150 | 154 | sources.filter(isUrl), | 151 | 155 | (url) => | @@ -167,7 +171,7 @@ export const Plugin = define({ | 167 | 171 | fetched.filter((file) => file !== undefined).map((file) => [file.path, file] as const), | 168 | 172 | ) | 169 | 173 | }, | 170 | | - (effect) => lock.withPermit(effect), | | 174 | + (effect, ..._args: [options?: { readonly fetch?: boolean }]) => lock.withPermit(effect), | 171 | 175 | ) | 172 | 176 |
| 173 | 177 | const isolate = <A, E, R>(source: string, effect: Effect.Effect<A, E, R>) => | @@ -214,7 +218,9 @@ export const Plugin = define({ | 214 | 218 | Stream.runForEach(() => reconfigure().pipe(Effect.andThen(refresh()), Effect.andThen(discovery.reload()))), | 215 | 219 | Effect.forkScoped({ startImmediately: true }), | 216 | 220 | ) | 217 | | - yield* reconfigure() | | 221 | + // Configured local sources are explicit requests that resolve without the network, so the | | 222 | + // first refresh must include them before activation reports ready. | | 223 | + yield* reconfigure({ fetch: false }) | 218 | 224 | yield* refresh() | 219 | 225 | yield* discovery.transform((editor) => { | 220 | 226 | if (loaded.current.type === "unavailable") { | @@ -223,6 +229,14 @@ export const Plugin = define({ | 223 | 229 | } | 224 | 230 | for (const file of loaded.current.files) editor.add(file) | 225 | 231 | }) | | 232 | + // Configured sources may name remote URLs, whose fetch runs the way a config update does: | | 233 | + // forked and publishing through reload, never gating activation on the network. | | 234 | + if (configured.sources.some(isUrl)) | | 235 | + yield* reconfigure().pipe( | | 236 | + Effect.andThen(refresh()), | | 237 | + Effect.andThen(discovery.reload()), | | 238 | + Effect.forkScoped({ startImmediately: true }), | | 239 | + ) | 226 | 240 | }).pipe( | 227 | 241 | Effect.catchCause((cause) => | 228 | 242 | Effect.logWarning("failed to activate instruction source", { cause }).pipe( | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/config/plugin/skill.ts +12-7Lines changed: 12 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -147,34 +147,37 @@ export const Plugin = define({ | 147 | 147 | }) | 148 | 148 |
| 149 | 149 | const refresh = Effect.fn("ConfigSkillPlugin.refresh")( | 150 | | - function* (file?: string) { | | 150 | + function* (input?: { readonly file?: string; readonly remote?: boolean }) { | 151 | 151 | yield* FiberMap.clear(watches) | 152 | 152 | const skills = new Map<Skill.ID, Skill.Info>() | 153 | | - const current = sources() | | 153 | + const current = input?.remote === false ? sources().filter((source) => source.type === "directory") : sources() | 154 | 154 | for (const source of current) { | 155 | 155 | for (const skill of yield* load(source)) skills.set(skill.id, skill) | 156 | 156 | } | 157 | 157 | loaded.skills = Array.from(skills.values()) | 158 | | - if (file) { | | 158 | + if (input?.file) { | 159 | 159 | yield* Effect.logInfo("skills rescanned", { | 160 | | - file, | | 160 | + file: input.file, | 161 | 161 | sources: current.map(Skill.Source.key), | 162 | 162 | skills: loaded.skills.map((skill) => skill.id), | 163 | 163 | }) | 164 | 164 | } | 165 | 165 | }, | 166 | | - (effect, ..._args: [file?: string]) => lock.withPermit(effect), | | 166 | + (effect, ..._args: [input?: { readonly file?: string; readonly remote?: boolean }]) => lock.withPermit(effect), | 167 | 167 | ) | 168 | 168 |
| 169 | 169 | // Editor saves arrive as bursts of watcher events; settle before rescanning once. Subscribe | 170 | 170 | // before debouncing so no update slips through while the debounce starts its pull. | 171 | 171 | const updates = yield* PubSub.subscribe(changes) | 172 | 172 | yield* Stream.fromSubscription(updates).pipe( | 173 | 173 | Stream.debounce("100 millis"), | 174 | | - Stream.runForEach((file) => refresh(file).pipe(Effect.andThen(ctx.skill.reload()))), | | 174 | + Stream.runForEach((file) => refresh({ file }).pipe(Effect.andThen(ctx.skill.reload()))), | 175 | 175 | Effect.forkScoped({ startImmediately: true }), | 176 | 176 | ) | 177 | | - yield* refresh() | | 177 | + // A URL source waits on the network, which must not count against the plugin setup bound. | | 178 | + // Register locally available skills now; the first catalog pull runs the way a config update | | 179 | + // does: forked and publishing through reload. | | 180 | + yield* refresh({ remote: false }) | 178 | 181 | yield* ctx.skill.transform((editor) => { | 179 | 182 | for (const skill of loaded.skills) editor.add(skill) | 180 | 183 | }) | @@ -189,5 +192,7 @@ export const Plugin = define({ | 189 | 192 | ), | 190 | 193 | Effect.forkScoped({ startImmediately: true }), | 191 | 194 | ) | | 195 | + if (sources().some((source) => source.type === "url")) | | 196 | + yield* refresh().pipe(Effect.andThen(ctx.skill.reload()), Effect.forkScoped({ startImmediately: true })) | 192 | 197 | }), | 193 | 198 | }) |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/integration.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -735,7 +735,7 @@ const layer = Layer.effect( | 735 | 735 | if (!implementation?.refresh) return credential.value | 736 | 736 | const now = yield* Clock.currentTimeMillis | 737 | 737 | if (credential.value.expires > now + Duration.toMillis(Duration.minutes(5))) return credential.value | 738 | | - const value = yield* authorize(implementation.refresh(credential.value)) | | 738 | + const value = yield* authorize(implementation.refresh(credential.value).pipe(Effect.timeout("10 seconds"))) | 739 | 739 | yield* credentials.update(credential.id, { value }) | 740 | 740 | return value | 741 | 741 | }), | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/plugin/provider/opencode.ts +24-1Lines changed: 24 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -233,6 +233,9 @@ export const OpencodePlugin = define< | 233 | 233 | } = { config: undefined, connection: undefined, organization: undefined, mcp: undefined } | 234 | 234 | // Status last reported for the active connection, so inference failures can name the fix. | 235 | 235 | let reported: IntegrationConnection.Status | undefined | | 236 | + // Whether the last load fell back without a Console answer, so the boot re-check only chases | | 237 | + // config the Console never delivered (a 404 is an answer: no managed config). | | 238 | + let failed = false | 236 | 239 | let explainers: Effect.Success<ReturnType<typeof ctx.session.hook>>[] = [] | 237 | 240 | // When the last Console config load started, so rejected inference can reuse a recent status. | 238 | 241 | let checked = Number.NEGATIVE_INFINITY | @@ -261,9 +264,19 @@ export const OpencodePlugin = define< | 261 | 264 | })), | 262 | 265 | ) | 263 | 266 | }), | 264 | | - Effect.tap(() => status(undefined)), | | 267 | + // Generous enough for a cold TLS and auth round trip on a slow network: a spurious | | 268 | + // timeout at boot starts the server without the organization policies this config carries. | | 269 | + Effect.timeoutOrElse({ | | 270 | + duration: Duration.seconds(15), | | 271 | + orElse: () => Effect.fail(new Error("OpenCode Console config request timed out")), | | 272 | + }), | | 273 | + Effect.tap(() => { | | 274 | + failed = false | | 275 | + return status(undefined) | | 276 | + }), | 265 | 277 | Effect.catch((cause) => | 266 | 278 | Effect.gen(function* () { | | 279 | + failed = true | 267 | 280 | // Only well-formed Console errors change the connection status; anything else may be transient. | 268 | 281 | const next = connectionStatus(cause) | 269 | 282 | if (next) yield* status(next) | @@ -608,6 +621,16 @@ export const OpencodePlugin = define< | 608 | 621 | }) | 609 | 622 | yield* guard(snapshot) | 610 | 623 |
| | 624 | + // A connection whose config load failed runs without the organization policies the Console | | 625 | + // may carry; re-check ahead of the periodic loop so a slow or briefly unreachable Console | | 626 | + // converges to managed in seconds rather than a minute. | | 627 | + if (failed) | | 628 | + yield* check().pipe( | | 629 | + Effect.delay(Duration.seconds(15)), | | 630 | + Effect.repeat({ times: 2, until: () => !failed }), | | 631 | + Effect.forkScoped, | | 632 | + ) | | 633 | + | 611 | 634 | // Console config can change independently of local credential activity, so re-fetch | 612 | 635 | // periodically and only rebuild the catalog and search providers when the snapshot differs. | 613 | 636 | // Only while connected: a signed-out location runs no timer and no credential lookups. | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/skill/discovery.ts +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -73,6 +73,9 @@ const layer = Layer.effect( | 73 | 73 | schedule: Schedule.exponential(200).pipe(Schedule.jittered), | 74 | 74 | }), | 75 | 75 | HttpClient.filterStatusOk, | | 76 | + // A source that accepts connections but never answers (hung DNS, black-holed network) | | 77 | + // must not stall skill loading; the bound covers the retries above. | | 78 | + HttpClient.transformResponse(Effect.timeout("10 seconds")), | 76 | 79 | ) | 77 | 80 |
| 78 | 81 | const download = Effect.fn("SkillDiscovery.download")(function* (url: string, destination: string) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/config/skill.test.ts +33-3Lines changed: 33 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -53,8 +53,10 @@ const startEntries = Effect.fnUntraced(function* ( | 53 | 53 | claude: [], | 54 | 54 | agents: [], | 55 | 55 | }, | | 56 | + remote = false, | 56 | 57 | ) { | 57 | 58 | const service = yield* Skill.Service | | 59 | + const bus = yield* Bus.Service | 58 | 60 | const pluginHost = host({ | 59 | 61 | skill: { | 60 | 62 | list: () => Effect.die("unused skill.list"), | @@ -65,6 +67,13 @@ const startEntries = Effect.fnUntraced(function* ( | 65 | 67 | yield* ConfigCompatibilityPlugin.Plugin.effect(pluginHost).pipe( | 66 | 68 | Effect.provide(Config.testLayer(entries, compatibility)), | 67 | 69 | ) | | 70 | + // Activation registers the skill transform and publishes once; a configured URL source pulls | | 71 | + // forked after activation and publishes once more through reload. Await both for remote tests | | 72 | + // so they observe settled startup state. | | 73 | + const settled = remote | | 74 | + ? yield* bus.subscribe(Skill.Event.Updated).pipe(Stream.take(2), Stream.runDrain, Effect.forkScoped) | | 75 | + : undefined | | 76 | + yield* Effect.yieldNow | 68 | 77 | yield* ConfigSkillPlugin.Plugin.effect( | 69 | 78 | pluginHost, | 70 | 79 | ).pipe( | @@ -73,10 +82,11 @@ const startEntries = Effect.fnUntraced(function* ( | 73 | 82 | Effect.provideService(Global.Service, Global.Service.of({ ...Global.make(), home })), | 74 | 83 | Effect.provideService(Location.Service, Location.Service.of(location({ directory: AbsolutePath.make(directory) }))), | 75 | 84 | ) | | 85 | + if (settled) yield* Fiber.join(settled).pipe(Effect.timeout("5 seconds")) | 76 | 86 | return service | 77 | 87 | }) | 78 | 88 |
| 79 | | -const start = (skills: string[], directory: string, discovery = emptyDiscovery) => | | 89 | +const start = (skills: string[], directory: string, discovery = emptyDiscovery, remote = false) => | 80 | 90 | startEntries( | 81 | 91 | [ | 82 | 92 | new Document({ | @@ -87,6 +97,8 @@ const start = (skills: string[], directory: string, discovery = emptyDiscovery) | 87 | 97 | directory, | 88 | 98 | directory, | 89 | 99 | discovery, | | 100 | + undefined, | | 101 | + remote, | 90 | 102 | ) | 91 | 103 |
| 92 | 104 | const discover = (directory: string, global: string) => | @@ -237,7 +249,7 @@ describe("ConfigSkillPlugin.Plugin", () => { | 237 | 249 | }, | 238 | 250 | }) | 239 | 251 |
| 240 | | - const skill = yield* start([first, "https://example.test/skills/"], tmp.path, discovery) | | 252 | + const skill = yield* start([first, "https://example.test/skills/"], tmp.path, discovery, true) | 241 | 253 | expect((yield* skill.list()).map((item) => item.id).toSorted()).toEqual([ | 242 | 254 | Skill.ID.make("deploy"), | 243 | 255 | Skill.ID.make("review"), | @@ -295,7 +307,25 @@ describe("ConfigSkillPlugin.Plugin", () => { | 295 | 307 | const url = "https://unreachable.example.test/skills/" | 296 | 308 | const discovery = SkillDiscovery.Service.of({ pull: () => Effect.die(`failed to pull ${url}`) }) | 297 | 309 |
| 298 | | - const skill = yield* start([tmp.path, url], tmp.path, discovery) | | 310 | + const skill = yield* start([tmp.path, url], tmp.path, discovery, true) | | 311 | + expect((yield* skill.list()).find((item) => item.id === "review")?.description).toBe("Available") | | 312 | + }), | | 313 | + ), | | 314 | + ), | | 315 | + ) | | 316 | + | | 317 | + it.live("activates with directory skills while a URL source hangs", () => | | 318 | + Effect.acquireDisposable(Effect.promise(() => tmpdir())).pipe( | | 319 | + Effect.flatMap((tmp) => | | 320 | + Effect.gen(function* () { | | 321 | + yield* Effect.promise(async () => { | | 322 | + await fs.mkdir(path.join(tmp.path, "review"), { recursive: true }) | | 323 | + await write(tmp.path, "review", "Available") | | 324 | + }) | | 325 | + const discovery = SkillDiscovery.Service.of({ pull: () => Effect.never }) | | 326 | + | | 327 | + // The forked catalog pull never settles; activation must still return with local skills. | | 328 | + const skill = yield* start([tmp.path, "https://hung.example.test/skills/"], tmp.path, discovery) | 299 | 329 | expect((yield* skill.list()).find((item) => item.id === "review")?.description).toBe("Available") | 300 | 330 | }), | 301 | 331 | ), | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/instruction-discovery.test.ts +15-2Lines changed: 15 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -74,6 +74,19 @@ const start = Effect.fnUntraced(function* (ctx = host()) { | 74 | 74 | return yield* InstructionDiscovery.Service | 75 | 75 | }) | 76 | 76 |
| | 77 | +// Activation publishes once registering the discovery transform, and the forked URL fetch | | 78 | +// publishes once more through reload; await both so tests observe settled remote state. | | 79 | +const startRemote = Effect.fnUntraced(function* (ctx = host()) { | | 80 | + const bus = yield* Bus.Service | | 81 | + const settled = yield* bus | | 82 | + .subscribe(InstructionDiscovery.Event.Updated) | | 83 | + .pipe(Stream.take(2), Stream.runDrain, Effect.forkScoped) | | 84 | + yield* Effect.yieldNow | | 85 | + const discovery = yield* start(ctx) | | 86 | + yield* Fiber.join(settled).pipe(Effect.timeout("5 seconds")) | | 87 | + return discovery | | 88 | +}) | | 89 | + | 77 | 90 | const configured = (instructions: string[]) => new Document({ type: "document", info: new Info({ instructions }) }) | 78 | 91 |
| 79 | 92 | const readFailure = (tag: "NotFound" | "Unknown", code: string, file: string) => | @@ -317,7 +330,7 @@ describe("ConfigInstructionPlugin.Plugin", () => { | 317 | 330 | ]) | 318 | 331 | }) | 319 | 332 |
| 320 | | - const discovery = yield* start() | | 333 | + const discovery = yield* startRemote() | 321 | 334 | const initialized = yield* readInitial(yield* discovery.load()) | 322 | 335 | // The unreachable URL and missing file are skipped without making AGENTS.md unavailable. | 323 | 336 | expect(initialized.text).toBe( | @@ -388,7 +401,7 @@ describe("ConfigInstructionPlugin.Plugin", () => { | 388 | 401 | }) | 389 | 402 | const events = yield* PubSub.unbounded<ReturnType<typeof configUpdated>>() | 390 | 403 | const config = yield* Config.Test | 391 | | - const discovery = yield* start(host({ event: { subscribe: () => Stream.fromPubSub(events) } })) | | 404 | + const discovery = yield* startRemote(host({ event: { subscribe: () => Stream.fromPubSub(events) } })) | 392 | 405 | const initialized = yield* readInitial(yield* discovery.load()) | 393 | 406 | expect(initialized.text).toBe( | 394 | 407 | [`Instructions from: ${first}\nfirst`, `Instructions from: ${remote}\nremote`].join("\n\n"), | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/npm.test.ts +72-1Lines changed: 72 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,7 +2,7 @@ import fs from "fs/promises" | 2 | 2 | import path from "path" | 3 | 3 | import { pathToFileURL } from "url" | 4 | 4 | import { describe, expect, test } from "bun:test" | 5 | | -import { Effect } from "effect" | | 5 | +import { Duration, Effect } from "effect" | 6 | 6 | import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" | 7 | 7 | import { Global } from "@opencode/util/global" | 8 | 8 | import { Npm } from "@opencode/util/npm" | @@ -111,6 +111,30 @@ async function createRegistryFixture(directory: string) { | 111 | 111 | } | 112 | 112 | } | 113 | 113 |
| | 114 | +function createHungRegistry() { | | 115 | + // Accepts requests and never answers, like a black-holed or firewalled registry. | | 116 | + const server = Bun.serve({ | | 117 | + hostname: "127.0.0.1", | | 118 | + port: 0, | | 119 | + fetch: () => new Promise<Response>(() => {}), | | 120 | + }) | | 121 | + return { | | 122 | + url: server.url, | | 123 | + async configure(cache: string, spec: string) { | | 124 | + const root = path.join(cache, "npm", await Npm.cacheKey(spec)) | | 125 | + await fs.mkdir(root, { recursive: true }) | | 126 | + await Bun.write( | | 127 | + path.join(root, ".npmrc"), | | 128 | + `registry=${server.url}\n@fixture:registry=${server.url}\ncache=${path.join(cache, "npm-cache")}\nfetch-retries=0\n`, | | 129 | + ) | | 130 | + return root | | 131 | + }, | | 132 | + async [Symbol.asyncDispose]() { | | 133 | + await server.stop(true) | | 134 | + }, | | 135 | + } | | 136 | +} | | 137 | + | 114 | 138 | describe("Npm.sanitize", () => { | 115 | 139 | test("keeps normal scoped package specs unchanged", () => { | 116 | 140 | expect(Npm.sanitize("@opencode/acme")).toBe("@opencode/acme") | @@ -310,6 +334,29 @@ describe("Npm.add", () => { | 310 | 334 | expect(result.current).toBeFalse() | 311 | 335 | }, 30_000) | 312 | 336 |
| | 337 | + test("fails within the configured bound when the registry never responds", async () => { | | 338 | + await using tmp = await tmpdir() | | 339 | + await using registry = createHungRegistry() | | 340 | + const cache = path.join(tmp.path, "cache") | | 341 | + const spec = "@fixture/hung-plugin@latest" | | 342 | + const root = await registry.configure(cache, spec) | | 343 | + | | 344 | + const error = await Effect.gen(function* () { | | 345 | + const npm = yield* Npm.Service | | 346 | + return yield* npm.add(spec).pipe(Effect.flip) | | 347 | + }).pipe( | | 348 | + Effect.provideService(Npm.Timeouts, { install: Duration.millis(500), check: Duration.millis(500) }), | | 349 | + Effect.scoped, | | 350 | + Effect.provide(npmLayer(cache)), | | 351 | + Effect.runPromise, | | 352 | + ) | | 353 | + | | 354 | + expect(error).toBeInstanceOf(Npm.InstallFailedError) | | 355 | + if (error instanceof Npm.InstallFailedError) expect(String(error.cause)).toContain("did not finish") | | 356 | + // The timed-out install must leave no staging directory behind. | | 357 | + expect((await fs.readdir(root)).filter((entry) => entry.startsWith(".staging-"))).toEqual([]) | | 358 | + }) | | 359 | + | 313 | 360 | // Symlink creation needs elevated privileges on Windows. | 314 | 361 | test.skipIf(win)("records Git revisions when the cache directory is reached through a symlink", async () => { | 315 | 362 | await using tmp = await tmpdir() | @@ -443,4 +490,28 @@ describe("Npm.check and Npm.update", () => { | 443 | 490 |
| 444 | 491 | expect(outdated).toBeFalse() | 445 | 492 | }) | | 493 | + | | 494 | + test("fails checks within the configured bound when the registry never responds", async () => { | | 495 | + await using tmp = await tmpdir() | | 496 | + await using registry = createHungRegistry() | | 497 | + const cache = path.join(tmp.path, "cache") | | 498 | + const spec = "@fixture/hung-plugin@latest" | | 499 | + const root = await registry.configure(cache, spec) | | 500 | + const directory = path.join(root, "1000", "node_modules", "@fixture", "hung-plugin") | | 501 | + await fs.mkdir(directory, { recursive: true }) | | 502 | + await writePackage(directory, { name: "@fixture/hung-plugin" }) | | 503 | + | | 504 | + const error = await Effect.gen(function* () { | | 505 | + const npm = yield* Npm.Service | | 506 | + return yield* npm.check(spec).pipe(Effect.flip) | | 507 | + }).pipe( | | 508 | + Effect.provideService(Npm.Timeouts, { install: Duration.millis(500), check: Duration.millis(500) }), | | 509 | + Effect.scoped, | | 510 | + Effect.provide(npmLayer(cache)), | | 511 | + Effect.runPromise, | | 512 | + ) | | 513 | + | | 514 | + expect(error).toBeInstanceOf(Npm.InstallFailedError) | | 515 | + if (error instanceof Npm.InstallFailedError) expect(String(error.cause)).toContain("did not finish") | | 516 | + }) | 446 | 517 | }) |
|