From 494f9a39aaf934a7a111380e37438cb86ebc54bf Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Fri, 17 Apr 2026 15:36:05 -0400 Subject: [PATCH 1/4] fix(opencode): skip share sync for unshared sessions --- packages/opencode/src/share/share-next.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/opencode/src/share/share-next.ts b/packages/opencode/src/share/share-next.ts index 3484d5da76cb..66481ef882d6 100644 --- a/packages/opencode/src/share/share-next.ts +++ b/packages/opencode/src/share/share-next.ts @@ -118,6 +118,9 @@ export const layer = Layer.effect( function sync(sessionID: SessionID, data: Data[]): Effect.Effect { return Effect.gen(function* () { if (disabled) return + const share = yield* get(sessionID) + if (!share) return + const s = yield* InstanceState.get(state) const existing = s.queue.get(sessionID) if (existing) { From 8a42d89b85a7b6ebfcdf17bcef31348df27aa3f7 Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Fri, 17 Apr 2026 15:45:13 -0400 Subject: [PATCH 2/4] perf(opencode): cache shared session membership --- packages/opencode/src/share/share-next.ts | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/opencode/src/share/share-next.ts b/packages/opencode/src/share/share-next.ts index 66481ef882d6..34a5de0f9d69 100644 --- a/packages/opencode/src/share/share-next.ts +++ b/packages/opencode/src/share/share-next.ts @@ -40,6 +40,7 @@ export type Share = typeof ShareSchema.Type type State = { queue: Map }> scope: Scope.Closeable + shared: Set } type Data = @@ -118,10 +119,9 @@ export const layer = Layer.effect( function sync(sessionID: SessionID, data: Data[]): Effect.Effect { return Effect.gen(function* () { if (disabled) return - const share = yield* get(sessionID) - if (!share) return - const s = yield* InstanceState.get(state) + if (!s.shared.has(sessionID)) return + const existing = s.queue.get(sessionID) if (existing) { for (const item of data) { @@ -146,7 +146,7 @@ export const layer = Layer.effect( const state: InstanceState.InstanceState = yield* InstanceState.make( Effect.fn("ShareNext.state")(function* (_ctx) { - const cache: State = { queue: new Map(), scope: yield* Scope.make() } + const cache: State = { queue: new Map(), scope: yield* Scope.make(), shared: new Set() } yield* Effect.addFinalizer(() => Scope.close(cache.scope, Exit.void).pipe( @@ -160,6 +160,11 @@ export const layer = Layer.effect( if (disabled) return cache + const existingShares = yield* db((db) => db.select({ sessionID: SessionShareTable.session_id }).from(SessionShareTable).all()) + for (const row of existingShares) { + cache.shared.add(row.sessionID as SessionID) + } + const watch = ( def: D, fn: (evt: { properties: any }) => Effect.Effect, @@ -239,7 +244,10 @@ export const layer = Layer.effect( s.queue.delete(sessionID) const share = yield* get(sessionID) - if (!share) return + if (!share) { + s.shared.delete(sessionID) + return + } const req = yield* request() const res = yield* HttpClientRequest.post(`${req.baseUrl}${req.api.sync(share.id)}`).pipe( @@ -310,6 +318,7 @@ export const layer = Layer.effect( .run(), ) const s = yield* InstanceState.get(state) + s.shared.add(sessionID) yield* full(sessionID).pipe( Effect.catchCause((cause) => Effect.sync(() => { @@ -335,6 +344,9 @@ export const layer = Layer.effect( ) yield* db((db) => db.delete(SessionShareTable).where(eq(SessionShareTable.session_id, sessionID)).run()) + const s = yield* InstanceState.get(state) + s.shared.delete(sessionID) + s.queue.delete(sessionID) }) return Service.of({ init, url, request, create, remove }) From 7a5da57bcc327a3b0cef3637c774250ade1b96b8 Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Fri, 17 Apr 2026 15:57:10 -0400 Subject: [PATCH 3/4] perf(opencode): cache share membership lazily --- packages/opencode/src/share/share-next.ts | 26 ++++++++++++----------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/packages/opencode/src/share/share-next.ts b/packages/opencode/src/share/share-next.ts index 34a5de0f9d69..3f249d605471 100644 --- a/packages/opencode/src/share/share-next.ts +++ b/packages/opencode/src/share/share-next.ts @@ -40,7 +40,7 @@ export type Share = typeof ShareSchema.Type type State = { queue: Map }> scope: Scope.Closeable - shared: Set + shared: Map } type Data = @@ -120,7 +120,14 @@ export const layer = Layer.effect( return Effect.gen(function* () { if (disabled) return const s = yield* InstanceState.get(state) - if (!s.shared.has(sessionID)) return + const cached = s.shared.get(sessionID) + if (cached === false) return + if (cached === undefined) { + const share = yield* get(sessionID) + const isShared = share !== undefined + s.shared.set(sessionID, isShared) + if (!isShared) return + } const existing = s.queue.get(sessionID) if (existing) { @@ -146,7 +153,7 @@ export const layer = Layer.effect( const state: InstanceState.InstanceState = yield* InstanceState.make( Effect.fn("ShareNext.state")(function* (_ctx) { - const cache: State = { queue: new Map(), scope: yield* Scope.make(), shared: new Set() } + const cache: State = { queue: new Map(), scope: yield* Scope.make(), shared: new Map() } yield* Effect.addFinalizer(() => Scope.close(cache.scope, Exit.void).pipe( @@ -158,12 +165,7 @@ export const layer = Layer.effect( ), ) - if (disabled) return cache - - const existingShares = yield* db((db) => db.select({ sessionID: SessionShareTable.session_id }).from(SessionShareTable).all()) - for (const row of existingShares) { - cache.shared.add(row.sessionID as SessionID) - } + if (disabled) return cache const watch = ( def: D, @@ -245,7 +247,7 @@ export const layer = Layer.effect( const share = yield* get(sessionID) if (!share) { - s.shared.delete(sessionID) + s.shared.set(sessionID, false) return } @@ -318,7 +320,7 @@ export const layer = Layer.effect( .run(), ) const s = yield* InstanceState.get(state) - s.shared.add(sessionID) + s.shared.set(sessionID, true) yield* full(sessionID).pipe( Effect.catchCause((cause) => Effect.sync(() => { @@ -345,7 +347,7 @@ export const layer = Layer.effect( yield* db((db) => db.delete(SessionShareTable).where(eq(SessionShareTable.session_id, sessionID)).run()) const s = yield* InstanceState.get(state) - s.shared.delete(sessionID) + s.shared.set(sessionID, false) s.queue.delete(sessionID) }) From b324bbddcd09dd1f130f85243ba8a8c1bfbcc554 Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Fri, 17 Apr 2026 16:08:00 -0400 Subject: [PATCH 4/4] refactor(opencode): simplify share membership caching --- packages/opencode/src/share/share-next.ts | 60 +++++++++++++---------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/packages/opencode/src/share/share-next.ts b/packages/opencode/src/share/share-next.ts index 3f249d605471..2622f4f7f0cb 100644 --- a/packages/opencode/src/share/share-next.ts +++ b/packages/opencode/src/share/share-next.ts @@ -38,9 +38,9 @@ const ShareSchema = Schema.Struct({ export type Share = typeof ShareSchema.Type type State = { - queue: Map }> + queue: Map> scope: Scope.Closeable - shared: Map + shared: Map } type Data = @@ -119,26 +119,20 @@ export const layer = Layer.effect( function sync(sessionID: SessionID, data: Data[]): Effect.Effect { return Effect.gen(function* () { if (disabled) return - const s = yield* InstanceState.get(state) - const cached = s.shared.get(sessionID) - if (cached === false) return - if (cached === undefined) { - const share = yield* get(sessionID) - const isShared = share !== undefined - s.shared.set(sessionID, isShared) - if (!isShared) return - } + const share = yield* getCached(sessionID) + if (!share) return + const s = yield* InstanceState.get(state) const existing = s.queue.get(sessionID) if (existing) { for (const item of data) { - existing.data.set(key(item), item) + existing.set(key(item), item) } return } const next = new Map(data.map((item) => [key(item), item])) - s.queue.set(sessionID, { data: next }) + s.queue.set(sessionID, next) yield* flush(sessionID).pipe( Effect.delay(1000), Effect.catchCause((cause) => @@ -153,19 +147,20 @@ export const layer = Layer.effect( const state: InstanceState.InstanceState = yield* InstanceState.make( Effect.fn("ShareNext.state")(function* (_ctx) { - const cache: State = { queue: new Map(), scope: yield* Scope.make(), shared: new Map() } + const cache: State = { queue: new Map(), scope: yield* Scope.make(), shared: new Map() } yield* Effect.addFinalizer(() => Scope.close(cache.scope, Exit.void).pipe( Effect.andThen( Effect.sync(() => { cache.queue.clear() + cache.shared.clear() }), ), ), ) - if (disabled) return cache + if (disabled) return cache const watch = ( def: D, @@ -237,6 +232,18 @@ export const layer = Layer.effect( return { id: row.id, secret: row.secret, url: row.url } satisfies Share }) + const getCached = Effect.fnUntraced(function* (sessionID: SessionID) { + const s = yield* InstanceState.get(state) + if (s.shared.has(sessionID)) { + const cached = s.shared.get(sessionID) + return cached === null ? undefined : cached + } + + const share = yield* get(sessionID) + s.shared.set(sessionID, share ?? null) + return share + }) + const flush = Effect.fn("ShareNext.flush")(function* (sessionID: SessionID) { if (disabled) return const s = yield* InstanceState.get(state) @@ -245,16 +252,13 @@ export const layer = Layer.effect( s.queue.delete(sessionID) - const share = yield* get(sessionID) - if (!share) { - s.shared.set(sessionID, false) - return - } + const share = yield* getCached(sessionID) + if (!share) return const req = yield* request() const res = yield* HttpClientRequest.post(`${req.baseUrl}${req.api.sync(share.id)}`).pipe( HttpClientRequest.setHeaders(req.headers), - HttpClientRequest.bodyJson({ secret: share.secret, data: Array.from(queued.data.values()) }), + HttpClientRequest.bodyJson({ secret: share.secret, data: Array.from(queued.values()) }), Effect.flatMap((r) => http.execute(r)), ) @@ -320,7 +324,7 @@ export const layer = Layer.effect( .run(), ) const s = yield* InstanceState.get(state) - s.shared.set(sessionID, true) + s.shared.set(sessionID, result) yield* full(sessionID).pipe( Effect.catchCause((cause) => Effect.sync(() => { @@ -335,8 +339,13 @@ export const layer = Layer.effect( const remove = Effect.fn("ShareNext.remove")(function* (sessionID: SessionID) { if (disabled) return log.info("removing share", { sessionID }) - const share = yield* get(sessionID) - if (!share) return + const s = yield* InstanceState.get(state) + const share = yield* getCached(sessionID) + if (!share) { + s.shared.delete(sessionID) + s.queue.delete(sessionID) + return + } const req = yield* request() yield* HttpClientRequest.delete(`${req.baseUrl}${req.api.remove(share.id)}`).pipe( @@ -346,8 +355,7 @@ export const layer = Layer.effect( ) yield* db((db) => db.delete(SessionShareTable).where(eq(SessionShareTable.session_id, sessionID)).run()) - const s = yield* InstanceState.get(state) - s.shared.set(sessionID, false) + s.shared.delete(sessionID) s.queue.delete(sessionID) })