Skip to content

feat(sessions): GetTotalCostAsync, a session's cost with every descendant subagent session - #985

Merged
Blind-Striker merged 5 commits into
masterfrom
fix/971-session-total-cost
Nov 24, 2026
Merged

Blind-Striker merged 5 commits into
masterfrom
fix/971-session-total-cost

Conversation

@Blind-Striker

Copy link
Copy Markdown
Collaborator

📝 Description

What does this PR do?

It adds Sessions.GetTotalCostAsync: a session's total cost including its subagent sessions, as opencode's own first-party client computes it. The source is the client data store's cost(), packages/client/src/solid/data.ts:1582-1590 at the v2.3.2 pin.

  • The rule:

    • a session with a parent returns its own Cost;
    • any other session returns its own Cost plus the Cost of every descendant session, the subtree upstream's store converges on as it loads them.
  • How it reads:

    • it reads the descendants depth-first;
    • each level goes through the children traversal with ParentId, descending order and pages of 200, as upstream's family sync reads a level;
    • each level is read in full before the walk descends;
    • a visited set counts each session once, even if the server returns a cycle.
  • Errors: NoThrow is refused at the call, and every failure propagates.

  • The code: the public door is a thin extension, SessionsClientExtensions, over an internal SessionCostTotal.

  • One recorded divergence (ADR-0031), in docs/architecture/client-runtime.md, section Pagination: upstream's store returns 0 for a session it has not loaded; the SDK fails with the server's not-found error, because it asks the server, where an absent session is a wrong identifier. The entry names its reversal trigger.

  • The source watch: upstream's packages/client/src/solid/data.ts is now watched. There are three behaviours, anchored at:

    • the cost rule;
    • the 200-per-page children read;
    • the registration that files every loaded session under its root.

    The entry was added through refresh-spec at the unchanged pin, so spec/receipt.json gains its record and spec/SNAPSHOT.md gets the tool's Date: line.

    • At an unchanged pin, the receipt's addedOperations and removedOperations are empty, because there is no delta.
    • The v2.3.0 → v2.3.2 delta stays in the refresh commit's receipt and message.
    • Nothing reads the committed arrays except the refresh command's console summary.
  • Docs:

    • the canon (Pagination, plus the Error channels list of doors that refuse NoThrow);
    • the consumer guide's NoThrow limits (docs/guide/errors-and-responses.md);
    • CHANGELOG.md under Added.

Related Issue(s):

🔄 Type of Change

  • ✨ New feature (non-breaking change that adds functionality)

🎯 Protocol & API Surface

  • Public API changed deliberately: tests/OpenCode.Sdk.Tests/Snapshots/PublicApi.verified.txt gains SessionsClientExtensions.GetTotalCostAsync. The Extensions package is unchanged.
  • No generated output changed
  • dotnet run --file tools/opencode-tool.cs -- generate --verify passes
  • The pinned snapshot (spec/) changed through refresh-spec at the unchanged pin, following spec/SNAPSHOT.md. refresh-spec --verify passes.
  • Nothing under external/ was hand-edited

🧪 Testing

How has this been tested?

  • Unit / contract tests added (SessionsClientExtensionsTests, canned HTTP):
    • a root with 250 children over two pages, total 250.5, with the exact ordered request list;
    • a child's own cost, with one request;
    • a grandchild, 1 + 2 + 3;
    • a cycle, counted once, for a matching and a differing returned id;
    • not-found propagation;
    • the NoThrow refusal and an invalid id, both at the call;
    • root-first summation order;
    • options, token and failure identity at every level, through the mocking seam.
  • Integration tests added (SessionsClientExtensionsLiveTests): a real subagent child, made by driving the built-in subagent tool through the simulated server. It proves the parent filter and the walk.
    • At the pin the simulation reports no token usage, so its costs are 0. The contract tests prove the non-zero arithmetic.
  • Mutation proof: 14 mutants, each built analyzer-clean and killed at its assertion on net10.0 and net11.0. They cover:
    • dropped pages;
    • the child early return;
    • the root's own cost;
    • the page size, the filter and the order;
    • a swallowed not-found;
    • the summation order;
    • an asynchronous id check;
    • stopping at depth 1;
    • no visited set;
    • lost options or token below the root;
    • a swallowed failure below the root;
    • descending before a level is fully read.
  • Verified against a real opencode serve 2.3.2 (the simulated server)

Test Environment:

  • opencode server version: 2.3.2
  • Target frameworks tested: net10.0, net11.0 (net472 through the Windows replay)
  • Operating systems: macOS (arm64)

✅ Completion Gate

  • The local check in quality-gates.md passes on the pushed head:
    • slopwatch, the Release build with 0 warnings, both format passes, and the clone gate with 0 new;
    • Tools 2,875/2,875;
    • SDK 4,977/4,977 on net10.0 and on net11.0;
    • --help, generate --verify and refresh-spec --verify.

📚 Documentation

  • XML documentation comments added for the public API
  • The consumer guide under docs/guide/ updated
  • CHANGELOG.md entry added

✅ Code Quality Checklist

  • No new analyzer warnings; no suppression
  • No tests skipped, weakened, or deleted
  • Branch is up to date with master
  • Conventional Commits, no AI-attribution trailers

@Blind-Striker Blind-Striker added this to the 1.x milestone Nov 23, 2026
@Blind-Striker
Blind-Striker added this pull request to the merge queue Nov 24, 2026
Merged via the queue into master with commit 9ab6ff1 Nov 24, 2026
27 of 29 checks passed
@Blind-Striker
Blind-Striker deleted the fix/971-session-total-cost branch November 24, 2026 08:28
@Blind-Striker Blind-Striker mentioned this pull request Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

parity: a session's total cost including its subagents, as upstream's client data store computes it

1 participant