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, 03:37 UTC
| 1 | import { CheckpointScopeId, ProjectId, RunId, ThreadId } from "@t3tools/contracts"; |
| 2 | import * as Context from "effect/Context"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FiberMap from "effect/FiberMap"; |
| 5 | import * as FiberSet from "effect/FiberSet"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Option from "effect/Option"; |
| 8 | import * as Schema from "effect/Schema"; |
| 9 | |
| 10 | import * as ProjectEnrichment from "../project/ProjectEnrichmentService.ts"; |
| 11 | import * as ProjectService from "../project/ProjectService.ts"; |
| 12 | import * as PullRequestService from "../pullRequest/PullRequestService.ts"; |
| 13 | import * as VcsStatusBroadcaster from "../vcs/VcsStatusBroadcaster.ts"; |
| 14 | import * as WorkspaceEntries from "../workspace/WorkspaceEntries.ts"; |
| 15 | import * as CheckpointCapture from "./CheckpointCaptureService.ts"; |
| 16 | import * as ProjectionStore from "./ProjectionStore.ts"; |
| 17 | |
| 18 | export class RunFinalizationError extends Schema.TaggedError<RunFinalizationError>()( |
| 19 | "RunFinalizationError", |
| 20 | { |
| 21 | threadId: ThreadId, |
| 22 | runId: RunId, |
| 23 | scopeId: CheckpointScopeId, |
| 24 | operation: Schema.Literals(["capture-checkpoint", "refresh-workspace"]), |
| 25 | cause: Schema.Defect(), |
| 26 | }, |
| 27 | ) {} |
| 28 | |
| 29 | export class RunFinalizationRefreshError extends Schema.TaggedError<RunFinalizationRefreshError>()( |
| 30 | "RunFinalizationRefreshError", |
| 31 | { cwd: Schema.String, cause: Schema.Defect() }, |
| 32 | ) {} |
| 33 | |
| 34 | export class RunFinalizationObserver extends Context.Reference<{ |
| 35 | readonly refreshAfterTurn: (projectId: ProjectId) => Effect.Effect<void>; |
| 36 | readonly refresh: (input: { |
| 37 | readonly cwd: string; |
| 38 | readonly threadId: ThreadId; |
| 39 | readonly runId: RunId; |
| 40 | }) => Effect.Effect<void, RunFinalizationRefreshError>; |
| 41 | }>("t3/orchestration-v2/RunFinalizationObserver", { |
| 42 | defaultValue: () => ({ refresh: () => Effect.void, refreshAfterTurn: () => Effect.void }), |
| 43 | }) {} |
| 44 | |
| 45 | export class RunFinalizationService extends Context.Service< |
| 46 | RunFinalizationService, |
| 47 | { |
| 48 | readonly finalize: (input: { |
| 49 | readonly threadId: ThreadId; |
| 50 | readonly runId: RunId; |
| 51 | readonly scopeId: CheckpointScopeId; |
| 52 | }) => Effect.Effect<void, RunFinalizationError>; |
| 53 | } |
| 54 | >()("t3/orchestration-v2/RunFinalizationService") {} |
| 55 | |
| 56 | const make = Effect.gen(function* () { |
| 57 | const checkpointCapture = yield* CheckpointCapture.CheckpointCaptureServiceV2; |
| 58 | const projections = yield* ProjectionStore.ProjectionStoreV2; |
| 59 | const observer = yield* RunFinalizationObserver; |
| 60 | const refreshes = yield* FiberSet.make<void>(); |
| 61 | |
| 62 | const finalize: RunFinalizationService["Service"]["finalize"] = Effect.fn( |
| 63 | "RunFinalizationService.finalize", |
| 64 | )(function* (input) { |
| 65 | yield* checkpointCapture |
| 66 | .execute(input) |
| 67 | .pipe( |
| 68 | Effect.mapError( |
| 69 | (cause) => new RunFinalizationError({ ...input, operation: "capture-checkpoint", cause }), |
| 70 | ), |
| 71 | ); |
| 72 | const projection = yield* projections |
| 73 | .getCheckpointContext(input.threadId) |
| 74 | .pipe( |
| 75 | Effect.mapError( |
| 76 | (cause) => new RunFinalizationError({ ...input, operation: "refresh-workspace", cause }), |
| 77 | ), |
| 78 | ); |
| 79 | const cwd = projection.checkpointScopes.find((scope) => scope.id === input.scopeId)?.cwd; |
| 80 | if (cwd !== undefined) { |
| 81 | // The refresh only updates the display, so the thread's next turn must not wait for it. |
| 82 | // Never replaced by a newer one: back-to-back turns would interrupt each before it publishes. |
| 83 | yield* observer.refresh({ cwd, threadId: input.threadId, runId: input.runId }).pipe( |
| 84 | Effect.catchTags({ |
| 85 | RunFinalizationRefreshError: (cause) => |
| 86 | Effect.logWarning("Failed to refresh workspace after checkpoint capture", { |
| 87 | ...input, |
| 88 | cause, |
| 89 | }), |
| 90 | }), |
| 91 | FiberSet.run(refreshes), |
| 92 | ); |
| 93 | } |
| 94 | }); |
| 95 | return RunFinalizationService.of({ finalize }); |
| 96 | }); |
| 97 | |
| 98 | export const layer = Layer.effect(RunFinalizationService, make); |
| 99 | |
| 100 | export const layerObserver = Layer.effect( |
| 101 | RunFinalizationObserver, |
| 102 | Effect.gen(function* () { |
| 103 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 104 | const vcsStatus = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 105 | const projections = yield* ProjectionStore.ProjectionStoreV2; |
| 106 | const pullRequests = yield* PullRequestService.PullRequestService; |
| 107 | const projects = yield* ProjectService.ProjectService; |
| 108 | const enrichment = yield* ProjectEnrichment.ProjectEnrichmentService; |
| 109 | const pullRequestLookups = yield* FiberMap.make<string>(); |
| 110 | const identityRefreshes = yield* FiberMap.make<ProjectId>(); |
| 111 | // A turn can add the first remote (`git remote add`, `gh repo create --push`), which the |
| 112 | // enrichment and resolver caches would otherwise keep reporting as missing for a minute. |
| 113 | const refreshMissingRepositoryIdentity = Effect.fn( |
| 114 | "RunFinalizationService.refreshMissingRepositoryIdentity", |
| 115 | )( |
| 116 | function* (projectId: ProjectId) { |
| 117 | const project = yield* projects.getById(projectId); |
| 118 | if (Option.isNone(project)) return; |
| 119 | const { workspaceRoot } = project.value; |
| 120 | if ((yield* enrichment.readRepositoryIdentity(workspaceRoot)) !== null) return; |
| 121 | yield* enrichment.refreshRepositoryIdentity(workspaceRoot); |
| 122 | }, |
| 123 | (effect, projectId) => |
| 124 | effect.pipe( |
| 125 | Effect.catch((error) => |
| 126 | Effect.logWarning("failed to refresh repository identity after run completion", { |
| 127 | projectId, |
| 128 | detail: error.message, |
| 129 | }), |
| 130 | ), |
| 131 | ), |
| 132 | ); |
| 133 | return { |
| 134 | refreshAfterTurn: pullRequests.refreshAfterTurn, |
| 135 | refresh: Effect.fn("RunFinalizationService.refresh")( |
| 136 | function* ({ cwd, threadId, runId }) { |
| 137 | const [, local] = yield* Effect.all( |
| 138 | [workspaceEntries.refresh(cwd), vcsStatus.refreshLocalStatus(cwd)], |
| 139 | { concurrency: "unbounded" }, |
| 140 | ); |
| 141 | const thread = yield* projections.getThreadShell(threadId); |
| 142 | if (!thread) return; |
| 143 | // Forge discovery probes can take seconds; like the PR lookup, the next turn must not wait. |
| 144 | yield* refreshMissingRepositoryIdentity(thread.projectId).pipe( |
| 145 | FiberMap.run(identityRefreshes, thread.projectId), |
| 146 | ); |
| 147 | if (local.refName === null || local.isDefaultRef) return; |
| 148 | if (thread.branch !== local.refName) return; |
| 149 | if (thread.activeRunId !== null && thread.activeRunId !== runId) return; |
| 150 | // The forge CLI can take minutes, so the next turn must not wait for it. A newer |
| 151 | // turn's lookup replaces a running one instead of queueing behind the cwd's lock. |
| 152 | yield* vcsStatus.refreshPullRequestStatus(cwd).pipe( |
| 153 | Effect.catch((error) => |
| 154 | Effect.logWarning("failed to refresh pull request status after run completion", { |
| 155 | threadId, |
| 156 | cwd, |
| 157 | detail: error.message, |
| 158 | }), |
| 159 | ), |
| 160 | FiberMap.run(pullRequestLookups, cwd), |
| 161 | ); |
| 162 | }, |
| 163 | (effect, { cwd }) => |
| 164 | effect.pipe(Effect.mapError((cause) => new RunFinalizationRefreshError({ cwd, cause }))), |
| 165 | ), |
| 166 | }; |
| 167 | }), |
| 168 | ); |