Forward recorded Codex turn timestamps to chat history

This commit is contained in:
2026-11-06 00:00:21 +00:00
parent 6d4ed4ac67
commit 20a69b7e47
3 changed files with 63 additions and 5 deletions
+5
View File
@@ -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.
+17 -5
View File
@@ -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 {
+41
View File
@@ -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)
}
}
}