Skip to content

feat(score): score finished sessions in the background - #1364

Merged
jeff-r2026 merged 13 commits into
Tencent:mainfrom
SaulMoro:saulmoro/1179-background-session-scoring
Oct 25, 2026
Merged

jeff-r2026 merged 13 commits into
Tencent:mainfrom
SaulMoro:saulmoro/1179-background-session-scoring

Conversation

@SaulMoro

Copy link
Copy Markdown
Collaborator

Summary

Closes #1179.

A member who sets scoring.sessions: true gets their finished sessions judged in the background. A session counts as finished once nothing has happened in it for six hours. Every scorer with sessions: true judges it, sampled by its samplingRate.

Session work goes through the same judge leases as workflow passes, so any driver takes it with score --next. After a session start's pull, a hidden local driver judges the rest and leaves the labels pending for the next pull to publish.

session start (background pass)
  pull()                                  unchanged
  settleLateUsageReport()                 unchanged
  sessionScoringScopes(cwd)               project scope, then user scope, where scoring.sessions is on
  spawn detached `teamai score-sessions`  not awaited

teamai score-sessions                     one per data home (non-blocking scores/score-sessions.lock)
  for each scope, for each local judge offer (verified tools, then every tool at best-effort)
    takeJudgeLease(only: 'session')       stops at idle, the daily limit, or 30 minutes
    judgeLease(publish: false)            label pending; the next pull publishes it
 takeOneLease(config, request, now)
   runs = scoreEntries(...)                                    workflow, unchanged
+  sessions = SessionWork.entries(...)                         captures, 6 h idle, oldest first, sampled
+  prepared = SessionWork.read(first leasable session)         read before the lock, never under it
   AttemptStore.locked
     sweep, close requests
+    prune attempts of sessions no longer captured (not leased today, not live), evidence first;
+      this also runs after the member or team turned session scoring off
     live leases: workflow hands back (unchanged)
+                 a session lease left out of `running` lapses
     workflow entries first (unchanged)
+    session entry: untried, no model label, under the daily limit, the one read before the lock
+      -> one write: leased attempt with its label context (agent, used skills, first prompt)
+      -> or failed `evidence_empty`
   evidence after the lock

A session key is (scorer, rubric sourceHash, session), and it is tried once:

open --take--> leased --submit--> labelled
                 |--release / local judge failed / isolation unmet--> tried
                 |--lapsed--> tried --late submit, no model label since--> labelled
tried and labelled are final; editing the rubric makes a new key

The settings, from first to last precedence:

  1. The member's config.yaml (scoring.sessions, scoring.daily_limit). A workspace's children count when the background driver resolves its scope.
  2. Active namespaces' scorers/<ns>/scoring.yaml; between them, sessions: false and the lowest daily_limit win.
  3. scorers/scoring.yaml.
  4. The defaults: sessions: false, daily_limit: 10.

A namespace scorer judges a session only when every capture of it had that namespace active under the capture's own team.selection. Workspace children count only when the capture's fingerprint matches.

Type of Change

  • New feature (non-breaking change that adds functionality)
  • Bug fix (non-breaking change that fixes an issue)
  • Refactor / internal cleanup

Commits

Each commit passes the type check, lint and its tests on its own.

Commit What it does
fix(score): keep workspace children when scoring a session score --session now sees the namespaces that a workspace's children activate.
fix(score): name the scorer file that asks for verified isolation The refusal names scorers/<ns>/<name>.md, as the lease path already does.
refactor(scorers): list the scoring.yaml keys once One table drives the parser and the problem text.
refactor(score): share the session read, evidence and label score --session and session leases share one builder. There is no output change.
refactor(scorers): name each attempt's source The attempt becomes a union over source. Old files read as workflow attempts.
feat(score): hand out finished sessions as judge work The core of the feature.
feat(score): cap session launches per day Adds scoring.daily_limit. Every session attempt leased today counts, including failed ones.
feat(score): team defaults for session scoring Adds sessions and daily_limit in scoring.yaml.
feat(score): scope session scorers by the capture's selection Applies namespace scorers per capture selection.
feat(score): add the hidden score-sessions driver The local driver.
feat(hooks): score finished sessions after a session start's pull Spawns the detached driver after the pull.
fix(score): give background judges the recorded tool homes Fills a missing CLAUDE_CONFIG_DIR, CODEX_HOME or CODEBUDDY_CONFIG_DIR from the recorded home.
fix(score): prune attempts of sessions that are gone Removes attempts of sessions that are no longer captured.

