mirror of
https://github.com/hanzoai/dev-desktop.git
synced 2026-11-22 00:14:30 +00:00
169 lines
7.0 KiB
TypeScript
169 lines
7.0 KiB
TypeScript
import { CheckpointScopeId, ProjectId, RunId, ThreadId } from "@hanzodev/contracts";
|
|
import * as Context from "effect/Context";
|
|
import * as Effect from "effect/Effect";
|
|
import * as FiberMap from "effect/FiberMap";
|
|
import * as FiberSet from "effect/FiberSet";
|
|
import * as Layer from "effect/Layer";
|
|
import * as Option from "effect/Option";
|
|
import * as Schema from "effect/Schema";
|
|
|
|
import * as ProjectEnrichment from "../project/ProjectEnrichmentService.ts";
|
|
import * as ProjectService from "../project/ProjectService.ts";
|
|
import * as PullRequestService from "../pullRequest/PullRequestService.ts";
|
|
import * as VcsStatusBroadcaster from "../vcs/VcsStatusBroadcaster.ts";
|
|
import * as WorkspaceEntries from "../workspace/WorkspaceEntries.ts";
|
|
import * as CheckpointCapture from "./CheckpointCaptureService.ts";
|
|
import * as ProjectionStore from "./ProjectionStore.ts";
|
|
|
|
export class RunFinalizationError extends Schema.TaggedError<RunFinalizationError>()(
|
|
"RunFinalizationError",
|
|
{
|
|
threadId: ThreadId,
|
|
runId: RunId,
|
|
scopeId: CheckpointScopeId,
|
|
operation: Schema.Literals(["capture-checkpoint", "refresh-workspace"]),
|
|
cause: Schema.Defect(),
|
|
},
|
|
) {}
|
|
|
|
export class RunFinalizationRefreshError extends Schema.TaggedError<RunFinalizationRefreshError>()(
|
|
"RunFinalizationRefreshError",
|
|
{ cwd: Schema.String, cause: Schema.Defect() },
|
|
) {}
|
|
|
|
export class RunFinalizationObserver extends Context.Reference<{
|
|
readonly refreshAfterTurn: (projectId: ProjectId) => Effect.Effect<void>;
|
|
readonly refresh: (input: {
|
|
readonly cwd: string;
|
|
readonly threadId: ThreadId;
|
|
readonly runId: RunId;
|
|
}) => Effect.Effect<void, RunFinalizationRefreshError>;
|
|
}>("hanzo/orchestration-v2/RunFinalizationObserver", {
|
|
defaultValue: () => ({ refresh: () => Effect.void, refreshAfterTurn: () => Effect.void }),
|
|
}) {}
|
|
|
|
export class RunFinalizationService extends Context.Service<
|
|
RunFinalizationService,
|
|
{
|
|
readonly finalize: (input: {
|
|
readonly threadId: ThreadId;
|
|
readonly runId: RunId;
|
|
readonly scopeId: CheckpointScopeId;
|
|
}) => Effect.Effect<void, RunFinalizationError>;
|
|
}
|
|
>()("hanzo/orchestration-v2/RunFinalizationService") {}
|
|
|
|
const make = Effect.gen(function* () {
|
|
const checkpointCapture = yield* CheckpointCapture.CheckpointCaptureServiceV2;
|
|
const projections = yield* ProjectionStore.ProjectionStoreV2;
|
|
const observer = yield* RunFinalizationObserver;
|
|
const refreshes = yield* FiberSet.make<void>();
|
|
|
|
const finalize: RunFinalizationService["Service"]["finalize"] = Effect.fn(
|
|
"RunFinalizationService.finalize",
|
|
)(function* (input) {
|
|
yield* checkpointCapture
|
|
.execute(input)
|
|
.pipe(
|
|
Effect.mapError(
|
|
(cause) => new RunFinalizationError({ ...input, operation: "capture-checkpoint", cause }),
|
|
),
|
|
);
|
|
const projection = yield* projections
|
|
.getCheckpointContext(input.threadId)
|
|
.pipe(
|
|
Effect.mapError(
|
|
(cause) => new RunFinalizationError({ ...input, operation: "refresh-workspace", cause }),
|
|
),
|
|
);
|
|
const cwd = projection.checkpointScopes.find((scope) => scope.id === input.scopeId)?.cwd;
|
|
if (cwd !== undefined) {
|
|
// The refresh only updates the display, so the thread's next turn must not wait for it.
|
|
// Never replaced by a newer one: back-to-back turns would interrupt each before it publishes.
|
|
yield* observer.refresh({ cwd, threadId: input.threadId, runId: input.runId }).pipe(
|
|
Effect.catchTags({
|
|
RunFinalizationRefreshError: (cause) =>
|
|
Effect.logWarning("Failed to refresh workspace after checkpoint capture", {
|
|
...input,
|
|
cause,
|
|
}),
|
|
}),
|
|
FiberSet.run(refreshes),
|
|
);
|
|
}
|
|
});
|
|
return RunFinalizationService.of({ finalize });
|
|
});
|
|
|
|
export const layer = Layer.effect(RunFinalizationService, make);
|
|
|
|
export const layerObserver = Layer.effect(
|
|
RunFinalizationObserver,
|
|
Effect.gen(function* () {
|
|
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
|
|
const vcsStatus = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
|
|
const projections = yield* ProjectionStore.ProjectionStoreV2;
|
|
const pullRequests = yield* PullRequestService.PullRequestService;
|
|
const projects = yield* ProjectService.ProjectService;
|
|
const enrichment = yield* ProjectEnrichment.ProjectEnrichmentService;
|
|
const pullRequestLookups = yield* FiberMap.make<string>();
|
|
const identityRefreshes = yield* FiberMap.make<ProjectId>();
|
|
// A turn can add the first remote (`git remote add`, `gh repo create --push`), which the
|
|
// enrichment and resolver caches would otherwise keep reporting as missing for a minute.
|
|
const refreshMissingRepositoryIdentity = Effect.fn(
|
|
"RunFinalizationService.refreshMissingRepositoryIdentity",
|
|
)(
|
|
function* (projectId: ProjectId) {
|
|
const project = yield* projects.getById(projectId);
|
|
if (Option.isNone(project)) return;
|
|
const { workspaceRoot } = project.value;
|
|
if ((yield* enrichment.readRepositoryIdentity(workspaceRoot)) !== null) return;
|
|
yield* enrichment.refreshRepositoryIdentity(workspaceRoot);
|
|
},
|
|
(effect, projectId) =>
|
|
effect.pipe(
|
|
Effect.catch((error) =>
|
|
Effect.logWarning("failed to refresh repository identity after run completion", {
|
|
projectId,
|
|
detail: error.message,
|
|
}),
|
|
),
|
|
),
|
|
);
|
|
return {
|
|
refreshAfterTurn: pullRequests.refreshAfterTurn,
|
|
refresh: Effect.fn("RunFinalizationService.refresh")(
|
|
function* ({ cwd, threadId, runId }) {
|
|
const [, local] = yield* Effect.all(
|
|
[workspaceEntries.refresh(cwd), vcsStatus.refreshLocalStatus(cwd)],
|
|
{ concurrency: "unbounded" },
|
|
);
|
|
const thread = yield* projections.getThreadShell(threadId);
|
|
if (!thread) return;
|
|
// Forge discovery probes can take seconds; like the PR lookup, the next turn must not wait.
|
|
yield* refreshMissingRepositoryIdentity(thread.projectId).pipe(
|
|
FiberMap.run(identityRefreshes, thread.projectId),
|
|
);
|
|
if (local.refName === null || local.isDefaultRef) return;
|
|
if (thread.branch !== local.refName) return;
|
|
if (thread.activeRunId !== null && thread.activeRunId !== runId) return;
|
|
// The forge CLI can take minutes, so the next turn must not wait for it. A newer
|
|
// turn's lookup replaces a running one instead of queueing behind the cwd's lock.
|
|
yield* vcsStatus.refreshPullRequestStatus(cwd).pipe(
|
|
Effect.catch((error) =>
|
|
Effect.logWarning("failed to refresh pull request status after run completion", {
|
|
threadId,
|
|
cwd,
|
|
detail: error.message,
|
|
}),
|
|
),
|
|
FiberMap.run(pullRequestLookups, cwd),
|
|
);
|
|
},
|
|
(effect, { cwd }) =>
|
|
effect.pipe(Effect.mapError((cause) => new RunFinalizationRefreshError({ cwd, cause }))),
|
|
),
|
|
};
|
|
}),
|
|
);
|