#21553: fix: resolve workspace template dir in bundled dist/ layout
agents
size: S
trusted-contributor
Cluster:
Workspace Path Fixes
## Problem
Cron isolated sessions fail with:
```
Error: Missing workspace template: TOOLS.md (/usr/lib/node_modules/docs/reference/templates/TOOLS.md).
Ensure docs/reference/templates are packaged.
```
The template file exists at `/usr/lib/node_modules/openclaw/docs/reference/templates/TOOLS.md`, but the fallback path resolves incorrectly.
## Root Cause
`FALLBACK_TEMPLATE_DIR` in `workspace-templates.ts` uses a hardcoded relative path:
```typescript
path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../docs/reference/templates')
```
This assumes the source layout where the file lives at `src/agents/workspace-templates.ts` (2 levels deep). In the bundled `dist/` layout (1 level deep), this resolves to:
- Expected: `/usr/lib/node_modules/openclaw/docs/reference/templates`
- Actual: `/usr/lib/node_modules/docs/reference/templates` ❌
When `resolveOpenClawPackageRoot()` also returns `null` (because cwd is the workspace dir in cron isolated sessions, not the package dir), no candidate path is valid.
## Fix
Replace the hardcoded `../../` relative path with `findTemplateDirFromAncestors()` — a small helper that walks up from `import.meta.url` to find the nearest parent directory containing `docs/reference/templates/`. This works for both:
- Source layout: `src/agents/` (2 levels up)
- Bundled layout: `dist/` (1 level up)
- Any future restructuring
## Changes
- **`src/agents/workspace-templates.ts`** — Replace hardcoded relative fallback with ancestor directory walk
- **`src/agents/workspace-templates.ancestor-walk.test.ts`** — 3 test cases:
- Module nested 1 level deep (`dist/`) ✅
- Module nested 2 levels deep (`src/agents/`) ✅
- cwd is workspace dir, not package root (cron isolated session scenario) ✅
## Testing
```
✓ finds templates when module is nested one level deep (dist/)
✓ finds templates when module is nested two levels deep (src/agents/)
✓ finds templates when cwd is the workspace (not package root)
Test Files 1 passed (1)
Tests 3 passed (3)
```
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Replaces hardcoded relative path fallback with ancestor directory walk to correctly resolve workspace template directory in both source (`src/agents/`) and bundled (`dist/`) layouts.
- Introduces `findTemplateDirFromAncestors()` helper that walks up from `import.meta.url` to find `docs/reference/templates/`
- Fixes cron isolated session failures where the old hardcoded `../../` path resolved to incorrect location in bundled layout
- Adds comprehensive test coverage for 1-level deep (dist), 2-level deep (src/agents), and cwd-mismatch scenarios
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- The fix is well-designed with proper fallback handling, comprehensive test coverage for all scenarios, and directly addresses the root cause. The ancestor walk approach is more robust than hardcoded relative paths and handles both current and future directory structures
- No files require special attention
<sub>Last reviewed commit: f8cab06</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#17757: fix(agents): resolve relative workspace paths against state dir, no...
by Phineas1500 · 2026-02-16
78.0%
#12368: fix(cron): respect OPENCLAW_STATE_DIR for cron store path
by ComBba · 2026-02-09
77.7%
#11529: fix(wizard): strip shell-style backslash escapes from workspace paths
by mcaxtr · 2026-02-07
77.1%
#2884: fix: Create memory directory and symlink identity files during work...
by webdevtodayjason · 2026-01-27
76.1%
#23085: fix(workspace): respect OPENCLAW_STATE_DIR for workspace paths, fix...
by charojo · 2026-02-22
76.0%
#13577: fix(onboard): validate workspace directory before setup steps
by mcaxtr · 2026-02-10
75.3%
#17815: fix: use $HOME as cwd for global update to prevent path-dedot panic
by frankekn · 2026-02-16
75.0%
#13497: fix(docker): normalize workspace paths for container compatibility
by janckerchen · 2026-02-10
74.8%
#2753: fix: Support nested packages in pnpm-workspace.yaml (#2745)
by HarshalJain-cs · 2026-01-27
74.7%
#10589: fix: chrome extension install fails in bundled dist layout
by joetomasone · 2026-02-06
74.6%