Skip to content

fix: clean up merged config in scripts that replace the lib EXIT trap - #134

Merged
fdaviddpt merged 1 commit into
Digital-Process-Tools:mainfrom
A3IO:jaine
Jul 25, 2026
Merged

fdaviddpt merged 1 commit into
Digital-Process-Tools:mainfrom
A3IO:jaine

Conversation

@whatrwewaitingf0r

Copy link
Copy Markdown
Contributor

Summary

lib-memory-dir.sh creates ${TMPDIR}/remember-config-$$.json at source time and registers a chaining EXIT trap to remove it (it even parses trap -p EXIT specifically to avoid clobbering an existing trap). But two callers source the lib first and install their own plain trap ... EXIT afterwards — and bash keeps a single EXIT trap, so the lib's cleanup is silently discarded:

  • save-session.sh:72 — trap cleanup EXIT, and cleanup() removes only $LOCK_FILE + ${CLEANUP_FILES[@]}; the merged config is never registered there
  • run-consolidation.sh:55 — trap 'rm -f "$LOCK_FILE"' EXIT

Result: one orphaned remember-config-<pid>.json (a 3-byte {} in a default install with no config.json) per run, on every exit taken after the script's trap is installed — for save-session.sh that includes the cooldown-skip and lock-skip paths. We found ~13k stray files in $TMPDIR on one long-running machine. (Orthogonal to #125 — that affects how often these scripts are forked; this fixes the per-run cleanup.)

Changes

  • save-session.sh: register $REMEMBER_CONFIG in CLEANUP_FILES so the existing cleanup() removes it
  • run-consolidation.sh: append $REMEMBER_CONFIG to the lock-cleanup trap
  • tests/test_tmpdir_cleanup.py: regression tests running both real scripts in an isolated TMPDIR, asserting no remember-config-* survives (both fail before this fix, pass after)

Test plan

  • pytest passes locally (504 passed, 41 skipped; macOS, bash 5.3.12)
  • New behavior covered by a test (tests/test_tmpdir_cleanup.py, red before / green after)
  • Targeted manual checks: ran save-session.sh on the lock-skip and cooldown-skip paths and run-consolidation.sh on the no-staging path; no remember-config-* file remained, exit codes and logs unchanged
  • Not tested on Linux / Windows-Git-Bash

Checklist

  • Docs updated if a new config key, hook, or skill was added (README.md, config.example.json) — N/A, none added
  • Version bumped in .claude-plugin/plugin.json if shipping a release — N/A, not a release
  • Changelog entry added in README.md — not included; happy to add if you'd like it in this PR

lib-memory-dir.sh registers a chaining EXIT trap to remove its
${TMPDIR}/remember-config-$$.json, but save-session.sh and
run-consolidation.sh install their own plain 'trap ... EXIT' afterwards.
Bash keeps a single EXIT trap, so the lib's cleanup was discarded and one
merged-config file was orphaned per run — accumulating thousands of stray
files in TMPDIR on active installs.

Register $REMEMBER_CONFIG in save-session.sh's CLEANUP_FILES and append
it to run-consolidation.sh's lock-cleanup trap. Regression tests run both
scripts in an isolated TMPDIR and assert no remember-config-* survives.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a TMPDIR leak where lib-memory-dir.sh creates remember-config-<pid>.json and installs a cleanup EXIT trap, but save-session.sh and run-consolidation.sh later overwrite that trap and orphan the merged-config temp file.

Changes:

  • save-session.sh: adds $REMEMBER_CONFIG to its existing CLEANUP_FILES so the script’s cleanup() removes the merged-config temp file.
  • run-consolidation.sh: extends its lock cleanup EXIT trap to also remove $REMEMBER_CONFIG.
  • Adds tests/test_tmpdir_cleanup.py to regression-test that both scripts leave no remember-config-*.json behind in an isolated TMPDIR.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
tests/test_tmpdir_cleanup.py New regression tests that run real scripts under an isolated TMPDIR and assert no merged-config temp file remains.
scripts/save-session.sh Ensures $REMEMBER_CONFIG is included in the script’s cleanup list (but see lock-cleanup bug comment).
scripts/run-consolidation.sh Ensures $REMEMBER_CONFIG is removed by the script’s EXIT trap along with its lock file.
Comments suppressed due to low confidence (1)

scripts/save-session.sh:75

  • trap cleanup EXIT is installed before the lock acquisition check. On the lock-held early-exit path (exit 0), cleanup() currently removes $LOCK_FILE unconditionally, which can delete another process' live lock and allow concurrent runs. Since this change touches cleanup behavior (now also removing $REMEMBER_CONFIG), it’s a good opportunity to make lock removal conditional on ownership (lock file containing $$).
CLEANUP_FILES+=("$REMEMBER_CONFIG")

# Remove lock file and all accumulated temp files on exit.
cleanup() { rm -f "$LOCK_FILE" "${CLEANUP_FILES[@]}"; }
trap cleanup EXIT

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@fdaviddpt
fdaviddpt merged commit 500de24 into Digital-Process-Tools:main Jul 25, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants