okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
4fb068a8c
main
1/** 2* A reply the model could not finish (AGENT-LOST, 11-01): pure helpers, no I/O. 3* 4* A model reply has a ceiling (the "output budget"). A thinking model that then writes a whole file in one tool call can spend all of it 5* before the call is closed. The Harness drops a tool call that was not closed (its arguments are half a JSON text), the turn ends with 6* the reason `max-tokens`, and until now the person saw nothing: no file, no card, no sentence. Here: 7* 8* - `replyCap(window)` how much one reply may produce (the request's ceiling), much larger than the budget the Harness reserves 9* for compaction (profile.mjs `localOutputBudget`): the Harness still clamps it to what the window has left. 10* - `ToolDraft` what a tool call that is being written looks like so far: its name, the file it is for, a size, a tail of the text. 11* - `cutNotice(...)` the plain sentence for the chat, and `cutNudge(...)` the message that lets the agent carry on in smaller pieces. 12*/ 13 14/** Half the window, at most 64K tokens, and never less than the budget compaction reserves. The Harness clamps it to the room left (window minus prompt). */ 15export function replyCap ( window , reserved = 0 ) { 16const w = Number . isFinite ( window ) && window > 0 ?Math . floor ( window ) :32768 ; 17return Math . max ( Math . floor ( reserved ) || 0 , Math . min ( Math . floor ( w / 2 ), 65536 )); 18} 19 20const ESCAPES = { n :'\n' , t :'\t' , r :'' , '"' :'"' , '\\' :'\\' , '/' :'/' , b :'' , f :'' }; 21 22/** A tail of a JSON string body, read back to text: `\n` becomes a newline, `\"` a quote. A half-written escape at the end is dropped. */ 23export function unescapeJsonTail ( raw ) { 24let text = String ( raw ?? '' ); 25const cut = / \\ ( u [ 0 - 9a - fA - F ]{ 0 , 3 }) ? $ / . exec ( text ); 26if ( cut ) text = text . slice ( 0 , cut . index ); 27return text . replace ( / \\ ( u [ 0 - 9a - fA - F ]{ 4 } | [ nrtbf" \\ / ]) / g , ( _ , e ) => ( e [ 0 ] === 'u' ?String . fromCharCode ( parseInt ( e . slice ( 1 ), 16 )) :ESCAPES [ e ] ?? e )); 28} 29 30const TARGET = / " (?: file_path | path | filePath ) "\s * :\s * " ((?:[ ^ " \\ ] | \\. ) * ) " / ; 31const URL_ARG = / "url"\s * :\s * " ((?:[ ^ " \\ ] | \\. ) * ) " / ; 32/** The long text argument of the common writing tools; the tail of the arguments is previewed after the first one of these. */ 33const BODY = / " (?: content | new_string | new_str | text | code | body | command | file_text ) "\s * :\s * " / ; 34 35/** One tool call being written: the model's streamed arguments, kept small. */ 36export class ToolDraft { 37constructor ( id , name = '' ) { 38this . id = id ; this . name = name ; 39this . bytes = 0 ; this . lines = 0 ; 40this . head = '' ; // the first 4,000 characters: the body argument usually starts here 41this . where = '' ; // the file (or address) the call is for, once seen: a model may write it before or after the text 42this . carry = '' ; // the last 300 characters, so a name split between two pieces is still seen 43this . tail = '' ; // the last 2,400 characters: what is being written now 44this . at = 0 ; // when a frame was last sent for it 45} 46 47add ( piece ) { 48const text = String ( piece ?? '' ); 49if ( ! text ) return ; 50this . bytes += text . length ; 51for ( let i = text . indexOf ( '\\n' ); i >= 0 ; i = text . indexOf ( '\\n' , i + 2 )) this . lines += 1 ; 52if ( this . head . length < 4000 ) this . head += text . slice ( 0 , 4000 - this . head . length ); 53this . tail = ( this . tail + text ). slice ( - 2400 ); 54if ( ! this . where ) { 55const seen = this . carry + text ; 56const raw = ( TARGET . exec ( seen ) ?? URL_ARG . exec ( seen ))?.[ 1 ]; 57if ( raw !== undefined ) { try { this . where = JSON . parse ( `" ${ raw } "` ); } catch { this . where = unescapeJsonTail ( raw ); } } 58this . carry = seen . slice ( - 300 ); 59} 60} 61 62get target () { return this . where ; } 63 64/** The text of the body so far, only its last `max` characters; empty until the body argument has started. */ 65preview ( max = 1600 ) { 66const start = BODY . exec ( this . head ); 67if ( ! start ) return '' ; 68const bodyAt = start . index + start [ 0 ]. length ; // where the body begins, counted in streamed characters 69if ( bodyAt >= this . bytes ) return '' ; 70const tailAt = this . bytes - this . tail . length ; // where the kept tail begins 71const body = unescapeJsonTail ( this . tail . slice ( Math . max ( 0 , bodyAt - tailAt ))); 72return body . length > max ?body . slice ( - max ) :body ; 73} 74 75/** The frame for the page. */ 76frame ({ done= false , ...extra } = {}) { 77return { type :'agent-tool-draft' , draft :{ id :this . id , name :this . name , target :this . target , bytes :this . bytes , lines :this . lines , preview :done ?'' :this . preview (), ...extra , done} }; 78} 79} 80 81const kb = n => ( n >= 10_000 ?` ${ Math . round ( n / 1000 )} KB` :` ${( n / 1000 ). toFixed ( 1 )} KB` ); 82 83/** Calls the model was still writing when its reply was cut: [{ name, target, bytes }]. */ 84export function cutCalls ( drafts ) { 85return [ ...drafts ]. filter ( d => d . bytes > 0 ). map ( d => ({ name :d . name , target :d . target , bytes :d . bytes })); 86} 87 88const withTokens = tokens => ( tokens > 0 ?` ( ${ Number ( tokens ). toLocaleString ( 'en-US' )} tokens)` :'' ); 89 90/** The sentence for the person, with the catalog slots it needs: { text, key, values }. `again` is true when the agent was already asked to carry on. */ 91export function cutNotice ({ calls= [], tokens= 0 , again= false , retrying= false } = {}) { 92const call = calls . find ( c => c . target ) ?? calls [ 0 ] ?? null ; 93const file = call ?. target ?String ( call . target ). split ( / [ \\ / ] / ). filter ( Boolean ). at ( - 1 ) :'' ; 94const what = file || ( / ^ ( write | edit ) $ / . test ( call ?. name ?? '' ) ?'a file' :call ?. name ); 95if ( call && retrying ) { 96const key = 'That reply reached its length limit{0} while writing {1} ({2}), so that write did not happen. I asked the assistant to look at what is on disk and write the rest in smaller parts.' ; 97const values = [ withTokens ( tokens ), what , kb ( call . bytes )]; 98return { key, values, text :fill ( key , values ) }; 99} 100if ( call ) { 101const key = 'The reply reached its length limit{0} while writing {1} ({2}), so that write did not happen. Say “continue” and it will look at what is on disk and go on in smaller parts.' ; 102const values = [ withTokens ( tokens ), what , kb ( call . bytes )]; 103return { key, values, text :fill ( key , values ) }; 104} 105if ( retrying ) { 106const key = 'That reply reached its length limit{0} and was cut off. I asked the assistant to carry on.' ; 107return { key, values :[ withTokens ( tokens )], text :fill ( key , [ withTokens ( tokens )]) }; 108} 109const key = 'The reply reached its length limit{0} and was cut off. Say “continue” to go on.' ; 110return { key, values :[ withTokens ( tokens )], text :fill ( key , [ withTokens ( tokens )]) }; 111} 112 113export function fill ( template , values ) { return String ( template ). replace ( / \{ ( \d + ) \} / g , ( _ , i ) => String ( values [ Number ( i )] ?? '' )); } 114 115/** The message sent to the assistant so it carries on. It is not shown as the person's message. */ 116export function cutNudge ({ calls= [], tokens= 0 } = {}) { 117const call = calls . find ( c => c . target ) ?? calls [ 0 ] ?? null ; 118if ( ! call ) { 119return 'Your last reply was cut off by the output limit before it was finished. Carry on from where it stopped without repeating what you already wrote, and keep your thinking short.' ; 120} 121const file = call . target || 'the file' ; 122return `Your last reply was cut off by the output limit ${ tokens > 0 ? ` ( ${ Number ( tokens ). toLocaleString ( 'en-US' )} tokens)` : '' } while you were writing ${ file } in one tool call, so that call did not run and nothing was changed by it. First look at what is on disk now (list the folder, read the end of the file). Do not delete or start over a file that is already complete. If it is not complete, write what is missing in several smaller parts (about 150 lines or fewer each) with edits or appends, and read the end of the file back when you are done. Keep your thinking short.` ; 123}