@young1lin/dsh-ui-gitworkbench 0.1.15 → 0.1.17

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.
Files changed (42) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/CHANGELOG_EN.md +26 -0
  3. package/README.md +30 -5
  4. package/README_EN.md +1 -1
  5. package/lib/client.js +1600 -519
  6. package/lib/dir-listing.js +34 -0
  7. package/lib/fs-remove.js +5 -36
  8. package/lib/index.js +233 -52
  9. package/lib/path-lock.js +54 -0
  10. package/lib/worktree.js +133 -0
  11. package/lib/write-checked.js +1 -1
  12. package/package.json +1 -1
  13. package/src/client/ChromeGlyph.tsx +5 -0
  14. package/src/client/CodeEditor.tsx +19 -1
  15. package/src/client/DiffViews.tsx +122 -123
  16. package/src/client/FileBrowser.tsx +196 -23
  17. package/src/client/GitWorkbenchPanel.module.css +1 -0
  18. package/src/client/GitWorkbenchPanel.tsx +114 -12
  19. package/src/client/SideRails.tsx +106 -0
  20. package/src/client/diff-cells.tsx +147 -0
  21. package/src/client/diff-model.ts +20 -0
  22. package/src/client/diff-nav.ts +4 -1
  23. package/src/client/dir-tree.ts +31 -1
  24. package/src/client/file-rows.ts +40 -0
  25. package/src/client/h-rail.ts +70 -0
  26. package/src/client/ignored-cache.ts +193 -0
  27. package/src/client/index.ts +22 -3
  28. package/src/client/locales.ts +18 -4
  29. package/src/client/row-heights.ts +225 -0
  30. package/src/client/styles/changes.css +33 -2
  31. package/src/client/styles/controls.css +5 -0
  32. package/src/client/styles/files.css +5 -0
  33. package/src/client/styles/rails.css +72 -0
  34. package/src/client/use-row-window.ts +7 -3
  35. package/src/client/use-variable-row-window.ts +210 -0
  36. package/src/dir-listing.ts +47 -0
  37. package/src/fs-remove.ts +5 -36
  38. package/src/index.ts +257 -55
  39. package/src/path-lock.ts +56 -0
  40. package/src/types/dsh-shim.d.ts +12 -2
  41. package/src/worktree.ts +153 -0
  42. package/src/write-checked.ts +1 -1
@@ -0,0 +1,54 @@
1
+ /**
2
+ * The one place a client-supplied path becomes an absolute path on disk.
3
+ *
4
+ * git needs no such lock: whatever pathspec it is handed, it will not read or
5
+ * write outside the repository, which is why `isSafePathArg` only has to keep
6
+ * a path from being mistaken for an option. The moment a path leaves git and
7
+ * reaches `readFile`, `stat` or `readdir`, that backstop is gone — `join(root,
8
+ * '../../../etc/passwd')` is just a path, and the browser is the least trusted
9
+ * source of paths this plugin has.
10
+ *
11
+ * So every filesystem call in the host resolves through here, and the RPCs
12
+ * that do it are pinned by `host-rooted-paths.test.ts` so a new one cannot
13
+ * quietly join a client string onto the root instead.
14
+ *
15
+ * What this does NOT defend against, deliberately and for the same reason
16
+ * `fs-remove.ts` says so: `resolve` is lexical, so a SYMLINK inside the
17
+ * worktree that points outward still resolves inside and is followed. Closing
18
+ * that means a `realpath` per segment on every read of every file, for a case
19
+ * git itself does not defend against and that presupposes write access to the
20
+ * repository the reader already opened.
21
+ *
22
+ * @module @young1lin/dsh-ui-gitworkbench/path-lock
23
+ */
24
+ import { resolve, sep } from 'node:path';
25
+ import { isSafeRelativePath } from './discard-ops.js';
26
+ /**
27
+ * Resolve a repo-relative path against the worktree root, refusing to leave it.
28
+ *
29
+ * Two locks, not one. {@link isSafeRelativePath} rejects the traversal
30
+ * SPELLINGS — absolute paths, drive letters, UNC prefixes, NUL bytes, any `..`
31
+ * segment including one buried mid-path. The `startsWith` below re-checks the
32
+ * RESOLVED path, which is the form the filesystem acts on, so a path that
33
+ * survives the first check by being spelled unusually still has to land inside
34
+ * the root to be acted on.
35
+ *
36
+ * @param root - the worktree directory, absolute.
37
+ * @param relative - repo-relative path from the client or from git's output.
38
+ * @returns the absolute path to act on.
39
+ * @throws if the path is not a safe relative path, resolves outside the root,
40
+ * or IS the root.
41
+ */
42
+ export function resolveInside(root, relative) {
43
+ if (!isSafeRelativePath(relative)) {
44
+ throw new Error(`unsafe path argument: ${JSON.stringify(relative)}`);
45
+ }
46
+ const base = resolve(root);
47
+ const target = resolve(base, relative);
48
+ if (target === base)
49
+ throw new Error('refusing to act on the worktree root itself');
50
+ if (!target.startsWith(base + sep)) {
51
+ throw new Error(`path escapes the worktree: ${JSON.stringify(relative)}`);
52
+ }
53
+ return target;
54
+ }
package/lib/worktree.js CHANGED
@@ -138,9 +138,142 @@ export function isRefName(ref) {
138
138
  && !ref.includes('..')
139
139
  && REF_CHARS.test(ref);
140
140
  }
