@@ -255,8 +255,24 @@ vi.mock("../uiStateStore", () => ({
255255} ) ) ;
256256vi . mock ( "./useSettings" , ( ) => ( { useClientSettings : ( ) => ( { } ) } ) ) ;
257257
258+ import { derivePhysicalProjectKey } from "../logicalProject" ;
259+ import { buildPhysicalToLogicalProjectKeyMap } from "../sidebarProjectGrouping" ;
258260import { useNewThreadHandler } from "./useHandleNewThread" ;
259261
262+ /** Logical key New Chat should register: the group key from `buildProjectGroups`, not a re-derivation. */
263+ function expectedLogicalProjectKey ( project : {
264+ environmentId : string ;
265+ workspaceRoot : string ;
266+ } ) : string {
267+ return (
268+ buildPhysicalToLogicalProjectKeyMap ( {
269+ projects : testState . projects ,
270+ settings : { } ,
271+ primaryEnvironmentId : "environment-primary" ,
272+ } ) . get ( derivePhysicalProjectKey ( project ) ) ?? "remote-project"
273+ ) ;
274+ }
275+
260276describe . each ( [
261277 [ "new" , null ] ,
262278 [
@@ -283,7 +299,7 @@ describe.each([
283299 const opened = await pendingOpen ;
284300
285301 expect ( testState . draftStore . setLogicalProjectDraftThreadId ) . toHaveBeenCalledWith (
286- "remote-project" ,
302+ expectedLogicalProjectKey ( testState . remoteProject ) ,
287303 projectRef ,
288304 opened ! . draftId ,
289305 expect . objectContaining ( { runtimeMode } ) ,
@@ -327,7 +343,7 @@ describe.each([
327343 threadId : draft ?. threadId ?? "thread-delayed" ,
328344 } ) ;
329345 expect ( testState . draftStore . setLogicalProjectDraftThreadId ) . toHaveBeenCalledWith (
330- "remote-project" ,
346+ expectedLogicalProjectKey ( testState . remoteProject ) ,
331347 projectRef ,
332348 opened ! . draftId ,
333349 expect . objectContaining ( { envMode : "worktree" , startFromOrigin } ) ,
@@ -354,7 +370,7 @@ describe.each([
354370 const opened = await openThread ( projectRef , { envMode : "worktree" , startFromOrigin } ) ;
355371
356372 expect ( testState . draftStore . setLogicalProjectDraftThreadId ) . toHaveBeenCalledWith (
357- "remote-project" ,
373+ expectedLogicalProjectKey ( testState . remoteProject ) ,
358374 projectRef ,
359375 opened ! . draftId ,
360376 expect . objectContaining ( { envMode : "worktree" , startFromOrigin } ) ,
@@ -369,6 +385,7 @@ describe("useNewThreadHandler with a disconnected remote", () => {
369385 projectId : "project-remote" ,
370386 } as never ;
371387
388+ /** Marks the SSH environment reconnecting so New Chat cannot target it. */
372389 function disconnectRemote ( ) {
373390 testState . environments = testState . environments . map ( ( environment ) =>
374391 environment . environmentId === "environment-ssh"
@@ -425,7 +442,7 @@ describe("useNewThreadHandler with a disconnected remote", () => {
425442 expect ( opened ) . toEqual ( { draftId : "draft-delayed" , threadId : "thread-delayed" } ) ;
426443 expect ( testState . toastAdd ) . not . toHaveBeenCalled ( ) ;
427444 expect ( testState . draftStore . setLogicalProjectDraftThreadId ) . toHaveBeenCalledWith (
428- "remote-project" ,
445+ expectedLogicalProjectKey ( testState . localProject ) ,
429446 { environmentId : "environment-primary" , projectId : "project-local" } ,
430447 "draft-delayed" ,
431448 expect . objectContaining ( {
@@ -436,6 +453,36 @@ describe("useNewThreadHandler with a disconnected remote", () => {
436453 ) ;
437454 } ) ;
438455
456+ it ( "reuses the group key when the reachable winner has no repository identity" , async ( ) => {
457+ testState . reset ( null ) ;
458+ const unidentifiedLocal = {
459+ ...testState . localProject ,
460+ repositoryIdentity : null ,
461+ updatedAt : "2026-01-04T00:00:00.000Z" ,
462+ } ;
463+ testState . projects = [
464+ { ...testState . remoteProject , repositoryIdentity : testState . repositoryIdentity } ,
465+ unidentifiedLocal ,
466+ {
467+ ...testState . localProject ,
468+ id : "project-local-identified" ,
469+ updatedAt : "2026-01-03T00:00:00.000Z" ,
470+ } ,
471+ ] ;
472+ disconnectRemote ( ) ;
473+
474+ const pendingOpen = useNewThreadHandler ( ) ( remoteRef ) ;
475+ testState . completeProjectFileRead ( null ) ;
476+ await pendingOpen ;
477+
478+ expect ( testState . draftStore . setLogicalProjectDraftThreadId ) . toHaveBeenCalledWith (
479+ testState . repositoryIdentity . canonicalKey ,
480+ { environmentId : "environment-primary" , projectId : "project-local" } ,
481+ "draft-delayed" ,
482+ expect . objectContaining ( { envMode : "local" } ) ,
483+ ) ;
484+ } ) ;
485+
439486 it ( "keeps the requested worktree when that environment is connected" , async ( ) => {
440487 testState . reset ( null ) ;
441488 const featureWorktree = {
@@ -453,7 +500,7 @@ describe("useNewThreadHandler with a disconnected remote", () => {
453500 await pendingOpen ;
454501
455502 expect ( testState . draftStore . setLogicalProjectDraftThreadId ) . toHaveBeenCalledWith (
456- "remote-project" ,
503+ expectedLogicalProjectKey ( featureWorktree ) ,
457504 { environmentId : "environment-primary" , projectId : "project-worktree" } ,
458505 "draft-delayed" ,
459506 expect . objectContaining ( { envMode : "local" } ) ,
0 commit comments