Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2105,8 +2105,9 @@ wording. T3 Code records whether a person or an agent created each message, so
the sessions it drives are read from its state (`$T3CODE_HOME`, or `~/.t3`)
and nowhere else. Other Claude Code sessions count the turns Claude Code marks
as a human's, with its reminders dropped and a slash command spelled as typed;
other Codex sessions count the content Codex marks as the user's, outside
headless (`codex exec`) and sub-agent sessions. A log or record in a shape it
other Codex sessions count the content Codex marks as the user's (before 0.162,
which marks none, the parts but its environment, shell commands, and AGENTS.md instructions),
outside headless (`codex exec`) and sub-agent sessions. A log or record in a shape it
does not know fails the command rather than being skipped. Output goes to
stdout only; nothing is stored or posted.

Expand Down
30 changes: 0 additions & 30 deletions quest/m1/codex-146-messages.md

This file was deleted.

3 changes: 2 additions & 1 deletion src/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1577,7 +1577,8 @@ export function main(argv: string[], out: Output): number | Promise<number> {
"its state ($T3CODE_HOME, or ~/.t3), which records whether a person or an agent sent each message. " +
"Other Claude Code sessions, from $CLAUDE_CONFIG_DIR (or ~/.config/claude and ~/.claude), count a turn " +
"Claude Code marks as a human's; other Codex sessions, from $CODEX_HOME (or ~/.codex), count the " +
"content Codex marks as the user's, outside headless and sub-agent sessions. The repository is the " +
"content Codex marks as the user's (before 0.162, which marks none, the parts but its environment, shell commands, and " +
"AGENTS.md instructions), outside headless and sub-agent sessions. The repository is the " +
"checkout's GitHub origin, else the directory. Output goes to stdout only, except a note on stderr " +
"naming each Claude Code session whose malformed lines, such as a partial record a crashed write left, " +
"were skipped and how many; nothing is stored or posted. Each answer a person gave an agent's blocking question (Claude Code's AskUserQuestion, " +
Expand Down
20 changes: 19 additions & 1 deletion src/cli/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ export function readCodexMessages(path: string, since: number, repository: (dir:
const entries: Entry[] = [];
let session: string | undefined;
let typed = false;
let unmarked = false;
let where = "";
let skill: string | null = null;
const asked = new Map<string, CodexQuestion[] | null>();
Expand All @@ -346,6 +347,7 @@ export function readCodexMessages(path: string, since: number, repository: (dir:
throw new Error(`${path} does not start with a Codex session_meta record`);
if (typeof payload.source !== "string" || payload.source === "exec") return false;
typed = payload.originator !== T3_CODEX;
unmarked = writesNoProvenance(payload.cli_version);
session = payload.id;
const url = (payload.git as Record<string, unknown> | undefined)?.repository_url;
const repo = typeof url === "string" ? githubRepository(url) : null;
Expand Down Expand Up @@ -448,6 +450,9 @@ export function readCodexMessages(path: string, since: number, repository: (dir:
let sources: unknown[];
if (Array.isArray(provenance)) {
sources = provenance.map((each) => (each as { provenance?: { type?: unknown } } | null)?.provenance?.type);
} else if (unmarked && meta?.content_item_kinds === undefined) {
// Codex before 0.162 marks no part, so the parts it adds are told apart by how they open, and the rest is typed.
sources = content.map((part) => (CODEX_WRAPPER.test(part?.text as string) ? "harness" : "user"));
} else {
// Codex 0.162's TUI marks each part with a kind instead. A compaction replays earlier turns, already read
// where they were typed, with every kind unknown; a part whose kind says nothing otherwise cannot be told
Expand Down Expand Up @@ -487,10 +492,21 @@ export function codexSkill(text: string): string | null {
return /<name>\s*([^<]+?)\s*<\/name>/.exec(text)?.[1] ?? null;
}

/** Whether a Codex version predates 0.162, the first seen to mark who wrote each part of a user turn. */
function writesNoProvenance(version: unknown): boolean {
const [major, minor] =
/^(\d+)\.(\d+)\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/.exec(String(version))?.slice(1, 3).map(Number) ??
[];
return major === 0 && minor !== undefined && minor < 162;
}

/** How the parts Codex before 0.162 adds to a user turn open: the environment, the AGENTS.md instructions, and a `!` command. */
const CODEX_WRAPPER = /^\s*(?:<environment_context>|# AGENTS\.md instructions\b|<user_shell_command>)/;

/** Who Codex says wrote a part of a user turn: the person, or the instructions, harness, or skill it added. */
const CODEX_SOURCES = new Set(["user", "agents_md", "harness", "skills"]);

/** The content kinds Codex 0.162 gives the parts it adds to a user turn, never typed by the person. */
/** The content kinds Codex 0.162 gives the parts of a user turn that are not text the person typed to the agent. */
const CODEX_INJECTED = new Set([
"agents_md.instructions",
"environments.environment_context",
Expand All @@ -503,6 +519,8 @@ const CODEX_INJECTED = new Set([
"tools.deferred_namespaces",
"plugins.recommendations",
"model.base_instructions",
// A command the person ran with `!`, which Codex shows its agent with the output: a command, not a message.
"shell.user_command",
]);

/** T3 Code's agent names, as the harness names `quest usage` reports. */
Expand Down
64 changes: 64 additions & 0 deletions test/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ describe("readCodexMessages from Codex 0.162's TUI", () => {
]),
kindTurn("2026-11-06T10:01:00Z", [["fix it", "user.text"]]),
kindTurn("2026-11-06T10:02:00Z", [["replayed", "unknown"]]),
kindTurn("2026-11-06T10:03:00Z", [
["<user_shell_command>ls</user_shell_command>", "shell.user_command"],
]),
),
);
expect(readCodexMessages(path, SINCE, plain).map((m) => m.kind === "message" && m.text)).toEqual(["fix it"]);
Expand All @@ -216,6 +219,67 @@ describe("readCodexMessages from Codex 0.162's TUI", () => {
});
});

describe("readCodexMessages from Codex 0.146's TUI", () => {
/** A user turn that marks nothing but its turn, as codex-tui 0.146 writes. */
const bareTurn = (timestamp: string, parts: string[]) => ({
type: "response_item",
timestamp,
payload: {
type: "message",
role: "user",
content: parts.map((text) => ({ type: "input_text", text })),
internal_chat_message_metadata_passthrough: { turn_id: "t" },
},
});
const meta = (version: string) => codexMeta({ source: "cli", originator: "codex-tui", cli_version: version });

test("reads typed text and sets the parts Codex adds aside", () => {
const path = join(tempDir(), "rollout.jsonl");
writeFileSync(
path,
lines(
meta("0.146.0"),
bareTurn("2026-11-06T10:00:00Z", [
"# AGENTS.md instructions for /work/proj\n\n<INSTRUCTIONS>",
"<environment_context>\n <cwd>/work/proj</cwd>",
]),
bareTurn("2026-11-06T10:00:30Z", ["<environment_context>\n <current_date>2026-11-06</current_date>"]),
bareTurn("2026-11-06T10:00:40Z", [
"<user_shell_command>\n<command>ls</command>\n</user_shell_command>",
]),
bareTurn("2026-11-06T10:01:00Z", ["fix it"]),
),
);
expect(readCodexMessages(path, SINCE, plain).map((m) => m.kind === "message" && m.text)).toEqual(["fix it"]);
});

test("reads a prerelease or build version before 0.162", () => {
for (const head of [meta("0.146.0-alpha.1"), meta("0.146.0+build.5")]) {
const path = join(tempDir(), "rollout.jsonl");
writeFileSync(path, lines(head, bareTurn("2026-11-06T10:01:00Z", ["fix it"])));
expect(readCodexMessages(path, SINCE, plain).map((m) => m.kind === "message" && m.text)).toEqual([
"fix it",
]);
}
});

test("fails on a turn without provenance from a version that writes it, or one with no version", () => {
for (const head of [
meta("0.162.1"),
meta("0.167.0-alpha.6"),
meta("1.0.0"),
meta("unknown"),
meta("0.146.not-a-version"),
meta("0.146.0 "),
codexMeta({}),
]) {
const path = join(tempDir(), "rollout.jsonl");
writeFileSync(path, lines(head, bareTurn("2026-11-06T10:01:00Z", ["fix it"])));
expect(() => readCodexMessages(path, SINCE, plain)).toThrow(/provenance/);
}
});
});

/** A Claude Code answer to `AskUserQuestion`, as the tool result's record holds it. */
const claudeAnswer = (timestamp: string, answers: object, extra: object = {}) => ({
type: "user",
Expand Down