@young1lin/dsh-ui-gitworkbench 0.1.15 → 0.1.16

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 (41) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/CHANGELOG_EN.md +16 -0
  3. package/README.md +4 -3
  4. package/README_EN.md +1 -1
  5. package/lib/client.js +1519 -477
  6. package/lib/dir-listing.js +34 -0
  7. package/lib/fs-remove.js +5 -36
  8. package/lib/index.js +192 -41
  9. package/lib/path-lock.js +54 -0
  10. package/lib/worktree.js +83 -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 +116 -121
  16. package/src/client/FileBrowser.tsx +196 -23
  17. package/src/client/GitWorkbenchPanel.module.css +1 -0
  18. package/src/client/GitWorkbenchPanel.tsx +100 -12
  19. package/src/client/SideRails.tsx +106 -0
  20. package/src/client/diff-cells.tsx +147 -0
  21. package/src/client/diff-nav.ts +4 -1
  22. package/src/client/dir-tree.ts +31 -1
  23. package/src/client/file-rows.ts +40 -0
  24. package/src/client/h-rail.ts +70 -0
  25. package/src/client/ignored-cache.ts +193 -0
  26. package/src/client/index.ts +22 -3
  27. package/src/client/locales.ts +12 -2
  28. package/src/client/row-heights.ts +225 -0
  29. package/src/client/styles/changes.css +33 -2
  30. package/src/client/styles/controls.css +5 -0
  31. package/src/client/styles/files.css +5 -0
  32. package/src/client/styles/rails.css +72 -0
  33. package/src/client/use-row-window.ts +7 -3
  34. package/src/client/use-variable-row-window.ts +210 -0
  35. package/src/dir-listing.ts +47 -0
  36. package/src/fs-remove.ts +5 -36
  37. package/src/index.ts +217 -43
  38. package/src/path-lock.ts +56 -0
  39. package/src/types/dsh-shim.d.ts +12 -2
  40. package/src/worktree.ts +97 -0
  41. package/src/write-checked.ts +1 -1
package/src/worktree.ts CHANGED
@@ -187,6 +187,103 @@ export function worktreeDir(repoRoot: string, name: string): string {
187
187
  return `${repoRoot.replace(/\/+$/, '')}/.agents/worktrees/${name}`
188
188
  }
189
189
 
