@kabmo-labs/kabmo 0.27.0 → 0.28.0
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.
- package/dist/client/index.html +1 -1
- package/dist/client/index.js +495 -467
- package/dist/client/static/js/index-81f7berm.css +1 -0
- package/dist/client/static/js/{index-e3qh2yps.js → index-xzvyrxya.js} +795 -795
- package/marketplace/plugins/command-runner/package.json +3 -17
- package/marketplace/plugins/command-runner/settings-schema.json +14 -0
- package/marketplace/plugins/git-forge/README.md +17 -7
- package/marketplace/plugins/git-forge/package.json +4 -30
- package/marketplace/plugins/git-forge/panel.css +16 -0
- package/marketplace/plugins/git-forge/settings-schema.json +27 -0
- package/marketplace/plugins/git-forge/src/panel/Badge.tsx +35 -22
- package/marketplace/plugins/git-forge/src/panel/Change.tsx +20 -24
- package/marketplace/plugins/git-forge/src/panel/CiBadge.tsx +16 -7
- package/marketplace/plugins/git-forge/src/panel/Credential.tsx +31 -26
- package/marketplace/plugins/git-forge/src/panel/Glyph.tsx +18 -2
- package/marketplace/plugins/git-forge/src/panel/Header.tsx +160 -96
- package/marketplace/plugins/git-forge/src/panel/ItemView.tsx +60 -44
- package/marketplace/plugins/git-forge/src/panel/List.tsx +150 -22
- package/marketplace/plugins/git-forge/src/panel/Row.tsx +11 -5
- package/marketplace/plugins/git-forge/src/panel/Sidebar.tsx +17 -15
- package/marketplace/plugins/git-forge/src/panel/Write.tsx +84 -61
- package/marketplace/plugins/git-forge/src/panel/format.test.ts +99 -0
- package/marketplace/plugins/git-forge/src/panel/format.ts +59 -16
- package/marketplace/plugins/git-forge/src/panel/index.test.tsx +109 -5
- package/marketplace/plugins/git-forge/src/panel/index.tsx +37 -4
- package/marketplace/plugins/git-forge/src/panel/use-forge.ts +17 -0
- package/marketplace/plugins/git-forge/src/panel/write.test.tsx +31 -0
- package/marketplace/plugins/session-info/package.json +2 -10
- package/marketplace/plugins/session-info/settings-schema.json +8 -0
- package/marketplace/plugins/stats/README.md +17 -5
- package/marketplace/plugins/stats/package.json +3 -23
- package/marketplace/plugins/stats/settings-schema.json +20 -0
- package/marketplace/plugins/stats/src/app.tsx +1 -1
- package/marketplace/plugins/stats/src/groups.test.ts +46 -1
- package/marketplace/plugins/stats/src/groups.ts +22 -11
- package/marketplace/plugins/stats/src/harness-leases.test.ts +84 -0
- package/marketplace/plugins/stats/src/harness-leases.ts +132 -0
- package/marketplace/plugins/stats/src/labels.test.ts +54 -1
- package/marketplace/plugins/stats/src/labels.ts +33 -7
- package/marketplace/plugins/stats/src/sampler.test.ts +61 -0
- package/marketplace/plugins/stats/src/sampler.ts +39 -5
- package/marketplace/plugins/stats/src/snapshot.ts +6 -0
- package/package.json +2 -2
- package/src/client/components/common/harness-glyph/index.tsx +18 -1
- package/src/client/components/common/session-actions-menu/index.test.tsx +106 -0
- package/src/client/components/common/session-actions-menu/index.tsx +43 -1
- package/src/client/components/common/session-row/TrailingSlot.tsx +9 -4
- package/src/client/components/common/session-row/index.tsx +4 -0
- package/src/client/components/common/session-row/session-row.test.tsx +6 -0
- package/src/client/components/layout/session-sidebar/CategoryItem.tsx +2 -1
- package/src/client/components/layout/session-sidebar/SessionItem.tsx +9 -1
- package/src/client/components/layout/session-sidebar/modern/WorkspaceSection.tsx +2 -1
- package/src/client/components/layout/session-sidebar/scheduler-modal/ModelFields.tsx +202 -0
- package/src/client/components/layout/session-sidebar/scheduler-modal/TaskForm.tsx +80 -19
- package/src/client/components/layout/session-sidebar/scheduler-modal/TaskList.tsx +23 -3
- package/src/client/components/layout/session-sidebar/scheduler-modal/index.tsx +3 -0
- package/src/client/components/mobile/mobile-right-sidebar/EditorSurface.tsx +156 -0
- package/src/client/components/mobile/mobile-right-sidebar/FullEditor.tsx +41 -86
- package/src/client/components/mobile/mobile-session-sidebar/Item.tsx +2 -1
- package/src/client/components/mobile/mobile-session-sidebar/SessionRow.test.ts +6 -0
- package/src/client/components/mobile/mobile-session-sidebar/SessionRow.tsx +9 -1
- package/src/client/components/settings/Modal.tsx +6 -2
- package/src/client/components/settings/Sidebar.tsx +30 -3
- package/src/client/components/settings/categories/HarnessSettings.tsx +11 -7
- package/src/client/components/settings/categories/NotificationSettings.test.tsx +40 -15
- package/src/client/components/settings/categories/NotificationSettings.tsx +142 -64
- package/src/client/components/settings/categories/PanelPluginSettings.tsx +1 -1
- package/src/client/components/settings/categories/harness-settings/CustomHarnessForm.tsx +186 -0
- package/src/client/components/settings/categories/panel-plugins/InstalledList.tsx +11 -0
- package/src/client/components/settings/categories/panel-plugins/PluginCard.test.tsx +1 -0
- package/src/client/components/settings/categories/panel-plugins/PluginCard.tsx +15 -1
- package/src/client/components/settings/categories/panel-plugins/SettingsModal.test.tsx +165 -0
- package/src/client/components/settings/categories/panel-plugins/SettingsModal.tsx +260 -0
- package/src/client/components/workspace/chat-timeline/AiMessageFooter.fork.test.ts +24 -0
- package/src/client/components/workspace/chat-timeline/AiMessageFooter.tsx +8 -4
- package/src/client/components/workspace/chat-timeline/ComposerDock.tsx +34 -0
- package/src/client/components/workspace/chat-timeline/EmptyWorkspacePrompt.tsx +3 -2
- package/src/client/components/workspace/chat-timeline/MessageItem.fork.test.ts +80 -0
- package/src/client/components/workspace/chat-timeline/MessageItem.tsx +9 -3
- package/src/client/components/workspace/chat-timeline/MessageList.tsx +3 -1
- package/src/client/components/workspace/chat-timeline/RunFooter.tsx +2 -1
- package/src/client/components/workspace/chat-timeline/TimelineBody.tsx +2 -1
- package/src/client/components/workspace/chat-timeline/chat-input/ComposerRows.tsx +8 -0
- package/src/client/components/workspace/chat-timeline/chat-input/SessionModeDropdown.test.tsx +9 -0
- package/src/client/components/workspace/chat-timeline/chat-input/SessionModeDropdown.tsx +4 -1
- package/src/client/components/workspace/chat-timeline/chat-input/Toolbar.tsx +24 -0
- package/src/client/components/workspace/chat-timeline/chat-input/WaitingDependencyPicker.tsx +13 -20
- package/src/client/components/workspace/chat-timeline/chat-input/index.tsx +2 -0
- package/src/client/components/workspace/chat-timeline/chat-input/props.ts +5 -5
- package/src/client/components/workspace/chat-timeline/index.tsx +11 -5
- package/src/client/components/workspace/chat-timeline/target-picker/DraftTargetRow.tsx +1 -9
- package/src/client/components/workspace/chat-timeline/target-picker/ParentSessionPicker.test.tsx +101 -0
- package/src/client/components/workspace/chat-timeline/target-picker/ParentSessionPicker.tsx +2 -2
- package/src/client/components/workspace/editor/FilePane.tsx +9 -4
- package/src/client/components/workspace/editor/Notices.tsx +2 -2
- package/src/client/components/workspace/editor/Pane.tsx +2 -2
- package/src/client/components/workspace/editor/Toolbar.tsx +16 -10
- package/src/client/components/workspace/editor/index.tsx +6 -10
- package/src/client/components/workspace/editor/toolbar-clusters.test.ts +4 -3
- package/src/client/components/workspace/editor/toolbar-clusters.tsx +23 -14
- package/src/client/components/workspace/terminal-panel/RealtimeTerminalView.tsx +9 -1
- package/src/client/components/workspace/terminal-panel/focus.test.ts +70 -0
- package/src/client/components/workspace/terminal-panel/focus.ts +46 -0
- package/src/client/components/workspace/terminal-panel/index.tsx +14 -1
- package/src/client/components/workspace/terminal-panel/mount.ts +17 -9
- package/src/client/components/workspace/terminal-panel/scroll-hold.test.ts +158 -0
- package/src/client/components/workspace/terminal-panel/scroll-hold.ts +136 -0
- package/src/client/hooks/chat/composer/use-draft.ts +45 -1
- package/src/client/hooks/chat/omp/pending-sessions.waiting.test.ts +11 -10
- package/src/client/hooks/chat/omp/pending-waiting.ts +15 -7
- package/src/client/hooks/chat/omp/prompt-send.test.ts +11 -0
- package/src/client/hooks/chat/omp/prompt-send.ts +3 -0
- package/src/client/hooks/chat/timeline/actions.ts +4 -4
- package/src/client/hooks/chat/timeline/composer-modes.ts +13 -2
- package/src/client/hooks/chat/timeline/fork-open.ts +52 -0
- package/src/client/hooks/chat/timeline/index.ts +5 -2
- package/src/client/hooks/chat/timeline/mode-availability.ts +7 -0
- package/src/client/hooks/chat/timeline/modes-contract.ts +6 -0
- package/src/client/hooks/chat/timeline/modes-hydrate.ts +93 -0
- package/src/client/hooks/chat/timeline/modes.ts +32 -52
- package/src/client/hooks/chat/timeline/new-chat-open.ts +7 -6
- package/src/client/hooks/chat/timeline/new-chat.ts +10 -1
- package/src/client/hooks/chat/timeline/pending-dispatch.ts +4 -0
- package/src/client/hooks/chat/timeline/pending-session-mode.test.ts +136 -0
- package/src/client/hooks/chat/timeline/pending-session-mode.ts +69 -0
- package/src/client/hooks/chat/timeline/pending-warmup.test.ts +147 -0
- package/src/client/hooks/chat/timeline/pending-warmup.ts +62 -0
- package/src/client/hooks/chat/timeline/run-identity.test.ts +12 -0
- package/src/client/hooks/chat/timeline/run-identity.ts +4 -1
- package/src/client/hooks/chat/timeline/send-props.ts +2 -0
- package/src/client/hooks/chat/timeline/send-steer.test.ts +1 -0
- package/src/client/hooks/chat/timeline/send.test-util.ts +1 -0
- package/src/client/hooks/chat/timeline/send.test.ts +1 -0
- package/src/client/hooks/chat/timeline/session-harness.ts +4 -0
- package/src/client/hooks/chat/timeline/use-fork-session.ts +9 -22
- package/src/client/hooks/chat/waiting/cancel.ts +13 -6
- package/src/client/hooks/chat/waiting/choice.ts +13 -16
- package/src/client/hooks/chat/waiting/composer-wait.ts +70 -0
- package/src/client/hooks/chat/waiting/dispatch.ts +48 -28
- package/src/client/hooks/chat/waiting/held.ts +37 -13
- package/src/client/hooks/chat/waiting/hold.test.ts +22 -0
- package/src/client/hooks/chat/waiting/hold.ts +20 -11
- package/src/client/hooks/chat/waiting/restore.test.ts +69 -0
- package/src/client/hooks/chat/waiting/restore.ts +34 -0
- package/src/client/hooks/chat/waiting/store.ts +9 -3
- package/src/client/hooks/editor/file-editor-types.ts +21 -12
- package/src/client/hooks/editor/save-file.test.ts +1 -1
- package/src/client/hooks/editor/save-file.ts +35 -8
- package/src/client/hooks/editor/target-key.ts +17 -0
- package/src/client/hooks/editor/use-disk-digest.test.ts +82 -0
- package/src/client/hooks/editor/use-disk-digest.ts +164 -0
- package/src/client/hooks/editor/use-disk-sync.ts +212 -0
- package/src/client/hooks/editor/use-file-cache.ts +113 -0
- package/src/client/hooks/editor/use-file-editor.test.ts +90 -0
- package/src/client/hooks/editor/use-file-editor.ts +90 -52
- package/src/client/hooks/editor/use-file-read.ts +24 -10
- package/src/client/hooks/editor/use-file-writes.ts +4 -4
- package/src/client/hooks/models/harness-catalog.ts +15 -0
- package/src/client/hooks/ui/notifications.ts +17 -8
- package/src/client/hooks/workspace/panel-registry.ts +1 -1
- package/src/client/hooks/workspace/scheduled-tasks.ts +26 -11
- package/src/client/lib/notifications/os.test.ts +1 -30
- package/src/client/lib/notifications/os.ts +0 -15
- package/src/client/lib/notifications/platform.test.ts +68 -0
- package/src/client/lib/notifications/platform.ts +36 -0
- package/src/server/index.ts +7 -0
- package/src/server/lib/harness/acp/client.ts +81 -87
- package/src/server/lib/harness/acp/prewarm.server.ts +237 -0
- package/src/server/lib/harness/acp/prewarm.test.ts +171 -0
- package/src/server/lib/harness/acp/prompt.test.ts +70 -1
- package/src/server/lib/harness/acp/prompt.ts +32 -1
- package/src/server/lib/harness/acp/protocol.ts +23 -0
- package/src/server/lib/harness/acp/session-handshake.test.ts +26 -0
- package/src/server/lib/harness/acp/session-handshake.ts +22 -20
- package/src/server/lib/harness/acp/session-options.ts +16 -0
- package/src/server/lib/harness/acp/session-process.ts +43 -0
- package/src/server/lib/harness/acp/session.ts +11 -12
- package/src/server/lib/harness/acp/stdio.ts +129 -0
- package/src/server/lib/harness/acp/verbs.test.ts +97 -0
- package/src/server/lib/harness/acp/verbs.ts +16 -6
- package/src/server/lib/harness/auto-probe.server.ts +4 -3
- package/src/server/lib/harness/capabilities.ts +8 -0
- package/src/server/lib/harness/config.server.ts +3 -2
- package/src/server/lib/harness/custom.server.ts +209 -0
- package/src/server/lib/harness/launch.ts +6 -1
- package/src/server/lib/harness/register.server.ts +8 -1
- package/src/server/lib/harness/schema.server.ts +21 -0
- package/src/server/lib/harness/session-lease.server.test.ts +99 -0
- package/src/server/lib/harness/session-lease.server.ts +193 -0
- package/src/server/lib/harness/session-store.server.test.ts +8 -8
- package/src/server/lib/harness/session-store.server.ts +25 -4
- package/src/server/lib/harness/session-title.server.ts +12 -2
- package/src/server/lib/harness/spawn.server.ts +123 -20
- package/src/server/lib/harness/update-queue.server.ts +3 -3
- package/src/server/lib/lifecycle/after-listen.ts +12 -1
- package/src/server/lib/lifecycle/paths.ts +19 -1
- package/src/server/lib/mcp/registry.server.ts +155 -0
- package/src/server/lib/mcp/registry.test.ts +169 -0
- package/src/server/lib/mcp/store.server.ts +38 -0
- package/src/server/lib/omp/rpc/frame-fold-contract.ts +59 -0
- package/src/server/lib/omp/rpc/frame-fold-frames.test.ts +64 -1
- package/src/server/lib/omp/rpc/frame-fold-host.test-util.ts +2 -1
- package/src/server/lib/omp/rpc/frame-fold.test.ts +2 -1
- package/src/server/lib/omp/rpc/frame-fold.ts +6 -45
- package/src/server/lib/omp/rpc/prewarm-pool.ts +19 -5
- package/src/server/lib/omp/rpc/run-model.test.ts +116 -0
- package/src/server/lib/omp/rpc/run-model.ts +4 -2
- package/src/server/lib/omp/session/sidebar-data.harness.test.ts +6 -3
- package/src/server/lib/omp/session/standby.server.ts +13 -2
- package/src/server/lib/panels/manifest.test.ts +47 -1
- package/src/server/lib/panels/manifest.ts +19 -0
- package/src/server/lib/panels/registry.server.ts +19 -2
- package/src/server/lib/panels/scan.server.ts +26 -2
- package/src/server/lib/panels/settings-schema.server.ts +72 -0
- package/src/server/lib/plugins/background.server.ts +21 -3
- package/src/server/lib/plugins/background.test.ts +13 -0
- package/src/server/lib/plugins/loader.server.ts +35 -51
- package/src/server/lib/plugins/mcp.server.ts +100 -0
- package/src/server/lib/plugins/mcp.test.ts +75 -0
- package/src/server/lib/plugins/schedule-runner.server.ts +153 -0
- package/src/server/lib/plugins/schedule-runner.test.ts +186 -0
- package/src/server/lib/plugins/schedule-store.server.ts +107 -0
- package/src/server/lib/plugins/schema.server.ts +8 -0
- package/src/server/lib/plugins/settings.server.ts +56 -19
- package/src/server/lib/plugins/shutdown.server.ts +58 -0
- package/src/server/lib/plugins/shutdown.test.ts +28 -3
- package/src/server/lib/realtime/topics/global.server.ts +5 -1
- package/src/server/lib/schedule/rows.server.ts +9 -0
- package/src/server/lib/schedule/runtime.server.test.ts +91 -0
- package/src/server/lib/schedule/runtime.server.ts +101 -23
- package/src/server/lib/schedule/schema.server.ts +15 -0
- package/src/server/lib/schedule/store.server.test.ts +34 -0
- package/src/server/lib/schedule/store.server.ts +33 -9
- package/src/server/lib/sessions/fork.server.test.ts +47 -0
- package/src/server/lib/sessions/fork.server.ts +17 -1
- package/src/server/lib/sessions/spawn.server.ts +2 -2
- package/src/server/lib/waiting/runtime.server.test.ts +48 -0
- package/src/server/lib/waiting/runtime.server.ts +22 -0
- package/src/server/lib/waiting/schema.server.ts +8 -0
- package/src/server/lib/waiting/store.server.test.ts +14 -0
- package/src/server/lib/waiting/store.server.ts +6 -2
- package/src/server/routes/agent/command-resume.test.ts +9 -4
- package/src/server/routes/agent/new-mode.test.ts +40 -0
- package/src/server/routes/agent/new.ts +31 -10
- package/src/server/routes/agent/prewarm.ts +48 -7
- package/src/server/routes/fs/action.ts +9 -1
- package/src/server/routes/fs/digest.test.ts +100 -0
- package/src/server/routes/fs/digest.ts +106 -0
- package/src/server/routes/fs/index.ts +2 -0
- package/src/server/routes/fs/read.ts +6 -1
- package/src/server/routes/harness/custom.test.ts +131 -0
- package/src/server/routes/harness/import-action.ts +2 -2
- package/src/server/routes/harness/root.ts +41 -8
- package/src/server/routes/harness/update.ts +2 -2
- package/src/server/routes/panels/bindings.ts +2 -0
- package/src/server/routes/panels/settings.test.ts +165 -0
- package/src/server/routes/panels/settings.ts +106 -0
- package/src/server/routes/schedule/body.ts +14 -0
- package/src/server/routes/schedule/index.ts +28 -4
- package/src/server/routes/settings/mcp.ts +9 -26
- package/src/server/routes/waiting/index.ts +21 -2
- package/src/server/routes/waiting/routes.test.ts +21 -0
- package/src/shared/lib/chat/composer/delegation.ts +3 -3
- package/src/shared/lib/chat/timeline/fork-anchor.test.ts +97 -0
- package/src/shared/lib/chat/timeline/fork-anchor.ts +47 -0
- package/src/shared/lib/fs/content-hash.ts +29 -0
- package/src/shared/lib/harness/catalog.ts +48 -6
- package/src/shared/lib/harness/fork.ts +60 -0
- package/src/shared/lib/harness/mcp-facts.test.ts +30 -0
- package/src/shared/lib/harness/mcp-facts.ts +65 -0
- package/src/shared/lib/harness/menu.test.ts +16 -1
- package/src/shared/lib/harness/menu.ts +2 -2
- package/src/shared/lib/harness/rewind.ts +6 -6
- package/src/shared/lib/harness/session-modes.ts +31 -0
- package/src/shared/lib/harness/transcript.test.ts +29 -0
- package/src/shared/lib/harness/transcript.ts +19 -0
- package/src/shared/lib/notifications/settings.test.ts +21 -0
- package/src/shared/lib/notifications/settings.ts +24 -0
- package/src/shared/lib/omp/session/prewarm.test.ts +28 -4
- package/src/shared/lib/omp/session/prewarm.ts +25 -6
- package/src/shared/lib/omp/session/stream-state.server.test.ts +25 -0
- package/src/shared/lib/omp/session/stream-state.server.ts +8 -4
- package/src/shared/lib/panels/capabilities.test.ts +1 -1
- package/src/shared/lib/panels/capabilities.ts +3 -0
- package/src/shared/lib/plugins/settings.ts +22 -0
- package/src/shared/lib/schedule/parse.test.ts +48 -0
- package/src/shared/lib/schedule/parse.ts +69 -45
- package/src/shared/lib/schedule/timezone.ts +128 -0
- package/src/shared/lib/settings/defaults.ts +2 -0
- package/src/shared/lib/workspace/refresh-cadence.test.ts +25 -1
- package/src/shared/lib/workspace/relative-time.ts +16 -0
- package/src/shared/types/harness.ts +7 -0
- package/src/shared/types/omp/agent.ts +6 -0
- package/src/shared/types/panels-registry.ts +3 -3
- package/src/shared/types/panels.ts +4 -2
- package/src/shared/types/schedule.ts +13 -0
- package/src/shared/types/settings/state.ts +12 -1
- package/src/shared/types/waiting.ts +9 -0
- package/src/test-support/file-editor.test-util.ts +70 -5
- package/dist/client/static/js/index-32rpkj6b.css +0 -1
- package/src/client/hooks/editor/use-external-change-notice.ts +0 -71
- package/src/client/hooks/editor/use-external-change.test.ts +0 -88
- package/src/client/hooks/editor/use-external-change.ts +0 -146
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The editor's disk SYNCHRONISATION: the watcher, the two answers it produces,
|
|
8
|
+
* and the manual Reload.
|
|
9
|
+
*
|
|
10
|
+
* Split from `useFileEditor` because it is a self-contained cluster — one poll,
|
|
11
|
+
* one verdict, one counter the panel renders — and it is the piece with a rule
|
|
12
|
+
* worth naming:
|
|
13
|
+
*
|
|
14
|
+
* - **A CLEAN buffer follows the disk.** Nothing of the user's is at stake, so a
|
|
15
|
+
* file changed by an agent, a `git checkout` or another editor is re-read
|
|
16
|
+
* without asking. The bytes stay on screen until the response lands.
|
|
17
|
+
* - **A DIRTY buffer never does.** Its edits are the user's, and only the user
|
|
18
|
+
* can say which side wins, so the hook reports a revision instead and the
|
|
19
|
+
* panel asks (the external-change strip).
|
|
20
|
+
* - **A file that still hashes the same is not a change.** Its mtime may have
|
|
21
|
+
* moved (`touch`, a no-op formatter, a checkout of the revision already open);
|
|
22
|
+
* that only means the save path's conflict base must move with it, or the next
|
|
23
|
+
* save would be refused over a change that never happened.
|
|
24
|
+
*
|
|
25
|
+
* Reload is the manual counterpart: it re-reads even a clean buffer (the answer
|
|
26
|
+
* to "read it again"), and it discards a dirty one — including the debounced
|
|
27
|
+
* write sitting behind those edits, which would otherwise land after the read
|
|
28
|
+
* and put the discarded text back on disk.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import { useCallback, useEffect, useRef, useState } from 'preact/hooks';
|
|
32
|
+
import type { FileEditorTarget } from '@/client/hooks/editor/file-editor-types';
|
|
33
|
+
import type { PendingWrite } from '@/client/hooks/editor/use-file-writes';
|
|
34
|
+
import { useDiskDigest, type DiskDigest } from '@/client/hooks/editor/use-disk-digest';
|
|
35
|
+
import { editorTargetKey } from '@/client/hooks/editor/target-key';
|
|
36
|
+
import { isProbeableTab } from '@/client/hooks/workspace/missing-files';
|
|
37
|
+
|
|
38
|
+
/** A mutable ref, typed structurally — every ref here is read without a null check. */
|
|
39
|
+
interface LiveRef<T> {
|
|
40
|
+
current: T;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface DiskSyncDeps {
|
|
44
|
+
/** The active buffer's key, or `''` when nothing is open. */
|
|
45
|
+
key: string;
|
|
46
|
+
target: FileEditorTarget | null;
|
|
47
|
+
/** The hash the ACTIVE buffer was read at (or the one its last save wrote). */
|
|
48
|
+
baseHash: string | null;
|
|
49
|
+
/** Bumped by `reset()`/a save; also what makes the watcher ask immediately. */
|
|
50
|
+
revision: number;
|
|
51
|
+
hashesRef: LiveRef<Record<string, string | null>>;
|
|
52
|
+
mtimesRef: LiveRef<Record<string, number | null>>;
|
|
53
|
+
contentsRef: LiveRef<Record<string, string>>;
|
|
54
|
+
baselinesRef: LiveRef<Record<string, string>>;
|
|
55
|
+
pendingRef: LiveRef<PendingWrite | null>;
|
|
56
|
+
/** Reads on their way — a verdict must not queue a second one. */
|
|
57
|
+
readingRef: LiveRef<number>;
|
|
58
|
+
loadedRef: LiveRef<Set<string>>;
|
|
59
|
+
/** Forces the read effect to re-run for the active file. */
|
|
60
|
+
bumpRevision: () => void;
|
|
61
|
+
/** Take the disk's mtime as this buffer's base, writing nothing. */
|
|
62
|
+
adoptDiskMtime: (diskMtimeMs: number) => void;
|
|
63
|
+
/** Drop one buffer's conflict entry without resolving anything. */
|
|
64
|
+
clearConflict: (fileKey: string) => void;
|
|
65
|
+
/** Discard the active buffer and re-read it. */
|
|
66
|
+
reloadDiscardingEdits: () => void;
|
|
67
|
+
/** Drop the debounced write for a key. */
|
|
68
|
+
discardPendingWrite: (fileKey: string) => void;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface DiskSync {
|
|
72
|
+
/**
|
|
73
|
+
* The disk bytes differ from a buffer that HAS edits, or null.
|
|
74
|
+
*
|
|
75
|
+
* Nothing was replaced: the panel renders this as the external-change strip, so
|
|
76
|
+
* the user chooses between the two copies. Cleared by itself as soon as the
|
|
77
|
+
* buffer and the disk agree again (a reload, a save, a "Keep mine" write).
|
|
78
|
+
*/
|
|
79
|
+
diskChange: DiskDigest | null;
|
|
80
|
+
reloadFromDisk: () => void;
|
|
81
|
+
/**
|
|
82
|
+
* Answer the strip with the user's buffer: adopt the disk's mtime and stop
|
|
83
|
+
* asking about THIS change.
|
|
84
|
+
*
|
|
85
|
+
* Nothing is written — the buffer stays dirty and the autosave owns the write —
|
|
86
|
+
* and the accepted hash is remembered, so the watcher does not raise the same
|
|
87
|
+
* strip again on its next poll. A LATER change is a new hash and asks again.
|
|
88
|
+
*/
|
|
89
|
+
keepMineAgainstDisk: () => void;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function useDiskSync({
|
|
93
|
+
key,
|
|
94
|
+
target,
|
|
95
|
+
baseHash,
|
|
96
|
+
revision,
|
|
97
|
+
hashesRef,
|
|
98
|
+
mtimesRef,
|
|
99
|
+
contentsRef,
|
|
100
|
+
baselinesRef,
|
|
101
|
+
pendingRef,
|
|
102
|
+
readingRef,
|
|
103
|
+
loadedRef,
|
|
104
|
+
bumpRevision,
|
|
105
|
+
adoptDiskMtime,
|
|
106
|
+
clearConflict,
|
|
107
|
+
reloadDiscardingEdits,
|
|
108
|
+
discardPendingWrite,
|
|
109
|
+
}: DiskSyncDeps): DiskSync {
|
|
110
|
+
const [diskChange, setDiskChange] = useState<DiskDigest | null>(null);
|
|
111
|
+
/**
|
|
112
|
+
* The hash the user answered "Keep mine" about, so the same change is not
|
|
113
|
+
* reported again on the next poll.
|
|
114
|
+
*
|
|
115
|
+
* A ref rather than state: the watcher reads it from a callback that must stay
|
|
116
|
+
* stable, and dismissing a strip is not something the UI derives from.
|
|
117
|
+
*/
|
|
118
|
+
const dismissedRef = useRef<string | null>(null);
|
|
119
|
+
|
|
120
|
+
// The verdict describes the tab it was made for; switching tabs must not carry
|
|
121
|
+
// a stale "changed on disk" strip onto another file.
|
|
122
|
+
useEffect(() => {
|
|
123
|
+
setDiskChange(null);
|
|
124
|
+
dismissedRef.current = null;
|
|
125
|
+
}, [key]);
|
|
126
|
+
|
|
127
|
+
const adoptUnchangedMtime = useCallback(
|
|
128
|
+
(diskMtimeMs: number) => {
|
|
129
|
+
if (!key) return;
|
|
130
|
+
// The bytes are the ones this buffer holds; only the clock moved. Adopting
|
|
131
|
+
// the mtime keeps the next save from being refused over nothing, and it is
|
|
132
|
+
// also the answer to a strip the user has already seen: the disk is not
|
|
133
|
+
// ahead of the buffer, so there is nothing left to ask.
|
|
134
|
+
if (mtimesRef.current[key] !== diskMtimeMs) adoptDiskMtime(diskMtimeMs);
|
|
135
|
+
setDiskChange(null);
|
|
136
|
+
// The bytes agree again, so a dismissal of an older change is spent.
|
|
137
|
+
dismissedRef.current = null;
|
|
138
|
+
},
|
|
139
|
+
[key, mtimesRef, adoptDiskMtime],
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
const adoptDiskChange = useCallback(
|
|
143
|
+
(digest: DiskDigest) => {
|
|
144
|
+
if (!key) return;
|
|
145
|
+
// A probe that started before the last read landed compares against a
|
|
146
|
+
// baseline that has since moved; the live hash is the truth, and the bytes
|
|
147
|
+
// it names are already on screen.
|
|
148
|
+
if (hashesRef.current[key] === digest.hash) {
|
|
149
|
+
setDiskChange(null);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (dismissedRef.current === digest.hash) return;
|
|
153
|
+
if (diskChange?.hash === digest.hash) return;
|
|
154
|
+
const typed = contentsRef.current[key];
|
|
155
|
+
const queued = pendingRef.current;
|
|
156
|
+
const dirty =
|
|
157
|
+
(typed !== undefined && typed !== baselinesRef.current[key]) ||
|
|
158
|
+
(queued !== null && editorTargetKey(queued.file) === key);
|
|
159
|
+
if (dirty) {
|
|
160
|
+
setDiskChange(digest);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
// A read already on its way (a workspace refresh, a manual Reload, an
|
|
164
|
+
// earlier verdict) will land these bytes; asking again would be one more
|
|
165
|
+
// request for the same answer.
|
|
166
|
+
if (readingRef.current > 0) return;
|
|
167
|
+
loadedRef.current.delete(key);
|
|
168
|
+
bumpRevision();
|
|
169
|
+
},
|
|
170
|
+
[key, hashesRef, contentsRef, baselinesRef, pendingRef, readingRef, loadedRef, bumpRevision, diskChange],
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
useDiskDigest({
|
|
174
|
+
// Same predicate the tab strip and the phone use for "this tab names a file
|
|
175
|
+
// on disk": a diff tab, an attachment chip and a handle/device file have no
|
|
176
|
+
// server path to ask about. The id only satisfies the predicate's shape.
|
|
177
|
+
path: target && isProbeableTab({ ...target, id: key }) ? target.path ?? null : null,
|
|
178
|
+
root: target?.root ?? null,
|
|
179
|
+
repo: target?.repo ?? null,
|
|
180
|
+
baseHash,
|
|
181
|
+
revision,
|
|
182
|
+
onUnchanged: adoptUnchangedMtime,
|
|
183
|
+
onChanged: adoptDiskChange,
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
const reloadFromDisk = useCallback(() => {
|
|
187
|
+
if (!target) return;
|
|
188
|
+
const fileKey = editorTargetKey(target);
|
|
189
|
+
const typed = contentsRef.current[fileKey];
|
|
190
|
+
const dirty = typed !== undefined && typed !== baselinesRef.current[fileKey];
|
|
191
|
+
if (dirty) {
|
|
192
|
+
discardPendingWrite(fileKey);
|
|
193
|
+
reloadDiscardingEdits();
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
clearConflict(fileKey);
|
|
197
|
+
loadedRef.current.delete(fileKey);
|
|
198
|
+
bumpRevision();
|
|
199
|
+
}, [target, contentsRef, baselinesRef, discardPendingWrite, reloadDiscardingEdits, clearConflict, loadedRef, bumpRevision]);
|
|
200
|
+
|
|
201
|
+
const keepMineAgainstDisk = useCallback(() => {
|
|
202
|
+
if (!diskChange) return;
|
|
203
|
+
// Remember the exact change that was dismissed: the disk still has those
|
|
204
|
+
// bytes until the buffer's own write lands, so the watcher would otherwise
|
|
205
|
+
// raise the strip again three seconds later.
|
|
206
|
+
dismissedRef.current = diskChange.hash;
|
|
207
|
+
adoptDiskMtime(diskChange.mtimeMs);
|
|
208
|
+
setDiskChange(null);
|
|
209
|
+
}, [diskChange, adoptDiskMtime]);
|
|
210
|
+
|
|
211
|
+
return { diskChange, reloadFromDisk, keepMineAgainstDisk };
|
|
212
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The editor's per-file CACHE lifecycle: throw every cached read away, keep only
|
|
8
|
+
* the files still open, and switch the ending a buffer saves in.
|
|
9
|
+
*
|
|
10
|
+
* These three belong together because they all answer "what does the cache hold
|
|
11
|
+
* right now", and they must touch EVERY cache — the bytes, the baselines, the
|
|
12
|
+
* line endings, the mtimes and the content hashes. A `retain` that forgot one of
|
|
13
|
+
* them left an entry a later tab could inherit under the same key; the hash list
|
|
14
|
+
* is part of that set, or a reopened tab would compare the disk against a
|
|
15
|
+
* fingerprint from the file it used to be.
|
|
16
|
+
*
|
|
17
|
+
* `reset` deliberately does NOT clear the bytes: they stay on screen until the
|
|
18
|
+
* fresh read lands, so a workspace refresh can never blank the editor.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { useCallback } from 'preact/hooks';
|
|
22
|
+
import { retainKeys } from '@/client/hooks/editor/save-file';
|
|
23
|
+
import { editorTargetKey } from '@/client/hooks/editor/target-key';
|
|
24
|
+
import type { FileEditorTarget } from '@/client/hooks/editor/file-editor-types';
|
|
25
|
+
import { detectLineEnding, toLf, type LineEnding } from '@/shared/lib/code/line-endings';
|
|
26
|
+
|
|
27
|
+
/** A mutable ref, typed structurally — every ref here is read without a null check. */
|
|
28
|
+
interface LiveRef<T> {
|
|
29
|
+
current: T;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface FileCacheDeps {
|
|
33
|
+
target: FileEditorTarget | null;
|
|
34
|
+
loadedRef: LiveRef<Set<string>>;
|
|
35
|
+
bumpRevision: () => void;
|
|
36
|
+
setContents: (update: (previous: Record<string, string>) => Record<string, string>) => void;
|
|
37
|
+
setBaselines: (update: (previous: Record<string, string>) => Record<string, string>) => void;
|
|
38
|
+
setLineEndings: (update: (previous: Record<string, LineEnding>) => Record<string, LineEnding>) => void;
|
|
39
|
+
setMtimes: (update: (previous: Record<string, number | null>) => Record<string, number | null>) => void;
|
|
40
|
+
setHashes: (update: (previous: Record<string, string | null>) => Record<string, string | null>) => void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface FileCache {
|
|
44
|
+
/**
|
|
45
|
+
* Store one read's bytes in EVERY cache: the LF buffer, its baseline, the
|
|
46
|
+
* ending it arrived with, the mtime it was read at, and the content hash the
|
|
47
|
+
* disk watcher compares against.
|
|
48
|
+
*/
|
|
49
|
+
remember: (fileKey: string, text: string, mtimeMs: number | null, hash: string | null) => void;
|
|
50
|
+
/** Re-read every cached file; a re-render cannot skip a key already marked read. */
|
|
51
|
+
reset: () => void;
|
|
52
|
+
/** Drop every cache entry for a tab that is no longer open. */
|
|
53
|
+
retain: (ids: Array<number | string>) => void;
|
|
54
|
+
/** Change the ending the NEXT save writes. */
|
|
55
|
+
setEol: (eol: LineEnding) => void;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function useFileCache({
|
|
59
|
+
target,
|
|
60
|
+
loadedRef,
|
|
61
|
+
bumpRevision,
|
|
62
|
+
setContents,
|
|
63
|
+
setBaselines,
|
|
64
|
+
setLineEndings,
|
|
65
|
+
setMtimes,
|
|
66
|
+
setHashes,
|
|
67
|
+
}: FileCacheDeps): FileCache {
|
|
68
|
+
const remember = useCallback(
|
|
69
|
+
(fileKey: string, text: string, mtimeMs: number | null, hash: string | null) => {
|
|
70
|
+
// The buffer holds LF only; the style the bytes arrived with is kept beside
|
|
71
|
+
// it so the write can put the file back the way it was found.
|
|
72
|
+
const ending = detectLineEnding(text);
|
|
73
|
+
const lf = toLf(text);
|
|
74
|
+
setContents((previous) => ({ ...previous, [fileKey]: lf }));
|
|
75
|
+
setBaselines((previous) => ({ ...previous, [fileKey]: lf }));
|
|
76
|
+
setLineEndings((previous) => ({ ...previous, [fileKey]: ending }));
|
|
77
|
+
setMtimes((previous) => ({ ...previous, [fileKey]: mtimeMs }));
|
|
78
|
+
setHashes((previous) => ({ ...previous, [fileKey]: hash }));
|
|
79
|
+
},
|
|
80
|
+
[setContents, setBaselines, setLineEndings, setMtimes, setHashes],
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
const reset = useCallback(() => {
|
|
84
|
+
loadedRef.current.clear();
|
|
85
|
+
bumpRevision();
|
|
86
|
+
}, [loadedRef, bumpRevision]);
|
|
87
|
+
|
|
88
|
+
const retain = useCallback(
|
|
89
|
+
(ids: Array<number | string>) => {
|
|
90
|
+
const keep = new Set(ids.map(String));
|
|
91
|
+
for (const loaded of Array.from(loadedRef.current)) {
|
|
92
|
+
if (!keep.has(loaded)) loadedRef.current.delete(loaded);
|
|
93
|
+
}
|
|
94
|
+
setContents((previous) => retainKeys(previous, keep));
|
|
95
|
+
setLineEndings((previous) => retainKeys(previous, keep));
|
|
96
|
+
setMtimes((previous) => retainKeys(previous, keep));
|
|
97
|
+
setHashes((previous) => retainKeys(previous, keep));
|
|
98
|
+
},
|
|
99
|
+
[loadedRef, setContents, setLineEndings, setMtimes, setHashes],
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
// Only the ending moves: the BUFFER is LF-only either way, and the bytes are
|
|
103
|
+
// built at write time, so nothing on screen changes and no re-read is needed.
|
|
104
|
+
const setEol = useCallback(
|
|
105
|
+
(next: LineEnding) => {
|
|
106
|
+
if (!target) return;
|
|
107
|
+
setLineEndings((previous) => ({ ...previous, [editorTargetKey(target)]: next }));
|
|
108
|
+
},
|
|
109
|
+
[target, setLineEndings],
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
return { remember, reset, retain, setEol };
|
|
113
|
+
}
|
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
createEditorHarness,
|
|
20
20
|
freshState,
|
|
21
21
|
stubFetch,
|
|
22
|
+
stubHash,
|
|
22
23
|
} from '@/test-support/file-editor.test-util';
|
|
23
24
|
|
|
24
25
|
const editor = createEditorHarness();
|
|
@@ -118,6 +119,95 @@ describe('useFileEditor revalidation', () => {
|
|
|
118
119
|
});
|
|
119
120
|
});
|
|
120
121
|
|
|
122
|
+
describe('useFileEditor auto-refresh', () => {
|
|
123
|
+
/**
|
|
124
|
+
* Ask the watcher now, the way coming back to the window does.
|
|
125
|
+
*
|
|
126
|
+
* The probe is a promise, so the whole turn runs inside `act` (`runAsync`): a
|
|
127
|
+
* verdict delivered outside it updates state without committing the effects,
|
|
128
|
+
* and the re-read under test is an effect.
|
|
129
|
+
*/
|
|
130
|
+
async function focusWindow() {
|
|
131
|
+
await editor.runAsync(async () => {
|
|
132
|
+
window.dispatchEvent(new Event('focus'));
|
|
133
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
test('a file changed from outside replaces a CLEAN buffer', async () => {
|
|
138
|
+
const state = freshState();
|
|
139
|
+
stubFetch(state);
|
|
140
|
+
await editor.mount();
|
|
141
|
+
expect(editor.mounted?.content).toBe(DISK);
|
|
142
|
+
|
|
143
|
+
// Another process (an agent's tool call, `git checkout`) rewrote the file.
|
|
144
|
+
state.body = `${DISK}const c = 3;\n`;
|
|
145
|
+
await focusWindow();
|
|
146
|
+
|
|
147
|
+
expect(editor.mounted?.content).toBe(state.body);
|
|
148
|
+
expect(editor.mounted?.isDirty).toBe(false);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test('a file rewritten with the SAME bytes does not touch the buffer', async () => {
|
|
152
|
+
const state = freshState();
|
|
153
|
+
stubFetch(state);
|
|
154
|
+
await editor.mount();
|
|
155
|
+
const readsAfterMount = state.reads;
|
|
156
|
+
|
|
157
|
+
// A `touch`, a formatter that changed nothing, a checkout of the revision
|
|
158
|
+
// already open: the mtime moved, the bytes did not.
|
|
159
|
+
await focusWindow();
|
|
160
|
+
|
|
161
|
+
expect(state.reads).toBe(readsAfterMount);
|
|
162
|
+
expect(editor.mounted?.content).toBe(DISK);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test('a DIRTY buffer is never replaced — the panel is told instead', async () => {
|
|
166
|
+
const state = freshState();
|
|
167
|
+
stubFetch(state);
|
|
168
|
+
await editor.mount();
|
|
169
|
+
|
|
170
|
+
editor.mounted?.onChange('my own edit');
|
|
171
|
+
await editor.settle();
|
|
172
|
+
|
|
173
|
+
state.body = `${DISK}const c = 3;\n`;
|
|
174
|
+
await focusWindow();
|
|
175
|
+
|
|
176
|
+
expect(editor.mounted?.content).toBe('my own edit');
|
|
177
|
+
expect(editor.mounted?.isDirty).toBe(true);
|
|
178
|
+
// The panel is told, so the external-change strip can offer the two answers.
|
|
179
|
+
expect(editor.mounted?.diskChange?.hash).toBe(stubHash(state.body));
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test('reloadFromDisk re-reads a clean buffer on demand', async () => {
|
|
183
|
+
const state = freshState();
|
|
184
|
+
stubFetch(state);
|
|
185
|
+
await editor.mount();
|
|
186
|
+
const readsAfterMount = state.reads;
|
|
187
|
+
|
|
188
|
+
state.body = `${DISK}const c = 3;\n`;
|
|
189
|
+
await editor.run(() => editor.mounted?.reloadFromDisk());
|
|
190
|
+
await editor.settle();
|
|
191
|
+
|
|
192
|
+
expect(state.reads).toBe(readsAfterMount + 1);
|
|
193
|
+
expect(editor.mounted?.content).toBe(state.body);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test('reloadFromDisk discards a dirty buffer', async () => {
|
|
197
|
+
const state = freshState();
|
|
198
|
+
stubFetch(state);
|
|
199
|
+
await editor.mount();
|
|
200
|
+
|
|
201
|
+
editor.mounted?.onChange('my own edit');
|
|
202
|
+
await editor.settle();
|
|
203
|
+
await editor.run(() => editor.mounted?.reloadFromDisk());
|
|
204
|
+
await editor.settle();
|
|
205
|
+
|
|
206
|
+
expect(editor.mounted?.content).toBe(DISK);
|
|
207
|
+
expect(editor.mounted?.isDirty).toBe(false);
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
|
|
121
211
|
describe('useFileEditor unsupported files', () => {
|
|
122
212
|
test('a kind refusal reports the server sentence as unsupported', async () => {
|
|
123
213
|
const state = freshState();
|
|
@@ -1,22 +1,20 @@
|
|
|
1
1
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'preact/hooks';
|
|
2
2
|
import { getLanguageFromPath } from '@/shared/lib/code/language';
|
|
3
3
|
import { resolveViewer } from '@/client/hooks/editor/resolve-viewer';
|
|
4
|
-
import {
|
|
5
|
-
import { retainKeys } from '@/client/hooks/editor/save-file';
|
|
4
|
+
import { toDiskText, toLf, type LineEnding } from '@/shared/lib/code/line-endings';
|
|
6
5
|
import { deviceFileFor } from '@/client/lib/launch/device-files';
|
|
7
6
|
import { fileHandleFor } from '@/client/lib/launch/file-handlers';
|
|
8
7
|
import { useFileRead } from '@/client/hooks/editor/use-file-read';
|
|
9
8
|
import { useFileTransfer } from '@/client/hooks/editor/use-file-transfer';
|
|
10
9
|
import { useFileWrites } from '@/client/hooks/editor/use-file-writes';
|
|
11
10
|
import { useWriteConflict } from '@/client/hooks/editor/use-write-conflict';
|
|
11
|
+
import { useDiskSync } from '@/client/hooks/editor/use-disk-sync';
|
|
12
|
+
import { useFileCache } from '@/client/hooks/editor/use-file-cache';
|
|
13
|
+
import { editorTargetKey as targetKey } from '@/client/hooks/editor/target-key';
|
|
12
14
|
import type { FileEditorSaveStatus, FileEditorTarget, UseFileEditorOptions, UseFileEditorResult } from '@/client/hooks/editor/file-editor-types';
|
|
13
15
|
|
|
14
16
|
export type { FileEditorSaveStatus, FileEditorTarget, UseFileEditorOptions, UseFileEditorResult } from '@/client/hooks/editor/file-editor-types';
|
|
15
17
|
|
|
16
|
-
function targetKey(target: FileEditorTarget): string {
|
|
17
|
-
return String(target.id ?? target.path ?? target.name);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
18
|
/**
|
|
21
19
|
* Owns the file-editing contract shared by the desktop editor and the phone's
|
|
22
20
|
* full-screen editor: read, debounced FormData save, copy, download, dirty
|
|
@@ -52,6 +50,16 @@ export function useFileEditor(
|
|
|
52
50
|
* inline attachment, a handle-backed file, or a read that failed.
|
|
53
51
|
*/
|
|
54
52
|
const [mtimes, setMtimes] = useState<Record<string, number | null>>({});
|
|
53
|
+
/**
|
|
54
|
+
* The content hash of the bytes each cached file was read at (or last wrote).
|
|
55
|
+
*
|
|
56
|
+
* The auto-refresh watcher compares the disk against it, so a file rewritten
|
|
57
|
+
* with identical bytes — a checkout of the revision already open, a formatter
|
|
58
|
+
* that changes nothing, a `touch` — never replaces a buffer. `null` means "no
|
|
59
|
+
* fingerprint": an inline attachment, a failed read, a file too large for the
|
|
60
|
+
* digest, or a handle-backed write (nothing to hash against).
|
|
61
|
+
*/
|
|
62
|
+
const [hashes, setHashes] = useState<Record<string, string | null>>({});
|
|
55
63
|
const [saveStatus, setSaveStatus] = useState<FileEditorSaveStatus>('idle');
|
|
56
64
|
/** Bumped by `reset()` to re-run the read effect for the active file. */
|
|
57
65
|
const [revision, setRevision] = useState(0);
|
|
@@ -84,6 +92,8 @@ export function useFileEditor(
|
|
|
84
92
|
// duplicate fetch while the first one is still in flight.
|
|
85
93
|
const loadedRef = useRef<Set<string>>(new Set());
|
|
86
94
|
const pendingRef = useRef<{ timer: number; file: FileEditorTarget; content: string } | null>(null);
|
|
95
|
+
/** Reads on their way — see `useFileRead`. */
|
|
96
|
+
const readingRef = useRef(0);
|
|
87
97
|
|
|
88
98
|
// Live mirrors of the content maps: a read response resolves long after it
|
|
89
99
|
// was requested, and it has to know whether the buffer it read is still the
|
|
@@ -98,17 +108,24 @@ export function useFileEditor(
|
|
|
98
108
|
lineEndingsRef.current = lineEndings;
|
|
99
109
|
const mtimesRef = useRef(mtimes);
|
|
100
110
|
mtimesRef.current = mtimes;
|
|
111
|
+
const hashesRef = useRef(hashes);
|
|
112
|
+
hashesRef.current = hashes;
|
|
101
113
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
114
|
+
/**
|
|
115
|
+
* The cache's lifecycle (refresh everything, drop closed tabs, switch the saved
|
|
116
|
+
* ending) lives in its own hook — see `use-file-cache.ts` for why the five caches
|
|
117
|
+
* must move together.
|
|
118
|
+
*/
|
|
119
|
+
const { remember, reset, retain, setEol } = useFileCache({
|
|
120
|
+
target,
|
|
121
|
+
loadedRef,
|
|
122
|
+
bumpRevision: () => setRevision((prev) => prev + 1),
|
|
123
|
+
setContents,
|
|
124
|
+
setBaselines,
|
|
125
|
+
setLineEndings,
|
|
126
|
+
setMtimes,
|
|
127
|
+
setHashes,
|
|
128
|
+
});
|
|
112
129
|
|
|
113
130
|
/**
|
|
114
131
|
* The read path lives in its own hook — see `use-file-read.ts` for the two
|
|
@@ -123,6 +140,7 @@ export function useFileEditor(
|
|
|
123
140
|
contentsRef,
|
|
124
141
|
baselinesRef,
|
|
125
142
|
pendingRef,
|
|
143
|
+
readingRef,
|
|
126
144
|
remember,
|
|
127
145
|
});
|
|
128
146
|
|
|
@@ -145,9 +163,15 @@ export function useFileEditor(
|
|
|
145
163
|
pendingRef,
|
|
146
164
|
flushOnUnmount,
|
|
147
165
|
onSaveStart: () => setSaveStatus('saving'),
|
|
148
|
-
onSaved: (fileKey, text) => {
|
|
166
|
+
onSaved: (fileKey, text, written) => {
|
|
149
167
|
setSaveStatus('saved');
|
|
150
168
|
setBaselines((prev) => ({ ...prev, [fileKey]: text }));
|
|
169
|
+
// Adopt what the server reported about the bytes it just wrote: the mtime
|
|
170
|
+
// is the next save's conflict base, and the hash is the auto-refresh
|
|
171
|
+
// baseline. Without them the watcher would read the user's own save as an
|
|
172
|
+
// external change, and the next save would be refused against its base.
|
|
173
|
+
if (written.mtimeMs !== null) setMtimes((prev) => ({ ...prev, [fileKey]: written.mtimeMs }));
|
|
174
|
+
if (written.hash !== null) setHashes((prev) => ({ ...prev, [fileKey]: written.hash }));
|
|
151
175
|
onFileSaved?.();
|
|
152
176
|
},
|
|
153
177
|
onSaveFailed: () => setSaveStatus(reportSaveError ? 'error' : 'idle'),
|
|
@@ -186,6 +210,48 @@ export function useFileEditor(
|
|
|
186
210
|
bumpRevision: () => setRevision((prev) => prev + 1),
|
|
187
211
|
});
|
|
188
212
|
|
|
213
|
+
/**
|
|
214
|
+
* Drop the debounced write for `fileKey`, if one is armed.
|
|
215
|
+
*
|
|
216
|
+
* A Reload throws the buffer away, so the write that buffer had queued must go
|
|
217
|
+
* with it — and the read path yields to a queued write, so leaving it armed
|
|
218
|
+
* would also make the reload land nowhere.
|
|
219
|
+
*/
|
|
220
|
+
const discardPendingWrite = useCallback((fileKey: string) => {
|
|
221
|
+
const queued = pendingRef.current;
|
|
222
|
+
if (!queued || targetKey(queued.file) !== fileKey) return;
|
|
223
|
+
window.clearTimeout(queued.timer);
|
|
224
|
+
pendingRef.current = null;
|
|
225
|
+
}, []);
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* The disk watcher and the answers it produces — one cluster, in its own hook
|
|
229
|
+
* (see `use-disk-sync.ts`): a clean buffer follows the disk, a dirty one is
|
|
230
|
+
* never replaced, and a file that still hashes the same is not a change.
|
|
231
|
+
*
|
|
232
|
+
* The conflict machine is passed in rather than imported there so "adopt the
|
|
233
|
+
* mtime" and "discard and re-read" stay the SAME implementations the strips
|
|
234
|
+
* call; a second copy is how one of them refuses a save the other accepted.
|
|
235
|
+
*/
|
|
236
|
+
const { diskChange, reloadFromDisk, keepMineAgainstDisk } = useDiskSync({
|
|
237
|
+
key,
|
|
238
|
+
target,
|
|
239
|
+
baseHash: hashes[key] ?? null,
|
|
240
|
+
revision,
|
|
241
|
+
hashesRef,
|
|
242
|
+
mtimesRef,
|
|
243
|
+
contentsRef,
|
|
244
|
+
baselinesRef,
|
|
245
|
+
pendingRef,
|
|
246
|
+
readingRef,
|
|
247
|
+
loadedRef,
|
|
248
|
+
bumpRevision: () => setRevision((prev) => prev + 1),
|
|
249
|
+
adoptDiskMtime: conflictState.adopt,
|
|
250
|
+
clearConflict: conflictState.clear,
|
|
251
|
+
reloadDiscardingEdits: conflictState.reload,
|
|
252
|
+
discardPendingWrite,
|
|
253
|
+
});
|
|
254
|
+
|
|
189
255
|
useEffect(() => {
|
|
190
256
|
if (saveStatus !== 'saved') return;
|
|
191
257
|
const timer = window.setTimeout(() => setSaveStatus('idle'), 1500);
|
|
@@ -225,35 +291,6 @@ export function useFileEditor(
|
|
|
225
291
|
byteViewer: isBinaryViewer,
|
|
226
292
|
});
|
|
227
293
|
|
|
228
|
-
const reset = useCallback(() => {
|
|
229
|
-
// Invalidate every key, then let the read effect refetch the active one.
|
|
230
|
-
// Content is deliberately NOT cleared: it stays on screen until the fresh
|
|
231
|
-
// bytes land, so a refresh can never blank the editor (or drop the file
|
|
232
|
-
// that is open) while the request is in flight.
|
|
233
|
-
loadedRef.current.clear();
|
|
234
|
-
setRevision((prev) => prev + 1);
|
|
235
|
-
}, []);
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* Switch the ending this buffer saves in.
|
|
239
|
-
*
|
|
240
|
-
* Only the ending moves: the BUFFER is LF-only either way, and the bytes are built
|
|
241
|
-
* at write time, so nothing on screen changes and no re-read is needed.
|
|
242
|
-
*/
|
|
243
|
-
const setEol = useCallback((next: LineEnding) => {
|
|
244
|
-
if (!target) return;
|
|
245
|
-
setLineEndings((prev) => ({ ...prev, [targetKey(target)]: next }));
|
|
246
|
-
}, [target]);
|
|
247
|
-
|
|
248
|
-
const retain = useCallback((ids: Array<number | string>) => {
|
|
249
|
-
const keep = new Set(ids.map(String));
|
|
250
|
-
for (const loaded of Array.from(loadedRef.current)) {
|
|
251
|
-
if (!keep.has(loaded)) loadedRef.current.delete(loaded);
|
|
252
|
-
}
|
|
253
|
-
setContents((prev) => retainKeys(prev, keep));
|
|
254
|
-
setLineEndings((prev) => retainKeys(prev, keep));
|
|
255
|
-
setMtimes((prev) => retainKeys(prev, keep));
|
|
256
|
-
}, []);
|
|
257
294
|
|
|
258
295
|
return {
|
|
259
296
|
content,
|
|
@@ -293,12 +330,13 @@ export function useFileEditor(
|
|
|
293
330
|
confirmSaveMissing: writes.confirmSaveMissing,
|
|
294
331
|
cancelSaveMissing: writes.cancelSaveMissing,
|
|
295
332
|
conflict: conflictState.conflict,
|
|
296
|
-
resolveConflictByReload:
|
|
333
|
+
resolveConflictByReload: () => {
|
|
334
|
+
if (key) discardPendingWrite(key);
|
|
335
|
+
conflictState.reload();
|
|
336
|
+
},
|
|
297
337
|
resolveConflictByKeepingMine: conflictState.keepMine,
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
revertToDisk: conflictState.reload,
|
|
302
|
-
adoptDiskMtime: conflictState.adopt,
|
|
338
|
+
reloadFromDisk,
|
|
339
|
+
diskChange,
|
|
340
|
+
keepMineAgainstDisk,
|
|
303
341
|
};
|
|
304
342
|
}
|