141
+ /**
142
+ * The extra rules an ENTER branch must satisfy beyond {@link isRefName}.
143
+ *
144
+ * isRefName guards untrusted refs that reach git as positional arguments
145
+ * (leading `-`, `..`, alien characters). A branch worktreeEnter CREATES has
146
+ * two more classes of trouble: spellings check-ref-format refuses that
147
+ * REF_CHARS happens to pass (a leading or trailing dot, a `.lock` ending),
148
+ * and `head` — a legal ref on Linux that collides with HEAD on the
149
+ * case-insensitive filesystems the host runs on.
150
+ */
151
+ function isEnterBranch(branch) {
152
+ return isRefName(branch)
153
+ && !branch.startsWith('.')
154
+ && !branch.endsWith('.')
155
+ && !branch.endsWith('.lock')
156
+ && branch.toLowerCase() !== 'head';
157
+ }
158
+ /**
159
+ * Decide the branch a worktreeEnter call lands on.
160
+ *
161
+ * The worktree NAME is the identity knob — the directory under
162
+ * `.agents/worktrees/` — and doubles as the branch by default (the old
163
+ * contract, kept for callers that pass no branchName). branchName splits the
164
+ * two for the one thing the name can never express: a SLASH branch
165
+ * (`feature/foo` is a legal ref and an impossible Windows directory).
166
+ *
167
+ * Reuse keeps the registered worktree's own branch, request or no request;
168
+ * an explicit branchName it displaces is reported as branchOverridden so the
169
+ * hint can say so — refusing there would break enter's idempotency (the same
170
+ * call re-issued must rebind, not explode).
171
+ *
172
+ * An illegal branchName is REFUSED, never substituted: the worktree name may
173
+ * be auto-generated because a directory label is arbitrary, but a branch is
174
+ * semantic — silently renaming it lands work on the wrong branch.
175
+ * @param wtName - sanitized worktree name (the default branch).
176
+ * @param branchName - caller-requested branch, or undefined for the default.
177
+ * @param existingBranch - the registered worktree's own branch when the
178
+ * target directory already holds one, else undefined.
179
+ * @returns the branch to create or keep, plus whether an explicit request
180
+ * was set aside — or the refusal error.
181
+ */
182
+ export function resolveEnterBranch(wtName, branchName, existingBranch) {
183
+ if (branchName !== undefined && !isEnterBranch(branchName)) {
184
+ return { ok: false, error: 'branchName is not a valid branch name' };
185
+ }
186
+ if (existingBranch !== undefined) {
187
+ return { ok: true, branch: existingBranch, branchOverridden: branchName !== undefined && branchName !== existingBranch };
188
+ }
189
+ return { ok: true, branch: branchName ?? wtName, branchOverridden: false };
190
+ }
141
191
  export function worktreeDir(repoRoot, name) {
142
192
  return `${repoRoot.replace(/\/+$/, '')}/.agents/worktrees/${name}`;
143
193
  }
