fix: stream from current position, faster session detection (500ms)

- New sessions start from current file offset, not 0. Shows live
  thinking from the moment of detection, not a backlog dump.
- Session poll reduced from 2s to 500ms for faster pickup.
- Auto-pin with null body (MM pin API quirk).
This commit is contained in:
sol
2026-03-07 18:47:25 +00:00
parent b5bde4ec20
commit 6d31d77567
2 changed files with 19 additions and 5 deletions

View File

@@ -34,7 +34,7 @@ class SessionMonitor extends EventEmitter {
constructor(opts) { constructor(opts) {
super(); super();
this.transcriptDir = opts.transcriptDir; this.transcriptDir = opts.transcriptDir;
this.pollMs = opts.pollMs || 2000; this.pollMs = opts.pollMs || 500;
this.defaultChannel = opts.defaultChannel || null; this.defaultChannel = opts.defaultChannel || null;
this.logger = opts.logger || null; this.logger = opts.logger || null;

View File

@@ -51,6 +51,18 @@ class StatusWatcher extends EventEmitter {
addSession(sessionKey, transcriptFile, initialState = {}) { addSession(sessionKey, transcriptFile, initialState = {}) {
if (this.sessions.has(sessionKey)) return; if (this.sessions.has(sessionKey)) return;
// For new sessions (no saved offset), start from current file position
// so we only show NEW content going forward — not the entire backlog
let startOffset = initialState.lastOffset || 0;
if (!initialState.lastOffset) {
try {
var stat = fs.statSync(transcriptFile);
startOffset = stat.size;
} catch (_e) {
startOffset = 0;
}
}
const state = { const state = {
sessionKey, sessionKey,
transcriptFile, transcriptFile,
@@ -58,7 +70,7 @@ class StatusWatcher extends EventEmitter {
startTime: initialState.startTime || Date.now(), startTime: initialState.startTime || Date.now(),
lines: initialState.lines || [], lines: initialState.lines || [],
pendingToolCalls: 0, pendingToolCalls: 0,
lastOffset: initialState.lastOffset || 0, lastOffset: startOffset,
lastActivityAt: Date.now(), lastActivityAt: Date.now(),
agentId: initialState.agentId || extractAgentId(sessionKey), agentId: initialState.agentId || extractAgentId(sessionKey),
depth: initialState.depth || 0, depth: initialState.depth || 0,
@@ -71,12 +83,14 @@ class StatusWatcher extends EventEmitter {
this.fileToSession.set(transcriptFile, sessionKey); this.fileToSession.set(transcriptFile, sessionKey);
if (this.logger) { if (this.logger) {
this.logger.debug({ sessionKey, transcriptFile }, 'Session added to watcher'); this.logger.debug({ sessionKey, transcriptFile, startOffset }, 'Session added to watcher');
} }
// Immediately read any existing content // Only read if we have a saved offset (recovery) — new sessions start streaming from current position
if (initialState.lastOffset) {
this._readFile(sessionKey, state); this._readFile(sessionKey, state);
} }
}
/** /**
* Remove a session from watching. * Remove a session from watching.