Plan: the agent’s measured time on the issues it closes (#576)

Closes: #576. Relates to: #352 (the recap, whose records this reads), #646 (the recap’s end time), #300 (glab’s ambient login; the token this reads is the workspace’s own). Branch: feat/time-spent. Status: Done (2026-11-01) — U1-U3 on feat/time-spent; the land tool’s removal is #682

Erratum (2026-11-01): the ledger is constructed in packages/pi-modes/index.ts, not in registerRecap, so the real branchOf (gitBranchOf, in recap.ts) is passed there; TurnLedger’s `branchOf parameter defaults to naming no branch.

Erratum (2026-11-12): the ledger no longer reads the branch at all (#785). gitBranchOf and the branchOf parameter are gone; the session gives each finished turn its branch with a non-blocking read (nameBranch, in session-wiring.ts) before the turn is stored.

Erratum (2026-11-01): gadhs-time-posted is one entry per issue, { branch, issue, ms, at }, and ms is the total actually posted there (each share in whole minutes), not the measured time: with one entry per branch, a post refused on one issue and accepted on another would either post the accepted one twice or lose the refused one, and recording the measured time would drop each post’s rounding for good. Each issue is owed its equal share less its own recorded total.

Erratum (2026-11-01): the docs subsection is under modes.adoc’s == The turn recap, as === Time on the issue, not under the workflow guards: it reads the recap’s records and guards nothing.

Erratum (2026-11-01): the post runs detached from agent_end (void, catching its own failures), so a slow GitLab does not hold the turn’s end for up to 10 s an issue.

Design

What the operator decided (2026-11-01)

Simple timekeeping, nothing to type: the harness posts the agent’s measured working time to the issue’s GitLab time tracking, shown in the sidebar, automatically. No /estimate. The value is measured from session records, never computed or adjusted by the agent. The trigger is a push of the branch, since only this repository has a land tool and the operator has dropped it as over-engineering; every repository pushes.

What is measured, and what is not

The recap (#352, packages/pi-modes/recap.ts) already records every turn: ms from agent_start to agent_end, and endedAt. A turn is the agent working: its tool calls, its helpers (a helper’s wall time lies inside the parent’s turn, and a helper’s own session writes its own recap entries into its own session file, which this never reads), and waits it monitored. The time between turns, when the human reads, thinks or is away, is in no turn. That is the issue’s own definition of spent time: nothing new is measured and no number is estimated.

A turn is attributed to the branch the session’s cwd was on when the turn ended: RecapRecord gains branch?: string. TurnLedger.finish takes it from a branchOf(cwd) dependency that registerRecap (session-wiring.ts) supplies: git rev-parse --abbrev-ref HEAD through execFileSync in the session’s cwd, 2 s timeout, undefined on main, on HEAD (detached), on a non-zero exit or on any throw. The recap’s card does not show the branch.

Which issue

The issues the branch’s commits close: Closes #N on a line of its own in git log origin/main..HEAD --format=%B, the reading the MR description uses. A branch whose commits close nothing posts nothing: Relates to is not a closure, and a partial MR’s time is posted when the closing branch lands. A branch that closes several issues splits the time equally, rounded down to the minute each, and each summary names the others (shared with #M).

When: the end of the turn that pushed

The poster lives in pi-modes beside the recap (packages/pi-modes/time-spent.ts), because the record it needs is the ledger’s and the moment is the ledger’s finish. Putting it in pi-workflow would need the in-flight turn’s time across the package boundary, which the runtime bridge does not carry.

The ledger learns of a push from tool_result, as it learns of commits: a shell command whose pi-bash structure (analyze, already a pi-modes dependency) has a command with argv head git and subcommand push, possibly under a wrapper (timeout 1200 git push …), that ran without error (isError !== true), is a push. It records pushed: true on the turn. A command that relocates (cd, pushd, git -C, --git-dir, --work-tree) anywhere in it is not read as a push: the ledger has only the session’s cwd, and a push elsewhere is another repository’s. This loses nothing that matters, since the next plain push posts the accrued time.

At agent_end, when the finished record has pushed and a branch, the poster runs, after the recap entry is appended:

  1. the closures, from git log origin/main..HEAD --format=%B in the session’s cwd; none, stop;

  2. the measured time: the sum of ms over this session’s recap entries (ctx.sessionManager.getEntries()) whose branch is this one, the just-appended record included;

  3. what was already posted: the last gadhs-time-posted entry of this session for this branch, { branch, issues, ms, at }; the difference is what to post;

  4. a difference under one minute posts nothing and records nothing (it is posted with the next push);

  5. one POST /api/v4/projects/:id/issues/:iid/add_spent_time per issue, duration in GitLab’s form (1h12m, 45m), summary measured by pi: N turns on <branch>, session <first 8 of the session id>[, shared with #M];

  6. on success, pi.appendEntry("gadhs-time-posted", { branch, issues, ms: measured, at }), and one notification time: 1h 12m posted to #576.

Several sessions on one branch each post their own slices; GitLab adds them. A session that never pushes posts nothing; the issue asked for measured time, and a re-push after review posts the slice since the last post.

The token and the project

The project is the origin remote’s path, URL-encoded, from git remote get-url origin, read as projectPath in tools/lib/land.mjs reads it; that function and closesOf are copied into time-spent.ts with their cases, not moved: the land tool keeps its own until its removal (its own issue). The API host is the remote’s host.

The token is the workspace’s own, the way the judge rubric describes acting as the project’s bot (judge.ts, the forge-token line): GITLAB_TOKEN or GITLAB_ACCESS_TOKEN from pi’s environment, else the GITLAB_TOKEN= line of <repo root>/.env.local, read with readFileSync, the value held in a local and put in the PRIVATE-TOKEN header only. It is never logged, never in a notification, never in a diagnostics record, never in an error message: a failure reports GitLab’s status code and the issue number. The request is fetch with a 10 s AbortSignal.timeout.

What the agent sees

Nothing it can act on: a notification line and a diagnostics record (time-spent.posted or time-spent.skipped with a reason: no-token, no-closes, under-minute, http-<status>, unreachable). The agent neither computes nor confirms the number.

Risks

Risk Mode Mitigation

No token on the box

Silent: time is measured but never posted

One notification per session, the first time: "time measured, not posted: no GITLAB_TOKEN". The diagnostics record says it each turn. Nothing else changes.

GitLab refuses or is unreachable

Silent per turn

One notification with the status; no gadhs-time-posted entry, so the next push posts the whole difference again. No retry loop: the next push is the retry.

A push read where there was none, or missed

Over- or under-attribution of the moment, never of the amount

The amount is the ledger’s sum either way; a missed push delays the post to the next one. A false read posts real time early. The structure check is pi-bash’s, the same the commit guard trusts; the relocation refusal is the conservative side.

Two issues share a branch

Ambiguity

Equal split, named in each summary.

A turn on the wrong branch (the agent switched branches mid-session)

Misattribution of that turn

Attribution is by the branch at the turn’s end, which is where the work was committed. Documented as the rule.

The token leaks

Security

It is read into a local and used in a header; a test asserts no diagnostics record, notification or thrown message contains it.

Out of scope

/estimate; totals read from other sessions' files; attribution by MR; a backfill; posting from the land tool (dropped); the land tool’s removal (its own issue: tools/lib/land.mjs, tools/lib/test/land.test.mjs, the land verb in tools/task.mjs and tools/lib/ship-run.mjs, and the recap’s LAND_LINE, which would read `glab mr create’s URL instead).

