Skip to content

feat(workspace): create a Space from a node's directory - #757

Merged
DeadWaveWave merged 1 commit into
DeadWaveWave:mainfrom
MtWay:feat/space-from-node-directory
Nov 27, 2026
Merged

DeadWaveWave merged 1 commit into
DeadWaveWave:mainfrom
MtWay:feat/space-from-node-directory

Conversation

@MtWay

@MtWay MtWay commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

💡 Change Scope

  • Small Change: Fast feedback, localized UI/logic, low-risk.
  • Large Change: New feature, cross-boundary logic, runtime-risk (persistence, IPC, lifecycle, recovery).

📝 What Does This PR Do?

Adds a selection context-menu action on folder nodes — "Create Space from This Directory" — that creates a top-level Space bound to the folder's directory, resolving the containing project mount. The item is hidden for document nodes and for nodes a Space already owns (creating inside a Space makes a child Space, which the position-sensitive Create Space expresses).

Reuses existing logic instead of parallel copies:

  • mount picking → exported resolveBestMount (domain normalisation handles Windows case and macOS /private), reached through the canvas spaceMountLaunchContext facade
  • mount query → listProjectMounts
  • node path → getFilesystemNodeUri, the menu's existing resolver
  • folder / in-Space eligibility → useFilesystemNodePaths, exposed via FilesystemNodesContext

🏗️ Large Change Spec (Required if "Large Change" is checked)

1. Context & Business Logic

A folder node carries a directory URI. This adds a shortcut to create a top-level Space bound to that directory and its containing mount. It applies to folder nodes only and is hidden once a Space owns the node. If the folder has no usable directory, or the directory falls outside every mount, a warning is shown and nothing is written.

2. State Ownership & Invariants

  • Owner: the Space record in the workspace document, written by the existing createSpace. Mount topology truth stays with Main/Worker and is read only through the side-effect-free mount.list query. No new durable state or persistence schema.
  • Invariants:
    1. Applies only to folder nodes that no Space owns yet.
    2. Writes a Space only when the directory resolves inside an existing project mount; otherwise it warns and writes nothing, never falling back to a local location.
    3. A project switch or unmount while mount.list resolves writes nothing (lifetime.active, like blank-canvas create).

3. Verification Plan & Regression Layer

Hook tests on useWorkspaceCanvasCreateSpace (tests/unit/contexts/useSpaces.createSpaceFromNodeDirectory.spec.tsx): mount hit writes mountId + directory; outside every mount → warning + no write; unmount during the query → no write; in-Space node → no write; document node → no write. No new E2E — the invariant does not need a real Desktop boundary (DEVELOPMENT.md). docs/canvas/SPACE_LIFECYCLE_SPEC.md updated in the same change.


🔗 Co-change partners left untouched

  • src/contexts/workspace/presentation/renderer/components/workspaceCanvas/types.ts (with src/contexts/workspace/presentation/renderer/components/workspaceCanvas/WorkspaceCanvasView.types.ts, 9/14): The new createSpaceFromNodeDirectory prop is a callback on the view-props interface only; it reuses existing types (nodeId: string) and introduces no new shared domain/node-data type, so types.ts needs no change (tsc passes without it).

✅ Delivery & Compliance Checklist

  • The local quick gate passes: pnpm verify:quick / pnpm pre-commit is green; any additional focused tests are reported above.
  • Full CI passes on the final revision before merge; local quick checks are not reported as full regression.
  • I have signed the CLA if required (see CLA.md).
  • I have locked down the behavior with the strongest asset that fits, in this order: type or structure, mechanical check, runtime assertion, scenario test (or explicitly stated why none applies).
  • I have strictly adhered to the DEVELOPMENT.md architectural boundaries.
  • I have attached a screenshot or screen recording (if this touches the UI).
  • I have updated the documentation accordingly (if adding a feature or changing a contract).

📸 Screenshots / Visual Evidence

7f39ffc5-0dd3-42f6-a2e6-827629aae941 fa29f0c8-2e74-4b0f-9300-3b9dd263fda2

@CLAassistant

CLAassistant commented Nov 24, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@MtWay
MtWay force-pushed the feat/space-from-node-directory branch from 119ec59 to 0619fcc Compare November 24, 2026 10:14

@DeadWaveWave DeadWaveWave left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @MtWay, and welcome. Binding a Space to a folder you've dropped onto the canvas is a good shortcut, and we want it. I approved the CI run, so you'll see results on this revision soon. Before it can merge, these need to change:

