Skip to content

Teams cost - #17

Open
Felixoid wants to merge 2 commits into
Tiziano-AI:mainfrom
Felixoid:teams-cost
Open

Teams cost#17
Felixoid wants to merge 2 commits into
Tiziano-AI:mainfrom
Felixoid:teams-cost

Conversation

@Felixoid

Copy link
Copy Markdown
Contributor

Summary

Adds token usage and cost tracking for child Pi processes launched by agent_team. Each message_end RPC record carries the child's AssistantMessage.usage payload; RpcUsageTracker (in rpc-message-end-handler.ts) accumulates it synchronously as steps run. On step completion the cost flows into StepState, surfaces as StepSnapshot.usage (per-step) and RunSnapshot.teamUsage (run total), and appears in run_status/step_result model output.

In interactive Pi, a separate footer status line (team: $X.XXX ↑Xk ↓Xk ...) accumulates across all agent_team runs in the session via ctx.ui.setStatus. This is distinct from the parent session's own $X.XXX cost counter and is controlled by the new agent_team:footer-cost boolean flag (default true).

Scope

Proposal issue, if required:

Checklist

  • This branch is based on current origin/main. It based on Fix gate tests for npm --json output format change from array to object #16
  • The PR is scoped to one coherent change.
  • I did not bump package.json version.
  • I did not create a dated release section in CHANGELOG.md.
  • User-visible unreleased changes are recorded under ## Unreleased, if applicable.
  • I did not reintroduce removed compatibility paths, old action names, stale trust gates, or non-enforced authority paperwork.
  • I did not weaken role capability truth: package:validator requires effective bash; package:worker requires effective edit or write.
  • I did not run npm publish, create tags, push release branches, or create GitHub Releases.

Design-gate check

This PR changes any of the following:

  • public agent_team actions or schema
  • graph authority, tool/capability policy, or package-agent semantics
  • persistence, recovery, reattach, cleanup, or retention
  • mutation/edit behavior, git worktrees, or patch handling
  • scheduling/background/repeated execution
  • provider/model/extension discovery policy
  • secret, credential, environment, or filesystem trust boundaries

No design-gate boxes apply. teamUsage and usage are additive read-only fields on existing snapshot types; no action schemas, authority gates, or persistence paths change. Here's the proposal issue #15

Validation

Paste fresh command output or explain why a command is deferred.

pnpm run typecheck
pnpm test
pnpm run gate
git diff --check

They pass clean, but don't fit the limit.

$ tsc --noEmit
$ node --import ./tests/node-test-ref-timers.mjs --no-warnings --experimental-strip-types --loader ./tests/pi-peer-loader.mjs --test tests/*.test.ts
....
ℹ tests 269
ℹ suites 0
ℹ pass 269
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 11290.306636
$ pnpm run typecheck && pnpm test && pnpm run smoke:fake-pi && pnpm run check:pack && pnpm run check:pi-load && pnpm run check:public-docs && pnpm run check:source-size
$ tsc --noEmit
$ node --import ./tests/node-test-ref-timers.mjs --no-warnings --experimental-strip-types --loader ./tests/pi-peer-loader.mjs --test tests/*.test.ts
.....
ℹ tests 269
ℹ suites 0
ℹ pass 269
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 11634.507153
$ node --no-warnings --experimental-strip-types --loader ./tests/pi-peer-loader.mjs tests/smoke-fake-pi.ts
$ node --experimental-strip-types tests/check-pack.ts
$ node --no-warnings --experimental-strip-types --loader ./tests/pi-peer-loader.mjs tests/check-package-load.ts
$ node --experimental-strip-types tests/check-public-docs.ts
$ node --experimental-strip-types tests/check-source-size.ts
Time: 41.92 s.

Notes for reviewers

  • Cost source: usage is accumulated from message_end RPC records (AssistantMessage.usage), not from a get_session_stats fetch. This avoids any async race between a stats request and natural child close. If a step produces no message_end records with usage (e.g. fake child in tests), usage stays undefined and no footer write occurs.
  • Partial cost on failure/cancel/timeout: RpcUsageTracker captures all message_end events received before termination; partial cost is preserved.
  • Footer state: sessionTeamUsage and countedRunsBySession are in-memory maps in the extension closure, cleared on session_shutdown. The status line is only written when a run terminates with real usage, and only cleared on shutdown if cost was ever accumulated.
  • Flag: agent_team:footer-cost false disables the footer line going forward; it does not retroactively clear an already-set status.
  • rendering.test.ts: the setStatus guard now scans src/ (implementation files must not touch UI) plus a positive assertion that any setStatus in index.ts must use FOOTER_COST_FLAG.
  • check-source-size.ts: two files exceeded the 18432-byte budget after additions; extracted RpcUsageTracker into rpc-message-end-handler.ts and token/cost formatters into result-model-text.ts.

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.

1 participant