Repository navigation
Expand file tree
/
Copy pathtranscript-turn.js
More file actions
228 lines (210 loc) · 10.2 KB
/
Copy pathtranscript-turn.js
File metadata and controls
228 lines (210 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// transcript-turn.js — whether a session transcript shows its main turn closed.
// see .ai/contexts/trigger-watcher.md, "Transcript fallback while the descriptor stays busy"
'use strict';
const fs = require('fs');
const { createHash } = require('node:crypto');
const { createTranscriptTailReader, readRange } = require('./transcript-tail');
const CLOSED_STOP_REASONS = new Set(['end_turn', 'stop_sequence']);
const DEFAULT_TAIL_BYTES = 256 * 1024;
const COMPACT_CHUNK_BYTES = 256 * 1024;
function stampOf(entry) {
const t = typeof entry.timestamp === 'string' ? Date.parse(entry.timestamp) : NaN;
return Number.isFinite(t) ? t : null;
}
const MAX_PROMPTS = 50;
const COMMAND_ELEMENTS_ONLY = /^(?:\s*<(command-message|command-name|command-args|command-contents)>[\s\S]*?<\/\1>)+\s*$/;
function isLocalCommandOutput(entry) {
const content = entry.message && entry.message.content;
if (typeof content !== 'string') return false;
const t = content.trim();
return t.startsWith('<local-command-stdout>') && t.endsWith('</local-command-stdout>');
}
function isManualCompactBoundary(entry) {
return !!entry && entry.type === 'system' && entry.subtype === 'compact_boundary'
&& !!entry.compactMetadata && entry.compactMetadata.trigger === 'manual';
}
function endsTurn(entry, turnDurationAfter, previous) {
if (entry.type === 'user') {
if (entry.isCompactSummary === true) return isManualCompactBoundary(previous);
return isLocalCommandOutput(entry) || /^\[Request interrupted by user(?:\]| for tool use\])/.test(entryText(entry));
}
return entry.isApiErrorMessage === true
|| turnDurationAfter && CLOSED_STOP_REASONS.has(entry.message && entry.message.stop_reason);
}
function entryText(entry) {
const content = entry.message?.content;
return typeof content === 'string' ? content : Array.isArray(content)
? content.filter(block => block.type === 'text').map(block => block.text).join('\n') : '';
}
function parseMainThread(text) {
const entries = [];
for (const raw of String(text).split('\n')) {
const line = raw.trim();
if (!line) continue;
let entry;
try { entry = JSON.parse(line); } catch { continue; }
if (!entry || typeof entry !== 'object' || entry.isSidechain) continue;
entries.push(entry);
}
return entries;
}
function latestWrittenOutput(text) {
const complete = text.slice(0, text.lastIndexOf('\n') + 1);
const entries = parseMainThread(complete);
for (let i = entries.length - 1; i >= 0; i--) {
const entry = entries[i];
if (entry.isMeta || entry.isCompactSummary || !['assistant', 'user'].includes(entry.type)) continue;
const content = entry.message?.content;
const blocks = typeof content === 'string' ? [{ type: 'text', text: content }] : content;
if (!Array.isArray(blocks)) continue;
const visible = blocks.filter(block => block && ['text', 'thinking', 'tool_use', 'tool_result', 'image'].includes(block.type));
if (!visible.length) continue;
if (entry.type === 'user' && !visible.some(block => block.type === 'tool_result'
|| block.type === 'text' && /^\s*<(?:task-notification|local-command-stdout|local-command-stderr)>/.test(block.text || ''))) continue;
if (visible.every(block => ['text', 'thinking'].includes(block.type) && !(block.text || block.thinking || '').trim())) continue;
const output = visible.map(block => {
switch (block.type) {
case 'text': return ['text', block.text];
case 'thinking': return ['thinking', block.thinking];
case 'tool_use': return ['tool_use', block.id, block.name, canonicalWrittenValue(block.input)];
case 'tool_result': return ['tool_result', block.tool_use_id, canonicalWrittenValue(block.content || block.output)];
case 'image': return ['image', canonicalWrittenValue(block.source)];
}
});
return createHash('sha256').update(JSON.stringify([entry.uuid || entry.message?.id || entry.timestamp || null, entry.type, output])).digest('hex');
}
return null;
}
function canonicalWrittenValue(value) {
if (Array.isArray(value)) return value.map(canonicalWrittenValue);
if (value && typeof value === 'object') return Object.fromEntries(Object.keys(value).sort().map(key => [key, canonicalWrittenValue(value[key])]));
return value;
}
function promptOf(entry) {
if (entry.type === 'queue-operation') {
return entry.operation === 'enqueue' && typeof entry.content === 'string' ? entry.content : null;
}
if (entry.type !== 'user' || entry.isMeta) return null;
const content = entry.message && entry.message.content;
return typeof content === 'string' ? content : null;
}
function collectPrompts(entries) {
const prompts = [];
for (const entry of entries) {
const text = promptOf(entry);
const at = stampOf(entry);
if (text !== null && at !== null) prompts.push({ at, text });
}
return prompts.slice(-MAX_PROMPTS);
}
function promptMatches(text, command) {
if (typeof text !== 'string' || typeof command !== 'string') return false;
const want = command.trim();
if (!want) return false;
const got = text.trim();
if (got === want) return true;
if (want.startsWith('!')) {
const bash = got.match(/^<bash-input>([\s\S]*)<\/bash-input>$/);
const typed = want.slice(1).trim();
return !!bash && !!typed && bash[1].trim() === typed;
}
if (!COMMAND_ELEMENTS_ONLY.test(got)) return false;
const name = got.match(/<command-name>([^<]*)<\/command-name>/);
return !!name && name[1].trim() === want.split(/\s/)[0];
}
function classifyTranscriptTail(text) {
const entries = parseMainThread(text);
const prompts = collectPrompts(entries);
const compactBoundaries = entries.filter(isManualCompactBoundary)
.map((entry) => ({ at: stampOf(entry), uuid: entry.uuid || null }))
.filter((entry) => entry.at !== null);
let lastEntryAt = null;
let enqueued = 0;
let removed = 0;
let dequeued = 0;
let turnDurationAfter = false;
for (let i = entries.length - 1; i >= 0; i -= 1) {
const entry = entries[i];
if (entry.type === 'system' && entry.subtype === 'informational') continue;
const at = stampOf(entry);
if (at !== null && (lastEntryAt === null || at > lastEntryAt)) lastEntryAt = at;
if (entry.type === 'queue-operation') {
if (entry.operation === 'enqueue') enqueued += 1;
else if (entry.operation === 'remove') removed += 1;
else if (entry.operation === 'dequeue') dequeued += 1;
continue;
}
if (entry.type === 'system' && entry.subtype === 'turn_duration') turnDurationAfter = true;
if (entry.type !== 'user' && entry.type !== 'assistant') continue;
if (entry.type === 'user' && (entryText(entry).trim().startsWith('<task-notification>')
|| entry.isCompactSummary === true && !isManualCompactBoundary(entries[i - 1]))) continue;
const queueIdle = enqueued <= removed && dequeued === 0;
const closed = endsTurn(entry, turnDurationAfter, entries[i - 1]) && queueIdle && at !== null;
return { state: at === null ? 'unknown' : (closed ? 'closed' : 'open'), closed, closedAt: closed ? at : null, lastEntryAt, prompts, compactBoundaries };
}
return { state: 'unknown', closed: false, closedAt: null, lastEntryAt, prompts, compactBoundaries };
}
// see .ai/contexts/trigger-watcher.md, "Compaction evidence"
function readCompactChunk(filePath, stat, cursor) {
if (cursor.invalid || cursor.filePath !== filePath || cursor.dev !== stat.dev
|| cursor.ino !== stat.ino || stat.size < cursor.offset) {
cursor.invalid = true;
throw new Error('transcript cursor invalidated');
}
const length = Math.min(COMPACT_CHUNK_BYTES, stat.size - cursor.offset);
const chunk = readRange(filePath, cursor.offset, length, stat);
cursor.offset += chunk.length;
const data = Buffer.concat([cursor.pending, chunk]);
const lastNewline = data.lastIndexOf(10);
let complete = lastNewline < 0 ? Buffer.alloc(0) : data.subarray(0, lastNewline + 1);
cursor.pending = Buffer.from(data.subarray(lastNewline + 1));
if (cursor.skipLine && complete.length) {
complete = complete.subarray(complete.indexOf(10) + 1);
cursor.skipLine = false;
}
if (cursor.pending.length > COMPACT_CHUNK_BYTES) {
cursor.pending = Buffer.alloc(0);
cursor.skipLine = true;
}
return parseMainThread(complete.toString('utf8')).filter(isManualCompactBoundary)
.map((entry) => ({ at: stampOf(entry), uuid: entry.uuid || null }))
.filter((entry) => entry.at !== null);
}
function createTranscriptTurnReader({ tailBytes = DEFAULT_TAIL_BYTES } = {}) {
const cache = new Map();
const tails = createTranscriptTailReader({ tailBytes });
return {
read(filePath, compactionCursor) {
let stat;
try {
const exact = fs.statSync(filePath, { bigint: true });
stat = { dev: exact.dev, ino: exact.ino, size: Number(exact.size),
mtimeMs: Number(exact.mtimeNs / 1_000_000_000n) * 1000 + Number(exact.mtimeNs % 1_000_000_000n) / 1e6 };
} catch { return null; }
const hit = cache.get(filePath);
let cached = hit;
if (!hit || hit.mtimeMs !== stat.mtimeMs || hit.size !== stat.size || hit.ino !== stat.ino || hit.dev !== stat.dev
|| !hit.complete || !compactionCursor && !hit.writtenComplete) {
let tail;
try { tail = tails.read(filePath, stat, compactionCursor ? tailBytes : undefined); } catch { return null; }
const turn = { ...classifyTranscriptTail(tail.text), mtimeMs: stat.mtimeMs,
writtenOutput: latestWrittenOutput(tail.writtenText) ?? hit?.turn.writtenOutput ?? '' };
cached = { mtimeMs: stat.mtimeMs, size: stat.size, dev: stat.dev, ino: stat.ino,
writtenComplete: !compactionCursor, complete: tail.complete,
skipLine: !tail.endsWithNewline, turn };
cache.set(filePath, cached);
}
const turn = { ...cached.turn };
if (compactionCursor) {
try { turn.compactBoundaries = readCompactChunk(filePath, stat, compactionCursor); } catch { return null; }
}
return { ...turn, compactionCursor: { filePath, dev: stat.dev, ino: stat.ino,
offset: stat.size, pending: Buffer.alloc(0), skipLine: cached.skipLine } };
},
forget(filePath) {
cache.delete(filePath);
tails.forget(filePath);
},
};
}
module.exports = { classifyTranscriptTail, createTranscriptTurnReader, promptMatches, CLOSED_STOP_REASONS };