Skip to content

factory: spend this month split by repository (factory.spend_by_repo) - #2132

Merged
santoshkumarradha merged 6 commits into
app/v3from
c32/engine-spend-by-repo
Nov 2, 2026
Merged

santoshkumarradha merged 6 commits into
app/v3from
c32/engine-spend-by-repo

Conversation

@santoshkumarradha

@santoshkumarradha santoshkumarradha commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

Summary

A Repos page can now show what each repository spent this month. The figure comes from the factory, not from this engine.

Why the factory is the source. spend.report reads this engine's local ledger. A ledger line has no repository: its filter offers model, kind, subject, session, root, task, automation, run, workspace and agent. It also records only calls made by this machine. In a team factory, jobs run on other people's runners, and this engine never sees those calls. The factory keeps each job's money on the job's document: factory.JobSpent gives the run's Stream.Spent, or ChatSpent before any run. It keeps every job document, not only the jobs that appear in the queue. So the honest source is a factory-side read over job documents. A repository dimension on spend.report cells would be both wrong and partial.

factory.spend_by_repo {factory, from, to}            (app wire)
  appBackend.FactorySpendByRepo                     one GET, as the connection's caller
    GET /v1/spend/repos?from=YYYY-MM-DD&to=YYYY-MM-DD (factory)
      factory.SpendRange(from, to, factory zone)    both required, to >= from, <= 366 days
      seam.Load -> forViewer                        the queue's visibility filter
      for each job with an owner/name repo and spend > 0:
        place JobSpent(job) at SpentAt(job)          run start, else created
        keep if from <= day <= to
      -> {from, to, time_zone, repos:[{repo, spent_usd, jobs}], total_usd}

Placement rule. Each job's whole spend lands on one day in the factory's own zone. That day is the day the job's run started. For a job that never ran, it is the day the job was created, which covers chat spend only. The job document keeps one running total, not spend by day. So a run that crosses midnight or the end of a month counts entirely on its start day.

Time zone. time_zone is the IANA name of the zone the days are read in, taken from the factory's clock. When that clock is on the process's Local zone, the factory finds the name the way Go does:

  • from TZ, where an empty TZ means UTC;
  • if TZ is not set, from the target of the /etc/localtime link, using the part after zoneinfo/.

When neither gives a name, the field is left off; it is never answered as "Local".

Wire. Hello feature factory.spend_by_repo. FactoryCapabilities.spend_by_repo is true.

// factory.spend_by_repo params
{"factory": "factory_local", "from": "2026-10-01", "to": "2026-10-31"}
// result
{"factory": "factory_local", "from": "2026-10-01", "to": "2026-10-31",
 "time_zone": "America/Chicago",
 "repos": [{"repo": "acme/api", "spent_usd": 1.375, "jobs": 2},
           {"repo": "acme/web", "spent_usd": 0.75, "jobs": 1}],
 "total_usd": 2.125}
  • repos is sorted by name. It is [] when nothing was spent. total_usd is the sum of repos.
  • jobs counts only jobs that spent something.
  • time_zone is left off when the factory cannot name its zone.
  • bad_params is returned, and nothing is sent, when:
    • from or to is missing or is not a YYYY-MM-DD day,
    • to is before from,
    • the range is longer than 366 days,
    • or the params contain an unknown field.
  • The route answers 422 for the same range errors.

Evidence

  • Before (red):
    • Route: TestSpendByRepoPlacesEachJobOnItsRunsStartDayForWhatTheCallerCanSee failed with GET ?from=2026-10-01&to=2026-10-31 = 404 {"error":"factory endpoint not found"...}.
    • App wire: TestAppFactorySpendByRepoReadsTheFactorysJobSpendByRepository failed with undefined: appwire.FeatureFactorySpendByRepo.
    • time_zone follow-up: both tests failed because the field was missing. The route test failed with owner's October = {... TimeZone: ...}, want {... TimeZone:America/Chicago ...}.
  • After (green): both tests pass. go test ./internal/appwire/ ./internal/factory/api/ ./internal/factory/serve/ passes, including the PROTOCOL.md contract test.
  • What the route test covers:
    • A factory clock in America/Chicago, with jobs in two months and two repositories.
    • A job that started at 23:30 on 30 September in the factory's zone, which is already October in UTC. It counts in September.
    • A chat-only job, counted on the day it was created.
    • A job that spent nothing, which is not counted.
    • A repository the signed-in viewer cannot read, which is hidden from that viewer but shown to the owner.
    • The 366-day limit and the 367-day refusal.
    • An empty month that answers repos: [].
    • Local with TZ=Europe/London answers Europe/London.
    • Local with no TZ resolves from an /etc/localtime target.
    • Local that nothing can resolve leaves the field off.
    • POST answers 405.
  • Real-model e2e: a real local factory ran a real job: factory.local_start, factory.local_run, factory.job_start, a chat stage, a check, and a stop at the approve gate. Every role was pinned to OpenRouter deepseek/deepseek-v4.1-flash, and assertFactoryPin checks every resolved role. A recorder saw 4 calls, all requesting that model. All response chunks named deepseek/deepseek-v4.1-flash, with no fallback. GitHub received 0 requests. The job's recorded spend was $0.005720. factory.spend_by_repo over the app wire returned {repos:[{repo:"acme/api", spent_usd:0.005720070999999999, jobs:1}], total_usd:0.005720070999999999}, which is equal to the job's own Stream.Spent and to the recorder's billed cost.
    • Caveat: the e2e ran before time_zone was added; the field is covered by the tests above.
    • Caveat: this used the opt-in TestLiveLocalRunReachesTheApproveGateOnOneModel with a local-only patch that is not in this PR. On the base branch that test stops before its run. It asserts that factory.local_start leaves the runner off, but the runner now turns on by itself once GitHub is known. I relaxed those two runner assertions and added the factory.spend_by_repo read at the end. That stale assertion is outside this change.

