start phase 1

This commit is contained in:
Julius Marminge authored and Julius Marminge committed 2026-02-26 13:20:36 -08:00
1 parent 6954d1b91c
commit fefea0bdf6
16 files changed
+625 -4904

No files matched your search

@@ -22,7 +22,7 @@ import {
makeOrchestrationIntegrationHarness,
type OrchestrationIntegrationHarness,
} from "./OrchestrationEngineHarness.integration.ts";
import { checkpointRefForThreadTurn } from "../src/checkpointing/Refs.ts";
import { checkpointRefForThreadTurn } from "../src/checkpointing/Utils.ts";
const asMessageId = (value: string): MessageId => MessageId.makeUnsafe(value);
const asProjectId = (value: string): ProjectId => ProjectId.makeUnsafe(value);
@@ -9,7 +9,7 @@ import { Effect, Layer } from "effect";
import { describe, expect, it } from "vitest";
import { ProjectionSnapshotQuery } from "../../orchestration/Services/ProjectionSnapshotQuery.ts";
import { checkpointRefForThreadTurn } from "../Refs.ts";
import { checkpointRefForThreadTurn } from "../Utils.ts";
import { CheckpointDiffQueryLive } from "./CheckpointDiffQuery.ts";
import { CheckpointStore, type CheckpointStoreShape } from "../Services/CheckpointStore.ts";
import { CheckpointDiffQuery } from "../Services/CheckpointDiffQuery.ts";
@@ -9,35 +9,12 @@ import { Effect, Layer, Schema } from "effect";
import { ProjectionSnapshotQuery } from "../../orchestration/Services/ProjectionSnapshotQuery.ts";
import { CheckpointInvariantError, CheckpointUnavailableError } from "../Errors.ts";
import { checkpointRefForThreadTurn } from "../Refs.ts";
import { checkpointRefForThreadTurn, resolveThreadWorkspaceCwd } from "../Utils.ts";
import { CheckpointStore } from "../Services/CheckpointStore.ts";
import { CheckpointDiffQuery, type CheckpointDiffQueryShape } from "../Services/CheckpointDiffQuery.ts";
function trimToNonEmpty(value: string | null | undefined): string | undefined {
const normalized = value?.trim();
return normalized && normalized.length > 0 ? normalized : undefined;
}
function resolveThreadWorkspaceCwd(input: {
readonly thread: {
readonly projectId: ProjectId;
readonly worktreePath: string | null;
};
readonly projects: ReadonlyArray<{
readonly id: ProjectId;
readonly workspaceRoot: string;
}>;
}): string | undefined {
const worktreeCwd = trimToNonEmpty(input.thread.worktreePath);
if (worktreeCwd) {
return worktreeCwd;
}
const workspaceRoot = input.projects.find(
(project) => project.id === input.thread.projectId,
)?.workspaceRoot;
return trimToNonEmpty(workspaceRoot);
}
import {
CheckpointDiffQuery,
type CheckpointDiffQueryShape,
} from "../Services/CheckpointDiffQuery.ts";
const isTurnDiffResult = Schema.is(OrchestrationGetTurnDiffResult);
-10
View File
@@ -1,10 +0,0 @@
import { Encoding } from "effect";
import { CheckpointRef, type ThreadId } from "@t3tools/contracts";
export const CHECKPOINT_REFS_PREFIX = "refs/t3/checkpoints";
export function checkpointRefForThreadTurn(threadId: ThreadId, turnCount: number): CheckpointRef {
return CheckpointRef.makeUnsafe(
`${CHECKPOINT_REFS_PREFIX}/${Encoding.encodeBase64Url(threadId)}/turn/${turnCount}`,
);
}
+36
View File
@@ -0,0 +1,36 @@
import { Encoding } from "effect";
import { CheckpointRef, ProjectId, type ThreadId } from "@t3tools/contracts";
export const CHECKPOINT_REFS_PREFIX = "refs/t3/checkpoints";
export function checkpointRefForThreadTurn(threadId: ThreadId, turnCount: number): CheckpointRef {
return CheckpointRef.makeUnsafe(
`${CHECKPOINT_REFS_PREFIX}/${Encoding.encodeBase64Url(threadId)}/turn/${turnCount}`,
);
}
function trimToNonEmpty(value: string | null | undefined): string | undefined {
const normalized = value?.trim();
return normalized && normalized.length > 0 ? normalized : undefined;
}
export function resolveThreadWorkspaceCwd(input: {
readonly thread: {
readonly projectId: ProjectId;
readonly worktreePath: string | null;
};
readonly projects: ReadonlyArray<{
readonly id: ProjectId;
readonly workspaceRoot: string;
}>;
}): string | undefined {
const worktreeCwd = trimToNonEmpty(input.thread.worktreePath);
if (worktreeCwd) {
return worktreeCwd;
}
const workspaceRoot = input.projects.find(
(project) => project.id === input.thread.projectId,
)?.workspaceRoot;
return trimToNonEmpty(workspaceRoot);
}
@@ -35,7 +35,7 @@ import {
ProviderService,
type ProviderServiceShape,
} from "../../provider/Services/ProviderService.ts";
import { checkpointRefForThreadTurn } from "../../checkpointing/Refs.ts";
import { checkpointRefForThreadTurn } from "../../checkpointing/Utils.ts";
const asProjectId = (value: string): ProjectId => ProjectId.makeUnsafe(value);
const asSessionId = (value: string): ProviderSessionId => ProviderSessionId.makeUnsafe(value);
@@ -334,7 +334,10 @@ describe("CheckpointReactor", () => {
threadId: ProviderThreadId.makeUnsafe("provider-thread-1"),
turnId: asTurnId("turn-1"),
});
await waitForGitRefExists(harness.cwd, checkpointRefForThreadTurn(ThreadId.makeUnsafe("thread-1"), 0));
await waitForGitRefExists(
harness.cwd,
checkpointRefForThreadTurn(ThreadId.makeUnsafe("thread-1"), 0),
);
fs.writeFileSync(path.join(harness.cwd, "README.md"), "v2\n", "utf8");
harness.provider.emit({
@@ -354,12 +357,12 @@ describe("CheckpointReactor", () => {
(entry) => entry.latestTurnId === "turn-1" && entry.checkpoints.length === 1,
);
expect(thread.checkpoints[0]?.checkpointTurnCount).toBe(1);
expect(gitRefExists(harness.cwd, checkpointRefForThreadTurn(ThreadId.makeUnsafe("thread-1"), 0))).toBe(
true,
);
expect(gitRefExists(harness.cwd, checkpointRefForThreadTurn(ThreadId.makeUnsafe("thread-1"), 1))).toBe(
true,
);
expect(
gitRefExists(harness.cwd, checkpointRefForThreadTurn(ThreadId.makeUnsafe("thread-1"), 0)),
).toBe(true);
expect(
gitRefExists(harness.cwd, checkpointRefForThreadTurn(ThreadId.makeUnsafe("thread-1"), 1)),
).toBe(true);
expect(
gitShowFileAtRef(
harness.cwd,
@@ -398,7 +401,10 @@ describe("CheckpointReactor", () => {
}),
);
await waitForGitRefExists(harness.cwd, checkpointRefForThreadTurn(ThreadId.makeUnsafe("thread-1"), 0));
await waitForGitRefExists(
harness.cwd,
checkpointRefForThreadTurn(ThreadId.makeUnsafe("thread-1"), 0),
);
expect(
gitShowFileAtRef(
harness.cwd,
@@ -448,9 +454,9 @@ describe("CheckpointReactor", () => {
});
await waitForEvent(harness.engine, (event) => event.type === "thread.turn-diff-completed");
expect(gitRefExists(harness.cwd, checkpointRefForThreadTurn(ThreadId.makeUnsafe("thread-1"), 1))).toBe(
true,
);
expect(
gitRefExists(harness.cwd, checkpointRefForThreadTurn(ThreadId.makeUnsafe("thread-1"), 1)),
).toBe(true);
expect(
gitShowFileAtRef(
harness.cwd,
@@ -556,7 +562,10 @@ describe("CheckpointReactor", () => {
turnId: asTurnId("turn-after-runtime-failure"),
});
await waitForGitRefExists(harness.cwd, checkpointRefForThreadTurn(ThreadId.makeUnsafe("thread-1"), 0));
await waitForGitRefExists(
harness.cwd,
checkpointRefForThreadTurn(ThreadId.makeUnsafe("thread-1"), 0),
);
expect(
gitRefExists(harness.cwd, checkpointRefForThreadTurn(ThreadId.makeUnsafe("thread-1"), 0)),
).toBe(true);
@@ -636,9 +645,9 @@ describe("CheckpointReactor", () => {
numTurns: 1,
});
expect(fs.readFileSync(path.join(harness.cwd, "README.md"), "utf8")).toBe("v2\n");
expect(gitRefExists(harness.cwd, checkpointRefForThreadTurn(ThreadId.makeUnsafe("thread-1"), 2))).toBe(
false,
);
expect(
gitRefExists(harness.cwd, checkpointRefForThreadTurn(ThreadId.makeUnsafe("thread-1"), 2)),
).toBe(false);
});
it("processes consecutive revert requests with deterministic rollback sequencing", async () => {
@@ -5,14 +5,16 @@ import {
ProviderSessionId,
ThreadId,
TurnId,
type ProjectId,
type OrchestrationEvent,
type ProviderRuntimeEvent,
} from "@t3tools/contracts";
import { Cause, Effect, Layer, Option, Queue, Stream } from "effect";
import { parseTurnDiffFilesFromUnifiedDiff } from "../../checkpointing/Diffs.ts";
import { checkpointRefForThreadTurn } from "../../checkpointing/Refs.ts";
import {
checkpointRefForThreadTurn,
resolveThreadWorkspaceCwd,
} from "../../checkpointing/Utils.ts";
import { CheckpointStore } from "../../checkpointing/Services/CheckpointStore.ts";
import { ProviderService } from "../../provider/Services/ProviderService.ts";
import { CheckpointReactor, type CheckpointReactorShape } from "../Services/CheckpointReactor.ts";
@@ -40,27 +42,6 @@ function trimToNonEmpty(value: string | null | undefined): string | undefined {
return normalized && normalized.length > 0 ? normalized : undefined;
}
function resolveThreadWorkspaceCwd(input: {
readonly thread: {
readonly projectId: ProjectId;
readonly worktreePath: string | null;
};
readonly projects: ReadonlyArray<{
readonly id: ProjectId;
readonly workspaceRoot: string;
}>;
}): string | undefined {
const worktreeCwd = trimToNonEmpty(input.thread.worktreePath);
if (worktreeCwd) {
return worktreeCwd;
}
const workspaceRoot = input.projects.find(
(project) => project.id === input.thread.projectId,
)?.workspaceRoot;
return trimToNonEmpty(workspaceRoot);
}
function checkpointStatusFromRuntime(status: string | undefined): "ready" | "missing" | "error" {
switch (status) {
case "failed":
@@ -26,7 +26,7 @@ function toTurnId(value: string | undefined): TurnId | undefined {
}
function truncateDetail(value: string, limit = 180): string {
return value.length > limit ? `${value.slice(0, limit - 1)}...` : value;
return value.length > limit ? `${value.slice(0, limit - 3)}...` : value;
}
function runtimeEventToActivities(
@@ -130,7 +130,6 @@ const make = Effect.gen(function* () {
const providerService = yield* ProviderService;
const turnMessageIdsByTurnKey = new Map<string, Set<MessageId>>();
const latestMessageIdByTurnKey = new Map<string, MessageId>();
const rememberAssistantMessageId = (
sessionId: ProviderSessionId,
@@ -144,7 +143,6 @@ const make = Effect.gen(function* () {
} else {
turnMessageIdsByTurnKey.set(key, new Set([messageId]));
}
latestMessageIdByTurnKey.set(key, messageId);
};
const getAssistantMessageIdsForTurn = (sessionId: ProviderSessionId, turnId: TurnId) => {
@@ -162,11 +160,6 @@ const make = Effect.gen(function* () {
turnMessageIdsByTurnKey.delete(key);
}
}
for (const key of latestMessageIdByTurnKey.keys()) {
if (key.startsWith(prefix)) {
latestMessageIdByTurnKey.delete(key);
}
}
};
const processEvent = (event: ProviderRuntimeEvent) =>
+3 -2
View File
@@ -72,7 +72,7 @@ const isServerNotRunningError = (error: unknown): boolean => {
};
function rejectUpgrade(socket: Duplex, statusCode: number, message: string): void {
socket.write(
socket.end(
`HTTP/1.1 ${statusCode} ${statusCode === 401 ? "Unauthorized" : "Bad Request"}\r\n` +
"Connection: close\r\n" +
"Content-Type: text/plain\r\n" +
@@ -80,7 +80,6 @@ function rejectUpgrade(socket: Duplex, statusCode: number, message: string): voi
"\r\n" +
message,
);
socket.destroy();
}
function parseBooleanEnv(value: string | undefined): boolean | undefined {
@@ -506,6 +505,8 @@ export const createServer = Effect.fn(function* (options: ServerOptions = {}): E
});
httpServer.on("upgrade", (request, socket, head) => {
socket.on("error", () => {}); // Prevent unhandled `EPIPE`/`ECONNRESET` from crashing the process if the client disconnects mid-handshake
if (authToken) {
let providedToken: string | null = null;
try {
+1 -4
View File
@@ -53,10 +53,7 @@ export class WsTransport {
throw new Error("Request method is required");
}
const id = String(this.nextId++);
const body =
params !== undefined
? { _tag: method, ...(params as Record<string, unknown>) }
: { _tag: method };
const body = params != null ? { ...params, _tag: method } : { _tag: method };
const message: WsRequestEnvelope = { id, body };
return new Promise<T>((resolve, reject) => {