Skip to content

hooks: resolve project/worktree execution context generically #669

Description

@carlospedreira

Context

PR #642 fixes the immediate enter-done verify-chain bug by selecting a task worktree when the worktree registry contains a valid entry. Two broader execution-context limitations were deliberately accepted as follow-up scope for that pragmatic fix.

BotRoot is <project>/.bot, not the repository root. Also, persisted TaskInstance records use a closed schema and do not contain a top-level working_directory field. Transition hooks otherwise run in fresh child runspaces whose location is currently inherited from the runtime process.

Required behavior

  • Resolve explicit hook roots from production data:
    • BotRoot: <project>/.bot for framework/state content.
    • ProjectRoot: parent of BotRoot for standalone tasks and fallback behavior.
    • WorktreeRoot: valid worktree registry path for workflow tasks when available.
  • For workflow tasks, prefer a valid WorktreeRoot; if the registry entry is missing or stale, fall back to ProjectRoot, never BotRoot and never the runtime launch directory.
  • For standalone tasks, run hooks from ProjectRoot.
  • Make working-directory selection generic in the transition dispatcher/status-handler path so every shipped or project transition hook receives the same deterministic context, rather than fixing individual leaf hooks.
  • Thread the resolved roots through RunContext using names that cannot be confused with BotRoot; initialize each hook runspace to the resolved execution root.
  • Define and consistently provide DOTBOT_PROJECT_ROOT and DOTBOT_STATE_ROOT to child processes where those variables are part of the hook contract.
  • Ensure enter-in-progress performs its advertised worktree lookup using a valid BotRoot instead of silently skipping it in the fresh runspace.
  • Do not add a top-level working_directory field to TaskInstance merely to support this behavior unless the closed schema is intentionally revised as a separate design decision.

Acceptance criteria / tests

  • A real standalone TaskInstance transitioned through the runtime HTTP/status path runs a marker verify/transition hook from the repository root, not .bot.
  • A workflow TaskInstance with a valid registry entry runs from the worktree root.
  • A workflow TaskInstance with a stale/missing registry entry falls back to the repository root.
  • Tests launch the runtime from an unrelated directory and prove at least two transition statuses, including a project/custom hook, do not inherit that directory.
  • Tests use schema-valid TaskInstance records; no synthetic top-level working_directory property.
  • Tests cover the resolved RunContext roots and any documented environment variables visible to hook child processes.
  • Hook location/context is restored or isolated after completion and timeout so one hook cannot leak cwd into another.
  • Existing Test-Hooks.ps1, dispatch tests, and runtime status-transition tests remain green.

Follow-up from #642 / #628.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status
    Inbox

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions