Skip to content

Commit 0ac6907

Browse files
committed
feat(codex): import existing sessions
1 parent bfc3150 commit 0ac6907

28 files changed

Lines changed: 2136 additions & 32 deletions

‎apps/server/src/auth/RpcAuthorization.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ export const RPC_REQUIRED_SCOPES = {
5858
[WS_METHODS.projectsSearchContents]: AuthOrchestrationReadScope,
5959
[WS_METHODS.projectsSearchEntries]: AuthOrchestrationReadScope,
6060
[WS_METHODS.projectsWriteFile]: AuthOrchestrationOperateScope,
61+
[WS_METHODS.codexListSessions]: AuthOrchestrationReadScope,
62+
[WS_METHODS.codexImportSessions]: AuthOrchestrationOperateScope,
6163
[WS_METHODS.shellOpenInEditor]: AuthOrchestrationOperateScope,
6264
[WS_METHODS.filesystemBrowse]: AuthOrchestrationReadScope,
6365
[WS_METHODS.assetsCreateUrl]: AuthOrchestrationReadScope,

‎apps/server/src/environment/ServerEnvironment.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ it.layer(NodeServices.layer)("ServerEnvironmentLive", (it) => {
7070
expect(second.capabilities.repositoryIdentity).toBe(true);
7171
expect(second.capabilities.connectionProbe).toBe(true);
7272
expect(second.capabilities.threadTitleRegeneration).toBe(true);
73+
expect(second.capabilities.codexSessionImport).toBe(true);
7374
}),
7475
);
7576

