Skip to content

feat(accounting): report client compaction to the accountant (#2137) - #2145

Merged
boazdavid merged 1 commit into
rossoctl:mainfrom
boazdavid:fix/2137-report-client-compaction
Nov 26, 2026
Merged

boazdavid merged 1 commit into
rossoctl:mainfrom
boazdavid:fix/2137-report-client-compaction

Conversation

@boazdavid

Copy link
Copy Markdown
Contributor

Summary

Cortex now tells the accountant when a request is the first one after a client compaction (/compact or auto). The accountant then lists the savings that the compaction ended, and Cortex writes that list on the cost record. Before this change, Result.Compaction was always nil.

Fixes #2137.

What changed

  1. core/pipeline: new pctx.SetClientCompaction and ClientCompaction. They work like SetAccountingThread. An empty value does not erase a set one.
  2. core/plugins/contextguru: resolveThread sets unknown when the thread tracker sets AfterCompaction. This is the rule of context-guru's proxy (compactionVerdict).
  3. core/accounting: RequestEnd copies the verdict into RequestMeta.Compaction. Host.Savings copies Result.Compaction into Extras.Compaction.
  4. core/cost/event: new Event.Compaction, for example "compaction":{"trigger":"unknown","ended":["test:big"]}. It is omitted when there was no compaction. No figure or total reads it.
  5. core/plugins/inferenceparser: writes Extras.Compaction on the cost record. A compaction alone is a reason to publish a record, as a spend is.
  6. Docs: sections 5.4 and 5.5 of the accountant integration design describe the verdict and the record field.

The fix does not use agentall.IsClientCompaction, as the issue proposes.

  • That function is true on the compaction request itself: the request that asks the model for a summary.
  • R55 wants the request after it, which holds the summary. thread.Result.AfterCompaction marks that request.

How it was tested

  1. TestOnRequest_ReportsTheRequestAfterAClientCompaction runs 4 requests through the plugin: a first turn, the compaction request, the first request after, and a next turn. Only the first request after gets unknown. Without the change in resolveThread, the test fails.
  2. TestHost_ReportsClientCompaction: the verdict reaches RequestMeta.Compaction, and the accountant answers with Result.Compaction. A plain request gets neither.
  3. TestHost_SavingsCarriesTheCompactionRecord: request 1 removes a tool (test:big). Request 2 comes after a compaction without that tool. Extras.Compaction is {unknown, [test:big]}.
  4. TestSettleCost_PublishesTheCompactionRecord: a request with only a compaction record still gets a cost record.
  5. TestEvent_CompactionWire and TestEventJSONTagsArePinned_EveryField fix the JSON shape. ended is [], not null.
  6. gofmt -l core cmd prints nothing. go vet is clean in core, cmd/agentop and cmd/cortex.
  7. go test ./...:
    • cmd/cortex and cmd/agentop pass. The cmd/agentop root package takes about 985 s on this Mac, so it needs -timeout 30m.
    • core passes except core/cost/ledger and core/cost/usage. Both fail on main too, because the DST tests for America/Havana do not match this Mac's time-zone data.

Open questions

  • No view shows the record yet. Cortex has no thread view. agentop shows totals per session. A compaction count there needs new usage.Counts fields, and that can be a follow-up.

Technical notes

  • The trigger is always unknown. The wire does not tell a manual /compact from an auto compaction.
  • With no context-guru plugin, nothing sets the verdict, and the host reports nothing.
  • Accountant R55: the library prices nothing from the verdict. This change adds a record only. No dollar figure changes.

Assisted-By: Claude (Anthropic AI) noreply@anthropic.com

@boazdavid
boazdavid requested a review from a team as a code owner November 26, 2026 09:25
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 43 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 464f9f0e-3dc2-4db6-be9b-f2233a209181
Commits

Reviewing files that changed from the base of the PR and between 4189c7a and 7637f3d.

Files selected for processing: 14

  • core/accounting/host.go
  • core/accounting/host_test.go
  • core/accounting/savings.go
  • core/cost/event/event.go
  • core/cost/event/event_test.go
  • core/pipeline/accounting.go
  • core/pipeline/accounting_test.go
  • core/pipeline/context.go
  • core/plugins/contextguru/thread.go
  • core/plugins/contextguru/thread_test.go
  • core/plugins/inferenceparser/accounting.go
  • core/plugins/inferenceparser/accounting_test.go
  • core/plugins/inferenceparser/cost.go
  • docs/superpowers/specs/2026-11-17-accountant-integration-design.md
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions github-actions Bot added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 26, 2026
…l#2137)

The context-guru thread tracker marks the first request after a client
compaction (thread.Result.AfterCompaction), but Cortex never told the
accountant. RequestMeta.Compaction was always empty, so Result.Compaction
was always nil.

- pipeline: add pctx.SetClientCompaction and ClientCompaction.
- contextguru: resolveThread sets "unknown" when AfterCompaction is set.
  It does not use IsClientCompaction: that is true on the compaction
  request itself, and R55 wants the request after it.
- accounting: RequestEnd copies the verdict into RequestMeta.Compaction.
  Host.Savings copies Result.Compaction into Extras.Compaction.
- cost/event: add Event.Compaction (trigger, ended ChangeIDs). The
  inference parser writes it on the cost record. A compaction alone is
  a reason to publish a record. No figure or total reads it.
- docs: describe the verdict and the record field in the integration
  design (sections 5.4 and 5.5).

Fixes rossoctl#2137

Assisted-By: Claude (Anthropic AI) <noreply@anthropic.com>
Signed-off-by: DAVID BOAZ <DAVIDBO@il.ibm.com>
@boazdavid
boazdavid force-pushed the fix/2137-report-client-compaction branch from 9be934f to 7637f3d Compare November 26, 2026 09:29

@clawgenti clawgenti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

This wires the client-compaction verdict through to the accountant: context-guru's resolveThread sets unknown on the request thread.Result.AfterCompaction marks, the host passes it as RequestMeta.Compaction, and Result.Compaction comes back on the cost record as event.Event.Compaction. That is the same rule context-guru's proxy uses. Using AfterCompaction instead of IsClientCompaction, as the issue proposed, is correct, because R55 wants the request that holds the summary. I found no must-fix issues.

What I checked locally on head 7637f3d:

  • go build ./... passes in core, cmd/cortex and cmd/agentop. go vet is clean on the touched packages, and gofmt -l prints nothing.
  • core/accounting, core/cost/event, core/pipeline, core/plugins/contextguru and core/plugins/inferenceparser pass. core/cost/ledger and core/cost/usage fail only in the America/Havana DST tests, which depend on the machine's tzdata and have nothing to do with this diff. That matches the PR description.
  • The claimed fix works. With the res.AfterCompaction block removed from thread.go, TestOnRequest_ReportsTheRequestAfterAClientCompaction fails (first request after: ClientCompaction = "", want "unknown"). With it, the test passes.
  • Decoder safety: Compaction is a new optional pointer field (omitempty). Older records have no compaction key and still decode. No consumer decodes cost events with DisallowUnknownFields, and no other producer writes a compaction key in another shape. The pinned-wire and field-count tests are updated. Ended is copied into a non-nil slice, so it serializes as [].
  • SetClientCompaction ignores an empty value, so a later plugin cannot clear the verdict. With no context-guru plugin, nothing is set and nothing changes.

Not a blocker: no view shows the record yet. As the PR says, that can be a follow-up (the issue's proposal 2).

Author: boazdavid (CONTRIBUTOR association, repo admin permission: maintainer)
Areas reviewed: Go (accounting host, pipeline context, context-guru plugin, inference parser, cost event wire format), Docs
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: pending at review time (DCO, Trivy, Dependency Review, YAML Lint, Hadolint, CodeQL python and action pinning pass; Go CI still running). Verified locally as above.

@clawgenti clawgenti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Follow-up to review 6071652911 at this same head (7637f3d), which a concurrent run of this program posted while this run was drafting; the two reviews are independent and I found no distinct finding to add to it. This body is here for two reasons: to record that the second review at this head is a duplicate rather than a second opinion, and to carry the <!-- reviewed --> marker the program needs so the PR stops being re-queued every cycle (the earlier review has none).

For completeness, what I verified myself before seeing the other review: go test on core/accounting, core/pipeline and core/cost/event at head; and a positive control — deleting Compaction: pctx.ClientCompaction(), from Host.RequestEnd fails both TestHost_ReportsClientCompaction and TestHost_SavingsCarriesTheCompactionRecord (RequestMeta.Compaction = "", want "unknown"), so the tests do catch the wiring. The line was restored. I also confirmed compactionRecord copies Ended into a fresh slice, so a nil Ended serialises as [] and never null, and that the publish gate in settleCost only adds a reason to publish.

Author: boazdavid (CONTRIBUTOR — returning external)
Areas reviewed: Go (accounting host, pipeline context, cost/event wire format, inferenceparser), Docs
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: Go CI mostly green at this head, with a few jobs still in progress at review time (DCO pass, CodeRabbit skipped)
Not checked:

  • core/plugins/contextguru tests — github.com/rossoctl/context-guru v0.7.9 is not in this sandbox's module cache and the module proxy is unreachable, so TestOnRequest_ReportsTheRequestAfterAClientCompaction could not be run here; the accounting side of the wiring was run.
  • cmd/cortex build — same missing module.

Generated by repoman github-pr-review@af425c8.

@boazdavid
boazdavid merged commit 0af244f into rossoctl:main Nov 26, 2026
41 of 42 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 26, 2026
@boazdavid
boazdavid deleted the fix/2137-report-client-compaction branch November 26, 2026 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

feat(accounting): report client compaction to the accountant (RequestMeta.Compaction)

2 participants