Skip to content

fix(agentengine): preserve fractional seconds in event timestamp encoding - #2199

Merged
wolo-lab merged 3 commits into
google:mainfrom
sundeep8967:fix/agentengine-fractional-timestamp
Nov 7, 2026
Merged

wolo-lab merged 3 commits into
google:mainfrom
sundeep8967:fix/agentengine-fractional-timestamp

Conversation

@sundeep8967

Copy link
Copy Markdown
Contributor

Description

Fixes fractional timestamp truncation in Agent Engine event JSON encoding.

When converting time.Time in server/agentengine/internal/helper/encode.go, t.UnixMilli() / 1000.0 evaluated as integer division in Go because UnixMilli() returns int64. This caused any fractional seconds (e.g. 1700000000.25) to be truncated to integer seconds (1700000000).

Converting t.UnixMilli() to float64 prior to division retains the millisecond resolution and aligns with floating-point Unix timestamp representations across session and event APIs.

Changes

  • In server/agentengine/internal/helper/encode.go, cast t.UnixMilli() to float64 before dividing by 1000.0.
  • Added unit test TestTimeFractionalSeconds in server/agentengine/internal/helper/encode_test.go verifying that non-zero fractional millisecond timestamps encode accurately as float64.

Fixes #2169

Testing

  • Ran: go test -v ./server/agentengine/internal/helper -run TestTimeFractionalSeconds (PASS)
  • Ran: go test ./server/agentengine/... (PASS)

@wolo-lab wolo-lab left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One change is needed before merge: the line this PR rewrites should keep microsecond precision, not millisecond.

The cast fixes the whole-second truncation from #2169, confirmed by running the new test on the merge-base (fails with int64 1700000000) and on this branch (passes), and by driving EmitJSON with a session.Event: a client now receives "timestamp":1700000000.25 where it received 1700000000 before.

Must change before merge:

  • Use microseconds. encode.go line 64 still builds the value from UnixMilli(), so everything below one millisecond is dropped. The rest of the repository settled on microseconds for this field: the adkrest server emits events with float64(UnixMicro()) / 1e6, the database session service truncates stored events to microseconds, and the session.Event JSON decoder resolves numeric timestamps to microseconds to match Python's datetime (adk-python emits time.time() as a float). The same Agent Engine server even reports last_update_time at nanosecond resolution, so after this PR a client gets …0.25 on the stream and …0.2505 from get_session for the same event, and two events within one millisecond are indistinguishable by timestamp. Measured with the adkrest fixture time.UnixMicro(1733040000123456): this branch streams 1733040000.123, adkrest emits 1733040000.123456, and a 0.5 ms fraction (time.Unix(1700000000, 500_000)) streams as a bare 1700000000. float64(t.UnixMicro()) / 1e6 gives 1733040000.123456, keeps whole-second values and the zero time byte-identical, and passes the existing tests, including the new one.

Three pre-existing defects in convertSnake surfaced while checking this (an unsigned integer anywhere in the event makes the whole event fall back to the raw encoder with an RFC 3339 timestamp string, omitzero never omits, and a zero time encodes as year 1 where adkrest emits 0). None of them are for this PR. I will file them separately.

…ding

Convert t.UnixMicro() to float64 and divide by 1e6 to preserve microsecond
timestamp precision across event streaming, matching adkrest, session, and
database models. Also add unit test verifying microsecond resolution.

Fixes google#2169

Signed-off-by: sundeep8967 <[email protected]>
@sundeep8967
sundeep8967 force-pushed the fix/agentengine-fractional-timestamp branch from 8043f59 to 60d0241 Compare November 7, 2026 20:34
@sundeep8967

Copy link
Copy Markdown
Contributor Author

Thanks @wolo-lab for the thorough review and guidance! Updated to float64(t.UnixMicro()) / 1e6 to match the microsecond resolution used across adkrest, session.Event decoding, and database models, and updated the test suite accordingly. Re-verified clean.

@sundeep8967

Copy link
Copy Markdown
Contributor Author

Updated the test suite in commit 415a6d8:

  • Added a whole-second fixture expecting exactly 1700000000.0.
  • Added an EmitJSON serialization test with a real session.Event asserting that rw.Body.Bytes() produces the expected microsecond-resolution float (1733040000.123456) in the final output received by HTTP clients.

Verified all tests pass cleanly.

@wolo-lab wolo-lab left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@wolo-lab
wolo-lab merged commit c00807e into google:main Nov 7, 2026
15 checks passed
wolo-lab added a commit that referenced this pull request Nov 16, 2026
…1720, #2106, #2090, #1870, #1988, #2052, #2107, #2199, #2191, #2192) (#2358)

* fix(server/triggers): honor MaxConcurrentRuns in PubSubController (#1720)

(cherry picked from commit 26495ae)

* fix(adkrest): let AutoCreateSession reach /run and /run_sse (#2106)

(cherry picked from commit f6da03a)

* fix(adkrest): answer [] from /run when a run yields no events (#2090)

(cherry picked from commit 0159a2e)

* fix(adkrest): include timestamp in REST Event model (#1870)

(cherry picked from commit 4db0bd9)

* fix(adkrest): preserve debug span attribute types (#1988)

(cherry picked from commit c377836)

* fix(triggers): use timer-and-select for context-aware retry backoff (#2052)

(cherry picked from commit 123fea9)

* fix(adkrest): answer 500, not 404, when /run cannot read the session (#2107)

(cherry picked from commit 8ce8f01)

* fix(agentengine): preserve fractional seconds in event timestamp encoding (#2199)

(cherry picked from commit c00807e)

* fix(adkrest): treat MaxConcurrentRuns<=0 as unlimited in the Eventarc trigger (#2191)

(cherry picked from commit 2278f8f)

* fix(adkrest): drop nil parts when serializing events and scanning the event graph  (#2192)

(cherry picked from commit 3193128)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Agent Engine truncates fractional event timestamps

2 participants