Fix three bugs in orchestration thread state management
1. Preserve Codex provider thread ID instead of overwriting with orchestration UUID:
- reducer.ts: Use payload.session as-is instead of replacing its threadId
- wsServer.ts: Use event.threadId (Codex thread ID) with fallback chain in
turn/started, turn/completed, and error handlers
2. Set latestTurnStartedAt and latestTurnDurationMs in session-set reducer:
- Set latestTurnStartedAt when session transitions to running
- Compute latestTurnDurationMs when session transitions to ready
3. Fix stale closure in AutoProjectBootstrap causing duplicate threads:
- Use a ref for state.threads to always read the latest value in the async
.then() callback
This commit is contained in:
1 parent
9ef01eb97e
commit
9299f6580f
3 files changed
+23
-7
No files matched your search
@@ -214,10 +214,21 @@ export function reduceEvent(
|
||||
return nextBase;
|
||||
}
|
||||
|
||||
const session: OrchestrationSession = yield* decodeSession({
|
||||
...payload.session,
|
||||
threadId: payload.threadId,
|
||||
});
|
||||
const session: OrchestrationSession = yield* decodeSession(payload.session);
|
||||
|
||||
const turnTimingPatch: ThreadPatch =
|
||||
session.status === "running" && session.activeTurnId
|
||||
? { latestTurnStartedAt: event.occurredAt, latestTurnDurationMs: null }
|
||||
: session.status === "ready" && thread.latestTurnStartedAt
|
||||
? (() => {
|
||||
const startMs = Date.parse(thread.latestTurnStartedAt);
|
||||
const endMs = Date.parse(event.occurredAt);
|
||||
if (!Number.isNaN(startMs) && !Number.isNaN(endMs) && endMs >= startMs) {
|
||||
return { latestTurnDurationMs: endMs - startMs } as ThreadPatch;
|
||||
}
|
||||
return {};
|
||||
})()
|
||||
: {};
|
||||
|
||||
return {
|
||||
...nextBase,
|
||||
@@ -225,6 +236,7 @@ export function reduceEvent(
|
||||
session,
|
||||
updatedAt: event.occurredAt,
|
||||
error: session.lastError,
|
||||
...turnTimingPatch,
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
@@ -237,7 +237,7 @@ export function createServer(options: ServerOptions) {
|
||||
sessionId: event.sessionId,
|
||||
provider: event.provider,
|
||||
status: event.method === "turn/started" ? "running" : "ready",
|
||||
threadId: thread.id,
|
||||
threadId: event.threadId ?? thread.session?.threadId ?? thread.id,
|
||||
activeTurnId: event.method === "turn/started" ? (event.turnId ?? null) : null,
|
||||
createdAt: thread.session?.createdAt ?? now,
|
||||
updatedAt: now,
|
||||
@@ -311,7 +311,7 @@ export function createServer(options: ServerOptions) {
|
||||
sessionId: event.sessionId,
|
||||
provider: event.provider,
|
||||
status: "error",
|
||||
threadId: thread.id,
|
||||
threadId: event.threadId ?? thread.session?.threadId ?? thread.id,
|
||||
activeTurnId: event.turnId ?? null,
|
||||
createdAt: thread.session?.createdAt ?? now,
|
||||
updatedAt: now,
|
||||
|
||||
@@ -147,6 +147,8 @@ function AutoProjectBootstrap() {
|
||||
const api = useNativeApi();
|
||||
const { state, dispatch } = useStore();
|
||||
const bootstrappedRef = useRef(false);
|
||||
const threadsRef = useRef(state.threads);
|
||||
threadsRef.current = state.threads;
|
||||
|
||||
useEffect(() => {
|
||||
if (!api) return;
|
||||
@@ -173,7 +175,9 @@ function AutoProjectBootstrap() {
|
||||
const projects = await api.projects.list();
|
||||
dispatch({ type: "SYNC_PROJECTS", projects });
|
||||
|
||||
const hasThread = state.threads.some((thread) => thread.projectId === result.project.id);
|
||||
const hasThread = threadsRef.current.some(
|
||||
(thread) => thread.projectId === result.project.id,
|
||||
);
|
||||
if (hasThread) {
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user