Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.
fix(teamhub): tolerate recorder clock skew
muterC8d8bc04e4da2
diff --git a/apps/teamhub/AGENTS.md b/apps/teamhub/AGENTS.mdindex bd67a685..4a97f4b2 100644--- a/apps/teamhub/AGENTS.md+++ b/apps/teamhub/AGENTS.md@@ -199,6 +199,12 @@ Go service owning Human HTTP/SSE, Agent callbacks, and active product state.`notify_river_queue` so the job does not wait for the queue's poll. Such aloop is woken on commit and sleeps until its next due time, with a fallbackof at least one minute; it never polls on a short fixed interval.+- Voice recording timestamps come from a recorder clock distinct from PostgreSQL.+ A recorder is bound to a persisted Capture epoch; normalize a reported start+ earlier than the Capture's clock to that clock before confirming its timeline.+ A missing start stays unconfirmed. Keep audio duration and transcript offsets+ on the confirmed timeline, and enqueue an already-requested Stop when the+ initial recorder becomes known, including after restart reconciliation.- Voice: `TEAMHUB_VOICE_MEDIA` (`on` by default) builds the voice media planeover LiveKit; `off`, a missing `LINGER_SECRET_SEED`, or missing ffmpeg keepvoice off (the last two log `voice.media_unavailable`) without stoppingdiff --git a/apps/teamhub/internal/voicemedia/initial_timeline.go b/apps/teamhub/internal/voicemedia/initial_timeline.goindex 89ea1219..c668e159 100644--- a/apps/teamhub/internal/voicemedia/initial_timeline.go+++ b/apps/teamhub/internal/voicemedia/initial_timeline.go@@ -14,8 +14,13 @@ import (var errCaptureStoppedBeforeMedia = errors.New("Capture stopped before media began")func initialMediaStart(requested, started time.Time) (time.Time, error) {- if started.IsZero() || started.Before(requested) {- return time.Time{}, fmt.Errorf("%w: initial media start timestamp is missing or precedes the request", ErrProviderUnavailable)+ if started.IsZero() {+ return time.Time{}, fmt.Errorf("%w: initial media start timestamp is missing", ErrProviderUnavailable)+ }+ // This recorder follows an epoch issued after the capture was persisted.+ // Its wall clock can lag PostgreSQL, so preserve that causal lower bound.+ if started.Before(requested) {+ return requested, nil}return started, nil}diff --git a/apps/teamhub/internal/voicemedia/service_test.go b/apps/teamhub/internal/voicemedia/service_test.goindex ab3e3d90..16ad692d 100644--- a/apps/teamhub/internal/voicemedia/service_test.go+++ b/apps/teamhub/internal/voicemedia/service_test.go@@ -91,16 +91,22 @@ func TestActiveVoiceSessionConstraintIsAProductConflict(t *testing.T) {}}-func TestInitialMediaStartRejectsMissingAndPreRequestClock(t *testing.T) {- requested := time.Date(2026, 9, 10, 12, 0, 0, 0, time.UTC)- for _, started := range []time.Time{{}, requested.Add(-time.Millisecond)} {- if _, err := initialMediaStart(requested, started); !errors.Is(err, ErrProviderUnavailable) {- t.Fatalf("untrusted media start accepted: start=%v err=%v", started, err)- }- }- started := requested.Add(5 * time.Second)- if got, err := initialMediaStart(requested, started); err != nil || !got.Equal(started) {- t.Fatalf("actual media start changed: got=%v err=%v", got, err)+func TestInitialMediaStartNormalizesRecorderClock(t *testing.T) {+ requested := time.Date(2026, 9, 10, 12, 0, 0, 500_000, time.UTC)+ if _, err := initialMediaStart(requested, time.Time{}); !errors.Is(err, ErrProviderUnavailable) {+ t.Fatalf("missing media start accepted: %v", err)+ }+ for _, offset := range []time.Duration{-5 * time.Second, -time.Millisecond, -500 * time.Microsecond, 0, 5 * time.Second} {+ t.Run(offset.String(), func(t *testing.T) {+ started := requested.Add(offset)+ want := requested+ if offset > 0 {+ want = started+ }+ if got, err := initialMediaStart(requested, started); err != nil || !got.Equal(want) {+ t.Fatalf("media start=%v want=%v err=%v", got, want, err)+ }+ })}}diff --git a/apps/teamhub/tests/integration/voice_media_clock_skew_test.go b/apps/teamhub/tests/integration/voice_media_clock_skew_test.gonew file mode 100644index 00000000..2a6bcccd--- /dev/null+++ b/apps/teamhub/tests/integration/voice_media_clock_skew_test.go@@ -0,0 +1,88 @@+package integration_test++import (+ "context"+ "strconv"+ "testing"+ "time"++ "github.com/simplexai-labs/linger/apps/teamhub/internal/voicemedia"+)++func TestVoiceRecorderClockSkewDoesNotStrandStop(t *testing.T) {+ allowParallelIntegrationTest(t)+ for _, stopBeforeConfirmation := range []bool{false, true} {+ t.Run(strconv.FormatBool(stopBeforeConfirmation), func(t *testing.T) {+ env, q, capture, epoch := newVoiceRecoveryLifecycle(t)+ capture, err := q.GetCaptureSessionForAccount(t.Context(), aliceCapture(capture.ID))+ if err != nil {+ t.Fatal(err)+ }+ requested := capture.CreatedAt.Time+ started := requested.Add(-5 * time.Second)+ if _, err = env.pool.Exec(t.Context(), "UPDATE voice_sessions SET created_at=$2 WHERE id=$1", capture.VoiceSessionID.String, requested.Add(-time.Second)); err != nil {+ t.Fatal(err)+ }+ if _, err = env.pool.Exec(t.Context(), "UPDATE capture_sessions SET status='starting',transcript_start_offset_ms=1000 WHERE id=$1", capture.ID); err != nil {+ t.Fatal(err)+ }+ if _, err = env.pool.Exec(t.Context(), "UPDATE voice_media_epochs SET observed_state='requested',provider_egress_id=NULL,first_archived_ms=NULL,archived_through_ms=NULL WHERE id=$1", epoch.ID); err != nil {+ t.Fatal(err)+ }+ recorder := newVoiceRecorderRoom(t, env.pool)+ recorder.direct("recovery-tx", epoch.StagingPrefix)+ recorder.report(t.Context(), epoch.StagingPrefix, "active", started, 2_000)+ recorder.stop = func(ctx context.Context, prefix string) {+ recorder.report(ctx, prefix, "available", started, 2_000)+ }+ service := voicemedia.New(env.pool, recorder.provider(voicemedia.LiveKitConfig{}, env.pool), nil)+ stop := func() {+ t.Helper()+ if _, err := service.EndCapture(t.Context(), voicemedia.EndCaptureRequest{AccountID: "account_alice", CaptureSessionID: capture.ID}); err != nil {+ t.Fatal(err)+ }+ if _, err := env.pool.Exec(t.Context(), "UPDATE capture_sessions SET stop_requested_at=$2 WHERE id=$1", capture.ID, requested.Add(2*time.Second)); err != nil {+ t.Fatal(err)+ }+ }+ reconcile := func() {+ t.Helper()+ if _, err := env.pool.Exec(t.Context(), "UPDATE voice_media_epochs SET updated_at=clock_timestamp()-interval '3 seconds' WHERE id=$1", epoch.ID); err != nil {+ t.Fatal(err)+ }+ if _, err := service.RunJobStepForTest(t.Context(), voicemedia.CaptureReconcileArgs{CaptureSessionID: capture.ID}); err != nil {+ t.Fatalf("recorder clock skew stranded reconciliation: %v", err)+ }+ }+ if stopBeforeConfirmation {+ stop()+ }+ reconcile()+ confirmed, err := q.GetCaptureSessionForAccount(t.Context(), aliceCapture(capture.ID))+ if err != nil || !confirmed.CreatedAt.Time.Equal(requested) || confirmed.TranscriptStartOffsetMs.Int64 != 1_000 {+ t.Fatalf("capture left the server timeline: %+v err=%v", confirmed, err)+ }+ if !stopBeforeConfirmation {+ stop()+ }+ requireVoiceJobQueued(t, env.pool, voicemedia.CaptureStopArgs{CaptureSessionID: capture.ID}, "clock-skewed recorder admission")+ if _, err = service.RunJobStepForTest(t.Context(), voicemedia.CaptureStopArgs{CaptureSessionID: capture.ID}); err != nil {+ t.Fatal(err)+ }+ if recorder.stops.Load() != 1 {+ t.Fatalf("recorder was not stopped: %d", recorder.stops.Load())+ }+ reconcile()+ reconcile()+ sealed, err := q.GetVoiceMediaEpoch(t.Context(), epoch.ID)+ if err != nil || sealed.ObservedState != "available" || sealed.FinalEndMs.Int64 != 2_000 || sealed.FirstArchivedMs.Int64 != 0 {+ t.Fatalf("recorded audio did not seal on its full timeline: %+v err=%v", sealed, err)+ }+ completed, err := q.GetCaptureSessionForAccount(t.Context(), aliceCapture(capture.ID))+ if err != nil || !completed.MaterializeRequestedAt.Valid {+ t.Fatalf("stopped capture never reached materialization: %+v err=%v", completed, err)+ }+ requireVoiceJobQueued(t, env.pool, voicemedia.CaptureMaterializeArgs{CaptureSessionID: capture.ID}, "clock-skewed recording completion")+ })+ }+}