Scope

Each unit is one commit on feat/time-spent, pi-modes' suite green and the new tests mutation-checked (the behaviour’s line removed, the test fails). U3 closes #576 by Closes #576 in its commit.

U1: the turn knows its branch and whether it pushed

packages/pi-modes/recap.ts: branch?: string and pushed?: true on RecapRecord, both accepted by recapOf when absent (as endedAt? is); TurnLedger takes branchOf: (cwd: string) ⇒ string | undefined beside now; read sets pushed from a isPush(command) helper over pi-bash’s analyze; finish sets branch. packages/pi-modes/session-wiring.ts: registerRecap constructs the ledger with the real branchOf.

Tests, packages/pi-modes/test/recap.test.ts: unit — a turn carries branchOf’s answer; `main, HEAD and undefined give no branch; a plain push, a wrapped push and a push in an && chain set pushed; a failed push, git push --dry-run, a push under cd or git -C, and git pull do not. Regression — an entry without the new fields still parses. Unit, for the real branchOf in a temp repository: a branch, main, detached HEAD, a non-repository and a missing directory; it never throws.

U2: the poster

packages/pi-modes/time-spent.ts: closesOf, projectPath (copied from land.mjs), gitlabDuration(ms), split(ms, n), measured(entries, branch), alreadyPosted(entries, branch), tokenFor(env, root), postSpentTime(deps, record) with git, fetch, appendEntry, notify, entries, sessionId, env, readFile as dependencies. session-wiring.ts: registerRecap’s `agent_end calls it after appendEntry when the record is pushed and branched; a throw is caught and logged, never pi’s.

Tests, packages/pi-modes/test/time-spent.test.ts: unit — each pure function (closesOf and projectPath with land.test.mjs’s cases; `gitlabDuration at 59 s, 60 s, 1 h, 1 h 12 m; split of 7 m over 2). Contract — the request postSpentTime makes: method, URL with the encoded project and the issue, the PRIVATE-TOKEN header, the JSON body’s duration and summary, against a recorded fake fetch. Integration, through a fake fetch and a fake git: a first push posts the sum of the branch’s entries; a second posts only the difference and names the second session count; a branch closing nothing posts nothing and records no-closes; under a minute posts nothing; two issues get the split and each summary names the other; GitLab 404 notifies once and appends no entry, so the next push posts the whole difference; no token notifies once per session. Security — with a token of a known string, no diagnostics record, notification or error message contains it.

U3: docs and release note

docs/modules/ROOT/pages/modes.adoc, under == Workflow guards (deterministic, at the tool-call seam): a subsection Time on the issue: what is measured, the branch rule, the push and the turn’s end, the equal split, what it never does (estimate, adjust, read other sessions). docs/modules/ROOT/pages/tuning.adoc: beside the glab 401 row, where the token for the time post is read from. CHANGELOG.adoc Unreleased, Added. Commit message Closes #576.

Edit this page · latest