diff --git a/packages/core/src/location-lifecycle.ts b/packages/core/src/location-lifecycle.ts index 18faf851ee6..1045e52e44b 100644 --- a/packages/core/src/location-lifecycle.ts +++ b/packages/core/src/location-lifecycle.ts @@ -13,6 +13,9 @@ import { Rpc } from "./rpc.js" import { SessionEvent } from "./session/event.js" const isSessionEvent = Schema.is(SessionEvent.Durable) +const durableSessionEventTypes: ReadonlySet = new Set( + SessionEvent.DurableDefinitions.map((definition) => definition.type), +) export class Service extends Context.Service< Service, @@ -37,13 +40,17 @@ const layer = Layer.effect( const rpc = yield* Rpc.Service const project = yield* Project.Service const scope = yield* Effect.scope + const self = LocationServiceMap.canonical( + Location.Ref.make({ directory: location.directory, workspaceID: location.workspaceID }), + ) yield* project.activate(location.project.id) const unsubscribe = yield* bus.listen((event) => { - if (!isSessionEvent(event) || !event.location) return Effect.void + if (!event.location || !durableSessionEventTypes.has(event.type)) return Effect.void const ref = LocationServiceMap.canonical(event.location) - if (ref.directory !== location.directory || ref.workspaceID !== location.workspaceID) return Effect.void + if (ref.directory !== self.directory || ref.workspaceID !== self.workspaceID) return Effect.void + if (!isSessionEvent(event)) return Effect.void // Listeners run inside publish; keep the write and its Project event off that path. - return project.activate(location.project.id).pipe(Effect.forkIn(scope), Effect.asVoid) + return project.activate(location.project.id, { forkIn: scope }) }) yield* Effect.addFinalizer(() => unsubscribe) let closed = false diff --git a/packages/core/src/project.ts b/packages/core/src/project.ts index 5aa050f8253..63a523b5fb0 100644 --- a/packages/core/src/project.ts +++ b/packages/core/src/project.ts @@ -63,9 +63,15 @@ export const root = Effect.fn("Project.root")( Effect.orElseSucceed(() => undefined), ) -const ACTIVATE_INTERVAL = 60_000 +export const ACTIVATE_INTERVAL = 60_000 const SWEEP_INTERVAL = Duration.hours(1) +export interface Options { + readonly now?: () => number +} + +const Now = Context.Reference<() => number>("@opencode/Project/Now", { defaultValue: () => Date.now }) + export interface ListInput { /** Includes archived Projects when true. */ readonly archived?: boolean @@ -76,11 +82,16 @@ export interface ResolveOptions { readonly persist?: boolean } +export interface ActivateOptions { + /** Forks unthrottled persistence writes into `forkIn` instead of blocking the caller. */ + readonly forkIn?: Scope.Scope +} + export interface Interface { readonly list: (input?: ListInput) => Effect.Effect> readonly update: (input: UpdateInput) => Effect.Effect /** Records Project activity for recency ordering, at most once per minute per Project. */ - readonly activate: (projectID: ID) => Effect.Effect + readonly activate: (projectID: ID, options?: ActivateOptions) => Effect.Effect /** Resolves the owning Project, persisting it unless `persist` is false. */ readonly resolve: (input: AbsolutePath, options?: ResolveOptions) => Effect.Effect /** Archives Projects whose known directories are all gone; resolving a Project unarchives it. */ @@ -118,6 +129,7 @@ function fromRow(row: typeof ProjectTable.$inferSelect): Info { const layer = Layer.effect( Service, Effect.gen(function* () { + const now = yield* Now const fs = yield* FSUtil.Service const git = yield* Git.Service const proc = yield* AppProcess.Service @@ -273,20 +285,26 @@ const layer = Layer.effect( }) const activated = new Map() - const activate = Effect.fn("Project.activate")(function* (projectID: ID) { - const now = Date.now() - if (now - (activated.get(projectID) ?? 0) < ACTIVATE_INTERVAL) return - activated.set(projectID, now) + const persistActivate = Effect.fn("Project.activate")(function* (projectID: ID, timestamp: number) { const row = yield* db .update(ProjectTable) // Activity is not a metadata edit, so keep time_updated out of the implicit bump. - .set({ time_active: now, time_updated: sql`${ProjectTable.time_updated}` }) + .set({ time_active: timestamp, time_updated: sql`${ProjectTable.time_updated}` }) .where(eq(ProjectTable.id, projectID)) .returning() .get() .pipe(Effect.orDie) if (row) yield* bus.publish(ProjectSchema.Event.Updated, fromRow(row)) }) + const activate = (projectID: ID, options?: ActivateOptions): Effect.Effect => + Effect.suspend(() => { + const timestamp = now() + const last = activated.get(projectID) + if (last !== undefined && timestamp - last < ACTIVATE_INTERVAL) return Effect.void + activated.set(projectID, timestamp) + const write = persistActivate(projectID, timestamp) + return options?.forkIn ? write.pipe(Effect.forkIn(options.forkIn), Effect.asVoid) : write + }) const cached = Effect.fnUntraced(function* (dir: string) { return yield* fs.readFileString(path.join(dir, "opencode")).pipe( @@ -490,11 +508,15 @@ const layer = Layer.effect( }), ) -export const node = makeGlobalNode({ - service: Service, - layer: layer, - deps: [Bus.node, Database.node, FSUtil.node, Git.node, AppProcess.node], -}) +export function configured(options?: Options) { + return makeGlobalNode({ + service: Service, + layer: options?.now ? layer.pipe(Layer.provide(Layer.succeed(Now, options.now))) : layer, + deps: [Bus.node, Database.node, FSUtil.node, Git.node, AppProcess.node], + }) +} + +export const node = configured() export const sweepNode = makeGlobalNode({ name: "project-archive-sweep", diff --git a/packages/core/test/project-activate.test.ts b/packages/core/test/project-activate.test.ts new file mode 100644 index 00000000000..8bd08fd3260 --- /dev/null +++ b/packages/core/test/project-activate.test.ts @@ -0,0 +1,144 @@ +import { describe, expect } from "bun:test" +import { Effect, Exit, Fiber, Layer, Scope, Tracer } from "effect" +import { Agent } from "@opencode/schema/agent" +import { SessionID } from "@opencode/schema/session-id" +import { SessionMessage } from "@opencode/schema/session-message" +import { Bus } from "@opencode/core/bus" +import { Database } from "@opencode/core/database/database" +import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" +import { Location } from "@opencode/core/location" +import { LocationLifecycle } from "@opencode/core/location-lifecycle" +import { Project } from "@opencode/core/project" +import { ProjectTable } from "@opencode/core/project/sql" +import { AbsolutePath, NonNegativeInt } from "@opencode/core/schema" +import { SessionEvent } from "@opencode/core/session/event" +import { LayerNode } from "@opencode/util/effect/layer-node" +import { Global } from "@opencode/util/global" +import { tempGlobalLayer } from "./fixture/global" +import { testEffect } from "./lib/effect" + +const it = testEffect(Layer.empty) + +function spanRecorder() { + const spans: Array<{ readonly span: Tracer.NativeSpan; readonly fiberID: number | undefined }> = [] + const tracer = Tracer.make({ + span(options) { + const span = new Tracer.NativeSpan(options) + spans.push({ span, fiberID: Fiber.getCurrent()?.id }) + return span + }, + }) + return { + tracer, + activateSpans: () => spans.filter((entry) => entry.span.name === "Project.activate"), + } +} + +describe("Project.activate throttling", () => { + it.effect("stays lazy and skips spans and forks on throttled repeats", () => + Effect.gen(function* () { + const { tracer, activateSpans } = spanRecorder() + let currentNow = 100_000 + const layer = AppNodeBuilder.build(LayerNode.group([Database.node, Bus.node, Project.node]), [ + Global.node.replace(tempGlobalLayer), + Project.node.replace(Project.configured({ now: () => currentNow })), + ]) + + yield* Effect.gen(function* () { + const callerID = Fiber.getCurrent()?.id + const db = (yield* Database.Service).db + const project = yield* Project.Service + const forkScope = yield* Scope.make() + const id = Project.ID.make("proj_throttled") + yield* db + .insert(ProjectTable) + .values({ + id, + worktree: AbsolutePath.make("/repo"), + sandboxes: [], + time_created: 1, + time_updated: 1, + time_active: 1, + }) + .run() + + // Building the effect without executing it must not consume the throttle window. + const unused = project.activate(id) + expect(unused).toBeDefined() + + for (let i = 0; i < 25; i++) { + yield* project.activate(id, { forkIn: forkScope }) + } + yield* Effect.yieldNow + yield* Scope.close(forkScope, Exit.void) + expect(activateSpans()).toHaveLength(1) + expect(activateSpans()[0]?.fiberID).not.toBe(callerID) + expect((yield* project.list())[0]?.time.active).toBe(100_000) + + currentNow += Project.ACTIVATE_INTERVAL - 1 + yield* project.activate(id) + expect((yield* project.list())[0]?.time.active).toBe(100_000) + + currentNow += 1 + for (let i = 0; i < 10; i++) { + yield* project.activate(id) + } + expect((yield* project.list())[0]?.time.active).toBe(100_000 + Project.ACTIVATE_INTERVAL) + }).pipe(Effect.provideService(Tracer.Tracer, tracer), Effect.provide(layer)) + + expect(activateSpans()).toHaveLength(2) + }), + ) + + it.effect("ignores non-matching bus traffic and throttles durable lifecycle updates", () => + Effect.gen(function* () { + const { tracer, activateSpans } = spanRecorder() + let currentNow = 200_000 + const targetRef = Location.Ref.make({ directory: AbsolutePath.make("/repo/one") }) + const otherRef = Location.Ref.make({ directory: AbsolutePath.make("/repo/two") }) + + const layer = AppNodeBuilder.build(LayerNode.group([Bus.node, LocationLifecycle.node]), [ + Global.node.replace(tempGlobalLayer), + Location.node.replace(Location.boundNode(targetRef)), + Project.node.replace(Project.configured({ now: () => currentNow })), + ]) + + yield* Effect.gen(function* () { + const bus = yield* Bus.Service + const sessionID = SessionID.make("ses_test123456") + const messageID = SessionMessage.ID.make("msg_test123456") + expect(activateSpans()).toHaveLength(1) + + for (let i = 0; i < 10; i++) { + yield* bus.publish( + SessionEvent.Text.Delta, + { sessionID, assistantMessageID: messageID, ordinal: NonNegativeInt.make(0), delta: "hi" }, + { location: targetRef }, + ) + yield* bus.publish( + SessionEvent.AgentSelected, + { sessionID, agent: Agent.ID.make("build") }, + { location: otherRef }, + ) + yield* bus.publish( + SessionEvent.AgentSelected, + { sessionID, agent: Agent.ID.make("build") }, + { location: targetRef }, + ) + } + yield* Effect.yieldNow + expect(activateSpans()).toHaveLength(1) + + currentNow += Project.ACTIVATE_INTERVAL + yield* bus.publish( + SessionEvent.AgentSelected, + { sessionID, agent: Agent.ID.make("build") }, + { location: targetRef }, + ) + yield* Effect.yieldNow + }).pipe(Effect.provide(layer), Effect.provideService(Tracer.Tracer, tracer)) + + expect(activateSpans()).toHaveLength(2) + }), + ) +})