GeezerVison is showing github.com — fix: reduce session event-loop load under many concurrent agents · anomalyco/opencode@d84bbc6
File treeExpand file treeCollapse file tree Open diff view settings server/routes/instance/httpapi/handlers
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/core/src/database/database.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,7 +36,7 @@ const layer = Layer.effect( | 36 | 36 |
| 37 | 37 | yield* db | 38 | 38 | .run("PRAGMA wal_checkpoint(PASSIVE)") | 39 | | - .pipe(Effect.ignore, Effect.repeat(Schedule.spaced("5 minutes")), Effect.forkScoped) | | 39 | + .pipe(Effect.ignore, Effect.repeat(Schedule.spaced("5 minutes").pipe(Schedule.jittered)), Effect.forkScoped) | 40 | 40 |
| 41 | 41 | return { db } | 42 | 42 | }).pipe(Effect.orDie), | |
Collapse file Copy file name to clipboard +17-3Lines changed: 17 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | export * as Wildcard from "./wildcard" | 2 | 2 |
| 3 | | -export function match(input: string, pattern: string) { | 4 | | - const normalized = input.replaceAll("\\", "/") | | 3 | +// Compiled patterns never carry the g or y flag, so the cached regex keeps no lastIndex state. | | 4 | +const compiled = new Map<string, RegExp>() | | 5 | + | | 6 | +function glob(pattern: string) { | | 7 | + const cached = compiled.get(pattern) | | 8 | + if (cached) return cached | 5 | 9 | let escaped = pattern | 6 | 10 | .replaceAll("\\", "/") | 7 | 11 | .replace(/[.+^${}()|[\]\\]/g, "\\$&") | @@ -10,5 +14,15 @@ export function match(input: string, pattern: string) { | 10 | 14 |
| 11 | 15 | if (escaped.endsWith(" .*")) escaped = escaped.slice(0, -3) + "( .*)?" | 12 | 16 |
| 13 | | - return new RegExp("^" + escaped + "$", process.platform === "win32" ? "si" : "s").test(normalized) | | 17 | + const expression = new RegExp("^" + escaped + "$", process.platform === "win32" ? "si" : "s") | | 18 | + if (compiled.size >= 512) { | | 19 | + const oldest = compiled.keys().next() | | 20 | + if (!oldest.done) compiled.delete(oldest.value) | | 21 | + } | | 22 | + compiled.set(pattern, expression) | | 23 | + return expression | | 24 | +} | | 25 | + | | 26 | +export function match(input: string, pattern: string) { | | 27 | + return glob(pattern).test(input.replaceAll("\\", "/")) | 14 | 28 | } |
Collapse file Copy file name to clipboard +36Lines changed: 36 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { describe, expect, test } from "bun:test" | | 2 | +import { Wildcard } from "@opencode-ai/core/util/wildcard" | | 3 | + | | 4 | +describe("Wildcard.match", () => { | | 5 | + test("matches exact patterns", () => { | | 6 | + expect(Wildcard.match("bash", "bash")).toBe(true) | | 7 | + expect(Wildcard.match("bash", "edit")).toBe(false) | | 8 | + }) | | 9 | + | | 10 | + test("supports star and question wildcards", () => { | | 11 | + expect(Wildcard.match("git status", "git *")).toBe(true) | | 12 | + expect(Wildcard.match("git status", "git st?tus")).toBe(true) | | 13 | + expect(Wildcard.match("git status", "git ?tatu")).toBe(false) | | 14 | + }) | | 15 | + | | 16 | + test("treats backslashes as path separators", () => { | | 17 | + expect(Wildcard.match("src\\index.ts", "src/index.ts")).toBe(true) | | 18 | + expect(Wildcard.match("src/index.ts", "src\\index.ts")).toBe(true) | | 19 | + }) | | 20 | + | | 21 | + test("escapes regex metacharacters in patterns", () => { | | 22 | + expect(Wildcard.match("file.txt", "file.txt")).toBe(true) | | 23 | + expect(Wildcard.match("fileXtxt", "file.txt")).toBe(false) | | 24 | + expect(Wildcard.match("a+b", "a+b")).toBe(true) | | 25 | + }) | | 26 | + | | 27 | + test("caches compiled patterns without changing results", () => { | | 28 | + const pattern = "packages/*/src/*.ts" | | 29 | + const first = Wildcard.match("packages/core/src/index.ts", pattern) | | 30 | + const second = Wildcard.match("packages/core/src/index.ts", pattern) | | 31 | + const mismatch = Wildcard.match("packages/core/test/index.ts", pattern) | | 32 | + expect(first).toBe(true) | | 33 | + expect(second).toBe(first) | | 34 | + expect(mismatch).toBe(false) | | 35 | + }) | | 36 | +}) |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/tui/event-batch.ts +55-10Lines changed: 55 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,17 +37,62 @@ function mergeDelta(left: GlobalEvent, right: GlobalEvent): GlobalEvent | undefi | 37 | 37 | } | 38 | 38 | } | 39 | 39 |
| 40 | | -// Folds runs of adjacent part deltas for the same part field into single events. | 41 | | -// Non-delta events never merge, and only adjacent events merge, so stream order | 42 | | -// is preserved for every consumer. | | 40 | +function deltaKey(event: GlobalEvent, properties: DeltaProperties): string { | | 41 | + return [ | | 42 | + event.directory, | | 43 | + event.project, | | 44 | + event.workspace, | | 45 | + properties.sessionID, | | 46 | + properties.messageID, | | 47 | + properties.partID, | | 48 | + properties.field, | | 49 | + ].join("\0") | | 50 | +} | | 51 | + | | 52 | +function partReplacementID(event: GlobalEvent): string | undefined { | | 53 | + const payload = event.payload | | 54 | + if (!payload || typeof payload !== "object") return | | 55 | + if (payload.type !== "message.part.updated" && payload.type !== "message.part.removed") return | | 56 | + const properties = payload.properties | | 57 | + if (!properties || typeof properties !== "object") return | | 58 | + if (typeof properties.partID === "string") return properties.partID | | 59 | + const part = properties.part | | 60 | + if (part && typeof part === "object" && typeof part.id === "string") return part.id | | 61 | +} | | 62 | + | | 63 | +// Folds part deltas for the same part field into single events even when other | | 64 | +// lanes interleave between them. A part update or removal for the same part | | 65 | +// invalidates its pending delta so appended text can never reorder across a | | 66 | +// full-part replacement. | 43 | 67 | export function collapseEventBatch(events: GlobalEvent[]): GlobalEvent[] { | 44 | | - return events.reduce<GlobalEvent[]>((collapsed, event) => { | 45 | | - const previous = collapsed[collapsed.length - 1] | 46 | | - const merged = previous ? mergeDelta(previous, event) : undefined | 47 | | - if (merged) collapsed[collapsed.length - 1] = merged | 48 | | - else collapsed.push(event) | 49 | | - return collapsed | 50 | | - }, []) | | 68 | + return events.reduce<{ collapsed: GlobalEvent[]; keys: Map<string, number> }>( | | 69 | + (state, event) => { | | 70 | + const delta = deltaProperties(event) | | 71 | + if (delta) { | | 72 | + const key = deltaKey(event, delta) | | 73 | + const index = state.keys.get(key) | | 74 | + const previous = index === undefined ? undefined : state.collapsed[index] | | 75 | + const merged = previous ? mergeDelta(previous, event) : undefined | | 76 | + if (merged && index !== undefined) { | | 77 | + state.collapsed[index] = merged | | 78 | + return state | | 79 | + } | | 80 | + state.keys.set(key, state.collapsed.length) | | 81 | + state.collapsed.push(event) | | 82 | + return state | | 83 | + } | | 84 | + const partID = partReplacementID(event) | | 85 | + if (partID) { | | 86 | + for (const [key, index] of state.keys) { | | 87 | + const pending = state.collapsed[index] | | 88 | + if (pending && deltaProperties(pending)?.partID === partID) state.keys.delete(key) | | 89 | + } | | 90 | + } | | 91 | + state.collapsed.push(event) | | 92 | + return state | | 93 | + }, | | 94 | + { collapsed: [], keys: new Map() }, | | 95 | + ).collapsed | 51 | 96 | } | 52 | 97 |
| 53 | 98 | // The event bridge mirrors every durable event as a second "sync" payload for | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/event-v2-bridge.ts +19-4Lines changed: 19 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,19 +16,34 @@ const layer = Layer.effect( | 16 | 16 | Effect.gen(function* () { | 17 | 17 | const events = yield* EventV2.Service | 18 | 18 |
| | 19 | + const locationCache = new Map<string, Location.Info>() | | 20 | + | 19 | 21 | const publish: EventV2.Interface["publish"] = (definition, data, options) => | 20 | 22 | Effect.gen(function* () { | 21 | 23 | if (options?.location) return yield* events.publish(definition, data, options) | 22 | 24 | const ctx = yield* InstanceRef | 23 | 25 | if (!ctx) return yield* events.publish(definition, data, options) | 24 | 26 | const workspaceID = yield* WorkspaceRef | 25 | | - return yield* events.publish(definition, data, { | 26 | | - ...options, | 27 | | - location: new Location.Info({ | | 27 | + // Location is constant per directory/workspace; building the schema class | | 28 | + // and two absolute paths on every event is measurable at delta rates. | | 29 | + const key = `${ctx.directory}\0${workspaceID ?? ""}\0${ctx.project.id}\0${ctx.worktree}` | | 30 | + let location = locationCache.get(key) | | 31 | + if (!location) { | | 32 | + location = new Location.Info({ | 28 | 33 | directory: AbsolutePath.make(ctx.directory), | 29 | 34 | ...(workspaceID ? { workspaceID } : {}), | 30 | 35 | project: { id: Project.ID.make(ctx.project.id), directory: AbsolutePath.make(ctx.worktree) }, | 31 | | - }), | | 36 | + }) | | 37 | + if (locationCache.size >= 64) { | | 38 | + const oldest = locationCache.keys().next() | | 39 | + if (!oldest.done) locationCache.delete(oldest.value) | | 40 | + } | | 41 | + Object.freeze(location) | | 42 | + locationCache.set(key, location) | | 43 | + } | | 44 | + return yield* events.publish(definition, data, { | | 45 | + ...options, | | 46 | + location, | 32 | 47 | }) | 33 | 48 | }) | 34 | 49 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/permission/index.ts +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,10 +26,9 @@ interface State { | 26 | 26 | } | 27 | 27 |
| 28 | 28 | export function evaluate(permission: string, pattern: string, ...rulesets: PermissionV1.Ruleset[]): PermissionV1.Rule { | | 29 | + const rules = rulesets.length === 1 ? rulesets[0] : rulesets.flat() | 29 | 30 | return ( | 30 | | - rulesets | 31 | | - .flat() | 32 | | - .findLast((rule) => Wildcard.match(permission, rule.permission) && Wildcard.match(pattern, rule.pattern)) ?? { | | 31 | + rules.findLast((rule) => Wildcard.match(permission, rule.permission) && Wildcard.match(pattern, rule.pattern)) ?? { | 33 | 32 | action: "ask", | 34 | 33 | permission, | 35 | 34 | pattern: "*", | @@ -71,7 +70,7 @@ const layer = Layer.effect( | 71 | 70 |
| 72 | 71 | for (const pattern of request.patterns) { | 73 | 72 | const rule = evaluate(request.permission, pattern, ruleset, approved) | 74 | | - yield* Effect.logInfo("evaluated", { permission: request.permission, pattern, action: rule }) | | 73 | + yield* Effect.logDebug("evaluated", { permission: request.permission, pattern, action: rule }) | 75 | 74 | if (rule.action === "deny") { | 76 | 75 | return yield* new PermissionV1.DeniedError({ | 77 | 76 | ruleset: ruleset.filter((rule) => Wildcard.match(request.permission, rule.permission)), | @@ -143,6 +142,7 @@ const layer = Layer.effect( | 143 | 142 | if (input.reply === "once") return | 144 | 143 |
| 145 | 144 | for (const pattern of existing.info.always) { | | 145 | + if (approved.some((rule) => rule.permission === existing.info.permission && rule.pattern === pattern)) continue | 146 | 146 | approved.push({ | 147 | 147 | permission: existing.info.permission, | 148 | 148 | pattern, | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/plugin/index.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -253,6 +253,8 @@ const layer = Layer.effect( | 253 | 253 | } | 254 | 254 |
| 255 | 255 | const unsubscribe = yield* events.listen((event) => { | | 256 | + // Streaming deltas fire per token; plugins consume completed parts, not raw stream text. | | 257 | + if (event.type === "message.part.delta") return Effect.void | 256 | 258 | if (event.location?.directory !== ctx.directory) return Effect.void | 257 | 259 | return Effect.sync(() => { | 258 | 260 | for (const hook of hooks) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/server/routes/instance/httpapi/handlers/event.ts +17-10Lines changed: 17 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,17 +28,24 @@ function eventResponse(events: EventV2.Interface) { | 28 | 28 | const workspaceID = yield* InstanceState.workspaceID | 29 | 29 | // Listener registration is eager, so events published after this point cannot | 30 | 30 | // be lost while the HTTP body fiber is starting or emitting server.connected. | 31 | | - const queue = yield* Queue.unbounded<EventV2.Payload>() | 32 | | - const unsubscribe = yield* events.listen((event) => Effect.sync(() => Queue.offerUnsafe(queue, event))) | 33 | | - yield* Effect.addFinalizer(() => unsubscribe) | 34 | | - const stream = Stream.fromQueue(queue).pipe( | 35 | | - Stream.filter( | 36 | | - (event) => | 37 | | - event.location?.directory === instance.directory && | 38 | | - (event.location.workspaceID === undefined || event.location.workspaceID === workspaceID), | 39 | | - ), | 40 | | - Stream.map((event) => ({ id: event.id, type: event.type, properties: event.data })), | | 31 | + // The buffer is bounded: when it overflows a server.desync marker is sent so | | 32 | + // clients resynchronize instead of silently diverging. | | 33 | + const queue = yield* Queue.dropping<{ id: string; type: string; properties: unknown }>(1024) | | 34 | + let dropped = false | | 35 | + const unsubscribe = yield* events.listen((event) => | | 36 | + Effect.sync(() => { | | 37 | + // Filter before enqueue so foreign directories and workspaces never occupy buffer space. | | 38 | + if (event.location?.directory !== instance.directory) return | | 39 | + if (event.location.workspaceID !== undefined && event.location.workspaceID !== workspaceID) return | | 40 | + if (dropped) { | | 41 | + if (!Queue.offerUnsafe(queue, { id: eventID(), type: "server.desync", properties: {} })) return | | 42 | + dropped = false | | 43 | + } | | 44 | + if (!Queue.offerUnsafe(queue, { id: event.id, type: event.type, properties: event.data })) dropped = true | | 45 | + }), | 41 | 46 | ) | | 47 | + yield* Effect.addFinalizer(() => unsubscribe) | | 48 | + const stream = Stream.fromQueue(queue) | 42 | 49 | const disposed = Stream.callback<{ id: string; type: string; properties: unknown }>((queue) => { | 43 | 50 | const listener = (event: { | 44 | 51 | directory?: string | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/processor.ts +16-10Lines changed: 16 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -355,14 +355,15 @@ const layer = Layer.effect( | 355 | 355 | ) | 356 | 356 | const recentParts = parts.slice(-DOOM_LOOP_THRESHOLD) | 357 | 357 |
| | 358 | + const inputNeedle = JSON.stringify(input) | 358 | 359 | if ( | 359 | 360 | recentParts.length !== DOOM_LOOP_THRESHOLD || | 360 | 361 | !recentParts.every( | 361 | 362 | (part) => | 362 | 363 | part.type === "tool" && | 363 | 364 | part.tool === value.name && | 364 | 365 | part.state.status !== "pending" && | 365 | | - JSON.stringify(part.state.input) === JSON.stringify(input), | | 366 | + JSON.stringify(part.state.input) === inputNeedle, | 366 | 367 | ) | 367 | 368 | ) { | 368 | 369 | return | @@ -468,14 +469,17 @@ const layer = Layer.effect( | 468 | 469 | cost: usage.cost, | 469 | 470 | }) | 470 | 471 | yield* session.updateMessage(ctx.assistantMessage) | | 472 | + const stepUnchanged = ctx.snapshot !== undefined && completedSnapshot === ctx.snapshot | 471 | 473 | if (ctx.snapshot) { | 472 | 474 | // track() stages then write-trees, so an unchanged tree proves the | 473 | 475 | // snapshot index already equals ctx.snapshot and patch() would diff | 474 | 476 | // to an empty file list. Skip its redundant stage + diff subprocesses. | 475 | | - const unchanged = completedSnapshot !== undefined && completedSnapshot === ctx.snapshot | 476 | | - const patch = unchanged | | 477 | + const patch = stepUnchanged | 477 | 478 | ? { hash: ctx.snapshot, files: [] as string[] } | 478 | | - : yield* snapshot.patch(ctx.snapshot) | | 479 | + : yield* snapshot.patch( | | 480 | + ctx.snapshot, | | 481 | + completedSnapshot !== undefined ? { to: completedSnapshot } : undefined, | | 482 | + ) | 479 | 483 | if (patch.files.length) { | 480 | 484 | yield* session.updatePart({ | 481 | 485 | id: PartID.ascending(), | @@ -488,12 +492,14 @@ const layer = Layer.effect( | 488 | 492 | } | 489 | 493 | ctx.snapshot = undefined | 490 | 494 | } | 491 | | - yield* summary | 492 | | - .summarize({ | 493 | | - sessionID: ctx.sessionID, | 494 | | - messageID: ctx.assistantMessage.parentID, | 495 | | - }) | 496 | | - .pipe(Effect.ignore, Effect.forkIn(scope)) | | 495 | + if (!stepUnchanged) { | | 496 | + yield* summary | | 497 | + .summarize({ | | 498 | + sessionID: ctx.sessionID, | | 499 | + messageID: ctx.assistantMessage.parentID, | | 500 | + }) | | 501 | + .pipe(Effect.ignore, Effect.forkIn(scope)) | | 502 | + } | 497 | 503 | if ( | 498 | 504 | !ctx.assistantMessage.summary && | 499 | 505 | isOverflow({ cfg: yield* config.get(), tokens: usage.tokens, model: ctx.model }) | |
|