chompyy 0.4.7 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -6
- package/dist/advice.js +2 -1
- package/dist/agentlimits.js +77 -0
- package/dist/agentusage.js +96 -0
- package/dist/appstate.js +72 -5
- package/dist/away.js +44 -5
- package/dist/blueprint.js +3 -7
- package/dist/board.js +4 -0
- package/dist/cli.js +328 -15
- package/dist/codexquota.js +106 -0
- package/dist/codexusage.js +10 -0
- package/dist/config.js +2 -0
- package/dist/context.js +30 -0
- package/dist/costs.js +1 -0
- package/dist/daytime.js +68 -0
- package/dist/demon.js +20 -0
- package/dist/didwork.js +46 -0
- package/dist/label.js +33 -0
- package/dist/learning.js +35 -0
- package/dist/menu.js +17 -0
- package/dist/nightcap.js +60 -0
- package/dist/nudge.js +47 -18
- package/dist/pacing.js +16 -1
- package/dist/questions.js +71 -0
- package/dist/recap.js +74 -0
- package/dist/receipt.js +48 -0
- package/dist/recipe.js +79 -0
- package/dist/send.js +46 -20
- package/dist/stamps.js +14 -0
- package/dist/taste.js +67 -7
- package/dist/triggers.js +5 -0
- package/dist/unkeep.js +220 -0
- package/dist/usage.js +21 -1
- package/dist/vendor/codeburn/antigravity.js +309 -0
- package/dist/vendor/codeburn/codex.js +429 -0
- package/dist/vendor/codeburn/copilot.js +292 -0
- package/dist/vendor/codeburn/gemini.js +195 -0
- package/dist/vendor/codeburn/grokbot.js +182 -0
- package/dist/vendor/codeburn/kimi.js +178 -0
- package/dist/vendor/codeburn/shims/chalk.js +14 -0
- package/dist/vendor/codeburn/shims/strip-ansi.js +6 -0
- package/dist/vendor/codeburn/src/act/backup.js +83 -0
- package/dist/vendor/codeburn/src/act/model-defaults.js +151 -0
- package/dist/vendor/codeburn/src/act/types.js +37 -0
- package/dist/vendor/codeburn/src/bash-utils.js +204 -0
- package/dist/vendor/codeburn/src/behavioral-weight.js +47 -0
- package/dist/vendor/codeburn/src/cache-dir.js +26 -0
- package/dist/vendor/codeburn/src/cache-refresh-lock.js +543 -0
- package/dist/vendor/codeburn/src/cache-sweep.js +67 -0
- package/dist/vendor/codeburn/src/classifier.js +209 -0
- package/dist/vendor/codeburn/src/claude-identity.js +223 -0
- package/dist/vendor/codeburn/src/codex-cache.js +377 -0
- package/dist/vendor/codeburn/src/codex-fork-replay.js +232 -0
- package/dist/vendor/codeburn/src/codex-jsonl-fields.js +99 -0
- package/dist/vendor/codeburn/src/codex-session-index.js +113 -0
- package/dist/vendor/codeburn/src/codex-throughput.js +544 -0
- package/dist/vendor/codeburn/src/compare-stats.js +405 -0
- package/dist/vendor/codeburn/src/config.js +145 -0
- package/dist/vendor/codeburn/src/content-utils.js +56 -0
- package/dist/vendor/codeburn/src/context-tree-codex.js +329 -0
- package/dist/vendor/codeburn/src/context-tree.js +664 -0
- package/dist/vendor/codeburn/src/copilot-aiu.js +16 -0
- package/dist/vendor/codeburn/src/currency.js +189 -0
- package/dist/vendor/codeburn/src/cursor-cache.js +238 -0
- package/dist/vendor/codeburn/src/cursor-import.js +402 -0
- package/dist/vendor/codeburn/src/cursor-sync.js +256 -0
- package/dist/vendor/codeburn/src/daily-cache.js +2036 -0
- package/dist/vendor/codeburn/src/data/litellm-snapshot.json +1 -0
- package/dist/vendor/codeburn/src/data/pricing-fallback.json +1 -0
- package/dist/vendor/codeburn/src/day-aggregator.js +327 -0
- package/dist/vendor/codeburn/src/editor-data-dir.js +11 -0
- package/dist/vendor/codeburn/src/fetch-utils.js +18 -0
- package/dist/vendor/codeburn/src/format.js +118 -0
- package/dist/vendor/codeburn/src/fs-utils.js +270 -0
- package/dist/vendor/codeburn/src/git-origin.js +334 -0
- package/dist/vendor/codeburn/src/granular-history.js +328 -0
- package/dist/vendor/codeburn/src/hermes-session-ledger.js +512 -0
- package/dist/vendor/codeburn/src/launcher-homes.js +142 -0
- package/dist/vendor/codeburn/src/live-sessions.js +358 -0
- package/dist/vendor/codeburn/src/math-utils.js +8 -0
- package/dist/vendor/codeburn/src/menubar-json.js +294 -0
- package/dist/vendor/codeburn/src/models.js +1879 -0
- package/dist/vendor/codeburn/src/optimize-one-hour-cache.js +92 -0
- package/dist/vendor/codeburn/src/optimize.js +4110 -0
- package/dist/vendor/codeburn/src/parse-workers.js +219 -0
- package/dist/vendor/codeburn/src/parser.js +6953 -0
- package/dist/vendor/codeburn/src/path-privacy.js +26 -0
- package/dist/vendor/codeburn/src/plans.js +135 -0
- package/dist/vendor/codeburn/src/pr-signals.js +132 -0
- package/dist/vendor/codeburn/src/project-filter-warnings.js +33 -0
- package/dist/vendor/codeburn/src/provider-issues.js +30 -0
- package/dist/vendor/codeburn/src/providers/amp.js +286 -0
- package/dist/vendor/codeburn/src/providers/antigravity.js +1731 -0
- package/dist/vendor/codeburn/src/providers/claude.js +551 -0
- package/dist/vendor/codeburn/src/providers/cline-cli.js +401 -0
- package/dist/vendor/codeburn/src/providers/cline.js +63 -0
- package/dist/vendor/codeburn/src/providers/codebuff.js +336 -0
- package/dist/vendor/codeburn/src/providers/codewhale.js +442 -0
- package/dist/vendor/codeburn/src/providers/codex.js +1720 -0
- package/dist/vendor/codeburn/src/providers/command-code.js +196 -0
- package/dist/vendor/codeburn/src/providers/copilot.js +3278 -0
- package/dist/vendor/codeburn/src/providers/crush.js +226 -0
- package/dist/vendor/codeburn/src/providers/cursor-agent.js +837 -0
- package/dist/vendor/codeburn/src/providers/cursor.js +1163 -0
- package/dist/vendor/codeburn/src/providers/devin.js +569 -0
- package/dist/vendor/codeburn/src/providers/droid.js +324 -0
- package/dist/vendor/codeburn/src/providers/dsh.js +690 -0
- package/dist/vendor/codeburn/src/providers/forge.js +245 -0
- package/dist/vendor/codeburn/src/providers/gemini.js +230 -0
- package/dist/vendor/codeburn/src/providers/goose.js +252 -0
- package/dist/vendor/codeburn/src/providers/grok.js +581 -0
- package/dist/vendor/codeburn/src/providers/grokbot.js +290 -0
- package/dist/vendor/codeburn/src/providers/hermes.js +771 -0
- package/dist/vendor/codeburn/src/providers/ibm-bob.js +53 -0
- package/dist/vendor/codeburn/src/providers/index.js +360 -0
- package/dist/vendor/codeburn/src/providers/kilo-code.js +50 -0
- package/dist/vendor/codeburn/src/providers/kimi.js +363 -0
- package/dist/vendor/codeburn/src/providers/kimicode.js +402 -0
- package/dist/vendor/codeburn/src/providers/kiro.js +1174 -0
- package/dist/vendor/codeburn/src/providers/lingtai-tui.js +372 -0
- package/dist/vendor/codeburn/src/providers/mistral-vibe-unified.js +236 -0
- package/dist/vendor/codeburn/src/providers/mistral-vibe.js +405 -0
- package/dist/vendor/codeburn/src/providers/mux.js +242 -0
- package/dist/vendor/codeburn/src/providers/open-design.js +217 -0
- package/dist/vendor/codeburn/src/providers/openclaude.js +309 -0
- package/dist/vendor/codeburn/src/providers/openclaw.js +442 -0
- package/dist/vendor/codeburn/src/providers/opencode-file-parser.js +131 -0
- package/dist/vendor/codeburn/src/providers/opencode.js +91 -0
- package/dist/vendor/codeburn/src/providers/pi.js +429 -0
- package/dist/vendor/codeburn/src/providers/quickdesk.js +491 -0
- package/dist/vendor/codeburn/src/providers/qwen.js +193 -0
- package/dist/vendor/codeburn/src/providers/session-message.js +118 -0
- package/dist/vendor/codeburn/src/providers/sqlite-session-parser.js +457 -0
- package/dist/vendor/codeburn/src/providers/types.js +5 -0
- package/dist/vendor/codeburn/src/providers/vercel-gateway.js +135 -0
- package/dist/vendor/codeburn/src/providers/vscode-cline-parser.js +212 -0
- package/dist/vendor/codeburn/src/providers/warp.js +575 -0
- package/dist/vendor/codeburn/src/providers/zcode.js +249 -0
- package/dist/vendor/codeburn/src/providers/zed.js +187 -0
- package/dist/vendor/codeburn/src/providers/zerostack.js +139 -0
- package/dist/vendor/codeburn/src/quota/antigravity.js +310 -0
- package/dist/vendor/codeburn/src/quota/claude.js +244 -0
- package/dist/vendor/codeburn/src/quota/cursor.js +301 -0
- package/dist/vendor/codeburn/src/quota/security.js +299 -0
- package/dist/vendor/codeburn/src/quota/types.js +1 -0
- package/dist/vendor/codeburn/src/scan-facts.js +99 -0
- package/dist/vendor/codeburn/src/session-cache.js +3040 -0
- package/dist/vendor/codeburn/src/session-count-label.js +14 -0
- package/dist/vendor/codeburn/src/session-output.js +139 -0
- package/dist/vendor/codeburn/src/session-population.js +30 -0
- package/dist/vendor/codeburn/src/sessions-report.js +1342 -0
- package/dist/vendor/codeburn/src/sqlite.js +528 -0
- package/dist/vendor/codeburn/src/sync/otlp.js +420 -0
- package/dist/vendor/codeburn/src/telemetry-snapshot.js +244 -0
- package/dist/vendor/codeburn/src/token-estimate.js +5 -0
- package/dist/vendor/codeburn/src/types.js +16 -0
- package/dist/vendor/codeburn/src/work-units.js +126 -0
- package/dist/vendor/codeburn/src/workflow-insights.js +228 -0
- package/dist/vendor/codeburn/src/wsl.js +198 -0
- package/dist/vendor/codeburn/src/yield.js +666 -0
- package/package.json +11 -3
|
@@ -0,0 +1,666 @@
|
|
|
1
|
+
// @ts-nocheck: codeburn type-checks this under its own tsconfig; Chompyy adds noUncheckedIndexedAccess (see UPSTREAM.md)
|
|
2
|
+
import { execFileSync } from 'child_process';
|
|
3
|
+
import { realpathSync } from 'fs';
|
|
4
|
+
import { resolve } from 'path';
|
|
5
|
+
import { cachedProjectIdentitiesForRange } from './daily-cache.js';
|
|
6
|
+
import { reportUnmatchedProjectPatterns } from './project-filter-warnings.js';
|
|
7
|
+
import { filterProjectsByName, parseAllSessions } from './parser.js';
|
|
8
|
+
import { isTrustedAbsoluteWorkingDirectory } from './path-privacy.js';
|
|
9
|
+
const SAFE_REF_PATTERN = /^[A-Za-z0-9._/\-]+$/;
|
|
10
|
+
function runGit(args, cwd) {
|
|
11
|
+
try {
|
|
12
|
+
return execFileSync('git', args, { cwd, encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] }).trim();
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function canonicalPath(path) {
|
|
19
|
+
try {
|
|
20
|
+
return realpathSync.native(path);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return path;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Resolve a directory to its canonical repository identity, or null when it is
|
|
28
|
+
* not inside a git work tree.
|
|
29
|
+
*
|
|
30
|
+
* The key is the absolute `git-common-dir` (the shared object store), NOT the
|
|
31
|
+
* raw path: `git rev-parse --is-inside-work-tree` is true in every subdirectory
|
|
32
|
+
* and `git log --all` returns the same repo-wide list from any of them, so
|
|
33
|
+
* keying groups on the raw path would let two monorepo subdirectories — or two
|
|
34
|
+
* worktrees of one repo — each award the same commit. All subdirectories AND
|
|
35
|
+
* all worktrees of one repo share this common-dir, so they collapse to a single
|
|
36
|
+
* group; distinct repositories keep distinct keys.
|
|
37
|
+
*
|
|
38
|
+
* Cached per directory: resolution runs once per unique path (many sessions
|
|
39
|
+
* share a path), never once per session.
|
|
40
|
+
*/
|
|
41
|
+
function resolveRepoIdentity(dir, cache) {
|
|
42
|
+
const cached = cache.get(dir);
|
|
43
|
+
if (cached !== undefined)
|
|
44
|
+
return cached;
|
|
45
|
+
let identity = null;
|
|
46
|
+
// One fork answers both questions; line 1 is --is-inside-work-tree, line 2 the
|
|
47
|
+
// --git-common-dir (relative to `dir` for a subdir, absolute for a worktree).
|
|
48
|
+
const out = runGit(['rev-parse', '--is-inside-work-tree', '--git-common-dir'], dir);
|
|
49
|
+
if (out) {
|
|
50
|
+
const [insideWorkTree, commonDir] = out.split('\n');
|
|
51
|
+
if (insideWorkTree === 'true' && commonDir) {
|
|
52
|
+
// realpath canonicalizes symlinks: git reports a linked worktree's
|
|
53
|
+
// common-dir as a realpath but the main worktree's as ".git" relative to
|
|
54
|
+
// its (possibly symlinked) path, so both must be canonicalized to collapse
|
|
55
|
+
// to one key.
|
|
56
|
+
// Accepted residual: moving the main repo after `git worktree add` leaves
|
|
57
|
+
// Git's linked-worktree metadata stale, so its worktrees may not unify;
|
|
58
|
+
// that is Git-state staleness, not a grouping bug.
|
|
59
|
+
identity = { key: canonicalPath(resolve(dir, commonDir)), gitDir: dir };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
cache.set(dir, identity);
|
|
63
|
+
return identity;
|
|
64
|
+
}
|
|
65
|
+
function getMainBranch(cwd) {
|
|
66
|
+
const result = runGit(['symbolic-ref', 'refs/remotes/origin/HEAD'], cwd);
|
|
67
|
+
if (result) {
|
|
68
|
+
const branch = result.replace('refs/remotes/origin/', '');
|
|
69
|
+
if (SAFE_REF_PATTERN.test(branch))
|
|
70
|
+
return branch;
|
|
71
|
+
}
|
|
72
|
+
const branches = runGit(['branch', '-a'], cwd) ?? '';
|
|
73
|
+
if (branches.includes('main'))
|
|
74
|
+
return 'main';
|
|
75
|
+
if (branches.includes('master'))
|
|
76
|
+
return 'master';
|
|
77
|
+
return 'main';
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Normalize a git remote URL to a host-scoped repo identity (`host/org/repo`)
|
|
81
|
+
* usable as a server-side join key. Handles the three common transports:
|
|
82
|
+
*
|
|
83
|
+
* git@github.com:org/repo.git -> github.com/org/repo
|
|
84
|
+
* ssh://git@github.com:22/org/repo.git -> github.com/org/repo
|
|
85
|
+
* https://user:tok@github.com/org/repo.git -> github.com/org/repo
|
|
86
|
+
*
|
|
87
|
+
* Credentials and ports are stripped (a token embedded in an https remote must
|
|
88
|
+
* never leave the machine), the host is lowercased (path case is preserved),
|
|
89
|
+
* and a trailing `.git` / `/` is removed. Local paths and `file://` remotes
|
|
90
|
+
* return null — a repo with no network remote has no server-side identity.
|
|
91
|
+
*/
|
|
92
|
+
/** Max length of an emitted repo identity (`host/org/repo`). */
|
|
93
|
+
const MAX_REPO_IDENTITY_LENGTH = 200;
|
|
94
|
+
/**
|
|
95
|
+
* Positive validation (allow-list) of a composed repo identity — the final
|
|
96
|
+
* gate EVERY branch passes through before anything is returned. The host must
|
|
97
|
+
* look like a hostname and every path segment like a repo path segment, so no
|
|
98
|
+
* upstream parsing quirk (transport-helper remotes like `ext::…` or
|
|
99
|
+
* `codecommit::…`, credentials that survived a malformed URL, oversized
|
|
100
|
+
* strings) can reach the wire. Rejecting is always safe: an unrecognizable
|
|
101
|
+
* remote simply has no server-side identity.
|
|
102
|
+
*/
|
|
103
|
+
function isValidRepoIdentity(host, segments) {
|
|
104
|
+
if (!/^[a-z0-9]([a-z0-9.-]*[a-z0-9])?$/.test(host))
|
|
105
|
+
return false;
|
|
106
|
+
if (segments.length === 0)
|
|
107
|
+
return false;
|
|
108
|
+
return segments.every(s => /^[A-Za-z0-9._~-]+$/.test(s));
|
|
109
|
+
}
|
|
110
|
+
export function normalizeRemoteUrl(url) {
|
|
111
|
+
const trimmed = url.trim();
|
|
112
|
+
if (!trimmed)
|
|
113
|
+
return null;
|
|
114
|
+
// Windows drive-letter paths (`C:\Users\...`, `C:/Users/...`) are local
|
|
115
|
+
// filesystem paths, not scp-like remotes — without this check the scp-like
|
|
116
|
+
// branch would parse `C:` as a host and emit the user's local path as a
|
|
117
|
+
// repo identity.
|
|
118
|
+
if (/^[a-zA-Z]:[\\/]/.test(trimmed))
|
|
119
|
+
return null;
|
|
120
|
+
let host;
|
|
121
|
+
let path;
|
|
122
|
+
if (/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(trimmed)) {
|
|
123
|
+
let parsed;
|
|
124
|
+
try {
|
|
125
|
+
parsed = new URL(trimmed);
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
if (parsed.protocol === 'file:')
|
|
131
|
+
return null;
|
|
132
|
+
if (!parsed.hostname)
|
|
133
|
+
return null;
|
|
134
|
+
host = parsed.hostname;
|
|
135
|
+
path = parsed.pathname;
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
// scp-like syntax: [user@]host:path. Credentials (userinfo) are split off
|
|
139
|
+
// at the FIRST `@` BEFORE any host matching — expressing userinfo as an
|
|
140
|
+
// optional regex group lets backtracking abandon the group and re-parse a
|
|
141
|
+
// credential prefix as `host:path`, dumping the token into the path
|
|
142
|
+
// (e.g. `x-access-token:ghp_…@github.com/org/repo`). Host must be at
|
|
143
|
+
// least 2 chars: a single-character "host" is a Windows drive-relative
|
|
144
|
+
// path (`C:repo`), never a real remote host.
|
|
145
|
+
const at = trimmed.indexOf('@');
|
|
146
|
+
const rest = at >= 0 ? trimmed.slice(at + 1) : trimmed;
|
|
147
|
+
const scpLike = /^([^:/\\]{2,}):(?!\/\/)(.+)$/.exec(rest);
|
|
148
|
+
if (!scpLike)
|
|
149
|
+
return null;
|
|
150
|
+
host = scpLike[1];
|
|
151
|
+
path = scpLike[2];
|
|
152
|
+
}
|
|
153
|
+
const cleanPath = path
|
|
154
|
+
.replace(/\/+/g, '/') // collapse doubled slashes → one join key, not two
|
|
155
|
+
.replace(/^\/+/, '')
|
|
156
|
+
.replace(/\/+$/, '')
|
|
157
|
+
.replace(/\.git$/i, ''); // case-insensitive: Repo.GIT joins with repo.git
|
|
158
|
+
if (!cleanPath)
|
|
159
|
+
return null;
|
|
160
|
+
const identity = `${host.toLowerCase()}/${cleanPath}`;
|
|
161
|
+
if (identity.length > MAX_REPO_IDENTITY_LENGTH)
|
|
162
|
+
return null;
|
|
163
|
+
if (!isValidRepoIdentity(host.toLowerCase(), cleanPath.split('/')))
|
|
164
|
+
return null;
|
|
165
|
+
return identity;
|
|
166
|
+
}
|
|
167
|
+
/** `git remote get-url origin`, normalized. Null when absent or local-only. */
|
|
168
|
+
function getRepoRemote(gitDir) {
|
|
169
|
+
const url = runGit(['remote', 'get-url', 'origin'], gitDir);
|
|
170
|
+
return url ? normalizeRemoteUrl(url) : null;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Find SHAs that were the target of a `git revert` ANYWHERE in the repo's
|
|
174
|
+
* history (not just the time window). The standard `git revert` body
|
|
175
|
+
* format is "This reverts commit <SHA>." which we grep out.
|
|
176
|
+
*
|
|
177
|
+
* The previous implementation flagged a commit as `isRevert` based on the
|
|
178
|
+
* substring "revert" appearing in its OWN subject. Two bugs there:
|
|
179
|
+
* 1. Subjects like "Add revert button" matched.
|
|
180
|
+
* 2. The session that PERFORMED the revert was tagged "reverted", not the
|
|
181
|
+
* session whose work was being reverted — so the original session always
|
|
182
|
+
* looked productive even after its work was thrown away.
|
|
183
|
+
*/
|
|
184
|
+
function getRevertedShas(cwd) {
|
|
185
|
+
const bodies = runGit(['log', '--all', '--grep=^This reverts commit', '--format=%B%x1e'], cwd) ?? '';
|
|
186
|
+
const set = new Set();
|
|
187
|
+
const re = /This reverts commit ([0-9a-f]{7,40})/g;
|
|
188
|
+
let m;
|
|
189
|
+
while ((m = re.exec(bodies)) !== null) {
|
|
190
|
+
set.add(m[1].toLowerCase());
|
|
191
|
+
}
|
|
192
|
+
return set;
|
|
193
|
+
}
|
|
194
|
+
function getCommitsInRange(cwd, since, until, mainBranch) {
|
|
195
|
+
const sinceStr = since.toISOString();
|
|
196
|
+
const untilStr = until.toISOString();
|
|
197
|
+
const log = runGit(['log', '--all', `--since=${sinceStr}`, `--until=${untilStr}`, '--format=%H|%aI|%s'], cwd);
|
|
198
|
+
if (!log)
|
|
199
|
+
return [];
|
|
200
|
+
const mainCommits = new Set((runGit(['log', mainBranch, '--format=%H'], cwd) ?? '').split('\n').filter(Boolean));
|
|
201
|
+
const revertedShas = getRevertedShas(cwd);
|
|
202
|
+
return log.split('\n').filter(Boolean).map(line => {
|
|
203
|
+
const [sha] = line.split('|');
|
|
204
|
+
const timestamp = line.split('|')[1] ?? '';
|
|
205
|
+
return {
|
|
206
|
+
sha,
|
|
207
|
+
timestamp: new Date(timestamp),
|
|
208
|
+
inMain: mainCommits.has(sha),
|
|
209
|
+
// wasReverted: matches when ANY later commit's body says
|
|
210
|
+
// "This reverts commit <sha>". Compare against the full SHA AND its
|
|
211
|
+
// 7-char short prefix to be safe; git revert sometimes records the
|
|
212
|
+
// short form.
|
|
213
|
+
wasReverted: revertedShas.has(sha.toLowerCase()) ||
|
|
214
|
+
revertedShas.has(sha.toLowerCase().slice(0, 7)),
|
|
215
|
+
};
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
function sessionWindow(session) {
|
|
219
|
+
if (!session.firstTimestamp)
|
|
220
|
+
return null;
|
|
221
|
+
const start = new Date(session.firstTimestamp);
|
|
222
|
+
const lastTs = session.lastTimestamp ?? session.firstTimestamp;
|
|
223
|
+
const end = new Date(new Date(lastTs).getTime() + 60 * 60 * 1000);
|
|
224
|
+
return { start, end, sessionId: session.sessionId };
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Branch names a session was observed on, from its turns' `gitBranch` metadata
|
|
228
|
+
* (recorded by providers that capture it, e.g. Claude Code). Empty for
|
|
229
|
+
* providers without branch metadata — the merged-branch rescue is additive and
|
|
230
|
+
* simply never fires for those.
|
|
231
|
+
*/
|
|
232
|
+
function sessionBranches(session) {
|
|
233
|
+
const names = new Set();
|
|
234
|
+
for (const turn of session.turns) {
|
|
235
|
+
const branch = turn.gitBranch;
|
|
236
|
+
if (typeof branch === 'string' && branch)
|
|
237
|
+
names.add(branch);
|
|
238
|
+
}
|
|
239
|
+
return names;
|
|
240
|
+
}
|
|
241
|
+
function refMatchesSessionBranch(ref, branch) {
|
|
242
|
+
if (ref === branch)
|
|
243
|
+
return true;
|
|
244
|
+
// Only the remote-tracking shape (`origin/<branch>`, `upstream/<branch>`):
|
|
245
|
+
// a bare suffix match would let an unrelated `topic/<branch>` rescue a
|
|
246
|
+
// session that never touched it.
|
|
247
|
+
if (!ref.endsWith(`/${branch}`))
|
|
248
|
+
return false;
|
|
249
|
+
const prefix = ref.slice(0, ref.length - branch.length - 1);
|
|
250
|
+
return prefix.length > 0 && !prefix.includes('/');
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Evidence per session-observed branch name for the squash-merge rescue:
|
|
254
|
+
*
|
|
255
|
+
* A GitHub squash merge lands the branch's work on main under a brand-new SHA,
|
|
256
|
+
* so neither the original SHAs nor the branch ref appear in main's history.
|
|
257
|
+
* The signature that survives is the TREE: the squashed commit's tree is
|
|
258
|
+
* byte-identical to the branch tip's. The rule therefore requires the branch
|
|
259
|
+
* to carry commits of its own (tip tree differs from its merge-base tree, and
|
|
260
|
+
* `base..ref` is non-empty) and its tip tree to match a commit on main.
|
|
261
|
+
*
|
|
262
|
+
* Ancestry is deliberately NOT a rule: a branch that never advanced past an
|
|
263
|
+
* old main commit is an ancestor of main by construction, and rescuing through
|
|
264
|
+
* it calls dead work shipped — on one real corpus, 567 of 600 first-draft
|
|
265
|
+
* rescues came from exactly one such parked ref.
|
|
266
|
+
*/
|
|
267
|
+
function buildMergedBranchIndex(gitDir, mainBranch, wanted) {
|
|
268
|
+
if (wanted.size === 0)
|
|
269
|
+
return null;
|
|
270
|
+
const mainLog = runGit(['log', mainBranch, '--format=%T'], gitDir) ?? '';
|
|
271
|
+
const mainTrees = new Set(mainLog.split('\n').filter(Boolean));
|
|
272
|
+
// Collect EVERY matching ref per branch name: a stale local copy that does
|
|
273
|
+
// not qualify must not hide a remote-tracking ref that does (and vice
|
|
274
|
+
// versa), so all candidates become evidence rather than first-wins.
|
|
275
|
+
const allRefs = (runGit(['for-each-ref', 'refs/heads', 'refs/remotes', '--format=%(refname:short)'], gitDir) ?? '')
|
|
276
|
+
.split('\n').map(l => l.trim()).filter(Boolean);
|
|
277
|
+
const branches = new Map();
|
|
278
|
+
for (const branch of wanted) {
|
|
279
|
+
const evidence = [];
|
|
280
|
+
for (const ref of allRefs) {
|
|
281
|
+
if (!refMatchesSessionBranch(ref, branch))
|
|
282
|
+
continue;
|
|
283
|
+
const tree = runGit(['rev-parse', `${ref}^{tree}`], gitDir);
|
|
284
|
+
if (!tree)
|
|
285
|
+
continue;
|
|
286
|
+
const base = runGit(['merge-base', ref, mainBranch], gitDir);
|
|
287
|
+
const baseTree = base ? runGit(['rev-parse', `${base}^{tree}`], gitDir) : null;
|
|
288
|
+
const ownLog = base ? runGit(['log', '--format=%aI', `${base}..${ref}`], gitDir) ?? '' : '';
|
|
289
|
+
const commitDates = ownLog.split('\n').filter(Boolean).map(d => new Date(d));
|
|
290
|
+
evidence.push({ ref, tree, baseTree, commitDates });
|
|
291
|
+
}
|
|
292
|
+
if (evidence.length > 0)
|
|
293
|
+
branches.set(branch, evidence);
|
|
294
|
+
}
|
|
295
|
+
return { mainTrees, branches, mainBranch };
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Whether this session's own work shipped through a squash merge: the branch it
|
|
299
|
+
* was observed on carries commits, its tip tree is on main, and at least one of
|
|
300
|
+
* its own commits was made inside the session's window — the session
|
|
301
|
+
* contributed, rather than merely having run on a branch that later shipped.
|
|
302
|
+
*/
|
|
303
|
+
function sessionBranchShipped(session, branches, index) {
|
|
304
|
+
const window = sessionWindow(session);
|
|
305
|
+
if (!window)
|
|
306
|
+
return false;
|
|
307
|
+
for (const branch of branches) {
|
|
308
|
+
if (branch === index.mainBranch)
|
|
309
|
+
continue;
|
|
310
|
+
for (const evidence of index.branches.get(branch) ?? []) {
|
|
311
|
+
if (evidence.commitDates.length === 0)
|
|
312
|
+
continue;
|
|
313
|
+
if (evidence.tree === evidence.baseTree)
|
|
314
|
+
continue;
|
|
315
|
+
if (!index.mainTrees.has(evidence.tree))
|
|
316
|
+
continue;
|
|
317
|
+
if (evidence.commitDates.some(date => date >= window.start && date <= window.end))
|
|
318
|
+
return true;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
323
|
+
function attributeCommits(sessions, commits) {
|
|
324
|
+
const attributions = sessions.map(session => ({
|
|
325
|
+
window: sessionWindow(session),
|
|
326
|
+
commits: [],
|
|
327
|
+
lostCandidacy: false,
|
|
328
|
+
}));
|
|
329
|
+
for (const commit of commits) {
|
|
330
|
+
const candidates = attributions.filter((attribution) => attribution.window !== null &&
|
|
331
|
+
commit.timestamp >= attribution.window.start &&
|
|
332
|
+
commit.timestamp <= attribution.window.end);
|
|
333
|
+
const owner = candidates.reduce((current, candidate) => {
|
|
334
|
+
if (current === null)
|
|
335
|
+
return candidate;
|
|
336
|
+
const currentSpan = current.window.end.getTime() - current.window.start.getTime();
|
|
337
|
+
const candidateSpan = candidate.window.end.getTime() - candidate.window.start.getTime();
|
|
338
|
+
if (candidateSpan !== currentSpan)
|
|
339
|
+
return candidateSpan < currentSpan ? candidate : current;
|
|
340
|
+
if (candidate.window.start.getTime() !== current.window.start.getTime()) {
|
|
341
|
+
return candidate.window.start < current.window.start ? candidate : current;
|
|
342
|
+
}
|
|
343
|
+
// sessionId, not array position: session order is cache-state dependent.
|
|
344
|
+
return candidate.window.sessionId < current.window.sessionId ? candidate : current;
|
|
345
|
+
}, null);
|
|
346
|
+
for (const candidate of candidates) {
|
|
347
|
+
if (candidate !== owner)
|
|
348
|
+
candidate.lostCandidacy = true;
|
|
349
|
+
}
|
|
350
|
+
owner?.commits.push(commit);
|
|
351
|
+
}
|
|
352
|
+
return attributions;
|
|
353
|
+
}
|
|
354
|
+
function categorizeSession(session, commits, lostCandidacy) {
|
|
355
|
+
if (!session.firstTimestamp) {
|
|
356
|
+
return { category: 'abandoned', commitCount: 0 };
|
|
357
|
+
}
|
|
358
|
+
if (commits.length === 0) {
|
|
359
|
+
// Ambiguous only when this session's window actually contained a commit
|
|
360
|
+
// that a tighter window won — mere overlap with a credited session is not
|
|
361
|
+
// enough to withhold the abandoned classification.
|
|
362
|
+
return {
|
|
363
|
+
category: lostCandidacy ? 'ambiguous' : 'abandoned',
|
|
364
|
+
commitCount: 0,
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
const inMainCount = commits.filter(c => c.inMain).length;
|
|
368
|
+
// A session is "reverted" when at least half of its in-main commits were
|
|
369
|
+
// later reverted out (revert detected via "This reverts commit <sha>"
|
|
370
|
+
// anywhere later in history, not in the same time window).
|
|
371
|
+
const revertedCount = commits.filter(c => c.inMain && c.wasReverted).length;
|
|
372
|
+
if (revertedCount > 0 && revertedCount >= inMainCount / 2) {
|
|
373
|
+
return { category: 'reverted', commitCount: commits.length };
|
|
374
|
+
}
|
|
375
|
+
if (inMainCount > 0) {
|
|
376
|
+
return { category: 'productive', commitCount: inMainCount };
|
|
377
|
+
}
|
|
378
|
+
return { category: 'abandoned', commitCount: commits.length };
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Group sessions by canonical repository identity and load each group's
|
|
382
|
+
* commits for the range. Shared by `computeYield` (categorization) and
|
|
383
|
+
* `computeAttributionRecords` (sync). Grouping semantics are unchanged from
|
|
384
|
+
* the original computeYield implementation: each commit is awarded at most
|
|
385
|
+
* once across the whole repo; monorepo subdirectories and worktrees collapse
|
|
386
|
+
* to one group; a project whose path is missing or not a git repo falls back
|
|
387
|
+
* to the cwd repo (or an empty commit list when cwd is not a repo either).
|
|
388
|
+
*/
|
|
389
|
+
function buildRepoGroups(projects, range, cwd, identityMode = 'project-path') {
|
|
390
|
+
const repoIdentityCache = new Map();
|
|
391
|
+
const cwdIdentity = resolveRepoIdentity(cwd, repoIdentityCache);
|
|
392
|
+
const cwdCommits = cwdIdentity
|
|
393
|
+
? getCommitsInRange(cwd, range.start, range.end, getMainBranch(cwd))
|
|
394
|
+
: [];
|
|
395
|
+
const repoGroups = new Map();
|
|
396
|
+
for (const project of projects) {
|
|
397
|
+
for (const session of project.sessions) {
|
|
398
|
+
const sourcePath = identityMode === 'trusted-session-cwd'
|
|
399
|
+
? session.workingDirectory
|
|
400
|
+
: project.projectPath;
|
|
401
|
+
const ownIdentity = isTrustedAbsoluteWorkingDirectory(sourcePath)
|
|
402
|
+
? resolveRepoIdentity(sourcePath, repoIdentityCache)
|
|
403
|
+
: null;
|
|
404
|
+
const identity = ownIdentity ?? cwdIdentity;
|
|
405
|
+
const groupKey = identity ? identity.key : cwd;
|
|
406
|
+
let group = repoGroups.get(groupKey);
|
|
407
|
+
if (!group) {
|
|
408
|
+
group = {
|
|
409
|
+
commits: !identity
|
|
410
|
+
? []
|
|
411
|
+
: cwdIdentity && identity.key === cwdIdentity.key
|
|
412
|
+
? cwdCommits
|
|
413
|
+
: getCommitsInRange(identity.gitDir, range.start, range.end, getMainBranch(identity.gitDir)),
|
|
414
|
+
sessions: [],
|
|
415
|
+
projectNames: [],
|
|
416
|
+
ownIdentity: [],
|
|
417
|
+
gitDir: identity?.gitDir ?? null,
|
|
418
|
+
};
|
|
419
|
+
repoGroups.set(groupKey, group);
|
|
420
|
+
}
|
|
421
|
+
group.sessions.push(session);
|
|
422
|
+
group.projectNames.push(project.project);
|
|
423
|
+
group.ownIdentity.push(ownIdentity !== null);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return repoGroups;
|
|
427
|
+
}
|
|
428
|
+
export async function computeYield(range, cwd, provider = 'all', projectFilter, excludeFilter) {
|
|
429
|
+
const parsed = await parseAllSessions(range, provider);
|
|
430
|
+
await reportUnmatchedProjectPatterns(parsed, projectFilter, excludeFilter, () => cachedProjectIdentitiesForRange(range));
|
|
431
|
+
const projects = filterProjectsByName(parsed, projectFilter, excludeFilter);
|
|
432
|
+
const summary = {
|
|
433
|
+
productive: { cost: 0, sessions: 0 },
|
|
434
|
+
reverted: { cost: 0, sessions: 0 },
|
|
435
|
+
abandoned: { cost: 0, sessions: 0 },
|
|
436
|
+
ambiguous: { cost: 0, sessions: 0 },
|
|
437
|
+
total: { cost: 0, sessions: 0 },
|
|
438
|
+
details: [],
|
|
439
|
+
};
|
|
440
|
+
const repoGroups = buildRepoGroups(projects, range, cwd);
|
|
441
|
+
for (const group of repoGroups.values()) {
|
|
442
|
+
const attributions = attributeCommits(group.sessions, group.commits);
|
|
443
|
+
// All branch names observed across this group's sessions, so one index
|
|
444
|
+
// covers every session; built lazily below only if a rescue is attempted.
|
|
445
|
+
const groupBranches = new Set();
|
|
446
|
+
for (const session of group.sessions) {
|
|
447
|
+
for (const branch of sessionBranches(session))
|
|
448
|
+
groupBranches.add(branch);
|
|
449
|
+
}
|
|
450
|
+
let mergedBranchIndex;
|
|
451
|
+
const rescueIfShipped = (session, branches) => {
|
|
452
|
+
if (groupBranches.size === 0 || !group.gitDir)
|
|
453
|
+
return false;
|
|
454
|
+
if (mergedBranchIndex === undefined) {
|
|
455
|
+
mergedBranchIndex = buildMergedBranchIndex(group.gitDir, getMainBranch(group.gitDir), groupBranches);
|
|
456
|
+
}
|
|
457
|
+
return mergedBranchIndex !== null && sessionBranchShipped(session, branches, mergedBranchIndex);
|
|
458
|
+
};
|
|
459
|
+
for (const [index, session] of group.sessions.entries()) {
|
|
460
|
+
const attribution = attributions[index];
|
|
461
|
+
let { category, commitCount } = categorizeSession(session, attribution?.commits ?? [], attribution?.lostCandidacy ?? false);
|
|
462
|
+
// #1442: the timestamp-window heuristics call work abandoned that later
|
|
463
|
+
// shipped — a squash merge lands the work under a SHA the window can
|
|
464
|
+
// never see. When the session was observed on a branch whose tip tree is
|
|
465
|
+
// on main AND whose own commits overlap this session's window, the
|
|
466
|
+
// session's work demonstrably shipped and it is productive. Reverted
|
|
467
|
+
// stays reverted: that verdict already describes shipped-then-undone.
|
|
468
|
+
if (category === 'abandoned' || category === 'ambiguous') {
|
|
469
|
+
if (rescueIfShipped(session, sessionBranches(session))) {
|
|
470
|
+
category = 'productive';
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
summary[category].cost += session.totalCostUSD;
|
|
474
|
+
summary[category].sessions += 1;
|
|
475
|
+
summary.total.cost += session.totalCostUSD;
|
|
476
|
+
summary.total.sessions += 1;
|
|
477
|
+
summary.details.push({
|
|
478
|
+
sessionId: session.sessionId,
|
|
479
|
+
project: group.projectNames[index] ?? session.project,
|
|
480
|
+
cost: session.totalCostUSD,
|
|
481
|
+
category,
|
|
482
|
+
commitCount,
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
return summary;
|
|
487
|
+
}
|
|
488
|
+
export function formatYieldSummary(summary) {
|
|
489
|
+
const { productive, reverted, abandoned, ambiguous, total } = summary;
|
|
490
|
+
const pct = (n) => total.cost > 0 ? Math.round((n / total.cost) * 100) : 0;
|
|
491
|
+
const fmt = (n) => `$${n.toFixed(2)}`;
|
|
492
|
+
const lines = [
|
|
493
|
+
'',
|
|
494
|
+
`Productive: ${fmt(productive.cost).padStart(8)} (${pct(productive.cost)}%) - ${productive.sessions} sessions shipped to main`,
|
|
495
|
+
`Reverted: ${fmt(reverted.cost).padStart(8)} (${pct(reverted.cost)}%) - ${reverted.sessions} sessions were reverted`,
|
|
496
|
+
`Abandoned: ${fmt(abandoned.cost).padStart(8)} (${pct(abandoned.cost)}%) - ${abandoned.sessions} sessions never committed`,
|
|
497
|
+
`Ambiguous: ${fmt(ambiguous.cost).padStart(8)} (${pct(ambiguous.cost)}%) - ${ambiguous.sessions} sessions lost commits to concurrent sessions`,
|
|
498
|
+
'',
|
|
499
|
+
'Attribution: timestamp-window + merged-branch evidence (heuristic)',
|
|
500
|
+
'',
|
|
501
|
+
`Total: ${fmt(total.cost).padStart(8)} - ${total.sessions} sessions`,
|
|
502
|
+
'',
|
|
503
|
+
];
|
|
504
|
+
return lines.join('\n');
|
|
505
|
+
}
|
|
506
|
+
export function buildYieldJsonReport(summary, periodLabel, range) {
|
|
507
|
+
const bucket = (value) => ({
|
|
508
|
+
costUSD: value.cost,
|
|
509
|
+
sessions: value.sessions,
|
|
510
|
+
costPercent: summary.total.cost > 0
|
|
511
|
+
? Math.round((value.cost / summary.total.cost) * 1000) / 10
|
|
512
|
+
: 0,
|
|
513
|
+
sessionPercent: summary.total.sessions > 0
|
|
514
|
+
? Math.round((value.sessions / summary.total.sessions) * 1000) / 10
|
|
515
|
+
: 0,
|
|
516
|
+
});
|
|
517
|
+
return {
|
|
518
|
+
period: {
|
|
519
|
+
label: periodLabel,
|
|
520
|
+
start: range.start.toISOString(),
|
|
521
|
+
end: range.end.toISOString(),
|
|
522
|
+
},
|
|
523
|
+
summary: {
|
|
524
|
+
productive: bucket(summary.productive),
|
|
525
|
+
reverted: bucket(summary.reverted),
|
|
526
|
+
abandoned: bucket(summary.abandoned),
|
|
527
|
+
ambiguous: bucket(summary.ambiguous),
|
|
528
|
+
total: {
|
|
529
|
+
costUSD: summary.total.cost,
|
|
530
|
+
sessions: summary.total.sessions,
|
|
531
|
+
},
|
|
532
|
+
productiveToRevertedCostRatio: summary.reverted.cost > 0
|
|
533
|
+
? Math.round((summary.productive.cost / summary.reverted.cost) * 100) / 100
|
|
534
|
+
: null,
|
|
535
|
+
},
|
|
536
|
+
methodology: 'timestamp-window',
|
|
537
|
+
details: summary.details.map(detail => ({
|
|
538
|
+
sessionId: detail.sessionId,
|
|
539
|
+
project: detail.project,
|
|
540
|
+
costUSD: detail.cost,
|
|
541
|
+
category: detail.category,
|
|
542
|
+
commitCount: detail.commitCount,
|
|
543
|
+
})),
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
/** Max PR links retained per session attribution record. */
|
|
547
|
+
export const MAX_PR_LINKS_PER_SESSION = 20;
|
|
548
|
+
/**
|
|
549
|
+
* Shape-check PR links before they leave the machine. Upstream parsers only
|
|
550
|
+
* verify truthiness, so arbitrary strings can land in `session.prLinks`.
|
|
551
|
+
* Keep only https URLs shaped like a PR (`/org/repo/pull/N` — GitHub and
|
|
552
|
+
* GitHub Enterprise), bounded in length, capped per session, sorted.
|
|
553
|
+
*
|
|
554
|
+
* Links are REBUILT from `origin + pathname`, never passed through verbatim:
|
|
555
|
+
* userinfo (`https://alice:token@…`), query strings (copy-pasted GitHub
|
|
556
|
+
* links routinely carry `?notification_referrer_id=…`), and fragments are
|
|
557
|
+
* all dropped. Rebuilt links that collapse to the same URL dedupe.
|
|
558
|
+
*/
|
|
559
|
+
export function sanitizePrLinks(links) {
|
|
560
|
+
const valid = new Set();
|
|
561
|
+
for (const link of links) {
|
|
562
|
+
if (typeof link !== 'string' || link.length === 0 || link.length > 512)
|
|
563
|
+
continue;
|
|
564
|
+
let url;
|
|
565
|
+
try {
|
|
566
|
+
url = new URL(link);
|
|
567
|
+
}
|
|
568
|
+
catch {
|
|
569
|
+
continue;
|
|
570
|
+
}
|
|
571
|
+
if (url.protocol !== 'https:')
|
|
572
|
+
continue;
|
|
573
|
+
if (!/^\/[^/]+\/[^/]+\/pull\/\d+$/.test(url.pathname))
|
|
574
|
+
continue;
|
|
575
|
+
const rebuilt = `${url.origin}${url.pathname}`;
|
|
576
|
+
if (rebuilt.length > 256)
|
|
577
|
+
continue;
|
|
578
|
+
valid.add(rebuilt);
|
|
579
|
+
}
|
|
580
|
+
return [...valid].sort().slice(0, MAX_PR_LINKS_PER_SESSION);
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Compute per-session attribution records for sync. Reuses the exact yield
|
|
584
|
+
* repo grouping + tightest-window commit attribution (`methodology:
|
|
585
|
+
* timestamp-window`), then joins in each repo group's normalized origin
|
|
586
|
+
* remote and the session's sanitized PR links.
|
|
587
|
+
*
|
|
588
|
+
* Inclusion rules:
|
|
589
|
+
* - Only sessions whose OWN project path resolved to a repo participate in
|
|
590
|
+
* commit attribution; cwd-fallback sessions never carry a repo or commits
|
|
591
|
+
* (privacy gate — see below) but still emit a record when they have PR links.
|
|
592
|
+
* - Commits require a normalized remote; a SHA without a repo identity has
|
|
593
|
+
* no server-side meaning.
|
|
594
|
+
* - A session with no commits and no PR links is emitted ONLY when it lost a
|
|
595
|
+
* commit to a tighter-window session in THIS computation (`lostCandidacy`)
|
|
596
|
+
* — a retraction candidate. A session that merely aged its commits out of
|
|
597
|
+
* the range lost them to nobody, and emitting an empty record for it would
|
|
598
|
+
* permanently retract a still-correct server-side count.
|
|
599
|
+
*
|
|
600
|
+
* Takes already-parsed projects (sync push has them in hand) instead of
|
|
601
|
+
* re-parsing like computeYield does.
|
|
602
|
+
*/
|
|
603
|
+
export function computeAttributionRecords(projects, range, cwd) {
|
|
604
|
+
const repoGroups = buildRepoGroups(projects, range, cwd, 'trusted-session-cwd');
|
|
605
|
+
const records = [];
|
|
606
|
+
for (const group of repoGroups.values()) {
|
|
607
|
+
const remote = group.gitDir ? getRepoRemote(group.gitDir) : null;
|
|
608
|
+
// Privacy gate: only sessions whose identity came from their own explicit
|
|
609
|
+
// provider-recorded cwd participate in commit attribution. A session whose cwd no
|
|
610
|
+
// longer resolves (deleted/renamed dir, non-repo session) inherits the
|
|
611
|
+
// cwd-fallback identity in buildRepoGroups — attributing it here would
|
|
612
|
+
// egress whatever repo the user happens to be pushing from, with commits
|
|
613
|
+
// that session never touched. Fallback sessions get no repo and no
|
|
614
|
+
// commits; they still emit a record when they carry PR links (which are
|
|
615
|
+
// session-native and safe). Excluding them from the competition also
|
|
616
|
+
// prevents a fallback window from stealing a commit that belongs to a
|
|
617
|
+
// genuine session.
|
|
618
|
+
const ownSessions = group.sessions.filter((_, i) => group.ownIdentity[i]);
|
|
619
|
+
const attributions = attributeCommits(ownSessions, group.commits);
|
|
620
|
+
// Keyed by object reference: session objects are unique per group entry,
|
|
621
|
+
// whereas sessionId strings could collide across projects.
|
|
622
|
+
const attributionBySession = new Map();
|
|
623
|
+
const lostCandidacyBySession = new Map();
|
|
624
|
+
for (const [i, session] of ownSessions.entries()) {
|
|
625
|
+
attributionBySession.set(session, attributions[i]?.commits ?? []);
|
|
626
|
+
lostCandidacyBySession.set(session, attributions[i]?.lostCandidacy ?? false);
|
|
627
|
+
}
|
|
628
|
+
for (const [index, session] of group.sessions.entries()) {
|
|
629
|
+
if (!session.firstTimestamp)
|
|
630
|
+
continue;
|
|
631
|
+
const isOwn = group.ownIdentity[index] === true;
|
|
632
|
+
const sessionRemote = isOwn ? remote : null;
|
|
633
|
+
const attributedCommits = sessionRemote
|
|
634
|
+
? (attributionBySession.get(session) ?? [])
|
|
635
|
+
: [];
|
|
636
|
+
const prLinks = sanitizePrLinks(session.prLinks ?? []);
|
|
637
|
+
// Empty sessions are retraction candidates ONLY when they lost a commit
|
|
638
|
+
// to a tighter-window session in THIS run: that commit's server-side
|
|
639
|
+
// attribution is migrating, so the loser must re-emit commit_count=0.
|
|
640
|
+
// An empty session whose commits merely aged out of the --since range
|
|
641
|
+
// (rolling window, or a narrower window than a previous push) lost them
|
|
642
|
+
// to NOBODY — emitting a retraction for it would permanently zero a
|
|
643
|
+
// still-correct server-side count, because the original state key stays
|
|
644
|
+
// ledgered and is never re-sent.
|
|
645
|
+
const lostToTighterSession = sessionRemote !== null &&
|
|
646
|
+
(lostCandidacyBySession.get(session) ?? false);
|
|
647
|
+
if (attributedCommits.length === 0 && prLinks.length === 0 && !lostToTighterSession)
|
|
648
|
+
continue;
|
|
649
|
+
records.push({
|
|
650
|
+
sessionId: session.sessionId,
|
|
651
|
+
project: group.projectNames[index] ?? session.project,
|
|
652
|
+
repo: sessionRemote,
|
|
653
|
+
prLinks,
|
|
654
|
+
commits: attributedCommits.map(c => ({
|
|
655
|
+
sha: c.sha,
|
|
656
|
+
timestamp: c.timestamp.toISOString(),
|
|
657
|
+
inMain: c.inMain,
|
|
658
|
+
wasReverted: c.wasReverted,
|
|
659
|
+
})),
|
|
660
|
+
firstTimestamp: session.firstTimestamp,
|
|
661
|
+
lastTimestamp: session.lastTimestamp ?? session.firstTimestamp,
|
|
662
|
+
});
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
return records;
|
|
666
|
+
}
|