@deepseek-ai/dsh-experimental-client-ui-forge 0.2.2-alpha.3 → 0.2.2-rc.2
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/README.i18n.yaml +8 -8
- package/README.md +41 -14
- package/README.zh.md +40 -13
- package/lib/client.js +13 -11800
- package/lib/client.js.map +1 -0
- package/lib/index.js +1340 -115
- package/lib/typert.host.js +1715 -212
- package/lib/typert.remote-client.d.ts +42 -5
- package/lib/typert.remote-client.d.ts.map +1 -0
- package/lib/typert.remote-client.js +1523 -244
- package/lib/types/client/ActionToast.d.ts.map +1 -0
- package/lib/types/client/ActionToast.js.map +1 -0
- package/lib/types/client/BaseMenu.d.ts +30 -0
- package/lib/types/client/BaseMenu.d.ts.map +1 -0
- package/lib/types/client/BaseMenu.js.map +1 -0
- package/lib/types/client/ChangeRequestCard.d.ts +9 -5
- package/lib/types/client/ChangeRequestCard.d.ts.map +1 -0
- package/lib/types/client/ChangeRequestCard.js.map +1 -0
- package/lib/types/client/ChangeRequestOverview.d.ts +124 -0
- package/lib/types/client/ChangeRequestOverview.d.ts.map +1 -0
- package/lib/types/client/ChangeRequestOverview.js.map +1 -0
- package/lib/types/client/ChangedFiles.d.ts +2 -0
- package/lib/types/client/ChangedFiles.d.ts.map +1 -0
- package/lib/types/client/ChangedFiles.js.map +1 -0
- package/lib/types/client/ChangesTab.d.ts +30 -5
- package/lib/types/client/ChangesTab.d.ts.map +1 -0
- package/lib/types/client/ChangesTab.js.map +1 -0
- package/lib/types/client/ChangesTabTitle.d.ts.map +1 -0
- package/lib/types/client/ChangesTabTitle.js.map +1 -0
- package/lib/types/client/CommitMenu.d.ts +41 -0
- package/lib/types/client/CommitMenu.d.ts.map +1 -0
- package/lib/types/client/CommitMenu.js.map +1 -0
- package/lib/types/client/DiffFile.d.ts +47 -15
- package/lib/types/client/DiffFile.d.ts.map +1 -0
- package/lib/types/client/DiffFile.js.map +1 -0
- package/lib/types/client/FileLabel.d.ts.map +1 -0
- package/lib/types/client/FileLabel.js.map +1 -0
- package/lib/types/client/FileTreePane.d.ts +4 -2
- package/lib/types/client/FileTreePane.d.ts.map +1 -0
- package/lib/types/client/FileTreePane.js.map +1 -0
- package/lib/types/client/ForgeTab.d.ts +9 -1
- package/lib/types/client/ForgeTab.d.ts.map +1 -0
- package/lib/types/client/ForgeTab.js.map +1 -0
- package/lib/types/client/ForgeTabTitle.d.ts.map +1 -0
- package/lib/types/client/ForgeTabTitle.js.map +1 -0
- package/lib/types/client/HeaderButton.d.ts.map +1 -0
- package/lib/types/client/HeaderButton.js.map +1 -0
- package/lib/types/client/LocalChangesCard.d.ts.map +1 -0
- package/lib/types/client/LocalChangesCard.js.map +1 -0
- package/lib/types/client/RefreshGlyph.d.ts.map +1 -0
- package/lib/types/client/RefreshGlyph.js.map +1 -0
- package/lib/types/client/ReviewComposer.d.ts +20 -6
- package/lib/types/client/ReviewComposer.d.ts.map +1 -0
- package/lib/types/client/ReviewComposer.js.map +1 -0
- package/lib/types/client/ReviewControls.d.ts +79 -0
- package/lib/types/client/ReviewControls.d.ts.map +1 -0
- package/lib/types/client/ReviewControls.js.map +1 -0
- package/lib/types/client/ReviewThread.d.ts +4 -4
- package/lib/types/client/ReviewThread.d.ts.map +1 -0
- package/lib/types/client/ReviewThread.js.map +1 -0
- package/lib/types/client/ReviewThreads.d.ts +108 -0
- package/lib/types/client/ReviewThreads.d.ts.map +1 -0
- package/lib/types/client/ReviewThreads.js.map +1 -0
- package/lib/types/client/SessionHoverForge.d.ts.map +1 -0
- package/lib/types/client/SessionHoverForge.js.map +1 -0
- package/lib/types/client/SessionRowForge.d.ts +1 -1
- package/lib/types/client/SessionRowForge.d.ts.map +1 -0
- package/lib/types/client/SessionRowForge.js.map +1 -0
- package/lib/types/client/SubmitReview.d.ts +12 -4
- package/lib/types/client/SubmitReview.d.ts.map +1 -0
- package/lib/types/client/SubmitReview.js.map +1 -0
- package/lib/types/client/ThreadList.d.ts +43 -0
- package/lib/types/client/ThreadList.d.ts.map +1 -0
- package/lib/types/client/ThreadList.js.map +1 -0
- package/lib/types/client/ViewOptionsMenu.d.ts +31 -0
- package/lib/types/client/ViewOptionsMenu.d.ts.map +1 -0
- package/lib/types/client/ViewOptionsMenu.js.map +1 -0
- package/lib/types/client/WorktreeProgress.d.ts.map +1 -0
- package/lib/types/client/WorktreeProgress.js.map +1 -0
- package/lib/types/client/WorktreeSettings.d.ts +12 -0
- package/lib/types/client/WorktreeSettings.d.ts.map +1 -0
- package/lib/types/client/WorktreeSettings.js.map +1 -0
- package/lib/types/client/WorktreeToggle.d.ts.map +1 -0
- package/lib/types/client/WorktreeToggle.js.map +1 -0
- package/lib/types/client/changes-address.d.ts.map +1 -0
- package/lib/types/client/changes-address.js.map +1 -0
- package/lib/types/client/changes-store.d.ts +20 -5
- package/lib/types/client/changes-store.d.ts.map +1 -0
- package/lib/types/client/changes-store.js.map +1 -0
- package/lib/types/client/checkout.d.ts.map +1 -0
- package/lib/types/client/checkout.js.map +1 -0
- package/lib/types/client/code-selection.d.ts.map +1 -0
- package/lib/types/client/code-selection.js.map +1 -0
- package/lib/types/client/diff-rows.d.ts.map +1 -0
- package/lib/types/client/diff-rows.js.map +1 -0
- package/lib/types/client/file-tree.d.ts.map +1 -0
- package/lib/types/client/file-tree.js.map +1 -0
- package/lib/types/client/format.d.ts.map +1 -0
- package/lib/types/client/format.js.map +1 -0
- package/lib/types/client/glyphs.d.ts +4 -2
- package/lib/types/client/glyphs.d.ts.map +1 -0
- package/lib/types/client/glyphs.js.map +1 -0
- package/lib/types/client/host-viewed.d.ts +84 -0
- package/lib/types/client/host-viewed.d.ts.map +1 -0
- package/lib/types/client/host-viewed.js.map +1 -0
- package/lib/types/client/index.d.ts.map +1 -0
- package/lib/types/client/index.js.map +1 -0
- package/lib/types/client/locales.d.ts +175 -3
- package/lib/types/client/locales.d.ts.map +1 -0
- package/lib/types/client/locales.js.map +1 -0
- package/lib/types/client/menu-search.d.ts +10 -0
- package/lib/types/client/menu-search.d.ts.map +1 -0
- package/lib/types/client/menu-search.js.map +1 -0
- package/lib/types/client/mount.d.ts +4 -0
- package/lib/types/client/mount.d.ts.map +1 -0
- package/lib/types/client/mount.js.map +1 -0
- package/lib/types/client/review-queue.d.ts +5 -8
- package/lib/types/client/review-queue.d.ts.map +1 -0
- package/lib/types/client/review-queue.js.map +1 -0
- package/lib/types/client/review-threads.d.ts +108 -0
- package/lib/types/client/review-threads.d.ts.map +1 -0
- package/lib/types/client/review-threads.js.map +1 -0
- package/lib/types/client/review.d.ts +31 -4
- package/lib/types/client/review.d.ts.map +1 -0
- package/lib/types/client/review.js.map +1 -0
- package/lib/types/client/section-reach.d.ts +26 -0
- package/lib/types/client/section-reach.d.ts.map +1 -0
- package/lib/types/client/section-reach.js.map +1 -0
- package/lib/types/client/session-list.d.ts.map +1 -0
- package/lib/types/client/session-list.js.map +1 -0
- package/lib/types/client/source.d.ts.map +1 -0
- package/lib/types/client/source.js.map +1 -0
- package/lib/types/client/status.d.ts.map +1 -0
- package/lib/types/client/status.js.map +1 -0
- package/lib/types/client/thread-state.d.ts +68 -0
- package/lib/types/client/thread-state.d.ts.map +1 -0
- package/lib/types/client/thread-state.js.map +1 -0
- package/lib/types/client/toolbar-fold.d.ts +19 -0
- package/lib/types/client/toolbar-fold.d.ts.map +1 -0
- package/lib/types/client/toolbar-fold.js.map +1 -0
- package/lib/types/client/viewed-files.d.ts +70 -0
- package/lib/types/client/viewed-files.d.ts.map +1 -0
- package/lib/types/client/viewed-files.js.map +1 -0
- package/lib/types/client/worktree-settings-form.d.ts +32 -0
- package/lib/types/client/worktree-settings-form.d.ts.map +1 -0
- package/lib/types/client/worktree-settings-form.js.map +1 -0
- package/lib/types/commit-range.d.ts +17 -0
- package/lib/types/commit-range.d.ts.map +1 -0
- package/lib/types/commit-range.js.map +1 -0
- package/lib/types/diff.d.ts +10 -2
- package/lib/types/diff.d.ts.map +1 -0
- package/lib/types/diff.js.map +1 -0
- package/lib/types/git.d.ts.map +1 -0
- package/lib/types/git.js.map +1 -0
- package/lib/types/index.d.ts +194 -14
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/layers.d.ts +66 -0
- package/lib/types/layers.d.ts.map +1 -0
- package/lib/types/layers.js.map +1 -0
- package/lib/types/local-diff.d.ts +54 -6
- package/lib/types/local-diff.d.ts.map +1 -0
- package/lib/types/local-diff.js.map +1 -0
- package/lib/types/panel.d.ts.map +1 -0
- package/lib/types/panel.js.map +1 -0
- package/lib/types/parse.d.ts +16 -0
- package/lib/types/parse.d.ts.map +1 -0
- package/lib/types/parse.js.map +1 -0
- package/lib/types/patch-cache.d.ts +12 -7
- package/lib/types/patch-cache.d.ts.map +1 -0
- package/lib/types/patch-cache.js.map +1 -0
- package/lib/types/review-cache.d.ts +54 -0
- package/lib/types/review-cache.d.ts.map +1 -0
- package/lib/types/review-cache.js.map +1 -0
- package/lib/types/states.d.ts.map +1 -0
- package/lib/types/states.js.map +1 -0
- package/lib/types/types.d.ts +113 -11
- package/lib/types/types.d.ts.map +1 -0
- package/lib/types/types.js.map +1 -0
- package/lib/types/wildmatch.d.ts +14 -0
- package/lib/types/wildmatch.d.ts.map +1 -0
- package/lib/types/wildmatch.js.map +1 -0
- package/lib/types/worktree.d.ts +8 -6
- package/lib/types/worktree.d.ts.map +1 -0
- package/lib/types/worktree.js.map +1 -0
- package/package.json +59 -49
- package/src/client/ActionToast.tsx +38 -0
- package/src/client/BaseMenu.module.css +153 -0
- package/src/client/BaseMenu.tsx +180 -0
- package/src/client/ChangeRequestCard.tsx +439 -0
- package/src/client/ChangeRequestOverview.module.css +196 -0
- package/src/client/ChangeRequestOverview.tsx +620 -0
- package/src/client/ChangedFiles.module.css +26 -0
- package/src/client/ChangedFiles.tsx +144 -0
- package/src/client/ChangesTab.module.css +984 -0
- package/src/client/ChangesTab.tsx +1288 -0
- package/src/client/ChangesTabTitle.tsx +28 -0
- package/src/client/CommitMenu.module.css +139 -0
- package/src/client/CommitMenu.tsx +186 -0
- package/src/client/DiffFile.tsx +621 -0
- package/src/client/FileLabel.module.css +53 -0
- package/src/client/FileLabel.tsx +66 -0
- package/src/client/FileTreePane.module.css +10 -0
- package/src/client/FileTreePane.tsx +79 -0
- package/src/client/ForgeTab.module.css +627 -0
- package/src/client/ForgeTab.tsx +263 -0
- package/src/client/ForgeTabTitle.tsx +21 -0
- package/src/client/HeaderButton.module.css +29 -0
- package/src/client/HeaderButton.tsx +68 -0
- package/src/client/LocalChangesCard.tsx +83 -0
- package/src/client/RefreshGlyph.module.css +22 -0
- package/src/client/RefreshGlyph.tsx +35 -0
- package/src/client/ReviewComposer.module.css +37 -0
- package/src/client/ReviewComposer.tsx +128 -0
- package/src/client/ReviewControls.module.css +162 -0
- package/src/client/ReviewControls.tsx +191 -0
- package/src/client/ReviewThread.tsx +96 -0
- package/src/client/ReviewThreads.module.css +326 -0
- package/src/client/ReviewThreads.tsx +443 -0
- package/src/client/SessionHoverForge.tsx +72 -0
- package/src/client/SessionList.module.css +85 -0
- package/src/client/SessionRowForge.tsx +86 -0
- package/src/client/SubmitReview.module.css +26 -0
- package/src/client/SubmitReview.tsx +95 -0
- package/src/client/ThreadList.module.css +218 -0
- package/src/client/ThreadList.tsx +165 -0
- package/src/client/ViewOptionsMenu.tsx +79 -0
- package/src/client/WorktreeProgress.module.css +226 -0
- package/src/client/WorktreeProgress.tsx +280 -0
- package/src/client/WorktreeSettings.module.css +29 -0
- package/src/client/WorktreeSettings.tsx +42 -0
- package/src/client/WorktreeToggle.module.css +132 -0
- package/src/client/WorktreeToggle.tsx +165 -0
- package/src/client/changes-address.ts +110 -0
- package/src/client/changes-store.ts +155 -0
- package/src/client/checkout.ts +75 -0
- package/src/client/code-selection.ts +53 -0
- package/src/client/diff-rows.ts +199 -0
- package/src/client/file-tree.ts +86 -0
- package/src/client/format.ts +37 -0
- package/src/client/glyphs.tsx +151 -0
- package/src/client/host-viewed.ts +195 -0
- package/src/client/index.ts +31 -0
- package/src/client/locales.ts +3446 -0
- package/src/client/menu-search.ts +16 -0
- package/src/client/mount.ts +439 -0
- package/src/client/review-queue.ts +256 -0
- package/src/client/review-threads.ts +204 -0
- package/src/client/review.ts +120 -0
- package/src/client/section-reach.ts +61 -0
- package/src/client/session-list.ts +79 -0
- package/src/client/source.ts +274 -0
- package/src/client/status.ts +171 -0
- package/src/client/thread-state.ts +127 -0
- package/src/client/toolbar-fold.ts +70 -0
- package/src/client/viewed-files.ts +178 -0
- package/src/client/worktree-settings-form.ts +43 -0
- package/src/commit-range.ts +27 -0
- package/src/css-modules.d.ts +5 -0
- package/src/diff.ts +162 -0
- package/src/git.ts +293 -0
- package/src/index.ts +1162 -0
- package/src/layers.ts +163 -0
- package/src/local-diff.ts +438 -0
- package/src/panel.ts +422 -0
- package/src/parse.ts +137 -0
- package/src/patch-cache.ts +73 -0
- package/src/review-cache.ts +98 -0
- package/src/states.ts +350 -0
- package/src/types.ts +504 -0
- package/src/wildmatch.ts +121 -0
- package/src/worktree.ts +576 -0
package/src/panel.ts
ADDED
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
/** Live panel state of one followed Session, shared by all of its followers. */
|
|
2
|
+
|
|
3
|
+
import { isDeepStrictEqual } from 'node:util'
|
|
4
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
5
|
+
import type { ChangeRequestSnapshot, ForgeReadiness, ForgeRepository } from '@deepseek-ai/dsh-experimental-forge'
|
|
6
|
+
import type { SessionChangeRequest } from '@deepseek-ai/dsh-experimental-forge-session'
|
|
7
|
+
import type {} from '@deepseek-ai/dsh-experimental-worktree'
|
|
8
|
+
import type {} from '@deepseek-ai/dsh-sandbox-policy'
|
|
9
|
+
import { pluginRecordOf } from '@deepseek-ai/dsh-session'
|
|
10
|
+
import type { Session, SessionEvent } from '@deepseek-ai/dsh-session'
|
|
11
|
+
import type { ProjectionHandle } from '@deepseek-ai/dsh-session-projection'
|
|
12
|
+
import { deadline } from '@deepseek-ai/dsh-timeout'
|
|
13
|
+
import type {} from '@deepseek-ai/dsh-working-directory'
|
|
14
|
+
import { gitIn, hasBranch, inspectCheckout } from './git.ts'
|
|
15
|
+
import type { GitConfig, Inspection } from './git.ts'
|
|
16
|
+
import { localChanges } from './local-diff.ts'
|
|
17
|
+
import type { DiffConfig } from './local-diff.ts'
|
|
18
|
+
import { remoteOrder, upstreamBranch } from './parse.ts'
|
|
19
|
+
import type {
|
|
20
|
+
ForgeCheckoutState, ForgeLocalChanges, ForgeMissingWorktree, ForgePanelChangeRequest, ForgePanelState, ForgePanelWorktree,
|
|
21
|
+
ForgeWorktreeBlocker, ForgeWorktreeState,
|
|
22
|
+
} from './types.ts'
|
|
23
|
+
import { within, WORKTREE_RECORD } from './worktree.ts'
|
|
24
|
+
import type { WorktreeCreations } from './worktree.ts'
|
|
25
|
+
|
|
26
|
+
/** Git and diff limits plus the sign-in probe deadline. */
|
|
27
|
+
export interface PanelConfig extends DiffConfig {
|
|
28
|
+
/** Deadline in milliseconds for one provider sign-in probe. */
|
|
29
|
+
readonly readinessTimeoutMs: number
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The checkout's upstream branch and the remote URL its change requests are looked up at. */
|
|
33
|
+
interface Upstream {
|
|
34
|
+
/** URL of the upstream's remote that a provider recognizes. */
|
|
35
|
+
readonly remote: string
|
|
36
|
+
/** Branch name on that remote. */
|
|
37
|
+
readonly branch: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Panel state of one Session. Construction starts the first checkout
|
|
42
|
+
* inspection and watches the Session's change requests. Inspections run one at
|
|
43
|
+
* a time; a request that arrives during one runs once more after it, and a
|
|
44
|
+
* result for a directory the Session has since left is dropped. A sign-in
|
|
45
|
+
* probe runs when an inspection recognizes a new code host and on `refresh`;
|
|
46
|
+
* a probe result for a host the panel no longer shows is dropped. Each change
|
|
47
|
+
* request keeps the latest snapshot a poll returned while later polls fail.
|
|
48
|
+
* When an inspection finds an upstream branch on a remote a provider
|
|
49
|
+
* recognizes, the open change requests of that branch are bound as discovered,
|
|
50
|
+
* once per remote and branch until a refresh. The
|
|
51
|
+
* worktree facts follow the `forgeWorktree` projection, the live creation, and the last failed one;
|
|
52
|
+
* each inspection also decides whether the Session's sandbox can write the
|
|
53
|
+
* checkout and its shared Git directory, which a new worktree needs, whether
|
|
54
|
+
* the worktree the Session created and works in is gone, and how many lines
|
|
55
|
+
* the `branch` scope changes; a measurement that fails leaves that null.
|
|
56
|
+
*/
|
|
57
|
+
export class SessionPanel {
|
|
58
|
+
private current: ForgePanelState
|
|
59
|
+
private readonly followers = new Set<() => void>()
|
|
60
|
+
private readonly lifetime = new AbortController()
|
|
61
|
+
private readonly tasks = new Set<Promise<void>>()
|
|
62
|
+
private readonly stopWatching: () => void
|
|
63
|
+
private readonly snapshots = new Map<string, ChangeRequestSnapshot>()
|
|
64
|
+
private blocker: ForgeWorktreeBlocker | null = null
|
|
65
|
+
private missing: ForgeMissingWorktree | null = null
|
|
66
|
+
private inspection: Promise<void> | null = null
|
|
67
|
+
private rerun: Promise<void> | null = null
|
|
68
|
+
/** Remote URL and branch of the upstream that the last branch lookup covered, until a refresh. */
|
|
69
|
+
private discovered: string | null = null
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @param ctx - service context that provides the forge, Session, directory, and process services.
|
|
73
|
+
* @param config - Git limits and the sign-in probe deadline.
|
|
74
|
+
* @param session - Session the panel describes.
|
|
75
|
+
* @param worktreeStates - reads the `forgeWorktree` projection of the Session.
|
|
76
|
+
* @param creations - reads the running creation and restoration, the last failed creation, and what a message of the
|
|
77
|
+
* Session waits for.
|
|
78
|
+
* @param onIdle - called when the last follower leaves.
|
|
79
|
+
*/
|
|
80
|
+
constructor(
|
|
81
|
+
private readonly ctx: Context,
|
|
82
|
+
private readonly config: PanelConfig,
|
|
83
|
+
readonly session: Session,
|
|
84
|
+
private readonly worktreeStates: ProjectionHandle<ForgeWorktreeState>,
|
|
85
|
+
private readonly creations: Pick<WorktreeCreations, 'of' | 'restorationOf' | 'failureOf' | 'waitingOf'>,
|
|
86
|
+
private readonly onIdle: () => void,
|
|
87
|
+
) {
|
|
88
|
+
this.current = {
|
|
89
|
+
directory: ctx.workingDirectory.get(session),
|
|
90
|
+
checkout: { state: 'pending' },
|
|
91
|
+
repository: null,
|
|
92
|
+
readiness: null,
|
|
93
|
+
changeRequests: this.entries(ctx.forgeSession.changeRequests(session)),
|
|
94
|
+
worktree: this.worktree(),
|
|
95
|
+
local: null,
|
|
96
|
+
}
|
|
97
|
+
this.stopWatching = ctx.forgeSession.watch(session, (changeRequests) => {
|
|
98
|
+
this.publish({ ...this.current, changeRequests: this.entries(changeRequests) })
|
|
99
|
+
})
|
|
100
|
+
void this.inspect()
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Yield the current state, then each later state, until `signal` aborts or the panel stops.
|
|
105
|
+
* @param signal - ends this follower.
|
|
106
|
+
* @returns the states in publication order; intermediate states may be skipped.
|
|
107
|
+
*/
|
|
108
|
+
async *follow(signal: AbortSignal): AsyncGenerator<ForgePanelState> {
|
|
109
|
+
let wake: (() => void) | undefined
|
|
110
|
+
const notify = (): void => { wake?.() }
|
|
111
|
+
this.followers.add(notify)
|
|
112
|
+
signal.addEventListener('abort', notify)
|
|
113
|
+
this.lifetime.signal.addEventListener('abort', notify)
|
|
114
|
+
try {
|
|
115
|
+
let delivered: ForgePanelState | undefined
|
|
116
|
+
while (!signal.aborted && !this.lifetime.signal.aborted) {
|
|
117
|
+
if (this.current !== delivered) {
|
|
118
|
+
delivered = this.current
|
|
119
|
+
yield delivered
|
|
120
|
+
continue
|
|
121
|
+
}
|
|
122
|
+
await new Promise<void>((resolve) => { wake = resolve })
|
|
123
|
+
wake = undefined
|
|
124
|
+
}
|
|
125
|
+
} finally {
|
|
126
|
+
this.followers.delete(notify)
|
|
127
|
+
signal.removeEventListener('abort', notify)
|
|
128
|
+
this.lifetime.signal.removeEventListener('abort', notify)
|
|
129
|
+
if (this.followers.size === 0) this.onIdle()
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* React to one committed event of the Session: a directory change moves the
|
|
135
|
+
* panel and reinspects; a tool result reinspects, because the tool may have
|
|
136
|
+
* changed the checkout; a sandbox mode change reinspects, because it decides
|
|
137
|
+
* whether a worktree can be created; the first turn, the first step, and
|
|
138
|
+
* worktree records update the worktree facts.
|
|
139
|
+
* @param event - committed Session event.
|
|
140
|
+
*/
|
|
141
|
+
observe(event: SessionEvent): void {
|
|
142
|
+
if (event.type === 'working-directory/change') {
|
|
143
|
+
this.publish({ ...this.current, directory: event.data.cwd, checkout: { state: 'pending' }, local: null })
|
|
144
|
+
void this.inspect()
|
|
145
|
+
} else if (event.type === 'tool/result' || event.type === 'sandbox/mode') {
|
|
146
|
+
void this.inspect()
|
|
147
|
+
} else if (event.type === 'turn/start' || event.type === 'step/start' || pluginRecordOf(event)?.type === WORKTREE_RECORD) {
|
|
148
|
+
this.updateWorktree()
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Republish the worktree facts when they changed, such as after a creation moved or a worktree service appeared or left. */
|
|
153
|
+
updateWorktree(): void {
|
|
154
|
+
const worktree = this.worktree()
|
|
155
|
+
if (!sameWorktree(worktree, this.current.worktree)) this.publish({ ...this.current, worktree })
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Forget the missing worktree once it was checked out again; the next publication carries it, and a reinspection confirms it. */
|
|
159
|
+
restored(): void {
|
|
160
|
+
this.missing = null
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Reinspect the checkout, such as after its directory was created again. */
|
|
164
|
+
reinspect(): void {
|
|
165
|
+
void this.inspect()
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Check again whether the worktree the Session works in is gone, publishing a change.
|
|
170
|
+
* @param signal - cancellation of the check.
|
|
171
|
+
* @returns the unavailable worktree, or null for a valid checkout or a Session working elsewhere.
|
|
172
|
+
* @throws when the filesystem or Git cannot answer.
|
|
173
|
+
*/
|
|
174
|
+
async checkMissing(signal: AbortSignal): Promise<ForgeMissingWorktree | null> {
|
|
175
|
+
const directory = this.current.directory
|
|
176
|
+
const state = this.worktreeStates.stateOf(this.session)
|
|
177
|
+
const missing = await missingWorktree(
|
|
178
|
+
this.ctx, this.config, this.session, state, directory, AbortSignal.any([signal, this.lifetime.signal]),
|
|
179
|
+
)
|
|
180
|
+
if (this.current.directory === directory) {
|
|
181
|
+
this.missing = missing
|
|
182
|
+
this.updateWorktree()
|
|
183
|
+
}
|
|
184
|
+
return missing
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Reinspect the checkout, probe sign-in again, and poll every bound change request at once.
|
|
189
|
+
* @returns settles once the inspection and the sign-in probe have ended; each poll publishes its result when it finishes.
|
|
190
|
+
*/
|
|
191
|
+
async refresh(): Promise<void> {
|
|
192
|
+
this.discovered = null
|
|
193
|
+
const inspection = this.inspect()
|
|
194
|
+
const probe = this.current.repository === null ? undefined : this.probe(this.current.repository)
|
|
195
|
+
for (const entry of this.current.changeRequests) {
|
|
196
|
+
if (entry.ref !== null) this.ctx.forge.refresh(entry.ref)
|
|
197
|
+
}
|
|
198
|
+
await Promise.all([inspection, probe])
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Stop inspections, probes, and the change request watch, end every follower, and wait for pending work.
|
|
203
|
+
* @returns settles once pending inspections and probes have ended.
|
|
204
|
+
*/
|
|
205
|
+
async dispose(): Promise<void> {
|
|
206
|
+
this.lifetime.abort()
|
|
207
|
+
this.stopWatching()
|
|
208
|
+
await Promise.allSettled(this.tasks)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
private entries(changeRequests: readonly SessionChangeRequest[]): ForgePanelChangeRequest[] {
|
|
212
|
+
return changeRequests.map((entry) => {
|
|
213
|
+
if (entry.update !== null && 'snapshot' in entry.update) this.snapshots.set(entry.url, entry.update.snapshot)
|
|
214
|
+
return { ...entry, snapshot: this.snapshots.get(entry.url) ?? null }
|
|
215
|
+
})
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
private worktree(): ForgePanelWorktree {
|
|
219
|
+
const state = this.worktreeStates.stateOf(this.session)
|
|
220
|
+
return {
|
|
221
|
+
editable: this.ctx.get('worktrees') !== undefined && !state.locked && state.created === null,
|
|
222
|
+
blocker: this.blocker,
|
|
223
|
+
request: state.request,
|
|
224
|
+
created: state.created,
|
|
225
|
+
creation: this.creations.of(this.session),
|
|
226
|
+
failure: this.creations.failureOf(this.session),
|
|
227
|
+
missing: this.missing,
|
|
228
|
+
restoration: this.creations.restorationOf(this.session),
|
|
229
|
+
waiting: this.creations.waitingOf(this.session),
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
private publish(next: ForgePanelState): void {
|
|
234
|
+
// A reinspection after a tool result usually finds the same checkout; equal states reach no follower.
|
|
235
|
+
if (isDeepStrictEqual(next, this.current)) return
|
|
236
|
+
this.current = next
|
|
237
|
+
for (const notify of [...this.followers]) notify()
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Track `task` for disposal; the returned promise resolves once it settles, whatever the outcome. */
|
|
241
|
+
private run(task: Promise<void>): Promise<void> {
|
|
242
|
+
this.tasks.add(task)
|
|
243
|
+
const settle = (): void => { this.tasks.delete(task) }
|
|
244
|
+
return task.then(settle, settle)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Inspect the checkout, one inspection at a time; calls during an
|
|
249
|
+
* inspection share one trailing rerun.
|
|
250
|
+
* @returns resolves once an inspection that started after the call has ended.
|
|
251
|
+
*/
|
|
252
|
+
private inspect(): Promise<void> {
|
|
253
|
+
if (this.lifetime.signal.aborted) return Promise.resolve()
|
|
254
|
+
if (this.inspection === null) {
|
|
255
|
+
this.inspection = this.run(this.inspectOnce()).then(() => { this.inspection = null })
|
|
256
|
+
return this.inspection
|
|
257
|
+
}
|
|
258
|
+
this.rerun ??= this.inspection.then(() => {
|
|
259
|
+
this.rerun = null
|
|
260
|
+
return this.inspect()
|
|
261
|
+
})
|
|
262
|
+
return this.rerun
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
private async inspectOnce(): Promise<void> {
|
|
266
|
+
const directory = this.current.directory
|
|
267
|
+
let checkout: ForgeCheckoutState
|
|
268
|
+
let repository: ForgeRepository | null = null
|
|
269
|
+
let blocker: ForgeWorktreeBlocker | null = null
|
|
270
|
+
let local: ForgeLocalChanges | null = null
|
|
271
|
+
let upstream: Upstream | null = null
|
|
272
|
+
try {
|
|
273
|
+
const inspection = await inspectCheckout(this.ctx, this.config, this.session, directory, this.lifetime.signal)
|
|
274
|
+
if (inspection === undefined) {
|
|
275
|
+
checkout = { state: 'none' }
|
|
276
|
+
blocker = 'not-git'
|
|
277
|
+
} else {
|
|
278
|
+
checkout = { state: 'ready', checkout: inspection.checkout }
|
|
279
|
+
repository = this.recognize(inspection)
|
|
280
|
+
upstream = this.upstreamOf(inspection)
|
|
281
|
+
blocker = await this.blockerOf(inspection)
|
|
282
|
+
local = await this.localOf(inspection)
|
|
283
|
+
}
|
|
284
|
+
} catch (error) {
|
|
285
|
+
checkout = { state: 'failed', message: error instanceof Error ? error.message : String(error) }
|
|
286
|
+
}
|
|
287
|
+
let missing: ForgeMissingWorktree | null = null
|
|
288
|
+
try {
|
|
289
|
+
const state = this.worktreeStates.stateOf(this.session)
|
|
290
|
+
missing = await missingWorktree(this.ctx, this.config, this.session, state, directory, this.lifetime.signal)
|
|
291
|
+
} catch (_error) {
|
|
292
|
+
// Without an answer from the filesystem or Git the panel offers no restoration.
|
|
293
|
+
}
|
|
294
|
+
if (this.lifetime.signal.aborted || this.current.directory !== directory) return
|
|
295
|
+
this.blocker = blocker
|
|
296
|
+
this.missing = missing
|
|
297
|
+
const sameHost = repository !== null && this.current.repository !== null && hostOf(repository) === hostOf(this.current.repository)
|
|
298
|
+
const readiness = sameHost ? this.current.readiness : null
|
|
299
|
+
this.publish({ ...this.current, checkout, repository, readiness, worktree: this.worktree(), local })
|
|
300
|
+
if (repository !== null && !sameHost) void this.probe(repository)
|
|
301
|
+
if (upstream !== null) this.discover(upstream)
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** The upstream branch with the first URL of its remote that a provider recognizes. */
|
|
305
|
+
private upstreamOf(inspection: Inspection): Upstream | null {
|
|
306
|
+
const tracked = upstreamBranch(inspection.upstream, inspection.remotes)
|
|
307
|
+
const remote = tracked?.remote.urls.find(url => this.ctx.forge.repositoryFromRemote(url) !== undefined)
|
|
308
|
+
return tracked === undefined || remote === undefined ? null : { remote, branch: tracked.branch }
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Bind the open change requests of the upstream branch, once per remote and branch until a refresh. */
|
|
312
|
+
private discover(upstream: Upstream): void {
|
|
313
|
+
const key = `${upstream.remote}\n${upstream.branch}`
|
|
314
|
+
if (this.discovered === key) return
|
|
315
|
+
this.discovered = key
|
|
316
|
+
void this.run(this.ctx.forgeSession.discoverBranch(this.session, upstream.remote, upstream.branch))
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** Decide whether the Session's sandbox lets the worktree service write the checkout and its shared Git directory. */
|
|
320
|
+
private async blockerOf(inspection: Inspection): Promise<ForgeWorktreeBlocker | null> {
|
|
321
|
+
const policy = this.ctx.sandboxPolicy.resolve({ session: this.session })
|
|
322
|
+
if (policy.mode === 'danger-full-access') return null
|
|
323
|
+
if (policy.mode === 'read-only') return 'read-only'
|
|
324
|
+
const root = this.ctx.fs.processPath(await this.ctx.fs.resolve(policy.workspaceRoot, { signal: this.lifetime.signal }))
|
|
325
|
+
return within(root, inspection.checkout.root) && within(root, inspection.commonDir) ? null : 'outside-writable-root'
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** Measure the `branch` scope; a failed measurement only hides the local changes card. */
|
|
329
|
+
private async localOf(inspection: Inspection): Promise<ForgeLocalChanges | null> {
|
|
330
|
+
try {
|
|
331
|
+
return await localChanges(this.ctx, this.config, this.session, inspection, this.worktree().created, this.lifetime.signal)
|
|
332
|
+
} catch (_error) {
|
|
333
|
+
// The changes view reads and reports the same changes itself.
|
|
334
|
+
return null
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
private recognize(inspection: Inspection): ForgeRepository | null {
|
|
339
|
+
for (const remote of remoteOrder(inspection.upstream, inspection.remotes)) {
|
|
340
|
+
for (const url of remote.urls) {
|
|
341
|
+
const repository = this.ctx.forge.repositoryFromRemote(url)
|
|
342
|
+
if (repository !== undefined) return repository
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
return null
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** Probe sign-in on the repository's host; resolves once the probe has ended. */
|
|
349
|
+
private probe(repository: ForgeRepository): Promise<void> {
|
|
350
|
+
const host = hostOf(repository)
|
|
351
|
+
return this.run((async () => {
|
|
352
|
+
let readiness: ForgeReadiness
|
|
353
|
+
try {
|
|
354
|
+
using timeout = deadline(this.lifetime.signal, this.config.readinessTimeoutMs, 'FORGE_PANEL_READINESS_TIMEOUT')
|
|
355
|
+
readiness = await this.ctx.forge.readiness(repository, timeout.signal)
|
|
356
|
+
} catch (error) {
|
|
357
|
+
readiness = { state: 'unavailable', message: error instanceof Error ? error.message : String(error) }
|
|
358
|
+
}
|
|
359
|
+
if (this.lifetime.signal.aborted || this.current.repository === null || hostOf(this.current.repository) !== host) return
|
|
360
|
+
this.publish({ ...this.current, readiness })
|
|
361
|
+
})())
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function sameWorktree(left: ForgePanelWorktree, right: ForgePanelWorktree): boolean {
|
|
366
|
+
return left.editable === right.editable && left.blocker === right.blocker && left.request === right.request
|
|
367
|
+
&& left.created === right.created && left.creation === right.creation && left.failure === right.failure
|
|
368
|
+
&& left.missing?.branchExists === right.missing?.branchExists && left.restoration === right.restoration
|
|
369
|
+
&& left.waiting === right.waiting
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Decide whether a Session works inside the worktree it created while that
|
|
374
|
+
* worktree's directory is gone, and whether its branch still exists.
|
|
375
|
+
* @param ctx - context that provides `fs` and the process services.
|
|
376
|
+
* @param config - Git limits.
|
|
377
|
+
* @param session - Session to check.
|
|
378
|
+
* @param state - recorded worktree and restoration completion.
|
|
379
|
+
* @param directory - the Session's current directory.
|
|
380
|
+
* @param signal - cancellation of the check.
|
|
381
|
+
* @returns the unavailable worktree, or null for a valid checkout or a Session working elsewhere.
|
|
382
|
+
* @throws when the filesystem or Git cannot answer.
|
|
383
|
+
*/
|
|
384
|
+
export async function missingWorktree(
|
|
385
|
+
ctx: Context,
|
|
386
|
+
config: GitConfig,
|
|
387
|
+
session: Session,
|
|
388
|
+
state: ForgeWorktreeState,
|
|
389
|
+
directory: string,
|
|
390
|
+
signal: AbortSignal,
|
|
391
|
+
): Promise<ForgeMissingWorktree | null> {
|
|
392
|
+
const created = state.created
|
|
393
|
+
if (created === null || !within(created.path, directory)) return null
|
|
394
|
+
if (!state.restoring && await ctx.fs.lstat(created.path, undefined, signal) !== undefined) {
|
|
395
|
+
const git = gitIn(ctx, config, session, created.path, signal)
|
|
396
|
+
const top = await git('rev-parse', ['rev-parse', '--show-toplevel'], [0, 128])
|
|
397
|
+
if (top.exitCode === 0) {
|
|
398
|
+
const target = await ctx.fs.resolve(created.path, { signal })
|
|
399
|
+
const root = await ctx.fs.resolve(top.stdout.replace(/\r?\n$/, ''), { cwd: created.path, signal })
|
|
400
|
+
if (root.targetKey === target.targetKey) {
|
|
401
|
+
const common = await git('common-dir', ['rev-parse', '--path-format=absolute', '--git-common-dir'])
|
|
402
|
+
const source = await gitIn(ctx, config, session, created.repositoryRoot, signal)(
|
|
403
|
+
'common-dir', ['rev-parse', '--path-format=absolute', '--git-common-dir'],
|
|
404
|
+
)
|
|
405
|
+
const actual = await ctx.fs.resolve(common.stdout.replace(/\r?\n$/, ''), { cwd: created.path, signal })
|
|
406
|
+
const expected = await ctx.fs.resolve(source.stdout.replace(/\r?\n$/, ''), { cwd: created.repositoryRoot, signal })
|
|
407
|
+
if (actual.targetKey === expected.targetKey) return null
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
try {
|
|
412
|
+
return { branchExists: await hasBranch(ctx, config, session, created.repositoryRoot, created.branch, signal) }
|
|
413
|
+
} catch (error) {
|
|
414
|
+
signal.throwIfAborted()
|
|
415
|
+
if (!state.restoring) throw error
|
|
416
|
+
return { branchExists: null }
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function hostOf(repository: ForgeRepository): string {
|
|
421
|
+
return `${repository.forge}:${repository.host}`
|
|
422
|
+
}
|
package/src/parse.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/** Parsers for the porcelain output of the read-only Git commands the panel runs. */
|
|
2
|
+
|
|
3
|
+
import type { ForgeBaseBranch } from './types.ts'
|
|
4
|
+
|
|
5
|
+
/** One remote with the URLs `git remote -v` prints for it. */
|
|
6
|
+
export interface GitRemote {
|
|
7
|
+
/** Remote name such as `origin`. */
|
|
8
|
+
readonly name: string
|
|
9
|
+
/** Distinct URLs, fetch URL first. */
|
|
10
|
+
readonly urls: readonly string[]
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Read the first line that `git rev-parse` prints for a directory query such as `--show-toplevel`.
|
|
15
|
+
* @param output - stdout of the command.
|
|
16
|
+
* @returns the directory.
|
|
17
|
+
* @throws when the output is empty.
|
|
18
|
+
*/
|
|
19
|
+
export function parseTopLevel(output: string): string {
|
|
20
|
+
const [root = ''] = output.split(/\r?\n/)
|
|
21
|
+
if (root.length === 0) throw new Error('git rev-parse printed no directory')
|
|
22
|
+
return root
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Read the first line of a one-name output such as `git symbolic-ref --short HEAD`.
|
|
27
|
+
* @param output - stdout of the command.
|
|
28
|
+
* @returns the name, or null when the command printed none.
|
|
29
|
+
*/
|
|
30
|
+
export function parseName(output: string): string | null {
|
|
31
|
+
const [name = ''] = output.split(/\r?\n/)
|
|
32
|
+
return name.length === 0 ? null : name
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Ref prefixes of the branches the `branch` scope can compare with: local branches, then remote-tracking branches. */
|
|
36
|
+
export const BASE_REF_PREFIXES = ['refs/heads/', 'refs/remotes/'] as const
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Select the branches the `branch` scope can compare with from `git for-each-ref`
|
|
40
|
+
* output, keeping its order and leaving out symbolic refs and the checked-out branch.
|
|
41
|
+
* @param output - stdout of `git for-each-ref --format=%(refname)%00%(symref)` over {@link BASE_REF_PREFIXES}.
|
|
42
|
+
* @param current - checked-out branch, or null when HEAD is detached.
|
|
43
|
+
* @param query - text the names must contain, ignoring case; empty or blank for every branch.
|
|
44
|
+
* @param limit - most branches in each group.
|
|
45
|
+
* @returns remote-tracking and local branches, at most `limit` of each.
|
|
46
|
+
*/
|
|
47
|
+
export function parseBaseBranches(
|
|
48
|
+
output: string,
|
|
49
|
+
current: string | null,
|
|
50
|
+
query: string,
|
|
51
|
+
limit: number,
|
|
52
|
+
): { readonly remote: ForgeBaseBranch[]; readonly local: ForgeBaseBranch[] } {
|
|
53
|
+
const [heads, remotes] = BASE_REF_PREFIXES
|
|
54
|
+
const needle = query.trim().toLowerCase()
|
|
55
|
+
const remote: ForgeBaseBranch[] = []
|
|
56
|
+
const local: ForgeBaseBranch[] = []
|
|
57
|
+
for (const line of output.split(/\r?\n/)) {
|
|
58
|
+
const [ref = '', symref = ''] = line.split('\0')
|
|
59
|
+
if (symref.length > 0) continue
|
|
60
|
+
const group = ref.startsWith(heads) ? local : ref.startsWith(remotes) ? remote : undefined
|
|
61
|
+
if (group === undefined || group.length >= limit) continue
|
|
62
|
+
const name = ref.slice(group === local ? heads.length : remotes.length)
|
|
63
|
+
if (name.length === 0 || (group === local && name === current) || !name.toLowerCase().includes(needle)) continue
|
|
64
|
+
group.push({ ref, name })
|
|
65
|
+
}
|
|
66
|
+
return { remote, local }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Order worktree base branches: the checked-out branch first, then the other
|
|
71
|
+
* listed branches in listed order.
|
|
72
|
+
* @param current - checked-out branch, or null when HEAD is detached.
|
|
73
|
+
* @param output - stdout of `git for-each-ref --format=%(refname:lstrip=2) refs/heads/`.
|
|
74
|
+
* @param limit - most names to return.
|
|
75
|
+
* @returns distinct branch names, at most `limit`.
|
|
76
|
+
*/
|
|
77
|
+
export function baseBranches(current: string | null, output: string, limit: number): string[] {
|
|
78
|
+
const listed = output.split(/\r?\n/).filter(name => name.length > 0 && name !== current)
|
|
79
|
+
return (current === null ? listed : [current, ...listed]).slice(0, limit)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Group the lines of `git remote -v` by remote.
|
|
84
|
+
* @param output - stdout of the command.
|
|
85
|
+
* @returns remotes in first-listed order.
|
|
86
|
+
*/
|
|
87
|
+
export function parseRemotes(output: string): GitRemote[] {
|
|
88
|
+
const remotes = new Map<string, string[]>()
|
|
89
|
+
for (const line of output.split(/\r?\n/)) {
|
|
90
|
+
const [, name = '', url = '', direction = ''] = /^(\S+)\t(.+) \((fetch|push)\)$/.exec(line) ?? []
|
|
91
|
+
if (name.length === 0) continue
|
|
92
|
+
let urls = remotes.get(name)
|
|
93
|
+
if (urls === undefined) {
|
|
94
|
+
urls = []
|
|
95
|
+
remotes.set(name, urls)
|
|
96
|
+
}
|
|
97
|
+
if (urls.includes(url)) continue
|
|
98
|
+
if (direction === 'fetch') urls.unshift(url)
|
|
99
|
+
else urls.push(url)
|
|
100
|
+
}
|
|
101
|
+
return [...remotes].map(([name, urls]) => ({ name, urls }))
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Split an upstream branch such as `origin/feature/x` into its remote and the
|
|
106
|
+
* branch on that remote. The remote is the longest remote name followed by `/`
|
|
107
|
+
* at the start of the upstream branch.
|
|
108
|
+
* @param upstream - upstream branch such as `origin/main`, or null.
|
|
109
|
+
* @param remotes - remotes in listed order.
|
|
110
|
+
* @returns the remote and branch, or undefined without an upstream or a remote it starts with.
|
|
111
|
+
*/
|
|
112
|
+
export function upstreamBranch(
|
|
113
|
+
upstream: string | null,
|
|
114
|
+
remotes: readonly GitRemote[],
|
|
115
|
+
): { readonly remote: GitRemote; readonly branch: string } | undefined {
|
|
116
|
+
if (upstream === null) return undefined
|
|
117
|
+
const remote = remotes
|
|
118
|
+
.filter(candidate => upstream.startsWith(`${candidate.name}/`))
|
|
119
|
+
.reduce<GitRemote | undefined>(
|
|
120
|
+
(longest, candidate) => longest === undefined || candidate.name.length > longest.name.length ? candidate : longest,
|
|
121
|
+
undefined,
|
|
122
|
+
)
|
|
123
|
+
return remote === undefined ? undefined : { remote, branch: upstream.slice(remote.name.length + 1) }
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Order remotes for code host recognition: the upstream's remote first, then
|
|
128
|
+
* `origin`, then the rest in listed order.
|
|
129
|
+
* @param upstream - upstream branch such as `origin/main`, or null.
|
|
130
|
+
* @param remotes - remotes in listed order.
|
|
131
|
+
* @returns the same remotes in recognition order.
|
|
132
|
+
*/
|
|
133
|
+
export function remoteOrder(upstream: string | null, remotes: readonly GitRemote[]): GitRemote[] {
|
|
134
|
+
const tracked = upstreamBranch(upstream, remotes)?.remote
|
|
135
|
+
const rank = (remote: GitRemote): number => remote === tracked ? 0 : remote.name === 'origin' ? 1 : 2
|
|
136
|
+
return [...remotes].sort((left, right) => rank(left) - rank(right))
|
|
137
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Change request patches kept in memory with the repository attributes read beside them, so the changes view reads
|
|
3
|
+
* neither from the code host again while a change request keeps its head commit and base branch.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Buffer } from 'node:buffer'
|
|
7
|
+
import type { ChangeRequestKey } from '@deepseek-ai/dsh-experimental-forge'
|
|
8
|
+
|
|
9
|
+
/** One change request's patch and the root `.gitattributes` text at its head commit, empty without one. */
|
|
10
|
+
export interface CachedPatch {
|
|
11
|
+
readonly patch: string
|
|
12
|
+
readonly attributes: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface Entry {
|
|
16
|
+
/** Head commit and base branch the patch was read at. */
|
|
17
|
+
readonly version: string
|
|
18
|
+
readonly value: CachedPatch
|
|
19
|
+
/** UTF-8 size of the patch and the attributes. */
|
|
20
|
+
readonly bytes: number
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* One patch per change request, bounded by the total UTF-8 size of the kept
|
|
25
|
+
* patches and their attributes; the least recently read patch leaves first.
|
|
26
|
+
*/
|
|
27
|
+
export class PatchCache {
|
|
28
|
+
private readonly entries = new Map<ChangeRequestKey, Entry>()
|
|
29
|
+
private bytes = 0
|
|
30
|
+
|
|
31
|
+
/** @param maxBytes - most UTF-8 bytes kept together; a larger patch is not kept. */
|
|
32
|
+
constructor(private readonly maxBytes: number) {}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Read the patch kept for a change request at a version, marking it the most recently read.
|
|
36
|
+
* @param key - the change request.
|
|
37
|
+
* @param version - head commit and base branch the reader needs.
|
|
38
|
+
* @returns the patch and its attributes, or undefined when none is kept at that version.
|
|
39
|
+
*/
|
|
40
|
+
get(key: ChangeRequestKey, version: string): CachedPatch | undefined {
|
|
41
|
+
const entry = this.entries.get(key)
|
|
42
|
+
if (entry?.version !== version) return undefined
|
|
43
|
+
this.entries.delete(key)
|
|
44
|
+
this.entries.set(key, entry)
|
|
45
|
+
return entry.value
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Keep a patch in place of the change request's earlier one, then drop the
|
|
50
|
+
* least recently read patches until the rest fit within `maxBytes`.
|
|
51
|
+
* @param key - the change request.
|
|
52
|
+
* @param version - head commit and base branch the patch was read at.
|
|
53
|
+
* @param value - the patch and its attributes.
|
|
54
|
+
*/
|
|
55
|
+
set(key: ChangeRequestKey, version: string, value: CachedPatch): void {
|
|
56
|
+
this.remove(key)
|
|
57
|
+
const bytes = Buffer.byteLength(value.patch) + Buffer.byteLength(value.attributes)
|
|
58
|
+
if (bytes > this.maxBytes) return
|
|
59
|
+
this.entries.set(key, { version, value, bytes })
|
|
60
|
+
this.bytes += bytes
|
|
61
|
+
for (const oldest of this.entries.keys()) {
|
|
62
|
+
if (this.bytes <= this.maxBytes) break
|
|
63
|
+
this.remove(oldest)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private remove(key: ChangeRequestKey): void {
|
|
68
|
+
const entry = this.entries.get(key)
|
|
69
|
+
if (entry === undefined) return
|
|
70
|
+
this.entries.delete(key)
|
|
71
|
+
this.bytes -= entry.bytes
|
|
72
|
+
}
|
|
73
|
+
}
|