Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

fix(session): read hook-written session records in session_info and session_current (#4548)

kevinbd19b6f984fb
diff --git a/v3/@claude-flow/cli/__tests__/session-hook-record-shape.test.ts b/v3/@claude-flow/cli/__tests__/session-hook-record-shape.test.ts
new file mode 100644
index 0000000000..c50a361171
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/session-hook-record-shape.test.ts
@@ -0,0 +1,74 @@
+/**
+ * The session hook (`session.js`, written by init) stores `current.json` and
+ * `session-<n>.json` archives shaped `{ id, startedAt, ... }` in the same
+ * directory session_save uses. session_list already reads both shapes
+ * (ADR-093 F6); session_info and session_current returned an undefined
+ * sessionId and time for them.
+ */
+import { describe, it, expect, beforeEach, afterEach } from 'vitest';
+import { mkdirSync, mkdtempSync, rmSync, utimesSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { sessionTools } from '../src/mcp-tools/session-tools.js';
+
+const tool = (name: string) => {
+ const t = sessionTools.find(t => t.name === name);
+ if (!t) throw new Error(`MCP tool not registered: ${name}`);
+ return t;
+};
+
+const startedAt = '2026-10-27T10:00:00.000Z';
+
+describe('session tools read session-hook records', () => {
+ let dir: string;
+ let sessionDir: string;
+ let prevCwd: string | undefined;
+
+ beforeEach(async () => {
+ dir = mkdtempSync(join(tmpdir(), 'ruflo-session-hook-shape-'));
+ prevCwd = process.env.CLAUDE_FLOW_CWD;
+ process.env.CLAUDE_FLOW_CWD = dir;
+ await tool('session_save').handler({ name: 'saved', includeMemory: false, includeTasks: false, includeAgents: false });
+ sessionDir = join(dir, '.claude-flow', 'sessions');
+ mkdirSync(sessionDir, { recursive: true });
+ const hookRecord = { id: 'session-1761559200000', startedAt, cwd: dir, context: {}, metrics: { edits: 0, commands: 0, tasks: 0, errors: 0 } };
+ writeFileSync(join(sessionDir, 'session-1761559200000.json'), JSON.stringify({ ...hookRecord, endedAt: startedAt }));
+ const current = join(sessionDir, 'current.json');
+ writeFileSync(current, JSON.stringify(hookRecord));
+ // The hook rewrites current.json on every update, so it is the newest file.
+ const later = new Date(Date.now() + 60_000);
+ utimesSync(current, later, later);
+ });
+
+ afterEach(() => {
+ if (prevCwd === undefined) delete process.env.CLAUDE_FLOW_CWD;
+ else process.env.CLAUDE_FLOW_CWD = prevCwd;
+ rmSync(dir, { recursive: true, force: true });
+ });
+
+ it('session_info reports the id and start time of an archived hook session', async () => {
+ const info = await tool('session_info').handler({ sessionId: 'session-1761559200000' }) as Record<string, unknown>;
+ expect(info.error).toBeUndefined();
+ expect(info.sessionId).toBe('session-1761559200000');
+ expect(info.name).toBe('session-1761559200000');
+ expect(info.savedAt).toBe(startedAt);
+ });
+
+ it('session_current reports the id and start time of current.json', async () => {
+ const current = await tool('session_current').handler({}) as Record<string, unknown>;
+ expect(current.status).toBe('active');
+ expect(current.sessionId).toBe('session-1761559200000');
+ expect(current.startedAt).toBe(startedAt);
+ });
+
+ it('session_info still reports a saved session as written', async () => {
+ const list = await tool('session_list').handler({ sortBy: 'name' }) as { sessions: Array<{ sessionId: string; name: string }> };
+ const saved = list.sessions.find(s => s.name === 'saved');
+ expect(saved).toBeDefined();
+ const info = await tool('session_info').handler({ sessionId: saved!.sessionId }) as Record<string, unknown>;
+ expect(info.sessionId).toBe(saved!.sessionId);
+ expect(info.name).toBe('saved');
+ expect(typeof info.savedAt).toBe('string');
+ expect(info.savedAt).not.toBe('');
+ });
+});
diff --git a/v3/@claude-flow/cli/src/mcp-tools/session-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/session-tools.ts
index b580d63c98..08cfcdb113 100644
--- a/v3/@claude-flow/cli/src/mcp-tools/session-tools.ts
+++ b/v3/@claude-flow/cli/src/mcp-tools/session-tools.ts
@@ -81,6 +81,22 @@ function ensureSessionDir(): void {
}
}
+/**
+ * The session hook (`session.js` from init/helpers-generator.ts) writes its
+ * records into this same directory: `current.json` and `session-<n>.json`
+ * archives shaped `{ id, startedAt, ... }` rather than session_save's
+ * `{ sessionId, name, savedAt, stats }`. Read the id and time from either shape
+ * (ADR-093 F6), falling back to the file's own id.
+ */
+function sessionIdentity(session: SessionRecord, fileId: string): { sessionId: string; savedAt: string } {
+ const raw = session as unknown as Record<string, unknown>;
+ const pick = (...values: unknown[]) => values.find((v): v is string => typeof v === 'string' && v !== '');
+ return {
+ sessionId: pick(raw.sessionId, raw.id) ?? fileId,
+ savedAt: pick(raw.savedAt, raw.startedAt) ?? '',
+ };
+}
+
function loadSession(sessionId: string): SessionRecord | null {
try {
const path = getSessionPath(sessionId);
@@ -662,13 +678,14 @@ export const sessionTools: MCPTool[] = [
if (session) {
const path = getSessionPath(sessionId);
const stat = statSync(path);
+ const identity = sessionIdentity(session, sessionId);
return {
- sessionId: session.sessionId,
- name: session.name,
+ sessionId: identity.sessionId,
+ name: session.name ?? identity.sessionId,
description: session.description,
- savedAt: session.savedAt,
- stats: session.stats,
+ savedAt: identity.savedAt,
+ stats: session.stats ?? null,
fileSize: stat.size,
path,
hasData: {
@@ -705,12 +722,13 @@ export const sessionTools: MCPTool[] = [
const sessionId = newest.replace(/\.json$/, '');
const session = loadSession(sessionId);
if (!session) return { sessionId, status: 'unknown', startedAt: '', error: 'Session file unreadable' };
+ const identity = sessionIdentity(session, sessionId);
return {
- sessionId: session.sessionId,
- name: session.name,
+ sessionId: identity.sessionId,
+ name: session.name ?? identity.sessionId,
status: 'active',
- startedAt: session.savedAt,
- stats: session.stats,
+ startedAt: identity.savedAt,
+ stats: session.stats ?? null,
};
},
},