@newrelic/preflight 1.14.36 → 1.14.37

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.
@@ -48,6 +48,10 @@ const GIT_DIFF_RE = /\bgit\s+diff\b/;
48
48
  const GIT_LOG_RE = /\bgit\s+log\b/;
49
49
  const GIT_COMMIT_RE = /\bgit\s+commit\b/;
50
50
  const GIT_WORKTREE_RE = /\bgit\s+worktree\b/;
51
+ // Only `add`/`remove` create or tear down real isolation — `list`/`prune`/
52
+ // `lock`/etc. are read-only inspection and shouldn't inflate the "worktree
53
+ // ops" count with commands that don't reflect any parallel-isolation work.
54
+ const GIT_WORKTREE_ADD_REMOVE_RE = /\bgit\s+worktree\s+(?:add|remove)\b/;
51
55
  const GIT_CHECKOUT_OURS_RE = /\bgit\s+checkout\s+--ours\b/;
52
56
  const GIT_CHECKOUT_THEIRS_RE = /\bgit\s+checkout\s+--theirs\b/;
53
57
  const GIT_CHERRY_PICK_RE = /\bgit\s+cherry-pick\b/;
@@ -61,6 +65,26 @@ const GH_PR_CHECKS_RE = /\bgh\s+pr\s+checks\b/;
61
65
  const GH_COMMAND_RE = /\bgh\s+/;
62
66
  // Extract PR number from gh commands
63
67
  const GH_PR_NUMBER_RE = /\bgh\s+pr\s+\w+\s+(\d+)/;
