Skip to content

refactor(logs): one run-log module — parseLogFile, listLogFiles, resolveLogDir, formatDuration defined once #1471

Closed

Description

@admarble

Epic: graph-2026-10-consolidation Tier: mechanical Execution: sequant_run

Problem

Five run-log helpers are defined two or three times each in non-test src/: parseLogFile (byte-identical in commands/logs.ts, commands/stats.ts, mcp/tools/logs.ts), listLogFiles (identical in logs.ts, stats.ts), formatDuration (identical in logs.ts, stats.ts, lib/workflow/phase-executor.ts), and the log-directory resolver under two names, resolveLogPath (logs.ts, stats.ts, mcp/tools/logs.ts) and resolveLogDir (lib/merge-check/index.ts, mcp/tools/run.ts), all with the same contract: expand ~ in a custom path, else the project log dir if it exists, else the global one. The MCP copies take no custom path. accretion dups on HEAD (ad71b341) reports all five names (accretion-audit, dogfood/baseline-sequant.md).

Acceptance Criteria

  • AC-1: Each of parseLogFile, listLogFiles, resolveLogDir and formatDuration has exactly one definition in non-test src/, and resolveLogPath has none. Evidence: for s in parseLogFile listLogFiles resolveLogDir formatDuration; do rg -l "function $s\(" src -g '!*.test.ts' | wc -l; done; rg -l 'function resolveLogPath\(' src -g '!*.test.ts' | wc -l → 1 1 1 1 then 0.
  • AC-2: The shared module keeps every copy's behaviour: tilde expansion of a custom path, project-over-global precedence when .sequant/logs exists, global fallback when it does not, null from parseLogFile on unreadable or invalid JSON, and formatDuration(65) → 1m 5s. Evidence: npx vitest run src/lib/run-logs.test.ts → ≥ 5 passed, 0 failed.
  • AC-3: Every former caller compiles against the shared module and the existing command tests still pass. Evidence: npm run build && npx vitest run src/commands/stats.test.ts src/commands/stats-source.test.ts src/commands/stats-failure-categories.test.ts src/mcp/tools/run.test.ts src/lib/merge-check/watch.test.ts src/lib/workflow/phase-executor.test.ts → 0 failed.
  • AC-4: The detector no longer reports the five names on this branch (must run against the real dependency, the accretion CLI on this machine). Evidence: ACCRETION_HOME=$(mktemp -d) node ~/Projects/accretion-audit/dist/cli.js dups --repo "$PWD" --json | node -e "let s='';process.stdin.on('data',d=>s+=d).on('end',()=>{const n=new Set(JSON.parse(s).groups.map(g=>g.name));const hit=['parseLogFile','listLogFiles','resolveLogDir','resolveLogPath','formatDuration'].filter(x=>n.has(x));console.log(hit.join(',')||'none');process.exit(hit.length?1:0)})" → prints none, exit 0.

Prior fixes / producers

May touch

src/lib/run-logs.ts (new) and its test, src/lib/cli-ui/format.ts (add formatDuration), src/commands/logs.ts, src/commands/stats.ts, src/mcp/tools/logs.ts, src/mcp/tools/run.ts, src/lib/merge-check/index.ts, src/lib/workflow/phase-executor.ts, their tests, the changelog entry. Serialize with #1472 (mcp/tools/run.ts) and #1473 (cli-ui/format.ts): this node first (graph D-6).

Non-Goals

  • Changing any output format, log path or precedence (I-3: move and delete only).
  • formatElapsedTime in cli-ui/format.ts (different contract, §3).
  • Any edit under plugin/, .claude/skills/ or templates/skills/ (I-1).

