Files
backnotprop__plannotator/packages/shared/improvement-hooks.ts
Hrand Liu e0aee7451b feat: add PLANNOTATOR_DATA_DIR env var to customize data directory (#795)
* feat: add PLANNOTATOR_DATA_DIR env var to customize data directory

* fix: update missed hardcoded paths to use PLANNOTATOR_DATA_DIR

OpenCode plugin and VS Code extension still used hardcoded
~/.plannotator paths, causing the IPC registry and plan backing
file to diverge from the server when PLANNOTATOR_DATA_DIR is set.

Also exports data-dir from @plannotator/shared and documents the
new env var in AGENTS.md.

Co-authored-by: Chris Werner Rau <14326070+cwrau@users.noreply.github.com>
Co-authored-by: João O. Santos <34689526+Joao-O-Santos@users.noreply.github.com>

* fix: vendor data-dir.ts into Pi extension and rewrite imports

The Pi extension copies shared/server modules into generated/ at
build time. Without vendoring data-dir.ts and rewriting the
parent-relative imports, typecheck fails on all generated files
that import getPlannotatorDataDir.

* refactor: eliminate duplicated data-dir logic and clean up call sites

- VS Code extension: replace inlined getPlannotatorDataDir() copy with
  import from the canonical packages/shared/data-dir.ts (esbuild bundles
  it, so no runtime dependency needed)
- storage.ts: hoist repeated getPlannotatorDataDir() calls to a
  module-level DATA_DIR constant, matching the pattern config.ts uses
- data-dir.ts: remove inaccurate docstring claim about relative path
  resolution (the code does not call resolve())
- improvement-hooks.ts: hoist to DATA_DIR constant, clarify comments
  on the two-level hook lookup (hooks/ subdir vs root fallback)

* fix: resolve relative PLANNOTATOR_DATA_DIR to absolute path

A relative value like ./data would break readArchivedPlan's path
traversal guard, which compares a resolve()'d absolute path against
the still-relative planDir prefix. Always return an absolute path
so all callers get consistent path shapes.

* fix: use @plannotator/shared/data-dir imports in server package

Switch from relative ../shared/data-dir imports to the package
export, matching the convention every other server file follows.
Update Pi vendor script sed rules to match the new import style.

* fix: use package imports in server and respect data dir in compound skill

Server modules: switch from relative ../shared/data-dir imports to
@plannotator/shared/data-dir, matching the convention every other
server file follows. Update Pi vendor script sed rules to match.

Compound skill: update hardcoded ~/.plannotator paths to check
PLANNOTATOR_DATA_DIR first, so the skill reads plans and writes
the improvement hook to the correct location when users set a
custom data directory.

Co-authored-by: Chris Werner Rau <14326070+cwrau@users.noreply.github.com>
Co-authored-by: João O. Santos <34689526+Joao-O-Santos@users.noreply.github.com>

* fix: remove remaining hardcoded ~/.plannotator assumptions

- Settings UI: replace hardcoded path in label and placeholder with
  generic text that doesn't assume a specific data directory
- quickLabels: update agent tip to reference PLANNOTATOR_DATA_DIR
  so the agent checks the correct plans directory
- codex-review: hoist getPlannotatorDataDir() to module-level DATA_DIR
  constant, eliminating redundant per-call resolution in debugLog()
- Tests: make submit-plan and storage tests resilient to
  PLANNOTATOR_DATA_DIR being set in the environment
- Install scripts (sh, ps1, cmd): check PLANNOTATOR_DATA_DIR before
  falling back to ~/.plannotator for config.json attestation lookup

* fix: expand tilde in install script and update test assertions

install.sh: PLANNOTATOR_DATA_DIR set to ~/... stays literal inside
double quotes, so the config file check silently failed. Add case
statement to expand ~ the same way the runtime data-dir.ts does.

install.test.ts: update three assertions that checked for hardcoded
~/.plannotator paths — now verify PLANNOTATOR_DATA_DIR awareness
instead.

* docs: add PLANNOTATOR_DATA_DIR to env var reference with VS Code note

Document the new env var on the marketing site's environment
variables reference page. Include a footnote about ensuring
VS Code inherits the variable when launched from the Dock.

---------

Co-authored-by: Michael Ramos <mdramos8@gmail.com>
Co-authored-by: Chris Werner Rau <14326070+cwrau@users.noreply.github.com>
Co-authored-by: João O. Santos <34689526+Joao-O-Santos@users.noreply.github.com>
2026-05-26 14:56:07 -07:00

116 lines
3.4 KiB
TypeScript

/**
* Improvement Hook Reader
*
* Reads improvement hook files from ~/.plannotator/hooks/.
* Falls back to the legacy path (~/.plannotator/) when the new-path
* file is absent, for compatibility with files written before the
* path migration. If the new-path file exists but is invalid (empty,
* oversized, not a regular file), the legacy path is NOT consulted —
* this prevents resurrecting stale instructions.
*
* Runtime-agnostic: uses only node:fs, node:path, node:os.
*
* Security model:
* - Hardcoded base paths (no user input determines file path)
* - KNOWN_HOOKS allowlist (only pre-registered relative paths)
* - Size cap to prevent runaway context injection
* - Same trust model as ~/.plannotator/config.json
*/
import { join } from "path";
import { readFileSync, statSync } from "fs";
import { getPlannotatorDataDir } from "./data-dir";
const DATA_DIR = getPlannotatorDataDir();
/** Hooks subdirectory (preferred location) */
const HOOKS_BASE_DIR = join(DATA_DIR, "hooks");
/** Fallback: hooks placed directly in the data dir (pre-hooks-subdir layout) */
const LEGACY_BASE_DIR = DATA_DIR;
/** Maximum file size to read (50 KB) */
const MAX_FILE_SIZE = 50 * 1024;
/**
* Known improvement hook file paths, keyed by hook name.
* `path` is relative to HOOKS_BASE_DIR (~/.plannotator/hooks/).
* `legacyPath` is relative to LEGACY_BASE_DIR (~/.plannotator/).
*/
const KNOWN_HOOKS = {
"enterplanmode-improve": {
path: "compound/enterplanmode-improve-hook.txt",
legacyPath: "compound/enterplanmode-improve-hook.txt",
},
} as const;
export type ImprovementHookName = keyof typeof KNOWN_HOOKS;
export function getImprovementHookExpectedPath(
hookName: ImprovementHookName,
): string | null {
const entry = KNOWN_HOOKS[hookName];
if (!entry) return null;
return join(HOOKS_BASE_DIR, entry.path);
}
export interface ImprovementHookResult {
content: string;
hookName: ImprovementHookName;
filePath: string;
}
/** Check whether a path exists on disk (any file type). */
function fileExists(path: string): boolean {
try {
statSync(path);
return true;
} catch {
return false;
}
}
/** Validate and read a hook file. Returns the result, or null if invalid. */
function tryReadHookFile(
filePath: string,
hookName: ImprovementHookName,
): ImprovementHookResult | null {
try {
const stat = statSync(filePath);
if (!stat.isFile() || stat.size === 0 || stat.size > MAX_FILE_SIZE) return null;
const content = readFileSync(filePath, "utf-8").trim();
if (!content) return null;
return { content, hookName, filePath };
} catch {
return null;
}
}
/**
* Read an improvement hook file by name.
*
* Lookup order:
* 1. New path (HOOKS_BASE_DIR + path). If it exists and validates, return it.
* 2. If the new path exists but is invalid (empty, oversized, etc.), return null.
* 3. Only if the new path does not exist, try the legacy path (LEGACY_BASE_DIR + legacyPath).
*/
export function readImprovementHook(
hookName: ImprovementHookName,
): ImprovementHookResult | null {
const entry = KNOWN_HOOKS[hookName];
if (!entry) return null;
const newPath = join(HOOKS_BASE_DIR, entry.path);
// New path exists — use it exclusively (even if invalid)
if (fileExists(newPath)) {
return tryReadHookFile(newPath, hookName);
}
// New path absent — fall back to legacy path
const legacyFilePath = join(LEGACY_BASE_DIR, entry.legacyPath);
return tryReadHookFile(legacyFilePath, hookName);
}