Test Plan

  • npx tsc --noEmit passes. This includes the new type test src/__tests__/scorers-attempts.type-test.ts.
  • npm run lint passes.
  • npx vitest run passes: 549 files, 15805 tests, 21 skipped.
  • Tests were added or updated for the change:
    • New suites: session-work.test.ts, score-sessions.test.ts and e2e/score-sessions.test.ts.
    • Additions to session-scoring, scorers-settings, scorers-attempts, scorers-judge and hook-handlers.
    • Day-boundary tests use a local-noon clock. They pass under TZ=Australia/Sydney and TZ=Pacific/Honolulu.
  • npm run build, then the e2e files e2e/score-sessions, e2e/score-session and e2e/score-external-driver: 3 files pass.
    • The new e2e runs the real hook-dispatch session-start --bg-only. The label waits as pending, with nothing on teamai-reports, and the next teamai pull publishes it.
    • It fails when the driver publishes per label, and when the hook does not start the driver.

Real CLI at head 22d9faf9e, with node dist/index.js, on a temporary HOME with a local bare team repo (provider git, agent Claude):

  • The team scorer has sessions: true and samplingRate: 100. The member set scoring.sessions: true.
  • Three Claude sessions are captured: idle for 8 h, idle for 7 h, and active 1 h ago.
  • The judge is a fake claude on PATH, which reports the verified version, so nothing was spent.
$ teamai score --next   # from <root>/app
{"ref":"s7a7591dd56c27/a3d0cf9e7","source":"session","scorer":"session-quality","subject":{"platform":"claude-code","sessionId":"7c1e0000-0000-4000-8000-0000000000a1"},"cwd":"<root>/home/.teamai","submit":"teamai score --lease s7a7591dd56c27/a3d0cf9e7 --submit --stdin"}
$ cd <root>/home/.teamai && teamai score --lease s7a7591dd56c27/a3d0cf9e7 --submit --stdin
{"label":{"scorer":"session-quality","subject":{"platform":"claude-code","sessionId":"7c1e0000-0000-4000-8000-0000000000a1"},"label":"shallow","passed":false},"published":true}
$ teamai score-sessions   # hidden driver
exit=0
labels.jsonl:
{"source":"session","sessionId":"7c1e0000-0000-4000-8000-0000000000a1","label":"shallow","judge":{"tool":"claude","isolation":"verified"}}
{"source":"session","sessionId":"7c1e0000-0000-4000-8000-0000000000b2","label":"thorough","judge":{"tool":"claude","model":{"reported":"fake-judge"},"isolation":"verified"}}
pending: 1
$ teamai score --next   # again: the session active 1 h ago is not work
{"data":{"idle":{"open":0,"leased":0}}}
$ teamai pull
exit=0
pending: 0
teamai-reports scores files: scores/tester/2026-10.jsonl
published labels: 2

Not run here:

  • Providers gitlab and github, and agents Codex, CodeBuddy and OpenCode. These are left to CI.
  • Windows: the WMI-started background pass and the recorded tool homes. This environment cannot run them.
  • A signed-in real judge, to avoid spending on an account.

Related Issues

Closes #1179. Part of #1137, builds on #1178.

Notes for Reviewers

Departures from the issue body. The first is new here; the issue comment records the rest:

  • Activity is the later of the session's latest dashboard event and its session file's last write, with the latest start or resume as a floor. It is not AgentSession.list updatedAt: the Claude adapter reads only the first 200 records, and compaction of events.jsonl drops the events of idle sessions. A session whose file is gone is not a candidate.
  • The daily count comes from the attempt files: distinct attempt ids of session attempts leased on today's local date. There is no scores/daily.json. The limit is checked under scores/attempts.lock, right before the write.
  • Any driver takes session work through score --next. A lease carries source and the cwd to submit from. For the user scope, that cwd is the data home.
  • Team defaults come from scoring.yaml, as listed above.

Decisions worth checking:

  • No hand-back for sessions. A session lease that a driver leaves out of running is neither handed back nor released. It lapses, so a restarted driver cannot launch a second, uncounted judge on it.
  • A late submit is refused when a model label exists for the key. For example, a member ran score --session meanwhile. Without this, the background label would replace theirs in the outbox, since both share one label key.
  • One local judge per session lease. Before its local judge starts, judgeLease marks the session lease with judgingSince, under the attempts lock. A second score --lease <ref> is refused with lease_judging, even after a crash. --submit can still settle the lease.
  • A failed local judge closes a session lease inside judgeLease, keeping the judge's own code. A judge that ran below the required isolation closes it too.
  • A session judge's answer survives a busy attempts lock. Its settle, and its close after a failure, are tried again for about five seconds instead of being dropped. The judge never runs again to recover an answer.
  • The local driver never pushes per label. judgeLease(..., { publish: false }) leaves its labels pending. --submit, --lease <ref> and on-demand scoring publish as before.
  • judgeLease takes an optional clock and stamps the settle time after the judge returns. That matches the old new Date() behaviour, and lets tests control it.
  • Workflow entries keep their reopen, batching, hand-back, evidence and label rules. The existing workflow-scoring tests are unchanged and pass. Workflow leases now also carry source: 'workflow'.
  • Isolation of the local judge. The local driver offers verified tools at verified first, then every tool at best-effort. It never offers a tool at a level that tool cannot reach, because that would spend the key and a slot.
  • Planned "one evidence cap" fix dropped. Both session paths already share the builder's single cap.
  • No background process when scoring is off. The hook spawns the driver only when a scope turns session scoring on.