Merge Danger

Door: two-way. This adds one read route, one read method, one hello word and one capability field. It writes nothing and changes nothing that already exists.

Blast Radius: small. An older desk ignores the new word and the new fields. An older factory answers the new route with not found.

Limits, said plainly:

  • A job that runs again starts its spend from zero (freshStream), so only its latest run's spend counts.
  • Jobs whose repository is not owner/name are left out.
  • Standalone PR jobs are left out, because the seam loads issue jobs only.
  • A repository that is turned off still counts, because the money was spent.

🤖 Generated with Claude Code

GET /v1/spend/repos?from=&to= folds the factory's job documents into
{from, to, repos:[{repo, spent_usd, jobs}], total_usd} for the
repositories the caller can read. A job's whole spend is placed on the
day its run started, in the factory's zone, or on its creation day when
it never ran. factory.spend_by_repo carries it over the app wire; hello
announces factory.spend_by_repo and capabilities say spend_by_repo.
time_zone is the IANA name of the factory's clock zone. A process on its
Local zone names it from TZ (empty is UTC) or the /etc/localtime link,
and leaves the field off when neither names one.
…-repo

# Conflicts:
#	cmd/codeaf/appfactory.go
#	internal/appwire/factory.go
@santoshkumarradha
santoshkumarradha marked this pull request as ready for review November 2, 2026 07:21
@santoshkumarradha
santoshkumarradha merged commit cac0e71 into app/v3 Nov 2, 2026
1 check passed
@santoshkumarradha
santoshkumarradha deleted the c32/engine-spend-by-repo branch November 2, 2026 07:21
santoshkumarradha added a commit that referenced this pull request Nov 2, 2026
* factory: each job names the area of the repository its change touched

A job read and every job on the queue now carry area: the repository's
declared area most of the job's changed files fall under, or else the
top-level directory most of them sit under, written with a trailing slash
("cli/"). A job that changed no file, or only files at the root, carries
none. factory.JobArea is the one pure rule; the queue and the job's own read
apply the repository's declared areas, which they have at hand.

Hello announces factory.job_area.

* factory: each repository's job spend over the factory's days

GET /v1/spend/repos?from=&to= folds the factory's job documents into
{from, to, repos:[{repo, spent_usd, jobs}], total_usd} for the
repositories the caller can read. A job's whole spend is placed on the
day its run started, in the factory's zone, or on its creation day when
it never ran. factory.spend_by_repo carries it over the app wire; hello
announces factory.spend_by_repo and capabilities say spend_by_repo.

* factory e2e: the live local run reads the job's area and step model

The live lane writes its file under greet/ and checks that the queue and
the job's read both say area greet/ and that the write step's recorded model
is the pinned one. factory.local_start now turns the runner on itself, so the
lane no longer expects it off before the explicit run.

* changes: each factory job names its area (#2131)

* changes: each repository's spend by the factory (#2132)

* factory: the spend read names the zone its days are read in

time_zone is the IANA name of the factory's clock zone. A process on its
Local zone names it from TZ (empty is UTC) or the /etc/localtime link,
and leaves the field off when neither names one.

* factory: a job's area is two segments under a container, and one answer everywhere

Under a container directory (src, internal, pkg, lib, app, apps, packages,
crates, cmd; factory.AreaContainers) a job's area is the first two segments,
such as internal/factory/; a file directly inside one counts as the container.

Every job the server sends is judged once, in withVersion, by api.AreaIn
against the areas repositories declared as of the handler's last floor read,
so a verb's job and the queue never name different areas.

* factory api: the codec says area by the same rule the server says it again

FromItem and FromSnapshot keep saying area through api.AreaIn, so a queue
encoded on its own matches the one the server sends; the server's withVersion
says it again with the declarations of its last floor read.

* factory: one repository's facts counted from every job it keeps

factory.repo_facts {factory, repo, days?, months?} reads the factory's
GET /v1/repos/{owner}/{repo}/facts: jobs finished and pull requests merged
in the window and this month, spend by calendar month by the spend-by-repo
placement rule, jobs, rounds and questions by area by the job-area rule, and
jobs stuck on one step for a day or more, each with its own phrase. Bounded
to 20 areas and 20 stuck steps (97 facts); hello word factory.repo_facts and
capabilities repo_facts.

* factory: gofmt the repository facts test

* factory: repository facts say area by the one change-area rule

* factory: a repository's spend under a cent reads as less than a cent

The app test reads the facts at the fixture factory's own clock.

* changes: one repository's facts counted by the factory (#2143)

* factory e2e: the live local run expects local_start to turn the runner on

local_start turns the runner on by design, so the lane now fails when it is
left off, and it reads the recorder's call count under the recorder's lock.
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