68
+ // hydrateGitLog()'s dedup window, used only against a hook-observed commit
69
+ // event (one with no hash in its command text): `git log`'s %ct has 1-second
70
+ // resolution and a hook-observed commit event's timestamp is recorded when
71
+ // the tool call completes, so the two timestamps for the same commit are
72
+ // close but never exactly equal — and the hook event's command text is the
73
+ // raw pre-execution shell string, which never contains the resulting hash,
74
+ // so dedup against it can't key on a hash match. Treat any existing
75
+ // hook-observed commit event within this window as the same commit.
76
+ //
77
+ // A hydrated commit ALWAYS carries a real hash from `git log`, so comparing
78
+ // it against another hydrated event uses exact hash equality (via
79
+ // HYDRATED_COMMIT_HASH_RE below) instead of this proximity window — two
80
+ // genuinely distinct commits landing within the window (e.g. rapid
81
+ // sequential commits in the same `git log` batch) must not collapse into
82
+ // one just because their timestamps are close.
83
+ const COMMIT_DEDUP_WINDOW_MS = 5_000;
84
+ // Matches the synthetic command text hydrateGitLog() gives its own events
85
+ // (see below), letting isDuplicate() tell a hydrated event apart from a
86
+ // hook-observed one and recover its hash.
87
+ const HYDRATED_COMMIT_HASH_RE = /^git commit \((.+)\)$/;
64
88
  const REJECT_INDICATORS = [
65
89
  /\[rejected\]/i,
66
90
  /non-fast-forward/i,
@@ -69,15 +93,27 @@ const REJECT_INDICATORS = [
69
93
  ];
70
94
  // Conflict file path extraction: "CONFLICT (content): Merge conflict in <path>"
71
95
  const CONFLICT_FILE_RE = /Merge conflict in (.+)/g;
96
+ // Render order for suggestions/best-practices — most severe first — rather
97
+ // than fixed source-code push order, which could put a critical item below
98
+ // a milder one.
99
+ const SUGGESTION_SEVERITY_RANK = {
100
+ critical: 0,
101
+ warning: 1,
102
+ info: 2,
103
+ };
104
+ const BEST_PRACTICE_STATUS_RANK = {
105
+ fail: 0,
106
+ warn: 1,
107
+ pass: 2,
108
+ unknown: 3,
109
+ };
72
110
  // ---------------------------------------------------------------------------
73
111
  // Tracker
74
112
  // ---------------------------------------------------------------------------
75
113
  export class GitEfficiencyTracker {
76
114
  events = [];
77
115
  conflictRecords = [];
78
- pendingConflictTimestamp = null;
79
- pendingConflictCommand = '';
80
- pendingConflictFiles = [];
116
+ pendingConflicts = [];
81
117
  lastSyncTimestamp = null;
82
118
  pullsSinceLastConflict = 0;
83
119
  consecutiveFailedPushes = 0;
@@ -93,6 +129,24 @@ export class GitEfficiencyTracker {
93
129
  editedFiles = new Set();
94
130
  hasUsedWorktree = false;
95
131
  hasUsedForceWithLease = false;
132
+ // Tracks whether ANY bare (non-lease) force-push occurred this session,
133
+ // independent of whether --force-with-lease was ever also used. This is
134
+ // the single shared signal both the force_with_lease best-practice check
135
+ // and the force_push suggestion's severity gate on, so the two checks
136
+ // agree on what counts as "was this session's force-push usage safe."
137
+ hasUsedBareForcePush = false;
138
+ // Count of bare (non-lease) force-pushes specifically — kept separate from
139
+ // stats.forcePushes (which sums bare AND lease-protected pushes) so
140
+ // severity scales with how much *unsafe* force-pushing happened, not with
141
+ // total force-push volume. A safe --force-with-lease push must never be
142
+ // able to escalate severity that a bare push alone already set.
143
+ bareForcePushCount = 0;
144
+ // Snapshot of whether any bare force-push landed while repoContext.branch
145
+ // matched repoContext.defaultBranch — a bare push to the shared default
146
+ // branch can clobber other collaborators' work, unlike an identical push
147
+ // to a personal feature branch only one person is using, so the two must
148
+ // not scale to the same severity.
149
+ hasForcePushedToDefaultBranch = false;
96
150
  totalToolCalls = 0;
97
151
  sessionStartTimestamp = null;
98
152
  commitTimestamps = [];
@@ -113,7 +167,6 @@ export class GitEfficiencyTracker {
113
167
  commitsBehindMain = null;
114
168
  quickConflictResolutions = 0;
115
169
  prEvents = [];
116
- firstCommitTimestamp = null;
117
170
  repoContext = {
118
171
  repoName: null,
119
172
  branch: null,
@@ -142,10 +195,18 @@ export class GitEfficiencyTracker {
142
195
  const command = record.command;
143
196
  if (!command)
144
197
  return;
145
- // Track GitHub CLI PR commands (skip if `git commit` is the *command*, not
146
- // text that happens to appear inside a gh argument like --title).
147
- if (GH_COMMAND_RE.test(command) && !command.trimStart().startsWith('git ')) {
148
- this.processGhCommand(command, record.timestamp);
198
+ // Track GitHub CLI PR commands. Split on shell separators first so a
199
+ // `gh` invocation chained after a `git` command (e.g. `git push && gh pr
200
+ // create --fill`) is still detected — checking the git-prefix guard
201
+ // against the whole compound string would skip it even though only the
202
+ // first segment is a `git` command. Each segment still skips the case
203
+ // where "gh" is just text inside a git argument, e.g. `git commit -m "gh
204
+ // pr create note"`.
205
+ for (const segment of command.split(/&&|;|\|/)) {
206
+ const trimmedSegment = segment.trim();
207
+ if (GH_COMMAND_RE.test(trimmedSegment) && !trimmedSegment.startsWith('git ')) {
208
+ this.processGhCommand(trimmedSegment, record.timestamp);
209
+ }
149
210
  }
150
211
  if (!GIT_COMMAND_RE.test(command))
151
212
  return;
@@ -196,14 +257,29 @@ export class GitEfficiencyTracker {
196
257
  const merged = this.prEvents.filter((e) => e.action === 'merge').length;
197
258
  const checksViewed = this.prEvents.filter((e) => e.action === 'checks').length;
198
259
  const prsUpdated = this.prEvents.filter((e) => e.action === 'edit' || e.action === 'ready').length;
199
- // Time from first commit to first PR creation
200
- let avgTimeToCreateMs = null;
201
- if (created > 0 && this.firstCommitTimestamp !== null) {
202
- const firstCreate = this.prEvents.find((e) => e.action === 'create');
203
- if (firstCreate) {
204
- avgTimeToCreateMs = Math.max(0, firstCreate.timestamp - this.firstCommitTimestamp);
260
+ // Time from each PR's most recent preceding commit to its `gh pr
261
+ // create`, averaged across every PR opened this session — not a single
262
+ // delta anchored to whichever commit happened to be the very first one
263
+ // this tracker ever saw, which would go stale after the first PR and
264
+ // ignore every PR opened later in the same session.
265
+ const sortedCommitTimestamps = [...this.commitTimestamps].sort((a, b) => a - b);
266
+ const timesToCreate = [];
267
+ for (const prEvent of this.prEvents) {
268
+ if (prEvent.action !== 'create')
269
+ continue;
270
+ let precedingCommitTimestamp = null;
271
+ for (const commitTimestamp of sortedCommitTimestamps) {
272
+ if (commitTimestamp > prEvent.timestamp)
273
+ break;
274
+ precedingCommitTimestamp = commitTimestamp;
275
+ }
276
+ if (precedingCommitTimestamp !== null) {
277
+ timesToCreate.push(Math.max(0, prEvent.timestamp - precedingCommitTimestamp));
205
278
  }
206
279
  }
280
+ const avgTimeToCreateMs = timesToCreate.length > 0
281
+ ? timesToCreate.reduce((a, b) => a + b, 0) / timesToCreate.length
282
+ : null;
207
283
  return {
208
284
  created,
209
285
  merged,
@@ -224,8 +300,24 @@ export class GitEfficiencyTracker {
224
300
  success: true,
225
301
  durationMs: null,
226
302
  };
227
- // Only add if we don't already have this commit tracked
228
- const isDuplicate = this.events.some((e) => e.type === 'commit' && e.command?.includes(commit.hash));
303
+ // Only add if we don't already have this commit tracked. Against an
304
+ // existing hydrated event (one carrying its own real hash), match by
305
+ // exact hash equality — precise, and avoids collapsing two genuinely
306
+ // distinct commits that just happen to land within the proximity
307
+ // window. Against an existing hook-observed event, fall back to
308
+ // timestamp proximity: a prior session's hook-observed `commit` event,
309
+ // replayed via replayTimeline() before this method ever runs, has no
310
+ // hash in its command text at all, so a hash match would never catch
311
+ // it and every restart would double-count that commit.
312
+ const isDuplicate = this.events.some((e) => {
313
+ if (e.type !== 'commit')
314
+ return false;
315
+ const existingHash = e.command ? HYDRATED_COMMIT_HASH_RE.exec(e.command)?.[1] : undefined;
316
+ if (existingHash !== undefined) {
317
+ return existingHash === commit.hash;
318
+ }
319
+ return Math.abs(e.timestamp - commit.timestamp) < COMMIT_DEDUP_WINDOW_MS;
320
+ });
229
321
  if (!isDuplicate) {
230
322
  this.events.push(event);
231
323
  this.commitTimestamps.push(commit.timestamp);
@@ -289,8 +381,27 @@ export class GitEfficiencyTracker {
289
381
  const pushCount = this.events.filter((e) => e.type === 'push' || e.type === 'force_push' || e.type === 'force_push_lease').length;
290
382
  const commitCount = this.events.filter((e) => e.type === 'commit').length;
291
383
  const branchOperations = this.events.filter((e) => e.type === 'branch').length;
292
- const resolved = this.conflictRecords.filter((c) => c.resolution === 'resolved');
293
- const conflictResolutionRate = this.conflictRecords.length > 0 ? resolved.length / this.conflictRecords.length : null;
384
+ // A conflict that's currently open (mid-merge, not yet aborted or
385
+ // resolved) is counted in the mergeConflicts/rebaseConflicts KPI above,
386
+ // but this.conflictRecords only gains an entry once it's aborted or
387
+ // resolved — so it would otherwise never enter the resolution-rate
388
+ // denominator, letting a session with one resolved and one still-open
389
+ // conflict show a "perfect" 100% rate. Synthesizing (not persisting) a
390
+ // 'pending' record per still-queued conflict keeps the denominator
391
+ // honest without ever double-counting once that conflict does resolve —
392
+ // at that point it leaves the queue and gets a real entry instead.
393
+ const allConflictRecords = [
394
+ ...this.conflictRecords,
395
+ ...this.pendingConflicts.map((p) => ({
396
+ timestamp: p.timestamp,
397
+ resolution: 'pending',
398
+ resolutionTimeMs: null,
399
+ command: p.command,
400
+ files: p.files,
401
+ })),
402
+ ];
403
+ const resolved = allConflictRecords.filter((c) => c.resolution === 'resolved');
404
+ const conflictResolutionRate = allConflictRecords.length > 0 ? resolved.length / allConflictRecords.length : null;
294
405
  const resolutionTimes = resolved
295
406
  .filter((c) => c.resolutionTimeMs !== null)
296
407
  .map((c) => c.resolutionTimeMs);
@@ -356,9 +467,13 @@ export class GitEfficiencyTracker {
356
467
  // (GitEfficiency.tsx's `[...timeline].reverse().slice(0, 30)`) picks
357
468
  // the true newest 30, not whichever 30 happened to be pushed last.
358
469
  gitCommandTimeline: [...this.events].sort((a, b) => a.timestamp - b.timestamp).slice(-50),
359
- conflictHistory: [...this.conflictRecords].sort((a, b) => a.timestamp - b.timestamp),
360
- suggestions,
361
- bestPractices,
470
+ conflictHistory: [...allConflictRecords].sort((a, b) => a.timestamp - b.timestamp),
471
+ // Both arrays render in whatever order the checks above happen to
472
+ // .push() in — fixed source-code order, not severity order — so a
473
+ // critical-severity item could render below a milder one. Sort by
474
+ // severity (most severe first) before exposing.
475
+ suggestions: [...suggestions].sort((a, b) => SUGGESTION_SEVERITY_RANK[a.severity] - SUGGESTION_SEVERITY_RANK[b.severity]),
476
+ bestPractices: [...bestPractices].sort((a, b) => BEST_PRACTICE_STATUS_RANK[a.status] - BEST_PRACTICE_STATUS_RANK[b.status]),
362
477
  preventionScore,
363
478
  efficiencyScore,
364
479
  riskIndicators,
@@ -371,9 +486,7 @@ export class GitEfficiencyTracker {
371
486
  reset(_sessionId) {
372
487
  this.events = [];
373
488
  this.conflictRecords = [];
374
- this.pendingConflictTimestamp = null;
375
- this.pendingConflictCommand = '';
376
- this.pendingConflictFiles = [];
489
+ this.pendingConflicts = [];
377
490
  this.lastSyncTimestamp = null;
378
491
  this.pullsSinceLastConflict = 0;
379
492
  this.consecutiveFailedPushes = 0;
@@ -389,6 +502,9 @@ export class GitEfficiencyTracker {
389
502
  this.editedFiles.clear();
390
503
  this.hasUsedWorktree = false;
391
504
  this.hasUsedForceWithLease = false;
505
+ this.hasUsedBareForcePush = false;
506
+ this.bareForcePushCount = 0;
507
+ this.hasForcePushedToDefaultBranch = false;
392
508
  this.totalToolCalls = 0;
393
509
  this.sessionStartTimestamp = null;
394
510
  this.commitTimestamps = [];
@@ -404,7 +520,6 @@ export class GitEfficiencyTracker {
404
520
  this.commitsBehindMain = null;
405
521
  this.quickConflictResolutions = 0;
406
522
  this.prEvents = [];
407
- this.firstCommitTimestamp = null;
408
523
  this.repoContext = { repoName: null, branch: null, remoteName: null, defaultBranch: null };
409
524
  }
410
525
  // -------------------------------------------------------------------------
@@ -472,18 +587,26 @@ export class GitEfficiencyTracker {
472
587
  return { ...base, type: 'other_git' };
473
588
  }
474
589
  processEvent(event, command, record) {
475
- // Track conflict resolution strategies regardless of event type
476
- if (GIT_CHECKOUT_OURS_RE.test(command))
477
- this.oursCount++;
478
- if (GIT_CHECKOUT_THEIRS_RE.test(command))
479
- this.theirsCount++;
480
- if (GIT_CHERRY_PICK_RE.test(command))
481
- this.cherryPickCount++;
590
+ // Attribute ours/theirs/cherry-pick resolution strategy to the oldest
591
+ // still-open conflict, not to every matching command — a multi-file
592
+ // conflict resolved with one `--ours` per file must count once toward
593
+ // that conflict, not once per file, and a strategy command run with no
594
+ // conflict pending isn't resolving anything. `--abort` also matches the
595
+ // bare cherry-pick pattern, so it's excluded explicitly — aborting isn't
596
+ // a resolution strategy.
597
+ const oldestPending = this.pendingConflicts[0];
598
+ if (oldestPending) {
599
+ if (GIT_CHECKOUT_OURS_RE.test(command))
600
+ oldestPending.usedOurs = true;
601
+ if (GIT_CHECKOUT_THEIRS_RE.test(command))
602
+ oldestPending.usedTheirs = true;
603
+ if (GIT_CHERRY_PICK_RE.test(command) && !CHERRY_PICK_ABORT_RE.test(command)) {
604
+ oldestPending.usedCherryPick = true;
605
+ }
606
+ }
482
607
  switch (event.type) {
483
608
  case 'merge_conflict':
484
609
  case 'rebase_conflict': {
485
- this.pendingConflictTimestamp = event.timestamp;
486
- this.pendingConflictCommand = command;
487
610
  const output = record.error ?? '';
488
611
  const files = [];
489
612
  let match;
@@ -492,56 +615,64 @@ export class GitEfficiencyTracker {
492
615
  files.push(match[1].trim());
493
616
  this.conflictedFiles.add(match[1].trim());
494
617
  }
495
- this.pendingConflictFiles = files;
618
+ this.pendingConflicts.push({
619
+ timestamp: event.timestamp,
620
+ command,
621
+ files,
622
+ usedOurs: false,
623
+ usedTheirs: false,
624
+ usedCherryPick: false,
625
+ });
496
626
  this.pullsSinceLastConflict = 0;
497
627
  break;
498
628
  }
499
629
  case 'merge_abort':
500
630
  case 'rebase_abort':
501
- case 'cherry_pick_abort':
502
- if (this.pendingConflictTimestamp !== null) {
631
+ case 'cherry_pick_abort': {
632
+ const pending = this.pendingConflicts.shift();
633
+ if (pending) {
503
634
  this.conflictRecords.push({
504
- timestamp: this.pendingConflictTimestamp,
635
+ timestamp: pending.timestamp,
505
636
  resolution: 'aborted',
506
- resolutionTimeMs: event.timestamp - this.pendingConflictTimestamp,
507
- command: this.pendingConflictCommand,
508
- files: this.pendingConflictFiles,
637
+ resolutionTimeMs: event.timestamp - pending.timestamp,
638
+ command: pending.command,
639
+ files: pending.files,
509
640
  });
510
- this.pendingConflictTimestamp = null;
511
- this.pendingConflictCommand = '';
512
- this.pendingConflictFiles = [];
513
641
  }
514
642
  break;
643
+ }
515
644
  case 'commit': {
516
645
  // git commit --amend fixes a prior commit, not a merge conflict.
517
- // Clear the pending conflict on amend so the *next* normal commit
518
- // doesn't see a stale pendingConflictTimestamp (potentially hours old).
646
+ // Drop the oldest pending conflict on amend (without recording a
647
+ // resolution) so a later, unrelated commit doesn't retroactively
648
+ // "resolve" it.
519
649
  if (command.includes('--amend')) {
520
- this.pendingConflictTimestamp = null;
521
- this.pendingConflictCommand = '';
522
- this.pendingConflictFiles = [];
650
+ this.pendingConflicts.shift();
523
651
  }
524
- if (this.pendingConflictTimestamp !== null && !command.includes('--amend')) {
525
- const resolutionMs = event.timestamp - this.pendingConflictTimestamp;
526
- this.conflictRecords.push({
527
- timestamp: this.pendingConflictTimestamp,
528
- resolution: 'resolved',
529
- resolutionTimeMs: resolutionMs,
530
- command: this.pendingConflictCommand,
531
- files: this.pendingConflictFiles,
532
- });
533
- // Under 30s resolution with multiple conflicted files is suspiciously fast
534
- if (resolutionMs < 30_000 && this.pendingConflictFiles.length > 1) {
535
- this.quickConflictResolutions++;
652
+ else {
653
+ const pending = this.pendingConflicts.shift();
654
+ if (pending) {
655
+ const resolutionMs = event.timestamp - pending.timestamp;
656
+ this.conflictRecords.push({
657
+ timestamp: pending.timestamp,
658
+ resolution: 'resolved',
659
+ resolutionTimeMs: resolutionMs,
660
+ command: pending.command,
661
+ files: pending.files,
662
+ });
663
+ if (pending.usedOurs)
664
+ this.oursCount++;
665
+ if (pending.usedTheirs)
666
+ this.theirsCount++;
667
+ if (pending.usedCherryPick)
668
+ this.cherryPickCount++;
669
+ // Under 30s resolution with multiple conflicted files is suspiciously fast
670
+ if (resolutionMs < 30_000 && pending.files.length > 1) {
671
+ this.quickConflictResolutions++;
672
+ }
536
673
  }
537
- this.pendingConflictTimestamp = null;
538
- this.pendingConflictCommand = '';
539
- this.pendingConflictFiles = [];
540
674
  }
541
675
  this.commitTimestamps.push(event.timestamp);
542
- if (this.firstCommitTimestamp === null) {
543
- this.firstCommitTimestamp = event.timestamp;
544
- }
545
676
  this.commitsSinceLastSync++;
546
677
  this.statusChecksSinceLastAction = 0;
547
678
  break;
@@ -582,6 +713,13 @@ export class GitEfficiencyTracker {
582
713
  this.statusChecksSinceLastAction = 0;
583
714
  break;
584
715
  case 'force_push':
716
+ this.hasUsedBareForcePush = true;
717
+ this.bareForcePushCount++;
718
+ if (this.repoContext.branch !== null &&
719
+ this.repoContext.defaultBranch !== null &&
720
+ this.repoContext.branch === this.repoContext.defaultBranch) {
721
+ this.hasForcePushedToDefaultBranch = true;
722
+ }
585
723
  if (this.lastPushRejectedTimestamp !== null &&
586
724
  event.timestamp - this.lastPushRejectedTimestamp < 300_000) {
587
725
  this.forceAfterReject++;
@@ -613,8 +751,14 @@ export class GitEfficiencyTracker {
613
751
  this.statusChecksSinceLastAction = 0;
614
752
  break;
615
753
  case 'worktree':
616
- this.hasUsedWorktree = true;
617
- this.worktreeCommands++;
754
+ // Both the "worktree ops" count and the usesWorktrees/use_worktrees
755
+ // signal are meant to reflect real worktree usage, not read-only
756
+ // inspection — `list`/`prune`/`lock`/etc. shouldn't count as evidence
757
+ // that worktrees were used to isolate parallel work.
758
+ if (GIT_WORKTREE_ADD_REMOVE_RE.test(command)) {
759
+ this.worktreeCommands++;
760
+ this.hasUsedWorktree = true;
761
+ }
618
762
  break;
619
763
  case 'status':
620
764
  this.statusChecksSinceLastAction++;
@@ -624,18 +768,24 @@ export class GitEfficiencyTracker {
624
768
  break;
625
769
  }
626
770
  }
627
- // A pull immediately followed by a conflict means the branch had already
628
- // diverged enough that even the act of syncing produced a conflict — a
629
- // stronger signal of a stale branch than a conflict from, say, a later
630
- // merge or rebase attempted well after the pull.
771
+ // A pull that diverged enough to conflict is the strongest signal of a
772
+ // stale branch checked directly off the conflicting event's own command
773
+ // (a `git pull` whose own output contains a conflict indicator classifies
774
+ // as merge_conflict/rebase_conflict, never as 'pull', so this can't rely
775
+ // on the event's `type`). A pull immediately followed by a *separate*
776
+ // command that then conflicts is a weaker but still real signal, kept as a
777
+ // fallback for events that don't match the direct case.
631
778
  countStaleBranchPulls() {
632
779
  let staleCount = 0;
633
- for (let i = 0; i < this.events.length - 1; i++) {
634
- if (this.events[i].type === 'pull') {
635
- const next = this.events[i + 1];
636
- if (next.type === 'merge_conflict' || next.type === 'rebase_conflict') {
637
- staleCount++;
638
- }
780
+ for (let i = 0; i < this.events.length; i++) {
781
+ const event = this.events[i];
782
+ if (event.type !== 'merge_conflict' && event.type !== 'rebase_conflict')
783
+ continue;
784
+ if (GIT_PULL_RE.test(event.command ?? '')) {
785
+ staleCount++;
786
+ }
787
+ else if (i > 0 && this.events[i - 1].type === 'pull') {
788
+ staleCount++;
639
789
  }
640
790
  }
641
791
  return staleCount;
@@ -786,7 +936,12 @@ export class GitEfficiencyTracker {
786
936
  detail: 'No worktree usage detected. Consider worktrees if you run parallel sessions.',
787
937
  });
788
938
  }
789
- // 5. Use --force-with-lease instead of --force
939
+ // 5. Use --force-with-lease instead of --force. Gated on
940
+ // hasUsedBareForcePush rather than forcePushes/usesForceWithLease alone —
941
+ // those two count safe and unsafe force-pushes together, so checking
942
+ // usesForceWithLease alone would let one safe `--force-with-lease` mask a
943
+ // dangerous bare `--force` in the same session with a fully-passing
944
+ // status.
790
945
  if (stats.forcePushes === 0) {
791
946
  practices.push({
792
947
  id: 'force_with_lease',
@@ -795,15 +950,15 @@ export class GitEfficiencyTracker {
795
950
  detail: 'No force pushes yet.',
796
951
  });
797
952
  }
798
- else if (risk.usesForceWithLease) {
953
+ else if (this.hasUsedBareForcePush && risk.usesForceWithLease) {
799
954
  practices.push({
800
955
  id: 'force_with_lease',
801
956
  label: 'Use --force-with-lease',
802
- status: 'pass',
803
- detail: "Goodusing --force-with-lease which refuses to overwrite remote commits you haven't seen.",
957
+ status: 'warn',
958
+ detail: 'Mixed usage this session some force pushes used --force-with-lease, but at least one bare --force (unsafe) push also occurred. Always use --force-with-lease; it refuses to push if someone else has pushed to the branch since your last fetch.',
804
959
  });
805
960
  }
806
- else {
961
+ else if (this.hasUsedBareForcePush) {
807
962
  practices.push({
808
963
  id: 'force_with_lease',
809
964
  label: 'Use --force-with-lease',
@@ -811,24 +966,15 @@ export class GitEfficiencyTracker {
811
966
  detail: 'Using bare --force instead of --force-with-lease. The --force-with-lease flag is a safety net: it refuses to push if someone else has pushed to the branch since your last fetch. Always prefer it.',
812
967
  });
813
968
  }
814
- // 6. Don't force-push after a rejection without investigating
815
- if (risk.forceAfterReject > 0) {
816
- practices.push({
817
- id: 'no_force_after_reject',
818
- label: "Don't force-push after rejection",
819
- status: 'fail',
820
- detail: `Push was rejected ${risk.pushRejections} time(s) and then force-pushed ${risk.forceAfterReject} time(s). When a push is rejected, pull + rebase first to incorporate upstream changes. Force pushing after a rejection overwrites others' work.`,
821
- });
822
- }
823
- else if (risk.pushRejections > 0) {
969
+ else {
824
970
  practices.push({
825
- id: 'no_force_after_reject',
826
- label: "Don't force-push after rejection",
971
+ id: 'force_with_lease',
972
+ label: 'Use --force-with-lease',
827
973
  status: 'pass',
828
- detail: 'Push was rejected but correctly handled without force pushing.',
974
+ detail: "Good using --force-with-lease which refuses to overwrite remote commits you haven't seen.",
829
975
  });
830
976
  }
831
- // 7. Keep PRs small (proxy: many commits without pushing)
977
+ // 6. Keep PRs small (proxy: many commits without pushing)
832
978
  if (risk.commitsSinceLastSync > 15) {
833
979
  practices.push({
834
980
  id: 'small_increments',
@@ -845,7 +991,7 @@ export class GitEfficiencyTracker {
845
991
  detail: 'Good — committing and syncing in small batches.',
846
992
  });
847
993
  }
848
- // 8. Avoid editing hot files
994
+ // 7. Avoid editing hot files
849
995
  if (risk.hotFiles.length > 0) {
850
996
  practices.push({
851
997
  id: 'avoid_hot_files',
@@ -854,7 +1000,7 @@ export class GitEfficiencyTracker {
854
1000
  detail: `Editing files that previously conflicted: ${risk.hotFiles.slice(0, 3).join(', ')}${risk.hotFiles.length > 3 ? ` (+${risk.hotFiles.length - 3} more)` : ''}. These are "hot" files with active upstream changes — edits here are likely to conflict again. Consider coordinating or waiting for upstream to stabilize.`,
855
1001
  });
856
1002
  }
857
- // 9. Build/test before pushing
1003
+ // 8. Build/test before pushing
858
1004
  if (this.buildBeforePush === null && this.lastPushTimestamp === null) {
859
1005
  practices.push({
860
1006
  id: 'verify_before_push',
@@ -899,22 +1045,14 @@ export class GitEfficiencyTracker {
899
1045
  generateSuggestions(stats) {
900
1046
  const suggestions = [];
901
1047
  // --- Proactive prevention suggestions (fire BEFORE conflicts happen) ---
902
- if (stats.riskIndicators.syncedBeforeEditing === false) {
903
- suggestions.push({
904
- severity: 'warning',
905
- category: 'no_initial_sync',
906
- message: 'Started editing without syncing first. Run `git fetch && git rebase origin/main` (or your target branch) at the start of every session. This single habit prevents the majority of AI-assisted coding conflicts.',
907
- evidence: 'First file edit occurred before any git pull/fetch',
908
- });
909
- }
910
- if (stats.riskIndicators.commitsSinceLastSync > 8) {
911
- suggestions.push({
912
- severity: 'warning',
913
- category: 'drift_risk',
914
- message: `${stats.riskIndicators.commitsSinceLastSync} commits without syncing. You're accumulating drift that will compound into painful conflicts. Run \`git fetch && git rebase origin/main\` now — smaller, frequent rebases are far easier than one large one later.`,
915
- evidence: `${stats.riskIndicators.commitsSinceLastSync} commits since last pull/fetch/rebase`,
916
- });
917
- }
1048
+ //
1049
+ // syncedBeforeEditing, commitsSinceLastSync (drift), and hotFiles are
1050
+ // ongoing-state conditions already surfaced as their own Best Practice
1051
+ // checks (sync_before_edit, frequent_sync, avoid_hot_files) — duplicating
1052
+ // them here as one-off suggestions rendered the same fact twice, with a
1053
+ // conflicting severity in the hot-files case. Best Practices is their
1054
+ // canonical home; only force-after-reject (a one-time, reactive event
1055
+ // with actionable remediation) is kept as a suggestion.
918
1056
  if (stats.riskIndicators.forceAfterReject > 0) {
919
1057
  suggestions.push({
920
1058
  severity: 'critical',
@@ -923,14 +1061,6 @@ export class GitEfficiencyTracker {
923
1061
  evidence: `${stats.riskIndicators.forceAfterReject} force push(es) within 5 min of a rejection`,
924
1062
  });
925
1063
  }
926
- if (stats.riskIndicators.hotFiles.length > 0) {
927
- suggestions.push({
928
- severity: 'info',
929
- category: 'hot_files',
930
- message: `You're editing files that previously conflicted (${stats.riskIndicators.hotFiles.slice(0, 3).join(', ')}). These likely have active upstream work. Consider: (1) rebasing immediately to get latest state, (2) coordinating with whoever else is touching these files, or (3) deferring changes until upstream settles.`,
931
- evidence: `${stats.riskIndicators.hotFiles.length} previously-conflicted file(s) re-edited`,
932
- });
933
- }
934
1064
  // --- Reactive suggestions (fire after problems occur) ---
935
1065
  if (stats.mergeConflicts + stats.rebaseConflicts >= 3) {
936
1066
  suggestions.push({
@@ -956,20 +1086,40 @@ export class GitEfficiencyTracker {
956
1086
  evidence: `${stats.abortedOperations} aborted operations`,
957
1087
  });
958
1088
  }
959
- if (stats.forcePushes >= 2) {
1089
+ // Severity is gated on hasUsedBareForcePush/bareForcePushCount — the same
1090
+ // shared signal the force_with_lease best-practice check uses — rather
1091
+ // than the raw forcePushes count, which sums bare AND lease-protected
1092
+ // pushes together. Gating (and scaling) on the bare-only count keeps this
1093
+ // suggestion's ranking consistent with the best-practice check: adding a
1094
+ // safe --force-with-lease push on top of an existing bare push must never
1095
+ // raise severity, since only the bare push is actually risky.
1096
+ //
1097
+ // A bare push to the shared default branch is escalated to 'critical'
1098
+ // outright, regardless of count — it can clobber other collaborators'
1099
+ // work, unlike an identical push to a personal feature branch (which
1100
+ // still scales by count, as above).
1101
+ if (this.hasUsedBareForcePush) {
960
1102
  suggestions.push({
961
- severity: 'critical',
1103
+ severity: this.hasForcePushedToDefaultBranch
1104
+ ? 'critical'
1105
+ : this.bareForcePushCount >= 2
1106
+ ? 'critical'
1107
+ : 'warning',
962
1108
  category: 'force_push',
963
- message: 'Multiple force pushes this session. Always use --force-with-lease as a safety net. If you need to rewrite history, coordinate with collaborators first and ensure your local refs are up to date with `git fetch` before force pushing.',
964
- evidence: `${stats.forcePushes} force pushes this session`,
1109
+ message: this.hasForcePushedToDefaultBranch
1110
+ ? `Bare --force push used on the shared default branch (${this.repoContext.defaultBranch ?? 'default branch'}) — this can overwrite history other collaborators are building on. Always use --force-with-lease, and avoid force-pushing the default branch entirely if possible.`
1111
+ : 'Bare --force push used. Always use --force-with-lease instead — it refuses to push if someone else has pushed to the branch since your last fetch. If you need to rewrite history, coordinate with collaborators first and ensure your local refs are up to date with `git fetch` before force pushing.',
1112
+ evidence: this.hasForcePushedToDefaultBranch
1113
+ ? `${this.bareForcePushCount} bare --force push(es) this session, including at least one on the default branch`
1114
+ : `${this.bareForcePushCount} bare --force push(es) this session`,
965
1115
  });
966
1116
  }
967
- else if (stats.forcePushes === 1) {
1117
+ else if (stats.riskIndicators.usesForceWithLease && stats.forcePushes >= 2) {
968
1118
  suggestions.push({
969
1119
  severity: 'info',
970
1120
  category: 'force_push',
971
- message: "Force push used. Prefer --force-with-lease for safer force pushes it refuses to overwrite commits you haven't seen locally.",
972
- evidence: '1 force push',
1121
+ message: 'Multiple force pushes this session, all using --force-with-lease the safe pattern. Repeated history rewrites can still be worth a second look if they indicate a workflow issue upstream.',
1122
+ evidence: `${stats.forcePushes} lease-protected force pushes this session`,
973
1123
  });
974
1124
  }
975
1125
  if (stats.resetHards >= 2) {
@@ -1123,15 +1273,16 @@ export class GitEfficiencyTracker {
1123
1273
  };
1124
1274
  }
1125
1275
  computeConflictStrategy() {
1126
- const manualMergeCount = this.conflictRecords.filter((c) => c.resolution === 'resolved').length -
1276
+ const manualMergeCount = Math.max(0, this.conflictRecords.filter((c) => c.resolution === 'resolved').length -
1127
1277
  this.oursCount -
1128
- this.theirsCount;
1278
+ this.theirsCount -
1279
+ this.cherryPickCount);
1129
1280
  return {
1130
1281
  oursCount: this.oursCount,
1131
1282
  theirsCount: this.theirsCount,
1132
- manualMergeCount: Math.max(0, manualMergeCount),
1283
+ manualMergeCount,
1133
1284
  cherryPickCount: this.cherryPickCount,
1134
- totalResolutions: this.oursCount + this.theirsCount + Math.max(0, manualMergeCount),
1285
+ totalResolutions: this.oursCount + this.theirsCount + this.cherryPickCount + manualMergeCount,
1135
1286
  };
1136
1287
  }
1137
1288
  }