fix(codex): recover session lifecycle from hooks

This commit is contained in:
William Valentin
2026-04-21 13:02:58 -07:00
parent 8b6ce8e628
commit d5154b8eec
4 changed files with 526 additions and 172 deletions
+2
View File
@@ -201,6 +201,8 @@ The `hooks/codex/` directory contains a TypeScript handler for Codex CLI telemet
- prompt-submit hooks map user prompts into the next `run.start` - prompt-submit hooks map user prompts into the next `run.start`
- usage payloads emit both `run.end.payload.usage` and a `metric.snapshot` event - usage payloads emit both `run.end.payload.usage` and a `metric.snapshot` event
The Codex handler persists lightweight session state across hook subprocesses. If Codex only delivers later-stage hooks for a session, the handler can recover by emitting synthetic `session.start`/`run.start` events before the first `run.end` or usage snapshot. Full-fidelity lifecycle tracking still depends on configuring Codex session lifecycle hooks, not just `notify`.
Sample Codex hook configuration lives in [hooks/codex/hooks.json](/home/will/lab/agentmon/hooks/codex/hooks.json). On the local Codex CLI version we checked (`0.116.0`), `notify` is confirmed. Online reports suggest prompt-submit hooks may appear as `userpromptsubmit` or `userPromptSubmit`, so the sample config includes those aliases. Sample Codex hook configuration lives in [hooks/codex/hooks.json](/home/will/lab/agentmon/hooks/codex/hooks.json). On the local Codex CLI version we checked (`0.116.0`), `notify` is confirmed. Online reports suggest prompt-submit hooks may appear as `userpromptsubmit` or `userPromptSubmit`, so the sample config includes those aliases.
The current Codex integration does not assume tool or subagent span hooks exist. If a newer Codex CLI exposes official tool/span hooks, they can be added separately without changing the run/session flow above. The current Codex integration does not assume tool or subagent span hooks exist. If a newer Codex CLI exposes official tool/span hooks, they can be added separately without changing the run/session flow above.
+314 -143
View File
@@ -1,16 +1,13 @@
#!/usr/bin/env node #!/usr/bin/env node
// handler.ts
import { randomUUID as randomUUID2 } from "node:crypto";
import { mkdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
import { homedir, hostname } from "node:os";
import { join } from "node:path";
// ../shared/lib.ts
import { randomUUID } from "node:crypto"; import { randomUUID } from "node:crypto";
import { hostname } from "node:os";
const INGEST_URL = process.env.AGENTMON_INGEST_URL || "http://localhost:8080";
const FRAMEWORK = process.env.AGENTMON_FRAMEWORK || "codex";
const HOST = process.env.AGENTMON_HOST || hostname();
const BATCH_SIZE = 10;
const FLUSH_MS = 2e3;
const FETCH_TIMEOUT_MS = 500;
let buffer = [];
let flushTimer = null;
let isFlushing = false;
const activeRuns = /* @__PURE__ */ new Map();
function isRecord(value) { function isRecord(value) {
return value !== null && typeof value === "object" && !Array.isArray(value); return value !== null && typeof value === "object" && !Array.isArray(value);
} }
@@ -50,22 +47,209 @@ function safeJSONStringify(value) {
return String(value); return String(value);
} }
} }
function buildEnvelope(framework, host, type, sessionKey, opts = {}) {
const correlation = {};
if (sessionKey) {
correlation.session_id = sessionKey;
}
if (opts.runId) {
correlation.run_id = opts.runId;
}
if (opts.spanId) {
correlation.span_id = opts.spanId;
}
if (opts.parentSpanId) {
correlation.parent_span_id = opts.parentSpanId;
}
const envelope = {
schema: { name: "agentmon.event", version: 1 },
event: {
id: randomUUID(),
type,
ts: (/* @__PURE__ */ new Date()).toISOString(),
source: {
framework,
client_id: host,
host
}
}
};
if (Object.keys(correlation).length > 0) {
envelope.correlation = correlation;
}
if (opts.attributes && Object.keys(opts.attributes).length > 0) {
envelope.attributes = opts.attributes;
}
if (opts.payload && Object.keys(opts.payload).length > 0) {
envelope.payload = opts.payload;
}
return envelope;
}
function createTransport(ingestUrl, opts) {
const batchSize = opts?.batchSize ?? 10;
const flushMs = opts?.flushMs ?? 2e3;
const fetchTimeoutMs = opts?.fetchTimeoutMs ?? 500;
let buffer = [];
let flushTimer = null;
let isFlushing = false;
async function postBatch(batch) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), fetchTimeoutMs);
try {
await fetch(`${ingestUrl}/v1/events`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(batch),
signal: controller.signal
});
} finally {
clearTimeout(timeout);
}
}
function scheduleFlush() {
if (!flushTimer) {
flushTimer = setTimeout(() => {
void flush2();
}, flushMs);
}
}
async function flush2() {
if (flushTimer) {
clearTimeout(flushTimer);
flushTimer = null;
}
if (isFlushing || buffer.length === 0) {
return;
}
isFlushing = true;
const batch = buffer.splice(0, batchSize);
try {
await postBatch(batch);
} catch {
console.debug(`[agentmon] failed to flush ${batch.length} events`);
} finally {
isFlushing = false;
if (buffer.length > 0) {
if (buffer.length >= batchSize) {
void flush2();
} else {
scheduleFlush();
}
}
}
}
function enqueue2(event) {
buffer.push(event);
if (buffer.length >= batchSize) {
void flush2();
} else {
scheduleFlush();
}
}
return { enqueue: enqueue2, flush: flush2 };
}
async function readStdin() {
return new Promise((resolve) => {
let data = "";
let done = false;
const timer = setTimeout(() => finish(data), 100);
const finish = (value) => {
if (done)
return;
done = true;
clearTimeout(timer);
resolve(value);
};
process.stdin.on("data", (chunk) => {
data += chunk;
});
process.stdin.on("end", () => finish(data));
process.stdin.on("error", () => finish(""));
});
}
// handler.ts
var INGEST_URL = process.env.AGENTMON_INGEST_URL || "http://localhost:8080";
var FRAMEWORK = process.env.AGENTMON_FRAMEWORK || "codex";
var HOST = process.env.AGENTMON_HOST || hostname();
var { enqueue, flush } = createTransport(INGEST_URL);
var STATE_DIR = join(homedir(), ".agentmon-state", "codex");
function ensureStateDir() {
try {
mkdirSync(STATE_DIR, { recursive: true });
} catch {
}
}
function loadState(sessionKey) {
try {
const raw = readFileSync(join(STATE_DIR, sessionKey + ".json"), "utf8");
return JSON.parse(raw);
} catch {
return {};
}
}
function saveState(sessionKey, state) {
ensureStateDir();
try {
writeFileSync(join(STATE_DIR, sessionKey + ".json"), JSON.stringify(state), "utf8");
} catch {
}
}
function clearState(sessionKey) {
try {
unlinkSync(join(STATE_DIR, sessionKey + ".json"));
} catch {
}
}
function getContext(input) {
return isRecord(input.context) ? input.context : {};
}
function getSessionRecord(input) {
return isRecord(input.session) ? input.session : {};
}
function getConversationRecord(input) {
return isRecord(input.conversation) ? input.conversation : {};
}
function getSessionKey(input) { function getSessionKey(input) {
const context = getContext(input);
const session = getSessionRecord(input);
const conversation = getConversationRecord(input);
return pickString( return pickString(
input.id, input.id,
input.session, input.session,
input.sessionId, input.sessionId,
input.session_id, input.session_id,
input.sessionID,
input.threadId, input.threadId,
input.thread_id, input.thread_id,
input.threadID,
input.chatId, input.chatId,
input.chat_id, input.chat_id,
input.conversationId, input.conversationId,
input.conversation_id input.conversation_id,
context.id,
context.session,
context.sessionKey,
context.sessionId,
context.session_id,
context.threadId,
context.thread_id,
context.conversationId,
context.conversation_id,
session.id,
session.key,
session.sessionId,
session.session_id,
conversation.id,
conversation.sessionId,
conversation.session_id,
conversation.conversationId,
conversation.conversation_id
); );
} }
function getUsage(input) { function getUsage(input) {
const usage = isRecord(input.usage) ? input.usage : isRecord(input.llm) ? input.llm : isRecord(input.tokens) ? input.tokens : isRecord(input.llm_usage) ? input.llm_usage : void 0; const context = getContext(input);
const usage = isRecord(input.usage) ? input.usage : isRecord(input.llm) ? input.llm : isRecord(input.tokens) ? input.tokens : isRecord(input.llm_usage) ? input.llm_usage : isRecord(context.usage) ? context.usage : isRecord(context.llm) ? context.llm : isRecord(context.tokens) ? context.tokens : isRecord(context.llm_usage) ? context.llm_usage : void 0;
if (!usage) if (!usage)
return void 0; return void 0;
const result = {}; const result = {};
@@ -92,64 +276,70 @@ function getUsage(input) {
return Object.keys(result).length > 0 ? result : void 0; return Object.keys(result).length > 0 ? result : void 0;
} }
function getModel(input) { function getModel(input) {
const context = getContext(input);
return pickString( return pickString(
input.model, input.model,
isRecord(input.llm) ? input.llm.model : void 0, isRecord(input.llm) ? input.llm.model : void 0,
isRecord(input.usage) ? input.usage.model : void 0 isRecord(input.usage) ? input.usage.model : void 0,
context.model,
isRecord(context.llm) ? context.llm.model : void 0,
isRecord(context.usage) ? context.usage.model : void 0
); );
} }
function buildEnvelope(type, sessionKey, opts = {}) { function getPrompt(input) {
const correlation = {}; const context = getContext(input);
if (sessionKey) { return pickString(
correlation.session_id = sessionKey; input.prompt,
} input.text,
if (opts.runId) { input.message,
correlation.run_id = opts.runId; context.prompt,
} context.text,
if (opts.spanId) { context.message
correlation.span_id = opts.spanId; );
}
if (opts.parentSpanId) {
correlation.parent_span_id = opts.parentSpanId;
}
const envelope = {
schema: { name: "agentmon.event", version: 1 },
event: {
id: randomUUID(),
type,
ts: (/* @__PURE__ */ new Date()).toISOString(),
source: {
framework: FRAMEWORK,
client_id: HOST,
host: HOST
}
}
};
if (Object.keys(correlation).length > 0) {
envelope.correlation = correlation;
}
if (opts.attributes && Object.keys(opts.attributes).length > 0) {
envelope.attributes = opts.attributes;
}
if (opts.payload && Object.keys(opts.payload).length > 0) {
envelope.payload = opts.payload;
}
return envelope;
} }
function scheduleFlush() { function getDuration(input) {
if (!flushTimer) { const context = getContext(input);
flushTimer = setTimeout(() => { return pickNumber(
void flush(); input.duration_ms,
}, FLUSH_MS); input.elapsed_ms,
} input.duration,
context.duration_ms,
context.elapsed_ms,
context.duration
);
} }
function enqueue(event) { function getNotificationType(input) {
buffer.push(event); const context = getContext(input);
if (buffer.length >= BATCH_SIZE) { return pickString(
void flush(); input.type,
} else { input.notification_type,
scheduleFlush(); input.event,
input.event_type,
context.type,
context.notification_type,
context.event,
context.event_type
);
}
function saveSessionState(sessionKey, state) {
if (!sessionKey) {
return;
} }
saveState(sessionKey, state);
}
function ensureSessionStarted(sessionKey, input, state) {
if (!sessionKey || state.sessionStarted) {
return state;
}
enqueue(buildEnvelope(FRAMEWORK, HOST, "session.start", sessionKey, {
attributes: {
synthetic: true,
recovered_from: "codex-hook"
}
}));
state.sessionStarted = true;
saveSessionState(sessionKey, state);
return state;
} }
function enqueueMetricSnapshot(sessionKey, runId, usage, input) { function enqueueMetricSnapshot(sessionKey, runId, usage, input) {
if (!usage) { if (!usage) {
@@ -160,33 +350,46 @@ function enqueueMetricSnapshot(sessionKey, runId, usage, input) {
if (model) { if (model) {
metrics.model = model; metrics.model = model;
} }
enqueue(buildEnvelope("metric.snapshot", sessionKey, { enqueue(buildEnvelope(FRAMEWORK, HOST, "metric.snapshot", sessionKey, {
runId, runId,
payload: { metrics } payload: { metrics }
})); }));
} }
function startRun(sessionKey, input) { function startRun(sessionKey, input, state, synthetic = false) {
const runId = randomUUID(); ensureSessionStarted(sessionKey, input, state);
const runId = randomUUID2();
if (sessionKey) { if (sessionKey) {
activeRuns.set(sessionKey, runId); state.runId = runId;
saveSessionState(sessionKey, state);
} }
enqueue(buildEnvelope("run.start", sessionKey, { enqueue(buildEnvelope(FRAMEWORK, HOST, "run.start", sessionKey, {
runId, runId,
attributes: { attributes: {
trigger: pickString(input.trigger_type, input.trigger, input.event, input.event_type) trigger: pickString(input.trigger_type, input.trigger, input.event, input.event_type),
...synthetic && { synthetic: true }
}, },
payload: { payload: {
prompt_preview: truncate(pickString(input.prompt, input.message, input.text), 200) prompt_preview: truncate(getPrompt(input), 200)
} }
})); }));
return runId; return runId;
} }
function endRun(sessionKey, runId, input, duration) { function ensureRunStarted(sessionKey, input, state) {
if (!sessionKey) {
return void 0;
}
if (state.runId) {
return state.runId;
}
return startRun(sessionKey, input, state, true);
}
function endRun(sessionKey, state, input, duration) {
const runId = state.runId;
if (!runId) { if (!runId) {
return; return;
} }
const usage = getUsage(input); const usage = getUsage(input);
enqueue(buildEnvelope("run.end", sessionKey, { enqueue(buildEnvelope(FRAMEWORK, HOST, "run.end", sessionKey, {
runId, runId,
payload: { payload: {
status: "success", status: "success",
@@ -196,96 +399,73 @@ function endRun(sessionKey, runId, input, duration) {
} }
})); }));
enqueueMetricSnapshot(sessionKey, runId, usage, input); enqueueMetricSnapshot(sessionKey, runId, usage, input);
} state.runId = void 0;
async function postBatch(batch) { saveSessionState(sessionKey, state);
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
try {
await fetch(`${INGEST_URL}/v1/events`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(batch),
signal: controller.signal
});
} finally {
clearTimeout(timeout);
}
}
async function flush() {
if (flushTimer) {
clearTimeout(flushTimer);
flushTimer = null;
}
if (isFlushing || buffer.length === 0) {
return;
}
isFlushing = true;
const batch = buffer.splice(0, BATCH_SIZE);
try {
await postBatch(batch);
} catch {
console.debug(`[agentmon] failed to flush ${batch.length} events`);
} finally {
isFlushing = false;
if (buffer.length > 0) {
if (buffer.length >= BATCH_SIZE) {
void flush();
} else {
scheduleFlush();
}
}
}
} }
async function handleSessionStart(input) { async function handleSessionStart(input) {
const sessionKey = getSessionKey(input) || randomUUID(); const sessionKey = getSessionKey(input) || randomUUID2();
enqueue(buildEnvelope("session.start", sessionKey)); const state = loadState(sessionKey);
startRun(sessionKey, input); if (!state.sessionStarted) {
enqueue(buildEnvelope(FRAMEWORK, HOST, "session.start", sessionKey));
state.sessionStarted = true;
}
startRun(sessionKey, input, state);
await flush(); await flush();
} }
async function handleSessionEnd(input) { async function handleSessionEnd(input) {
const sessionKey = getSessionKey(input); const sessionKey = getSessionKey(input);
const runId = sessionKey ? activeRuns.get(sessionKey) : void 0; const state = sessionKey ? loadState(sessionKey) : {};
const usage = getUsage(input); const usage = getUsage(input);
const duration = pickNumber(input.duration_ms, input.elapsed_ms, input.duration); const duration = getDuration(input);
endRun(sessionKey, runId, input, duration); ensureSessionStarted(sessionKey, input, state);
enqueue(buildEnvelope("session.end", sessionKey, { if (!state.runId && sessionKey) {
ensureRunStarted(sessionKey, input, state);
}
endRun(sessionKey, state, input, duration);
enqueue(buildEnvelope(FRAMEWORK, HOST, "session.end", sessionKey, {
payload: { payload: {
model: getModel(input), model: getModel(input),
...usage && { usage } ...usage && { usage }
} }
})); }));
enqueueMetricSnapshot(sessionKey, runId, usage, input); if (sessionKey) {
activeRuns.delete(sessionKey || ""); clearState(sessionKey);
}
await flush(); await flush();
} }
async function handlePromptSubmit(input) { async function handlePromptSubmit(input) {
const sessionKey = getSessionKey(input); const sessionKey = getSessionKey(input);
const runId = sessionKey ? activeRuns.get(sessionKey) : void 0; const state = sessionKey ? loadState(sessionKey) : {};
const duration = pickNumber(input.elapsed_ms, input.duration_ms, input.duration); const duration = getDuration(input);
const prompt = pickString(input.prompt, input.text, input.message); const prompt = getPrompt(input);
if (runId && prompt) { ensureSessionStarted(sessionKey, input, state);
endRun(sessionKey, runId, input, duration); if (state.runId && prompt) {
endRun(sessionKey, state, input, duration);
} }
startRun(sessionKey, input); startRun(sessionKey, input, state, !state.sessionStarted);
await flush(); await flush();
} }
async function handleNotification(input) { async function handleNotification(input) {
const sessionKey = getSessionKey(input); const sessionKey = getSessionKey(input);
const notificationType = pickString(input.type, input.notification_type, input.event, input.event_type); const state = sessionKey ? loadState(sessionKey) : {};
const notificationType = getNotificationType(input);
const usage = getUsage(input); const usage = getUsage(input);
const duration = pickNumber(input.duration_ms, input.elapsed_ms); const duration = getDuration(input);
ensureSessionStarted(sessionKey, input, state);
if (notificationType === "agent-turn-complete" || notificationType === "Done" || notificationType === "turn.complete") { if (notificationType === "agent-turn-complete" || notificationType === "Done" || notificationType === "turn.complete") {
const runId = sessionKey ? activeRuns.get(sessionKey) : void 0; if (!state.runId && sessionKey) {
endRun(sessionKey, runId, input, duration); ensureRunStarted(sessionKey, input, state);
if (pickString(input.prompt, input.message, input.text)) { }
startRun(sessionKey, input); endRun(sessionKey, state, input, duration);
if (getPrompt(input)) {
startRun(sessionKey, input, state);
} }
} else if (usage) { } else if (usage) {
enqueueMetricSnapshot(sessionKey, sessionKey ? activeRuns.get(sessionKey) : void 0, usage, input); enqueueMetricSnapshot(sessionKey, state.runId, usage, input);
} }
await flush(); await flush();
} }
const handler = async () => { var handler = async () => {
const args = process.argv.slice(2); const args = process.argv.slice(2);
const hookType = args[0] || "unknown"; const hookType = args[0] || "unknown";
let input = {}; let input = {};
@@ -319,13 +499,4 @@ const handler = async () => {
console.debug("[agentmon] handler error:", err); console.debug("[agentmon] handler error:", err);
} }
}; };
async function readStdin() {
return new Promise((resolve) => {
let data = "";
process.stdin.on("data", (chunk) => data += chunk);
process.stdin.on("end", () => resolve(data));
process.stdin.on("error", () => resolve(""));
setTimeout(() => resolve(data), 100);
});
}
handler(); handler();
+208 -27
View File
@@ -1,6 +1,8 @@
#!/usr/bin/env node #!/usr/bin/env node
import { randomUUID } from 'node:crypto'; import { randomUUID } from 'node:crypto';
import { hostname } from 'node:os'; import { mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs';
import { homedir, hostname } from 'node:os';
import { join } from 'node:path';
import { import {
type Dict, type Dict,
isRecord, isRecord,
@@ -18,28 +20,108 @@ const HOST = process.env.AGENTMON_HOST || hostname();
const { enqueue, flush } = createTransport(INGEST_URL); const { enqueue, flush } = createTransport(INGEST_URL);
const activeRuns = new Map<string, string>(); interface SessionState {
sessionStarted?: boolean;
runId?: string;
}
const STATE_DIR = join(homedir(), '.agentmon-state', 'codex');
function ensureStateDir() {
try {
mkdirSync(STATE_DIR, { recursive: true });
} catch {
// Ignore state directory creation failures so hooks stay best-effort.
}
}
function loadState(sessionKey: string): SessionState {
try {
const raw = readFileSync(join(STATE_DIR, sessionKey + '.json'), 'utf8');
return JSON.parse(raw) as SessionState;
} catch {
return {};
}
}
function saveState(sessionKey: string, state: SessionState) {
ensureStateDir();
try {
writeFileSync(join(STATE_DIR, sessionKey + '.json'), JSON.stringify(state), 'utf8');
} catch {
// Ignore state write failures so event emission can continue.
}
}
function clearState(sessionKey: string) {
try {
unlinkSync(join(STATE_DIR, sessionKey + '.json'));
} catch {
// Ignore state cleanup failures.
}
}
function getContext(input: Dict): Dict {
return isRecord(input.context) ? input.context : {};
}
function getSessionRecord(input: Dict): Dict {
return isRecord(input.session) ? input.session : {};
}
function getConversationRecord(input: Dict): Dict {
return isRecord(input.conversation) ? input.conversation : {};
}
function getSessionKey(input: Dict): string | undefined { function getSessionKey(input: Dict): string | undefined {
const context = getContext(input);
const session = getSessionRecord(input);
const conversation = getConversationRecord(input);
return pickString( return pickString(
input.id, input.id,
input.session, input.session,
input.sessionId, input.sessionId,
input.session_id, input.session_id,
input.sessionID,
input.threadId, input.threadId,
input.thread_id, input.thread_id,
input.threadID,
input.chatId, input.chatId,
input.chat_id, input.chat_id,
input.conversationId, input.conversationId,
input.conversation_id, input.conversation_id,
context.id,
context.session,
context.sessionKey,
context.sessionId,
context.session_id,
context.threadId,
context.thread_id,
context.conversationId,
context.conversation_id,
session.id,
session.key,
session.sessionId,
session.session_id,
conversation.id,
conversation.sessionId,
conversation.session_id,
conversation.conversationId,
conversation.conversation_id,
); );
} }
function getUsage(input: Dict): Dict | undefined { function getUsage(input: Dict): Dict | undefined {
const context = getContext(input);
const usage = isRecord(input.usage) ? input.usage : const usage = isRecord(input.usage) ? input.usage :
isRecord(input.llm) ? input.llm : isRecord(input.llm) ? input.llm :
isRecord(input.tokens) ? input.tokens : isRecord(input.tokens) ? input.tokens :
isRecord(input.llm_usage) ? input.llm_usage : undefined; isRecord(input.llm_usage) ? input.llm_usage :
isRecord(context.usage) ? context.usage :
isRecord(context.llm) ? context.llm :
isRecord(context.tokens) ? context.tokens :
isRecord(context.llm_usage) ? context.llm_usage : undefined;
if (!usage) return undefined; if (!usage) return undefined;
const result: Dict = {}; const result: Dict = {};
@@ -58,13 +140,79 @@ function getUsage(input: Dict): Dict | undefined {
} }
function getModel(input: Dict): string | undefined { function getModel(input: Dict): string | undefined {
const context = getContext(input);
return pickString( return pickString(
input.model, input.model,
isRecord(input.llm) ? input.llm.model : undefined, isRecord(input.llm) ? input.llm.model : undefined,
isRecord(input.usage) ? input.usage.model : undefined, isRecord(input.usage) ? input.usage.model : undefined,
context.model,
isRecord(context.llm) ? context.llm.model : undefined,
isRecord(context.usage) ? context.usage.model : undefined,
); );
} }
function getPrompt(input: Dict): string | undefined {
const context = getContext(input);
return pickString(
input.prompt,
input.text,
input.message,
context.prompt,
context.text,
context.message,
);
}
function getDuration(input: Dict): number | undefined {
const context = getContext(input);
return pickNumber(
input.duration_ms,
input.elapsed_ms,
input.duration,
context.duration_ms,
context.elapsed_ms,
context.duration,
);
}
function getNotificationType(input: Dict): string | undefined {
const context = getContext(input);
return pickString(
input.type,
input.notification_type,
input.event,
input.event_type,
context.type,
context.notification_type,
context.event,
context.event_type,
);
}
function saveSessionState(sessionKey: string | undefined, state: SessionState) {
if (!sessionKey) {
return;
}
saveState(sessionKey, state);
}
function ensureSessionStarted(sessionKey: string | undefined, input: Dict, state: SessionState): SessionState {
if (!sessionKey || state.sessionStarted) {
return state;
}
enqueue(buildEnvelope(FRAMEWORK, HOST, 'session.start', sessionKey, {
attributes: {
synthetic: true,
recovered_from: 'codex-hook',
},
}));
state.sessionStarted = true;
saveSessionState(sessionKey, state);
return state;
}
function enqueueMetricSnapshot(sessionKey: string | undefined, runId: string | undefined, usage: Dict | undefined, input: Dict) { function enqueueMetricSnapshot(sessionKey: string | undefined, runId: string | undefined, usage: Dict | undefined, input: Dict) {
if (!usage) { if (!usage) {
return; return;
@@ -82,26 +230,41 @@ function enqueueMetricSnapshot(sessionKey: string | undefined, runId: string | u
})); }));
} }
function startRun(sessionKey: string | undefined, input: Dict): string { function startRun(sessionKey: string | undefined, input: Dict, state: SessionState, synthetic = false): string {
ensureSessionStarted(sessionKey, input, state);
const runId = randomUUID(); const runId = randomUUID();
if (sessionKey) { if (sessionKey) {
activeRuns.set(sessionKey, runId); state.runId = runId;
saveSessionState(sessionKey, state);
} }
enqueue(buildEnvelope(FRAMEWORK, HOST, 'run.start', sessionKey, { enqueue(buildEnvelope(FRAMEWORK, HOST, 'run.start', sessionKey, {
runId, runId,
attributes: { attributes: {
trigger: pickString(input.trigger_type, input.trigger, input.event, input.event_type), trigger: pickString(input.trigger_type, input.trigger, input.event, input.event_type),
...(synthetic && { synthetic: true }),
}, },
payload: { payload: {
prompt_preview: truncate(pickString(input.prompt, input.message, input.text), 200), prompt_preview: truncate(getPrompt(input), 200),
}, },
})); }));
return runId; return runId;
} }
function endRun(sessionKey: string | undefined, runId: string | undefined, input: Dict, duration?: number) { function ensureRunStarted(sessionKey: string | undefined, input: Dict, state: SessionState): string | undefined {
if (!sessionKey) {
return undefined;
}
if (state.runId) {
return state.runId;
}
return startRun(sessionKey, input, state, true);
}
function endRun(sessionKey: string | undefined, state: SessionState, input: Dict, duration?: number) {
const runId = state.runId;
if (!runId) { if (!runId) {
return; return;
} }
@@ -117,24 +280,35 @@ function endRun(sessionKey: string | undefined, runId: string | undefined, input
}, },
})); }));
enqueueMetricSnapshot(sessionKey, runId, usage, input); enqueueMetricSnapshot(sessionKey, runId, usage, input);
state.runId = undefined;
saveSessionState(sessionKey, state);
} }
async function handleSessionStart(input: Dict) { async function handleSessionStart(input: Dict) {
const sessionKey = getSessionKey(input) || randomUUID(); const sessionKey = getSessionKey(input) || randomUUID();
const state = loadState(sessionKey);
if (!state.sessionStarted) {
enqueue(buildEnvelope(FRAMEWORK, HOST, 'session.start', sessionKey)); enqueue(buildEnvelope(FRAMEWORK, HOST, 'session.start', sessionKey));
startRun(sessionKey, input); state.sessionStarted = true;
}
startRun(sessionKey, input, state);
await flush(); await flush();
} }
async function handleSessionEnd(input: Dict) { async function handleSessionEnd(input: Dict) {
const sessionKey = getSessionKey(input); const sessionKey = getSessionKey(input);
const runId = sessionKey ? activeRuns.get(sessionKey) : undefined; const state = sessionKey ? loadState(sessionKey) : {};
const usage = getUsage(input); const usage = getUsage(input);
const duration = pickNumber(input.duration_ms, input.elapsed_ms, input.duration); const duration = getDuration(input);
endRun(sessionKey, runId, input, duration); ensureSessionStarted(sessionKey, input, state);
if (!state.runId && sessionKey) {
ensureRunStarted(sessionKey, input, state);
}
endRun(sessionKey, state, input, duration);
enqueue(buildEnvelope(FRAMEWORK, HOST, 'session.end', sessionKey, { enqueue(buildEnvelope(FRAMEWORK, HOST, 'session.end', sessionKey, {
payload: { payload: {
@@ -143,41 +317,48 @@ async function handleSessionEnd(input: Dict) {
}, },
})); }));
enqueueMetricSnapshot(sessionKey, runId, usage, input); if (sessionKey) {
clearState(sessionKey);
activeRuns.delete(sessionKey || ''); }
await flush(); await flush();
} }
async function handlePromptSubmit(input: Dict) { async function handlePromptSubmit(input: Dict) {
const sessionKey = getSessionKey(input); const sessionKey = getSessionKey(input);
const runId = sessionKey ? activeRuns.get(sessionKey) : undefined; const state = sessionKey ? loadState(sessionKey) : {};
const duration = pickNumber(input.elapsed_ms, input.duration_ms, input.duration); const duration = getDuration(input);
const prompt = pickString(input.prompt, input.text, input.message); const prompt = getPrompt(input);
if (runId && prompt) { ensureSessionStarted(sessionKey, input, state);
endRun(sessionKey, runId, input, duration);
if (state.runId && prompt) {
endRun(sessionKey, state, input, duration);
} }
startRun(sessionKey, input); startRun(sessionKey, input, state, !state.sessionStarted);
await flush(); await flush();
} }
async function handleNotification(input: Dict) { async function handleNotification(input: Dict) {
const sessionKey = getSessionKey(input); const sessionKey = getSessionKey(input);
const notificationType = pickString(input.type, input.notification_type, input.event, input.event_type); const state = sessionKey ? loadState(sessionKey) : {};
const notificationType = getNotificationType(input);
const usage = getUsage(input); const usage = getUsage(input);
const duration = pickNumber(input.duration_ms, input.elapsed_ms); const duration = getDuration(input);
ensureSessionStarted(sessionKey, input, state);
if (notificationType === 'agent-turn-complete' || notificationType === 'Done' || notificationType === 'turn.complete') { if (notificationType === 'agent-turn-complete' || notificationType === 'Done' || notificationType === 'turn.complete') {
const runId = sessionKey ? activeRuns.get(sessionKey) : undefined; if (!state.runId && sessionKey) {
endRun(sessionKey, runId, input, duration); ensureRunStarted(sessionKey, input, state);
}
endRun(sessionKey, state, input, duration);
if (pickString(input.prompt, input.message, input.text)) { if (getPrompt(input)) {
startRun(sessionKey, input); startRun(sessionKey, input, state);
} }
} else if (usage) { } else if (usage) {
enqueueMetricSnapshot(sessionKey, sessionKey ? activeRuns.get(sessionKey) : undefined, usage, input); enqueueMetricSnapshot(sessionKey, state.runId, usage, input);
} }
await flush(); await flush();
+1 -1
View File
@@ -8,7 +8,7 @@
"agentmon-codex-handler": "./handler.js" "agentmon-codex-handler": "./handler.js"
}, },
"scripts": { "scripts": {
"build": "npx esbuild handler.ts --platform=node --format=esm --outfile=handler.js" "build": "npx esbuild handler.ts --bundle --platform=node --format=esm --outfile=handler.js"
}, },
"dependencies": {}, "dependencies": {},
"devDependencies": { "devDependencies": {