|
4 | 4 | import os |
5 | 5 | import time |
6 | 6 | from dataclasses import dataclass |
| 7 | +from pathlib import Path |
7 | 8 | from typing import Optional |
8 | 9 |
|
9 | 10 | _config_cache: Optional["RoadmapConfig"] = None |
@@ -67,24 +68,133 @@ def get_roadmap_config() -> RoadmapConfig: |
67 | 68 | return _config_cache |
68 | 69 |
|
69 | 70 |
|
70 | | -def resolve_workspace_root(explicit: Optional[str] = None) -> str: |
71 | | - """Resolve the project workspace for ROADMAP.md.""" |
| 71 | +class RoadmapWorkspaceError(ValueError): |
| 72 | + """Project workspace for ROADMAP.md could not be resolved safely.""" |
| 73 | + |
| 74 | + |
| 75 | +def _reject_quarantined(root: str) -> None: |
| 76 | + try: |
| 77 | + from plugins.dietcode.lib.kernel_workspace import is_quarantined_root |
| 78 | + except ImportError: |
| 79 | + return |
| 80 | + if is_quarantined_root(root): |
| 81 | + raise RoadmapWorkspaceError( |
| 82 | + f"ROADMAP.md belongs in your Hermes project workspace, not the DietCode plugin tree: {root}. " |
| 83 | + "Set kanban.workspace or HERMES_KANBAN_WORKSPACE to your project root." |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +def _candidate_from_env() -> tuple[Optional[str], str]: |
| 88 | + for key in ( |
| 89 | + "HERMES_KANBAN_WORKSPACE", |
| 90 | + "JOYZONING_WORKSPACE_ROOT", |
| 91 | + "DIETCODE_WORKSPACE_ROOT", |
| 92 | + ): |
| 93 | + val = os.environ.get(key, "").strip() |
| 94 | + if not val: |
| 95 | + continue |
| 96 | + root = str(Path(val).expanduser().resolve()) |
| 97 | + try: |
| 98 | + from plugins.dietcode.lib.kernel_workspace import is_quarantined_root |
| 99 | + |
| 100 | + if is_quarantined_root(root): |
| 101 | + continue |
| 102 | + except ImportError: |
| 103 | + pass |
| 104 | + return root, key |
| 105 | + return None, "env:unset" |
| 106 | + |
| 107 | + |
| 108 | +def _candidate_from_kanban_config() -> tuple[Optional[str], str]: |
| 109 | + try: |
| 110 | + from hermes_cli.config import load_config |
| 111 | + |
| 112 | + raw = load_config() |
| 113 | + if not isinstance(raw, dict): |
| 114 | + return None, "kanban:unset" |
| 115 | + kanban = raw.get("kanban", {}) |
| 116 | + if not isinstance(kanban, dict): |
| 117 | + return None, "kanban:unset" |
| 118 | + ws = str(kanban.get("workspace") or kanban.get("workspace_root") or "").strip() |
| 119 | + if not ws: |
| 120 | + return None, "kanban:unset" |
| 121 | + root = str(Path(ws).expanduser().resolve()) |
| 122 | + try: |
| 123 | + from plugins.dietcode.lib.kernel_workspace import is_quarantined_root |
| 124 | + |
| 125 | + if is_quarantined_root(root): |
| 126 | + return None, "kanban:quarantined" |
| 127 | + except ImportError: |
| 128 | + pass |
| 129 | + return root, "kanban.workspace" |
| 130 | + except Exception: |
| 131 | + return None, "kanban:unset" |
| 132 | + |
| 133 | + |
| 134 | +def resolve_workspace(*, explicit: Optional[str] = None) -> tuple[str, str]: |
| 135 | + """Resolve the user project workspace for ROADMAP.md (never plugin/kernel trees). |
| 136 | +
|
| 137 | + Returns ``(absolute_path, resolution_source)``. |
| 138 | + """ |
72 | 139 | if explicit and str(explicit).strip(): |
73 | | - from pathlib import Path |
| 140 | + root = str(Path(explicit).expanduser().resolve()) |
| 141 | + _reject_quarantined(root) |
| 142 | + return root, "explicit" |
74 | 143 |
|
75 | | - return str(Path(explicit).expanduser().resolve()) |
| 144 | + try: |
| 145 | + from plugins.dietcode.lib.kernel_workspace import ( |
| 146 | + is_quarantined_root, |
| 147 | + resolve_workspace_root as resolve_kernel_workspace, |
| 148 | + ) |
| 149 | + |
| 150 | + report = resolve_kernel_workspace() |
| 151 | + candidate = report.resolved_workspace_root |
| 152 | + if candidate and not is_quarantined_root(candidate): |
| 153 | + return candidate, report.resolution_detail |
| 154 | + except Exception: |
| 155 | + pass |
76 | 156 |
|
77 | 157 | try: |
78 | 158 | from plugins.dietcode.lib.agent.joyzoning.jsdp_harness_client import ( |
79 | 159 | resolve_workspace_root as _jz_resolve, |
80 | 160 | ) |
81 | 161 |
|
82 | | - return _jz_resolve(explicit=None) |
| 162 | + jz_root = _jz_resolve(explicit=None) |
| 163 | + if jz_root: |
| 164 | + try: |
| 165 | + from plugins.dietcode.lib.kernel_workspace import is_quarantined_root |
| 166 | + |
| 167 | + if not is_quarantined_root(jz_root): |
| 168 | + return jz_root, "joyzoning.jsdp" |
| 169 | + except ImportError: |
| 170 | + return jz_root, "joyzoning.jsdp" |
83 | 171 | except Exception: |
84 | | - from pathlib import Path |
| 172 | + pass |
| 173 | + |
| 174 | + env_root, env_source = _candidate_from_env() |
| 175 | + if env_root: |
| 176 | + return env_root, env_source |
85 | 177 |
|
86 | | - for key in ("HERMES_KANBAN_WORKSPACE", "JOYZONING_WORKSPACE_ROOT"): |
87 | | - val = os.environ.get(key, "").strip() |
88 | | - if val: |
89 | | - return str(Path(val).expanduser().resolve()) |
90 | | - return str(Path.cwd().resolve()) |
| 178 | + kanban_root, kanban_source = _candidate_from_kanban_config() |
| 179 | + if kanban_root: |
| 180 | + return kanban_root, kanban_source |
| 181 | + |
| 182 | + cwd = str(Path.cwd().resolve()) |
| 183 | + try: |
| 184 | + from plugins.dietcode.lib.kernel_workspace import is_quarantined_root |
| 185 | + |
| 186 | + if not is_quarantined_root(cwd): |
| 187 | + return cwd, "cwd" |
| 188 | + except ImportError: |
| 189 | + return cwd, "cwd" |
| 190 | + |
| 191 | + raise RoadmapWorkspaceError( |
| 192 | + "Could not resolve a project workspace for ROADMAP.md. " |
| 193 | + "Set kanban.workspace in ~/.hermes/config.yaml or export HERMES_KANBAN_WORKSPACE " |
| 194 | + "to your project root (not ~/.hermes/plugins/dietcode)." |
| 195 | + ) |
| 196 | + |
| 197 | + |
| 198 | +def resolve_workspace_root(explicit: Optional[str] = None) -> str: |
| 199 | + """Resolve the project workspace for ROADMAP.md.""" |
| 200 | + return resolve_workspace(explicit=explicit)[0] |
0 commit comments