fix(ui): open saved plans against their owning project

Saved Project knowledge plans opened as an empty editor whenever the
viewer could not resolve the owning project from the current directory:
managed chats (openchamber:chats is not a registered project), worktrees
outside the repo path, and plan tabs restored after a reload. Titles
still rendered because the list reads the manifest through the correct
owner.

- Thread the owner explicitly (savedProjectPlan = { projectRef, planId })
  from the panel, mobile surfaces, and persisted context tabs; PlanView
  no longer guesses the project.
- An unrecognized directory resolves to no owner instead of borrowing
  the active project's knowledge.
- Serialize plan writes per document (planSaveQueue) so close/switch
  within the autosave debounce no longer drops the last edits, saves
  cannot land out of order, and a recovered save clears the error banner.
- Send saved-plan contents inline in Improve/Implement prompts (they
  have no file path); disable those actions for managed-chat plans,
  which have no project directory to create a session in.
- Drop persisted plan tabs that carry an id without an owner rather than
  reopening them against a guessed project.
This commit is contained in:
Bohdan Triapitsyn
2026-08-27 20:18:12 +03:00
parent 03f4b5e3e0
commit 43c4cc625f
15 changed files with 826 additions and 125 deletions
@@ -943,7 +943,12 @@ export const ContextPanel: React.FC = () => {
: activeTab?.mode === 'notes'
? <ProjectContextPanel />
: activeTab?.mode === 'plan'
? <React.Suspense fallback={null}><PlanView targetPath={activeTab.targetPath} projectPlanId={activeTab.projectPlanId} /></React.Suspense>
? <React.Suspense fallback={null}><PlanView
targetPath={activeTab.targetPath}
savedProjectPlan={activeTab.projectPlanId && activeTab.projectPlanRef
? { projectRef: activeTab.projectPlanRef, planId: activeTab.projectPlanId }
: null}
/></React.Suspense>
: null;
const browserTabs = React.useMemo(
@@ -6,43 +6,53 @@ import { useProjectsStore } from '@/stores/useProjectsStore';
import { useDirectoryStore } from '@/stores/useDirectoryStore';
import { formatDirectoryName } from '@/lib/utils';
import { useSessionUIStore } from '@/sync/session-ui-store';
import { CHAT_DRAFT_PROJECT_ID } from '@/lib/chatDirectories';
import { useI18n } from '@/lib/i18n';
import { useProjectContextOwner } from '@/hooks/useProjectContextOwner';
import { CHAT_DRAFT_PROJECT_ID } from '@/lib/chatDirectories';
import type { ProjectRef } from '@/lib/projectContextApi';
import { useI18n } from '@/lib/i18n';
export const ProjectContextPanel: React.FC<{
onActionComplete?: () => void;
onOpenPlan?: (plan: { id: string; title: string }) => void;
onOpenPlan?: (plan: { id: string; title: string; projectRef: ProjectRef }) => void;
}> = ({ onActionComplete, onOpenPlan }) => {
const projects = useProjectsStore((state) => state.projects);
const homeDirectory = useDirectoryStore((state) => state.homeDirectory);
const { t } = useI18n();
const gitDirectories = useGitStore((state) => state.directories);
const chatSessionDirectory = useSessionUIStore((state) => state.currentSessionDirectory);
const projectRef = useProjectContextOwner(chatSessionDirectory);
const isChatContext = projectRef?.id === CHAT_DRAFT_PROJECT_ID;
const activeProject = React.useMemo(() => {
if (isChatContext) return null;
return projects.find((project) => project.id === projectRef?.id) ?? null;
}, [isChatContext, projectRef?.id, projects]);
// One owner decision shared with the panel, agent memory, and PlanView:
// chats resolve to the Chats owner, worktrees to their project, and an
// unrecognized directory owns nothing (null) rather than borrowing
// whichever project happens to be active.
const projectRef = useProjectContextOwner(chatSessionDirectory);
// Display-only lookup: a user-renamed project label wins over the directory
// name. The owner decision stays with the hook — this must not reintroduce
// a fallback.
const projects = useProjectsStore((state) => state.projects);
const labeledProject = React.useMemo(
() => (projectRef ? projects.find((project) => project.id === projectRef.id) ?? null : null),
[projectRef, projects],
);
const projectLabel = React.useMemo(() => {
if (isChatContext) return t('sessions.sidebar.activity.chatsTitle');
if (!activeProject) {
if (!projectRef) {
return null;
}
return activeProject.label?.trim()
|| formatDirectoryName(activeProject.path, homeDirectory)
|| activeProject.path;
}, [activeProject, homeDirectory, isChatContext, t]);
if (projectRef.id === CHAT_DRAFT_PROJECT_ID) {
return t('sessions.sidebar.activity.chatsTitle');
}
return labeledProject?.label?.trim()
|| formatDirectoryName(projectRef.path, homeDirectory)
|| projectRef.path;
}, [homeDirectory, labeledProject, projectRef, t]);
const canCreateWorktree = React.useMemo(() => {
if (!activeProject) {
if (!projectRef || projectRef.id === CHAT_DRAFT_PROJECT_ID) {
return false;
}
return gitDirectories.get(activeProject.path)?.isGitRepo === true;
}, [activeProject, gitDirectories]);
return gitDirectories.get(projectRef.path)?.isGitRepo === true;
}, [gitDirectories, projectRef]);
return (
/* The panel scrolls its own tab content; a scroller here would nest. */