|
| 1 | +import { expect } from "bun:test" |
| 2 | +import { Clock, Context, Duration, Effect, Exit, Layer, RcMap, Schedule, Scope } from "effect" |
| 3 | +import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" |
| 4 | +import { LayerNode } from "@opencode/util/effect/layer-node" |
| 5 | +import { makeGlobalNode } from "@opencode/util/effect/app-node" |
| 6 | +import { Global } from "@opencode/util/global" |
| 7 | +import { Bus } from "@opencode/core/bus" |
| 8 | +import { Database } from "@opencode/core/database/database" |
| 9 | +import { Job } from "@opencode/core/job" |
| 10 | +import { Location } from "@opencode/core/location" |
| 11 | +import { LocationActivity } from "@opencode/core/location-activity" |
| 12 | +import { LocationLifecycle } from "@opencode/core/location-lifecycle" |
| 13 | +import { LocationServiceMap } from "@opencode/core/location-services" |
| 14 | +import { AbsolutePath } from "@opencode/core/schema" |
| 15 | +import { Session } from "@opencode/core/session" |
| 16 | +import { SessionExecution } from "@opencode/core/session/execution" |
| 17 | +import { SessionStore } from "@opencode/core/session/store" |
| 18 | +import { tempGlobalLayer } from "./fixture/global" |
| 19 | +import { offlineModels } from "./fixture/models" |
| 20 | +import { tmpdirScoped } from "./fixture/tmpdir" |
| 21 | +import { testEffect } from "./lib/effect" |
| 22 | + |
| 23 | +// LocationActivity measures idleness on this clock; only the test moves it past the TTL (see browser-idle.test.ts). |
| 24 | +const timeToLive = Duration.minutes(1) |
| 25 | +const idle = { millis: 0 } |
| 26 | +const live = Clock.Clock.defaultValue() |
| 27 | +const activityClock: Clock.Clock = { |
| 28 | + currentTimeMillisUnsafe: () => live.currentTimeMillisUnsafe() + idle.millis, |
| 29 | + currentTimeMillis: Effect.sync(() => live.currentTimeMillisUnsafe() + idle.millis), |
| 30 | + currentTimeNanosUnsafe: () => live.currentTimeNanosUnsafe() + BigInt(idle.millis) * 1_000_000n, |
| 31 | + currentTimeNanos: Effect.sync(() => live.currentTimeNanosUnsafe() + BigInt(idle.millis) * 1_000_000n), |
| 32 | + monotonicTimeNanosUnsafe: () => live.monotonicTimeNanosUnsafe(), |
| 33 | + monotonicTimeNanos: live.monotonicTimeNanos, |
| 34 | + sleep: (duration) => live.sleep(duration), |
| 35 | +} |
| 36 | + |
| 37 | +const it = testEffect( |
| 38 | + AppNodeBuilder.build( |
| 39 | + LayerNode.group([Database.node, Bus.node, Session.node, LocationServiceMap.node, LocationActivity.node]), |
| 40 | + [ |
| 41 | + Global.node.replace(tempGlobalLayer), |
| 42 | + offlineModels, |
| 43 | + LocationActivity.node.replace( |
| 44 | + makeGlobalNode({ |
| 45 | + service: LocationActivity.Service, |
| 46 | + layer: LocationActivity.layer({ timeToLive, sweepInterval: "100 millis" }).pipe( |
| 47 | + Layer.provide(Layer.succeed(Clock.Clock, activityClock)), |
| 48 | + ), |
| 49 | + deps: [Bus.node, Job.node, LocationServiceMap.node, SessionExecution.node, SessionStore.node], |
| 50 | + }), |
| 51 | + ), |
| 52 | + ], |
| 53 | + ), |
| 54 | +) |
| 55 | + |
| 56 | +it.live( |
| 57 | + "idle sweep keeps a retained location and evicts it once released", |
| 58 | + () => |
| 59 | + Effect.gen(function* () { |
| 60 | + const locations = yield* LocationServiceMap.Service |
| 61 | + const [retained, sibling] = yield* Effect.forEach([0, 1], () => |
| 62 | + tmpdirScoped().pipe( |
| 63 | + Effect.map((dir) => |
| 64 | + LocationServiceMap.canonical(Location.Ref.make({ directory: AbsolutePath.make(dir.path) })), |
| 65 | + ), |
| 66 | + ), |
| 67 | + ) |
| 68 | + const lifecycle = Context.get( |
| 69 | + yield* locations.contextEffect(retained).pipe(Effect.scoped), |
| 70 | + LocationLifecycle.Service, |
| 71 | + ) |
| 72 | + yield* locations.contextEffect(sibling).pipe(Effect.scoped) |
| 73 | + const hold = yield* Scope.make() |
| 74 | + yield* lifecycle.retain.pipe(Scope.provide(hold)) |
| 75 | + const cached = (ref: Location.Ref) => RcMap.has(locations.rcMap, ref) |
| 76 | + // Each poll idles every location for a full TTL, whenever the sweep first saw it. |
| 77 | + const until = (check: Effect.Effect<boolean>) => |
| 78 | + Effect.sync(() => { |
| 79 | + idle.millis += Duration.toMillis(timeToLive) |
| 80 | + }).pipe( |
| 81 | + Effect.andThen(check), |
| 82 | + Effect.flatMap((done) => (done ? Effect.void : Effect.fail("pending"))), |
| 83 | + Effect.retry(Schedule.spaced("20 millis")), |
| 84 | + Effect.timeout("10 seconds"), |
| 85 | + Effect.orDie, |
| 86 | + ) |
| 87 | + |
| 88 | + // Both expired; the sweep that evicts the sibling skips the retained location. |
| 89 | + yield* until(cached(sibling).pipe(Effect.map((has) => !has))) |
| 90 | + expect(yield* cached(retained)).toBe(true) |
| 91 | + expect(lifecycle.isClosed()).toBe(false) |
| 92 | + |
| 93 | + yield* Scope.close(hold, Exit.void) |
| 94 | + yield* until(cached(retained).pipe(Effect.map((has) => !has))) |
| 95 | + expect(lifecycle.isClosed()).toBe(true) |
| 96 | + }), |
| 97 | + 30_000, |
| 98 | +) |
0 commit comments