Adds the respire-memory gateway middleware (packages/respire-memory, 0.3.6) by luodaoyi, with its registry.json entry and README row. It recalls from a loopback Respire Runtime on each new turn, reads the engine policy each turn and puts it in the history once (again only when it changes), keeps turn state in an encrypted store, and optionally archives turns in a detached worker through a user-configured Chat Completions service. Diary writes are confirmed by a bounded fragment readback. Run at 7aba754 on mote (Node 22.22.0, Bun 1.4.2): - packages/respire-memory: `node scripts/check.mjs` ok (4 protocol, policy dedup/change/revert, byte-identical prefixes, failed read, diary readback); `bun test` 1 pass. - root: `HOME=$(mktemp -d) bun test` 4187 pass, 9 skip, 0 fail (4196 tests, 328 files); `bun scripts/check.mjs` exit 0. - Review probe (fake loopback Runtime, 2000-byte policy, six chat turns with a host restart at turn 3, a policy change, then the Runtime down), Bun and Node: per-turn overhead grows ~524 bytes a turn (the read receipt), not the policy; no earlier prefix changed; one policy read per turn; two policy blocks after the change; with the Runtime down the latest turn is unchanged. With no Runtime at all the body passes through unchanged. - Capture probe: with capture on, a turn whose JSON exceeds the ~4000-char readback budget is written and then recorded unconfirmed, and archival skips it. The PR's check asserts this and the README documents it. - Harm check: no new dependencies, install scripts or workflow changes; network only to the loopback Runtime and the user's own generation URL. Not tried: a real Respire Runtime.
110 lines
4.9 KiB
JavaScript
110 lines
4.9 KiB
JavaScript
// Reused from Respire Codex: bounded extraction with original-evidence readback.
|
|
const record = value => value && typeof value === 'object' && !Array.isArray(value)
|
|
|
|
export const DIRECT_LIMIT = 256000
|
|
export const CHUNK_LIMIT = 48000
|
|
export const MAX_CHUNKS = 12
|
|
const FRAGMENT_LIMIT = 6000
|
|
const TYPES = new Set(['userMessage', 'agentMessage', 'commandExecution', 'mcpToolCall', 'dynamicToolCall', 'fileChange'])
|
|
|
|
/** Prepare ordered evidence from exactly one turn; never silently discard dialogue. */
|
|
export function prepareArchiveContext(items) {
|
|
const fragments = []
|
|
const groups = []
|
|
let clippedTools = 0
|
|
let omittedAttachments = 0
|
|
let originalChars = 0
|
|
for (const [index, item] of items.entries()) {
|
|
if (!TYPES.has(item.type)) continue
|
|
let text
|
|
const dialogue = item.type === 'userMessage' || item.type === 'agentMessage'
|
|
if (item.type === 'userMessage') {
|
|
text = (item.content || []).map(part => {
|
|
if (typeof part.text === 'string') return part.text
|
|
omittedAttachments++
|
|
return '[Non-text attachment omitted: ' + String(part.type) + ']'
|
|
}).join('\n')
|
|
} else if (item.type === 'agentMessage') text = item.text || ''
|
|
else text = JSON.stringify(item)
|
|
originalChars += text.length
|
|
const start = fragments.length
|
|
// Split originals before preparing previews so every omitted middle remains readable.
|
|
let offset = 0
|
|
let part = 0
|
|
do {
|
|
let end = Math.min(text.length, offset + FRAGMENT_LIMIT)
|
|
if (end < text.length && /[\uD800-\uDBFF]/.test(text[end - 1])) end--
|
|
fragments.push({
|
|
source: 'item-' + index + '-part-' + part++, type: item.type,
|
|
...(item.phase ? { phase: item.phase } : {}),
|
|
...(item.status ? { status: item.status } : {}),
|
|
...(item.exitCode !== undefined ? { exitCode: item.exitCode } : {}),
|
|
text: text.slice(offset, end),
|
|
})
|
|
offset = end
|
|
} while (offset < text.length)
|
|
groups.push({ dialogue, parts: fragments.slice(start), chars: text.length })
|
|
}
|
|
let prepared = fragments
|
|
if (JSON.stringify(fragments).length > DIRECT_LIMIT) {
|
|
prepared = groups.flatMap(({ dialogue, parts, chars }) => {
|
|
if (dialogue || parts.length <= 2) return parts
|
|
clippedTools++
|
|
return [parts[0], parts.at(-1)].map(fragment => ({
|
|
...fragment, partial: true, originalChars: chars,
|
|
text: fragment.text + '\n[PARTIAL: middle tool evidence available through respire_turn_evidence]',
|
|
}))
|
|
})
|
|
}
|
|
const serialized = JSON.stringify(prepared)
|
|
const chunks = []
|
|
if (serialized.length <= DIRECT_LIMIT) chunks.push(prepared)
|
|
else {
|
|
let chunk = []
|
|
for (const fragment of prepared) {
|
|
if (JSON.stringify([...chunk, fragment]).length > CHUNK_LIMIT) {
|
|
chunks.push(chunk)
|
|
chunk = []
|
|
}
|
|
chunk.push(fragment)
|
|
}
|
|
if (chunk.length) chunks.push(chunk)
|
|
}
|
|
if (chunks.length > MAX_CHUNKS) throw new Error('archive-segment-limit')
|
|
return {
|
|
chunks, fragments, userSources: fragments.filter(x => x.type === 'userMessage').map(x => x.source),
|
|
coverage: clippedTools || omittedAttachments ? 'partial' : 'complete',
|
|
originalChars, preparedChars: serialized.length, clippedTools, omittedAttachments,
|
|
}
|
|
}
|
|
|
|
/** Only accept small, source-linked candidate data, never free-form executable instructions. */
|
|
export function parseExtraction(text, fragments) {
|
|
if (typeof text !== 'string' || text.length > 6000) throw new Error('extraction-output-limit')
|
|
let value
|
|
try { value = JSON.parse(text) } catch { throw new Error('invalid-extraction') }
|
|
const sources = new Set(fragments.map(x => x.source))
|
|
const valid = (entry, fact) => record(entry) && typeof entry.text === 'string' &&
|
|
entry.text.trim().length > 0 && entry.text.length <= 1500 &&
|
|
Array.isArray(entry.sources) && entry.sources.length > 0 &&
|
|
entry.sources.every(source => sources.has(source)) &&
|
|
(!fact || ['confirmed', 'proposed', 'uncertain'].includes(entry.status)) &&
|
|
Object.keys(entry).every(key => ['text', 'sources', ...(fact ? ['status'] : [])].includes(key))
|
|
if (!record(value) || Object.keys(value).some(key => !['facts', 'constraints'].includes(key)) ||
|
|
!Array.isArray(value.facts) || !Array.isArray(value.constraints) ||
|
|
value.facts.length > 24 || value.constraints.length > 24 ||
|
|
!value.facts.every(x => valid(x, true)) || !value.constraints.every(x => valid(x, false))) {
|
|
throw new Error('invalid-extraction')
|
|
}
|
|
return value
|
|
}
|
|
|
|
/** Evidence stays in memory for this worker; unknown or oversized reads fail closed. */
|
|
export function readEvidence(fragments, sources) {
|
|
if (!Array.isArray(sources) || !sources.length || sources.length > 4 ||
|
|
sources.some(source => typeof source !== 'string')) throw new Error('invalid-evidence')
|
|
const selected = sources.map(source => fragments.find(x => x.source === source))
|
|
if (selected.some(x => !x) || JSON.stringify(selected).length > CHUNK_LIMIT) throw new Error('invalid-evidence')
|
|
return selected
|
|
}
|