Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(realtime): stamp comment:created broadcasts with the real UTC offset (#7523)
Bohan Jiang3881f6589f43
diff --git a/server/internal/service/comment_event_fields_test.go b/server/internal/service/comment_event_fields_test.gonew file mode 100644index 0000000000..488908098b--- /dev/null+++ b/server/internal/service/comment_event_fields_test.go@@ -0,0 +1,62 @@+package service++import (+ "testing"+ "time"++ "github.com/jackc/pgx/v5/pgtype"+ db "github.com/multica-ai/multica/server/pkg/db/generated"+)++func commentAt(created time.Time) db.Comment {+ return db.Comment{+ AuthorType: "system",+ Content: "task failed",+ Type: "system",+ CreatedAt: pgtype.Timestamptz{Time: created, Valid: true},+ }+}++// A comment:created broadcast must carry the SAME created_at string the REST+// timeline returns for that comment. pgx decodes timestamptz into the process+// location, so formatting with a literal "Z" suffix published local wall-clock+// digits under a UTC label: on an Asia/Shanghai deployment every WS-delivered+// agent/system comment landed 8 hours from its real instant, then jumped back+// into place once a refetch replaced it with the REST value.+func TestCommentEventFieldsCreatedAtCarriesRealOffset(t *testing.T) {+ shanghai := time.FixedZone("CST", 8*60*60)+ created := time.Date(2026, 8, 24, 17, 7, 5, 0, shanghai)++ got, _ := commentEventFields(commentAt(created))["created_at"].(string)+ if want := "2026-08-24T17:07:05+08:00"; got != want {+ t.Fatalf("created_at = %q, want %q", got, want)+ }+ parsed, err := time.Parse(time.RFC3339, got)+ if err != nil {+ t.Fatalf("parse created_at %q: %v", got, err)+ }+ if !parsed.Equal(created) {+ t.Fatalf("created_at %q decodes to %s, want %s", got, parsed.UTC(), created.UTC())+ }+}++// The overwhelmingly common deployment runs the server process in UTC, where+// the old literal-"Z" format was already correct. Pin that those clients see a+// byte-identical payload so the fix carries no migration for them.+func TestCommentEventFieldsCreatedAtUTCIsUnchanged(t *testing.T) {+ created := time.Date(2026, 8, 24, 9, 7, 5, 0, time.UTC)++ got, _ := commentEventFields(commentAt(created))["created_at"].(string)+ if want := "2026-08-24T09:07:05Z"; got != want {+ t.Fatalf("created_at = %q, want %q", got, want)+ }+}++// An unset timestamp must not render as a zero-value instant that would sort+// to the beginning of every timeline.+func TestCommentEventFieldsCreatedAtInvalidIsEmpty(t *testing.T) {+ got, ok := commentEventFields(db.Comment{})["created_at"].(string)+ if !ok || got != "" {+ t.Fatalf("created_at = %q (string=%v), want empty string", got, ok)+ }+}diff --git a/server/internal/service/task.go b/server/internal/service/task.goindex 359c086269..ea5dc82b5d 100644--- a/server/internal/service/task.go+++ b/server/internal/service/task.go@@ -5718,17 +5718,7 @@ func (s *TaskService) ensureDelegatedFailureRecoveryComment(ctx context.Context,ActorType: "system",ActorID: "",Payload: map[string]any{- "comment": map[string]any{- "id": util.UUIDToString(target.comment.ID),- "issue_id": util.UUIDToString(target.comment.IssueID),- "author_type": target.comment.AuthorType,- "author_id": util.UUIDToString(target.comment.AuthorID),- "content": target.comment.Content,- "type": target.comment.Type,- "parent_id": util.UUIDToPtr(target.comment.ParentID),- "source_task_id": util.UUIDToPtr(target.comment.SourceTaskID),- "created_at": target.comment.CreatedAt.Time.Format("2006-01-02T15:04:05Z"),- },+ "comment": commentEventFields(target.comment),"issue_title": target.issue.Title,"issue_status": target.issue.Status,},@@ -5868,17 +5858,7 @@ func (s *TaskService) exhaustDelegatedFailureRecovery(ctx context.Context, targeActorType: "system",ActorID: "",Payload: map[string]any{- "comment": map[string]any{- "id": util.UUIDToString(exhaustedComment.ID),- "issue_id": util.UUIDToString(exhaustedComment.IssueID),- "author_type": exhaustedComment.AuthorType,- "author_id": util.UUIDToString(exhaustedComment.AuthorID),- "content": exhaustedComment.Content,- "type": exhaustedComment.Type,- "parent_id": util.UUIDToPtr(exhaustedComment.ParentID),- "source_task_id": util.UUIDToPtr(exhaustedComment.SourceTaskID),- "created_at": exhaustedComment.CreatedAt.Time.Format("2006-01-02T15:04:05Z"),- },+ "comment": commentEventFields(exhaustedComment),"issue_title": target.issue.Title,"issue_status": target.issue.Status,},@@ -6620,6 +6600,34 @@ func (s *TaskService) getIssuePrefix(workspaceID pgtype.UUID) string {return ws.IssuePrefix}+// commentEventFields renders the `comment` object carried by comment:created+// broadcasts published outside the HTTP handler (agent replies, delegated+// failure recovery, recovery exhaustion).+//+// created_at goes through util.TimestampToString — the SAME helper the REST+// CommentResponse uses — so the timestamp a client receives over WS is+// byte-identical to the one it gets when it refetches that comment. These+// payloads used to format the time with a literal "Z" suffix instead, which+// is not Go's Z07:00 offset token: pgx decodes timestamptz into the process+// location (pgtype/timestamptz.go, no ScanLocation configured), so on a+// deployment whose process TZ is not UTC the broadcast stamped local+// wall-clock digits with a UTC label. Clients then placed the entry a whole+// offset away from its real instant and moved it again once a refetch+// replaced the value — visible as timeline entries jumping position.+func commentEventFields(c db.Comment) map[string]any {+ return map[string]any{+ "id": util.UUIDToString(c.ID),+ "issue_id": util.UUIDToString(c.IssueID),+ "author_type": c.AuthorType,+ "author_id": util.UUIDToString(c.AuthorID),+ "content": c.Content,+ "type": c.Type,+ "parent_id": util.UUIDToPtr(c.ParentID),+ "source_task_id": util.UUIDToPtr(c.SourceTaskID),+ "created_at": util.TimestampToString(c.CreatedAt),+ }+}+func (s *TaskService) createAgentComment(ctx context.Context, issueID, agentID pgtype.UUID, content, commentType string, parentID, sourceTaskID pgtype.UUID) {if content == "" {return@@ -6657,24 +6665,15 @@ func (s *TaskService) createAgentComment(ctx context.Context, issueID, agentID p}comment := created.Comment()s.CancelDeferredEscalationsForIssueAgent(ctx, issueID, agentID)+ commentFields := commentEventFields(comment)+ commentFields["revision"] = comment.Revisions.Bus.Publish(events.Event{Type: protocol.EventCommentCreated,WorkspaceID: util.UUIDToString(issue.WorkspaceID),ActorType: "agent",ActorID: util.UUIDToString(agentID),Payload: map[string]any{- "comment": map[string]any{- "id": util.UUIDToString(comment.ID),- "issue_id": util.UUIDToString(comment.IssueID),- "author_type": comment.AuthorType,- "author_id": util.UUIDToString(comment.AuthorID),- "content": comment.Content,- "type": comment.Type,- "parent_id": util.UUIDToPtr(comment.ParentID),- "source_task_id": util.UUIDToPtr(comment.SourceTaskID),- "created_at": comment.CreatedAt.Time.Format("2006-01-02T15:04:05Z"),- "revision": comment.Revision,- },+ "comment": commentFields,"issue_title": issue.Title,"issue_status": issue.Status,"issue_revision": created.IssueRevision,