mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(learn): match opencode worktrees across path separators
opencode stores Windows worktrees with forward slashes while str(Path(worktree)) renders backslashes on Windows, so the 'WHERE worktree = ?' lookup found no project row and v2 scans silently returned zero sessions. Normalize both spellings when locating the project row: separators, trailing slashes, and case for drive-letter/UNC paths (Windows filesystems are case-insensitive). POSIX paths stay case-sensitive. Legacy v1 and mixed-schema scans share the same lookup and keep working. Add a forward-slash Windows worktree regression that runs through discovery and scan and asserts the actual tool calls.
This commit is contained in:
@@ -70,6 +70,20 @@ def _session_table(tables: set[str]) -> str | None:
|
||||
return None
|
||||
|
||||
|
||||
def _worktree_key(value: str | Path) -> str:
|
||||
"""Normalize a worktree path for cross-platform database matching.
|
||||
|
||||
opencode stores Windows worktrees with POSIX separators (``C:/Users/...``)
|
||||
while ``str(Path(...))`` renders them with backslashes on Windows. Compare
|
||||
both spellings, and fold case for drive-letter/UNC paths because Windows
|
||||
filesystems are case-insensitive. POSIX paths stay case-sensitive.
|
||||
"""
|
||||
path = str(value).replace("\\", "/")
|
||||
if path.startswith("//") or (len(path) >= 2 and path[1] == ":"):
|
||||
path = path.casefold()
|
||||
return path.rstrip("/") or "/"
|
||||
|
||||
|
||||
def _extract_output(state: dict) -> str:
|
||||
"""Extract human-readable tool output from either schema shape.
|
||||
|
||||
@@ -235,14 +249,15 @@ class OpenCodePlugin(LearnPlugin, ConversationScanner):
|
||||
if sess_tbl is None:
|
||||
return []
|
||||
|
||||
cursor.execute(
|
||||
"SELECT id FROM project WHERE worktree = ?",
|
||||
(str(project.project_path),),
|
||||
)
|
||||
row = cursor.fetchone()
|
||||
if row is None:
|
||||
cursor.execute("SELECT id, worktree FROM project")
|
||||
target_worktree = _worktree_key(project.project_path)
|
||||
project_db_id: str | None = None
|
||||
for proj_id, worktree in cursor.fetchall():
|
||||
if isinstance(worktree, str) and _worktree_key(worktree) == target_worktree:
|
||||
project_db_id = proj_id
|
||||
break
|
||||
if project_db_id is None:
|
||||
return []
|
||||
project_db_id: str = row[0]
|
||||
|
||||
cursor.execute(
|
||||
f"SELECT id, time_created FROM {sess_tbl} WHERE project_id = ? ORDER BY time_created DESC LIMIT 200",
|
||||
|
||||
@@ -342,6 +342,7 @@ def _create_opencode_db_v2(
|
||||
project_id: str = "project-v2",
|
||||
project_name: str | None = None,
|
||||
session_id: str = "session-v2",
|
||||
worktree: str | None = None,
|
||||
) -> None:
|
||||
"""Build a minimal opencode v2 database (session_v2 + session_message).
|
||||
|
||||
@@ -383,7 +384,7 @@ def _create_opencode_db_v2(
|
||||
|
||||
conn.execute(
|
||||
"INSERT INTO project (id, worktree, name) VALUES (?, ?, ?)",
|
||||
(project_id, str(project_path), project_name),
|
||||
(project_id, worktree if worktree is not None else str(project_path), project_name),
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO session_v2 (id, project_id, time_created) VALUES (?, ?, ?)",
|
||||
@@ -466,6 +467,37 @@ def test_opencode_plugin_v2_schema_discovers_projects_and_scans_tool_calls(
|
||||
assert err_call.error_category == ErrorCategory.RUNTIME_ERROR
|
||||
|
||||
|
||||
def test_opencode_plugin_v2_forward_slash_windows_worktree_scans_calls(
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
# opencode stores Windows worktrees with forward slashes, while
|
||||
# str(Path(worktree)) renders them with backslashes on Windows. Scanning
|
||||
# must match both spellings instead of silently returning zero sessions.
|
||||
db_path = tmp_path / "opencode-v2-windows.db"
|
||||
_create_opencode_db_v2(
|
||||
db_path,
|
||||
Path("C:/Users/runner/project"),
|
||||
project_name="WindowsProject",
|
||||
worktree="C:/Users/runner/project",
|
||||
)
|
||||
|
||||
plugin = OpenCodePlugin(db_path=db_path)
|
||||
projects = plugin.discover_projects()
|
||||
assert len(projects) == 1
|
||||
|
||||
# Simulate discovery on Windows: Path() renders the stored worktree with
|
||||
# native separators (and may normalize the drive-letter case).
|
||||
projects[0].project_path = Path(r"c:\users\runner\project")
|
||||
|
||||
sessions = plugin.scan_project(projects[0])
|
||||
assert len(sessions) == 1
|
||||
assert [call.tool_call_id for call in sessions[0].tool_calls] == [
|
||||
"call-v2-1",
|
||||
"call-v2-2",
|
||||
]
|
||||
assert sessions[0].tool_calls[1].is_error is True
|
||||
|
||||
|
||||
def test_opencode_plugin_v2_null_project_name_falls_back_to_worktree(
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
|
||||
Reference in New Issue
Block a user