Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

39.6 KB
Raw Blame
import { isDesktop } from '@lobechat/const';
import type { DeviceGitUpstreamRef } from '@lobechat/types';
import { getWorkingDirEffectivePath, getWorkingDirSourcePath } from '@lobechat/types';
import isEqual from 'fast-deep-equal';

import { gitService } from '@/services/git';
import { topicSelectors } from '@/store/chat/selectors';
import { getChatStoreState } from '@/store/chat/store';
import { getGitStatusStoreState } from '@/store/device/gitStatus/store';
import { getElectronStoreState } from '@/store/electron';

import { applyOwnBranchToConfig, parseCreatedBranch } from './ownBranch';
import { linkPushedPullRequests } from './pushedPullRequests';
import {
  applyBranchToConfig,
  applyDetachedToConfig,
  applyPushToConfig,
  applyWorktreeAddToConfig,
  applyWorktreeExitToConfig,
  type WorktreeAddInfo,
} from './worktreeConfigEffects';

/**
 * Detect git / gh side effects in a heterogeneous CLI agent's successful shell
 * tool call and mirror them onto the run topic's working-directory metadata.
 *
 * Runs from the `claude-code` / `codex` executor's `onAfterCall` hook
 * (renderer-side, fired on `tool_end`). The CLI session cwd stays anchored to
 * the source repo; selected worktree / branch are topic metadata.
 *
 * Command parsing is a trigger + hint, not the source of truth: a Codex-style
 * login-shell wrapper (`/bin/zsh -lc "…"`) is peeled off first, `$VAR`
 * references are expanded from assignments earlier in the same command, and a
 * path that still carries shell syntax after that is never recorded literally —
 * the real path is read back from the device's `git worktree list` (matched by
 * the branch hint), or nothing is recorded at all.
 */

/** Flags on `git worktree add` that consume the following token as their value. */
const VALUE_FLAGS = new Set(['-b', '-B', '--reason']);
const BRANCH_VALUE_FLAGS = new Set(['-b', '-B']);
/**
 * Branch flags that can only CREATE: `-C` / `-B` / `--force-create` also reset an
 * existing branch, so a successful run with them proves nothing about who made it.
 */
const CREATE_ONLY_BRANCH_FLAGS = new Set(['-b', '-c', '--create', '--orphan']);
const BRANCH_SWITCH_VALUE_FLAGS = new Set([
  '-b',
  '-B',
  '-c',
  '-C',
  '--create',
  '--force-create',
  '--orphan',
]);
const DETACH_FLAGS = new Set(['-d', '--detach']);
const GIT_BRANCH_SWITCH_SUBCOMMANDS = new Set(['checkout', 'switch']);
const GIT_SWITCH_VALUE_FLAGS = new Set(['--conflict', '--pathspec-from-file']);
const BRANCH_OUTPUT_PATTERNS = [
  /Switched to (?:a new )?branch ['"]([^'"\n]+)['"]/i,
  /Already on ['"]([^'"\n]+)['"]/i,
  /branch ['"]([^'"\n]+)['"] set up to track/i,
] as const;

/**
 * Claude Code's `EnterWorktree` / `ExitWorktree` move the session without ever
 * shelling out, so the `git worktree add` sniffing above cannot see them. Their
 * tool_result ships only CC's `message` string (the structured `worktreePath`
 * never leaves the tool), which makes these sentences the wire contract.
 */
const CC_ENTER_PATTERN = /^(?:Created|Entered) worktree at (.+?)(?: on branch (\S+))?\. /;
const CC_EXIT_PATTERN = /^Exited\b/;
/**
 * Said only on the branch that `chdir`s the SESSION into the worktree. A subagent
 * pinned to its own cwd gets "This agent's working directory …" instead, because it
 * moved only itself — recording that would claim the whole run had relocated.
 * Requiring this sentence fails CLOSED: if CC rewords it we stop recording, rather
 * than record a worktree the session is not in.
 */
const CC_SESSION_ENTER_MARKER = 'The session is now working in the worktree';

interface WorktreeAddParseResult {
  branch?: string;
  /** `-b` named {@link branch}: git refuses it when the branch exists, so it is new. */
  createdBranch?: boolean;
  /** Resolved absolute path, present only when the command yielded a trustworthy literal. */
  path?: string;
  /** Positional commit-ish (often the checked-out branch) — hint for the device lookup. */
  ref?: string;
}

/** `created`: a creation flag named the branch (`-c` / `-b` / `--orphan` …). */
type BranchSwitchInfo = { branch: string; created?: true } | { detached: true };

const stripQuotes = (token: string): string => {
  if (token.length >= 2) {
    const first = token[0];
    const last = token.at(-1);
    if ((first === '"' && last === '"') || (first === "'" && last === "'")) {
      return token.slice(1, -1);
    }
  }
  return token;
};

const isAbsolute = (p: string): boolean =>
  p.startsWith('/') || p.startsWith('~') || /^[A-Z]:[\\/]/i.test(p) || p.startsWith('\\\\');

type ShellEnv = Record<string, string>;

const VAR_REFERENCE = /\$\{([A-Z_]\w*)\}|\$([A-Z_]\w*)/gi;

/** Expand `$VAR` / `${VAR}` from assignments seen earlier in the command; unknown refs stay. */
const expandShellVars = (value: string, env: ShellEnv): string =>
  value.replaceAll(VAR_REFERENCE, (ref, braced, bare) => env[braced ?? bare] ?? ref);

