11/**
2- * Resolve "which session am I?" — the heart of Weaver, validated by the Phase 0 spike.
3- * Ladder: explicit override → harness-native session id → controlling TTY (self → ancestry).
4- * Never returns a shared anonymous key: callers that require identity fail gracefully.
2+ * Resolve "which session am I?" — the heart of Weaver.
3+ * Identity ladder: explicit override → harness-native session id → controlling TTY
4+ * (self → ancestry). Never returns a shared anonymous key: callers that require identity
5+ * fail gracefully.
6+ * The harness *label* resolves separately: env signals first, then known executable names
7+ * in the process ancestry — harness env vars are not a stable API (OpenCode set
8+ * OPENCODE_RUN_ID through v1.16.x and removed it in v1.17.0 with no replacement).
59 */
610
711import { execFileSync } from "node:child_process" ;
812import os from "node:os" ;
13+ import path from "node:path" ;
914import type { IdSource } from "../store/store.ts" ;
1015
1116export interface Identity {
@@ -16,9 +21,14 @@ export interface Identity {
1621
1722type Env = Record < string , string | undefined > ;
1823
19- /** Per-harness session-id env vars, confirmed in Phase 0. Order = precedence. */
24+ /**
25+ * Per-harness session-id env vars. Order = precedence. These come and go with harness
26+ * releases: OpenCode ≤1.16.x set OPENCODE_RUN_ID (removed in v1.17.0 with no replacement);
27+ * OPENCODE_SESSION_ID is its proposed successor (anomalyco/opencode#12158).
28+ */
2029export const HARNESS_SESSION_ENVS : ReadonlyArray < readonly [ label : string , env : string ] > = [
2130 [ "claude-code" , "CLAUDE_CODE_SESSION_ID" ] ,
31+ [ "opencode" , "OPENCODE_SESSION_ID" ] ,
2232 [ "opencode" , "OPENCODE_RUN_ID" ] ,
2333 [ "codex" , "CODEX_THREAD_ID" ] ,
2434] ;
@@ -30,6 +40,8 @@ export interface ResolveOpts {
3040 host ?: string ;
3141 /** Injection seam for tests; defaults to the real `ps`-based lookup. */
3242 ttyResolver ?: ( pid : number ) => { device : string ; viaAncestry : boolean } | null ;
43+ /** Injection seam for tests; defaults to the real `ps`-based ancestry walk. */
44+ harnessResolver ?: ( pid : number ) => string | null ;
3345}
3446
3547function parseSessionArg ( argv : string [ ] ) : string | undefined {
@@ -51,6 +63,46 @@ function detectLabel(env: Env): string {
5163
5264const isRealTty = ( t : string ) : boolean => ! ! t && ! / ^ \? + $ / . test ( t ) ;
5365
66+ /** Executable basenames that identify a harness when seen in the process ancestry. */
67+ const HARNESS_PROCESS_NAMES : ReadonlyMap < string , string > = new Map ( [
68+ [ "claude" , "claude-code" ] ,
69+ [ "opencode" , "opencode" ] ,
70+ [ "codex" , "codex" ] ,
71+ [ "pi" , "pi" ] ,
72+ [ "cursor-agent" , "cursor" ] ,
73+ ] ) ;
74+
75+ /**
76+ * Walk the process ancestry looking for a known harness executable. Labels harnesses that
77+ * expose no env signal to subprocesses (e.g. OpenCode ≥1.17). Label only — never identity.
78+ */
79+ export function detectHarnessFromAncestry ( pid : number ) : string | null {
80+ const read = ( p : number ) : { ppid : number ; comm : string } | null => {
81+ try {
82+ const raw = execFileSync ( "ps" , [ "-o" , "ppid=,comm=" , "-p" , String ( p ) ] , {
83+ stdio : [ "ignore" , "pipe" , "ignore" ] ,
84+ } )
85+ . toString ( )
86+ . trim ( ) ;
87+ const m = / ^ ( \d + ) \s + ( .* ) $ / . exec ( raw ) ;
88+ return m ? { ppid : Number ( m [ 1 ] ) , comm : m [ 2 ] ?? "" } : null ;
89+ } catch {
90+ return null ;
91+ }
92+ } ;
93+
94+ let cur = pid ;
95+ for ( let i = 0 ; i < 8 ; i ++ ) {
96+ const row = read ( cur ) ;
97+ if ( ! row ) return null ;
98+ const label = HARNESS_PROCESS_NAMES . get ( path . basename ( row . comm ) . toLowerCase ( ) ) ;
99+ if ( label ) return label ;
100+ if ( ! row . ppid || row . ppid <= 1 ) return null ;
101+ cur = row . ppid ;
102+ }
103+ return null ;
104+ }
105+
54106/** Controlling terminal of the process, then its nearest ancestor. Returns null if none. */
55107export function resolveTtyDevice ( pid : number ) : { device : string ; viaAncestry : boolean } | null {
56108 const ps = ( p : number , field : string ) : string => {
@@ -83,21 +135,28 @@ export function resolveIdentity(opts: ResolveOpts = {}): Identity | null {
83135 const env = opts . env ?? ( process . env as Env ) ;
84136 const host = opts . host ?? os . hostname ( ) ;
85137 const argv = opts . argv ?? process . argv . slice ( 2 ) ;
138+ const pid = opts . pid ?? process . pid ;
139+
140+ // Env signals first; harness executables in the ancestry when the env says nothing.
141+ const label = ( ) : string => {
142+ const fromEnv = detectLabel ( env ) ;
143+ if ( fromEnv !== "unknown" ) return fromEnv ;
144+ return ( opts . harnessResolver ?? detectHarnessFromAncestry ) ( pid ) ?? "unknown" ;
145+ } ;
86146
87147 // 1. explicit override
88148 const explicit = ( parseSessionArg ( argv ) ?? env . WEAVER_SESSION ) ?. trim ( ) ;
89- if ( explicit ) return { key : `explicit:${ explicit } @${ host } ` , source : "explicit" , label : detectLabel ( env ) } ;
149+ if ( explicit ) return { key : `explicit:${ explicit } @${ host } ` , source : "explicit" , label : label ( ) } ;
90150
91151 // 2. harness-native session id (most reliable on the tool-call path)
92- for ( const [ label , key ] of HARNESS_SESSION_ENVS ) {
152+ for ( const [ harness , key ] of HARNESS_SESSION_ENVS ) {
93153 const value = env [ key ] ;
94- if ( value ) return { key : `harness:${ label } :${ value } @${ host } ` , source : "harness" , label } ;
154+ if ( value ) return { key : `harness:${ harness } :${ value } @${ host } ` , source : "harness" , label : harness } ;
95155 }
96156
97157 // 3. controlling TTY (self → nearest ancestor)
98- const tty = ( opts . ttyResolver ?? resolveTtyDevice ) ( opts . pid ?? process . pid ) ;
99- if ( tty )
100- return { key : `tty:${ tty . device } @${ host } ` , source : tty . viaAncestry ? "ancestry" : "tty" , label : detectLabel ( env ) } ;
158+ const tty = ( opts . ttyResolver ?? resolveTtyDevice ) ( pid ) ;
159+ if ( tty ) return { key : `tty:${ tty . device } @${ host } ` , source : tty . viaAncestry ? "ancestry" : "tty" , label : label ( ) } ;
101160
102161 // 4. none — caller decides (observer reads ok; mutating commands fail with a hint)
103162 return null ;
0 commit comments