@@ -550,6 +550,33 @@ export namespace SessionCompaction {
550550 } )
551551 }
552552
553+ function shellSegmentBefore ( value : string , end : number ) : string {
554+ let start = 0
555+ let quote : "'" | '"' | undefined
556+ let escaped = false
557+ for ( let i = 0 ; i < end ; i ++ ) {
558+ const char = value [ i ]
559+ if ( escaped ) {
560+ escaped = false
561+ continue
562+ }
563+ if ( char === "\\" && quote !== "'" ) {
564+ escaped = true
565+ continue
566+ }
567+ if ( quote ) {
568+ if ( char === quote ) quote = undefined
569+ continue
570+ }
571+ if ( char === "'" || char === '"' ) {
572+ quote = char
573+ continue
574+ }
575+ if ( char === ";" || char === "|" || char === "&" || char === "\n" ) start = i + 1
576+ }
577+ return value . slice ( start , end )
578+ }
579+
553580 /**
554581 * Ledger text is persisted into a later model prompt, so treat every tool
555582 * argument as sensitive. This intentionally over-redacts opaque credentials
@@ -561,19 +588,32 @@ export namespace SessionCompaction {
561588 / (?: a p i [ _ - ] ? k e y | a c c e s s [ _ - ] ? k e y | a c c e s s [ _ - ] ? t o k e n | s e s s i o n [ _ - ] ? t o k e n | c l i e n t [ _ - ] ? s e c r e t | p r i v a t e [ _ - ] ? k e y | (?: ^ | [ _ - ] ) (?: k e y | t o k e n | s e c r e t | p a s s w o r d | p a s s w d | c r e d e n t i a l | s i g n a t u r e | a u t h o r i z a t i o n | c o o k i e ) (?: $ | [ _ - ] ) ) / i
562589 let masked = Telemetry . maskString ( value )
563590
564- // curl-style authentication flags are credentials even though the generic
565- // long-flag classifier cannot safely treat every `user` argument as secret.
566- // Cover spaced, equals, and attached short-flag forms.
567- masked = masked
568- . replace (
569- / ( ^ | \s ) ( - - u s e r ) ( = | \s + ) (?: " [ ^ " ] * " | ' [ ^ ' ] * ' | [ ^ \s , ; ] + ) / gi,
570- ( _match , lead : string , flag : string , separator : string ) => `${ lead } ${ flag } ${ separator } <redacted>` ,
571- )
572- . replace (
573- / ( ^ | \s ) ( - u ) (?: ( = | \s + ) (?: " [ ^ " ] * " | ' [ ^ ' ] * ' | [ ^ \s , ; ] + ) | ( [ ^ \s , ; ] + ) ) / gi,
574- ( _match , lead : string , flag : string , separator : string | undefined ) =>
575- `${ lead } ${ flag } ${ separator ?? "" } <redacted>` ,
576- )
591+ // `-u` is also a benign flag for commands such as `git push -u` and
592+ // `python -u`. Redact it as authentication only in the current curl shell
593+ // segment, or when the value itself has a user:password shape. Long
594+ // `--user` follows the same rule so task literals are not discarded merely
595+ // because an unrelated CLI chose that option name.
596+ masked = masked . replace (
597+ / ( ^ | \s ) ( - - u s e r | - u ) (?: ( = | \s + ) ( " [ ^ " ] * " | ' [ ^ ' ] * ' | [ ^ \s , ; ] + ) | ( [ ^ \s , ; ] + ) ) / gi,
598+ (
599+ match ,
600+ lead : string ,
601+ flag : string ,
602+ separator : string | undefined ,
603+ separatedValue : string | undefined ,
604+ attachedValue : string | undefined ,
605+ offset : number ,
606+ whole : string ,
607+ ) => {
608+ // Attached values are valid only for short `-u` (`-ualice:pass`).
609+ if ( flag . toLowerCase ( ) === "--user" && separator === undefined ) return match
610+ const rawValue = ( separatedValue ?? attachedValue ?? "" ) . replace ( / ^ [ " ' ] | [ " ' ] $ / g, "" )
611+ const curlContext = / (?: ^ | [ \s / ] ) c u r l (? = \s | $ ) / i. test ( shellSegmentBefore ( whole , offset + lead . length ) )
612+ const credentialShaped = / ^ [ ^ : / \s ] + : [ ^ / \s ] + $ / . test ( rawValue )
613+ if ( ! curlContext && ! credentialShaped ) return match
614+ return `${ lead } ${ flag } ${ separator ?? "" } <redacted>`
615+ } ,
616+ )
577617
578618 // Strip URL userinfo and signed/query material before applying structural
579619 // command redaction. This works for HTTP-compatible and custom schemes.
@@ -913,6 +953,22 @@ export namespace SessionCompaction {
913953 export const PIN_WORKING_SLACK = 2_000
914954 export const PIN_CARD_MAX_TOKENS = 500
915955
956+ const TASK_PIN_FRAME = {
957+ open : "<system-reminder>" ,
958+ description :
959+ "Original task — authoritative over any summary. The conversation above was compacted into a summary; the task below is the user's own instruction, reproduced verbatim. If the summary and this task conflict, this task wins." ,
960+ close : "</system-reminder>" ,
961+ } as const
962+
963+ export function renderTaskPin ( body : string ) : string {
964+ return [ TASK_PIN_FRAME . open , TASK_PIN_FRAME . description , "" , body , TASK_PIN_FRAME . close ] . join ( "\n" )
965+ }
966+
967+ /** Tokens available for the verbatim body after the fixed reminder frame. */
968+ export function taskPinBodyBudget ( capTokens : number ) : number {
969+ return Math . max ( 0 , capTokens - Token . estimate ( renderTaskPin ( "" ) ) )
970+ }
971+
916972 export function pinEnabled ( cfg : ConfigInfo ) {
917973 return cfg . compaction ?. pin_task !== false
918974 }
@@ -1267,9 +1323,10 @@ When constructing the summary, try to stick to this template:
12671323 // small-window session, which would otherwise tell the summarizer to omit
12681324 // the task while no pin exists to compensate. Layered as an ADDITION to
12691325 // whichever summary prompt is active — never a replacement.
1326+ const taskPinBudget = pinBudget ( { cfg, model : sessionModel , sessionID : input . sessionID } )
12701327 if (
12711328 pinEnabled ( cfg ) &&
1272- pinBudget ( { cfg , model : sessionModel , sessionID : input . sessionID } ) > 0 &&
1329+ taskPinBodyBudget ( taskPinBudget ) > 0 &&
12731330 hasPinnableTask ( input . unfilteredMessages ?? input . messages )
12741331 )
12751332 promptText += "\n\n" + PIN_SUMMARY_ADDITION
0 commit comments