Behaviour

  1. Folder nodes only. Right now the item also appears on document nodes and quietly uses the file's parent folder, which "Create Space from This Directory" doesn't say. Please show it only for folder nodes. That is also what your description and E2E cover.
  2. Follow the existing Space rules when the folder is already inside a Space. createSpace takes the node out of its Space and puts a new top-level Space on top of the old parent. docs/canvas/SPACE_LIFECYCLE_SPEC.md says that creating inside a Space makes a child Space, and createSpaceFromSelectedNodes already decides this through resolveSelectedChildSpaceParent. Either go through that same decision and only override the directory (for a child Space, the directory must be inside the parent's scope; otherwise warn and write nothing), or, simpler, hide the item for nodes that already belong to a Space. Write whichever you pick into the spec.
  3. Nothing written after the canvas has moved on. After mount.list resolves, check the canvas lifetime the way useCreateEmptySpace does (lifetime.active). That way a project switch or unmount during the query writes nothing. The spec already promises this for blank-canvas create, and the new path should keep that promise.

Reuse, no parallel copies (DEVELOPMENT.md, "优先复用")

  1. Mount picking: resolveBestMount in src/contexts/space/application/resolveSpaceMountContext.ts already picks the mount with the longest root that contains the path. It uses the domain normalisation in spaceBoundaryPolicy.ts, which also handles macOS /private aliases and Windows case. Export it (or a small named wrapper) and drop pickMountForDirectory. Then the normalizeComparablePath(…, platform) change in the view helper can be reverted.
  2. Mount query: use listProjectMounts in hooks/spaceMountLaunchContext.ts and drop workspaceMountApi.ts.
  3. Node path: this menu already gets the path from getNodePathUri (useFilesystemNodePaths) to decide hasPath. Use the same resolver instead of re-reading data.folder / data.document.

Tests

  1. These invariants can be shown in a hook test on useWorkspaceCanvasCreateSpace, in the style of the existing createSpace tests: mount found → the Space is written with mountId and the directory; outside every mount → a warning and no write; switch during the query → no write; node inside a Space → whatever you chose in point 2. DEVELOPMENT.md adds a new E2E only when the invariant needs a real Desktop boundary, so I'd drop it. If you keep it, run pnpm e2e:budget:update --reason "Desktop: …": without the budget entry, CI fails. Also, the description mentions "Windows ConPTY E2E coverage", but the spec opens no terminal; please correct that.

PR description

  1. This is a Large Change. Please tick it and fill in the spec: the state owner (the Space record in the workspace document, written by createSpace), the 1–3 invariants (your new invariant 7 is a good start), and the verification. Add the ## 🔗 Co-change partners left untouched section from pnpm impact --base origin/main --pr-section. The pr-template check fails without it.

Happy to look again as soon as you push.

@DeadWaveWave

Copy link
Copy Markdown
Owner

CI finished on this revision. Only the two failures from my review: E2E case budget (point 7) and pr-template (point 8). Everything else passed, including the Ubuntu E2E shards, Windows and macOS. CI runs on your next push need my approval until your first PR is merged; I'll approve them when I see them.

@MtWay
MtWay force-pushed the feat/space-from-node-directory branch from 0619fcc to 3a2b57d Compare November 27, 2026 02:27
@MtWay

MtWay commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review — all eight points are addressed in the latest push.

Behaviour

  1. Folder nodes only. The item now renders only for folder nodes; document nodes no longer show it and no longer fall back to the file's parent folder. Both the menu and the handler guard on folder kind.
  2. Node already inside a Space. Took the simpler option you suggested: the item is hidden for any node a Space already owns, so the top-level shortcut never competes with the child-Space path. Written into SPACE_LIFECYCLE_SPEC.md (creation entry + invariant 7).
  3. Lifetime. After mount.list resolves, the handler checks lifetime.active exactly like useCreateEmptySpace; a project switch or unmount during the query writes nothing.

Reuse, no parallel copies

  1. Mount picking uses the exported resolveBestMount (domain normalisation in spaceBoundaryPolicy — Windows case + macOS /private). pickMountForDirectory is gone, and the normalizeComparablePath(…, platform) view-helper change is reverted.
  2. Mount query uses listProjectMounts; workspaceMountApi.ts is deleted.
  3. Node path uses getFilesystemNodeUri, the menu's existing resolver. Folder / in-Space eligibility lives in useFilesystemNodePaths and reaches the menu through FilesystemNodesContext, so the menu does not read the store directly.

To keep the architecture ratchet green without suppressions: the folder-kind test is an exit guard, and resolveBestMount reaches the canvas through the spaceMountLaunchContext facade (no new cross-context deep import).

Tests

  1. Dropped the E2E and the pure-function unit spec. Added hook tests on useWorkspaceCanvasCreateSpace (tests/unit/contexts/useSpaces.createSpaceFromNodeDirectory.spec.tsx): mount hit writes mountId + directory; outside every mount → warning + no write; unmount during the query → no write; in-Space node → no write; document node → no write. No new E2E, since the invariant does not need a real Desktop boundary. Corrected the "Windows ConPTY E2E" wording in the description.

PR description

  1. Ticked Large Change and filled the spec: state owner (the Space record in the workspace document, written by createSpace), three invariants, and the verification layer. Added the ## 🔗 Co-change partners left untouched section from pnpm impact --base origin/main --pr-section.

Verification: tsc, oxlint, arch:check (0 errors), line/naming, prettier and the affected unit suites are green locally. The full local suite has pre-existing, environment-dependent failures (CLI / PTY / platform / script tests) that also fail on a clean origin/main checkout — unrelated to this change.

Happy to switch point 2 to routing through resolveSelectedChildSpaceParent (child Space with the directory overridden) instead of hiding, if you'd prefer that behaviour.

@MtWay
MtWay force-pushed the feat/space-from-node-directory branch from 3a2b57d to 55af659 Compare November 27, 2026 03:10

@DeadWaveWave DeadWaveWave left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @MtWay, this is a thorough revision. I checked all eight points against the diff and they hold:

  • The item is limited to folder nodes and hidden once a Space owns the node, in both the menu and the handler.
  • The handler checks lifetime.active after mount.list.
  • It uses resolveBestMount, listProjectMounts and getFilesystemNodeUri, and the parallel copies are gone.
  • The E2E is replaced by hook tests.
  • The description has the spec and the co-change section.

pr-template is green now. I approved the CI run on 55af659 and it is still running.

The drive-root fix in normalizeComparablePath is a good catch. Before it, isPathInsideOrEqual('D:\\', 'D:\\work') was false for every caller, not only this one.

One thing is left: the gap while mount.list is pending.

1. Re-check the node after the await. createSpaceFromNodeDirectory does three things before await listProjectMounts(...): it reads the node, decides whether the node is eligible, and resolves directoryPath. It hands those results to createSpace afterwards. createSpace reads the document again, but it does not enforce invariant 1: it takes the node out of any Space that owns it and makes a new top-level Space. So a change that happens during the wait gets overwritten. I added two hook tests to your spec locally and both fail on 55af659:

  • Clicking the item twice before mount.list answers. The menu stays open until createSpace closes it, so a second click is possible. Both calls pass the check before the await. The second createSpace then takes the folder out of the Space the first call just made. Store result: [[], ['folder-1']], which is an empty Space plus a second Space holding the folder.
  • The folder is moved into an existing Space while mount.list is pending. The test does this with nodeStore.updateCanvasDocument. Result: that Space is emptied and a new top-level Space takes the folder.

A folder that navigates during the wait has the same problem: updateFolder changes folder.uri, so the Space would bind the old directory.

Fix: after the lifetime.active check, read the document again and run the same decision on the current node: it is a folder, no Space owns it, and its directory comes from getFilesystemNodeUri. If any of that no longer holds, write nothing.

To keep that decision in one place, extract it as a pure function over { nodes, spaces } and nodeId that returns the directory path or null. Call it from three places: isTopLevelSpaceDirectoryCandidate for the menu, before the await for the early "no directory" warning, and after the await. Right now the kind and owner checks are written twice, in useFilesystemNodePaths.ts and in the handler.

2. Close the context menu on click, before the query. copyPath and blank-canvas create (createSpaceFromContextMenu) already do this. It removes the second click from the UI, but the re-check in point 1 is still what keeps the invariant.

3. Tests. Add both cases above to useSpaces.createSpaceFromNodeDirectory.spec.tsx:

  • (a) Two calls before mount.list resolves leave exactly one Space, and it owns the folder.
  • (b) When the folder is moved into a Space while the query is pending, that Space still owns it and no new Space is written.

One trap: the spec mocks t as key => key, which ignores count. As soon as a test creates a second Space, resolveDefaultSpaceName loops forever, because every candidate name is the same string. Case (a) hangs until the mock interpolates the count, e.g. (key, options) => options?.count === undefined ? key : \${key}-${options.count}`. For (b), have useSpacesUnderTestalso returnnodeStore` so the test can write the move.

Nothing else is needed. Once these land and CI is green I'll merge. If you'd rather I make this change myself, say so and I'll push it to your branch.

@DeadWaveWave

Copy link
Copy Markdown
Owner

CI finished green on 55af659: every job passed, including the Ubuntu E2E shards, Windows and macOS. The three points in my review are all that's left. I'll approve the CI run on your next push once I've read the diff.

Add a context-menu action on filesystem nodes that creates a top-level Space
bound to the right-clicked node's directory, resolving the matching project
mount via the mount.list query.

- spaceDirectoryFromNode: resolve a node's directory and pick its mount
- workspaceMountApi: boundary for the canvas mount topology query
- wire createSpaceFromNodeDirectory through useSpaces, canvas view and menus
- normalizeComparablePath: optional platform param so it runs in node tests
- en/zh i18n, unit spec and Windows ConPTY E2E coverage

Co-Authored-By: Claude Code <noreply@anthropic.com>
@MtWay
MtWay force-pushed the feat/space-from-node-directory branch from 55af659 to d746d95 Compare November 27, 2026 09:13
@MtWay

MtWay commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Thanks — the pending-mount.list gap is closed in d746d95.

1. Re-check after the await, one decision function. Extracted resolveTopLevelSpaceDirectoryFromNode(document, nodeId) in useFilesystemNodePaths.ts: a pure function over { nodes, spaces } + nodeId that returns the folder's directory path or null (not a folder, a Space already owns it, or no usable directory). It is now the single source of the kind/owner/directory decision, called from three places:

  • isTopLevelSpaceDirectoryCandidate (menu eligibility),
  • before the await (the early "no directory" warning),
  • after the await and the lifetime.active check, on the freshly read document — if it returns null now, nothing is written.

The duplicated kind/owner checks in the handler are gone. createSpace is handed the re-checked directory, so a second click, a move into a Space, or a folder navigation during the wait can no longer overwrite the newer state.

2. Menu closes on click. createSpaceFromNodeDirectory calls ui.closeContextMenu() / ui.cancelSpaceRename() before the query, like copyPath and blank-canvas create. The post-query re-check still holds the invariant independently of the UI.

3. Tests. Added both cases to useSpaces.createSpaceFromNodeDirectory.spec.tsx:

  • (a) two calls before mount.list resolves leave exactly one Space, and it owns the folder;
  • (b) moving the folder into a Space while the query is pending leaves that Space owning it, with no new Space written.

Fixed the t mock to interpolate count ((key, options) => options?.count === undefined ? key : \${key}-${options.count}`) so resolveDefaultSpaceNameterminates, anduseSpacesUnderTestnow also returnsnodeStore`.

SPACE_LIFECYCLE_SPEC.md invariant 7 documents the shared decision function, the post-query re-check and the close-on-click. Local checks green: tsc, oxlint, arch:check (0 errors), line/naming, prettier, and the createSpace / menu / mount unit suites.

@DeadWaveWave DeadWaveWave left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The three points are addressed in d746d95. Checked: the kind/owner/directory decision is one pure function (resolveTopLevelSpaceDirectoryFromNode) used by the menu, before the query and again on the current document after it; the menu closes on click; with the post-query re-check removed locally, both new hook tests fail. CI is green on this revision.

@DeadWaveWave
DeadWaveWave merged commit 67412e7 into DeadWaveWave:main Nov 27, 2026
13 checks passed
@DeadWaveWave

Copy link
Copy Markdown
Owner

Merged as 67412e7, thanks @MtWay, and welcome. Checked: the item shows only for folder nodes that no Space owns; the Space binds the mount from resolveBestMount and writes nothing outside every mount; a project switch, a second click, or a move into a Space while mount.list is pending writes nothing (hook tests, verified failing without the re-check); the D:\\ drive-root fix makes drive-root mounts match for every caller of normalizeComparablePath; CLA signed, full CI green. One small follow-up you can ignore: the handler now warns "no usable directory" for an in-Space or document node, which the menu never offers; a more precise message would only matter for a click that races a move.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants