Skip to content

Commit d84bbc6

Browse files
committed
fix: reduce session event-loop load under many concurrent agents
- cache compiled wildcard patterns (permission checks built a fresh RegExp per comparison) - demote the per-pattern permission log, flatten rulesets once, dedupe approved rules - skip diffFull for unchanged turn spans; memoize the last turn diff per session/message (non-empty only, bounded) with single-flight summarize - reuse the step-finish tree for patch diffs via patch(hash, { to }) instead of re-staging the shared index - skip the summarize fork when a step's snapshot tree is unchanged - hoist the doom-loop input needle out of the per-event compare - SSE: filter before enqueue, bounded dropping queue, server.desync marker when a client lags - memoize and freeze the per-directory Location.Info payload - merge part deltas by part key instead of adjacency; skip raw deltas for plugin hooks - jitter the periodic WAL checkpoint so processes do not checkpoint in lockstep
1 parent 6c1ddb3 commit d84bbc6

12 files changed

Lines changed: 246 additions & 57 deletions

File tree

‎packages/core/src/database/database.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const layer = Layer.effect(
3636

3737
yield* db
3838
.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)
4040

4141
return { db }
4242
}).pipe(Effect.orDie),

‎packages/core/src/util/wildcard.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
export * as Wildcard from "./wildcard"
22

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
59
let escaped = pattern
610
.replaceAll("\\", "/")
711
.replace(/[.+^${}()|[\]\\]/g, "\\$&")
@@ -10,5 +14,15 @@ export function match(input: string, pattern: string) {
1014

1115
if (escaped.endsWith(" .*")) escaped = escaped.slice(0, -3) + "( .*)?"
1216

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("\\", "/"))
1428
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
})

‎packages/opencode/src/cli/tui/event-batch.ts‎

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,62 @@ function mergeDelta(left: GlobalEvent, right: GlobalEvent): GlobalEvent | undefi
3737
}
3838
}
3939

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.
4367
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
5196
}
5297

5398
// The event bridge mirrors every durable event as a second "sync" payload for

‎packages/opencode/src/event-v2-bridge.ts‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,34 @@ const layer = Layer.effect(
1616
Effect.gen(function* () {
1717
const events = yield* EventV2.Service
1818

19+
const locationCache = new Map<string, Location.Info>()
20+
1921
const publish: EventV2.Interface["publish"] = (definition, data, options) =>
2022
Effect.gen(function* () {
2123
if (options?.location) return yield* events.publish(definition, data, options)
2224
const ctx = yield* InstanceRef
2325
if (!ctx) return yield* events.publish(definition, data, options)
2426
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({
2833
directory: AbsolutePath.make(ctx.directory),
2934
...(workspaceID ? { workspaceID } : {}),
3035
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,
3247
})
3348
})
3449

‎packages/opencode/src/permission/index.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ interface State {
2626
}
2727

2828
export function evaluate(permission: string, pattern: string, ...rulesets: PermissionV1.Ruleset[]): PermissionV1.Rule {
29+
const rules = rulesets.length === 1 ? rulesets[0] : rulesets.flat()
2930
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)) ?? {
3332
action: "ask",
3433
permission,
3534
pattern: "*",
@@ -71,7 +70,7 @@ const layer = Layer.effect(
7170

7271
for (const pattern of request.patterns) {
7372
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 })
7574
if (rule.action === "deny") {
7675
return yield* new PermissionV1.DeniedError({
7776
ruleset: ruleset.filter((rule) => Wildcard.match(request.permission, rule.permission)),
@@ -143,6 +142,7 @@ const layer = Layer.effect(
143142
if (input.reply === "once") return
144143

145144
for (const pattern of existing.info.always) {
145+
if (approved.some((rule) => rule.permission === existing.info.permission && rule.pattern === pattern)) continue
146146
approved.push({
147147
permission: existing.info.permission,
148148
pattern,

‎packages/opencode/src/plugin/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ const layer = Layer.effect(
253253
}
254254

255255
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
256258
if (event.location?.directory !== ctx.directory) return Effect.void
257259
return Effect.sync(() => {
258260
for (const hook of hooks) {

‎packages/opencode/src/server/routes/instance/httpapi/handlers/event.ts‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,24 @@ function eventResponse(events: EventV2.Interface) {
2828
const workspaceID = yield* InstanceState.workspaceID
2929
// Listener registration is eager, so events published after this point cannot
3030
// 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+
}),
4146
)
47+
yield* Effect.addFinalizer(() => unsubscribe)
48+
const stream = Stream.fromQueue(queue)
4249
const disposed = Stream.callback<{ id: string; type: string; properties: unknown }>((queue) => {
4350
const listener = (event: {
4451
directory?: string

‎packages/opencode/src/session/processor.ts‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -355,14 +355,15 @@ const layer = Layer.effect(
355355
)
356356
const recentParts = parts.slice(-DOOM_LOOP_THRESHOLD)
357357

358+
const inputNeedle = JSON.stringify(input)
358359
if (
359360
recentParts.length !== DOOM_LOOP_THRESHOLD ||
360361
!recentParts.every(
361362
(part) =>
362363
part.type === "tool" &&
363364
part.tool === value.name &&
364365
part.state.status !== "pending" &&
365-
JSON.stringify(part.state.input) === JSON.stringify(input),
366+
JSON.stringify(part.state.input) === inputNeedle,
366367
)
367368
) {
368369
return
@@ -468,14 +469,17 @@ const layer = Layer.effect(
468469
cost: usage.cost,
469470
})
470471
yield* session.updateMessage(ctx.assistantMessage)
472+
const stepUnchanged = ctx.snapshot !== undefined && completedSnapshot === ctx.snapshot
471473
if (ctx.snapshot) {
472474
// track() stages then write-trees, so an unchanged tree proves the
473475
// snapshot index already equals ctx.snapshot and patch() would diff
474476
// 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
477478
? { 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+
)
479483
if (patch.files.length) {
480484
yield* session.updatePart({
481485
id: PartID.ascending(),
@@ -488,12 +492,14 @@ const layer = Layer.effect(
488492
}
489493
ctx.snapshot = undefined
490494
}
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+
}
497503
if (
498504
!ctx.assistantMessage.summary &&
499505
isOverflow({ cfg: yield* config.get(), tokens: usage.tokens, model: ctx.model })

0 commit comments

Comments
 (0)