-
Notifications
You must be signed in to change notification settings - Fork 1
feat: persistent message store with crash recovery #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
92c493a
feat: persistent message store with crash recovery (issue #8) (#2)
akurinnoy 27108d2
fix: address PR #17 review comments
akurinnoy a70e333
fix: move destructiveHint before handler in receive_messages
akurinnoy f9a556b
fix: handle flushToDisk I/O errors gracefully
akurinnoy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| apiVersion: v1 | ||
| kind: PersistentVolumeClaim | ||
| metadata: | ||
| name: che-mcp-server-data | ||
| spec: | ||
| accessModes: | ||
| - ReadWriteOnce | ||
| storageClassName: gp3-csi | ||
| resources: | ||
| requests: | ||
| storage: 1Gi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import { randomUUID } from 'node:crypto'; | ||
| import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'; | ||
| import { join } from 'node:path'; | ||
| import { DATA_DIR } from '../config.js'; | ||
|
|
||
| const inboxes: Map<string, Message[]> = new Map(); | ||
| let dataFile = ''; | ||
| let tmpFile = ''; | ||
|
|
||
| export interface Message { | ||
| message_id: string; | ||
| from: string; | ||
| to: string; | ||
| body: string; | ||
| thread_id: string; | ||
| timestamp: string; | ||
| } | ||
|
|
||
| export function initStore(dataDir: string): void { | ||
| dataFile = join(dataDir, 'messages.json'); | ||
| tmpFile = join(dataDir, 'messages.json.tmp'); | ||
| mkdirSync(dataDir, { recursive: true }); | ||
| inboxes.clear(); | ||
| // CRASH RECOVERY: promote a completed write that survived a crash | ||
| if (existsSync(tmpFile)) { | ||
| try { | ||
| JSON.parse(readFileSync(tmpFile, 'utf8')); | ||
| renameSync(tmpFile, dataFile); | ||
| } catch { | ||
| rmSync(tmpFile, { force: true }); | ||
| } | ||
| } | ||
| if (existsSync(dataFile)) { | ||
| try { | ||
| const entries = JSON.parse(readFileSync(dataFile, 'utf8')); | ||
| for (const [key, msgs] of entries) { | ||
| inboxes.set(key, msgs); | ||
| } | ||
| } catch { | ||
| // Corrupted data file — start with empty state | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export function sendMessage( | ||
| from: string, | ||
| to: string, | ||
| body: string, | ||
| thread_id?: string, | ||
| ): { message_id: string; thread_id: string } { | ||
| const message_id = randomUUID(); | ||
| const resolvedThreadId = thread_id ?? randomUUID(); | ||
|
|
||
| const message: Message = { | ||
| message_id, | ||
| from, | ||
| to, | ||
| body, | ||
| thread_id: resolvedThreadId, | ||
| timestamp: new Date().toISOString(), | ||
| }; | ||
|
|
||
| const inbox = inboxes.get(to) ?? []; | ||
| inbox.push(message); | ||
| inboxes.set(to, inbox); | ||
|
|
||
| flushToDisk(); | ||
|
|
||
| return { message_id, thread_id: resolvedThreadId }; | ||
| } | ||
|
|
||
| export function receiveMessages( | ||
| sessionId: string, | ||
| threadId?: string, | ||
| ): { messages: Message[] } { | ||
| const inbox = inboxes.get(sessionId); | ||
| if (!inbox || inbox.length === 0) { | ||
| return { messages: [] }; | ||
| } | ||
|
|
||
| if (threadId) { | ||
| const matching = inbox.filter(m => m.thread_id === threadId); | ||
| const remaining = inbox.filter(m => m.thread_id !== threadId); | ||
| if (remaining.length === 0) { | ||
| inboxes.delete(sessionId); | ||
| } else { | ||
| inboxes.set(sessionId, remaining); | ||
| } | ||
| flushToDisk(); | ||
| return { messages: matching }; | ||
| } | ||
|
|
||
| inboxes.delete(sessionId); | ||
| flushToDisk(); | ||
| return { messages: inbox }; | ||
| } | ||
|
|
||
| export function getUnreadCount(sessionId: string): number { | ||
| return inboxes.get(sessionId)?.length ?? 0; | ||
| } | ||
|
|
||
| export function clearAllInboxes(): void { | ||
| inboxes.clear(); | ||
| if (dataFile !== '' && existsSync(dataFile)) { | ||
| rmSync(dataFile); | ||
| } | ||
| if (tmpFile !== '' && existsSync(tmpFile)) { | ||
| rmSync(tmpFile); | ||
| } | ||
| } | ||
|
|
||
| function flushToDisk(): void { | ||
| if (dataFile === '') return; | ||
| const json = JSON.stringify(Array.from(inboxes.entries())); | ||
| try { | ||
| writeFileSync(tmpFile, json, 'utf8'); | ||
| renameSync(tmpFile, dataFile); | ||
| } catch (e) { | ||
| console.error('flushToDisk failed:', e); | ||
| } | ||
| } | ||
|
|
||
| if (!process.env.VITEST) { | ||
| initStore(DATA_DIR); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import type { Message } from '../messaging/store.js'; | ||
| import { receiveMessages } from '../messaging/store.js'; | ||
|
|
||
| export function receiveMessagesTool(params: { | ||
| session_id: string; | ||
| thread_id?: string; | ||
| }): { messages: Message[] } { | ||
| return receiveMessages(params.session_id, params.thread_id); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { sendMessage } from '../messaging/store.js'; | ||
|
|
||
| export function sendMessageTool(params: { | ||
| from: string; | ||
| to: string; | ||
| body: string; | ||
| thread_id?: string; | ||
| }): { message_id: string; thread_id: string } { | ||
| return sendMessage(params.from, params.to, params.body, params.thread_id); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { existsSync, mkdtempSync, rmSync } from 'node:fs'; | ||
| import { join } from 'node:path'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { | ||
| clearAllInboxes, | ||
| getUnreadCount, | ||
| initStore, | ||
| receiveMessages, | ||
| sendMessage, | ||
| } from '../../src/messaging/store.js'; | ||
|
|
||
| let tmpDir: string; | ||
|
|
||
| beforeEach(() => { | ||
| tmpDir = mkdtempSync(join(tmpdir(), 'store-persistence-test-')); | ||
| initStore(tmpDir); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| clearAllInboxes(); | ||
| rmSync(tmpDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| describe('MessageStore persistence', () => { | ||
| it('isolation — messages in one test do not leak to the next', () => { | ||
| sendMessage('supervisor', 'worker-1', 'test message'); | ||
| const result = receiveMessages('worker-1'); | ||
| expect(result.messages).toHaveLength(1); | ||
| expect(result.messages[0].body).toBe('test message'); | ||
| }); | ||
|
|
||
| it('persistence across restart — messages survive re-initialization from same directory', () => { | ||
| sendMessage('supervisor', 'worker-1', 'task brief'); | ||
| // Simulate restart: call initStore again from SAME directory WITHOUT clearAllInboxes | ||
| initStore(tmpDir); | ||
| // Messages should be loaded back from disk | ||
| expect(getUnreadCount('worker-1')).toBe(1); | ||
| const result = receiveMessages('worker-1'); | ||
| expect(result.messages).toHaveLength(1); | ||
| expect(result.messages[0].body).toBe('task brief'); | ||
| expect(result.messages[0].from).toBe('supervisor'); | ||
| expect(result.messages[0].to).toBe('worker-1'); | ||
| }); | ||
|
|
||
| it('file deletion — clearAllInboxes removes messages.json from disk', () => { | ||
| sendMessage('supervisor', 'worker-2', 'hello'); | ||
| const messagesFile = join(tmpDir, 'messages.json'); | ||
| // File should exist after sendMessage (flush occurred) | ||
| expect(existsSync(messagesFile)).toBe(true); | ||
| clearAllInboxes(); | ||
| // File should be deleted after clearAllInboxes | ||
| expect(existsSync(messagesFile)).toBe(false); | ||
| // In-memory state cleared too | ||
| const result = receiveMessages('worker-2'); | ||
| expect(result.messages).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('empty start — initStore on fresh directory gives no messages', () => { | ||
| const result = receiveMessages('nobody'); | ||
| expect(result.messages).toEqual([]); | ||
| expect(getUnreadCount('nobody')).toBe(0); | ||
| }); | ||
|
|
||
| it('count accuracy — getUnreadCount reflects correct count after reload', () => { | ||
| sendMessage('supervisor', 'worker-3', 'message one'); | ||
| sendMessage('supervisor', 'worker-3', 'message two'); | ||
| expect(getUnreadCount('worker-3')).toBe(2); | ||
| // Simulate restart | ||
| initStore(tmpDir); | ||
| expect(getUnreadCount('worker-3')).toBe(2); | ||
| receiveMessages('worker-3'); | ||
| expect(getUnreadCount('worker-3')).toBe(0); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.