diff --git a/hypaware-core/plugins-workspace/codex/src/backfill.js b/hypaware-core/plugins-workspace/codex/src/backfill.js index f9e76f89f..95d7df27f 100644 --- a/hypaware-core/plugins-workspace/codex/src/backfill.js +++ b/hypaware-core/plugins-workspace/codex/src/backfill.js @@ -32,7 +32,7 @@ import { setIfString, stampUsageOnLastAssistant, textBlocksFromContent, - toolResultBlockFromPayload, + toolResultMessageFromPayload, toolUseBlockFromPayload, } from './response-items.js' import { isPlainObject, stringValue } from 'hypaware/core/util' @@ -1261,11 +1261,9 @@ function projectedMessageFromItem(item) { break } case 'function_call_output': - case 'custom_tool_call_output': { - const block = toolResultBlockFromPayload(payload) - message = block ? { role: 'tool', content: [block] } : undefined + case 'custom_tool_call_output': + message = toolResultMessageFromPayload(payload) break - } case 'reasoning': message = reasoningMessageFromPayload(payload) break diff --git a/hypaware-core/plugins-workspace/codex/src/exchange-projector.js b/hypaware-core/plugins-workspace/codex/src/exchange-projector.js index 36f19e853..f6f185f76 100644 --- a/hypaware-core/plugins-workspace/codex/src/exchange-projector.js +++ b/hypaware-core/plugins-workspace/codex/src/exchange-projector.js @@ -18,7 +18,7 @@ import { setIfString, stampUsageOnLastAssistant, textBlocksFromContent, - toolResultBlockFromPayload, + toolResultMessageFromPayload, toolUseBlockFromPayload, } from './response-items.js' import { canonicalJson, isPlainObject, parseMaybeJson, sha256Hex, stringValue } from 'hypaware/core/util' @@ -572,8 +572,8 @@ function responsesInputMessages(input, skipInputIndex = -1) { continue } if (itemType === 'function_call_output' || itemType === 'custom_tool_call_output') { - const block = toolResultBlockFromPayload(item) - if (block) out.push({ role: 'tool', content: [block] }) + const message = toolResultMessageFromPayload(item) + if (message) out.push(message) continue } if (itemType === 'reasoning') { diff --git a/hypaware-core/plugins-workspace/codex/src/response-items.js b/hypaware-core/plugins-workspace/codex/src/response-items.js index 9102608e1..72165905b 100644 --- a/hypaware-core/plugins-workspace/codex/src/response-items.js +++ b/hypaware-core/plugins-workspace/codex/src/response-items.js @@ -81,44 +81,88 @@ export function toolUseBlockFromPayload(payload) { } /** - * `function_call_output` / `custom_tool_call_output` become a - * `tool_result` block referencing the originating `call_id`. The + * `function_call_output` / `custom_tool_call_output` become a tool message + * with a `tool_result` block referencing the originating `call_id`. The * gateway maps it to a `tool_result` part and back-fills the tool name * from the earlier call. * * @param {Record} payload - * @returns {JsonObject | undefined} + * @returns {AiGatewayProjectedMessage | undefined} */ -export function toolResultBlockFromPayload(payload) { +export function toolResultMessageFromPayload(payload) { const callId = stringValue(payload.call_id) ?? stringValue(payload.id) if (!callId) return undefined - const text = toolOutputText(payload.output) + const output = payload.output + const text = toolOutputText(output) + const directExitCode = integerExitCode(output) + const parsed = directExitCode === undefined + ? Array.isArray(output) ? output : parseToolOutputJson(text) + : undefined + const items = Array.isArray(parsed) ? parsed : undefined + const isError = items + ? scriptOutputError(toolOutputItemText(items[0])?.slice(0, 8192) ?? '', true) + : toolOutputError(directExitCode ?? integerExitCode(parsed), text) + /** @type {JsonObject} */ - const block = { type: 'tool_result', tool_use_id: callId, is_error: toolOutputError(payload.output, text) } + const block = { type: 'tool_result', tool_use_id: callId, is_error: isError } if (text !== undefined) block.content = text - return block + /** @type {AiGatewayProjectedMessage} */ + const message = { role: 'tool', content: [block] } + // A script can handle a failed command. Store a single command's exit code + // separately from the script status. + if (isError !== null && items?.length === 2) { + const commandExitCode = integerExitCode(parseToolOutputJson(toolOutputItemText(items[1]))) + if (commandExitCode !== undefined) message.attributes = { codex: { exit_code: commandExitCode } } + } + return message } /** - * Read only a recognized outer outcome. A successful script can contain a - * handled inner failure; arbitrary output and still-running tools are unknown. * Bound the extra parse work independently of the captured result's size. - * @param {unknown} output + * @param {string | undefined} text + */ +function parseToolOutputJson(text) { + if (text === undefined || text.length > 65536) return undefined + const first = text.trimStart()[0] + if (first === '{' || first === '[') return parseMaybeJson(text) + return undefined +} + +/** @param {unknown} value */ +function integerExitCode(value) { + const code = isPlainObject(value) ? value.exit_code : undefined + return typeof code === 'number' && Number.isInteger(code) ? code : undefined +} + +/** @param {unknown} item */ +function toolOutputItemText(item) { + if (isPlainObject(item) && (item.type === 'input_text' || item.type === 'output_text')) { + return stringValue(item.text) + } + return undefined +} + +/** + * Read only a recognized outer outcome. Arbitrary output and still-running + * tools are unknown. + * @param {number | undefined} exitCode * @param {string | undefined} text * @returns {boolean | null} */ -function toolOutputError(output, text) { - if (isPlainObject(output) && Number.isInteger(output.exit_code)) return output.exit_code !== 0 +function toolOutputError(exitCode, text) { + if (exitCode !== undefined) return exitCode !== 0 if (text === undefined) return null - if (text.length <= 65536 && text.trimStart().startsWith('{')) { - const value = parseMaybeJson(text) - if (isPlainObject(value) && Number.isInteger(value.exit_code)) return value.exit_code !== 0 - } const header = text.slice(0, 8192) const command = /^(?:Chunk ID:[^\r\n]*\r?\n)?(?:Wall time:[^\r\n]*\r?\n)?Process exited with code (-?\d+)\r?\n(?:Final output:|Output:)/.exec(header) if (command) return Number(command[1]) !== 0 - if (/^Script failed\r?\nScript error:/.test(header)) return true - if (/^Script completed\r?\n(?:Wall time:|Output:)/.test(header)) return false + return scriptOutputError(header) +} + +/** @param {string} header */ +function scriptOutputError(header, contentItems = false) { + if (contentItems && /^Script failed(?:\r?\n|$)/.test(header)) return true + if (/^Script failed\r?\n(?:Script error:|Wall time[: \t][^\r\n]*\r?\nOutput:)/.test(header)) return true + if (/^Script completed\r?\n(?:Wall time[: \t]|Output:)/.test(header)) return false return null } diff --git a/test/plugins/codex-tool-outcomes.test.js b/test/plugins/codex-tool-outcomes.test.js index 27051c835..edaf5458e 100644 --- a/test/plugins/codex-tool-outcomes.test.js +++ b/test/plugins/codex-tool-outcomes.test.js @@ -1,7 +1,7 @@ // @ts-check import test from 'node:test' import assert from 'node:assert/strict' -import { toolResultBlockFromPayload } from '../../hypaware-core/plugins-workspace/codex/src/response-items.js' +import { toolResultMessageFromPayload } from '../../hypaware-core/plugins-workspace/codex/src/response-items.js' import { aiGatewayRowsFromProjectedExchange, computeMessageId } from '../../hypaware-core/plugins-workspace/ai-gateway/src/message_projector.js' import { createCodexExchangeProjector } from '../../hypaware-core/plugins-workspace/codex/src/exchange-projector.js' @@ -12,23 +12,33 @@ const cases = [ { name: 'command terminal footer', output: 'Chunk ID: abc\nWall time: 0.1 seconds\nProcess exited with code 7\nFinal output:\nfailed', error: true }, { name: 'successful command footer with misleading content', output: 'Process exited with code 0\nFinal output:\nScript failed\nScript error: an old failure', error: false }, { name: 'failed script envelope', output: 'Script failed\nScript error: Error: synthetic failure', error: true }, + { name: 'flattened failed script envelope', output: 'Script failed\nWall time 0.0 seconds\nOutput:\nScript error:\nError: fixture', error: true }, { name: 'successful wrapper handles inner failure', output: 'Script completed\nWall time: 0.1 seconds\nOutput:\n{"exit_code":1,"handled":true}', error: false }, { name: 'unknown text', output: 'directory listing', error: null }, { name: 'running process', output: 'Process running with session ID 123\nOutput:\n', error: null }, { name: 'nested inner result has no outer outcome', output: '{"inner":{"exit_code":1},"handled":true}', error: null }, { name: 'unrecognized exit code', output: '{"exit_code":"failed"}', error: null }, { name: 'absent result', output: null, error: null }, + { + name: 'later array items cannot provide a script outcome', + output: [ + { type: 'input_text', text: 'Unrecognized output' }, + { type: 'input_text', text: 'Script failed\nScript error:\nError: quoted example' }, + { type: 'input_text', text: '{"exit_code":3}' }, + ], + error: null, + }, ] for (const scenario of cases) { test(`Codex tool outcome: ${scenario.name}`, () => { const payload = { type: 'function_call_output', call_id: 'call-shared', output: scenario.output } - const block = toolResultBlockFromPayload(payload) - assert.ok(block) - assert.equal(block.is_error, scenario.error) + const message = toolResultMessageFromPayload(payload) + assert.ok(message && Array.isArray(message.content)) + assert.equal(message.content[0].is_error, scenario.error) const rows = aiGatewayRowsFromProjectedExchange({ provider: 'openai', session_id: 'synthetic-session', conversation_id: 'synthetic-thread', - messages: [{ role: 'tool', content: [block] }], + messages: [message], }) assert.equal(rows.length, 1) assert.equal(rows[0].is_error ?? null, scenario.error === true ? true : null) @@ -39,11 +49,11 @@ for (const scenario of cases) { } test('scoped tool identities keep the same call id in unrelated sessions and verified copies separate', () => { - const block = toolResultBlockFromPayload({ call_id: 'same-call', output: '{"exit_code":1}' }) - assert.ok(block) + const message = toolResultMessageFromPayload({ call_id: 'same-call', output: '{"exit_code":1}' }) + assert.ok(message) const rows = ['session-a', 'session-b'].flatMap(session_id => aiGatewayRowsFromProjectedExchange({ provider: 'openai', session_id, conversation_id: 'thread', - messages: [{ role: 'tool', content: [block] }], + messages: [message], }, { gatewayId: 'synthetic-gateway' })) const withCopy = [...rows, { ...rows[0] }] assert.equal(new Set(withCopy.map(row => row.tool_call_id)).size, 1, 'global call-id distinct loses an attempt') @@ -70,3 +80,57 @@ test('live Codex outcome enrichment preserves the pre-upgrade message and part I assert.equal(row.part_id, `${expected}#0`) assert.equal(/** @type {any} */ (row.status).tool_status, 'error') }) + +// The three Codex Desktop results reported in #3029. +const scriptResults = [ + { + name: 'failed script', + header: 'Script failed\nWall time 0.0 seconds\nOutput:\n', + body: 'Script error:\nReferenceError: console is not defined\n at exec_main.mjs:1:1', + status: 'error', + }, + { + name: 'completed script with a command that exited 3', + header: 'Script completed\nWall time 0.1 seconds\nOutput:\n', + body: JSON.stringify({ chunk_id: 'f48a9d', exit_code: 3, output: 'hyptest-fail\n' }), + status: 'success', + exitCode: 3, + }, + { + name: 'completed script with a command that exited 0', + header: 'Script completed\nWall time 0.1 seconds\nOutput:\n', + body: JSON.stringify({ chunk_id: '62d272', exit_code: 0, output: 'hyptest-ok\n' }), + status: 'success', + exitCode: 0, + }, +] + +for (const scenario of scriptResults) { + for (const serialized of [false, true]) { + test(`Codex exec content items: ${scenario.name} (${serialized ? 'JSON string' : 'array'})`, async () => { + const items = [ + { type: 'input_text', text: scenario.header }, + { type: serialized ? 'output_text' : 'input_text', text: scenario.body }, + ] + const contentText = JSON.stringify(items) + const projection = await createCodexExchangeProjector().project(/** @type {any} */ ({ + exchange_id: 'script-exchange', provider: 'openai', path: '/v1/responses', + ts_start: '2026-11-18T02:14:32Z', request_headers: '{}', stream_events: [], + request_body: JSON.stringify({ model: 'synthetic', input: [ + { type: 'custom_tool_call_output', call_id: 'call-exec', output: serialized ? contentText : items }, + ] }), + response_body: JSON.stringify({ id: 'synthetic-response', output_text: 'done' }), + }), /** @type {any} */ ({ log: { info() {} } })) + assert.ok(projection && 'messages' in projection) + const row = aiGatewayRowsFromProjectedExchange(projection).find(row => row.tool_call_id === 'call-exec') + assert.ok(row) + assert.equal(/** @type {any} */ (row.status)?.tool_status, scenario.status) + assert.equal(row.is_error, scenario.status === 'error' ? true : undefined) + assert.equal(/** @type {any} */ (row.attributes)?.codex?.exit_code, scenario.exitCode) + assert.equal(row.content_text, contentText) + assert.equal(row.message_id, computeMessageId(projection.conversation_id ?? projection.session_id, 'tool', [ + { type: 'tool_result', tool_use_id: 'call-exec', content: contentText }, + ])) + }) + } +}