Skip to content

fix: prefer unified sessions for subagent attribution - #66746

Merged
pelikhan merged 8 commits into
mainfrom
pelikhan-unified-subagent-sessions
Oct 8, 2026
Merged

pelikhan merged 8 commits into
mainfrom
pelikhan-unified-subagent-sessions

Conversation

@pelikhan

@pelikhan pelikhan commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

Copilot subagent attribution currently guesses name(model) pairs from stdout, which can turn startup logs and printed code into fake agents while missing current CLI dispatch formats. This change makes structured unified sessions the primary source and keeps legacy logs as a backward-compatible fallback.

Approach

  • Read usage/aw_session.jsonl first, then the canonical agent-session.jsonl trace. Structured sessions without subagents suppress heuristic attribution; malformed structured evidence emits a warning before fallback.
  • Preserve Copilot subagent identity, parentage, model and effort, execution mode, model-selection source, first dispatched model, and assistant request-correlation metadata in unified artifacts. Project exact per-agent accounting snapshots from shutdown into session.result.agentMetrics without adding them to aggregate session usage.
  • Populate existing attribution fields from lifecycle events and subagent-only request counts, excluding main-agent traffic. Restrict legacy inference to CLI dispatch-marker lines in either supported format.
  • Update session declarations, generated schemas, documentation, and the patch changeset.

Integration coverage

Reuse the existing embedded session-parser harness to exercise Copilot native events, bootstrap traces, and structured logs through unified serialization into audit attribution. Cover nested agents, exact token and credit snapshots, snapshot deduplication, missing accounting, main-only sessions, and both legacy dispatch formats.

Validation

  • Shared session-parser and focused subagent attribution Go tests passed.
  • Focused JavaScript parser, projection, and schema tests passed, along with TypeScript checking.
  • Change-scoped formatting, lint, schema freshness, and workflow drift checks passed.

Scope

Addresses the session-source and persistence portions of #66740. Per-request proxy joins, routing-cost splits, and new per-agent reporting tables remain outside this change.

pelikhan and others added 2 commits October 7, 2026 18:56
Preserve Copilot subagent metadata and per-agent accounting snapshots in unified sessions. Read structured attribution before legacy logs and limit heuristic parsing to CLI dispatch lines.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Reuse the embedded session-parser harness to verify native, bootstrap, and structured-log reconstruction through unified artifacts into audit attribution. Cover nested agent metadata, exact accounting snapshots, missing accounting, main-only sessions, and legacy dispatch fallback.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@pelikhan
pelikhan marked this pull request as ready for review October 8, 2026 02:05
Copilot AI balanced review requested due to automatic review settings October 8, 2026 02:05
@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

✅ Design Decision Gate 🏗️ completed the design decision gate check. See the comment below for the result and any generated ADR draft.

🏗️ ADR gate enforced by Design Decision Gate 🏗️

@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

✅ PR Code Quality Reviewer completed the code quality review.

Testing safeoutputs availability for required completion signaling.

🔎 Code quality review by PR Code Quality Reviewer

@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

✅ Test Quality Sentinel completed test quality analysis.

Test Quality Sentinel skipped because pre-fetch PR data was unavailable: unable to fetch test file diff

🧪 Test quality analysis by Test Quality Sentinel

@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

✅ Ponytail Reviewer completed successfully!

Lean already. Ship.

Generated by Ponytail Reviewer for #66746

@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

🧠 Matt Pocock Skills Reviewer is reviewing this pull request using Matt Pocock's engineering skills...

@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

🏗️ Design Decision Gate: ADR Required

This PR triggers ADR enforcement, and no Architecture Decision Record was found.

Why enforcement applies

  • .design-gate.yml is absent, so default rules apply.
  • 570 added lines in business logic directories (pkg/), above the 100-line threshold.
  • No implementation label is present, but the volume condition alone is sufficient.

ADR search results

Action taken

A draft ADR has been committed to this branch:

docs/adr/66746-prefer-unified-sessions-for-subagent-attribution.md

It records the decision inferred from the diff: treat structured unified sessions (usage/aw_session.jsonl, then agent-session.jsonl) as the authoritative source for Copilot subagent model attribution, persist subagent lifecycle/correlation metadata and the exact agentMetrics snapshot, and narrow legacy agent-stdio.log inference to CLI dispatch-marker lines.