/** stripQuotes + variable expansion; single-quoted tokens never expand (shell semantics). */
const resolveToken = (token: string, env?: ShellEnv): string => {
  const value = stripQuotes(token);
  return !env || token.startsWith("'") ? value : expandShellVars(value, env);
};

/**
 * A path token still carrying shell syntax after expansion — an unknown `$VAR`,
 * `$(…)`, backticks, quotes from broken tokenization, or a `~` we cannot resolve
 * against the device home — is NOT the literal directory git created.
 */
const isLiteralPath = (p: string): boolean => !/["$'`]/.test(p) && !p.startsWith('~');

/**
 * Fold a pure-assignment segment (`WT=/x`, `export WT=/x A=b`) into the ongoing
 * env. Assignments prefixing a command (`WT=/x git …`) scope to that command only
 * and are NOT folded — the shell expands that command's args before applying them.
 */
const collectSegmentAssignments = (tokens: string[], env: ShellEnv): void => {
  const start = stripQuotes(tokens[0] ?? '') === 'export' ? 1 : 0;
  if (start >= tokens.length || !tokens.slice(start).every((t) => isAssignment(t))) return;
  for (let i = start; i < tokens.length; i += 1) {
    const eq = tokens[i].indexOf('=');
    env[tokens[i].slice(0, eq)] = resolveToken(tokens[i].slice(eq + 1), env);
  }
};

const getExecutableName = (token: string): string | undefined =>
  stripQuotes(token).split(/[\\/]/).at(-1)?.toLowerCase();

const isGitExecutable = (token: string): boolean => {
  const executable = getExecutableName(token);
  return executable === 'git' || executable === 'git.exe';
};

const normalizeBranch = (branch?: string): string | undefined => {
  const value = branch?.trim();
  // A `$`/backtick here is an unexpanded shell construct, not a branch name.
  if (!value || value === '-' || /[$`]/.test(value)) return undefined;
  return value.includes(':') ? value.split(':').at(-1) : value;
};

const getInlineFlagValue = (token: string, flags: readonly string[]): string | undefined => {
  const stripped = stripQuotes(token);
  for (const flag of flags) {
    if (stripped.startsWith(`${flag}=`)) return stripped.slice(flag.length + 1);
  }
};

const getShortFlagValue = (token: string, flags: readonly string[]): string | undefined => {
  const stripped = stripQuotes(token);
  for (const flag of flags) {
    if (flag.length === 2 && stripped.startsWith(flag) && stripped.length > flag.length) {
      return stripped.slice(flag.length);
    }
  }
};

const isAssignment = (t: string) => /^[A-Z_]\w*=/i.test(t);

const consumeCommandPreamble = (tokens: string[]): number => {
  let i = 0;
  while (i < tokens.length && (isAssignment(tokens[i]) || WRAPPERS.has(stripQuotes(tokens[i])))) {
    i += 1;
  }
  return i;
};

const findInCommand = <T>(
  command: string | string[],
  parser: (tokens: string[], env?: ShellEnv) => T | undefined,
): T | undefined => {
  if (Array.isArray(command)) {
    if (!command.every((t) => typeof t === 'string')) return undefined;
    // A shell-wrapped argv carries a raw shell string as its payload — recurse so
    // it gets the same separator splitting and `$VAR` expansion as the string form.
    const wrapped = unwrapShellArgv(command);
    if (wrapped !== undefined) return findInCommand(wrapped, parser);
    return parser(command);
  }
  if (typeof command !== 'string') return undefined;

  const env: ShellEnv = {};
  for (const segment of unwrapShellCommand(command).split(/[\n;|&]/)) {
    const tokens = tokenize(segment);
    const result = parser(tokens, env);
    if (result) return result;
    collectSegmentAssignments(tokens, env);
  }
};

const parseBranchFromGitOutput = (content?: string): string | undefined => {
  if (!content) return undefined;

  for (const pattern of BRANCH_OUTPUT_PATTERNS) {
    const branch = normalizeBranch(pattern.exec(content)?.[1]);
    if (branch) return branch;
  }
};

const getOptionValue = (
  tokens: string[],
  index: number,
  flags: readonly string[],
  env?: ShellEnv,
) => {
  const token = tokens[index];
  const inlineValue = getInlineFlagValue(token, flags) ?? getShortFlagValue(token, flags);
  if (inlineValue !== undefined) return { value: resolveToken(inlineValue, env) };
  if (flags.includes(stripQuotes(token))) {
    return { skipNext: true, value: resolveToken(tokens[index + 1] ?? '', env) };
  }
  return {};
};

const parseGitSubcommand = (
  tokens: string[],
  cwd?: string,
  env?: ShellEnv,
): { args: string[]; baseCwd?: string; subcommand: string } | undefined => {
  let i = consumeCommandPreamble(tokens);
  if (!isGitExecutable(tokens[i] ?? '')) return undefined;
  i += 1;

  let baseCwd = cwd;
  while (i < tokens.length && tokens[i].startsWith('-')) {
    if (tokens[i] === '-C') {
      const dir = resolveToken(tokens[i + 1] ?? '', env);
      if (dir) baseCwd = resolveWorktreePath(dir, baseCwd);
      i += 2;
    } else if (GIT_VALUE_OPTS.has(tokens[i])) {
      i += 2;
    } else {
      i += 1;
    }
  }

  const subcommand = stripQuotes(tokens[i] ?? '');
  if (!subcommand) return undefined;

  return { args: tokens.slice(i + 1), baseCwd, subcommand };
};

const parseGitBranchSwitchTokens = (
  tokens: string[],
  env?: ShellEnv,
): BranchSwitchInfo | undefined => {
  const git = parseGitSubcommand(tokens);
  if (!git || !GIT_BRANCH_SWITCH_SUBCOMMANDS.has(git.subcommand)) return undefined;

  let branch: string | undefined;
  let created = false;
  for (let i = 0; i < git.args.length; i += 1) {
    const token = git.args[i];
    if (DETACH_FLAGS.has(stripQuotes(token))) return { detached: true };

    const branchFlag = getOptionValue(git.args, i, [...BRANCH_SWITCH_VALUE_FLAGS], env);
    if (branchFlag.value) {
      branch = normalizeBranch(branchFlag.value);
      created = CREATE_ONLY_BRANCH_FLAGS.has(stripQuotes(token).split('=')[0]);
      break;
    }
    if (branchFlag.skipNext) {
      i += 1;
      continue;
    }

    const valueFlag = getOptionValue(git.args, i, [...GIT_SWITCH_VALUE_FLAGS]);
    if (valueFlag.skipNext) {
      i += 1;
      continue;
    }
    if (token === '--') break;
    if (token.startsWith('-')) continue;

    // `git checkout <name>` is path-or-branch ambiguous. Use command-only
    // inference only for `git switch <branch>`, whose operand is a branch.
    if (git.subcommand === 'switch') branch = normalizeBranch(resolveToken(token, env));
    break;
  }

  return branch ? { branch, ...(created ? { created: true as const } : {}) } : undefined;
};

const isGitBranchSwitchCommand = (tokens: string[]): boolean | undefined => {
  const git = parseGitSubcommand(tokens);
  return git && GIT_BRANCH_SWITCH_SUBCOMMANDS.has(git.subcommand) ? true : undefined;
};

const parseGitBranchSwitch = (
  command: string | string[],
  resultContent?: string,
): BranchSwitchInfo | undefined => {
  const outputBranch = parseBranchFromGitOutput(resultContent);
  if (outputBranch && findInCommand(command, isGitBranchSwitchCommand)) {
    return { branch: outputBranch };
  }

  return findInCommand(command, parseGitBranchSwitchTokens);
};

/**
 * One ref a `git push` moved, as the push itself reported it.
 * `local` is the ref that was pushed (a branch name, or `HEAD`); `remote` the
 * branch it landed on.
 */
interface PushRefMapping {
  local: string;
  remote: string;
}

/** `git push` options that consume the following token as their value. */
const GIT_PUSH_VALUE_FLAGS = new Set(['--exec', '--push-option', '--receive-pack', '--repo', '-o']);

/**
 * A ref line from `git push`'s status table:
 *
 * ```
 *  * [new branch]      worktree-feat+x -> feat/y
 *  + 031bf96...f6b1c2a worktree-feat+x -> feat/y (forced update)
 *    031bf96..f6b1c2a  worktree-feat+x -> feat/y
 * ```
 *
 * Anchored to that exact shape (flag, then a bracketed status or a SHA range, then
 * the mapping) so prose in a compound command's output — `git commit && git push`,
 * a diff, a log line — can never be mistaken for a push result.
 */
const PUSH_REF_LINE =
  /^\s*(?:\S\s+)?(\[[^\]]*\]|[\da-f]{4,40}\.{2,3}[\da-f]{4,40})\s+(\S+)\s+->\s+(\S+)(?:\s+\(.*\))?\s*$/i;

/**
 * Bracketed statuses that mean a branch is now published at the mapped ref. A SHA
 * range says the same. Everything else — `[rejected]`, `[remote rejected]`,
 * `[deleted]`, `[new tag]`, `[tag update]` — either failed or is not a branch, and
 * an allowlist keeps a future git status from being silently read as a success.
 */
const PUSH_SUCCESS_STATUS = /^\[(?:new branch|new reference|up to date)\]$/i;

const REMOTE_URL_PATTERN = /^\w[\w+.-]*:\/\/|^[^\s/][^\s/@]*@[^\s/]+:/;

/** A `git push <target>` positional that is a URL, not a named remote — unnameable. */
const isRemoteUrl = (value: string): boolean =>
  REMOTE_URL_PATTERN.test(value) || value.startsWith('/') || value.startsWith('.');

/**
 * `git push` mappings, read out of the command's own output. This is the ONE place
 * the local→remote mapping is stated for every push form — bare `git push`, `push -u
 * origin HEAD`, and `push origin local:remote` alike. Parsing the command instead
 * would only ever cover the explicit-refspec form.
 */
const parsePushRefMappings = (content?: string): PushRefMapping[] => {
  if (!content) return [];

  const mappings: PushRefMapping[] = [];
  for (const line of content.split('\n')) {
    const match = PUSH_REF_LINE.exec(line);
    if (!match) continue;

    const [, status, local, remote] = match;
    if (status.startsWith('[') && !PUSH_SUCCESS_STATUS.test(status)) continue;
    // `(none) -> x` is a branch deletion; a tag ref is not a branch.
    if (local === '(none)' || local.startsWith('refs/tags/') || remote.startsWith('refs/tags/'))
      continue;

    const mapping = { local, remote: remote.replace(/^refs\/heads\//, '') };
    // git writes the table to stderr; a result that carries both streams (or
    // echoes them) states the same mapping twice — one ref, not an ambiguity.
    if (!mappings.some((m) => m.local === mapping.local && m.remote === mapping.remote))
      mappings.push(mapping);
  }
  return mappings;
};

const parseGitPushTokens = (tokens: string[], env?: ShellEnv): { remote?: string } | undefined => {
  const git = parseGitSubcommand(tokens);
  if (!git || git.subcommand !== 'push') return undefined;

  for (let i = 0; i < git.args.length; i += 1) {
    const token = git.args[i];

    const optionValue = getOptionValue(git.args, i, [...GIT_PUSH_VALUE_FLAGS], env);
    if (optionValue.skipNext) {
      i += 1;
      continue;
    }
    if (optionValue.value !== undefined) continue;
    if (token.startsWith('-')) continue;

    // First positional is the push target. A URL target has no name we could record.
    const target = resolveToken(token, env);
    if (!target) continue;
    return isRemoteUrl(target) ? {} : { remote: target };
  }

  // Bare `git push` — the remote is whatever the branch tracks, conventionally origin.
  return {};
};

/**
 * The remote ref a successful `git push` published the topic's branch to.
 *
 * Fails closed on ambiguity: when the branch is known, a mapping must name it (or
 * `HEAD`), and a multi-ref push that leaves more than one candidate records nothing
 * rather than pick.
 */
const parseGitPush = (
  command: string | string[],
  resultContent?: string,
  branch?: string,
): DeviceGitUpstreamRef | undefined => {
  const push = findInCommand(command, parseGitPushTokens);
  if (!push) return undefined;

  const mappings = parsePushRefMappings(resultContent);
  const candidates = branch
    ? mappings.filter((mapping) => mapping.local === branch || mapping.local === 'HEAD')
    : mappings;
  if (candidates.length !== 1) return undefined;

  const remoteBranch = normalizeBranch(candidates[0].remote);
  if (!remoteBranch) return undefined;

  return { branch: remoteBranch, remote: push.remote ?? 'origin' };
};

/** The command runs a fallback when an earlier step fails (`a || b`). */
const hasFailureRecovery = (command: string | string[]): boolean =>
  (Array.isArray(command) ? command.join(' ') : command).includes('||');

/** Collapse `.`/`..` segments in a POSIX path without touching the filesystem. */
const normalizePosix = (p: string): string => {
  const isAbs = p.startsWith('/');
  const out: string[] = [];
  for (const part of p.split('/')) {
    if (part === '' || part === '.') continue;
    if (part === '..') {
      if (out.length > 0 && out.at(-1) !== '..') out.pop();
      else if (!isAbs) out.push('..');
    } else {
      out.push(part);
    }
  }
  return (isAbs ? '/' : '') + out.join('/');
};

const resolveWorktreePath = (p: string, cwd?: string): string => {
  // Windows / home-relative paths: can't resolve without the device fs, keep as-is.
  if (isAbsolute(p)) return p.startsWith('/') ? normalizePosix(p) : p;
  if (!cwd) return p;
  return normalizePosix(`${cwd}/${p}`);
};

const tokenize = (s: string): string[] => s.match(/"[^"]*"|'[^']*'|\S+/g) ?? [];

/** `git` global options that consume the following token as their value. */
const GIT_VALUE_OPTS = new Set([
  '-C',
  '-c',
  '--git-dir',
  '--work-tree',
  '--namespace',
  '--super-prefix',
  '--config-env',
]);
/** Command wrappers that may precede the real `git` executable. */
const WRAPPERS = new Set(['sudo', 'env', 'command', 'nice', 'nohup', 'time']);

/**
 * Codex ships every shell tool call wrapped in a login shell — the wire command is
 * `/bin/zsh -lc "git worktree add …"` (or the argv form `['bash', '-lc', 'git …']`)
 * — so the head token is the shell, never `git`/`gh`, and the real command hides
 * inside a single quoted token. The display layer strips this wrapper
 * (`stripShellWrapper` in `packages/builtin-tools/src/codex/commandExecutionUtils.ts`
 * — keep the accepted shapes in sync), so parsing must strip it too, or the UI shows
 * a bare git command that this module silently rejected.
 */
const SHELL_WRAPPER_PATTERN =
  /^(?:\/usr\/bin\/env\s+)?(?:\/\S+\/)?(?:bash|sh|zsh)\s+(?:-lc|-c|-l\s+-c)\s+(\S[\s\S]*)$/;
const SHELL_EXECUTABLES = new Set(['bash', 'sh', 'zsh']);

/** Undo the shell's own quoting of the wrapper payload (`-lc "git \"…\""` → `git "…"`). */
const stripOuterShellQuotes = (value: string): string => {
  const trimmed = value.trim();
  if (trimmed.length < 2) return trimmed;

  const quote = trimmed[0];
  if ((quote !== '"' && quote !== "'") || trimmed.at(-1) !== quote) return trimmed;

  const body = trimmed.slice(1, -1);
  if (quote === "'") return body.replaceAll("'\\''", "'");

  return body
    .replaceAll('\\"', '"')
    .replaceAll('\\`', '`')
    .replaceAll('\\$', '$')
    .replaceAll('\\\\', '\\');
};

/** `['/bin/zsh', '-lc', 'git …']` → `git …`; undefined when argv is not a shell wrapper. */
const unwrapShellArgv = (tokens: string[]): string | undefined => {
  let i = getExecutableName(tokens[0] ?? '') === 'env' ? 1 : 0;
  if (!SHELL_EXECUTABLES.has(getExecutableName(tokens[i] ?? '') ?? '')) return undefined;
  i += 1;
  if (stripQuotes(tokens[i] ?? '') === '-l') i += 1;
  const flag = stripQuotes(tokens[i] ?? '');
  if (flag !== '-c' && flag !== '-lc') return undefined;
  return tokens[i + 1];
};

/** Peel `bash|sh|zsh -lc "…"` layers off a raw command string until none remain. */
const unwrapShellCommand = (command: string): string => {
  let current = command;
  // Wrappers can nest (`zsh -lc 'bash -c "git …"'`) but only shallowly; each peel
  // strictly shortens the string, so the depth cap is a formality.
  for (let depth = 0; depth < 4; depth += 1) {
    const match = SHELL_WRAPPER_PATTERN.exec(current.trim());
    if (!match) break;
    const inner = stripOuterShellQuotes(match[1]);
    if (!inner) break;
    current = inner;
  }
  return current;
};

/**
 * If ONE command's tokens are a real `git … worktree add <path>` invocation, return
 * the path (resolved against the source cwd, honoring a `-C <dir>` override).
 * Requires the executable to actually be `git` — so `echo git worktree add …` or
 * `rg "git worktree add"` never match. When the path token stays shell syntax even
 * after `$VAR` expansion, `path` is omitted and only the branch/ref hints survive.
 */
const parseGitWorktreeAddTokens = (
  tokens: string[],
  cwd?: string,
  env?: ShellEnv,
): WorktreeAddParseResult | undefined => {
  let i = 0;

  // Strip leading `VAR=val` assignments and command wrappers (sudo/env/…).
  while (i < tokens.length && (isAssignment(tokens[i]) || WRAPPERS.has(stripQuotes(tokens[i])))) {
    i += 1;
  }
  if (!isGitExecutable(tokens[i] ?? '')) return undefined;
  i += 1;

  // git global options; `-C <dir>` rebases relative worktree paths.
  let baseCwd = cwd;
  while (i < tokens.length && tokens[i].startsWith('-')) {
    if (tokens[i] === '-C') {
      const dir = resolveToken(tokens[i + 1] ?? '', env);
      if (dir) baseCwd = resolveWorktreePath(dir, baseCwd);
      i += 2;
    } else if (GIT_VALUE_OPTS.has(tokens[i])) {
      i += 2;
    } else {
      i += 1; // valueless flag or `--opt=val`
    }
  }

  // Subcommand must be exactly `worktree add`.
  if (stripQuotes(tokens[i] ?? '') !== 'worktree') return undefined;
  i += 1;
  if (stripQuotes(tokens[i] ?? '') !== 'add') return undefined;
  i += 1;

  // Positionals after `add`: first is the worktree path, second the commit-ish.
  let branch: string | undefined;
  let createdBranch = false;
  let path: string | undefined;
  let ref: string | undefined;
  for (; i < tokens.length; i += 1) {
    const token = tokens[i];
    if (BRANCH_VALUE_FLAGS.has(token)) {
      branch = resolveToken(tokens[i + 1] ?? '', env) || branch;
      createdBranch = CREATE_ONLY_BRANCH_FLAGS.has(token);
      i += 1; // skip this flag's value
      continue;
    }
    if (VALUE_FLAGS.has(token)) {
      i += 1; // skip this flag's value
      continue;
    }
    if (/^\d*[<>]/.test(token)) break; // redirection — git's args end here
    if (token.startsWith('-')) continue; // other flags
    const value = resolveToken(token, env);
    if (!value) continue;
    if (path === undefined) {
      path = value;
      continue;
    }
    ref = value;
    break;
  }
  if (path === undefined) return undefined;

  return {
    ...(branch ? { branch } : {}),
    ...(branch && createdBranch ? { createdBranch: true } : {}),
    ...(ref ? { ref } : {}),
    ...(isLiteralPath(path) ? { path: resolveWorktreePath(path, baseCwd) } : {}),
  };
};

const parseWorktreeAddInfo = (
  command: string | string[],
  cwd?: string,
): WorktreeAddParseResult | undefined => {
  if (Array.isArray(command)) {
    if (!command.every((t) => typeof t === 'string')) return undefined;
    // A shell-wrapped argv carries a raw shell string as its payload — recurse so
    // it gets the same separator splitting and `$VAR` expansion as the string form.
    const wrapped = unwrapShellArgv(command);
    if (wrapped !== undefined) return parseWorktreeAddInfo(wrapped, cwd);
    // Already-tokenized argv → a single command, boundaries preserved. No shell
    // ran, so tokens are literal — no assignments to collect or vars to expand.
    return parseGitWorktreeAddTokens(command, cwd);
  }
  if (typeof command !== 'string') return undefined;

  const unwrapped = unwrapShellCommand(command);
  if (!/\bworktree\s+add\b/.test(unwrapped)) return undefined;

  // The LAST add wins: it is where the command left things, so an earlier scratch
  // worktree that was removed again (`add a && remove a && add b`) never sticks.
  const env: ShellEnv = {};
  let last: WorktreeAddParseResult | undefined;
  for (const segment of unwrapped.split(/[\n;|&]/)) {
    const tokens = tokenize(segment);
    last = parseGitWorktreeAddTokens(tokens, cwd, env) ?? last;
    collectSegmentAssignments(tokens, env);
  }
  return last;
};

/**
 * Parse a shell command for a real `git worktree add <path>` invocation and return
 * the target worktree path (resolved to absolute against `cwd` when relative).
 * `$VAR` references are expanded from assignments earlier in the same command.
 *
 * `command` is a raw string (tokenized, split on shell separators) OR the argv
 * array form some CLIs use (Codex) — for the array we DON'T re-join+re-tokenize, so
 * a path token containing spaces (`['git','worktree','add','/tmp/my wt']`) keeps its
 * boundary. Returns `undefined` when the call isn't an actual worktree-add, or when
 * the path token cannot be statically resolved to a literal directory.
 */
export const parseWorktreeAddPath = (
  command: string | string[],
  cwd?: string,
): string | undefined => {
  return parseWorktreeAddInfo(command, cwd)?.path;
};

const isWindowsPath = (p: string): boolean => /^[A-Z]:[\\/]/i.test(p) || p.startsWith('\\\\');

/** Path equality that ignores a trailing slash, and separators and case on Windows. */
const isSamePath = (a: string, b: string): boolean => {
  const normalize = (p: string) => {
    const path = p.replace(/[\\/]+$/, '');
    return isWindowsPath(p) ? path.replaceAll('\\', '/').toLowerCase() : path;
  };
  return normalize(a) === normalize(b);
};

/**
 * Whether the command's last word on `target` is a real `git worktree remove` of it.
 * Paths resolve like `add` paths; segments are read in shell order, so a later
 * `add` of the same path recreates it. Text only — the caller confirms on the
 * device, since `cond && git worktree remove …` may never have run.
 */
const removesWorktree = (command: string | string[], target: string, cwd?: string) => {
  let removed = false;
  findInCommand(command, (tokens, env) => {
    const added = parseGitWorktreeAddTokens(tokens, cwd, env)?.path;
    if (added && isSamePath(added, target)) removed = false;
    const git = parseGitSubcommand(tokens, cwd, env);
    if (git?.subcommand !== 'worktree' || stripQuotes(git.args[0] ?? '') !== 'remove') return;
    for (const token of git.args.slice(1)) {
      if (/^\d*[<>]/.test(token)) return; // redirection — git's args end here
      if (token.startsWith('-')) continue;
      const path = resolveToken(token, env);
      if (isLiteralPath(path) && isSamePath(resolveWorktreePath(path, git.baseCwd), target))
        removed = true;
      return;
    }
  });
  return removed;
};

/**
 * The source repo's worktrees as the run's device reports them; undefined when the
 * device cannot be asked. The topic's bound device (written at dispatch) or,
 * unbound, this machine — the local device answers over IPC, a remote one over RPC.
 */
const listDeviceWorktrees = async (source: string, boundDeviceId?: string) => {
  const currentDeviceId = getElectronStoreState().gatewayDeviceInfo?.deviceId;
  const targetDeviceId = boundDeviceId ?? currentDeviceId;
  const isLocalDevice = isDesktop && !!targetDeviceId && targetDeviceId === currentDeviceId;

  try {
    return await gitService.listGitWorktrees({
      deviceId: isLocalDevice ? undefined : targetDeviceId,
      path: source,
    });
  } catch {
    return undefined;
  }
};

/**
 * Ground truth for a worktree-add whose path token never survives static parsing
 * (`git worktree add "$WT" …`, `$(mktemp -d)`, `~/wt`): ask the run's target
 * device which worktree actually holds the branch the command referenced. Fails
 * closed — no usable branch hint, no match, or an unreachable device records
 * nothing rather than a bogus literal.
 */
const resolveWorktreeAddFromDevice = async (
  parsed: WorktreeAddParseResult,
  source: string,
  boundDeviceId?: string,
): Promise<WorktreeAddInfo | undefined> => {
  const branchHint = normalizeBranch(parsed.branch ?? parsed.ref);
  if (!branchHint) return undefined;

  const match = (await listDeviceWorktrees(source, boundDeviceId))?.find(
    (worktree) => worktree.branch === branchHint,
  );
  if (!match) return undefined;
  return { ...(match.branch ? { branch: match.branch } : {}), path: match.path };
};

/**
 * Whether the device's worktree list proves `worktree` is gone. An empty list is a
 * failed probe (a repository always lists its own checkout), not a confirmation.
 * The recorded branch is matched too: git lists the canonical path, which differs
 * from the recorded one when the worktree was added through a symlink, while a
 * branch is checked out in at most one worktree.
 */
const isWorktreeGone = (
  worktrees: { branch?: string; path: string }[] | undefined,
  worktree: string,
  branch?: string,
): boolean =>
  !!worktrees?.length &&
  !worktrees.some((w) => isSamePath(w.path, worktree) || (!!branch && w.branch === branch));

/**
 * The recorded worktree becomes the cwd of every later command on the source's
 * device, so it must be an absolute path in the source's own filesystem namespace.
 * A Windows source (`C:\repo`, `\\wsl.localhost\…`) whose command ran a nested
 * shell — `wsl.exe -- bash -lc "git worktree add /tmp/wt"` — yields a POSIX path
 * that does not exist for the Windows shell, and spawning there fails with ENOENT.
 */
const isWorktreePathOnSourceDevice = (path: string, source: string): boolean =>
  isWindowsPath(source) ? isWindowsPath(path) : path.startsWith('/');

/**
 * Turn a parse result into a recordable worktree: trust a statically resolved
 * path directly, otherwise read the real one back from the device. Either way a
 * path that is not usable as a cwd next to the source is dropped.
 */
const resolveWorktreeAddInfo = async (
  parsed: WorktreeAddParseResult | undefined,
  source: string,
  boundDeviceId?: string,
): Promise<WorktreeAddInfo | undefined> => {
  if (!parsed) return undefined;
  if (parsed.path && isWorktreePathOnSourceDevice(parsed.path, source)) {
    const branch = normalizeBranch(parsed.branch);
    return { ...(branch ? { branch } : {}), path: parsed.path };
  }
  const fromDevice = await resolveWorktreeAddFromDevice(parsed, source, boundDeviceId);
  if (!fromDevice || !isWorktreePathOnSourceDevice(fromDevice.path, source)) return undefined;
  return fromDevice;
};

/**
 * Record a successful `git worktree add` as the run topic's active worktree. Writes
 * to the passed `topicId` (the bound operation's topic — from the `onAfterCall`
 * context), NOT the globally-active topic, so a run whose topic the user has
 * navigated away from still updates its own state. No-op when the worktree resolves
 * to the source path itself or nothing would change.
 */
export const recordWorktreeAdd = async (params: {
  command: string | string[];
  topicId: string;
}): Promise<void> => {
  const { command, topicId } = params;
  const state = getChatStoreState();

  const topic = topicSelectors.getTopicById(topicId)(state);
  const currentConfig = topic?.metadata?.workingDirectoryConfig;
  const source = getWorkingDirSourcePath(currentConfig) ?? topic?.metadata?.workingDirectory;
  if (!source) return;

  const worktreeInfo = await resolveWorktreeAddInfo(
    parseWorktreeAddInfo(command, source),
    source,
    topic?.metadata?.boundDeviceId,
  );
  if (!worktreeInfo || worktreeInfo.path === source) return;

  const nextConfig = applyWorktreeAddToConfig(currentConfig, source, worktreeInfo);

  if (isEqual(currentConfig, nextConfig)) return;
  await state.updateTopicMetadata(topicId, { workingDirectoryConfig: nextConfig });
};

/**
 * Record successful heterogeneous CLI shell side effects onto the run topic:
 * - `git worktree add` selects the created worktree and explicit branch.
 * - `git worktree remove` of that worktree returns the topic to its source repo.
 * - `git switch` / confirmed `git checkout` updates the branch snapshot.
 * - `git push` records the remote ref the branch publishes to, and links the
 *   pull request the pushed branch already has (as a server Work row).
 * - A branch the command created is recorded as the conversation's own.
 */
export const recordGitCommandEffects = async (params: {
  command: string | string[];
  resultContent?: string;
  /** The shell call's id; lets the server re-read a push to link its PR. */
  toolCallId?: string;
  topicId: string;
}): Promise<void> => {
  const { command, resultContent, toolCallId, topicId } = params;
  const state = getChatStoreState();

  const topic = topicSelectors.getTopicById(topicId)(state);
  const currentConfig = topic?.metadata?.workingDirectoryConfig;
  const source = getWorkingDirSourcePath(currentConfig) ?? topic?.metadata?.workingDirectory;
  if (!source) return;

  let nextConfig = currentConfig;

  const worktreeInfo = await resolveWorktreeAddInfo(
    parseWorktreeAddInfo(command, source),
    source,
    topic?.metadata?.boundDeviceId,
  );
  if (worktreeInfo && worktreeInfo.path !== source) {
    nextConfig = applyWorktreeAddToConfig(nextConfig, source, worktreeInfo);
  }

  // Removing the worktree the topic runs in leaves it pointing at a deleted
  // directory, and every later run would fail to spawn there. The device has the
  // last word: a skipped or failed removal still lists the worktree, and an
  // unreachable device changes nothing.
  const activeWorktree = nextConfig?.git?.activeWorktree;
  if (activeWorktree && removesWorktree(command, activeWorktree, source)) {
    const worktrees = await listDeviceWorktrees(source, topic?.metadata?.boundDeviceId);
    if (isWorktreeGone(worktrees, activeWorktree, nextConfig?.git?.branch))
      nextConfig = applyWorktreeExitToConfig(nextConfig, source, { removed: true });
  }

  const branchSwitch = parseGitBranchSwitch(command, resultContent);
  if (branchSwitch) {
    nextConfig =
      'detached' in branchSwitch
        ? applyDetachedToConfig(nextConfig, source)
        : applyBranchToConfig(nextConfig, source, branchSwitch.branch);
  }

  // After the branch switch, so a `git switch -c x && git push` chain matches the
  // push against the branch it actually pushed rather than the one it left.
  const pushUpstream = parseGitPush(command, resultContent, nextConfig?.git?.branch);
  if (pushUpstream) {
    nextConfig = applyPushToConfig(nextConfig, source, pushUpstream);
  }

  // A branch the agent created is the conversation's own: its PR may be looked up.
  // The creation shows in git's output, or — with `-q`, which prints nothing —
  // in the creation flag that named it. A chain that recovers from a failure
  // (`git switch -c x || git switch x`) succeeds whether or not the creation
  // did, so only git's own output can say so there.
  const commandSaysCreated = !hasFailureRecovery(command);
  const created =
    parseCreatedBranch(resultContent) ??
    (commandSaysCreated && branchSwitch && 'created' in branchSwitch
      ? branchSwitch.branch
      : undefined) ??
    (() => {
      if (!commandSaysCreated) return undefined;
      const worktreeAdd = parseWorktreeAddInfo(command, source);
      return worktreeAdd?.createdBranch ? worktreeAdd.branch : undefined;
    })();
  nextConfig = applyOwnBranchToConfig(nextConfig, created);

  if (!isEqual(currentConfig, nextConfig)) {
    await state.updateTopicMetadata(topicId, { workingDirectoryConfig: nextConfig });
  }

  // A checkout performed inside the heterogeneous CLI bypasses ChatInput's
  // BranchSwitcher, which normally refreshes this cache itself. Revalidate the
  // shared branch key so the control bar follows
  // the repository's actual HEAD immediately after the tool call completes. A push
  // is refreshed too: it establishes the branch's remote ref, which this key carries.
  if (branchSwitch || pushUpstream) {
    const boundDeviceId = topic?.metadata?.boundDeviceId;
    const currentDeviceId = getElectronStoreState().gatewayDeviceInfo?.deviceId;
    const cacheDeviceId =
      currentDeviceId && boundDeviceId === currentDeviceId ? 'local' : boundDeviceId;
    const revalidateDeviceId = cacheDeviceId === 'local' ? undefined : cacheDeviceId;
    await getGitStatusStoreState().revalidateGitStatus(revalidateDeviceId, source, ['branch']);

    const branch = nextConfig?.git?.branch;
    if (pushUpstream && toolCallId && branch) {
      // Not awaited: the device lookup and the server's re-read may retry.
      void linkPushedPullRequests({
        branch,
        deviceId: revalidateDeviceId,
        path: getWorkingDirEffectivePath(nextConfig) ?? source,
        toolCallId,
        topicId,
      });
    }
  }
};

/**
 * Read the worktree CC entered out of `EnterWorktree`'s result message:
 *
 *   `${'Created' | 'Entered'} worktree at ${path}[ on branch ${branch}]. The session…`
 *
 * The path is delimited by ` on branch <ref>` or the `. ` before the next sentence,
 * so a path containing spaces survives; one containing `". "` would not. Returns
 * undefined unless the SESSION moved (see {@link CC_SESSION_ENTER_MARKER}).
 */
export const parseWorktreeEnterInfo = (content?: string): WorktreeAddInfo | undefined => {
  if (!content?.includes(CC_SESSION_ENTER_MARKER)) return undefined;

  const match = CC_ENTER_PATTERN.exec(content);
  const path = match?.[1]?.trim();
  if (!path) return undefined;

  const branch = normalizeBranch(match?.[2]);
  return { ...(branch ? { branch } : {}), path };
};

/**
 * Every successful `ExitWorktree` outcome — kept, removed, or "could not remove it"
 * — opens with `Exited …`, and all three leave the session back in its original
 * directory. The no-op ("no active EnterWorktree session") and the refusals are
 * validation failures, so they arrive with `success: false` and never get here.
 */
export const isWorktreeExitContent = (content?: string): boolean =>
  !!content && CC_EXIT_PATTERN.test(content);

/**
 * Record a successful `EnterWorktree` as the run topic's active worktree. Unlike
 * `git worktree add`, the path is never in the arguments — a bare call generates a
 * random name — so it is read back out of the result message.
 */
export const recordWorktreeEnter = async (params: {
  content?: string;
  topicId: string;
}): Promise<void> => {
  const { content, topicId } = params;
  const state = getChatStoreState();

  const topic = topicSelectors.getTopicById(topicId)(state);
  const currentConfig = topic?.metadata?.workingDirectoryConfig;
  const source = getWorkingDirSourcePath(currentConfig) ?? topic?.metadata?.workingDirectory;

  const worktreeInfo = parseWorktreeEnterInfo(content);
  if (!worktreeInfo || !source || worktreeInfo.path === source) return;

  let nextConfig = applyWorktreeAddToConfig(currentConfig, source, worktreeInfo);
  nextConfig = applyOwnBranchToConfig(nextConfig, parseCreatedBranch(content))!;

  if (isEqual(currentConfig, nextConfig)) return;
  await state.updateTopicMetadata(topicId, { workingDirectoryConfig: nextConfig });
};

/**
 * Record a successful `ExitWorktree` — the session is back in its original directory,
 * so the topic drops its worktree whether it was kept on disk or removed. Keeping it
 * would leave the topic pointing at a directory the run has left (and, on `remove`,
 * one that no longer exists).
 */
export const recordWorktreeExit = async (params: {
  content?: string;
  topicId: string;
}): Promise<void> => {
  const { content, topicId } = params;
  if (!isWorktreeExitContent(content)) return;

  const state = getChatStoreState();

  const topic = topicSelectors.getTopicById(topicId)(state);
  const currentConfig = topic?.metadata?.workingDirectoryConfig;
  const source = getWorkingDirSourcePath(currentConfig) ?? topic?.metadata?.workingDirectory;
  if (!source) return;

  // Never in a worktree → nothing to clear, and don't materialize an empty `git`.
  const currentGit = currentConfig?.git;
  if (!currentGit?.activeWorktree && !currentGit?.isWorktree) return;

  const nextConfig = applyWorktreeExitToConfig(currentConfig, source);

  if (isEqual(currentConfig, nextConfig)) return;
  await state.updateTopicMetadata(topicId, { workingDirectoryConfig: nextConfig });
};