Activity

  1. added
    refactor
    graph-2026-10-consolidation
    on Oct 16, 2026
  2. added a commit that references this issue on Oct 16, 2026
  3. admarble commented on Oct 19, 2026

    @admarble
    ContributorAuthor

    Runner context, 2026-10-19, before launch (W1 of graph-2026-10-consolidation, with #1474; stop-at-PR).

    Changed since filing (10-16): two of this issue's files have moved.

    For every phase, and the qa phase by name: run the full suite at most once, in the foreground, under timeout, after a WIP commit. Each MCP-dispatched phase has a 30-minute wall. Prefer the targeted npx vitest run <files> for the ACs.
    CHANGELOG: add [Unreleased] lines, not changelog.d/ fragments. #1370 has not merged.
    Freeze: nothing under plugin/, .claude/skills/ or templates/skills/.

  4. admarble commented on Oct 19, 2026

    @admarble
    ContributorAuthor

    Complexity: Standard (4 ACs, 7 directories)

    AC Quality Check

    All four ACs carry command-shaped Evidence:. Checked against base (8d1895c):

    • AC-1 fails today (3/2/2/3 definitions, resolveLogPath 3) — not vacuous.
    • AC-2 fails today (src/lib/run-logs.test.ts does not exist).
    • AC-3 passes today on unchanged code (existing tests) — it is a regression guard, not a gate on the work. Acceptable: it gates "nothing broke", and AC-1 gates "work done".
    • AC-4 fails today (accretion reports the five names).
    • Note: AC-2's "≥ 5 passed" is satisfied by the tests this issue adds. Cases to write: tilde expansion, project precedence, global fallback, parseLogFile unreadable → null, invalid JSON → null, formatDuration(65).

    Scope Assessment

    Non-Goals: present (no format/path/precedence changes; formatElapsedTime untouched; nothing under plugin/, .claude/skills/, templates/skills/).

    Metric Value Status
    Feature count 1 ✅
    AC items 4 ✅
    Directory spread 7 ⚠️ (mechanical move/delete)

    Verdict: ✅ SCOPE_OK

    Acceptance Criteria

    AC-1: One definition each; resolveLogPath gone

    Verification: Integration Test (the issue's rg loop)
    Scenario: Given the refactor → When the rg loop runs → Then 1 1 1 1 then 0
    Assumptions: a export { formatDuration } from re-export is not matched by function formatDuration\(.

    AC-2: Shared module preserves behaviour

    Verification: Unit Test (src/lib/run-logs.test.ts, real tmpdir, no fs mock)
    Scenario: Given custom ~/x → tilde expanded; .sequant/logs exists → project wins; absent → global; bad JSON/unreadable → null; formatDuration(65) → 1m 5s.

    AC-3: Callers compile, existing tests pass

    Verification: Integration Test (npm run build + the listed vitest files)

    AC-4: Detector reports none of the five names

    Verification: Manual/CLI run against the real accretion CLI at ~/Projects/accretion-audit/dist/cli.js.

    Implementation Plan

    Phase 1: Shared module

    1. Create src/lib/run-logs.ts exporting resolveLogDir(customPath?), listLogFiles(logDir), parseLogFile(filePath). Bodies are copied verbatim from src/commands/logs.ts:38-85 (the superset signature; the MCP copies are the same minus customPath).
    2. Add formatDuration to src/lib/cli-ui/format.ts (bodies in commands/logs.ts:90, commands/stats.ts:250, phase-executor.ts:818 are identical). Keep formatElapsedTime untouched.
    3. Write src/lib/run-logs.test.ts (≥ 5 cases from AC-2). Name it so it matches the vitest project that gives it a >5s timeout only if it needs it; it is fs-light, so the default is fine.

    Phase 2: Migrate callers (delete local copies, import shared)

    1. commands/logs.ts, commands/stats.ts: delete the 4 local helpers each, rename resolveLogPath( call sites to resolveLogDir( (stats.ts 1192/1238/1269/1307, logs.ts 341).
    2. mcp/tools/logs.ts: delete resolveLogPath/parseLogFile, import shared (call resolveLogDir()); drop now-unused LOG_PATHS/RunLogSchema/os imports.
    3. mcp/tools/run.ts:149: delete local resolveLogDir, import shared. Keep fix(mcp): sequant_run reports another run's issues and verdict when its own run log is missing — a runId lookup miss falls back to the time window #1515's runId-miss → time-window fallback untouched (only the resolver moves).
    4. lib/merge-check/index.ts:44: it is an exported function (commands/merge.ts:23 imports it). Delete the body and re-export from run-logs.ts (export { resolveLogDir } from "../run-logs.js") or repoint merge.ts; prefer repointing so the module has no pass-through. Keep fix(merge): sequant merge --check hard-codes origin/main in four places, so a master-default repo gets empty diffs and a clean report #1484's base-ref behaviour (it is elsewhere in this file; do not touch).
    5. lib/workflow/phase-executor.ts:818: delete the function and export { formatDuration } from "../cli-ui/format.js" — phase-executor.test.ts:35 and commands/run-compat.ts:11 import it from here. Re-export keeps both compiling; a follow-up can repoint them.
    6. CHANGELOG: [Unreleased] line (no changelog.d/ fragment, per runner note).

    Design Review

    1. Where does this belong? src/lib/run-logs.ts, next to lib/workflow/run-log-schema.ts which owns LOG_PATHS/RunLogSchema; formatDuration joins the formatters in cli-ui/format.ts.
    2. Simplest approach? Move-and-delete with verbatim bodies (I-3). Rejected: adding the helpers to run-log-schema.ts (mixes IO into a schema module) and a pass-through re-export in merge-check.
    3. Pattern / re-patch count? Copy-per-feature; prior fixes touching these symbols are listed in the issue (Skill output reliability: validation layers, schemas, and structured logging #22…feat(merge): --watch — wait for CI checks to finish, then run merge-check and report (kills the merge-after-green polling loop) #818, well over 2). This issue is the class fix — one producer — so it is not a "deliberately local" case.
    4. Reviewer challenge: "Why resolveLogDir and not resolveLogPath?" — the name that two of the call sites outside the CLI already use and that describes a directory; AC-1 mandates it.
    5. Producers/consumers (whole-repo grep, src): producers = the 5 resolver copies, 3 parse, 2 list, 3 duration. Consumers: commands/logs.ts, commands/stats.ts, mcp/tools/logs.ts, mcp/tools/run.ts (174, 197), merge-check/index.ts:281, commands/merge.ts (79, 124), commands/run-compat.ts:11, phase-executor.test.ts. Tests that mock fs.existsSync (mcp/tools/run.test.ts:39) still work because the shared module uses the same fs import path semantics — verify with that file after step 6 (mock is on "fs"; use import * as fs from "fs" / named existsSync from "fs", not node:fs, or the mock misses).

    Q3/Q5 do not contradict the prescribed lever: no divergence.

    Feature Quality Planning

    All standard checks pass. Notable items:

    • resolveLogDir copies agree: merge-check/index.ts:44 and mcp/tools/run.ts:149 differ only in customPath support and in os/homedir import; precedence (project → global-if-exists → project) is identical to the CLI copies. The runner's "stop if they disagree" condition does not trigger.
    • Tilde expansion uses .replace("~", …) (first occurrence, any position). Preserve it exactly — non-goal to fix.
    • fs mock-hazard above is the main regression risk for AC-3.

    Open Questions

    1. Repoint run-compat.ts/phase-executor.test.ts away from the phase-executor re-export? — Recommendation: no, keep re-export (smaller diff). Impact: one re-export line stays.

    Recommended Workflow

    Phases: exec → qa
    Quality Loop: disabled
    Reasoning: Mechanical move/delete with one new unit test file; AC-2 declares unit-test evidence, but the stubs are fully specified above, so testgen adds nothing.

    Label Review

    Current: refactor, graph-2026-10-consolidation
    Recommended: + planned
    Quality Loop: Won't auto-enable (SCOPE_OK).

  5. added
    planned
    on Oct 19, 2026
  6. admarble commented on Oct 19, 2026

    @admarble
    ContributorAuthor

    QA Verdict: NEEDS_VERIFICATION

    AC coverage: 4/4 met

    Suggestions:

    • src/lib/workflow/phase-executor.ts:60-61: the export { formatDuration } … line sits in the middle of the import block, after a blank line. It would read better after the last import.
    • scripts/analytics/analyze-runs.ts:496 still has its own formatDuration. It is outside src/, so AC-1 doesn't cover it, and the detector doesn't report it. I'm noting it, not counting it as a gap.

    Needs human review:

    • Required CI check test still pending on HEAD d07385d; re-run QA after it reports
  7. admarble commented on Oct 19, 2026

    @admarble
    ContributorAuthor

    Execution Verification (runner, 2026-10-19), PR #1547 @ d07385d

    QA returned NEEDS_VERIFICATION, with one gap: the required test check was still pending. The runner re-ran every Evidence command verbatim in the worktree:

    AC Result
    AC-1 1 1 1 1 then 0
    AC-2 src/lib/run-logs.test.ts: 7 passed, 0 failed
    AC-3 npm run build exit 0; the six listed test files: 381 passed, 0 failed
    AC-4 accretion dups (real CLI) prints none, exit 0

    CI: test passed on d07385d, as did every other check.

    Diff review: every removed copy of resolveLogDir/resolveLogPath, listLogFiles, parseLogFile and formatDuration was identical to the shared one. In run.ts, only the resolver moved. The #1515 runId-miss fallback and #1484's base-ref handling are unchanged.

    Follow-up from the spec: point phase-executor.test.ts and commands/run-compat.ts at cli-ui/format.ts and drop the formatDuration re-export in phase-executor.ts. This is left open for the owner, because run-compat.ts is outside this issue's file scope and the re-export breaks no AC. It is a natural fit for #1473, which also edits cli-ui/format.ts.

  8. admarble commented on Oct 19, 2026

    @admarble
    ContributorAuthor

    Follow-up ledger: the formatDuration re-export cleanup is folded into #1473 (owner, 2026-10-19); see its May-touch section.

  9. added a commit that references this issue on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    graph-2026-10-consolidationplannedrefactor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions