Files
deepseek-harness/packages/experimental/worktree/src/name.ts
T
Yichen Jiang accd5b77c4 feat(worktree): name generated worktrees after their Session
Generated worktree names now join the dsh- prefix, the first eight
hexadecimal digits of the UUID in the Session id, and four random
hexadecimal digits, such as dsh-6b1f02c4-9a3e. A Session id without a
UUID gets eight random hexadecimal digits after the prefix. The worktree
runtime exports generatedWorktreeName, uses it for omitted
create_worktree names, and defaults namePrefix to dsh-. The forge panel
names its Session worktrees with the same function and defaults
worktreePrefix to dsh-, so checkouts no longer nest under a dsh/
directory.

The create_worktree name parameter asks for a few lowercase words joined
by hyphens that describe the task, and the tool description drops the
defaults that its parameters already state.
2026-10-20 11:49:51 +08:00

22 lines
1.0 KiB
TypeScript

/** Generated worktree names that lead back to the Session that created them. */
import { randomBytes } from 'node:crypto'
import type { Agent } from '@deepseek-ai/dsh-agent'
const UUID = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i
/**
* Generate a branch and checkout name: the prefix, the first eight hexadecimal digits of the
* first UUID in the Session id, a hyphen, and four random hexadecimal digits. A Session id
* without a UUID gets eight random hexadecimal digits after the prefix instead.
* @param prefix - validated name prefix.
* @param sessionId - id of the Session the worktree is created for.
* @returns the generated name; it is unique only with high probability, so creation still rejects an existing branch.
*/
export function generatedWorktreeName(prefix: string, sessionId: Agent['session']['id']): string {
const uuid = UUID.exec(sessionId)?.[0]
return uuid === undefined
? `${prefix}${randomBytes(4).toString('hex')}`
: `${prefix}${uuid.slice(0, 8).toLowerCase()}-${randomBytes(2).toString('hex')}`
}