194
+ /**
195
+ * Deepest ancestor chain walked before the lookup gives up. Real delegation
196
+ * nests two or three levels; the cap exists so a corrupt lineage (a cycle the
197
+ * guard below somehow missed, a pathologically deep chain) costs a bounded
198
+ * number of lookups instead of walking forever.
199
+ */
200
+ const LINEAGE_HOP_CAP = 8;
201
+ /**
202
+ * Resolve the binding a session effectively works under: its own, else the
203
+ * nearest ancestor's.
204
+ *
205
+ * A subagent session never gets a binding of its own — `worktree_enter` is
206
+ * called by the session that wants the worktree — but it works wherever its
207
+ * parent conversation works: the standing prompt, the chip, and `worktree_exit`'s
208
+ * diagnostics all answer "which worktree is THIS session in" through here. The
209
+ * walk is re-resolved on every read, so a session exiting its worktree changes
210
+ * only its own binding: descendants lend the next bound ancestor up the chain
211
+ * on their next read (possibly none — the common case — possibly a grandparent's,
212
+ * which is still the conversation tree they work in) and nothing dangles.
213
+ *
214
+ * Own wins over inherited on purpose: a session that enters a worktree of its
215
+ * own is deliberately somewhere else than its parent.
216
+ * @param sessionId - the session whose effective binding is wanted.
217
+ * @param parentOf - session id → parent session id, as `agent/session-start`
218
+ * delivered it (subagent headers name their parent).
219
+ * @param bindingOf - binding lookup (the bindings file, or the prompt mirror).
220
+ * @returns the effective binding, or undefined when neither the session nor any
221
+ * ancestor (within the hop cap) is bound.
222
+ */
223
+ export function resolveEffectiveBinding(sessionId, parentOf, bindingOf) {
224
+ const own = bindingOf(sessionId);
225
+ if (own !== undefined)
226
+ return { binding: own, inherited: false };
227
+ const seen = new Set([sessionId]);
228
+ let ancestor = parentOf.get(sessionId);
229
+ for (let hops = 0; ancestor !== undefined && hops < LINEAGE_HOP_CAP; hops += 1) {
230
+ if (seen.has(ancestor))
231
+ return undefined;
232
+ seen.add(ancestor);
233
+ const binding = bindingOf(ancestor);
234
+ if (binding !== undefined)
235
+ return { binding, inherited: true };
236
+ ancestor = parentOf.get(ancestor);
237
+ }
238
+ return undefined;
239
+ }
240
+ /**
241
+ * The session's parent edge, read off a dsh session header: the id of the
242
+ * session this one was delegated by, or undefined for a top-level session (or
243
+ * a malformed empty value). Both `parentOf` feeds — the `agent/session-start`
244
+ * listener and the prompt-time self-heal — go through here, so their input
245
+ * guards cannot drift apart.
246
+ */
247
+ export function lineageEdgeOf(header) {
248
+ const parent = header?.parentSession;
249
+ return typeof parent === 'string' && parent.length > 0 ? parent : undefined;
250
+ }
251
+ /**
252
+ * The standing notice for a session's effective binding — the text the
253
+ * `worktree:binding` prompt context returns. Both variants carry the same two
254
+ * operational rules; what differs is who holds the binding, and the inherited
255
+ * variant must NOT offer `worktree_exit` (the caller cannot unbind a parent's
256
+ * binding — the exit would fail, and the model should not be told to try).
257
+ * @param name - worktree name (also the directory under `.agents/worktrees/`).
258
+ * @param branch - branch checked out there, when known.
259
+ * @param inherited - whether an ancestor, not this session, holds the binding.
260
+ */
261
+ export function bindingNotice(name, branch, inherited) {
262
+ const rel = `.agents/worktrees/${name}`;
263
+ const branchNote = branch === undefined ? '' : ` (branch ${branch})`;
264
+ const opening = inherited
265
+ ? `This session works in git worktree "${name}"${branchNote}, entered by its parent session.`
266
+ : `This session is bound to git worktree "${name}"${branchNote}.`;
267
+ const closing = inherited
268
+ ? 'A path without that prefix acts on the MAIN worktree, not the worktree this conversation works in. '
269
+ + '(The binding belongs to the parent session; worktree_exit here would not unbind it.)'
270
+ : 'A path without that prefix acts on the MAIN worktree, not the bound one. Call worktree_exit to unbind.';
271
+ return `${opening}\n`
272
+ + 'The session working directory is still the repository root, so the binding is a convention you must apply yourself:\n'
273
+ + `- shell commands: pass workdir "${rel}"\n`
274
+ + `- file tools: prefix every path with ${rel}/\n`
275
+ + closing;
276
+ }
144
277
  export function parseWorktreeList(porcelain) {
145
278
  const out = [];
146
279
  let path = '';
@@ -31,7 +31,7 @@
31
31
  */
32
32
  import { randomBytes } from 'node:crypto';
33
33
  import { renameWithRetry } from './atomic-json.js';
34
- import { resolveInside } from './fs-remove.js';
34
+ import { resolveInside } from './path-lock.js';
35
35
  import { decodesAsUtf8, isSafePathArg } from './git-ops.js';
36
36
  /**
37
37
  * Run one checked write end to end. Never throws: every failure, including an
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@young1lin/dsh-ui-gitworkbench",
3
- "version": "0.1.15",
3
+ "version": "0.1.17",
4
4
  "description": "Out-of-tree dsh web UI plugin: a session-header git workbench chip opening a drawer with the file tree, per-file diff, history, compare, staging, commit, and sync (fetch/pull/push).",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -16,6 +16,11 @@ const CHROME_GLYPH = {
16
16
  // and at 14px the only thing telling them apart is the arrow count.
17
17
  refresh: 'M8 3a5 5 0 1 0 4.546 2.914.5.5 0 0 1 .908-.417A6 6 0 1 1 8 2v1z M8 4.466V.534a.25.25 0 0 1 .41-.192l2.36 1.966c.12.1.12.284 0 .384L8.41 4.658A.25.25 0 0 1 8 4.466z',
18
18
  close: 'M2.146 2.854a.5.5 0 1 1 .708-.708L8 7.293l5.146-5.147a.5.5 0 0 1 .708.708L8.707 8l5.147 5.146a.5.5 0 0 1-.708.708L8 8.707l-5.146 5.147a.5.5 0 0 1-.708-.708L7.293 8 2.146 2.854Z',
19
+ // Drawn here rather than borrowed: Bootstrap has no wrap glyph, and the
20
+ // three text rules with a return arrow are what every editor spells this
21
+ // with. Same 16 viewBox and single fill as the rest, so the row still reads
22
+ // as one set.
23
+ wrap: 'M1 3.5a.5.5 0 0 1 .5-.5h13a.5.5 0 0 1 0 1h-13a.5.5 0 0 1-.5-.5zM1 7.5a.5.5 0 0 1 .5-.5h11a2.5 2.5 0 0 1 0 5h-2.293l1.147 1.146a.5.5 0 0 1-.708.708l-2-2a.5.5 0 0 1 0-.708l2-2a.5.5 0 0 1 .708.708L10.207 11H12.5a1.5 1.5 0 0 0 0-3h-11a.5.5 0 0 1-.5-.5zM1 12.5a.5.5 0 0 1 .5-.5h4a.5.5 0 0 1 0 1h-4a.5.5 0 0 1-.5-.5z',
19
24
  } as const
20
25
 
21
26
  export function ChromeGlyph({ of }: { of: keyof typeof CHROME_GLYPH }): ReactNode {
@@ -66,6 +66,9 @@ const paintFacet = Facet.define<PaintFn | null, PaintFn | null>({
66
66
  combine: values => values.length > 0 ? values[0]! : null,
67
67
  })
68
68
  const paintCompartment = new Compartment()
69
+ /** Soft wrap, in and out without rebuilding the view — the caret, the undo
70
+ * stack and the selection all survive the toggle. */
71
+ const wrapCompartment = new Compartment()
69
72
 
70
73
  /**
71
74
  * How long after the last keystroke the editor recomputes what it paints.
@@ -422,7 +425,7 @@ const paneTheme = EditorView.theme({
422
425
  ...SEARCH_PANEL_THEME,
423
426
  })
424
427
 
425
- export function CodeEditor({ value, original, onChange, paint, indent, ariaLabel, onSave, blame, notCommitted, readOnly, onBlameClick }: {
428
+ export function CodeEditor({ value, original, onChange, paint, indent, ariaLabel, onSave, blame, notCommitted, readOnly, onBlameClick, wrap }: {
426
429
  /** The pane's buffer. The view is written to only when this really differs. */
427
430
  value: string
428
431
  /** The other side's whole text — the index side, for the unstaged layer this
@@ -450,6 +453,11 @@ export function CodeEditor({ value, original, onChange, paint, indent, ariaLabel
450
453
  readOnly?: boolean
451
454
  /** A click in the blame gutter, with the 1-based line number. */
452
455
  onBlameClick?: (line: number) => void
456
+ /** Soft wrap. CodeMirror owns variable line heights natively — its height
457
+ * oracle measures wrapped lines and its own viewport walk stays correct —
458
+ * so this is the whole change here, unlike the diff panes, whose windowing
459
+ * assumes a fixed row height. */
460
+ wrap?: boolean
453
461
  }): ReactNode {
454
462
  /**
455
463
  * Editable, and SAID to be editable, from one boolean.
@@ -486,6 +494,7 @@ export function CodeEditor({ value, original, onChange, paint, indent, ariaLabel
486
494
  searchCount,
487
495
  highlightActiveLine(),
488
496
  paintCompartment.of(paintFacet.of(paint)),
497
+ wrapCompartment.of(wrap === true ? EditorView.lineWrapping : []),
489
498
  painter,
490
499
  blameField,
491
500
  blameCompartment.of([]),
@@ -565,5 +574,14 @@ export function CodeEditor({ value, original, onChange, paint, indent, ariaLabel
565
574
  current.dispatch({ effects: paintCompartment.reconfigure(paintFacet.of(paint)) })
566
575
  }, [paint])
567
576
 
577
+ // Wrap in or out. Through the compartment rather than a rebuild: the reader
578
+ // toggles this to look at the line they are already on, and a rebuilt view
579
+ // would drop the caret and the undo stack to show it to them.
580
+ useEffect(() => {
581
+ const current = view.current
582
+ if (current === null) return
583
+ current.dispatch({ effects: wrapCompartment.reconfigure(wrap === true ? EditorView.lineWrapping : []) })
584
+ }, [wrap])
585
+
568
586
  return <div ref={host} className={css.cmHost} data-editable={editable ? '' : undefined} />
569
587
  }