From 20a69b7e47833697615fbbbf5ab7b7b9da489cde Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 6 Nov 2026 00:00:21 +0000 Subject: [PATCH] Forward recorded Codex turn timestamps to chat history --- docs/notes/codex-message-timestamps.md | 5 ++++ internal/codex/chat.go | 22 ++++++++++---- internal/codex/timestamps_test.go | 41 ++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 docs/notes/codex-message-timestamps.md create mode 100644 internal/codex/timestamps_test.go diff --git a/docs/notes/codex-message-timestamps.md b/docs/notes/codex-message-timestamps.md new file mode 100644 index 00000000..81e37e5c --- /dev/null +++ b/docs/notes/codex-message-timestamps.md @@ -0,0 +1,5 @@ +# Codex transcript timestamps + +Codex thread history previously flattened items without setting `ChatMessage.CreatedAt`, so clients correctly omitted timestamps. The installed App Server schema records `Turn.startedAt` in epoch seconds; individual historical ThreadItems have no creation timestamp. Forward the recorded turn start, converted to milliseconds, to both user and assistant rows (including failed-turn detail). This is turn-level timing, not an exact per-item send time. Missing, negative and out-of-range values remain absent; never substitute server read/render time. Pagination/order and client message identities are unchanged. + +Verified against the installed Codex generated schema (`v2/ThreadTurnsListResponse.json`, `ServerNotification.json`). Tests exercise real JSON history decoding, pagination order, user/assistant identity, repeated reads, in-progress turns, legacy missing times and invalid/overflowing values. Focused Codex tests passed. Deployment must wait for all shared runs and chat turns to settle; the first Codex bridge upgrade cannot retroactively preserve outstanding connection-bound approvals/questions. diff --git a/internal/codex/chat.go b/internal/codex/chat.go index 906fbede..60dc0b39 100644 --- a/internal/codex/chat.go +++ b/internal/codex/chat.go @@ -654,12 +654,13 @@ func (c *Chat) Messages(ctx context.Context, id string, limit int, cursor string turn := result.Data[index] for _, item := range turn.Items { if msg, ok := item.chatMessage(id); ok { + msg.CreatedAt = turn.createdAtMillis() messages = append(messages, msg) } } if turn.ID != "" && rawText(turn.Error) != "" { part := item{ID: turn.ID + "#error", Type: "Codex turn", Status: "failed", Error: turn.Error, Input: map[string]any{"turn_id": turn.ID}}.toolPart(true) - messages = append(messages, protocol.ChatMessage{ID: part.ID, SessionID: id, Role: protocol.ChatRoleAssistant, Parts: []protocol.ChatPart{part}}) + messages = append(messages, protocol.ChatMessage{ID: part.ID, SessionID: id, Role: protocol.ChatRoleAssistant, CreatedAt: turn.createdAtMillis(), Parts: []protocol.ChatPart{part}}) } } page := protocol.ChatHistoryPage{Messages: messages} @@ -975,10 +976,21 @@ type thread struct { } type turn struct { - ID string `json:"id"` - Status string `json:"status"` - Error json.RawMessage `json:"error"` - Items []item `json:"items"` + StartedAt int64 `json:"startedAt"` + ID string `json:"id"` + Status string `json:"status"` + Error json.RawMessage `json:"error"` + Items []item `json:"items"` +} + +// Codex history exposes a recorded turn start in seconds, rather than item +// creation times. Use that shared turn time for its user and assistant rows. +// Older history without timing remains unknown; never substitute read time. +func (t turn) createdAtMillis() int64 { + if t.StartedAt <= 0 || t.StartedAt > 8640000000000 { + return 0 + } + return t.StartedAt * 1000 } type item struct { diff --git a/internal/codex/timestamps_test.go b/internal/codex/timestamps_test.go new file mode 100644 index 00000000..0626edfa --- /dev/null +++ b/internal/codex/timestamps_test.go @@ -0,0 +1,41 @@ +package codex + +import ( + "context" + "encoding/json" + "testing" +) + +func TestHistoryCarriesRecordedTurnTimes(t *testing.T) { + c := modelTestChat(t, func(method string, _ json.RawMessage) any { + if method != "thread/turns/list" { + t.Fatalf("unexpected method %s", method) + } + return json.RawMessage(`{"data":[{"id":"new","startedAt":1793916000,"status":"inProgress","items":[{"id":"u","clientId":"client-u","type":"userMessage","content":[{"type":"text","text":"question"}]},{"id":"a","type":"agentMessage","text":"reply"}]},{"id":"old","items":[{"id":"legacy","type":"agentMessage","text":"old reply"}]}],"nextCursor":null}`) + }) + for range 2 { + page, err := c.Messages(context.Background(), fixtureThread, 50, "") + if err != nil || len(page.Messages) != 3 { + t.Fatalf("page=%+v err=%v", page, err) + } + if page.Messages[0].CreatedAt != 0 { + t.Fatal("invented legacy timestamp") + } + for _, msg := range page.Messages[1:] { + if msg.CreatedAt != 1793916000000 { + t.Fatalf("missing recorded time: %+v", msg) + } + } + if page.Messages[1].ID != "client-u" { + t.Fatal("lost user identity") + } + } +} + +func TestInvalidCodexTurnTimesStayUnknown(t *testing.T) { + for _, stamp := range []int64{0, -1, 8640000000001, 9223372036854775807} { + if (turn{StartedAt: stamp}).createdAtMillis() != 0 { + t.Fatalf("accepted %d", stamp) + } + } +}