Evidence used:

  • pkg/cli/token_usage_subagent_session.go (+240) — new structured attribution reader.
  • pkg/cli/token_usage_subagent.go (+52/-35) — structured-first with heuristic fallback; dispatch regex anchored to ● and (model: ...).
  • actions/setup/js/copilot_session.cjs, copilot_workflow_events.cjs, unified_session_payload.cjs — retain agentMetrics, agentType, modelSelectionSource, firstDispatchedModel, reasoningEffort, and assistant request-correlation fields.
  • Regenerated docs/public/schemas/agent-session.schema.json and unified-session.schema.json; spec update in docs/src/content/docs/specs/unified-agent-session-specification.md.

Next action for the author

Review the draft ADR, correct anything the gate inferred incorrectly (especially the decider list and the rejected alternatives), and change Status from Draft to Proposed or Accepted before merge.

🏗️ ADR gate enforced by Design Decision Gate 🏗️ · pi · opus50 · 56.8 AIC · ⌖ 50.8 AIC · ⊞ 1.7K · ◷
Comment /review to run again

Copilot AI 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.

🟡 Changes recommended

Retry attempts can leak stale subagent attribution into the final attempt’s token summary.

1 open finding
What changed in this PR

Prioritizes structured unified sessions for accurate Copilot subagent attribution while retaining legacy log fallback.

Changes:

  • Derives subagent models and request counts from session lifecycle/accounting data.
  • Preserves attribution metadata and updates schemas/documentation.
  • Adds Go and JavaScript integration coverage.
File Description
pkg/​cli/​token_usage_subagent.go Prefers structured attribution and narrows fallback matching.
pkg/​cli/​token_usage_subagent_session.go Parses session lifecycle and accounting data.
pkg/​cli/​token_usage_subagent_session_test.go Tests structured attribution behavior.
pkg/​cli/​session_parser_test.go Adds end-to-end Copilot session coverage.
docs/​src/​content/​docs/​specs/​unified-agent-session-specification.md Documents retained subagent evidence.
docs/​public/​schemas/​unified-session.schema.json Extends unified session schema.
docs/​public/​schemas/​agent-session.schema.json Adds reasoning effort and agent metrics.
actions/​setup/​js/​unified_session_payload.test.cjs Updates payload normalization expectations.
actions/​setup/​js/​unified_session_payload.cjs Retains attribution and correlation fields.
actions/​setup/​js/​types/​unified_session.d.ts Extends unified session types.
actions/​setup/​js/​types/​agent_session.d.ts Extends canonical session types.
actions/​setup/​js/​copilot_workflow_events.cjs Retains additional lifecycle metadata.
actions/​setup/​js/​copilot_session.cjs Projects per-agent accounting snapshots.
actions/​setup/​js/​copilot_dynamic_workflow.test.cjs Tests Copilot metadata projection.
.changeset/​patch-unified-subagent-sessions.md Records the patch release change.

🧠 Review effort: Balanced


Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.

Comment thread pkg/cli/token_usage_subagent_session.go
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Comment Memory

Peek at saved memory (pr-code-quality-reviewer)
reviewed_at: 2026-10-08T02:07:39Z
review_event: REQUEST_CHANGES
top_themes:
  - structured-source fallback bypasses canonical trace on parse errors
  - repeated subagent requests are no longer aggregated
files_reviewed:
  - pkg/cli/token_usage_subagent_session.go
  - pkg/cli/token_usage_subagent.go
  - pkg/cli/session_parser_test.go
  - actions/setup/js/unified_session_payload.cjs
  - actions/setup/js/copilot_workflow_events.cjs
comment_count: 3

Note

This comment is managed by comment memory.

Expand the saved memory block to view or edit the persistent context for this thread.
Edit only the text inside the backtick fences; workflow metadata and the footer are regenerated automatically.

Learn more about comment memory

🔎 Code quality review by PR Code Quality Reviewer · copilot · gpt54 · 63.7 AIC · ⌖ 5.89 AIC · ⊞ 19.8K · ◷
Comment /review to run again

