Skip to content

fix: working memory extraction — first user message instead of last #179

@dean0x

Description

@dean0x

Problem

The stop hook's background memory updater (scripts/hooks/background-memory-update) injects the wrong messages into the Sonnet prompt. It should extract the first user prompt and the last assistant output from the session transcript, but currently extracts the last user message instead of the first.

Root Cause

Line ~100-104 in background-memory-update:

last_user=$(grep '"type":"user"' "$transcript" 2>/dev/null \
    | ... \
    | tail -1)   # ← BUG: gets last user message, should be head -1 for first

The assistant extraction (line ~106-110) correctly uses tail -1 to get the last assistant message.

Fix

Change tail -1 to head -1 on the user message extraction line. The intent is:

  • First user message: captures what the session was about (the opening request)
  • Last assistant message: captures what was accomplished (the final state)

Verification

  • Test with a real session transcript to confirm the first user message is extracted
  • Verify the assistant message extraction still gets the last one
  • Check that WORKING-MEMORY.md updates reflect the session's opening topic, not a mid-session follow-up

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions