Files
mksglu__context-mode/tests/session/resolve-session-path.test.ts
Mert Koseoglu da6b117a6f refactor(hooks): bind session-helpers to bundled TS — end JS reimplementation
Strategy decision (after build-pipeline audit):

  Picked Strategy A-refined — bind hooks/session-helpers.mjs to the
  already-existing esbuild output hooks/session-db.bundle.mjs (which
  is generated from src/session/db.ts by the existing 'npm run bundle'
  step in package.json). Zero new build steps, zero new shipped files,
  zero new bundle pipeline complexity.

  Strategy B (codegen) rejected: would add a custom build script with
  brittle TS-text extraction; no precedent in repo.
  Strategy C (shared .mjs source) rejected: src/ is not in package.json
  'files' array, so a new src/session/path-helpers.mjs would not ship
  to npm consumers; adding src/ to files would broaden the published
  surface unnecessarily.

What changed:

  - src/session/db.ts: new export resolveSessionPath({projectDir,
    sessionsDir, ext, suffix?}) generalises resolveSessionDbPath over
    arbitrary file extension (.db, -events.md, .cleanup). Existing
    resolveSessionDbPath now delegates to it (contract preserved).
    Optional suffix? lets the hook layer inject its marker-cached
    suffix without re-invoking git.

  - hooks/session-helpers.mjs: deleted inline hashCanonical, hashLegacy,
    migrateAndJoin, normalizeWorktreePath, gitOutput,
    getCurrentWorktreeRoot, getMainWorktreeRoot, and the case-fold
    suffix computation. Now imports them from session-db.bundle.mjs
    via pathToFileURL dynamic import (same pattern as
    session-loaders.mjs — Windows-safe, see d2be2f7). Marker-file
    cache wrapper around getWorktreeSuffix retained because it amortises
    the git subprocess cost across hook forks (no equivalent need on
    the long-lived TS server).

  - hooks/session-db.bundle.mjs / cli.bundle.mjs / server.bundle.mjs:
    regenerated by 'npm run build' (gitignored but committed per
    project convention since prepublishOnly = npm run build).

  - tests/session/resolve-session-path.test.ts: 7 new tests pinning
    the new export's contract (compose, migrate, data-loss safety,
    Linux no-op, worktree separation, explicit suffix override,
    consistency with resolveSessionDbPath).

Lines of duplicated correctness logic deleted from hooks: ~60.

Architect self-review:
  Risk 1 (top-level await blocking): hooks already await the dynamic
    import of session-helpers.mjs, so no new latency.
  Risk 2 (bundle absent in marketplace install): cloned the bundle-first
    / build-fallback pattern from session-loaders.mjs verbatim.
  Risk 3 (Windows ESM dynamic import): used pathToFileURL(...).href —
    same fix d2be2f7 applied to statusline (cross-OS reference).
  Risk 4 (worktree-marker perf regression): preserved via the optional
    suffix? param on resolveSessionPath.
  Risk 5 (contract break): resolveSessionDbPath signature unchanged,
    all existing tests/session/db-path-canonical-migration.test.ts
    pass (10/10).
  Verdict: ship.

Future-proofing: when round-6 case-fix lands, it edits ONE file —
src/session/db.ts. The bundle regenerates, hooks inherit it. The
'kept in sync manually since hooks run as plain .mjs' comment is
gone for good.

Tests: 17 passed (10 existing + 7 new) on the path/migration suite.
Hook+statusline+session subset: 880 passed (matches baseline; 1
remaining flake is the pre-existing kiro/integration timeout
unrelated to this change).
2026-05-10 16:34:45 +03:00

7.1 KiB