Repository navigation
fix(agentengine): preserve fractional seconds in event timestamp encoding - #2199
Conversation
wolo-lab
left a comment
There was a problem hiding this comment.
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.goline 64 still builds the value fromUnixMilli(), so everything below one millisecond is dropped. The rest of the repository settled on microseconds for this field: theadkrestserver emits events withfloat64(UnixMicro()) / 1e6, the database session service truncates stored events to microseconds, and thesession.EventJSON decoder resolves numeric timestamps to microseconds to match Python'sdatetime(adk-python emitstime.time()as a float). The same Agent Engine server even reportslast_update_timeat nanosecond resolution, so after this PR a client gets…0.25on the stream and…0.2505fromget_sessionfor the same event, and two events within one millisecond are indistinguishable by timestamp. Measured with the adkrest fixturetime.UnixMicro(1733040000123456): this branch streams1733040000.123, adkrest emits1733040000.123456, and a 0.5 ms fraction (time.Unix(1700000000, 500_000)) streams as a bare1700000000.float64(t.UnixMicro()) / 1e6gives1733040000.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]>
8043f59 to
60d0241
Compare
|
Thanks @wolo-lab for the thorough review and guidance! Updated to |
Signed-off-by: sundeep8967 <[email protected]>
|
Updated the test suite in commit 415a6d8:
Verified all tests pass cleanly. |
…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)
Description
Fixes fractional timestamp truncation in Agent Engine event JSON encoding.
When converting
time.Timeinserver/agentengine/internal/helper/encode.go,t.UnixMilli() / 1000.0evaluated as integer division in Go becauseUnixMilli()returnsint64. This caused any fractional seconds (e.g.1700000000.25) to be truncated to integer seconds (1700000000).Converting
t.UnixMilli()tofloat64prior to division retains the millisecond resolution and aligns with floating-point Unix timestamp representations across session and event APIs.Changes
server/agentengine/internal/helper/encode.go, castt.UnixMilli()tofloat64before dividing by1000.0.TestTimeFractionalSecondsinserver/agentengine/internal/helper/encode_test.goverifying that non-zero fractional millisecond timestamps encode accurately asfloat64.Fixes #2169
Testing
go test -v ./server/agentengine/internal/helper -run TestTimeFractionalSeconds(PASS)go test ./server/agentengine/...(PASS)