Skip to content

[refactor] The observability state layer moves into @agenta/observability - #5955

Closed
ardaerzin wants to merge 1 commit into
obs/wp0-entities-prepfrom
obs/wp1-observability-state
Closed

[refactor] The observability state layer moves into @agenta/observability#5955
ardaerzin wants to merge 1 commit into
obs/wp0-entities-prepfrom
obs/wp1-observability-state

Conversation

@ardaerzin

Copy link
Copy Markdown
Contributor

Context

The observability page's state lived in oss/src/state/newObservability, which meant mobile could not read a trace list without importing from the desktop app. This moves that state layer into @agenta/observability.

Second of five stacked PRs. Base is obs/wp0-entities-prep.

Changes

The atoms, query helpers, constants and trace-span types move into @agenta/observability. oss/src/state/observability stays, but shrinks to the app's binding: the atoms it re-binds, a seams.ts for the app-only wiring, and useObservability.

newObservability is gone rather than left as a shim. OSS and EE lint-block re-exporting @agenta/*, so the call sites are rewritten instead.

SortResult becomes an alias of AnalyticsRange. They were already the same shape, and naming that here is what later lets one range picker serve both the dashboard and the traces toolbar.

Tests / notes

  • @agenta/observability builds, lints and passes its unit tests.
  • Retyping came with the move: the packages set @typescript-eslint/no-explicit-any to error, so every any that came across had to be resolved rather than carried.
  • Intermediate lanes in this stack may not build standalone. Only the tip is verified green.

@dosubot dosubot Bot added the size:XL This PR changes 500-999 lines, ignoring generated files. label Aug 12, 2026
@vercel

vercel Bot commented Aug 12, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
agenta-documentation Blocked Blocked Aug 12, 2026 12:17am

Request Review

@dosubot dosubot Bot added refactoring A code change that neither fixes a bug nor adds a feature typescript Pull requests that update typescript code labels Aug 12, 2026
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 585ef4fb-4796-4df9-8471-71555da7de3a

📥 Commits

Reviewing files that changed from the base of the PR and between 307b66d and 6b4cc1b.

📒 Files selected for processing (30)
  • web/oss/src/components/Filters/Sort.tsx
  • web/oss/src/lib/Types.ts
  • web/oss/src/services/tracing/types/index.ts
  • web/oss/src/state/Providers.tsx
  • web/oss/src/state/newObservability/index.ts
  • web/oss/src/state/observability/atoms.ts
  • web/oss/src/state/observability/index.ts
  • web/oss/src/state/observability/seams.ts
  • web/oss/src/state/observability/useObservability.ts
  • web/packages/agenta-observability/package.json
  • web/packages/agenta-observability/src/api/queryHelpers.ts
  • web/packages/agenta-observability/src/core/constants.ts
  • web/packages/agenta-observability/src/core/traceSpan.ts
  • web/packages/agenta-observability/src/core/types.ts
  • web/packages/agenta-observability/src/etl/adaptiveExportPacing.ts
  • web/packages/agenta-observability/src/etl/adaptiveTracePageFetcher.ts
  • web/packages/agenta-observability/src/etl/exportWriter.ts
  • web/packages/agenta-observability/src/etl/withRateLimitRetry.ts
  • web/packages/agenta-observability/src/hooks/useObservability.ts
  • web/packages/agenta-observability/src/hooks/useSessions.ts
  • web/packages/agenta-observability/src/index.ts
  • web/packages/agenta-observability/src/state/controls.ts
  • web/packages/agenta-observability/src/state/queries.ts
  • web/packages/agenta-observability/src/state/seams.ts
  • web/packages/agenta-observability/src/state/selectors.ts
  • web/packages/agenta-observability/src/utils/buildTraceQueryParams.ts
  • web/packages/agenta-observability/src/utils/filterCoercion.ts
  • web/packages/agenta-observability/tests/unit/queryHelpers.test.ts
  • web/packages/agenta-observability/tsconfig.json
  • web/packages/agenta-observability/vitest.config.ts
💤 Files with no reviewable changes (1)
  • web/oss/src/state/newObservability/index.ts

📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • Expanded observability capabilities for trace and session exploration, including pagination, filtering, sorting, and export utilities.
    • Added improved support for workflow context, application scope, annotations, and workspace-aware observability data.
    • Added persistent indicators for received traces and sessions.
    • Added resilient handling for rate-limited requests and cancelable data operations.
  • Bug Fixes

    • Improved trace and session query accuracy, attribute handling, filtering, and pagination.
    • Prevented observability state from being accessed before it is initialized.
  • Tests

    • Added comprehensive coverage for query filters, annotations, time ranges, and parameter handling.

Walkthrough

The PR moves observability types, state, query logic, and export utilities into @agenta/observability. OSS binds application state through host seams and re-exports package-owned types. The package adds typed query handling, adaptive paging, rate-limit retries, and unit-test coverage.

Changes

Observability package migration

Layer / File(s) Summary
Shared contracts and public package surface
web/packages/agenta-observability/package.json, web/packages/agenta-observability/src/core/*, web/packages/agenta-observability/src/state/seams.ts, web/packages/agenta-observability/src/index.ts
The package adds shared filter, sort, trace, tab, scope, workflow, and pagination contracts. It exposes state, hooks, query helpers, filters, and ETL utilities through public exports.
Scope-aware observability state
web/packages/agenta-observability/src/state/controls.ts, web/packages/agenta-observability/src/state/queries.ts, web/packages/agenta-observability/src/hooks/*, web/packages/agenta-observability/src/state/selectors.ts
Controls and queries now use observability scope, workflow context, workspace members, project IDs, typed response shapes, shared session state, and normalized ag attributes.
Typed query and export pipeline
web/packages/agenta-observability/src/api/queryHelpers.ts, web/packages/agenta-observability/src/utils/*, web/packages/agenta-observability/src/etl/*
Query and filter helpers replace any with unknown and explicit carriers. ETL utilities add abortable pacing, adaptive project-scoped page fetching, rate-limit retries, and shared CSV downloading.
OSS integration and validation
web/oss/src/state/Providers.tsx, web/oss/src/state/observability/*, web/oss/src/components/Filters/Sort.tsx, web/oss/src/lib/Types.ts, web/oss/src/services/tracing/types/index.ts, web/packages/agenta-observability/tests/unit/*, web/packages/agenta-observability/{tsconfig.json,vitest.config.ts}
OSS binds host atoms before rendering and exposes host-owned drawer and onboarding state. OSS filter and trace types re-export package types. Query-helper tests and Vitest configuration are added.

Estimated code review effort: 4 (Complex) | ~60 minutes

Mergeability Score: 🟡 Moderate · up to 6b4cc

This refactor moves observability state into a shared package, but the current head can reuse annotations from the wrong project and cannot pass frozen dependency installation until the lockfile is regenerated. These issues should be fixed before merging.

Sequence Diagram(s)

sequenceDiagram
  participant OSSProviders
  participant bindObservabilityHostAtoms
  participant ObservabilityScope
  participant ObservabilityControls
  participant ObservabilityQueries
  participant TraceAPI
  OSSProviders->>bindObservabilityHostAtoms: bind host scope and workflow atoms
  bindObservabilityHostAtoms->>ObservabilityScope: provide app, route, workflow, and workspace state
  ObservabilityControls->>ObservabilityScope: read persisted filter and trace-type scope
  ObservabilityQueries->>TraceAPI: execute project-scoped trace and session queries
  TraceAPI-->>ObservabilityQueries: return typed pages and normalized attributes
  ObservabilityQueries-->>ObservabilityControls: expose observability state
Loading

Possibly related PRs

  • Agenta-AI/agenta#4573: Migrates observability and tracing queries toward shared entity types and project-scoped trace fetching.
  • Agenta-AI/agenta#4474: Removes legacy newObservability exports that this PR also replaces.
  • Agenta-AI/agenta#5465: Continues trace-type consolidation in the shared observability package.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the primary change: moving the observability state layer into @agenta/observability.
Description check ✅ Passed The description directly explains the observability state migration, its scope, and the related type and test changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch obs/wp1-observability-state

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ardaerzin

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 13, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
web/packages/agenta-observability/package.json (1)

26-51: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Commit the regenerated web/pnpm-lock.yaml.

The packages/agenta-observability importer does not include the five manifest entries. Frozen installs fail with ERR_PNPM_OUTDATED_LOCKFILE. Run pnpm install from web and commit the lockfile.

Source: Pipeline failures

🧹 Nitpick comments (1)
web/packages/agenta-observability/src/index.ts (1)

1-3: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Reduce the new multiline comments.

These comments do not document a bug, race, or ordering constraint. Replace them with one short line, or remove them when the code is self-explanatory.

  • web/packages/agenta-observability/src/index.ts#L1-L3: Replace this and the repeated three-line section banners with one short section comment or no comment.
  • web/packages/agenta-observability/src/core/traceSpan.ts#L4-L7: Reduce the interface documentation to one short line.
  • web/oss/src/services/tracing/types/index.ts#L1-L2: Remove or reduce the re-export explanation to one short line.
  • web/oss/src/state/observability/useObservability.ts#L1-L6: Reduce the wrapper description to one short line.

As per coding guidelines, “Keep in-code comments to at most one short line; use longer comments only for genuinely surprising constraints such as bugs, races, or ordering requirements.”

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 585ef4fb-4796-4df9-8471-71555da7de3a

📥 Commits

Reviewing files that changed from the base of the PR and between 307b66d and 6b4cc1b.

📒 Files selected for processing (30)
  • web/oss/src/components/Filters/Sort.tsx
  • web/oss/src/lib/Types.ts
  • web/oss/src/services/tracing/types/index.ts
  • web/oss/src/state/Providers.tsx
  • web/oss/src/state/newObservability/index.ts
  • web/oss/src/state/observability/atoms.ts
  • web/oss/src/state/observability/index.ts
  • web/oss/src/state/observability/seams.ts
  • web/oss/src/state/observability/useObservability.ts
  • web/packages/agenta-observability/package.json
  • web/packages/agenta-observability/src/api/queryHelpers.ts
  • web/packages/agenta-observability/src/core/constants.ts
  • web/packages/agenta-observability/src/core/traceSpan.ts
  • web/packages/agenta-observability/src/core/types.ts
  • web/packages/agenta-observability/src/etl/adaptiveExportPacing.ts
  • web/packages/agenta-observability/src/etl/adaptiveTracePageFetcher.ts
  • web/packages/agenta-observability/src/etl/exportWriter.ts
  • web/packages/agenta-observability/src/etl/withRateLimitRetry.ts
  • web/packages/agenta-observability/src/hooks/useObservability.ts
  • web/packages/agenta-observability/src/hooks/useSessions.ts
  • web/packages/agenta-observability/src/index.ts
  • web/packages/agenta-observability/src/state/controls.ts
  • web/packages/agenta-observability/src/state/queries.ts
  • web/packages/agenta-observability/src/state/seams.ts
  • web/packages/agenta-observability/src/state/selectors.ts
  • web/packages/agenta-observability/src/utils/buildTraceQueryParams.ts
  • web/packages/agenta-observability/src/utils/filterCoercion.ts
  • web/packages/agenta-observability/tests/unit/queryHelpers.test.ts
  • web/packages/agenta-observability/tsconfig.json
  • web/packages/agenta-observability/vitest.config.ts
💤 Files with no reviewable changes (1)
  • web/oss/src/state/newObservability/index.ts

Comment on lines 177 to +183
queryKey: ["annotations", links],
queryFn: async () => {
if (Array.isArray(links) && !links.length) return [] as AnnotationDto[]
const res = await queryAllAnnotations({annotation: {links}})
const res = await queryAllAnnotations({
projectId: projectId ?? undefined,
queries: {annotation: {links}},
})

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.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Include projectId in the annotations query key.

queryFn scopes queryAllAnnotations by projectId, but Line 177 omits it from queryKey. A project change with the same links can reuse annotations cached for the prior project. Add projectId to the key.

Proposed fix
-        queryKey: ["annotations", links],
+        queryKey: ["annotations", projectId, links],

As per coding guidelines, include all reactive dependencies in queryKey.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
queryKey: ["annotations", links],
queryFn: async () => {
if (Array.isArray(links) && !links.length) return [] as AnnotationDto[]
const res = await queryAllAnnotations({annotation: {links}})
const res = await queryAllAnnotations({
projectId: projectId ?? undefined,
queries: {annotation: {links}},
})
queryKey: ["annotations", projectId, links],
queryFn: async () => {
if (Array.isArray(links) && !links.length) return [] as AnnotationDto[]
const res = await queryAllAnnotations({
projectId: projectId ?? undefined,
queries: {annotation: {links}},
})

Source: Coding guidelines

Comment on lines +595 to +598
const ag = agOf(trace)
const tokens =
(attrs as any)?.ag?.metrics?.tokens?.incremental?.total ||
(attrs as any)?.ag?.metrics?.tokens?.cumulative?.total ||
ag?.metrics?.tokens?.incremental?.total ||
ag?.metrics?.tokens?.cumulative?.total ||

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.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Preserve zero-valued metrics.

|| treats 0 as missing. If an incremental metric is 0 and a cumulative metric exists, the session total uses the cumulative value. Use ?? for these fallback chains.

Proposed fix
-                ag?.metrics?.tokens?.incremental?.total ||
-                ag?.metrics?.tokens?.cumulative?.total ||
-                (attrs["ag.usage.total_tokens"] as number) ||
-                (attrs["total_tokens"] as number) ||
+                ag?.metrics?.tokens?.incremental?.total ??
+                ag?.metrics?.tokens?.cumulative?.total ??
+                (attrs["ag.usage.total_tokens"] as number) ??
+                (attrs["total_tokens"] as number) ??
                 0
...
-                ag?.metrics?.costs?.incremental?.total || ag?.metrics?.costs?.cumulative?.total || 0
+                ag?.metrics?.costs?.incremental?.total ??
+                ag?.metrics?.costs?.cumulative?.total ??
+                0

Also applies to: 611-613

@coderabbitai coderabbitai Bot 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.

Caution

Inline review comments failed to post. This is likely due to GitHub's internal server error or limits when posting large numbers of comments. If you are seeing this consistently it is likely a permissions issue. Please check "Moderation" -> "Code review limits" under your organization settings.

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
web/packages/agenta-observability/package.json (1)

26-51: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Commit the regenerated web/pnpm-lock.yaml.

The packages/agenta-observability importer does not include the five manifest entries. Frozen installs fail with ERR_PNPM_OUTDATED_LOCKFILE. Run pnpm install from web and commit the lockfile.

Source: Pipeline failures

🧹 Nitpick comments (1)
web/packages/agenta-observability/src/index.ts (1)

1-3: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Reduce the new multiline comments.

These comments do not document a bug, race, or ordering constraint. Replace them with one short line, or remove them when the code is self-explanatory.

  • web/packages/agenta-observability/src/index.ts#L1-L3: Replace this and the repeated three-line section banners with one short section comment or no comment.
  • web/packages/agenta-observability/src/core/traceSpan.ts#L4-L7: Reduce the interface documentation to one short line.
  • web/oss/src/services/tracing/types/index.ts#L1-L2: Remove or reduce the re-export explanation to one short line.
  • web/oss/src/state/observability/useObservability.ts#L1-L6: Reduce the wrapper description to one short line.

As per coding guidelines, “Keep in-code comments to at most one short line; use longer comments only for genuinely surprising constraints such as bugs, races, or ordering requirements.”

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 585ef4fb-4796-4df9-8471-71555da7de3a

📥 Commits

Reviewing files that changed from the base of the PR and between 307b66d and 6b4cc1b.

📒 Files selected for processing (30)
  • web/oss/src/components/Filters/Sort.tsx
  • web/oss/src/lib/Types.ts
  • web/oss/src/services/tracing/types/index.ts
  • web/oss/src/state/Providers.tsx
  • web/oss/src/state/newObservability/index.ts
  • web/oss/src/state/observability/atoms.ts
  • web/oss/src/state/observability/index.ts
  • web/oss/src/state/observability/seams.ts
  • web/oss/src/state/observability/useObservability.ts
  • web/packages/agenta-observability/package.json
  • web/packages/agenta-observability/src/api/queryHelpers.ts
  • web/packages/agenta-observability/src/core/constants.ts
  • web/packages/agenta-observability/src/core/traceSpan.ts
  • web/packages/agenta-observability/src/core/types.ts
  • web/packages/agenta-observability/src/etl/adaptiveExportPacing.ts
  • web/packages/agenta-observability/src/etl/adaptiveTracePageFetcher.ts
  • web/packages/agenta-observability/src/etl/exportWriter.ts
  • web/packages/agenta-observability/src/etl/withRateLimitRetry.ts
  • web/packages/agenta-observability/src/hooks/useObservability.ts
  • web/packages/agenta-observability/src/hooks/useSessions.ts
  • web/packages/agenta-observability/src/index.ts
  • web/packages/agenta-observability/src/state/controls.ts
  • web/packages/agenta-observability/src/state/queries.ts
  • web/packages/agenta-observability/src/state/seams.ts
  • web/packages/agenta-observability/src/state/selectors.ts
  • web/packages/agenta-observability/src/utils/buildTraceQueryParams.ts
  • web/packages/agenta-observability/src/utils/filterCoercion.ts
  • web/packages/agenta-observability/tests/unit/queryHelpers.test.ts
  • web/packages/agenta-observability/tsconfig.json
  • web/packages/agenta-observability/vitest.config.ts
💤 Files with no reviewable changes (1)
  • web/oss/src/state/newObservability/index.ts
🛑 Comments failed to post (1)
web/packages/agenta-observability/src/etl/withRateLimitRetry.ts (1)

53-59: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Honor a zero-second Retry-After value.

Retry-After: 0 means retry immediately. The current > 0 checks use the 10-second fallback instead. Accept zero for both header and message values.

Proposed fix
-    if (Number.isFinite(headerSeconds) && headerSeconds > 0) {
+    if (Number.isFinite(headerSeconds) && headerSeconds >= 0) {
         delayMs = headerSeconds * 1000
...
-            if (Number.isFinite(seconds) && seconds > 0) delayMs = seconds * 1000
+            if (Number.isFinite(seconds) && seconds >= 0) delayMs = seconds * 1000
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

    if (Number.isFinite(headerSeconds) && headerSeconds >= 0) {
        delayMs = headerSeconds * 1000
    } else {
        const match = (e?.message ?? "").match(/retry after (\d+)\s*second/i)
        if (match) {
            const seconds = Number.parseInt(match[1], 10)
            if (Number.isFinite(seconds) && seconds >= 0) delayMs = seconds * 1000

@ardaerzin

Copy link
Copy Markdown
Contributor Author

Superseded by #6065, which carries this work reconciled onto main. Verified before closing: merging this branch into #6065's head produces no change to the tree, so nothing here is lost. Closing without merging; the branch stays put.

@ardaerzin ardaerzin closed this Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactoring A code change that neither fixes a bug nor adds a feature size:XL This PR changes 500-999 lines, ignoring generated files. typescript Pull requests that update typescript code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant