Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: pin a turn to the connection that owns its thread

[._.]/ Adam Eivyfb44b0652c73
diff --git a/server/lib/codexAccount.js b/server/lib/codexAccount.js
index 89abdfdf49..588c62d549 100644
--- a/server/lib/codexAccount.js
+++ b/server/lib/codexAccount.js
@@ -150,7 +150,10 @@ export const redactCodexPayload = (value, depth = 0) => {
*/
export const redactCodexMessage = (value) => {
if (typeof value !== 'string' || value.trim() === '') return null;
- const masked = value
+ // Clamped BEFORE the patterns run, so the scan is bounded by a constant no
+ // matter what an upstream error decides to quote at us.
+ const clamped = value.length > 400 ? `${value.slice(0, 400)}…` : value;
+ return clamped
// `key=<value>` / `key: <value>` where the key is credential-shaped AND the
// value LOOKS like a credential — long, and not plain prose. Requiring both
// is what keeps "The secret: sauce" intact while masking
@@ -167,7 +170,6 @@ export const redactCodexMessage = (value) => {
// A bare vendor key, which carries no label at all to key on.
.replace(/\b(sk|pk|rk)-[A-Za-z0-9._-]{8,}/g, REDACTED)
.replace(/\beyJ[A-Za-z0-9._-]{16,}/g, REDACTED);
- return masked.length > 400 ? `${masked.slice(0, 400)}…` : masked;
};
const trimmedString = (value) => (typeof value === 'string' && value.trim() !== '' ? value.trim() : null);
diff --git a/server/lib/codexTurn.js b/server/lib/codexTurn.js
index 18a2dae58f..e9152fe790 100644
--- a/server/lib/codexTurn.js
+++ b/server/lib/codexTurn.js
@@ -389,10 +389,16 @@ export const applyCodexTurnEvent = (acc, method, params) => {
return true;
case CODEX_TURN_NOTIFICATIONS.turnCompleted: {
const turn = params.turn;
- // Once the id is latched, a completion must name it. An anonymous
- // `turn/completed` would otherwise finish somebody else's turn — and hand
- // this caller the partial text streamed so far as a success.
- if (acc.turnId && turn?.id !== acc.turnId) return false;
+ // Once the id is latched, a completion must name it, or it cannot finish
+ // this turn — it would otherwise hand the caller whatever streamed so far
+ // as a success. A frame naming a DIFFERENT turn is somebody else's and is
+ // ignored; one naming NO turn is malformed, and fails this turn now rather
+ // than leaving it to wait out its full deadline.
+ if (acc.turnId && typeof turn?.id === 'string' && turn.id !== acc.turnId) return false;
+ if (acc.turnId && typeof turn?.id !== 'string') {
+ acc.status = 'malformed';
+ return true;
+ }
// A frame with no status is MALFORMED, and defaulting it to 'completed'
// would hand the caller whatever text had streamed so far as a finished
// answer. `finalizeCodexTurn` errors on anything that is not 'completed',
diff --git a/server/lib/codexTurn.test.js b/server/lib/codexTurn.test.js
index fe47e22116..409ed061bf 100644
--- a/server/lib/codexTurn.test.js
+++ b/server/lib/codexTurn.test.js
@@ -266,17 +266,26 @@ describe('turn projection', () => {
expect(finalizeCodexTurn(acc).text).toBe('message one. message two.');
});
- it('refuses an anonymous completion once the turn id is latched', () => {
- // An unnamed `turn/completed` would otherwise finish somebody else's turn and
- // hand this caller the partial text streamed so far as a success.
+ it('fails on an anonymous completion once the turn id is latched', () => {
+ // An unnamed `turn/completed` cannot finish this turn — it would hand the
+ // caller the partial text streamed so far as a success. Terminal rather than
+ // ignored, so the call fails now instead of waiting out its full deadline.
const acc = createTurnAccumulator({ threadId: 't', turnId: 'turn-1' });
delta(acc, '{"answer":"hal');
- expect(complete(acc, { status: 'completed' })).toBe(false);
+ expect(complete(acc, { status: 'completed' })).toBe(true);
- complete(acc, { id: 'turn-1', status: 'failed', error: { message: 'nope' } });
expect(finalizeCodexTurn(acc).text).toBeUndefined();
});
+ it('still ignores a completion that names a different turn', () => {
+ const acc = createTurnAccumulator({ threadId: 't', turnId: 'turn-1' });
+ delta(acc, 'mine');
+ expect(complete(acc, { id: 'turn-2', status: 'completed' })).toBe(false);
+
+ complete(acc, { id: 'turn-1', status: 'completed' });
+ expect(finalizeCodexTurn(acc).text).toBe('mine');
+ });
+
it('scrubs a credential quoted by a failed turn before it becomes an error', () => {
// The message becomes a thrown error's text, which aiProvider logs, reports
// over `ai:status`, and returns to the caller as `{ error }`.
diff --git a/server/services/codexAppServer.js b/server/services/codexAppServer.js
index dff25e6461..cd5f969b3a 100644
--- a/server/services/codexAppServer.js
+++ b/server/services/codexAppServer.js
@@ -769,11 +769,17 @@ export async function runCodexTextTurn({
const { effort: resolvedEffort, clamped, reason } = resolveCodexEffort(effort, cachedModelEntry(model));
if (clamped) console.log(`⚠️ Codex effort clamped: ${reason}`);
- const started = await call(CODEX_TURN_RPC.threadStart, {
+ // The whole turn is pinned to ONE connection, resolved up front rather than
+ // per request. A thread lives inside a single app-server process, so a
+ // mid-turn reconnect could not continue it — and `call`'s reconnect would
+ // silently send this turn's frames to a child that has never heard of its
+ // thread id.
+ const owner = await connect();
+ const started = await sendRequest(owner, CODEX_TURN_RPC.threadStart, {
...CODEX_TEXT_THREAD_CONFIG,
cwd,
...(model ? { model } : {}),
- });
+ }, REQUEST_TIMEOUT_MS);
const threadId = typeof started?.thread?.id === 'string' ? started.thread.id : null;
if (!threadId) {
throw codexError(CODEX_ERROR_CODES.protocol, 'Codex started a thread with no id.');
@@ -807,7 +813,7 @@ export async function runCodexTextTurn({
signal?.addEventListener('abort', onAbort, { once: true });
try {
- const response = await call(CODEX_TURN_RPC.turnStart, {
+ const response = await sendRequest(owner, CODEX_TURN_RPC.turnStart, {
threadId,
input: [{ type: 'text', text: prompt }],
sandboxPolicy: { ...CODEX_TEXT_TURN_SANDBOX },
@@ -841,12 +847,11 @@ export async function runCodexTextTurn({
activeTurns.delete(threadId);
// Best-effort: tell the app-server to stop working a turn PortOS has
// abandoned, so a cancelled request does not keep burning subscription
- // quota. Sent on the LIVE connection directly rather than through `call`,
- // which would `connect()` — spawning a whole new app-server to interrupt a
- // thread that only existed in the one that just died.
- const live = connection;
- if (acc.status === 'inProgress' && acc.turnId && live && !live.closed) {
- sendRequest(live, CODEX_TURN_RPC.turnInterrupt, { threadId, turnId: acc.turnId })
+ // quota. Sent to the connection that OWNS the thread, and only while it is
+ // still alive — routing it through `call` would reconnect, spawning a whole
+ // new app-server to interrupt a thread that only existed in the dead one.
+ if (acc.status === 'inProgress' && acc.turnId && !owner.closed) {
+ sendRequest(owner, CODEX_TURN_RPC.turnInterrupt, { threadId, turnId: acc.turnId })
.catch((err) => console.error(`❌ Codex turn interrupt failed: ${err.message}`));
}
}