Personal_AI_Infrastructure icon indicating copy to clipboard operation
Personal_AI_Infrastructure copied to clipboard

WorkCompletionLearning hook never generates learnings (2 bugs)

Open jacobo-ortiz opened this issue 8 months ago • 2 comments

Summary

hooks/WorkCompletionLearning.hook.ts (v4.0.3) has two bugs that prevent it from ever generating learning files in MEMORY/LEARNING/. The hook silently exits on every SessionEnd, meaning the WORK→LEARNING bridge has never functioned.

Bug 1: current-work.json dependency doesn't exist

Location: findStateFile() (line ~64)

The function looks for MEMORY/STATE/current-work.json or current-work-{sessionId}.json:

function findStateFile(sessionId?: string): string | null {
  if (sessionId) {
    const scoped = join(STATE_DIR, `current-work-${sessionId}.json`);
    if (existsSync(scoped)) return scoped;
  }
  const legacy = join(STATE_DIR, 'current-work.json');
  if (existsSync(legacy)) return legacy;
  return null;
}

Problem: Nothing in PAI or Claude Code creates either of these files. The hook always exits with "No active work session" on line 276.

Fix: Replace with a lookup in MEMORY/STATE/work.json (populated by PRDSync hook), matching session_id from stdin to sessionUUID in work.json entries.

Bug 2: Section header mismatch

Location: ISC extraction regex (line ~326)

const iscMatch = prdContent.match(/## IDEAL STATE CRITERIA[\s\S]*?(?=\n## |$)/);

Problem: PAI/PRDFORMAT.md v2.0 defines the section as ## Criteria, not ## IDEAL STATE CRITERIA. The regex never matches, so ISC content is never extracted.

Fix: Update regex to accept both formats:

const iscMatch = prdContent.match(/## (?:IDEAL STATE CRITERIA|Criteria)[\s\S]*?(?=\n## |$)/);

Additional: Field name mismatch

PRD frontmatter uses task: but WorkMeta interface expects title:. Similarly slug:→id: and started:→created_at:. The parseYaml() output needs field mapping for v4.0 PRDs.

Impact

  • MEMORY/LEARNING/SYSTEM/ and MEMORY/LEARNING/ALGORITHM/ remain permanently empty
  • The learning loop described in MEMORYSYSTEM.md never activates
  • 49 sessions tested, 0 learning files generated

Verification

Both bugs confirmed identical in upstream repo (Releases/v4.0.3/.claude/hooks/WorkCompletionLearning.hook.ts) and local install. Fix applied and tested locally — hook now correctly generates .md learning files with ISC content.

Environment

  • PAI v4.0.3
  • Claude Code on WSL2 (Linux 6.6.87.2)
  • Bun runtime

jacobo-ortiz avatar Mar 19 '26 04:03 jacobo-ortiz

Related Issues

After creating this issue I reviewed existing reports. These bugs have a history:

  • #648 / #668 (closed, not merged) — reported SessionEnd hooks never executing in v2.5/v3.0. Some of those bugs (stdin handling, field name work_dir→session_dir) were partially fixed in v4.0.3, but the current-work.json dependency was not.
  • #850 (closed as fixed in v4.0.3) — reported learning pipeline dead ends. Daniel confirmed and said it would be fixed in v4.0.3, but the fix was incomplete — the two bugs in this issue survived.
  • #966 (open PR) — proposes closing the feedback loop with rating-weighted readback. Complementary to this fix — #966 improves the read path, this issue fixes the write path.
  • #773 (closed) — reported static Insights placeholder. Related but different — that's about content quality, this is about the hook never executing at all.

The two bugs reported here (current-work.json dependency on a file nothing creates + ## IDEAL STATE CRITERIA regex vs ## Criteria in PRDFORMAT.md v2.0) are specific to v4.0.3 and were not covered by previous fixes.

jacobo-ortiz avatar Mar 19 '26 04:03 jacobo-ortiz

Excellent diagnosis — the big one (the never-written current-work.json dependency) was fixed and the hook now reads the work.json registry, so the WORK→LEARNING bridge actually runs. Your section-header catch was still live against the current ## Criteria format; we're landing the regex alternation in source. Thanks for tracing both all the way down.

danielmiessler avatar Jul 07 '26 18:07 danielmiessler