Skip to content

fix(shell): make mktemp templates work on BSD mktemp (macOS)#30

Closed
josemoreno801-netizen wants to merge 1 commit intoDigital-Process-Tools:mainfrom
josemoreno801-netizen:fix/mktemp-bsd-macos
Closed

fix(shell): make mktemp templates work on BSD mktemp (macOS)#30
josemoreno801-netizen wants to merge 1 commit intoDigital-Process-Tools:mainfrom
josemoreno801-netizen:fix/mktemp-bsd-macos

Conversation

@josemoreno801-netizen
Copy link
Copy Markdown
Contributor

Symptom

On macOS, bash scripts/save-session.sh --force fails immediately with:

mkstemp: File exists

…aborting the entire save pipeline. Same failure mode in scripts/run-tests.sh section 5.

Root cause

GNU mktemp (Linux) tolerates characters after the XXXXXX suffix and silently treats them as a literal suffix on the generated name. BSD mktemp (macOS default) does notman mktemp says:

The trailing 'Xs' are replaced with the current process number and/or a unique letter combination. The Xs must occur at the end of the template.

Templates like mktemp /tmp/remember-prompt-XXXXXX.txt are therefore invalid on BSD; mktemp errors out with mkstemp: File exists.

Fix

11 templates across two scripts had characters after XXXXXX:

  • scripts/save-session.sh — 5 sites (TMP_LAST_ENTRY, TMP_PROMPT, HAIKU_STDERR, NDC_PROMPT, NDC_ERR)
  • scripts/run-tests.sh — 6 sites (TMP_POS, TMP_EXTRACT_F, TMP_LAST_F, TMP_PROMPT_F, TMP_MEM, TMP_NDC)

All XXXXXX.{txt,json,md}XXXXXX. The temp files are short-lived (cleaned up via trap) and never opened by extension-sensitive tools, so dropping the suffix has no functional impact on Linux; it makes the templates valid on BSD.

Verification

  • macOS: bash scripts/save-session.sh --force now completes the save pipeline end-to-end.
  • macOS: bash scripts/run-tests.sh no longer fails section 5 sub-tests on mktemp.
  • Linux behavior unchanged (templates still produce the same per-call unique name; suffix never carried meaning).

Conflict note for maintainer

PR #29 (Windows session-dir slug + Python detection) also modifies scripts/save-session.sh and scripts/run-tests.sh. The two PRs touch overlapping mktemp lines but with different intent. Suggested merge order:

  1. Merge this PR first (small, surgical, BSD portability).
  2. Ask Windows: fix session-dir slug mismatch and Python detection #29 to rebase on the new main (mktemp lines now end at XXXXXX); the rebase should be mechanical.

Happy to rebase if a different order is preferred.

BSD mktemp (shipped on macOS) requires XXXXXX to be at the very end
of the template — any trailing characters (`.txt`, `.json`, `.md`)
are not treated as a suffix. The call either fails with
`mkstemp: File exists` once the literal path has been materialized
once, or returns the literal unrandomized path, defeating mktemp's
purpose. GNU mktemp accepts the suffix, which is why this has been
dormant in upstream on Linux CI and only surfaces on macOS when
`save-session.sh --force` bypasses the min-messages threshold.

Drop the trailing extensions from all 10 mktemp call sites:
- scripts/save-session.sh (5): TMP_LAST_ENTRY, TMP_PROMPT,
  HAIKU_STDERR, NDC_PROMPT, NDC_ERR
- scripts/run-tests.sh (5): TMP_POS, TMP_EXTRACT_F, TMP_LAST_F,
  TMP_PROMPT_F, TMP_MEM, TMP_NDC

The files are only read back through the variable, so the lost
extension has no functional impact. Verified end-to-end on macOS:
`bash scripts/save-session.sh --force` now succeeds; pytest still
199/199 @ 99% coverage.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
fdaviddpt pushed a commit that referenced this pull request Apr 25, 2026
BSD mktemp (macOS) treats characters after XXXXXX as literal — it
creates a file named exactly 'foo-XXXXXX.txt' with no randomization.
The first call succeeds silently, the second fails with 'File exists'.
This is both a collision bug and a security issue (predictable names).

Remove .txt/.json/.md extensions from all 11 mktemp calls in
save-session.sh (5) and run-tests.sh (6). Added tests: BSD mktemp
reproduction, guard against future extensions in scripts.

Inspired by josemoreno801-netizen's PR #30.
Closes #30 scope.

Co-Authored-By: Max <noreply>
fdaviddpt added a commit that referenced this pull request Apr 25, 2026
…, #32, #24, #30)

fix: centralize .remember/ bootstrap + portability fixes (Windows, macOS, Haiku)
@fdaviddpt
Copy link
Copy Markdown
Contributor

This was addressed in #33 commit 4 which takes the same approach (removing extensions after XXXXXX) plus adds a guard test to prevent future regressions.

Thank you @josemoreno801-netizen for the sharp macOS debugging! The BSD mktemp behavior is particularly insidious — it silently creates a non-random filename on the first call, then fails on the second. Your clear symptom description ('mkstemp: File exists') made reproduction straightforward. You've also got PRs #25 and #26 still open — we'll review those next. Thanks for the sustained contributions to the project!

@josemoreno801-netizen josemoreno801-netizen deleted the fix/mktemp-bsd-macos branch April 27, 2026 03:20
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.

2 participants