Skip to content

fix(hooks): Use project_dir for hook execution working directory#2105

Draft
xingyaoww wants to merge 3 commits intomainfrom
fix-hook-project-dir
Draft

fix(hooks): Use project_dir for hook execution working directory#2105
xingyaoww wants to merge 3 commits intomainfrom
fix-hook-project-dir

Conversation

@xingyaoww
Copy link
Collaborator

@xingyaoww xingyaoww commented Feb 17, 2026

Summary

Fixes an issue where hook commands with relative paths fail when hooks are loaded from a repository subdirectory.

Problem

When a user selects a repository (e.g., OpenHands/software-agent-sdk), hooks are loaded from /workspace/project/software-agent-sdk/.openhands/hooks.json. However, when the hook executor runs commands like .openhands/hooks/on_stop.sh, it uses the base workspace working_dir (/workspace/project) instead of the project directory where hooks were loaded from.

This causes hook commands to fail with "file not found" because the relative path doesn't resolve correctly.

Solution

  1. Add project_dir field to HookConfig: Stores the directory where hooks were loaded from
  2. Set project_dir in hooks_service.py: When loading hooks, store the project directory
  3. Use project_dir in LocalConversation: When setting up the hook executor, use project_dir (if set) instead of workspace.working_dir

Testing

This fix was identified through Datadog log analysis showing:

  • App-server correctly loading hooks and sending hook_config in StartConversationRequest
  • SDK receiving the config but executing hooks from the wrong directory

The fix ensures relative paths in hook commands resolve against the correct project directory.

Related

@xingyaoww can click here to continue refining the PR


Agent Server images for this PR

GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server

Variants & Base Images

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python amd64, arm64 nikolaik/python-nodejs:python3.12-nodejs22 Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:5df57e1-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-5df57e1-python \
  ghcr.io/openhands/agent-server:5df57e1-python

All tags pushed for this build

ghcr.io/openhands/agent-server:5df57e1-golang-amd64
ghcr.io/openhands/agent-server:5df57e1-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:5df57e1-golang-arm64
ghcr.io/openhands/agent-server:5df57e1-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:5df57e1-java-amd64
ghcr.io/openhands/agent-server:5df57e1-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:5df57e1-java-arm64
ghcr.io/openhands/agent-server:5df57e1-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:5df57e1-python-amd64
ghcr.io/openhands/agent-server:5df57e1-nikolaik_s_python-nodejs_tag_python3.12-nodejs22-amd64
ghcr.io/openhands/agent-server:5df57e1-python-arm64
ghcr.io/openhands/agent-server:5df57e1-nikolaik_s_python-nodejs_tag_python3.12-nodejs22-arm64
ghcr.io/openhands/agent-server:5df57e1-golang
ghcr.io/openhands/agent-server:5df57e1-java
ghcr.io/openhands/agent-server:5df57e1-python

About Multi-Architecture Support

  • Each variant tag (e.g., 5df57e1-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., 5df57e1-python-amd64) are also available if needed

When hooks are loaded from a repository subdirectory (e.g.,
/workspace/project/repo-name), the hook executor needs to run commands
from that directory, not the base workspace working_dir.

This fixes the issue where relative paths in hook commands like
.openhands/hooks/on_stop.sh would fail because they were being
resolved against /workspace/project instead of
/workspace/project/repo-name.

Changes:
- Add project_dir field to HookConfig
- Set project_dir in hooks_service when loading hooks from workspace
- Use project_dir (when set) as working_dir in LocalConversation's
  hook setup

Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions
Copy link
Contributor

github-actions bot commented Feb 17, 2026

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-sdk/openhands/sdk/conversation/impl
   local_conversation.py3312791%233, 277, 282, 310, 375, 427, 476, 484–486, 490–491, 573–574, 577, 723, 731, 733, 744, 746–748, 930, 935–938
openhands-sdk/openhands/sdk/hooks
   config.py1261290%73, 85–86, 92, 94, 179, 184, 245, 294–295, 297–298
TOTAL14072243082% 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments