@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 243 lines 9.2 KB
Raw
1 import { PgClient, type PgConnection } from "@effect/sql-pg"
2 import { PostgreSqlContainer } from "@testcontainers/postgresql"
3 import { Clock, Context, Data, type Duration, Effect, Layer, Redacted, type Scope, String } from "effect"
4 import * as Fs from "node:fs"
5 import { inspect } from "node:util"
6 import { getContainerRuntimeClient, Wait } from "testcontainers"
7
8 export class ContainerError extends Data.TaggedError("ContainerError")<{
9 cause: unknown
10 }> {}
11
12 // Opt-in diagnostics only: keep cleanup errors visible and preserve the teardown sequence.
13 const lifecycleEnabled = process.env.PG_LIFECYCLE_DIAGNOSTICS === "1"
14 const lifecycleFile = process.env.PG_LIFECYCLE_LOG
15 const lifecycleLog = (id: string, step: string, state: string, ms: number, error?: unknown) => {
16 if (!lifecycleEnabled) return
17 const line = `[pg-lifecycle] ${new Date().toISOString()} pid=${process.pid} ${id} ${step} ${state} ${ms}ms${
18 error === undefined ? "" : ` error=${inspect(error)}`
19 }\n`
20 // Bypass the reporter, which can drop console output from hooks. Diagnostics
21 // must not turn successful cleanup into a failure or replace its original error.
22 try {
23 process.stderr.write(line)
24 } catch {}
25 if (lifecycleFile !== undefined) {
26 try {
27 Fs.appendFileSync(lifecycleFile, line)
28 } catch (error) {
29 try {
30 process.stderr.write(`[pg-lifecycle] unable to append diagnostics: ${inspect(error)}\n`)
31 } catch {}
32 }
33 }
34 }
35
36 const lifecycle = async <A>(id: string, step: string, run: () => Promise<A>, signal?: AbortSignal): Promise<A> => {
37 if (!lifecycleEnabled) return run()
38 const started = Date.now()
39 const log = (state: string) => lifecycleLog(id, step, state, Date.now() - started)
40 log("start")
41 const pending = setInterval(() => log("pending"), 5_000)
42 pending.unref()
43 const interrupted = () => {
44 clearInterval(pending)
45 log("interrupted")
46 }
47 signal?.addEventListener("abort", interrupted, { once: true })
48 try {
49 const value = await run()
50 log("done")
51 return value
52 } catch (error) {
53 lifecycleLog(id, step, "failed", Date.now() - started, error)
54 throw error
55 } finally {
56 clearInterval(pending)
57 signal?.removeEventListener("abort", interrupted)
58 }
59 }
60
61 const marker = (id: string, step: string) => Effect.sync(() => lifecycleLog(id, step, "mark", 0))
62
63 // Brackets the client layer's finalizers: `disposal start` runs first on close,
64 // `disposal done` runs after every finalizer of the inner layer.
65 const instrumentClient = <A, E, R>(
66 name: string,
67 containerId: string,
68 inner: Layer.Layer<A, E, R>
69 ): Effect.Effect<Layer.Layer<A, E, R>, never, Scope.Scope> =>
70 !lifecycleEnabled ? Effect.succeed(inner) : Effect.as(
71 Effect.addFinalizer(() => marker(containerId, `${name} disposal done`)),
72 inner.pipe(Layer.tap(() => Effect.addFinalizer(() => marker(containerId, `${name} disposal start`))))
73 )
74
75 // Shared with the regression tests so they exercise the real container finalizer.
76 export const releasePgContainer = Effect.fnUntraced(
77 function*(container: {
78 getId(): string
79 stop(options: { remove: boolean }): Promise<unknown>
80 }, options?: {
81 // Lets the regression tests exhaust the retries without waiting 15 seconds.
82 readonly waitTimeout?: Duration.Input | undefined
83 }) {
84 const id = container.getId()
85 lifecycleLog(id, "container-release", "start", 0)
86 yield* Effect.promise(() => lifecycle(id, "stop", () => container.stop({ remove: false })))
87 const client = yield* Effect.promise(() => lifecycle(id, "runtime-client", () => getContainerRuntimeClient()))
88 const handle = client.container.getById(container.getId())
89 // Stop can return before the daemon processes the exit event; wait before removing.
90 // docker-modem can leave an aborted response pending forever. Abort each
91 // timed-out request and retry this read-only wait, never the whole release.
92 yield* Effect.tryPromise((signal) =>
93 lifecycle(id, "wait-not-running", () => handle.wait({ condition: "not-running", abortSignal: signal }), signal)
94 ).pipe(Effect.timeout(options?.waitTimeout ?? "5 seconds"), Effect.retry({ times: 2 }))
95 yield* Effect.promise(() => lifecycle(id, "remove", () => client.container.remove(handle, { removeVolumes: true })))
96 lifecycleLog(id, "container-release", "done", 0)
97 },
98 // Bound the entire release below the existing 30s hook limit, including
99 // stop and removal. Exhausted retries and timeouts remain visible defects.
100 Effect.timeout("20 seconds"),
101 (_) => Effect.orDie(_),
102 // Fixture finalizers inherit TestClock from it.layer; cleanup must use real deadlines.
103 Effect.provideService(Clock.Clock, Clock.Clock.defaultValue())
104 )
105
106 // PostgreSqlContainer also waits for listening ports, probing them with
107 // `docker exec` calls that have no timeout. In Deno CI one probe never exited,
108 // stalling startup past the 30s hook limit while the container was healthy.
109 // Its pg_isready health check already requires a TCP listener on localhost.
110 export const pgWaitStrategy = () => Wait.forHealthCheck()
111
112 export class PgContainer extends Context.Service<PgContainer>()("test/PgContainer", {
113 make: Effect.acquireRelease(
114 Effect.tryPromise({
115 try: () =>
116 lifecycle(
117 "new",
118 "container-start",
119 () => new PostgreSqlContainer("postgres:14-alpine").withWaitStrategy(pgWaitStrategy()).start()
120 ),
121 catch: (cause) => new ContainerError({ cause })
122 }).pipe(Effect.tap((container) => marker(container.getId(), "container-started"))),
123 (container) => releasePgContainer(container)
124 )
125 }) {
126 static readonly layer = Layer.effect(this)(this.make)
127
128 static layerClient = Layer.unwrap(
129 Effect.gen(function*() {
130 const container = yield* PgContainer
131 return yield* instrumentClient(
132 "layerClient",
133 container.getId(),
134 PgClient.layer({
135 url: Redacted.make(container.getConnectionUri())
136 })
137 )
138 })
139 ).pipe(Layer.provide(this.layer))
140
141 static layerMakeClient = Layer.unwrap(
142 Effect.gen(function*() {
143 const container = yield* PgContainer
144 return yield* instrumentClient(
145 "layerMakeClient",
146 container.getId(),
147 PgClient.layerFrom(PgClient.makeClient({
148 url: Redacted.make(container.getConnectionUri())
149 }))
150 )
151 })
152 ).pipe(Layer.provide(this.layer))
153
154 static layerMakeClientUnprepared = Layer.unwrap(
155 Effect.gen(function*() {
156 const container = yield* PgContainer
157 return yield* instrumentClient(
158 "layerMakeClientUnprepared",
159 container.getId(),
160 PgClient.layerFrom(PgClient.makeClient({
161 url: Redacted.make(container.getConnectionUri()),
162 prepare: false
163 }))
164 )
165 })
166 ).pipe(Layer.provide(this.layer))
167
168 static layerMakeClientAcquireForStream = Layer.unwrap(
169 Effect.gen(function*() {
170 const container = yield* PgContainer
171 return yield* instrumentClient(
172 "layerMakeClientAcquireForStream",
173 container.getId(),
174 PgClient.layerFrom(PgClient.makeClient({
175 url: Redacted.make(container.getConnectionUri()),
176 applicationName: "side-default",
177 acquireForStream: true
178 }))
179 )
180 })
181 ).pipe(Layer.provide(this.layer))
182
183 static layerClientWithTransforms = Layer.unwrap(
184 Effect.gen(function*() {
185 const container = yield* PgContainer
186 return yield* instrumentClient(
187 "layerClientWithTransforms",
188 container.getId(),
189 PgClient.layer({
190 url: Redacted.make(container.getConnectionUri()),
191 transformResultNames: String.snakeToCamel,
192 transformQueryNames: String.camelToSnake
193 })
194 )
195 })
196 ).pipe(Layer.provide(this.layer))
197
198 static layerClientForListen = Layer.unwrap(
199 Effect.gen(function*() {
200 const container = yield* PgContainer
201 return yield* instrumentClient(
202 "layerClientForListen",
203 container.getId(),
204 PgClient.layer({
205 url: Redacted.make(container.getConnectionUri()),
206 maxConnections: 2,
207 multiplex: true
208 })
209 )
210 })
211 ).pipe(Layer.provide(this.layer))
212 }
213
214 // `it.effect` runs under the TestClock, so poll loops sleep in real time.
215 export const realSleep = Effect.promise(() => new Promise((resolve) => setTimeout(resolve, 10)))
216 const activeTimeoutMs = 10_000
217
218 export const waitUntilActive = Effect.fnUntraced(
219 function*(observer: PgConnection.PgConnection, pid: number, advisoryLock = false) {
220 const startedAt = Date.now()
221 while (Date.now() - startedAt < activeTimeoutMs) {
222 const active = yield* observer.query(
223 `SELECT count(*)::int4 AS active FROM pg_stat_activity
224 WHERE pid = $1 AND state = 'active'
225 AND (NOT $2::boolean OR (wait_event_type = 'Lock' AND wait_event = 'advisory'))`,
226 [pid, advisoryLock]
227 )
228 if (active.rows[0].active === 1) return
229 yield* realSleep
230 }
231 return yield* Effect.fail(
232 new Error(
233 `PostgreSQL backend ${pid} did not become ${
234 advisoryLock ? "blocked on an advisory lock" : "active"
235 } within 10 seconds`
236 )
237 )
238 }
239 )
240
241 // Parse can report active before Execute starts. Cancellation needs the query to be waiting on the lock.
242 export const waitUntilAdvisoryLock = (observer: PgConnection.PgConnection, pid: number) =>
243 waitUntilActive(observer, pid, true)