@@ -24,6 +24,7 @@ import {
2424 computeRevertTurnCountByUserMessageId ,
2525 agentEditSetByTurnId ,
2626 pathIsInAgentEditSet ,
27+ toRepoRelativePath ,
2728 type TimelineEntry ,
2829} from "./session-logic" ;
2930import { type TurnDiffSummary } from "./types" ;
@@ -1656,30 +1657,59 @@ describe("agentEditSetByTurnId", () => {
16561657 } ) ;
16571658} ) ;
16581659
1660+ describe ( "toRepoRelativePath" , ( ) => {
1661+ it ( "strips the root prefix from an absolute path under the repo root" , ( ) => {
1662+ expect ( toRepoRelativePath ( "/repo/src/foo.ts" , "/repo" ) ) . toBe ( "src/foo.ts" ) ;
1663+ } ) ;
1664+
1665+ it ( "strips trailing slash from root before matching" , ( ) => {
1666+ expect ( toRepoRelativePath ( "/repo/src/foo.ts" , "/repo/" ) ) . toBe ( "src/foo.ts" ) ;
1667+ } ) ;
1668+
1669+ it ( "leaves an already-relative path unchanged" , ( ) => {
1670+ expect ( toRepoRelativePath ( "src/bar.ts" , "/repo" ) ) . toBe ( "src/bar.ts" ) ;
1671+ } ) ;
1672+
1673+ it ( "leaves a path that is absolute but outside the root unchanged" , ( ) => {
1674+ expect ( toRepoRelativePath ( "/other/src/foo.ts" , "/repo" ) ) . toBe ( "/other/src/foo.ts" ) ;
1675+ } ) ;
1676+
1677+ it ( "strips a leading ./ before processing" , ( ) => {
1678+ expect ( toRepoRelativePath ( "./src/foo.ts" , "/repo" ) ) . toBe ( "src/foo.ts" ) ;
1679+ } ) ;
1680+
1681+ it ( "returns the path unchanged when root is null" , ( ) => {
1682+ expect ( toRepoRelativePath ( "src/foo.ts" , null ) ) . toBe ( "src/foo.ts" ) ;
1683+ } ) ;
1684+
1685+ it ( "returns the path unchanged when root is undefined" , ( ) => {
1686+ expect ( toRepoRelativePath ( "/repo/src/foo.ts" , undefined ) ) . toBe ( "/repo/src/foo.ts" ) ;
1687+ } ) ;
1688+ } ) ;
1689+
16591690describe ( "pathIsInAgentEditSet" , ( ) => {
1660- it ( "matches when absolute agent path ends with /diffPath" , ( ) => {
1661- expect ( pathIsInAgentEditSet ( "src/foo.ts" , [ "/abs/repo/src/foo.ts" ] ) ) . toBe ( true ) ;
1691+ // Agent paths are now pre-normalized to repo-relative by agentEditSetByTurnId.
1692+ // pathIsInAgentEditSet uses exact equality after stripping a leading ./ from diffPath.
1693+
1694+ it ( "matches when both are repo-relative and equal" , ( ) => {
1695+ expect ( pathIsInAgentEditSet ( "src/foo.ts" , [ "src/foo.ts" ] ) ) . toBe ( true ) ;
16621696 } ) ;
16631697
16641698 it ( "matches when agent path equals diffPath exactly (relative-to-relative)" , ( ) => {
16651699 expect ( pathIsInAgentEditSet ( "src/bar.ts" , [ "src/bar.ts" ] ) ) . toBe ( true ) ;
16661700 } ) ;
16671701
1668- it ( "strips a leading ./ from diffPath before matching" , ( ) => {
1669- expect ( pathIsInAgentEditSet ( "./src/foo.ts" , [ "/abs/repo/ src/foo.ts" ] ) ) . toBe ( true ) ;
1702+ it ( "strips a leading ./ from diffPath before exact matching" , ( ) => {
1703+ expect ( pathIsInAgentEditSet ( "./src/foo.ts" , [ "src/foo.ts" ] ) ) . toBe ( true ) ;
16701704 } ) ;
16711705
1672- it ( "requires a / boundary — bare suffix without separator does NOT match" , ( ) => {
1673- // diff path "foo.ts" should not match agent "/repo/src/foo.ts" only via trailing substring
1674- // because the boundary check uses endsWith("/" + diffPath), so "foo.ts" hits the boundary
1675- // correctly; but "oo.ts" must NOT match "foo.ts"
1676- expect ( pathIsInAgentEditSet ( "oo.ts" , [ "/abs/repo/src/foo.ts" ] ) ) . toBe ( false ) ;
1706+ it ( "does NOT match a bare suffix without full path equality" , ( ) => {
1707+ // agent paths are repo-relative; "oo.ts" should not match "src/foo.ts"
1708+ expect ( pathIsInAgentEditSet ( "oo.ts" , [ "src/foo.ts" ] ) ) . toBe ( false ) ;
16771709 } ) ;
16781710
16791711 it ( "returns false when diffPath is not in agentPaths" , ( ) => {
1680- expect ( pathIsInAgentEditSet ( "src/other.ts" , [ "/abs/repo/src/foo.ts" , "src/bar.ts" ] ) ) . toBe (
1681- false ,
1682- ) ;
1712+ expect ( pathIsInAgentEditSet ( "src/other.ts" , [ "src/foo.ts" , "src/bar.ts" ] ) ) . toBe ( false ) ;
16831713 } ) ;
16841714
16851715 it ( "returns false for an empty agent set" , ( ) => {
@@ -1731,8 +1761,47 @@ describe("computeRevertTurnCountByUserMessageId — agent edit set gate", () =>
17311761 [ MessageId . make ( "a1" ) , summary ( "turn-1" , [ { path : "src/x.ts" } ] , 3 ) ] ,
17321762 ] ) ,
17331763 inferredCheckpointTurnCountByTurnId : { } ,
1734- agentEditSetByTurnId : new Map ( [ [ TurnId . make ( "turn-1" ) , [ "/repo/ src/x.ts" ] ] ] ) ,
1764+ agentEditSetByTurnId : new Map ( [ [ TurnId . make ( "turn-1" ) , [ "src/x.ts" ] ] ] ) ,
17351765 } ) ;
17361766 expect ( result . get ( MessageId . make ( "u1" ) ) ) . toBe ( 2 ) ;
17371767 } ) ;
1768+
1769+ it ( "per-span: shows affordance when matched turn is empty but a later turn in span has edits" , ( ) => {
1770+ // turn-1 (checkpointTurnCount 1) → matched turn for u1, restoreTarget = 0
1771+ // turn-2 (checkpointTurnCount 2) → in the span (> restoreTarget 0), non-empty agent edit set
1772+ // The affordance should be offered because the SPAN has agent edits.
1773+ // Current single-turn code gates on turn-1's empty set → does NOT offer → this test is RED.
1774+ const result = computeRevertTurnCountByUserMessageId ( {
1775+ timelineEntries : [ messageEntry ( "u1" , "user" ) , messageEntry ( "a1" , "assistant" ) ] ,
1776+ turnDiffSummaryByAssistantMessageId : new Map ( [
1777+ [ MessageId . make ( "a1" ) , summary ( "turn-1" , [ { path : "src/x.ts" } ] , 1 ) ] ,
1778+ // turn-2 exists in the summary map even though it's not in the timeline (it could be
1779+ // from a prior segment); the gate must scan ALL summaries to build the span union.
1780+ [ MessageId . make ( "a2" ) , summary ( "turn-2" , [ { path : "src/y.ts" } ] , 2 ) ] ,
1781+ ] ) ,
1782+ inferredCheckpointTurnCountByTurnId : { } ,
1783+ agentEditSetByTurnId : new Map ( [
1784+ [ TurnId . make ( "turn-1" ) , [ ] ] , // matched turn — empty
1785+ [ TurnId . make ( "turn-2" ) , [ "src/y.ts" ] ] , // in span (checkpointTurnCount 2 > restoreTarget 0) — non-empty
1786+ ] ) ,
1787+ } ) ;
1788+ expect ( result . has ( MessageId . make ( "u1" ) ) ) . toBe ( true ) ;
1789+ expect ( result . get ( MessageId . make ( "u1" ) ) ) . toBe ( 0 ) ; // Math.max(0, 1 - 1)
1790+ } ) ;
1791+
1792+ it ( "per-span: hides affordance when all turns in span have empty agent edit sets" , ( ) => {
1793+ const result = computeRevertTurnCountByUserMessageId ( {
1794+ timelineEntries : [ messageEntry ( "u1" , "user" ) , messageEntry ( "a1" , "assistant" ) ] ,
1795+ turnDiffSummaryByAssistantMessageId : new Map ( [
1796+ [ MessageId . make ( "a1" ) , summary ( "turn-1" , [ { path : "src/x.ts" } ] , 1 ) ] ,
1797+ [ MessageId . make ( "a2" ) , summary ( "turn-2" , [ { path : "src/y.ts" } ] , 2 ) ] ,
1798+ ] ) ,
1799+ inferredCheckpointTurnCountByTurnId : { } ,
1800+ agentEditSetByTurnId : new Map ( [
1801+ [ TurnId . make ( "turn-1" ) , [ ] ] , // empty
1802+ [ TurnId . make ( "turn-2" ) , [ ] ] , // also empty
1803+ ] ) ,
1804+ } ) ;
1805+ expect ( result . has ( MessageId . make ( "u1" ) ) ) . toBe ( false ) ;
1806+ } ) ;
17381807} ) ;
0 commit comments