190
+ // ---- session lineage: the binding a session effectively works under ----
191
+
192
+ /** A binding resolved for a session, with whether it is the session's own. */
193
+ export interface EffectiveBinding<T> {
194
+ readonly binding: T
195
+ /** False when the binding is the session's own; true when an ancestor's. */
196
+ readonly inherited: boolean
197
+ }
198
+
199
+ /**
200
+ * Deepest ancestor chain walked before the lookup gives up. Real delegation
201
+ * nests two or three levels; the cap exists so a corrupt lineage (a cycle the
202
+ * guard below somehow missed, a pathologically deep chain) costs a bounded
203
+ * number of lookups instead of walking forever.
204
+ */
205
+ const LINEAGE_HOP_CAP = 8
206
+
207
+ /**
208
+ * Resolve the binding a session effectively works under: its own, else the
209
+ * nearest ancestor's.
210
+ *
211
+ * A subagent session never gets a binding of its own — `worktree_enter` is
212
+ * called by the session that wants the worktree — but it works wherever its
213
+ * parent conversation works: the standing prompt, the chip, and `worktree_exit`'s
214
+ * diagnostics all answer "which worktree is THIS session in" through here. The
215
+ * walk is re-resolved on every read, so a session exiting its worktree changes
216
+ * only its own binding: descendants lend the next bound ancestor up the chain
217
+ * on their next read (possibly none — the common case — possibly a grandparent's,
218
+ * which is still the conversation tree they work in) and nothing dangles.
219
+ *
220
+ * Own wins over inherited on purpose: a session that enters a worktree of its
221
+ * own is deliberately somewhere else than its parent.
222
+ * @param sessionId - the session whose effective binding is wanted.
223
+ * @param parentOf - session id → parent session id, as `agent/session-start`
224
+ * delivered it (subagent headers name their parent).
225
+ * @param bindingOf - binding lookup (the bindings file, or the prompt mirror).
226
+ * @returns the effective binding, or undefined when neither the session nor any
227
+ * ancestor (within the hop cap) is bound.
228
+ */
229
+ export function resolveEffectiveBinding<T>(
230
+ sessionId: string,
231
+ parentOf: ReadonlyMap<string, string>,
232
+ bindingOf: (id: string) => T | undefined,
233
+ ): EffectiveBinding<T> | undefined {
234
+ const own = bindingOf(sessionId)
235
+ if (own !== undefined) return { binding: own, inherited: false }
236
+ const seen = new Set<string>([sessionId])
237
+ let ancestor = parentOf.get(sessionId)
238
+ for (let hops = 0; ancestor !== undefined && hops < LINEAGE_HOP_CAP; hops += 1) {
239
+ if (seen.has(ancestor)) return undefined
240
+ seen.add(ancestor)
241
+ const binding = bindingOf(ancestor)
242
+ if (binding !== undefined) return { binding, inherited: true }
243
+ ancestor = parentOf.get(ancestor)
244
+ }
245
+ return undefined
246
+ }
247
+
248
+ /**
249
+ * The session's parent edge, read off a dsh session header: the id of the
250
+ * session this one was delegated by, or undefined for a top-level session (or
251
+ * a malformed empty value). Both `parentOf` feeds — the `agent/session-start`
252
+ * listener and the prompt-time self-heal — go through here, so their input
253
+ * guards cannot drift apart.
254
+ */
255
+ export function lineageEdgeOf(header: { readonly parentSession?: string } | undefined): string | undefined {
256
+ const parent = header?.parentSession
257
+ return typeof parent === 'string' && parent.length > 0 ? parent : undefined
258
+ }
259
+
260
+ /**
261
+ * The standing notice for a session's effective binding — the text the
262
+ * `worktree:binding` prompt context returns. Both variants carry the same two
263
+ * operational rules; what differs is who holds the binding, and the inherited
264
+ * variant must NOT offer `worktree_exit` (the caller cannot unbind a parent's
265
+ * binding — the exit would fail, and the model should not be told to try).
266
+ * @param name - worktree name (also the directory under `.agents/worktrees/`).
267
+ * @param branch - branch checked out there, when known.
268
+ * @param inherited - whether an ancestor, not this session, holds the binding.
269
+ */
270
+ export function bindingNotice(name: string, branch: string | undefined, inherited: boolean): string {
271
+ const rel = `.agents/worktrees/${name}`
272
+ const branchNote = branch === undefined ? '' : ` (branch ${branch})`
273
+ const opening = inherited
274
+ ? `This session works in git worktree "${name}"${branchNote}, entered by its parent session.`
275
+ : `This session is bound to git worktree "${name}"${branchNote}.`
276
+ const closing = inherited
277
+ ? 'A path without that prefix acts on the MAIN worktree, not the worktree this conversation works in. '
278
+ + '(The binding belongs to the parent session; worktree_exit here would not unbind it.)'
279
+ : 'A path without that prefix acts on the MAIN worktree, not the bound one. Call worktree_exit to unbind.'
280
+ return `${opening}\n`
281
+ + 'The session working directory is still the repository root, so the binding is a convention you must apply yourself:\n'
282
+ + `- shell commands: pass workdir "${rel}"\n`
283
+ + `- file tools: prefix every path with ${rel}/\n`
284
+ + closing
285
+ }
286
+
190
287
  export function parseWorktreeList(porcelain: string): WorktreeEntry[] {
191
288
  const out: WorktreeEntry[] = []
192
289
  let path = ''
@@ -33,7 +33,7 @@
33
33
  import { randomBytes } from 'node:crypto'
34
34
 
35
35
  import { renameWithRetry } from './atomic-json.js'
36
- import { resolveInside } from './fs-remove.js'
36
+ import { resolveInside } from './path-lock.js'
37
37
  import { decodesAsUtf8, isSafePathArg, type OpFailure } from './git-ops.js'
38
38
  import type { GitRun } from './apply-blocks.js'
39
39