Skip to content

feat: track context window utilization - #2064

Merged
bradygaster merged 2 commits into
github:devfrom
anshi2018:anshi2018/1718-context-gauge
Oct 2, 2026
Merged

bradygaster merged 2 commits into
github:devfrom
anshi2018:anshi2018/1718-context-gauge

Conversation

@anshi2018

Copy link
Copy Markdown
Contributor

What

Measure per-session context-window utilization from exact Copilot runtime events, with a token-based estimate when exact data is unavailable, and surface threshold warnings and observability.

Why

Squad can select a context tier but previously had no gauge showing how much of the context window was occupied. This change makes context pressure measurable so routing and escalation decisions can be evaluated before exhaustion.

Closes #1718

How

  • Track exact session.usage_info samples and estimated fallback utilization per session.
  • Give exact runtime data precedence over estimates until session reset.
  • Emit crossing-only warnings with recovery and configurable thresholds.
  • Add typed context:utilization runtime events and the squad.context.utilization OpenTelemetry gauge.
  • Expose context-utilization accessors through StreamingPipeline.
  • Surface exact interactive-shell warnings and estimated squad watch summaries.
  • Capture and clean up Copilot usage-output files without changing custom agent commands.
  • Document the SDK, CLI, configuration, and environment variable.
  • Add adapter, runtime, shell, watch, event, and metric tests plus a package changeset.

Validation

  • npm run build -w @bradygaster/squad-sdk
  • npm run build -w @bradygaster/squad-cli
  • npm run lint
  • Focused run: 253 passed, 1 todo
  • One unrelated pre-existing Windows path-separator assertion remains in checks .squad/ralph-instructions.md inside teamRoot; all context-utilization tests pass.

Quick Check

  • SDK/CLI source changes include .changeset/1718-context-utilization.md

PR Readiness Checklist

Branch & Commit

  • Branch created from and rebased onto dev
  • Verified diff contains only intended changes
  • PR is not in draft mode
  • Commit history is clean

Build & Test

  • SDK and CLI builds pass
  • Context-utilization tests pass
  • npm run lint passes
  • npm run lint:eslint passes

Changeset

  • Changeset added

Docs

  • CLI, configuration, and SDK reference documentation updated

Breaking Changes

None.

Waivers

None.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: c8ae7013-1eb9-4f99-8762-ea407e3633f3
@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

🏗️ Architectural Review

Authoritative evidence: base-controlled pull_request_target reporter.

⚠️ Architectural review: 2 warning(s).

Severity Category Finding Files
🟡 warning export-surface Package entry point(s) modified with 21 new/changed export(s). New public API surface requires careful review for backward compatibility. packages/squad-sdk/src/index.ts
🟡 warning sweeping-refactor This PR touches 21 files (21 modified/added, 0 deleted). Large PRs are harder to review — consider splitting if possible. —

Automated architectural review — informational only.

@github-actions

Copy link
Copy Markdown
Contributor

🟠 Impact Analysis — PR #2064

Risk tier: 🟠 HIGH

📊 Summary

Metric Count
Files changed 21
Files added 2
Files modified 19
Files deleted 0
Modules touched 5
Critical files 2

🎯 Risk Factors

  • 21 files changed (21-50 → HIGH)
  • 5 modules touched (5-8 → HIGH)
  • Critical files touched: packages/squad-cli/src/cli/shell/index.ts, packages/squad-sdk/src/index.ts

📦 Modules Affected

docs (3 files)
  • docs/src/content/docs/reference/cli.md
  • docs/src/content/docs/reference/config.md
  • docs/src/content/docs/reference/sdk.md
root (1 file)
  • .changeset/1718-context-utilization.md
squad-cli (3 files)
  • packages/squad-cli/src/cli/commands/watch/agent-spawn.ts
  • packages/squad-cli/src/cli/commands/watch/capabilities/execute.ts
  • packages/squad-cli/src/cli/shell/index.ts
