An open-source, faster Devin alternative
TypeScript
fix: surface incomplete agent usage honestly
Igor Zalutskid29a59691846
diff --git a/docs/agent-sessions/api-reference.mdx b/docs/agent-sessions/api-reference.mdxindex c74a82d8..e02d214b 100644--- a/docs/agent-sessions/api-reference.mdx+++ b/docs/agent-sessions/api-reference.mdx@@ -185,7 +185,7 @@ A built-in runtime session pins the agent's active revision for its whole life.| `error?` | failure detail, if any || `started_at?` `completed_at?` | timing || `active_seconds` | database-derived active runtime seconds |-| `usage` | `{ reported:true, …tokens, total_cost_usd? }` or `{ reported:false }` |+| `usage` | `{ reported:true, complete?:false, …tokens, total_cost_usd? }` or `{ reported:false }`; `complete:false` marks a lower bound |Session usage includes `reported_turns`, `unreported_turns`, and `complete`.When `complete:false`, any token/cost total present is a lower bound. Missingdiff --git a/docs/agent-sessions/hooks.mdx b/docs/agent-sessions/hooks.mdxindex a6b7ee30..4a81ed77 100644--- a/docs/agent-sessions/hooks.mdx+++ b/docs/agent-sessions/hooks.mdx@@ -95,6 +95,27 @@ token and grants no session read or steer access. Watch the session in thedashboard or configure an outbound [webhook](/agent-sessions/webhooks) forresults.+## Common senders++Create a separate named Hook for each sender so it can be rotated or revoked+without interrupting the others.++- **Grafana:** Add a [Webhook contact point](https://grafana.com/docs/grafana/latest/alerting/configure-notifications/manage-contact-points/integrations/webhook-notifier/), use the Hook URL, and keep the method as `POST`. Grafana sends its alert JSON directly. Its standard webhook does not provide a supported delivery-id header; see [Retries and duplicates](#retries-and-duplicates).+- **Zapier:** Add a [Webhooks by Zapier](https://help.zapier.com/hc/en-us/articles/8496326446989-Send-webhooks-in-Zap-workflows) `POST` action, paste the Hook URL, select a JSON payload, and map fields from earlier steps. No separate authentication field is needed because the secret is already in the URL.+- **Supabase:** Create a [Database Webhook](https://supabase.com/docs/guides/database/webhooks), select the table and `INSERT`, `UPDATE`, or `DELETE` events, choose `POST`, and paste the Hook URL. Supabase sends the row-change JSON as the payload.+- **CI:** Store the complete Hook URL as a secret and send one JSON request. Use a stable delivery id for retries of the same run:++```yaml GitHub Actions+- name: Notify agent+ env:+ HOOK_URL: ${{ secrets.OC_AGENT_HOOK_URL }}+ run: |+ curl --fail-with-body "$HOOK_URL" \+ -H "Content-Type: application/json" \+ -H "Idempotency-Key: ci-$GITHUB_RUN_ID-$GITHUB_RUN_ATTEMPT" \+ -d "{\"repository\":\"$GITHUB_REPOSITORY\",\"sha\":\"$GITHUB_SHA\"}"+```+## Retries and duplicatesFor retry-safe delivery, send `Idempotency-Key`; when absent, OpenComputer usesdiff --git a/docs/agent-sessions/sessions.mdx b/docs/agent-sessions/sessions.mdxindex 9660eb3c..b79b8a0d 100644--- a/docs/agent-sessions/sessions.mdx+++ b/docs/agent-sessions/sessions.mdx@@ -100,9 +100,10 @@ Three independent knobs — don't overload one for another:Terminal turns expose `active_seconds` and normalized `usage`. A reported turncontains four disjoint token components, their `tokens` sum, and optional-`total_cost_usd`. Missing cost means unknown, not zero. A turn without a-trustworthy runtime result returns `reported: false` instead of inventing a-zero.+`total_cost_usd`. Missing cost means unknown, not zero. If only some runtime+observations were trustworthy, the turn keeps that lower bound and adds+`complete: false`; a turn with no trustworthy observation returns+`reported: false` instead of inventing a zero.The session `usage` rollup contains:diff --git a/sdks/typescript/src/agents/normalize.test.ts b/sdks/typescript/src/agents/normalize.test.tsindex 71224c43..17921e30 100644--- a/sdks/typescript/src/agents/normalize.test.ts+++ b/sdks/typescript/src/agents/normalize.test.ts@@ -22,6 +22,7 @@ describe("normalize (API response → idiomatic TS)", () => {usage?: {activeSeconds?: number;reportedTurns?: number;+ complete?: boolean;totalCostUsd?: number;cacheReadInputTokens?: number;};@@ -29,6 +30,7 @@ describe("normalize (API response → idiomatic TS)", () => {usage: {active_seconds: 12,reported_turns: 2,+ complete: false,total_cost_usd: 0.42,cache_read_input_tokens: 17,},@@ -37,6 +39,7 @@ describe("normalize (API response → idiomatic TS)", () => {expect(out.usage).toEqual({activeSeconds: 12,reportedTurns: 2,+ complete: false,totalCostUsd: 0.42,cacheReadInputTokens: 17,});diff --git a/sdks/typescript/src/agents/types.ts b/sdks/typescript/src/agents/types.tsindex 5eabd18b..99badbff 100644--- a/sdks/typescript/src/agents/types.ts+++ b/sdks/typescript/src/agents/types.ts@@ -11,9 +11,11 @@ export interface Limits { tokens?: number; turnSeconds?: number; turns?: number;export type UsageAttribution = "exact" | "best_effort";-/** A terminal turn whose runtime reported a complete normalized token total. */+/** A terminal turn with at least one trustworthy normalized usage observation. */export interface ReportedTurnUsage {reported: true;+ /** False means the token/cost values present are lower bounds. */+ complete?: false;inputTokens: number;outputTokens: number;cacheCreationInputTokens: number;diff --git a/web/src/api/mock-contracts.test.ts b/web/src/api/mock-contracts.test.tsindex 6e2684b5..e7ef1e89 100644--- a/web/src/api/mock-contracts.test.ts+++ b/web/src/api/mock-contracts.test.ts@@ -7,6 +7,7 @@ import {SessionListSchema,SessionSourceListSchema,SessionTurnListSchema,+ TurnUsageSchema,} from './schemas'describe('preview API contracts', () => {@@ -40,4 +41,18 @@ describe('preview API contracts', () => {SessionTurnListSchema.parse(mockFetch('/v3/sessions/ses_a1b2c3/turns')),).not.toThrow()})++ it('accepts a reported turn whose total is an explicit lower bound', () => {+ expect(() =>+ TurnUsageSchema.parse({+ reported: true,+ complete: false,+ input_tokens: 10,+ output_tokens: 2,+ cache_creation_input_tokens: 0,+ cache_read_input_tokens: 0,+ tokens: 12,+ }),+ ).not.toThrow()+ })})diff --git a/web/src/api/schemas.ts b/web/src/api/schemas.tsindex b957a309..af5090f1 100644--- a/web/src/api/schemas.ts+++ b/web/src/api/schemas.ts@@ -908,6 +908,7 @@ export const AgentSnapshotSchema = z.object({const UsageAttributionSchema = z.enum(['exact', 'best_effort'])const ReportedTurnUsageSchema = z.object({reported: z.literal(true),+ complete: z.literal(false).optional(),input_tokens: z.number().int().nonnegative(),output_tokens: z.number().int().nonnegative(),cache_creation_input_tokens: z.number().int().nonnegative(),diff --git a/web/src/components/agent-security-alert.tsx b/web/src/components/agent-security-alert.tsxindex 18a7e465..75a8c08a 100644--- a/web/src/components/agent-security-alert.tsx+++ b/web/src/components/agent-security-alert.tsx@@ -70,8 +70,8 @@ export function AgentSecurityAlertBanner() {const { data } = useQuery({queryKey: ['agent-security-notifications'],queryFn: listAgentSecurityNotifications,- retry: false,- refetchInterval: (query) => (query.state.error ? false : 30_000),+ retry: 2,+ refetchInterval: 30_000,})const acknowledge = useMutation({mutationFn: acknowledgeAgentSecurityNotification,diff --git a/web/src/lib/usage.test.ts b/web/src/lib/usage.test.tsindex b177a5da..377034aa 100644--- a/web/src/lib/usage.test.ts+++ b/web/src/lib/usage.test.ts@@ -47,6 +47,21 @@ describe('canonical usage presentation', () => {)})+ it('marks a partially reported turn as a lower bound', () => {+ const usage = {+ reported: true as const,+ complete: false as const,+ input_tokens: 100,+ output_tokens: 20,+ cache_creation_input_tokens: 0,+ cache_read_input_tokens: 0,+ tokens: 120,+ }++ expect(formatUsageTokens(usage)).toBe('≥120')+ expect(usageDisclosure(usage)).toBe('Lower-bound usage')+ })+it('keeps missing and historical usage unknown', () => {expect(formatUsageTokens({})).toBe('Unknown')expect(formatUsageCost({ reported: false })).toBe('Unknown')