‎apps/server/src/environment/ServerEnvironment.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ export const make = Effect.gen(function* () {
143143
threadSettlement: true,
144144
threadSnooze: true,
145145
threadTitleRegeneration: true,
146+
codexSessionImport: true,
146147
...(serverSelfUpdate === null ? {} : { serverSelfUpdate }),
147148
...(serverSelfUpdate === "boot-service" || serverSelfUpdate === "respawn"
148149
? { serverSelfUpdateProgress: true }
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import {
2+
CommandId,
3+
EventId,
4+
MessageId,
5+
ProjectId,
6+
ProviderInstanceId,
7+
ThreadId,
8+
type OrchestrationCommand,
9+
type OrchestrationEvent,
10+
} from "@t3tools/contracts";
11+
import * as NodeServices from "@effect/platform-node/NodeServices";
12+
import { expect, it } from "@effect/vitest";
13+
import * as Effect from "effect/Effect";
14+
15+
import { decideOrchestrationCommand } from "./decider.ts";
16+
import { createEmptyReadModel, projectEvent } from "./projector.ts";
17+
18+
const NOW = "2026-01-01T00:00:00.000Z";
19+
const PROJECT_ID = ProjectId.make("project-history-import");
20+
const THREAD_ID = ThreadId.make("thread-history-import");
21+
22+
const seedReadModel = projectEvent(createEmptyReadModel(NOW), {
23+
sequence: 1,
24+
eventId: EventId.make("evt-project-history-import"),
25+
aggregateKind: "project",
26+
aggregateId: PROJECT_ID,
27+
type: "project.created",
28+
occurredAt: NOW,
29+
commandId: CommandId.make("cmd-project-history-import"),
30+
causationEventId: null,
31+
correlationId: CommandId.make("cmd-project-history-import"),
32+
metadata: {},
33+
payload: {
34+
projectId: PROJECT_ID,
35+
title: "History Import",
36+
workspaceRoot: "/tmp/history-import",
37+
defaultModelSelection: null,
38+
scripts: [],
39+
createdAt: NOW,
40+
updatedAt: NOW,
41+
},
42+
});
43+
44+
it.layer(NodeServices.layer)("history import decider", (it) => {
45+
it.effect("creates a non-running thread and projects its text snapshot in order", () =>
46+
Effect.gen(function* () {
47+
const readModel = yield* seedReadModel;
48+
const command = {
49+
type: "thread.history.import",
50+
commandId: CommandId.make("cmd-history-import"),
51+
threadId: THREAD_ID,
52+
projectId: PROJECT_ID,
53+
title: "Codex conversation",
54+
modelSelection: {
55+
instanceId: ProviderInstanceId.make("codex"),
56+
model: "gpt-5-codex",
57+
},
58+
runtimeMode: "full-access",
59+
interactionMode: "default",
60+
createdAt: NOW,
61+
messages: [
62+
{
63+
messageId: MessageId.make("message-history-user"),
64+
role: "user",
65+
text: "Please inspect this project.",
66+
turnId: null,
67+
createdAt: "2026-01-01T00:01:00.000Z",
68+
},
69+
{
70+
messageId: MessageId.make("message-history-assistant"),
71+
role: "assistant",
72+
text: "I found the relevant files.",
73+
turnId: null,
74+
createdAt: "2026-01-01T00:02:00.000Z",
75+
},
76+
],
77+
} satisfies OrchestrationCommand;
78+
79+
const result = yield* decideOrchestrationCommand({ command, readModel });
80+
const events = Array.isArray(result) ? result : [result];
81+
82+
expect(events.map((event) => event.type)).toEqual([
83+
"thread.created",
84+
"thread.message-sent",
85+
"thread.message-sent",
86+
]);
87+
const created = events[0];
88+
expect(created?.type).toBe("thread.created");
89+
if (created?.type === "thread.created") {
90+
expect(created.payload.branch).toBeNull();
91+
expect(created.payload.worktreePath).toBeNull();
92+
}
93+
const messages = events.filter(
94+
(event): event is Extract<OrchestrationEvent, { type: "thread.message-sent" }> =>
95+
event.type === "thread.message-sent",
96+
);
97+
expect(messages.map((event) => event.payload.text)).toEqual([
98+
"Please inspect this project.",
99+
"I found the relevant files.",
100+
]);
101+
expect(messages.every((event) => event.payload.streaming === false)).toBe(true);
102+
103+
let projected = readModel;
104+
for (const [index, event] of events.entries()) {
105+
projected = yield* projectEvent(projected, { ...event, sequence: index + 2 });
106+
}
107+
const thread = projected.threads.find((entry) => entry.id === THREAD_ID);
108+
expect(thread?.session).toBeNull();
109+
expect(thread?.messages.map((message) => [message.role, message.text])).toEqual([
110+
["user", "Please inspect this project."],
111+
["assistant", "I found the relevant files."],
112+
]);
113+
}),
114+
);
115+
});

‎apps/server/src/orchestration/decider.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,65 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
378378
};
379379
}
380380

381+
case "thread.history.import": {
382+
yield* requireProject({
383+
readModel,
384+
command,
385+
projectId: command.projectId,
386+
});
387+
yield* requireThreadAbsent({
388+
readModel,
389+
command,
390+
threadId: command.threadId,
391+
});
392+
393+
const created = {
394+
...(yield* withEventBase({
395+
aggregateKind: "thread",
396+
aggregateId: command.threadId,
397+
occurredAt: command.createdAt,
398+
commandId: command.commandId,
399+
})),
400+
type: "thread.created" as const,
401+
payload: {
402+
threadId: command.threadId,
403+
projectId: command.projectId,
404+
title: command.title,
405+
modelSelection: command.modelSelection,
406+
runtimeMode: command.runtimeMode,
407+
interactionMode: command.interactionMode,
408+
branch: null,
409+
worktreePath: null,
410+
createdAt: command.createdAt,
411+
updatedAt: command.createdAt,
412+
},
413+
};
414+
const importedMessages = yield* Effect.forEach(command.messages, (message) =>
415+
withEventBase({
416+
aggregateKind: "thread",
417+
aggregateId: command.threadId,
418+
occurredAt: message.createdAt,
419+
commandId: command.commandId,
420+
}).pipe(
421+
Effect.map((eventBase) => ({
422+
...eventBase,
423+
type: "thread.message-sent" as const,
424+
payload: {
425+
threadId: command.threadId,
426+
messageId: message.messageId,
427+
role: message.role,
428+
text: message.text,
429+
turnId: message.turnId,
430+
streaming: false,
431+
createdAt: message.createdAt,
432+
updatedAt: message.createdAt,
433+
},
434+
})),
435+
),
436+
);
437+
return [created, ...importedMessages];
438+
}
439+
381440
case "thread.delete": {
382441
yield* requireThread({
383442
readModel,

‎apps/server/src/provider/Drivers/CodexDriver.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { ServerSettingsService } from "../../serverSettings.ts";
3737
import { ProviderDriverError } from "../Errors.ts";
3838
import { makeCodexAdapter } from "../Layers/CodexAdapter.ts";
3939
import { checkCodexProviderStatus, makePendingCodexProvider } from "../Layers/CodexProvider.ts";
40+
import { makeCodexThreadHistory } from "../Layers/CodexThreadHistory.ts";
4041
import { ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts";
4142
import { makeManagedServerProvider } from "../makeManagedServerProvider.ts";
4243
import type { ProviderDriver, ProviderInstance } from "../ProviderDriver.ts";
@@ -160,6 +161,11 @@ export const CodexDriver: ProviderDriver<CodexSettings, CodexDriverEnv> = {
160161
environment: processEnv,
161162
...(eventLoggers.native ? { nativeEventLogger: eventLoggers.native } : {}),
162163
});
164+
const threadHistory = makeCodexThreadHistory({
165+
config: effectiveConfig,
166+
environment: processEnv,
167+
spawner,
168+
});
163169
const textGeneration = yield* makeCodexTextGeneration(effectiveConfig, processEnv);
164170

165171
// Build a managed snapshot whose settings never change — mutations come
@@ -207,6 +213,7 @@ export const CodexDriver: ProviderDriver<CodexSettings, CodexDriverEnv> = {
207213
enabled,
208214
snapshot,
209215
adapter,
216+
threadHistory,
210217
textGeneration,
211218
} satisfies ProviderInstance;
212219
}),

‎apps/server/src/provider/Errors.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,24 @@ export class ProviderDriverError extends Schema.TaggedErrorClass<ProviderDriverE
156156
}
157157
}
158158

159+
/**
160+
* ProviderThreadHistoryError - A provider's optional native-history bridge
161+
* could not discover or read a persisted conversation.
162+
*/
163+
export class ProviderThreadHistoryError extends Schema.TaggedErrorClass<ProviderThreadHistoryError>()(
164+
"ProviderThreadHistoryError",
165+
{
166+
provider: Schema.String,
167+
operation: Schema.Literals(["list", "read"]),
168+
detail: Schema.String,
169+
cause: Schema.optional(Schema.Defect()),
170+
},
171+
) {
172+
override get message(): string {
173+
return `Provider thread history failed (${this.provider}) during ${this.operation}: ${this.detail}`;
174+
}
175+
}
176+
159177
/**
160178
* ProviderSessionNotFoundError - Provider-facing session not found.
161179
*/

0 commit comments

Comments
 (0)