Merge Danger

Door: Two-way for the code. Session attempt files, written only when a member turns sessions on, are a new on-disk shape. Reverting leaves them, and an older CLI skips them.

Blast Radius: Background session judging is opt-in.

  • Background session work is enabled only when scoring.sessions resolves to true.
  • Three changes apply regardless of that setting:
    • Workflow lease answers gain source.
    • score --session gets the workspace-children and isolation-message fixes.
    • Local judges get the recorded tool home when the env does not set one.
  • Version skew, documented in both guides. An older CLI rejects scorer frontmatter sessions because the frontmatter is strict. An older CLI that takes judge work in the same data home removes the rubrics of session leases. So update teamai everywhere before a team adds a session scorer or a member turns sessions on.
  • Editing a session scorer's rubric re-judges captured sessions. The daily limit spreads that over the following days.

score --session re-resolves the session's scope from its directory, but
dropped the workspace children before reading the scoring settings, so a
namespace only a child selects never applied its isolation.
When no judge tool here reaches verified isolation, score --session now
says which file asks for it, with the scorer's own path, as a lease
refusal does.
The parser and the unknown-key problem read one list, and the parser's
switch fails to compile on a key it does not handle.
One read of a session gives the evidence and the label context, so a
session judge lease can build both as score --session does. Output is
unchanged.
An attempt is now a workflow attempt or a session attempt. A session
attempt keeps the label context of the session read its lease took,
which only an unreadable session may lack. Workflow attempt files are
written as before, without a source. judgeLease takes the time it
settles at from its caller.
A member who sets scoring.sessions: true now gets each captured session
that has been idle for six hours as judge work for every scorer with
sessions: true. score --next hands out workflow work first, then
sessions, and the lease carries source and the cwd to submit from.

A session key is tried once. The session is read before the attempts
lock and the lease is written in one step under it. A failed local judge
closes the key, a lease left out of running lapses rather than being
handed back, and a late answer is refused once a judge has labelled the
session.
A member can set scoring.daily_limit to bound how many sessions one data
home leases per local day; 0 turns session scoring off. Every session
attempt counts, including ones that failed or could not be read, so a
broken session cannot spend the budget again. Once the day is spent,
idle no longer counts sessions as open.
A team can now turn session scoring on, and bound it, for every member
from scorers/scoring.yaml or a namespace's scoring.yaml. A member's
config.yaml still wins. Between active namespaces the cautious value
wins: sessions: false, and the lowest daily_limit.
A namespace scorer should only judge sessions that were delivered its
namespace. Each capture's recorded selection is overlaid on the scope
and resolved with the same namespace resolver pull uses. Workspace
children count only while their fingerprint still matches. A session
whose captures record no selection, or disagree, gets root and shared
scorers only.
Finished sessions need a driver that runs without anyone asking. This
one takes session work only, for the project scope and then the user
scope, judges each lease with a local tool, tries verified tools first
and then every tool at best-effort, and stops taking work after 30
minutes. A per-data-home lock keeps two runs from overlapping. Labels
stay pending; the next pull publishes them, so a background run never
pushes per label.
Without a trigger, finished sessions would only be judged when someone
runs a driver. The session-start pull now starts score-sessions as a
detached process once the pull and its late usage report have settled,
and only when the session's project or the user scope scores sessions,
so members who never turn it on pay nothing.
A judge started from the background hook pass may not inherit the
member's shell, so a moved Claude, Codex or CodeBuddy home was missing
and the tool judged with the wrong account or none. When the variable is
unset, the judge now gets the home teamai recorded for the tool. Its
settings were already read from there.
Session attempts are kept so a key is tried only once, which would let
them grow without end. Once no capture of a session is left it can never
be judge work again, so its attempts are removed under the attempts lock,
from the captures already listed for the lease, unless one is live or
was leased today and still counts towards the daily limit.
@SaulMoro SaulMoro assigned jeff-r2026 and SaulMoro and unassigned jeff-r2026 Oct 25, 2026
@github-actions

Copy link
Copy Markdown

No confident findings in the specified PR diff.

The PR description documents sufficient testing: a build, targeted e2e tests, and a representative real-CLI run at 22d9faf9e. Additional provider and agent coverage is explicitly deferred to CI.

Review was read-only; I did not run, build, or install anything from the PR.

@SaulMoro SaulMoro assigned jeff-r2026 and unassigned SaulMoro Oct 25, 2026
@github-actions

