wip
This commit is contained in:
1 parent
fcc94b3b2f
commit
20668e49a9
20 files changed
+710
-331
No files matched your search
@@ -18,6 +18,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@effect/platform-node": "4.0.0-beta.10",
|
||||
"@pierre/diffs": "^1.1.0-beta.16",
|
||||
"effect": "4.0.0-beta.10",
|
||||
"node-pty": "^1.1.0",
|
||||
"open": "^10.1.0",
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { parseTurnDiffFilesFromUnifiedDiff } from "./Diffs.ts";
|
||||
|
||||
describe("parseTurnDiffFilesFromUnifiedDiff", () => {
|
||||
it("returns empty list for empty diff", () => {
|
||||
expect(parseTurnDiffFilesFromUnifiedDiff("")).toEqual([]);
|
||||
});
|
||||
|
||||
it("parses per-file additions and deletions", () => {
|
||||
const diff = [
|
||||
"diff --git a/a.txt b/a.txt",
|
||||
"index 1111111..2222222 100644",
|
||||
"--- a/a.txt",
|
||||
"+++ b/a.txt",
|
||||
"@@ -1,2 +1,3 @@",
|
||||
" one",
|
||||
"-two",
|
||||
"+two updated",
|
||||
"+three",
|
||||
"diff --git a/src/b.ts b/src/b.ts",
|
||||
"index 3333333..4444444 100644",
|
||||
"--- a/src/b.ts",
|
||||
"+++ b/src/b.ts",
|
||||
"@@ -3,2 +3,0 @@",
|
||||
"-old",
|
||||
"-stale",
|
||||
"",
|
||||
].join("\n");
|
||||
|
||||
expect(parseTurnDiffFilesFromUnifiedDiff(diff)).toEqual([
|
||||
{ path: "a.txt", additions: 2, deletions: 1 },
|
||||
{ path: "src/b.ts", additions: 0, deletions: 2 },
|
||||
]);
|
||||
});
|
||||
|
||||
it("parses rename-only diffs with zero line changes", () => {
|
||||
const diff = [
|
||||
"diff --git a/src/old.ts b/src/new.ts",
|
||||
"similarity index 100%",
|
||||
"rename from src/old.ts",
|
||||
"rename to src/new.ts",
|
||||
"",
|
||||
].join("\n");
|
||||
|
||||
expect(parseTurnDiffFilesFromUnifiedDiff(diff)).toEqual([
|
||||
{ path: "src/new.ts", additions: 0, deletions: 0 },
|
||||
]);
|
||||
});
|
||||
|
||||
it("normalizes CRLF input before parsing", () => {
|
||||
const diff = [
|
||||
"diff --git a/a.txt b/a.txt",
|
||||
"index 1111111..2222222 100644",
|
||||
"--- a/a.txt",
|
||||
"+++ b/a.txt",
|
||||
"@@ -1 +1,2 @@",
|
||||
"-one",
|
||||
"+one updated",
|
||||
"+two",
|
||||
"",
|
||||
].join("\r\n");
|
||||
|
||||
expect(parseTurnDiffFilesFromUnifiedDiff(diff)).toEqual([
|
||||
{ path: "a.txt", additions: 2, deletions: 1 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
import { parsePatchFiles } from "@pierre/diffs";
|
||||
|
||||
export interface TurnDiffFileSummary {
|
||||
readonly path: string;
|
||||
readonly additions: number;
|
||||
readonly deletions: number;
|
||||
}
|
||||
|
||||
export function parseTurnDiffFilesFromUnifiedDiff(diff: string): ReadonlyArray<TurnDiffFileSummary> {
|
||||
const normalized = diff.replace(/\r\n/g, "\n").trim();
|
||||
if (normalized.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const parsedPatches = parsePatchFiles(normalized);
|
||||
const files = parsedPatches.flatMap((patch) =>
|
||||
patch.files.map((file) => ({
|
||||
path: file.name,
|
||||
additions: file.hunks.reduce((total, hunk) => total + hunk.additionLines, 0),
|
||||
deletions: file.hunks.reduce((total, hunk) => total + hunk.deletionLines, 0),
|
||||
})),
|
||||
);
|
||||
|
||||
return files.toSorted((left, right) => left.path.localeCompare(right.path));
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { OrchestrationEvent } from "@t3tools/contracts";
|
||||
import { Effect, Layer, ManagedRuntime } from "effect";
|
||||
import { Effect, Layer, ManagedRuntime, Queue, Stream } from "effect";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
@@ -65,33 +65,6 @@ describe("OrchestrationEngine", () => {
|
||||
await system.dispose();
|
||||
});
|
||||
|
||||
it("fans out read-model updates to subscribers", async () => {
|
||||
const system = await createOrchestrationSystem();
|
||||
const engine = system.engine;
|
||||
const updates: number[] = [];
|
||||
const unsubscribe = await system.run(
|
||||
engine.subscribeToReadModel((snapshot) => {
|
||||
updates.push(snapshot.sequence);
|
||||
}),
|
||||
);
|
||||
await system.run(
|
||||
engine.dispatch({
|
||||
type: "thread.create",
|
||||
commandId: "cmd-thread",
|
||||
threadId: "thread-2",
|
||||
projectId: "project-2",
|
||||
title: "fanout",
|
||||
model: "gpt-5-codex",
|
||||
branch: null,
|
||||
worktreePath: null,
|
||||
createdAt: new Date().toISOString(),
|
||||
}),
|
||||
);
|
||||
unsubscribe();
|
||||
expect(updates.length).toBeGreaterThan(0);
|
||||
await system.dispose();
|
||||
});
|
||||
|
||||
it("replays append-only events from sequence", async () => {
|
||||
const system = await createOrchestrationSystem();
|
||||
const engine = system.engine;
|
||||
@@ -123,6 +96,46 @@ describe("OrchestrationEngine", () => {
|
||||
await system.dispose();
|
||||
});
|
||||
|
||||
it("streams persisted domain events in order", async () => {
|
||||
const system = await createOrchestrationSystem();
|
||||
const engine = system.engine;
|
||||
const eventTypes: string[] = [];
|
||||
await system.run(
|
||||
Effect.gen(function* () {
|
||||
const eventQueue = yield* Queue.unbounded<OrchestrationEvent>();
|
||||
yield* Effect.forkScoped(
|
||||
Stream.take(engine.streamDomainEvents, 2).pipe(
|
||||
Stream.runForEach((event) => Queue.offer(eventQueue, event).pipe(Effect.asVoid)),
|
||||
),
|
||||
);
|
||||
yield* Effect.sleep("10 millis");
|
||||
const createdAt = new Date().toISOString();
|
||||
yield* engine.dispatch({
|
||||
type: "thread.create",
|
||||
commandId: "cmd-stream-domain-a",
|
||||
threadId: "thread-stream-domain",
|
||||
projectId: "project-stream-domain",
|
||||
title: "domain-stream",
|
||||
model: "gpt-5-codex",
|
||||
branch: null,
|
||||
worktreePath: null,
|
||||
createdAt,
|
||||
});
|
||||
yield* engine.dispatch({
|
||||
type: "thread.meta.update",
|
||||
commandId: "cmd-stream-domain-b",
|
||||
threadId: "thread-stream-domain",
|
||||
title: "domain-stream-updated",
|
||||
createdAt,
|
||||
});
|
||||
eventTypes.push((yield* Queue.take(eventQueue)).type);
|
||||
eventTypes.push((yield* Queue.take(eventQueue)).type);
|
||||
}).pipe(Effect.scoped),
|
||||
);
|
||||
expect(eventTypes).toEqual(["thread.created", "thread.meta-updated"]);
|
||||
await system.dispose();
|
||||
});
|
||||
|
||||
it("stores completed turn summaries even when no files changed", async () => {
|
||||
const system = await createOrchestrationSystem();
|
||||
const engine = system.engine;
|
||||
|
||||
@@ -4,12 +4,11 @@ import type {
|
||||
OrchestrationReadModel,
|
||||
} from "@t3tools/contracts";
|
||||
import { OrchestrationCommandSchema } from "@t3tools/contracts";
|
||||
import { Deferred, Effect, Layer, PubSub, Queue, Schema } from "effect";
|
||||
import { Deferred, Effect, Layer, PubSub, Queue, Schema, Stream } from "effect";
|
||||
|
||||
import { createLogger } from "../logger.ts";
|
||||
import { OrchestrationEventRepository } from "../persistence/Services/OrchestrationEvents.ts";
|
||||
import {
|
||||
toListenerCallbackError,
|
||||
toOrchestrationCommandDecodeError,
|
||||
toOrchestrationJsonParseError,
|
||||
type OrchestrationDispatchError,
|
||||
@@ -182,42 +181,14 @@ const makeOrchestrationEngine = Effect.gen(function* () {
|
||||
let readModel = createEmptyReadModel(new Date().toISOString());
|
||||
|
||||
const commandQueue = yield* Queue.unbounded<CommandEnvelope>();
|
||||
const readModelPubSub = yield* PubSub.unbounded<OrchestrationReadModel>();
|
||||
const eventPubSub = yield* PubSub.unbounded<OrchestrationEvent>();
|
||||
|
||||
const readModelListeners = new Set<(snapshot: OrchestrationReadModel) => void>();
|
||||
const domainEventListeners = new Set<(event: OrchestrationEvent) => void>();
|
||||
|
||||
const notifyReadModelListeners = (snapshot: OrchestrationReadModel) =>
|
||||
Effect.forEach(readModelListeners, (listener) =>
|
||||
Effect.try({
|
||||
try: () => listener(snapshot),
|
||||
catch: toListenerCallbackError("read-model"),
|
||||
}),
|
||||
).pipe(Effect.asVoid);
|
||||
|
||||
const notifyDomainEventListeners = (event: OrchestrationEvent) =>
|
||||
Effect.forEach(domainEventListeners, (listener) =>
|
||||
Effect.try({
|
||||
try: () => listener(event),
|
||||
catch: toListenerCallbackError("domain-event"),
|
||||
}),
|
||||
).pipe(Effect.asVoid);
|
||||
|
||||
const processEnvelope = (envelope: CommandEnvelope): Effect.Effect<void> =>
|
||||
Effect.gen(function* () {
|
||||
const eventBase = mapCommandToEvent(envelope.command);
|
||||
const savedEvent = yield* eventStore.append(eventBase);
|
||||
readModel = yield* reduceEvent(readModel, savedEvent);
|
||||
|
||||
const snapshot = readModel;
|
||||
yield* Effect.all([
|
||||
PubSub.publish(eventPubSub, savedEvent),
|
||||
PubSub.publish(readModelPubSub, snapshot),
|
||||
]);
|
||||
|
||||
yield* notifyDomainEventListeners(savedEvent);
|
||||
yield* notifyReadModelListeners(snapshot);
|
||||
yield* PubSub.publish(eventPubSub, savedEvent);
|
||||
|
||||
yield* Deferred.succeed(envelope.result, { sequence: savedEvent.sequence });
|
||||
}).pipe(Effect.catch((error) => Deferred.fail(envelope.result, error).pipe(Effect.asVoid)));
|
||||
@@ -269,29 +240,15 @@ const makeOrchestrationEngine = Effect.gen(function* () {
|
||||
return yield* dispatch(decoded);
|
||||
});
|
||||
|
||||
const subscribeToReadModel: OrchestrationEngineShape["subscribeToReadModel"] = (callback) =>
|
||||
Effect.sync(() => {
|
||||
readModelListeners.add(callback);
|
||||
return () => {
|
||||
readModelListeners.delete(callback);
|
||||
};
|
||||
});
|
||||
|
||||
const subscribeToDomainEvents: OrchestrationEngineShape["subscribeToDomainEvents"] = (callback) =>
|
||||
Effect.sync(() => {
|
||||
domainEventListeners.add(callback);
|
||||
return () => {
|
||||
domainEventListeners.delete(callback);
|
||||
};
|
||||
});
|
||||
const streamDomainEvents: OrchestrationEngineShape["streamDomainEvents"] =
|
||||
Stream.fromPubSub(eventPubSub);
|
||||
|
||||
return {
|
||||
getSnapshot,
|
||||
replayEvents,
|
||||
dispatchUnknown,
|
||||
dispatch,
|
||||
subscribeToReadModel,
|
||||
subscribeToDomainEvents,
|
||||
streamDomainEvents,
|
||||
} satisfies OrchestrationEngineShape;
|
||||
});
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ import type {
|
||||
OrchestrationReadModel,
|
||||
} from "@t3tools/contracts";
|
||||
import { ServiceMap } from "effect";
|
||||
import type { Effect } from "effect";
|
||||
import type { Effect, Stream } from "effect";
|
||||
|
||||
import type { OrchestrationDispatchError, OrchestrationEngineError } from "./Errors.ts";
|
||||
import type { OrchestrationEventRepositoryError } from "../persistence/Errors.ts";
|
||||
@@ -62,24 +62,9 @@ export interface OrchestrationEngineShape {
|
||||
) => Effect.Effect<{ sequence: number }, OrchestrationDispatchError, never>;
|
||||
|
||||
/**
|
||||
* Subscribe to read model updates.
|
||||
*
|
||||
* @param callback - Handler invoked on every read model update.
|
||||
* @returns Effect containing an unsubscribe function.
|
||||
* Stream persisted domain events in dispatch order.
|
||||
*/
|
||||
readonly subscribeToReadModel: (
|
||||
callback: (snapshot: OrchestrationReadModel) => void,
|
||||
) => Effect.Effect<() => void, never, never>;
|
||||
|
||||
/**
|
||||
* Subscribe to domain event fan-out.
|
||||
*
|
||||
* @param callback - Handler invoked for every persisted domain event.
|
||||
* @returns Effect containing an unsubscribe function.
|
||||
*/
|
||||
readonly subscribeToDomainEvents: (
|
||||
callback: (event: OrchestrationEvent) => void,
|
||||
) => Effect.Effect<() => void, never, never>;
|
||||
readonly streamDomainEvents: Stream.Stream<OrchestrationEvent>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -95,4 +95,164 @@ describe("orchestration reducer", () => {
|
||||
expect(next.updatedAt).toBe("2026-01-01T00:00:00.000Z");
|
||||
expect(next.threads).toEqual([]);
|
||||
});
|
||||
|
||||
it("tracks latest turn timing from session lifecycle events", async () => {
|
||||
const createdAt = "2026-02-23T08:00:00.000Z";
|
||||
const startedAt = "2026-02-23T08:00:05.000Z";
|
||||
const completedAt = "2026-02-23T08:00:11.250Z";
|
||||
const model = createEmptyReadModel(createdAt);
|
||||
|
||||
const createdEvent: OrchestrationEvent = {
|
||||
sequence: 1,
|
||||
eventId: "thread-created",
|
||||
type: "thread.created",
|
||||
aggregateType: "thread",
|
||||
aggregateId: "thread-1",
|
||||
occurredAt: createdAt,
|
||||
commandId: "cmd-create",
|
||||
payload: {
|
||||
id: "thread-1",
|
||||
projectId: "project-1",
|
||||
title: "demo",
|
||||
model: "gpt-5.3-codex",
|
||||
branch: null,
|
||||
worktreePath: null,
|
||||
createdAt,
|
||||
updatedAt: createdAt,
|
||||
},
|
||||
};
|
||||
|
||||
const runningSessionEvent: OrchestrationEvent = {
|
||||
sequence: 2,
|
||||
eventId: "session-running",
|
||||
type: "thread.session-set",
|
||||
aggregateType: "thread",
|
||||
aggregateId: "thread-1",
|
||||
occurredAt: startedAt,
|
||||
commandId: "cmd-running",
|
||||
payload: {
|
||||
threadId: "thread-1",
|
||||
session: {
|
||||
sessionId: "session-1",
|
||||
status: "running",
|
||||
provider: "codex",
|
||||
threadId: "thread-1",
|
||||
activeTurnId: "turn-1",
|
||||
createdAt,
|
||||
updatedAt: startedAt,
|
||||
lastError: null,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const readySessionEvent: OrchestrationEvent = {
|
||||
sequence: 3,
|
||||
eventId: "session-ready",
|
||||
type: "thread.session-set",
|
||||
aggregateType: "thread",
|
||||
aggregateId: "thread-1",
|
||||
occurredAt: completedAt,
|
||||
commandId: "cmd-ready",
|
||||
payload: {
|
||||
threadId: "thread-1",
|
||||
session: {
|
||||
sessionId: "session-1",
|
||||
status: "ready",
|
||||
provider: "codex",
|
||||
threadId: "thread-1",
|
||||
activeTurnId: null,
|
||||
createdAt,
|
||||
updatedAt: completedAt,
|
||||
lastError: null,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const afterCreate = await Effect.runPromise(reduceEvent(model, createdEvent));
|
||||
const afterRunning = await Effect.runPromise(reduceEvent(afterCreate, runningSessionEvent));
|
||||
const afterReady = await Effect.runPromise(reduceEvent(afterRunning, readySessionEvent));
|
||||
const thread = afterReady.threads[0];
|
||||
|
||||
expect(thread?.latestTurnId).toBe("turn-1");
|
||||
expect(thread?.latestTurnStartedAt).toBe(startedAt);
|
||||
expect(thread?.latestTurnCompletedAt).toBe(completedAt);
|
||||
expect(thread?.latestTurnDurationMs).toBe(6250);
|
||||
});
|
||||
|
||||
it("marks assistant messages completed using non-streaming updates", async () => {
|
||||
const createdAt = "2026-02-23T09:00:00.000Z";
|
||||
const deltaAt = "2026-02-23T09:00:01.000Z";
|
||||
const completeAt = "2026-02-23T09:00:03.500Z";
|
||||
const model = createEmptyReadModel(createdAt);
|
||||
|
||||
const createdEvent: OrchestrationEvent = {
|
||||
sequence: 1,
|
||||
eventId: "thread-created",
|
||||
type: "thread.created",
|
||||
aggregateType: "thread",
|
||||
aggregateId: "thread-1",
|
||||
occurredAt: createdAt,
|
||||
commandId: "cmd-create",
|
||||
payload: {
|
||||
id: "thread-1",
|
||||
projectId: "project-1",
|
||||
title: "demo",
|
||||
model: "gpt-5.3-codex",
|
||||
branch: null,
|
||||
worktreePath: null,
|
||||
createdAt,
|
||||
updatedAt: createdAt,
|
||||
},
|
||||
};
|
||||
|
||||
const deltaEvent: OrchestrationEvent = {
|
||||
sequence: 2,
|
||||
eventId: "assistant-delta",
|
||||
type: "message.sent",
|
||||
aggregateType: "thread",
|
||||
aggregateId: "thread-1",
|
||||
occurredAt: deltaAt,
|
||||
commandId: "cmd-delta",
|
||||
payload: {
|
||||
id: "assistant:msg-1",
|
||||
role: "assistant",
|
||||
text: "hello",
|
||||
threadId: "thread-1",
|
||||
createdAt: deltaAt,
|
||||
streaming: true,
|
||||
},
|
||||
};
|
||||
|
||||
const completionEvent: OrchestrationEvent = {
|
||||
sequence: 3,
|
||||
eventId: "assistant-complete",
|
||||
type: "message.sent",
|
||||
aggregateType: "thread",
|
||||
aggregateId: "thread-1",
|
||||
occurredAt: completeAt,
|
||||
commandId: "cmd-complete",
|
||||
payload: {
|
||||
id: "assistant:msg-1",
|
||||
role: "assistant",
|
||||
text: "",
|
||||
threadId: "thread-1",
|
||||
createdAt: completeAt,
|
||||
streaming: false,
|
||||
},
|
||||
};
|
||||
|
||||
const afterCreate = await Effect.runPromise(reduceEvent(model, createdEvent));
|
||||
const afterDelta = await Effect.runPromise(reduceEvent(afterCreate, deltaEvent));
|
||||
const afterComplete = await Effect.runPromise(reduceEvent(afterDelta, completionEvent));
|
||||
const message = afterComplete.threads[0]?.messages[0];
|
||||
|
||||
expect(message).toEqual({
|
||||
id: "assistant:msg-1",
|
||||
role: "assistant",
|
||||
text: "hello",
|
||||
createdAt: deltaAt,
|
||||
completedAt: completeAt,
|
||||
streaming: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -204,6 +204,7 @@ export function reduceEvent(
|
||||
role: payload.role,
|
||||
text: payload.text,
|
||||
createdAt: payload.createdAt,
|
||||
...(payload.streaming ? {} : { completedAt: event.occurredAt }),
|
||||
streaming: payload.streaming,
|
||||
},
|
||||
event.type,
|
||||
@@ -222,6 +223,9 @@ export function reduceEvent(
|
||||
: entry.text,
|
||||
streaming: message.streaming,
|
||||
createdAt: entry.createdAt,
|
||||
...(message.streaming
|
||||
? { completedAt: undefined }
|
||||
: { completedAt: message.completedAt ?? event.occurredAt }),
|
||||
}
|
||||
: entry,
|
||||
)
|
||||
@@ -260,12 +264,51 @@ export function reduceEvent(
|
||||
"session",
|
||||
);
|
||||
|
||||
const sessionUpdatedAt = session.updatedAt;
|
||||
const turnTimingPatch: ThreadPatch =
|
||||
session.status === "running" && session.activeTurnId
|
||||
? (() => {
|
||||
const isSameTurn = thread.latestTurnId === session.activeTurnId;
|
||||
return {
|
||||
latestTurnId: session.activeTurnId,
|
||||
latestTurnStartedAt:
|
||||
isSameTurn && thread.latestTurnStartedAt
|
||||
? thread.latestTurnStartedAt
|
||||
: sessionUpdatedAt,
|
||||
...(isSameTurn
|
||||
? {}
|
||||
: {
|
||||
latestTurnCompletedAt: null,
|
||||
latestTurnDurationMs: null,
|
||||
}),
|
||||
};
|
||||
})()
|
||||
: session.activeTurnId === null && thread.latestTurnId && thread.latestTurnStartedAt
|
||||
? (() => {
|
||||
const latestTurnCompletedAt = thread.latestTurnCompletedAt ?? sessionUpdatedAt;
|
||||
const startedAtMs = Date.parse(thread.latestTurnStartedAt);
|
||||
const completedAtMs = Date.parse(latestTurnCompletedAt);
|
||||
const latestTurnDurationMs =
|
||||
!Number.isNaN(startedAtMs) &&
|
||||
!Number.isNaN(completedAtMs) &&
|
||||
completedAtMs >= startedAtMs
|
||||
? completedAtMs - startedAtMs
|
||||
: thread.latestTurnDurationMs;
|
||||
|
||||
return {
|
||||
latestTurnCompletedAt,
|
||||
latestTurnDurationMs,
|
||||
};
|
||||
})()
|
||||
: {};
|
||||
|
||||
return {
|
||||
...nextBase,
|
||||
threads: updateThread(nextBase.threads, payload.threadId, {
|
||||
session,
|
||||
updatedAt: event.occurredAt,
|
||||
error: session.lastError,
|
||||
...turnTimingPatch,
|
||||
}),
|
||||
};
|
||||
});
|
||||
@@ -303,12 +346,23 @@ export function reduceEvent(
|
||||
...thread.turnDiffSummaries.filter((summary) => summary.turnId !== payload.turnId),
|
||||
nextSummary,
|
||||
].toSorted((left, right) => left.completedAt.localeCompare(right.completedAt));
|
||||
const startedAtMs = thread.latestTurnStartedAt
|
||||
? Date.parse(thread.latestTurnStartedAt)
|
||||
: Number.NaN;
|
||||
const completedAtMs = Date.parse(nextSummary.completedAt);
|
||||
const latestTurnDurationMs =
|
||||
!Number.isNaN(startedAtMs) &&
|
||||
!Number.isNaN(completedAtMs) &&
|
||||
completedAtMs >= startedAtMs
|
||||
? completedAtMs - startedAtMs
|
||||
: thread.latestTurnDurationMs;
|
||||
|
||||
return {
|
||||
...nextBase,
|
||||
threads: updateThread(nextBase.threads, payload.threadId, {
|
||||
latestTurnId: payload.turnId,
|
||||
latestTurnCompletedAt: nextSummary.completedAt,
|
||||
latestTurnDurationMs,
|
||||
turnDiffSummaries,
|
||||
updatedAt: event.occurredAt,
|
||||
}),
|
||||
|
||||
@@ -170,6 +170,45 @@ const lifecycleManager = new FakeCodexManager();
|
||||
const lifecycleLayer = it.layer(makeCodexAdapterLive({ manager: lifecycleManager }));
|
||||
|
||||
lifecycleLayer("CodexAdapterLive lifecycle", (it) => {
|
||||
it.effect("maps completed agent message items to canonical message.completed events", () =>
|
||||
Effect.gen(function* () {
|
||||
const adapter = yield* CodexAdapter;
|
||||
const firstEventFiber = yield* Stream.runHead(adapter.streamEvents).pipe(Effect.forkChild);
|
||||
|
||||
const event: ProviderEvent = {
|
||||
id: "evt-msg-complete",
|
||||
kind: "notification",
|
||||
provider: "codex",
|
||||
sessionId: "sess-1",
|
||||
createdAt: new Date().toISOString(),
|
||||
method: "item/completed",
|
||||
threadId: "thread-1",
|
||||
turnId: "turn-1",
|
||||
itemId: "msg_1",
|
||||
payload: {
|
||||
item: {
|
||||
type: "agentMessage",
|
||||
id: "msg_1",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
lifecycleManager.emit("event", event);
|
||||
const firstEvent = yield* Fiber.join(firstEventFiber);
|
||||
|
||||
assert.equal(firstEvent._tag, "Some");
|
||||
if (firstEvent._tag !== "Some") {
|
||||
return;
|
||||
}
|
||||
assert.equal(firstEvent.value.type, "message.completed");
|
||||
if (firstEvent.value.type !== "message.completed") {
|
||||
return;
|
||||
}
|
||||
assert.equal(firstEvent.value.itemId, "msg_1");
|
||||
assert.equal(firstEvent.value.turnId, "turn-1");
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("emits canonical events on stream and supports consumer interruption", () =>
|
||||
Effect.gen(function* () {
|
||||
const adapter = yield* CodexAdapter;
|
||||
|
||||
@@ -161,6 +161,39 @@ function itemDetail(
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function mapMessageCompletedEvent(event: ProviderEvent): ProviderRuntimeEvent | undefined {
|
||||
if (event.method !== "item/completed") {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const payload = asObject(event.payload);
|
||||
const item = asObject(payload?.item);
|
||||
if (!item) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const normalizedType = normalizeItemType(item.type ?? item.kind);
|
||||
if (!normalizedType.includes("agent message")) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const itemId = event.itemId ?? asString(item.id);
|
||||
if (!itemId) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
type: "message.completed",
|
||||
eventId: event.id,
|
||||
provider: event.provider,
|
||||
sessionId: event.sessionId,
|
||||
createdAt: event.createdAt,
|
||||
itemId,
|
||||
...(event.threadId ? { threadId: event.threadId } : {}),
|
||||
...(event.turnId ? { turnId: event.turnId } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
function mapToolEvent(event: ProviderEvent): ProviderRuntimeEvent | undefined {
|
||||
if (event.method !== "item/started" && event.method !== "item/completed") {
|
||||
return undefined;
|
||||
@@ -267,6 +300,11 @@ function mapToRuntimeEvents(event: ProviderEvent): ReadonlyArray<ProviderRuntime
|
||||
];
|
||||
}
|
||||
|
||||
const messageCompleted = mapMessageCompletedEvent(event);
|
||||
if (messageCompleted) {
|
||||
return [messageCompleted];
|
||||
}
|
||||
|
||||
const tool = mapToolEvent(event);
|
||||
if (tool) {
|
||||
return [tool];
|
||||
|
||||
+83
-28
@@ -50,11 +50,9 @@ import { ProviderSessionDirectoryLive } from "./provider/Layers/ProviderSessionD
|
||||
import { ProviderService, type ProviderServiceShape } from "./provider/Services/ProviderService";
|
||||
import { CheckpointStoreLive } from "./checkpointing/Layers/CheckpointStore";
|
||||
import { CheckpointServiceLive } from "./checkpointing/Layers/CheckpointService";
|
||||
import { parseTurnDiffFilesFromUnifiedDiff, type TurnDiffFileSummary } from "./checkpointing/Diffs";
|
||||
import { CheckpointRepositoryLive } from "./persistence/Layers/Checkpoints";
|
||||
import {
|
||||
makeEventNdjsonLogger,
|
||||
type EventNdjsonLogger,
|
||||
} from "./provider/Layers/EventNdjsonLogger";
|
||||
import { makeEventNdjsonLogger, type EventNdjsonLogger } from "./provider/Layers/EventNdjsonLogger";
|
||||
import * as SqlClient from "effect/unstable/sql/SqlClient";
|
||||
import * as SqlError from "effect/unstable/sql/SqlError";
|
||||
import * as Migrator from "effect/unstable/sql/Migrator";
|
||||
@@ -275,28 +273,39 @@ export function createServer(options: ServerOptions) {
|
||||
>,
|
||||
orchestrationEngine: OrchestrationEngineShape,
|
||||
) {
|
||||
unsubscribeDomainEvents = await runtime.runPromise(
|
||||
orchestrationEngine.subscribeToDomainEvents((event) => {
|
||||
orchestrationDomainEventLogger?.write({
|
||||
observedAt: new Date().toISOString(),
|
||||
event,
|
||||
});
|
||||
const push: WsPush = {
|
||||
type: "push",
|
||||
channel: ORCHESTRATION_WS_CHANNELS.domainEvent,
|
||||
data: event,
|
||||
};
|
||||
const message = JSON.stringify(push);
|
||||
let recipients = 0;
|
||||
for (const client of clients) {
|
||||
if (client.readyState === client.OPEN) {
|
||||
client.send(message);
|
||||
recipients += 1;
|
||||
}
|
||||
}
|
||||
logOutgoingPush(push, recipients);
|
||||
}),
|
||||
const subscriptionScope = await runtime.runPromise(Scope.make("sequential"));
|
||||
await runtime.runPromise(
|
||||
Effect.gen(function* () {
|
||||
yield* Effect.forkScoped(
|
||||
Stream.runForEach(orchestrationEngine.streamDomainEvents, (event) =>
|
||||
Effect.sync(() => {
|
||||
orchestrationDomainEventLogger?.write({
|
||||
observedAt: new Date().toISOString(),
|
||||
event,
|
||||
});
|
||||
const push: WsPush = {
|
||||
type: "push",
|
||||
channel: ORCHESTRATION_WS_CHANNELS.domainEvent,
|
||||
data: event,
|
||||
};
|
||||
const message = JSON.stringify(push);
|
||||
let recipients = 0;
|
||||
for (const client of clients) {
|
||||
if (client.readyState === client.OPEN) {
|
||||
client.send(message);
|
||||
recipients += 1;
|
||||
}
|
||||
}
|
||||
logOutgoingPush(push, recipients);
|
||||
}),
|
||||
),
|
||||
);
|
||||
}).pipe(Scope.provide(subscriptionScope)),
|
||||
);
|
||||
|
||||
unsubscribeDomainEvents = () => {
|
||||
runtime.runFork(Scope.close(subscriptionScope, Exit.void));
|
||||
};
|
||||
}
|
||||
|
||||
async function attachProviderSubscriptions(
|
||||
@@ -350,7 +359,9 @@ export function createServer(options: ServerOptions) {
|
||||
const processEvent = (event: ProviderRuntimeEvent) =>
|
||||
Effect.gen(function* () {
|
||||
const snapshot = yield* orchestrationEngine.getSnapshot();
|
||||
const thread = snapshot.threads.find((entry) => entry.session?.sessionId === event.sessionId);
|
||||
const thread = snapshot.threads.find(
|
||||
(entry) => entry.session?.sessionId === event.sessionId,
|
||||
);
|
||||
if (!thread) return;
|
||||
|
||||
const now = event.createdAt;
|
||||
@@ -393,9 +404,29 @@ export function createServer(options: ServerOptions) {
|
||||
});
|
||||
}
|
||||
|
||||
if (event.type === "message.completed") {
|
||||
const assistantMessageId = `assistant:${event.itemId}`;
|
||||
if (event.turnId) {
|
||||
rememberAssistantMessageId(event.sessionId, event.turnId, assistantMessageId);
|
||||
}
|
||||
yield* orchestrationEngine.dispatch({
|
||||
type: "message.send",
|
||||
commandId: crypto.randomUUID(),
|
||||
threadId: thread.id,
|
||||
messageId: assistantMessageId,
|
||||
role: "assistant",
|
||||
text: "",
|
||||
streaming: false,
|
||||
createdAt: now,
|
||||
});
|
||||
}
|
||||
|
||||
if (event.type === "turn.completed") {
|
||||
if (event.turnId) {
|
||||
const assistantMessageIds = getAssistantMessageIdsForTurn(event.sessionId, event.turnId);
|
||||
const assistantMessageIds = getAssistantMessageIdsForTurn(
|
||||
event.sessionId,
|
||||
event.turnId,
|
||||
);
|
||||
yield* Effect.forEach(assistantMessageIds, (assistantMessageId) =>
|
||||
orchestrationEngine.dispatch({
|
||||
type: "message.send",
|
||||
@@ -417,6 +448,30 @@ export function createServer(options: ServerOptions) {
|
||||
}
|
||||
|
||||
if (event.type === "checkpoint.captured" && event.turnId) {
|
||||
const files: ReadonlyArray<TurnDiffFileSummary> = yield* providerService
|
||||
.getCheckpointDiff({
|
||||
sessionId: event.sessionId,
|
||||
fromTurnCount: Math.max(0, event.turnCount - 1),
|
||||
toTurnCount: event.turnCount,
|
||||
})
|
||||
.pipe(
|
||||
Effect.map((result) => parseTurnDiffFilesFromUnifiedDiff(result.diff)),
|
||||
Effect.catch((error) =>
|
||||
Effect.sync(() => {
|
||||
logger.warn("failed to derive checkpoint file summary", {
|
||||
sessionId: event.sessionId,
|
||||
turnId: event.turnId,
|
||||
turnCount: event.turnCount,
|
||||
error:
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: typeof error === "string"
|
||||
? error
|
||||
: "unknown error",
|
||||
});
|
||||
}).pipe(Effect.as([] as ReadonlyArray<TurnDiffFileSummary>)),
|
||||
),
|
||||
);
|
||||
const assistantMessageId =
|
||||
getLatestAssistantMessageIdForTurn(event.sessionId, event.turnId) ??
|
||||
`assistant:${event.turnId}`;
|
||||
@@ -427,7 +482,7 @@ export function createServer(options: ServerOptions) {
|
||||
turnId: event.turnId,
|
||||
completedAt: now,
|
||||
...(event.status ? { status: event.status } : {}),
|
||||
files: [],
|
||||
files,
|
||||
assistantMessageId,
|
||||
checkpointTurnCount: event.turnCount,
|
||||
createdAt: now,
|
||||
|
||||
@@ -26,12 +26,11 @@ import {
|
||||
useRef,
|
||||
useState,
|
||||
} from "react";
|
||||
import { useMutation, useQueries, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useDebouncedValue } from "@tanstack/react-pacer";
|
||||
import { useNavigate, useSearch } from "@tanstack/react-router";
|
||||
import { type VirtualItem, useVirtualizer } from "@tanstack/react-virtual";
|
||||
import { gitBranchesQueryOptions, gitCreateWorktreeMutationOptions } from "~/lib/gitReactQuery";
|
||||
import { checkpointDiffQueryOptions } from "~/lib/providerReactQuery";
|
||||
import { projectSearchEntriesQueryOptions } from "~/lib/projectReactQuery";
|
||||
import { serverConfigQueryOptions, serverQueryKeys } from "~/lib/serverReactQuery";
|
||||
|
||||
@@ -55,7 +54,6 @@ import {
|
||||
import {
|
||||
derivePendingApprovals,
|
||||
derivePhase,
|
||||
deriveTurnDiffFilesFromUnifiedDiff,
|
||||
deriveTimelineEntries,
|
||||
type PendingApproval,
|
||||
deriveWorkLogEntries,
|
||||
@@ -70,7 +68,6 @@ import {
|
||||
DEFAULT_THREAD_TERMINAL_ID,
|
||||
MAX_THREAD_TERMINAL_COUNT,
|
||||
type ChatImageAttachment,
|
||||
type TurnDiffFileChange,
|
||||
type TurnDiffSummary,
|
||||
} from "../types";
|
||||
import { basenameOfPath, getVscodeIconUrlForEntry } from "../vscode-icons";
|
||||
@@ -186,33 +183,6 @@ interface ExpandedImagePreview {
|
||||
name: string;
|
||||
}
|
||||
|
||||
type TurnCheckpointFilesState =
|
||||
| {
|
||||
status: "pending" | "loading";
|
||||
files: TurnDiffFileChange[];
|
||||
errorMessage?: undefined;
|
||||
}
|
||||
| {
|
||||
status: "error";
|
||||
files: TurnDiffFileChange[];
|
||||
errorMessage: string;
|
||||
}
|
||||
| {
|
||||
status: "ready";
|
||||
files: TurnDiffFileChange[];
|
||||
errorMessage?: undefined;
|
||||
};
|
||||
|
||||
function checkpointErrorMessage(error: unknown): string {
|
||||
if (error instanceof Error && error.message.trim().length > 0) {
|
||||
return error.message;
|
||||
}
|
||||
if (typeof error === "string" && error.trim().length > 0) {
|
||||
return error;
|
||||
}
|
||||
return "Checkpoint diff is unavailable.";
|
||||
}
|
||||
|
||||
type ComposerCommandItem =
|
||||
| {
|
||||
id: string;
|
||||
@@ -432,17 +402,21 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
);
|
||||
const phase = derivePhase(activeThread?.session ?? null);
|
||||
const isWorking = phase === "running" || isSending || isConnecting || isRevertingCheckpoint;
|
||||
const activeTurnId = activeThread?.session?.activeTurnId ?? undefined;
|
||||
const nowIso = new Date(nowTick).toISOString();
|
||||
const threadActivities = activeThread?.activities ?? [];
|
||||
const workLogEntries = useMemo(
|
||||
() => deriveWorkLogEntries(threadActivities, undefined),
|
||||
[threadActivities],
|
||||
);
|
||||
const latestTurnWorkEntries = useMemo(
|
||||
() => deriveWorkLogEntries(threadActivities, activeTurnId ?? activeThread?.latestTurnId),
|
||||
[activeThread?.latestTurnId, activeTurnId, threadActivities],
|
||||
);
|
||||
const latestTurnHasToolActivity = useMemo(() => {
|
||||
const latestTurnId = activeThread?.latestTurnId;
|
||||
if (!latestTurnId) {
|
||||
return false;
|
||||
}
|
||||
return threadActivities.some(
|
||||
(activity) => activity.turnId === latestTurnId && activity.tone === "tool",
|
||||
);
|
||||
}, [activeThread?.latestTurnId, threadActivities]);
|
||||
const pendingApprovals = useMemo(
|
||||
() => derivePendingApprovals(threadActivities),
|
||||
[threadActivities],
|
||||
@@ -453,14 +427,6 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
);
|
||||
const { turnDiffSummaries, inferredCheckpointTurnCountByTurnId } =
|
||||
useTurnDiffSummaries(activeThread);
|
||||
const assistantCompletionByItemId = useMemo(() => {
|
||||
const completionByMessageId = new Map<string, string>();
|
||||
for (const summary of turnDiffSummaries) {
|
||||
if (!summary.assistantMessageId) continue;
|
||||
completionByMessageId.set(summary.assistantMessageId, summary.completedAt);
|
||||
}
|
||||
return completionByMessageId;
|
||||
}, [turnDiffSummaries]);
|
||||
const turnDiffSummaryByAssistantMessageId = useMemo(() => {
|
||||
const byMessageId = new Map<string, TurnDiffSummary>();
|
||||
for (const summary of turnDiffSummaries) {
|
||||
@@ -469,69 +435,6 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
}
|
||||
return byMessageId;
|
||||
}, [turnDiffSummaries]);
|
||||
const checkpointDiffInputsByTurn = useMemo(
|
||||
() =>
|
||||
turnDiffSummaries
|
||||
.filter((summary) => summary.assistantMessageId)
|
||||
.map((summary) => {
|
||||
const checkpointTurnCount =
|
||||
summary.checkpointTurnCount ?? inferredCheckpointTurnCountByTurnId[summary.turnId];
|
||||
return {
|
||||
turnId: summary.turnId,
|
||||
checkpointTurnCount:
|
||||
typeof checkpointTurnCount === "number" ? checkpointTurnCount : null,
|
||||
};
|
||||
}),
|
||||
[inferredCheckpointTurnCountByTurnId, turnDiffSummaries],
|
||||
);
|
||||
const checkpointDiffQueriesByTurn = useQueries({
|
||||
queries: checkpointDiffInputsByTurn.map((input) =>
|
||||
checkpointDiffQueryOptions(api, {
|
||||
sessionId: activeSessionId,
|
||||
threadRuntimeId: activeThreadRuntimeId,
|
||||
fromTurnCount:
|
||||
typeof input.checkpointTurnCount === "number"
|
||||
? Math.max(0, input.checkpointTurnCount - 1)
|
||||
: null,
|
||||
toTurnCount: input.checkpointTurnCount,
|
||||
cacheScope: `turn:${input.turnId}`,
|
||||
}),
|
||||
),
|
||||
});
|
||||
const turnCheckpointFilesByTurnId = useMemo(() => {
|
||||
const byTurnId = new Map<string, TurnCheckpointFilesState>();
|
||||
for (let index = 0; index < checkpointDiffInputsByTurn.length; index += 1) {
|
||||
const input = checkpointDiffInputsByTurn[index];
|
||||
const query = checkpointDiffQueriesByTurn[index];
|
||||
if (!input || !query) continue;
|
||||
|
||||
if (!activeSessionId || typeof input.checkpointTurnCount !== "number") {
|
||||
byTurnId.set(input.turnId, { status: "pending", files: [] });
|
||||
continue;
|
||||
}
|
||||
|
||||
const diff = query.data?.diff;
|
||||
if (typeof diff === "string") {
|
||||
byTurnId.set(input.turnId, {
|
||||
status: "ready",
|
||||
files: deriveTurnDiffFilesFromUnifiedDiff(diff),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
if (query.isError) {
|
||||
byTurnId.set(input.turnId, {
|
||||
status: "error",
|
||||
files: [],
|
||||
errorMessage: checkpointErrorMessage(query.error),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
byTurnId.set(input.turnId, { status: "loading", files: [] });
|
||||
}
|
||||
return byTurnId;
|
||||
}, [activeSessionId, checkpointDiffInputsByTurn, checkpointDiffQueriesByTurn]);
|
||||
const revertTurnCountByUserMessageId = useMemo(() => {
|
||||
const byUserMessageId = new Map<string, number>();
|
||||
for (let index = 0; index < timelineEntries.length; index += 1) {
|
||||
@@ -568,9 +471,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
const completionSummary = useMemo(() => {
|
||||
if (!activeThread?.latestTurnStartedAt) return null;
|
||||
if (!activeThread.latestTurnCompletedAt) return null;
|
||||
if (!latestTurnWorkEntries.some((entry) => entry.tone === "tool")) {
|
||||
return null;
|
||||
}
|
||||
if (!latestTurnHasToolActivity) return null;
|
||||
|
||||
if (
|
||||
typeof activeThread.latestTurnDurationMs === "number" &&
|
||||
@@ -589,7 +490,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
activeThread?.latestTurnStartedAt,
|
||||
activeThread?.latestTurnCompletedAt,
|
||||
activeThread?.latestTurnDurationMs,
|
||||
latestTurnWorkEntries,
|
||||
latestTurnHasToolActivity,
|
||||
]);
|
||||
const completionDividerBeforeEntryId = useMemo(() => {
|
||||
if (!activeThread?.latestTurnStartedAt) return null;
|
||||
@@ -2039,9 +1940,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
timelineEntries={timelineEntries}
|
||||
completionDividerBeforeEntryId={completionDividerBeforeEntryId}
|
||||
completionSummary={completionSummary}
|
||||
assistantCompletionByItemId={assistantCompletionByItemId}
|
||||
turnDiffSummaryByAssistantMessageId={turnDiffSummaryByAssistantMessageId}
|
||||
turnCheckpointFilesByTurnId={turnCheckpointFilesByTurnId}
|
||||
nowIso={nowIso}
|
||||
expandedWorkGroups={expandedWorkGroups}
|
||||
onToggleWorkGroup={onToggleWorkGroup}
|
||||
@@ -2505,9 +2404,7 @@ interface MessagesTimelineProps {
|
||||
timelineEntries: ReturnType<typeof deriveTimelineEntries>;
|
||||
completionDividerBeforeEntryId: string | null;
|
||||
completionSummary: string | null;
|
||||
assistantCompletionByItemId: Map<string, string>;
|
||||
turnDiffSummaryByAssistantMessageId: Map<string, TurnDiffSummary>;
|
||||
turnCheckpointFilesByTurnId: Map<string, TurnCheckpointFilesState>;
|
||||
nowIso: string;
|
||||
expandedWorkGroups: Record<string, boolean>;
|
||||
onToggleWorkGroup: (groupId: string) => void;
|
||||
@@ -2542,9 +2439,7 @@ const MessagesTimeline = memo(function MessagesTimeline({
|
||||
timelineEntries,
|
||||
completionDividerBeforeEntryId,
|
||||
completionSummary,
|
||||
assistantCompletionByItemId,
|
||||
turnDiffSummaryByAssistantMessageId,
|
||||
turnCheckpointFilesByTurnId,
|
||||
nowIso,
|
||||
expandedWorkGroups,
|
||||
onToggleWorkGroup,
|
||||
@@ -2793,10 +2688,8 @@ const MessagesTimeline = memo(function MessagesTimeline({
|
||||
row.message.id,
|
||||
);
|
||||
if (!turnSummary) return null;
|
||||
const checkpointFilesState = turnCheckpointFilesByTurnId.get(
|
||||
turnSummary.turnId,
|
||||
);
|
||||
const checkpointFiles = checkpointFilesState?.files ?? [];
|
||||
const checkpointFiles = turnSummary.files;
|
||||
if (checkpointFiles.length === 0) return null;
|
||||
const summaryStat = checkpointFiles.reduce(
|
||||
(acc, file) => {
|
||||
if (
|
||||
@@ -2812,10 +2705,7 @@ const MessagesTimeline = memo(function MessagesTimeline({
|
||||
},
|
||||
{ additions: 0, deletions: 0 },
|
||||
);
|
||||
const changedFileCountLabel =
|
||||
checkpointFilesState?.status === "ready"
|
||||
? String(checkpointFiles.length)
|
||||
: "...";
|
||||
const changedFileCountLabel = String(checkpointFiles.length);
|
||||
return (
|
||||
<div className="mt-2 rounded-lg border border-border/80 bg-card/45 p-2.5">
|
||||
<div className="mb-1.5 flex items-center justify-between gap-2">
|
||||
@@ -2843,66 +2733,40 @@ const MessagesTimeline = memo(function MessagesTimeline({
|
||||
View diff
|
||||
</Button>
|
||||
</div>
|
||||
{!checkpointFilesState ||
|
||||
checkpointFilesState.status === "pending" ? (
|
||||
<p className="text-[11px] text-muted-foreground/70">
|
||||
Waiting for checkpoint metadata...
|
||||
</p>
|
||||
) : checkpointFilesState?.status === "loading" ? (
|
||||
<p className="text-[11px] text-muted-foreground/70">
|
||||
Loading checkpoint diff...
|
||||
</p>
|
||||
) : checkpointFilesState?.status === "error" ? (
|
||||
<p
|
||||
className="truncate text-[11px] text-muted-foreground/70"
|
||||
title={checkpointFilesState.errorMessage}
|
||||
>
|
||||
{checkpointFilesState.errorMessage}
|
||||
</p>
|
||||
) : checkpointFiles.length > 0 ? (
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
{checkpointFiles.map((file) => (
|
||||
<button
|
||||
key={`${turnSummary.turnId}:${file.path}`}
|
||||
type="button"
|
||||
className="rounded-md border border-border/70 bg-background/70 px-2 py-1 font-mono text-[11px] text-muted-foreground/80 transition-colors hover:border-border hover:text-foreground/90"
|
||||
onClick={() => onOpenTurnDiff(turnSummary.turnId, file.path)}
|
||||
>
|
||||
{(() => {
|
||||
const stat =
|
||||
typeof file.additions === "number" &&
|
||||
typeof file.deletions === "number"
|
||||
? {
|
||||
additions: file.additions,
|
||||
deletions: file.deletions,
|
||||
}
|
||||
: null;
|
||||
if (!stat) {
|
||||
return file.path;
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<span>{file.path}</span>
|
||||
<span className="ml-1 text-muted-foreground/70">(</span>
|
||||
<span className="text-success">+{stat.additions}</span>
|
||||
<span className="mx-0.5 text-muted-foreground/70">
|
||||
/
|
||||
</span>
|
||||
<span className="text-destructive">
|
||||
-{stat.deletions}
|
||||
</span>
|
||||
<span className="text-muted-foreground/70">)</span>
|
||||
</>
|
||||
);
|
||||
})()}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<p className="text-[11px] text-muted-foreground/70">
|
||||
No changed files.
|
||||
</p>
|
||||
)}
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
{checkpointFiles.map((file) => (
|
||||
<button
|
||||
key={`${turnSummary.turnId}:${file.path}`}
|
||||
type="button"
|
||||
className="rounded-md border border-border/70 bg-background/70 px-2 py-1 font-mono text-[11px] text-muted-foreground/80 transition-colors hover:border-border hover:text-foreground/90"
|
||||
onClick={() => onOpenTurnDiff(turnSummary.turnId, file.path)}
|
||||
>
|
||||
{(() => {
|
||||
const stat =
|
||||
typeof file.additions === "number" &&
|
||||
typeof file.deletions === "number"
|
||||
? {
|
||||
additions: file.additions,
|
||||
deletions: file.deletions,
|
||||
}
|
||||
: null;
|
||||
if (!stat) {
|
||||
return file.path;
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<span>{file.path}</span>
|
||||
<span className="ml-1 text-muted-foreground/70">(</span>
|
||||
<span className="text-success">+{stat.additions}</span>
|
||||
<span className="mx-0.5 text-muted-foreground/70">/</span>
|
||||
<span className="text-destructive">-{stat.deletions}</span>
|
||||
<span className="text-muted-foreground/70">)</span>
|
||||
</>
|
||||
);
|
||||
})()}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})()}
|
||||
@@ -2911,10 +2775,7 @@ const MessagesTimeline = memo(function MessagesTimeline({
|
||||
row.message.createdAt,
|
||||
row.message.streaming
|
||||
? formatElapsed(row.message.createdAt, nowIso)
|
||||
: formatElapsed(
|
||||
row.message.createdAt,
|
||||
assistantCompletionByItemId.get(row.message.id),
|
||||
),
|
||||
: formatElapsed(row.message.createdAt, row.message.completedAt),
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
@@ -104,14 +104,28 @@ export default function DiffPanel({ mode = "inline" }: DiffPanelProps) {
|
||||
const activeSessionId = activeThread?.session?.sessionId ?? null;
|
||||
const { turnDiffSummaries, inferredCheckpointTurnCountByTurnId } =
|
||||
useTurnDiffSummaries(activeThread);
|
||||
const orderedTurnDiffSummaries = useMemo(
|
||||
() =>
|
||||
[...turnDiffSummaries].toSorted((left, right) => {
|
||||
const leftTurnCount =
|
||||
left.checkpointTurnCount ?? inferredCheckpointTurnCountByTurnId[left.turnId] ?? 0;
|
||||
const rightTurnCount =
|
||||
right.checkpointTurnCount ?? inferredCheckpointTurnCountByTurnId[right.turnId] ?? 0;
|
||||
if (leftTurnCount !== rightTurnCount) {
|
||||
return rightTurnCount - leftTurnCount;
|
||||
}
|
||||
return right.completedAt.localeCompare(left.completedAt);
|
||||
}),
|
||||
[inferredCheckpointTurnCountByTurnId, turnDiffSummaries],
|
||||
);
|
||||
|
||||
const selectedTurnId = diffSearch.diffTurnId ?? null;
|
||||
const selectedFilePath = selectedTurnId !== null ? (diffSearch.diffFilePath ?? null) : null;
|
||||
const selectedTurn =
|
||||
selectedTurnId === null
|
||||
? undefined
|
||||
: (turnDiffSummaries.find((summary) => summary.turnId === selectedTurnId) ??
|
||||
turnDiffSummaries[0]);
|
||||
: (orderedTurnDiffSummaries.find((summary) => summary.turnId === selectedTurnId) ??
|
||||
orderedTurnDiffSummaries[0]);
|
||||
const selectedCheckpointTurnCount =
|
||||
selectedTurn &&
|
||||
(selectedTurn.checkpointTurnCount ?? inferredCheckpointTurnCountByTurnId[selectedTurn.turnId]);
|
||||
@@ -126,7 +140,7 @@ export default function DiffPanel({ mode = "inline" }: DiffPanelProps) {
|
||||
[selectedCheckpointTurnCount],
|
||||
);
|
||||
const conversationCheckpointTurnCount = useMemo(() => {
|
||||
const turnCounts = turnDiffSummaries
|
||||
const turnCounts = orderedTurnDiffSummaries
|
||||
.map(
|
||||
(summary) =>
|
||||
summary.checkpointTurnCount ?? inferredCheckpointTurnCountByTurnId[summary.turnId],
|
||||
@@ -137,7 +151,7 @@ export default function DiffPanel({ mode = "inline" }: DiffPanelProps) {
|
||||
}
|
||||
const latest = Math.max(...turnCounts);
|
||||
return latest > 0 ? latest : undefined;
|
||||
}, [inferredCheckpointTurnCountByTurnId, turnDiffSummaries]);
|
||||
}, [inferredCheckpointTurnCountByTurnId, orderedTurnDiffSummaries]);
|
||||
const conversationCheckpointRange = useMemo(
|
||||
() =>
|
||||
!selectedTurn && typeof conversationCheckpointTurnCount === "number"
|
||||
@@ -152,11 +166,11 @@ export default function DiffPanel({ mode = "inline" }: DiffPanelProps) {
|
||||
? selectedCheckpointRange
|
||||
: conversationCheckpointRange;
|
||||
const conversationCacheScope = useMemo(() => {
|
||||
if (selectedTurn || turnDiffSummaries.length === 0) {
|
||||
if (selectedTurn || orderedTurnDiffSummaries.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return `conversation:${turnDiffSummaries.map((summary) => summary.turnId).join(",")}`;
|
||||
}, [selectedTurn, turnDiffSummaries]);
|
||||
return `conversation:${orderedTurnDiffSummaries.map((summary) => summary.turnId).join(",")}`;
|
||||
}, [orderedTurnDiffSummaries, selectedTurn]);
|
||||
const activeCheckpointDiffQuery = useQuery(
|
||||
checkpointDiffQueryOptions(api, {
|
||||
sessionId: activeSessionId,
|
||||
@@ -181,6 +195,8 @@ export default function DiffPanel({ mode = "inline" }: DiffPanelProps) {
|
||||
: null;
|
||||
|
||||
const selectedPatch = selectedTurn ? selectedTurnCheckpointDiff : conversationCheckpointDiff;
|
||||
const hasResolvedPatch = typeof selectedPatch === "string";
|
||||
const hasNoNetChanges = hasResolvedPatch && selectedPatch.trim().length === 0;
|
||||
const renderablePatch = useMemo(() => getRenderablePatch(selectedPatch), [selectedPatch]);
|
||||
const renderableFiles = useMemo(() => {
|
||||
if (!renderablePatch || renderablePatch.kind !== "files") {
|
||||
@@ -275,7 +291,7 @@ export default function DiffPanel({ mode = "inline" }: DiffPanelProps) {
|
||||
|
||||
useEffect(() => {
|
||||
updateTurnStripScrollState();
|
||||
}, [turnDiffSummaries, selectedTurnId, updateTurnStripScrollState]);
|
||||
}, [orderedTurnDiffSummaries, selectedTurnId, updateTurnStripScrollState]);
|
||||
|
||||
useEffect(() => {
|
||||
const element = turnStripRef.current;
|
||||
@@ -358,7 +374,7 @@ export default function DiffPanel({ mode = "inline" }: DiffPanelProps) {
|
||||
<div className="text-[10px] leading-tight font-medium">All turns</div>
|
||||
</div>
|
||||
</button>
|
||||
{turnDiffSummaries.map((summary, index) => (
|
||||
{orderedTurnDiffSummaries.map((summary) => (
|
||||
<button
|
||||
key={summary.turnId}
|
||||
type="button"
|
||||
@@ -377,7 +393,10 @@ export default function DiffPanel({ mode = "inline" }: DiffPanelProps) {
|
||||
>
|
||||
<div className="flex items-center gap-1">
|
||||
<span className="text-[10px] leading-tight font-medium">
|
||||
Turn {turnDiffSummaries.length - index}
|
||||
Turn{" "}
|
||||
{summary.checkpointTurnCount ??
|
||||
inferredCheckpointTurnCountByTurnId[summary.turnId] ??
|
||||
"?"}
|
||||
</span>
|
||||
<span className="text-[9px] leading-tight opacity-70">
|
||||
{formatTurnChipTimestamp(summary.completedAt)}
|
||||
@@ -413,7 +432,7 @@ export default function DiffPanel({ mode = "inline" }: DiffPanelProps) {
|
||||
<div className="flex flex-1 items-center justify-center px-5 text-center text-xs text-muted-foreground/70">
|
||||
Select a thread to inspect turn diffs.
|
||||
</div>
|
||||
) : turnDiffSummaries.length === 0 ? (
|
||||
) : orderedTurnDiffSummaries.length === 0 ? (
|
||||
<div className="flex flex-1 items-center justify-center px-5 text-center text-xs text-muted-foreground/70">
|
||||
No completed turns yet.
|
||||
</div>
|
||||
@@ -430,7 +449,9 @@ export default function DiffPanel({ mode = "inline" }: DiffPanelProps) {
|
||||
<p>
|
||||
{isLoadingCheckpointDiff
|
||||
? "Loading checkpoint diff..."
|
||||
: "No patch available for this selection."}
|
||||
: hasNoNetChanges
|
||||
? "No net changes in this selection."
|
||||
: "No patch available for this selection."}
|
||||
</p>
|
||||
</div>
|
||||
) : renderablePatch.kind === "files" ? (
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useMemo } from "react";
|
||||
import { inferCheckpointTurnCountByTurnId } from "../session-logic";
|
||||
import type { Thread, TurnDiffSummary } from "../types";
|
||||
import type { Thread } from "../types";
|
||||
|
||||
export function useTurnDiffSummaries(activeThread: Thread | undefined) {
|
||||
const turnDiffSummaries = useMemo(() => {
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import type { OrchestrationThreadActivity } from "@t3tools/contracts";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { derivePendingApprovals, deriveWorkLogEntries } from "./session-logic";
|
||||
import {
|
||||
derivePendingApprovals,
|
||||
deriveTurnDiffFilesFromUnifiedDiff,
|
||||
deriveWorkLogEntries,
|
||||
} from "./session-logic";
|
||||
|
||||
function makeActivity(
|
||||
overrides: Partial<OrchestrationThreadActivity>,
|
||||
@@ -87,4 +91,52 @@ describe("deriveWorkLogEntries", () => {
|
||||
const entries = deriveWorkLogEntries(activities, "turn-2");
|
||||
expect(entries.map((entry) => entry.id)).toEqual(["turn-2"]);
|
||||
});
|
||||
|
||||
it("omits checkpoint captured info entries", () => {
|
||||
const activities: OrchestrationThreadActivity[] = [
|
||||
makeActivity({
|
||||
id: "checkpoint",
|
||||
createdAt: "2026-02-23T00:00:01.000Z",
|
||||
label: "Checkpoint captured",
|
||||
tone: "info",
|
||||
}),
|
||||
makeActivity({
|
||||
id: "tool-complete",
|
||||
createdAt: "2026-02-23T00:00:02.000Z",
|
||||
label: "Command run complete",
|
||||
tone: "tool",
|
||||
}),
|
||||
];
|
||||
|
||||
const entries = deriveWorkLogEntries(activities, undefined);
|
||||
expect(entries.map((entry) => entry.id)).toEqual(["tool-complete"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("deriveTurnDiffFilesFromUnifiedDiff", () => {
|
||||
it("extracts per-file +/- counts from unified diff", () => {
|
||||
const diff = [
|
||||
"diff --git a/a.txt b/a.txt",
|
||||
"index 111..222 100644",
|
||||
"--- a/a.txt",
|
||||
"+++ b/a.txt",
|
||||
"@@ -1 +1,2 @@",
|
||||
"-old",
|
||||
"+new",
|
||||
"+extra",
|
||||
"diff --git a/b.txt b/b.txt",
|
||||
"index 333..444 100644",
|
||||
"--- a/b.txt",
|
||||
"+++ b/b.txt",
|
||||
"@@ -2,2 +2 @@",
|
||||
"-x",
|
||||
"-y",
|
||||
"+z",
|
||||
].join("\n");
|
||||
|
||||
expect(deriveTurnDiffFilesFromUnifiedDiff(diff)).toEqual([
|
||||
{ path: "a.txt", additions: 2, deletions: 1 },
|
||||
{ path: "b.txt", additions: 1, deletions: 2 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -112,6 +112,7 @@ export function deriveWorkLogEntries(
|
||||
);
|
||||
return ordered
|
||||
.filter((activity) => (latestTurnId ? activity.turnId === latestTurnId : true))
|
||||
.filter((activity) => activity.label !== "Checkpoint captured")
|
||||
.map((activity) => {
|
||||
const entry: WorkLogEntry = {
|
||||
id: activity.id,
|
||||
@@ -146,16 +147,47 @@ export function deriveTimelineEntries(
|
||||
}
|
||||
|
||||
export function deriveTurnDiffFilesFromUnifiedDiff(diff: string): TurnDiffFileChange[] {
|
||||
const normalized = diff.replace(/\r\n/g, "\n");
|
||||
const headerMatches = [...normalized.matchAll(/^diff --git .+$/gm)];
|
||||
if (headerMatches.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const files: TurnDiffFileChange[] = [];
|
||||
const lines = diff.split("\n");
|
||||
for (const line of lines) {
|
||||
if (!line.startsWith("diff --git ")) continue;
|
||||
const match = line.match(/^diff --git a\/(.+?) b\/(.+)$/);
|
||||
for (let index = 0; index < headerMatches.length; index += 1) {
|
||||
const header = headerMatches[index];
|
||||
if (!header) continue;
|
||||
const start = header.index ?? 0;
|
||||
const nextStart = headerMatches[index + 1]?.index ?? normalized.length;
|
||||
const segment = normalized.slice(start, nextStart);
|
||||
const headerLine = header[0];
|
||||
if (!headerLine) continue;
|
||||
|
||||
const match = headerLine.match(/^diff --git a\/(.+?) b\/(.+)$/);
|
||||
const filePath = match?.[2] ?? match?.[1];
|
||||
if (!filePath) continue;
|
||||
files.push({ path: filePath });
|
||||
|
||||
let additions = 0;
|
||||
let deletions = 0;
|
||||
for (const line of segment.split("\n")) {
|
||||
if (line.startsWith("+++ ") || line.startsWith("--- ")) continue;
|
||||
if (line.startsWith("+")) {
|
||||
additions += 1;
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith("-")) {
|
||||
deletions += 1;
|
||||
}
|
||||
}
|
||||
|
||||
files.push({
|
||||
path: filePath,
|
||||
additions,
|
||||
deletions,
|
||||
});
|
||||
}
|
||||
return files;
|
||||
|
||||
return files.toSorted((left, right) => left.path.localeCompare(right.path));
|
||||
}
|
||||
|
||||
export function inferCheckpointTurnCountByTurnId(
|
||||
|
||||
@@ -34,6 +34,7 @@ export interface ChatMessage {
|
||||
text: string;
|
||||
attachments?: ChatAttachment[];
|
||||
createdAt: string;
|
||||
completedAt?: string | undefined;
|
||||
streaming: boolean;
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@effect/platform-node": "4.0.0-beta.10",
|
||||
"@pierre/diffs": "^1.1.0-beta.16",
|
||||
"effect": "4.0.0-beta.10",
|
||||
"node-pty": "^1.1.0",
|
||||
"open": "^10.1.0",
|
||||
|
||||
@@ -18,6 +18,7 @@ export const OrchestrationMessageSchema = Schema.Struct({
|
||||
role: Schema.Literals(["user", "assistant"]),
|
||||
text: Schema.String,
|
||||
createdAt: IsoDateTimeSchema,
|
||||
completedAt: Schema.optional(IsoDateTimeSchema),
|
||||
streaming: Schema.Boolean,
|
||||
});
|
||||
|
||||
|
||||
@@ -85,6 +85,17 @@ export const ProviderRuntimeMessageDeltaEventSchema = Schema.Struct({
|
||||
delta: Schema.String,
|
||||
});
|
||||
|
||||
export const ProviderRuntimeMessageCompletedEventSchema = Schema.Struct({
|
||||
type: Schema.Literal("message.completed"),
|
||||
eventId: IdSchema,
|
||||
provider: ProviderRuntimeProviderSchema,
|
||||
sessionId: IdSchema,
|
||||
createdAt: IsoDateTimeSchema,
|
||||
itemId: IdSchema,
|
||||
threadId: Schema.optional(Schema.String),
|
||||
turnId: Schema.optional(Schema.String),
|
||||
});
|
||||
|
||||
export const ProviderRuntimeToolStartedEventSchema = Schema.Struct({
|
||||
type: Schema.Literal("tool.started"),
|
||||
eventId: IdSchema,
|
||||
@@ -172,6 +183,7 @@ export const ProviderRuntimeEventSchema = Schema.Union([
|
||||
ProviderRuntimeTurnStartedEventSchema,
|
||||
ProviderRuntimeTurnCompletedEventSchema,
|
||||
ProviderRuntimeMessageDeltaEventSchema,
|
||||
ProviderRuntimeMessageCompletedEventSchema,
|
||||
ProviderRuntimeToolStartedEventSchema,
|
||||
ProviderRuntimeToolCompletedEventSchema,
|
||||
ProviderRuntimeApprovalRequestedEventSchema,
|
||||
@@ -198,6 +210,9 @@ export type ProviderRuntimeTurnCompletedEvent = Schema.Schema.Type<
|
||||
export type ProviderRuntimeMessageDeltaEvent = Schema.Schema.Type<
|
||||
typeof ProviderRuntimeMessageDeltaEventSchema
|
||||
>;
|
||||
export type ProviderRuntimeMessageCompletedEvent = Schema.Schema.Type<
|
||||
typeof ProviderRuntimeMessageCompletedEventSchema
|
||||
>;
|
||||
export type ProviderRuntimeToolStartedEvent = Schema.Schema.Type<
|
||||
typeof ProviderRuntimeToolStartedEventSchema
|
||||
>;
|
||||
|
||||
Reference in new issue
Block a user