p = 'd2/rig-d2/make-d2-stubs.sh'
s = open(p, encoding='utf-8', newline='').read()

# The pwsh stub gains a PROCESS MODEL. The command lines are r10's ACTUAL ones and contain no
# reference to the isolated home -- that is the whole point of these arms.
old = """    *Win32_Process*)
      # The residual-identity probe. Its own positive control -- a nonzero enumeration
      # total -- is modelled here too, because a dead enumeration must not read as a
      # tidy host.
      case "${FP_RESIDUE_MODE:-none}" in
        dead)  printf 'RESIDUE_PROBE=UNREADABLE reason=empty-enumeration-is-not-an-empty-host\\n'; exit 0 ;;
        some)  printf 'RESIDUE_ENUM total=412\\n'
               printf 'RESIDUE pid=%s created=2026-09-13T00:00:00Z exe=[%s] cmd=[%s serve lan --bootstrap]\\n' \\
                 "${FP_RESIDUE_PID:-31337}" "${FP_EXE:-stub}" "${FP_EXE:-stub}"
               printf 'RESIDUE_PROBE=OK matched=1\\n'; exit 0 ;;
        *)     printf 'RESIDUE_ENUM total=412\\n'; printf 'RESIDUE_PROBE=OK matched=0\\n'; exit 0 ;;
      esac ;;"""

new = """    *Win32_Process*)
      # THE PROCESS MODEL. The two command lines below are r10's ACTUAL survivors and contain
      # NOTHING that names the isolated home -- that is exactly why a command-line predicate
      # could report a clean host over them, and why these arms exist.
      SUP_PID=${FP_SUP_PID:-4001}; BRAIN_PID=${FP_BRAIN_PID:-4002}
      SUP_CREATED='2026-09-13T02:00:00.000Z'; BRAIN_CREATED='2026-09-13T02:00:00.331Z'
      SUP_CMD="${FP_EXE:-stub} daemon run --detached"
      BRAIN_CMD="${FP_EXE:-stub} daemon brain --generation 0 --start-reason cold"
      MODEL="${FP_PROC_MODEL:-none}"
      case "$cmd" in
        *IDENT_PROBE*)
          # Capture time: the leg has just run, so unless the rig says otherwise both processes
          # exist and the pid file points at the supervisor.
          case "${FP_IDENT_MODE:-live}" in
            nopidfile)   printf 'IDENT_PROBE=UNREADABLE reason=no-pid-file\\n'; exit 0 ;;
            deadtable)   printf 'IDENT_PROBE=UNREADABLE reason=empty-enumeration-is-not-an-empty-host\\n'; exit 0 ;;
            notintable)  printf 'IDENT_ENUM total=412 supervisor_pid_from_file=%s\\n' "$SUP_PID"
                         printf 'IDENT_PROBE=UNREADABLE reason=supervisor-pid-%s-not-in-process-table\\n' "$SUP_PID"; exit 0 ;;
          esac
          [ -f "${SPT_D2_HOME:-/nonexistent}/daemon.pid" ] || { printf 'IDENT_PROBE=UNREADABLE reason=no-pid-file\\n'; exit 0; }
          printf 'IDENT_ENUM total=412 supervisor_pid_from_file=%s\\n' "$SUP_PID"
          printf 'IDENT role=supervisor pid=%s created=%s exe=[%s] cmd=[%s]\\n' "$SUP_PID" "$SUP_CREATED" "${FP_EXE:-stub}" "$SUP_CMD"
          printf 'IDENT role=brain pid=%s created=%s exe=[%s] cmd=[%s]\\n' "$BRAIN_PID" "$BRAIN_CREATED" "${FP_EXE:-stub}" "$BRAIN_CMD"
          printf 'IDENT_PROBE=OK tracked=2\\n'; exit 0 ;;
        *SUBJECT_EXE_PROBE*)
          case "$MODEL" in
            both)      printf 'SUBJECT_EXE pid=%s cmd=[%s]\\n' "$SUP_PID" "$SUP_CMD"
                       printf 'SUBJECT_EXE pid=%s cmd=[%s]\\n' "$BRAIN_PID" "$BRAIN_CMD"
                       printf 'SUBJECT_EXE_PROBE=OK running_the_pinned_executable=2 enum_total=412\\n' ;;
            brainonly) printf 'SUBJECT_EXE pid=%s cmd=[%s]\\n' "$BRAIN_PID" "$BRAIN_CMD"
                       printf 'SUBJECT_EXE_PROBE=OK running_the_pinned_executable=1 enum_total=412\\n' ;;
            unreadable) printf 'SUBJECT_EXE_PROBE=UNREADABLE reason=enumeration-failed\\n' ;;
            *)         printf 'SUBJECT_EXE_PROBE=OK running_the_pinned_executable=0 enum_total=412\\n' ;;
          esac
          exit 0 ;;
        *PROC=PRESENT*|*PROC=ABSENT*)
          # Cleanup's fresh re-query of ONE tracked pid.
          want="${SPT_D2_PID:-0}"
          case "$MODEL" in
            unreadable) printf 'PROC=UNREADABLE reason=empty-enumeration-is-not-an-empty-host\\n'; exit 0 ;;
            reused)
              # The pid answers, but it is somebody else's process: a bare pid match would call
              # this residue and be wrong.
              printf 'PROC=PRESENT pid=%s created=2026-09-13T01:00:00.000Z exe=[C:/Windows/System32/notepad.exe] cmd=[notepad.exe]\\n' "$want"
              exit 0 ;;
            both)
              if [ "$want" = "$SUP_PID" ]; then
                printf 'PROC=PRESENT pid=%s created=%s exe=[%s] cmd=[%s]\\n' "$SUP_PID" "$SUP_CREATED" "${FP_EXE:-stub}" "$SUP_CMD"; exit 0
              elif [ "$want" = "$BRAIN_PID" ]; then
                printf 'PROC=PRESENT pid=%s created=%s exe=[%s] cmd=[%s]\\n' "$BRAIN_PID" "$BRAIN_CREATED" "${FP_EXE:-stub}" "$BRAIN_CMD"; exit 0
              fi ;;
            brainonly)
              if [ "$want" = "$BRAIN_PID" ]; then
                printf 'PROC=PRESENT pid=%s created=%s exe=[%s] cmd=[%s]\\n' "$BRAIN_PID" "$BRAIN_CREATED" "${FP_EXE:-stub}" "$BRAIN_CMD"; exit 0
              fi ;;
          esac
          printf 'PROC=ABSENT pid=%s enum_total=412\\n' "$want"; exit 0 ;;
      esac
      printf 'PROC=ABSENT pid=%s enum_total=412\\n' "${SPT_D2_PID:-0}"; exit 0 ;;"""

assert s.count(old) == 1, 'residue stub block'
s = s.replace(old, new)
open(p, 'w', encoding='utf-8', newline='').write(s)
print('stub: r10-shaped process model installed')