Copy link
Copy Markdown

No confident findings in the specified PR diff. The earlier review raised no findings, so there are none to mark resolved.

The PR description documents sufficient testing: a build, targeted e2e tests, and a representative real-CLI run at head 22d9faf9e. Additional provider, agent, and Windows coverage is explicitly deferred to CI or identified as unavailable locally.

Review was read-only. I did not run, build, or install anything from the PR.

@jeff-r2026 jeff-r2026 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.

Reviewed (agent review) and verified on main stacked with this batch: tsc / lint / build pass, related unit tests and E2E score-sessions pass. No blocking findings.

Non-blocking follow-ups worth considering:

  • evidence_empty failures are recorded with leasedAt and count toward daily_limit, so unreadable sessions can use up the day's budget.
  • A failed judge run is never retried for that session/rubric; worth stating in the docs.
  • When a platform records no sessionFile and emits no dashboard events, idle detection falls back to capturedAt, so a long-running session could be judged while still active.
  • No test races two drivers on the same session key.

@jeff-r2026
jeff-r2026 merged commit 6e4d8dc into Tencent:main Oct 25, 2026
20 of 22 checks passed
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
* fix(score): keep workspace children when scoring a session

score --session re-resolves the session's scope from its directory, but
dropped the workspace children before reading the scoring settings, so a
namespace only a child selects never applied its isolation.

* fix(score): name the scorer file that asks for verified isolation

When no judge tool here reaches verified isolation, score --session now
says which file asks for it, with the scorer's own path, as a lease
refusal does.

* refactor(scorers): list the scoring.yaml keys once

The parser and the unknown-key problem read one list, and the parser's
switch fails to compile on a key it does not handle.

* refactor(score): share the session read, evidence and label

One read of a session gives the evidence and the label context, so a
session judge lease can build both as score --session does. Output is
unchanged.

* refactor(scorers): name each attempt's source

An attempt is now a workflow attempt or a session attempt. A session
attempt keeps the label context of the session read its lease took,
which only an unreadable session may lack. Workflow attempt files are
written as before, without a source. judgeLease takes the time it
settles at from its caller.

* feat(score): hand out finished sessions as judge work

A member who sets scoring.sessions: true now gets each captured session
that has been idle for six hours as judge work for every scorer with
sessions: true. score --next hands out workflow work first, then
sessions, and the lease carries source and the cwd to submit from.

A session key is tried once. The session is read before the attempts
lock and the lease is written in one step under it. A failed local judge
closes the key, a lease left out of running lapses rather than being
handed back, and a late answer is refused once a judge has labelled the
session.

* feat(score): cap session launches per day

A member can set scoring.daily_limit to bound how many sessions one data
home leases per local day; 0 turns session scoring off. Every session
attempt counts, including ones that failed or could not be read, so a
broken session cannot spend the budget again. Once the day is spent,
idle no longer counts sessions as open.

* feat(score): team defaults for session scoring

A team can now turn session scoring on, and bound it, for every member
from scorers/scoring.yaml or a namespace's scoring.yaml. A member's
config.yaml still wins. Between active namespaces the cautious value
wins: sessions: false, and the lowest daily_limit.

* feat(score): scope session scorers by the capture's selection

A namespace scorer should only judge sessions that were delivered its
namespace. Each capture's recorded selection is overlaid on the scope
and resolved with the same namespace resolver pull uses. Workspace
children count only while their fingerprint still matches. A session
whose captures record no selection, or disagree, gets root and shared
scorers only.

* feat(score): add the hidden score-sessions driver

Finished sessions need a driver that runs without anyone asking. This
one takes session work only, for the project scope and then the user
scope, judges each lease with a local tool, tries verified tools first
and then every tool at best-effort, and stops taking work after 30
minutes. A per-data-home lock keeps two runs from overlapping. Labels
stay pending; the next pull publishes them, so a background run never
pushes per label.

* feat(hooks): score finished sessions after a session start's pull

Without a trigger, finished sessions would only be judged when someone
runs a driver. The session-start pull now starts score-sessions as a
detached process once the pull and its late usage report have settled,
and only when the session's project or the user scope scores sessions,
so members who never turn it on pay nothing.

* fix(score): give background judges the recorded tool homes

A judge started from the background hook pass may not inherit the
member's shell, so a moved Claude, Codex or CodeBuddy home was missing
and the tool judged with the wrong account or none. When the variable is
unset, the judge now gets the home teamai recorded for the tool. Its
settings were already read from there.

* fix(score): prune attempts of sessions that are gone

Session attempts are kept so a key is tried only once, which would let
them grow without end. Once no capture of a session is left it can never
be judge work again, so its attempts are removed under the attempts lock,
from the captures already listed for the lease, unless one is live or
was leased today and still counts towards the daily limit.
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.

[feat] Score finished sessions in the background

2 participants