@github-actions github-actions 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.

Request changes

The new structured attribution path is close, but there are still blocking correctness gaps in how it falls through sources and publishes repeated subagent requests.

Blocking themes
  • A malformed preferred artifact currently short-circuits before agent-session.jsonl, so we can still regress to the lossy stdio heuristic even when valid structured evidence is present.
  • The structured path emits one row per agent id instead of aggregating identical logical subagent requests, which changes the public subagent_model_requests shape compared with the legacy path.

🔎 Code quality review by PR Code Quality Reviewer · copilot · gpt54 · 63.7 AIC · ⌖ 5.89 AIC · ⊞ 19.8K
Comment /review to run again

Comment thread pkg/cli/token_usage_subagent_session.go
Comment thread pkg/cli/token_usage_subagent_session.go
Comment thread pkg/cli/token_usage_subagent_session.go
@gh-aw-bot

Copy link
Copy Markdown
Collaborator

@copilot address the following outstanding work in one pass:

  1. Update this branch with the latest main using make merge-main, resolving any conflicts and preserving the intended changes.
  2. Review (pkg/cli/token_usage_subagent_session.go:142): A new session.init only marks the stream as found; it never scopes or resets agents and actualCounts. Copilot's canonical/unified session intentionally contains every retry attempt, while run telemetry accounts only the chronological final attempt. If an earlier attempt starts a subagent and the final attempt has none (or uses different IDs), that earlier subagent remains in these maps and is reported against the final attempt's totals. Track session/provenance identity and select the final session's snapshot, or clear prior-attempt state at the appropriate session boundary. - fix: prefer unified sessions for subagent attribution #66746 (comment)
  3. Review (pkg/cli/token_usage_subagent_session.go:60): Test comment from automation. - fix: prefer unified sessions for subagent attribution #66746 (comment)
  4. Review (pkg/cli/token_usage_subagent_session.go:60): Returning on the first parse error here means a broken usage/aw_session.jsonl skips a valid agent-session.jsonl and sends attribution back to the lossy stdio heuristic. - fix: prefer unified sessions for subagent attribution #66746 (comment)
  5. Review (pkg/cli/token_usage_subagent_session.go:193): This keys request rows by transient agent id and then emits them verbatim, so repeated launches of the same subagent/model pair become multiple rows with invocation_count=1 instead of one aggregated row. - fix: prefer unified sessions for subagent attribution #66746 (comment)

Push the necessary fixes, reply to each listed review thread and resolve it when addressed. Ignore feedback already answered or resolved. Use the pr-finisher skill and stop when only human review or CI remains; do not trigger CI.

Sous-chef head: ca4eddc
Sous-chef work: 0eca8b73851b33409687e5edb17c5051f9a6e2a47ee8197b31fbf9190a1c05e8 529828099d211bbf1d2f74927fc352a878992681ca07f7b06e4936c9aabdce9b 5ad72e0801b7bde1b024f1e0dc3e09089f07bb503a02c719893ff65998569218 c70f4e0919aca8befdcd85679af746c5b034618cb82be1b00ee2e6299e26d221
Sous-chef state: d5132314e31e7cbcc0a6360993eafcf67ca6ac92025deaf90a7927ace12aeeb9

Generated by 👨‍🍳 PR Sous Chef · pi · haiku45 · 9.71 AIC · ⌖ 18.9 AIC · ⊞ 1K · ◷
Comment /souschef to run again

Copilot AI and others added 2 commits October 8, 2026 02:54
…gent-sessions

Co-authored-by: gh-aw-bot <259018956+gh-aw-bot@users.noreply.github.com>
Co-authored-by: gh-aw-bot <259018956+gh-aw-bot@users.noreply.github.com>
pelikhan and others added 2 commits October 7, 2026 20:40
Preserve retry source scopes in structured attribution and publish subagent hierarchy, model configuration, requests, tokens, and credits in session step summaries.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@pelikhan
pelikhan merged commit a20907f into main Oct 8, 2026
72 of 73 checks passed
@pelikhan
pelikhan deleted the pelikhan-unified-subagent-sessions branch October 8, 2026 04:00
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

🎉 This pull request is included in a new release.

Release: v0.91.6

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.

4 participants