squad-sdk (7 files)
  • packages/squad-sdk/src/adapter/client.ts
  • packages/squad-sdk/src/index.ts
  • packages/squad-sdk/src/runtime/context-utilization.ts
  • packages/squad-sdk/src/runtime/event-bus.ts
  • packages/squad-sdk/src/runtime/event-payloads.ts
  • packages/squad-sdk/src/runtime/otel-metrics.ts
  • packages/squad-sdk/src/runtime/streaming.ts
tests (7 files)
  • test/cli/watch-agent-spawn.test.ts
  • test/cli/watch-capabilities.test.ts
  • test/event-bus.test.ts
  • test/otel-metrics.test.ts
  • test/repl-streaming.test.ts
  • test/session-adapter.test.ts
  • test/streaming.test.ts

⚠️ Critical Files

  • packages/squad-cli/src/cli/shell/index.ts
  • packages/squad-sdk/src/index.ts

This report is generated automatically for every PR. See #733 for details.

@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

🛫 PR Readiness Check

ℹ️ This comment updates on each push. Last checked: commit c3b5680

PR Scope: 📦🔧 Mixed (product + infrastructure)

⚠️ 3 item(s) to address before review

Status Check Details
❌ Single commit 2 commits — consider squashing before review
✅ Not in draft Ready for review
✅ Branch up to date Up to date with dev
❌ Copilot review No Copilot review yet — it may still be processing
✅ Changeset present Changeset file found
✅ Scope clean No .squad/ or docs/proposals/ files
✅ No merge conflicts No merge conflicts
✅ Copilot threads resolved No Copilot review threads
❌ CI passing 3 check(s) still running

Files Changed (21 files, +1085 −21)

File +/−
.changeset/1718-context-utilization.md +6 −0
docs/src/content/docs/reference/cli.md +1 −0
docs/src/content/docs/reference/config.md +1 −0
docs/src/content/docs/reference/sdk.md +22 −1
packages/squad-cli/src/cli/commands/watch/agent-spawn.ts +97 −4
packages/squad-cli/src/cli/commands/watch/capabilities/execute.ts +96 −7
packages/squad-cli/src/cli/shell/index.ts +91 −5
packages/squad-sdk/src/adapter/client.ts +1 −0
packages/squad-sdk/src/index.ts +1 −0
packages/squad-sdk/src/runtime/context-utilization.ts +161 −0
packages/squad-sdk/src/runtime/event-bus.ts +2 −1
packages/squad-sdk/src/runtime/event-payloads.ts +16 −0
packages/squad-sdk/src/runtime/otel-metrics.ts +36 −0
packages/squad-sdk/src/runtime/streaming.ts +113 −1
test/cli/watch-agent-spawn.test.ts +106 −1
test/cli/watch-capabilities.test.ts +90 −0
test/event-bus.test.ts +23 −0
test/otel-metrics.test.ts +32 −0
test/repl-streaming.test.ts +11 −0
test/session-adapter.test.ts +32 −0
test/streaming.test.ts +147 −1

Total: +1085 −21


This check runs automatically on every push. Fix any ❌ items and push again.
See CONTRIBUTING.md and PR Requirements for details.

@anshi2018
anshi2018 marked this pull request as ready for review September 24, 2026 19:12
@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

👋 Friendly nudge — this PR has had no activity for 7 days.

What needs attention:

  • 👀 No approving reviews yet. Request a review from a teammate.
  • ⬇️ 145 commits behind dev. Rebase to pick up latest changes.

If this PR is abandoned, please close it. If it's blocked on something external, leave a comment so the team knows.
This is an automated check that runs on weekdays. It won't nudge the same PR more than once per week.

@bradygaster
bradygaster merged commit c4949e9 into github:dev Oct 2, 2026
17 checks passed
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.

Context tier is settable but never measured - there is a dial with no gauge

2 participants