@@ -21,6 +21,16 @@ export interface ManagedSubagentDefaults {
2121 reasoningEffort ?: string ;
2222}
2323
24+ /**
25+ * The exact values the last injection wrote under a marker, as recorded in the
26+ * injection journal. A marker proves only that OpenCodex once wrote the line;
27+ * this record proves which value it wrote. A marked value that no longer
28+ * matches it was edited by someone else — the user, or a Codex settings writer
29+ * that keeps comments — and is adopted as user-owned instead of reverted.
30+ * Absent (`null`/`undefined`) means no evidence, and the marker alone decides.
31+ */
32+ export type WrittenSubagentDefaults = Partial < Record < ManagedSubagentDefaultKey , string > > ;
33+
2434export interface ManagedSubagentDefaultsConflict {
2535 key : ManagedSubagentDefaultKey ;
2636 line : number ;
@@ -303,15 +313,50 @@ function invalidInput(content: string, error: string): ManagedSubagentDefaultsTr
303313 return { ok : false , changed : false , content, conflicts : [ ] , error } ;
304314}
305315
316+ /** The decoded string value of a single-line `key = "value"` assignment, or null. */
317+ function assignedString ( line : SourceLine , key : ManagedSubagentDefaultKey ) : string | null {
318+ try {
319+ const value = ( Bun . TOML . parse ( line . text ) as Record < string , unknown > ) [ key ] ;
320+ return typeof value === "string" ? value : null ;
321+ } catch { // no-excuse-ok: catch -- an unparsable value is not edit evidence; the replace step rejects it.
322+ return null ;
323+ }
324+ }
325+
326+ /**
327+ * Marker lines whose value differs from what OpenCodex recorded writing. The
328+ * marker is dropped so the line reads as what it now is: a user-owned value.
329+ */
330+ function userEditedMarkers (
331+ lines : readonly SourceLine [ ] ,
332+ shape : TomlShape ,
333+ written : WrittenSubagentDefaults | null | undefined ,
334+ ) : number [ ] {
335+ if ( ! written ) return [ ] ;
336+ const stale : number [ ] = [ ] ;
337+ for ( const definition of shape . definitions . values ( ) ) {
338+ const recorded = written [ definition . key ] ;
339+ if ( ! definition . owned || typeof recorded !== "string" ) continue ;
340+ const current = assignedString ( lines [ definition . index ] ! , definition . key ) ;
341+ if ( current !== null && current !== recorded ) stale . push ( definition . index - 1 ) ;
342+ }
343+ return stale ;
344+ }
345+
306346/**
307347 * Add/update opencodex-owned native subagent defaults, or remove them with
308348 * `defaults = null`. Unmarked target keys are user-owned: they are retained and
309349 * returned as conflicts rather than overwritten. Ambiguous TOML is rejected
310350 * byte-for-byte so callers never have to guess what was changed.
351+ *
352+ * `written` is the journal's record of the values the last injection wrote.
353+ * When supplied, a marked value that differs from it is user-edited: its marker
354+ * is dropped and the value is treated exactly like any other user-owned key.
311355 */
312356export function transformManagedSubagentDefaults (
313357 content : string ,
314358 defaults : ManagedSubagentDefaults | null ,
359+ written ?: WrittenSubagentDefaults | null ,
315360) : ManagedSubagentDefaultsTransformResult {
316361 if ( defaults !== null ) {
317362 if ( typeof defaults . model !== "string" || defaults . model . length === 0 ) {
@@ -329,14 +374,20 @@ export function transformManagedSubagentDefaults(
329374 }
330375 }
331376
332- const lines = splitSourceLines ( content ) ;
377+ let lines = splitSourceLines ( content ) ;
333378 if ( defaults === null && ! lines . some ( line =>
334379 markerLine ( line , MANAGED_SUBAGENT_DEFAULT_MARKER )
335380 || markerLine ( line , MANAGED_AGENTS_TABLE_MARKER ) ) ) {
336381 return { ok : true , changed : false , content, conflicts : [ ] } ;
337382 }
338- const analysis = analyzeToml ( lines ) ;
383+ let analysis = analyzeToml ( lines ) ;
339384 if ( "error" in analysis ) return invalidInput ( content , analysis . error ) ;
385+ const stale = new Set ( userEditedMarkers ( lines , analysis . shape , written ) ) ;
386+ if ( stale . size > 0 ) {
387+ lines = lines . filter ( ( _ , index ) => ! stale . has ( index ) ) ;
388+ analysis = analyzeToml ( lines ) ;
389+ if ( "error" in analysis ) return invalidInput ( content , analysis . error ) ;
390+ }
340391 const { shape } = analysis ;
341392 const eol = dominantEol ( lines ) ;
342393 const desired = new Map < ManagedSubagentDefaultKey , string > ( ) ;
@@ -361,8 +412,10 @@ export function transformManagedSubagentDefaults(
361412 // Model and effort are one effective native-default pair. A partial write
362413 // could apply a newly managed effort to a user's model (or vice versa), so
363414 // any desired-key ownership conflict makes the whole enable/update a no-op.
415+ // Only an adopted user edit's stale marker is dropped from the result.
364416 if ( conflicts . length > 0 ) {
365- return { ok : true , changed : false , content, conflicts } ;
417+ const adopted = joinSourceLines ( lines ) ;
418+ return { ok : true , changed : adopted !== content , content : adopted , conflicts } ;
366419 }
367420 }
368421
0 commit comments