Updates
Release rolling / release (push) Successful in 16m49s

This commit is contained in:
2026-08-18 13:25:51 -04:00
parent e7b00df454
commit 154c13d902
28 changed files with 7052 additions and 708 deletions
+330 -100
View File
@@ -171,12 +171,83 @@ function bareAgentIsCompactionMessage(msg) {
const m = /** @type {Record<string, unknown>} */ (msg)
if (m.bare_os_compaction === true) return true
const c = typeof m.content === 'string' ? m.content : ''
const t = c.trim()
return (
String(m.role || '') === 'user' &&
/^\[context compaction\]/i.test(c.trim())
(/^\[context compaction\]/i.test(t) ||
/^This session is being continued from a previous conversation/i.test(t) ||
m.bare_os_reminder === true)
)
}
/** Grok-style continuation carrier minimum (extractive; slightly below LLM 500). */
var BARE_AGENT_MIN_SUMMARY_SEED_CHARS = 160
/**
* Strip drafting tags and neutralize leftover <summary>/<analysis> tokens
* so they cannot prime the next turn (Grok format_compact_summary).
* @param {string} raw
*/
function bareAgentFormatCompactSummary(raw) {
let result = String(raw || '')
result = result.replace(/<analysis>[\s\S]*?<\/analysis>/gi, '')
result = result.replace(/<\/?summary>/gi, '')
result = result
.replace(/<\/summary>/g, '<\u200b/summary>')
.replace(/<summary>/g, '<\u200bsummary>')
.replace(/<\/analysis>/g, '<\u200b/analysis>')
.replace(/<analysis>/g, '<\u200banalysis>')
while (result.indexOf('\n\n\n') >= 0) result = result.replace(/\n\n\n/g, '\n\n')
return result.trim()
}
/**
* @param {string} raw
*/
function bareAgentFormatCompactSummaryContent(raw) {
const cleaned = bareAgentFormatCompactSummary(raw)
return (
'This session is being continued from a previous conversation that ran out of context. ' +
'The summary below covers the earlier portion of the conversation.\n\n' +
cleaned
)
}
/**
* @param {string} text
*/
function bareAgentWrapUserQuery(text) {
const t = String(text || '').trim()
if (!t) return ''
if (/^<user_query>/.test(t)) return t
return '<user_query>\n' + t + '\n</user_query>'
}
/**
* @param {string} raw
*/
function bareAgentIsDegenerateSummary(raw) {
return bareAgentFormatCompactSummary(raw).length < BARE_AGENT_MIN_SUMMARY_SEED_CHARS
}
/**
* Collect unique path-like and command tokens for the artifacts section.
* @param {string} text
* @param {number} max
*/
function bareAgentExtractPathTokens(text, max) {
const s = String(text || '')
const found = []
const re = /(?:~|\/)[A-Za-z0-9._+\-@/]+/g
let m
while ((m = re.exec(s)) && found.length < max) {
const p = m[0]
if (p.length < 3 || found.indexOf(p) >= 0) continue
found.push(p)
}
return found
}
/**
* One-line digest for a message (extractive).
* @param {unknown} msg
@@ -208,45 +279,228 @@ function bareAgentDigestMessage(msg, max) {
return '- ' + role + ' ' + bareAgentClipText(text, max)
}
/**
* Grok-style 7-section extractive summary of older turn groups.
* @param {unknown[][]} groups
* @param {{ maxChars?: number, perMsg?: number }} [opts]
*/
function bareAgentBuildStructuredSummary(groups, opts) {
const maxChars = Math.max(400, Math.floor((opts && opts.maxChars) || 3500))
const perMsg = Math.max(60, Math.floor((opts && opts.perMsg) || 180))
/** @type {string[]} */
const userMsgs = []
/** @type {string[]} */
const tools = []
/** @type {string[]} */
const errors = []
/** @type {string[]} */
const solves = []
/** @type {string[]} */
const concepts = []
/** @type {string[]} */
const files = []
function pushUnique(arr, s, cap) {
const t = String(s || '').trim()
if (!t || arr.indexOf(t) >= 0) return
if (arr.length >= cap) return
arr.push(t)
}
for (const g of groups) {
for (const msg of g) {
if (!msg || typeof msg !== 'object') continue
const m = /** @type {Record<string, unknown>} */ (msg)
const role = String(m.role || '')
const text = bareAgentMessagePlainText(m)
for (const p of bareAgentExtractPathTokens(text, 8)) pushUnique(files, p, 24)
if (role === 'user' && !bareAgentIsCompactionMessage(m)) {
pushUnique(userMsgs, bareAgentClipText(text, perMsg + 80), 16)
} else if (role === 'tool') {
const name = typeof m.name === 'string' ? m.name : 'tool'
pushUnique(tools, name + ': ' + bareAgentClipText(text, perMsg), 20)
if (/error|fail|denied|not found|ENOENT|EXIT:[1-9]/i.test(text)) {
pushUnique(errors, name + ' ' + bareAgentClipText(text, 140), 12)
}
} else if (role === 'assistant') {
if (m.tool_calls && Array.isArray(m.tool_calls)) {
for (const tc of m.tool_calls) {
if (!tc || typeof tc !== 'object') continue
const fn = /** @type {Record<string, unknown>} */ (tc).function
if (fn && typeof fn === 'object') {
const n = /** @type {Record<string, unknown>} */ (fn).name
if (typeof n === 'string' && n) pushUnique(concepts, n, 16)
}
}
}
if (text) pushUnique(solves, bareAgentClipText(text, perMsg), 12)
}
}
}
const intent = userMsgs.length ? userMsgs[0] : '(no explicit user request in compacted prefix)'
/** @type {string[]} */
const lines = [
'1. Primary Request and Intent:',
' ' + intent,
'',
'2. Key Technical Concepts:',
concepts.length ? concepts.map((c) => ' - ' + c).join('\n') : ' - (none extracted)',
'',
'3. Tool Usage & Verification:',
tools.length ? tools.map((c) => ' - ' + c).join('\n') : ' - (none)',
'',
'4. Files & Code Artifacts:',
files.length ? files.map((c) => ' - ' + c).join('\n') : ' - (none)',
'',
'5. Errors and Fixes:',
errors.length ? errors.map((c) => ' - ' + c).join('\n') : ' - (none recorded)',
'',
'6. Problem Solving:',
solves.length ? solves.map((c) => ' - ' + c).join('\n') : ' - (in progress)',
'',
'7. User Messages:',
userMsgs.length ? userMsgs.map((c) => ' - ' + c).join('\n') : ' - (none)'
]
let body = lines.join('\n')
if (body.length > maxChars) body = body.slice(0, maxChars - 20) + '\n… truncated'
return body
}
/**
* Build a rolling summary from older turn groups.
* @param {unknown[][]} groups
* @param {{ maxChars?: number, perMsg?: number }} [opts]
*/
function bareAgentBuildCompactionSummary(groups, opts) {
const maxChars = Math.max(400, Math.floor((opts && opts.maxChars) || 3500))
const perMsg = Math.max(60, Math.floor((opts && opts.perMsg) || 180))
const structured = bareAgentBuildStructuredSummary(groups, opts)
return bareAgentFormatCompactSummaryContent(structured)
}
/**
* Post-compaction <system-reminder> (Grok reminder.rs analogue).
* @param {{
* autonomous?: { active?: boolean, goal?: string, status?: string, remainingMs?: number },
* droppedGroups?: number,
* afterTokens?: number,
* budget?: number
* }} [opts]
*/
function bareAgentBuildSystemReminder(opts) {
const o = opts && typeof opts === 'object' ? opts : {}
/** @type {string[]} */
const lines = [
'[context compaction] Older turns were summarized to fit the model context window. Preserve goals, paths, errors, and decisions below.',
''
]
let used = lines.join('\n').length
let kept = 0
for (const g of groups) {
/** @type {string[]} */
const block = []
for (const msg of g) {
const d = bareAgentDigestMessage(msg, perMsg)
if (d) block.push(d)
}
if (!block.length) continue
const chunk = block.join('\n') + '\n'
if (used + chunk.length > maxChars && kept > 0) {
lines.push('… (' + String(groups.length - kept) + ' older turns omitted)')
break
}
lines.push(chunk.trimEnd())
used += chunk.length
kept++
const parts = []
const auto = o.autonomous
if (auto && auto.active) {
parts.push(
'Autonomous run: ' +
String(auto.goal || '(goal)').slice(0, 240) +
' (status=' +
String(auto.status || 'running') +
(typeof auto.remainingMs === 'number'
? ', remaining=' + String(Math.max(0, Math.round(auto.remainingMs / 1000))) + 's'
: '') +
'). Keep using tools until task_complete.'
)
}
lines.push('')
lines.push(
'Continue from the recent messages after this summary. Prefer tools over guessing.'
)
let body = lines.join('\n')
if (body.length > maxChars) body = body.slice(0, maxChars - 20) + '\n… truncated'
return body
if (typeof o.droppedGroups === 'number' && o.droppedGroups > 0) {
parts.push(
'Context compacted: ' +
String(o.droppedGroups) +
' older turns summarized. Recent messages after the last user query are verbatim.'
)
}
if (typeof o.afterTokens === 'number' && typeof o.budget === 'number' && o.budget > 0) {
parts.push(
'Context usage after compact: ' +
String(o.afterTokens) +
'/' +
String(o.budget) +
' tokens (est.).'
)
}
if (!parts.length) return ''
return '<system-reminder>\n' + parts.join('\n') + '\n</system-reminder>'
}
/**
* Grok assemble: [system, last user query, recent after that turn, summary, reminder].
* @param {{
* system?: unknown | null,
* lastUserQuery?: string,
* recent?: unknown[],
* summaryText?: string,
* reminder?: string
* }} parts
* @returns {unknown[]}
*/
function bareAgentAssembleCompactedHistory(parts) {
const p = parts && typeof parts === 'object' ? parts : {}
/** @type {unknown[]} */
const out = []
if (p.system) out.push(p.system)
const q = String(p.lastUserQuery || '').trim()
if (q) {
out.push({
role: 'user',
content: bareAgentWrapUserQuery(q),
bare_os_user_query: true
})
}
if (Array.isArray(p.recent)) {
for (const m of p.recent) out.push(m)
}
const summary = String(p.summaryText || '').trim()
if (summary) {
out.push({
role: 'user',
content: summary,
bare_os_compaction: true
})
}
const rem = String(p.reminder || '').trim()
if (rem) {
out.push({
role: 'user',
content: rem,
bare_os_compaction: true,
bare_os_reminder: true
})
}
return out
}
/**
* Last non-compaction user text + messages after that turn.
* @param {unknown[][]} groups
*/
function bareAgentSplitLastUserAndRecent(groups) {
let lastUserIdx = -1
let lastUserText = ''
for (let i = groups.length - 1; i >= 0; i--) {
const g = groups[i]
if (!g || !g.length) continue
const first = g[0]
if (!first || typeof first !== 'object') continue
const m = /** @type {Record<string, unknown>} */ (first)
if (String(m.role || '') !== 'user') continue
if (bareAgentIsCompactionMessage(m)) continue
lastUserIdx = i
lastUserText = bareAgentMessagePlainText(m)
break
}
if (lastUserIdx < 0) {
return { lastUserText: '', older: groups, recent: /** @type {unknown[]} */ ([]) }
}
const older = groups.slice(0, lastUserIdx)
/** @type {unknown[]} */
const recent = []
const lastGroup = groups[lastUserIdx] || []
for (let i = 1; i < lastGroup.length; i++) recent.push(lastGroup[i])
for (let i = lastUserIdx + 1; i < groups.length; i++) {
for (const m of groups[i]) recent.push(m)
}
return { lastUserText, older, recent }
}
/**
@@ -259,7 +513,8 @@ function bareAgentBuildCompactionSummary(groups, opts) {
* toolMaxChars?: number,
* summaryMaxChars?: number,
* softRatio?: number,
* mode?: 'auto' | 'off' | 'aggressive'
* mode?: 'auto' | 'off' | 'aggressive',
* autonomous?: { active?: boolean, goal?: string, status?: string, remainingMs?: number }
* }} [opts]
* @returns {{
* messages: unknown[],
@@ -293,7 +548,7 @@ function bareAgentCompactMessagesForCtx(msgs, ctxSize, opts) {
const softRatio =
mode === 'aggressive'
? 0.7
: Math.min(0.95, Math.max(0.55, Number(opts && opts.softRatio) || 0.82))
: Math.min(0.95, Math.max(0.55, Number(opts && opts.softRatio) || 0.85))
const softBudget = Math.floor(budget * softRatio)
const keepRecentDefault = mode === 'aggressive' ? 4 : 8
@@ -358,41 +613,55 @@ function bareAgentCompactMessagesForCtx(msgs, ctxSize, opts) {
}
}
// Merge prior compaction blobs in the middle into the new summary.
const { system, groups } = bareAgentGroupMessageTurns(out)
let droppedGroups = 0
if (groups.length > keepRecent) {
const older = groups.slice(0, Math.max(0, groups.length - keepRecent))
const recent = groups.slice(Math.max(0, groups.length - keepRecent))
// Drop nested prior compaction-only groups from "older" but fold text in.
function applyFullReplace(allGroups, keep) {
const split = bareAgentSplitLastUserAndRecent(allGroups)
/** @type {unknown[][]} */
const olderFlat = []
for (const g of older) {
if (g.length === 1 && bareAgentIsCompactionMessage(g[0])) {
olderFlat.push(g)
} else olderFlat.push(g)
let older = split.older
/** @type {unknown[]} */
let recent = split.recent
// If "recent after last user" is huge, keep only the tail of those messages
// by regrouping them and folding extras into older.
if (keep > 0 && recent.length > keep * 4) {
const tail = recent.slice(-(keep * 3))
const head = recent.slice(0, Math.max(0, recent.length - tail.length))
if (head.length) older = older.concat([head])
recent = tail
}
const summaryText = bareAgentBuildCompactionSummary(olderFlat, {
const summaryText = bareAgentBuildCompactionSummary(older, {
maxChars: summaryMax,
perMsg: mode === 'aggressive' ? 120 : 180
})
const summaryMsg = {
role: 'user',
content: summaryText,
bare_os_compaction: true
if (older.length && bareAgentIsDegenerateSummary(summaryText)) {
tiers.push('degenerate-keep')
return null
}
const reminder = bareAgentBuildSystemReminder({
autonomous: opts && opts.autonomous,
droppedGroups: older.length,
afterTokens: 0,
budget
})
droppedGroups += older.length
return bareAgentAssembleCompactedHistory({
system: system,
lastUserQuery: split.lastUserText,
recent: recent,
summaryText: summaryText,
reminder: reminder
})
}
if (groups.length > keepRecent) {
const next = applyFullReplace(groups, keepRecent)
if (next) {
out = next
tiers.push('full-replace')
}
/** @type {unknown[]} */
const next = []
if (system) next.push(system)
next.push(summaryMsg)
for (const g of recent) for (const m of g) next.push(m)
out = next
droppedGroups = older.length
tiers.push('rolling-summary')
}
// Still over: reduce keepRecent iteratively.
let guard = 0
while (
bareAgentCompactEstimateTokens(out) > budget &&
@@ -402,49 +671,10 @@ function bareAgentCompactMessagesForCtx(msgs, ctxSize, opts) {
keepRecent--
guard++
const again = bareAgentGroupMessageTurns(out)
// Re-split: system + optional summary + recent
/** @type {unknown[][]} */
const gs = again.groups
// Find existing summary at start of groups
let start = 0
if (gs[0] && gs[0].length === 1 && bareAgentIsCompactionMessage(gs[0][0])) {
start = 1
}
const body = gs.slice(start)
if (body.length <= keepRecent) break
const older = body.slice(0, body.length - keepRecent)
const recent = body.slice(body.length - keepRecent)
const priorSummary =
start === 1 && gs[0] && gs[0][0]
? bareAgentMessagePlainText(gs[0][0])
: ''
const folded = priorSummary
? [
[
{
role: 'user',
content: priorSummary,
bare_os_compaction: true
}
],
...older
]
: older
const summaryText = bareAgentBuildCompactionSummary(folded, {
maxChars: summaryMax,
perMsg: 120
})
/** @type {unknown[]} */
const next = []
if (again.system) next.push(again.system)
next.push({
role: 'user',
content: summaryText,
bare_os_compaction: true
})
for (const g of recent) for (const m of g) next.push(m)
if (again.groups.length <= keepRecent) break
const next = applyFullReplace(again.groups, keepRecent)
if (!next) break
out = next
droppedGroups += older.length
tiers.push('tighten-keep=' + String(keepRecent))
}