Skip to content

fix: graceful error handling for /undo#1

Open
wjiuxing wants to merge 4 commits into
mainfrom
fix/undo-error-handling
Open

fix: graceful error handling for /undo#1
wjiuxing wants to merge 4 commits into
mainfrom
fix/undo-error-handling

Conversation

@wjiuxing

Copy link
Copy Markdown
Owner

Summary

Fixes clopca#17 — error logs appearing after /undo in opencode.

Root Cause

open-mem's hooks assume "write = permanent" with no undo/cleanup path. After /undo, opencode deletes messages but continues triggering hooks (e.g. session.idle), causing open-mem to error when referencing deleted data.

Changes

Logger utility (src/utils/logger.ts)

  • New Logger class with level-gated output (debugconsole.log, warnconsole.warn, errorconsole.error)
  • Configurable via OPEN_MEM_LOG_LEVEL env var (default: warn)

Error handling (all hooks)

  • Replace console.error with Logger instance — errors now properly categorized
  • Hooks no longer crash on missing/deleted data — gracefully degrade

Cleanup mechanism (src/db/pending.ts)

  • Add deleteBySessionId() to PendingMessageRepository — removes pending/failed messages for a session
  • Wired into session lifecycle cleanup path

Tests (970 pass, 0 fail)

  • Logger test suite: 121 lines covering all levels, routing, and configuration
  • deleteBySessionId tests: 3 tests covering status filtering, cross-session isolation
  • Updated all hook tests to pass Logger parameter (now required)

Files Changed (18 files, +451/-34)

Area Files What
Core src/utils/logger.ts New Logger utility
Core src/db/pending.ts deleteBySessionId method
Hooks chat-capture.ts, compaction.ts, context-inject.ts, session-events.ts, tool-capture.ts Replace console.error with Logger
Wiring src/index.ts, src/platform-worker.ts, src/adapters/platform/runtime.ts Logger instantiation + dependency injection
Tests 6 test files Logger tests + updated hook tests

Verification

  • ✅ TypeScript: tsc --noEmit — 0 errors
  • ✅ Tests: 970 pass, 0 fail across 80 files
  • ✅ Logger routes: debug/info → stdout, warn → stderr (distinct), error → stderr

jiuxingwang added 3 commits May 14, 2026 20:12
Replace raw console.error in all hooks with a configurable Logger
that respects config.logLevel (default: warn). Hook errors are
classified as warn (operational) or debug (cosmetic), so at default
settings most noise disappears after /undo.

- Add Logger utility (src/utils/logger.ts) with debug/info/warn/error
- Add PendingMessageRepository.deleteBySessionId() for future undo cleanup
- Wire logger through all 5 hooks and PlatformIngestionRuntime
- Update all affected tests

Closes clopca#17
- Logger: debug/info → console.log (stdout), warn → console.warn, error → console.error
- Tests: pass Logger to all hook call sites (was made required)
- Tests: capture console.log/warn/error for Logger verification
- Fix extra bracket in crud.test.ts
Address review feedback — deleteBySessionId is now called in:
- session.created: cleanup stale pending/failed from previous session lifecycle
- session.idle: cleanup orphaned messages (covers /undo scenario)
- session.completed/ended: final cleanup after processing

Also updated session-events test mock to include deleteBySessionId.
@wjiuxing
wjiuxing force-pushed the fix/undo-error-handling branch from ab933c7 to 85d2b39 Compare May 15, 2026 02:40
- Add message_id column to observations table (migration v2)
- Store messageID from chat.message hook in observations
- Add ObservationRepository.softDeleteByMessageId() for undo handling
- Handle message.removed events in createEventHandler to soft-delete
  observations tied to the removed message
- Remove deleteBySessionId from session.idle (message.removed is more
  precise, avoids aggressive pending message cleanup)
- Update schema idempotency test for 2 migrations
- Add 8 tests for message.removed handling (repository + event handler)
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.

[Bug] error logs after /undo

1 participant