# R18: Settlement mixed selection and thread isolation

Before [e9236274a3](https://git.cafe/pingdotgg/t3code/commit/e9236274a3a6e0c731a7df4092db1e686f039068); after [63e44670dd](https://git.cafe/pingdotgg/t3code/commit/63e44670dd1d222cf63619d5e02415f2a9fd3bc2).

Before run: `/tmp/lead-24640-r1/capture/2026-10-27T14-26-11.651Z-before-R18`. After run: `/tmp/lead-24640-r1/capture/2026-10-27T14-26-11.650Z-after-R18`.

Environment: fs-31 Linux x86_64, Node v26.12.1; no viewport/theme/DPR/font scaling. Fresh per-run app databases. Same RPC fixture on each side. Native sidecars compiled with isolated Rust 1.99.1 where used.

A fresh settlement table closes the idle target-thread shell. Its busy sibling and a busy terminal in another thread remain usable. Closed/remove and final metadata are captured.

Wrapper timings exclude Python startup and stream drain. Acquisition timings use the real terminal.posixProcessTableSnapshot trace span. An idle gap is END→next START, never start-to-start. Before delay=min(1000×2^failureCount,60000)ms; after delay=min(max(1000×2^failureCount,4×measured fallback acquisition),60000)ms. Native-failed production ticks increment failureCount even when fallback succeeds; successful absent-supplier harness ticks reset it. Native requests and terminal processing can add overhead.

| Event | Before ([e9236274a3](https://git.cafe/pingdotgg/t3code/commit/e9236274a3a6e0c731a7df4092db1e686f039068)) | After ([63e44670dd](https://git.cafe/pingdotgg/t3code/commit/63e44670dd1d222cf63619d5e02415f2a9fd3bc2)) | Δ |
|---|---|---|---|
| Fallback query 1 | R18-settle, closeIdle; shim START 2026-10-27T14:26:18.941Z (mono 921768064566199); END 2026-10-27T14:26:18.953Z (mono 921768076171079); 11.6ms, exit 0; acquisition 2026-10-27T14:26:18.903Z→2026-10-27T14:26:18.961Z, 58.8ms | R18-settle, closeIdle; shim START 2026-10-27T14:26:18.929Z (mono 921768052459308); END 2026-10-27T14:26:18.942Z (mono 921768065519979); 13.1ms, exit 0; acquisition 2026-10-27T14:26:18.891Z→2026-10-27T14:26:18.949Z, 57.5ms | Same idle-only/thread selection |
| Fallback query 2 | R18-settle, background; shim START 2026-10-27T14:26:19.412Z (mono 921768535024200); END 2026-10-27T14:26:19.426Z (mono 921768549133478); 14.1ms, exit 0; acquisition 2026-10-27T14:26:19.371Z→2026-10-27T14:26:19.433Z, 61.8ms | R18-settle, background; shim START 2026-10-27T14:26:19.481Z (mono 921768604444878); END 2026-10-27T14:26:19.498Z (mono 921768621421394); 17.0ms, exit 0; acquisition 2026-10-27T14:26:19.447Z→2026-10-27T14:26:19.506Z, 59.1ms | Measured duration/result above |
| Background completion→next-query gaps | No subsequent background pair | No subsequent background pair | Same idle-only/thread selection |
| Terminal event 1 | 2026-10-27T14:26:19.305Z `fixture-thread-a/term-0` closed, seq=3 | 2026-10-27T14:26:19.291Z `fixture-thread-a/term-0` closed, seq=3 | Compare identity/sequence; subscriptions deduplicated |
| Terminal event 2 | 2026-10-27T14:26:19.438Z `fixture-thread-a/term-1` activity, seq=6, busy=True, label=`fixture-child` | 2026-10-27T14:26:19.523Z `fixture-thread-a/term-1` activity, seq=5, busy=True, label=`fixture-child` | Compare identity/sequence; subscriptions deduplicated |
| Terminal event 3 | 2026-10-27T14:26:19.439Z `fixture-thread-b/term-10` activity, seq=6, busy=True, label=`fixture-child` | 2026-10-27T14:26:19.524Z `fixture-thread-b/term-10` activity, seq=6, busy=True, label=`fixture-child` | Compare identity/sequence; subscriptions deduplicated |
| Terminal event 4 | 2026-10-27T14:26:20.089Z `fixture-thread-a/term-1` closed, seq=8 | 2026-10-27T14:26:20.135Z `fixture-thread-a/term-1` closed, seq=7 | Compare identity/sequence; subscriptions deduplicated |
| Terminal event 5 | 2026-10-27T14:26:20.093Z `fixture-thread-b/term-10` closed, seq=7 | 2026-10-27T14:26:20.139Z `fixture-thread-b/term-10` closed, seq=7 | Compare identity/sequence; subscriptions deduplicated |
| Peak all fallback queries | 1 | 1 | Independent closeIdle may overlap one background query |
| Peak background fallback queries | 1 | 1 | Independent closeIdle may overlap one background query |

## Before scripted milestones and final observation states
```json
[
  {
    "kind": "control",
    "utc": "2026-10-27T14:26:11.866Z",
    "mono_ns": "921760989140640",
    "ms": 212.233833,
    "phase": "R18",
    "mode": "valid",
    "delay": 0
  },
  {
    "kind": "server-ready",
    "utc": "2026-10-27T14:26:18.296Z",
    "mono_ns": "921767419040576",
    "ms": 6642.136935
  },
  {
    "kind": "rpc-connected",
    "utc": "2026-10-27T14:26:18.416Z",
    "mono_ns": "921767539297799",
    "ms": 6762.395711
  },
  {
    "kind": "child-ready",
    "utc": "2026-10-27T14:26:18.615Z",
    "mono_ns": "921767738368399",
    "ms": 6961.460199,
    "threadId": "fixture-thread-a",
    "terminalId": "term-1",
    "childPid": 3403363,
    "marker": "READY_R18_1"
  },
  {
    "kind": "child-ready",
    "utc": "2026-10-27T14:26:18.670Z",
    "mono_ns": "921767792887623",
    "ms": 7015.979854,
    "threadId": "fixture-thread-b",
    "terminalId": "term-10",
    "childPid": 3403381,
    "marker": "READY_R18_10"
  },
  {
    "kind": "control",
    "utc": "2026-10-27T14:26:18.870Z",
    "mono_ns": "921767992863100",
    "ms": 7215.955682,
    "phase": "R18-settle",
    "mode": "valid",
    "delay": 0
  },
  {
    "kind": "settlement-cleanup-completed",
    "utc": "2026-10-27T14:26:19.477Z",
    "mono_ns": "921768600053610",
    "ms": 7823.146673,
    "threadId": "fixture-thread-a",
    "span": {
      "type": "effect-span",
      "name": "ThreadSettlementServiceV2.cleanUpSettledThread",
      "traceId": "370d4ca576d4b0742e3d13dbb2eb8601",
      "spanId": "ff2c0ba52653b80d",
      "parentSpanId": "fa20373bb4847323",
      "sampled": true,
      "kind": "internal",
      "startTimeUnixNano": "1793111178899417799",
      "endTimeUnixNano": "1793111179304129204",
      "durationMs": 404.711405,
      "attributes": {},
      "events": [],
      "links": [],
      "exit": {
        "_tag": "Success"
      }
    }
  },
  {
    "kind": "scenario-complete",
    "utc": "2026-10-27T14:26:20.082Z",
    "mono_ns": "921769204878201",
    "ms": 8427.969621
  },
  {
    "kind": "cleanup-start",
    "utc": "2026-10-27T14:26:20.214Z",
    "mono_ns": "921769337264618",
    "ms": 8560.361126,
    "pids": [
      3402377,
      3402378,
      3403356,
      3403363,
      3403374,
      3403381
    ],
    "ptyPids": [
      3403351,
      3403356,
      3403363,
      3403374,
      3403381
    ]
  },
  {
    "kind": "cleanup-end",
    "utc": "2026-10-27T14:26:22.406Z",
    "mono_ns": "921771529472730",
    "ms": 10752.568127,
    "survivorsBeforeFinalKill": [
      3403374,
      3403356,
      3402378,
      3402377
    ],
    "remaining": []
  }
]
```
Metadata snapshots taken before and after driver cleanup:
```json
[
  {
    "kind": "state",
    "utc": "2026-10-27T14:26:20.081Z",
    "mono_ns": "921769204697172",
    "ms": 8427.791617,
    "terminals": [
      {
        "threadId": "fixture-thread-a",
        "terminalId": "term-1",
        "cwd": "/tmp/lead-24640-r1/capture/2026-10-27T14-26-11.651Z-before-R18",
        "worktreePath": null,
        "status": "running",
        "pid": 3403356,
        "exitCode": null,
        "exitSignal": null,
        "hasRunningSubprocess": true,
        "label": "fixture-child",
        "updatedAt": "2026-10-27T14:26:19.479Z"
      },
      {
        "threadId": "fixture-thread-b",
        "terminalId": "term-10",
        "cwd": "/tmp/lead-24640-r1/capture/2026-10-27T14-26-11.651Z-before-R18",
        "worktreePath": null,
        "status": "running",
        "pid": 3403374,
        "exitCode": null,
        "exitSignal": null,
        "hasRunningSubprocess": true,
        "label": "fixture-child",
        "updatedAt": "2026-10-27T14:26:19.436Z"
      }
    ]
  },
  {
    "kind": "state",
    "utc": "2026-10-27T14:26:20.194Z",
    "mono_ns": "921769317596301",
    "ms": 8540.690045,
    "terminals": []
  }
]
```

## After scripted milestones and final observation states
```json
[
  {
    "kind": "control",
    "utc": "2026-10-27T14:26:11.877Z",
    "mono_ns": "921761000577374",
    "ms": 224.86558,
    "phase": "R18",
    "mode": "valid",
    "delay": 0
  },
  {
    "kind": "server-ready",
    "utc": "2026-10-27T14:26:18.297Z",
    "mono_ns": "921767420260695",
    "ms": 6644.551044
  },
  {
    "kind": "rpc-connected",
    "utc": "2026-10-27T14:26:18.417Z",
    "mono_ns": "921767539846658",
    "ms": 6764.1381
  },
  {
    "kind": "child-ready",
    "utc": "2026-10-27T14:26:18.600Z",
    "mono_ns": "921767722889482",
    "ms": 6947.177477,
    "threadId": "fixture-thread-a",
    "terminalId": "term-1",
    "childPid": 3403355,
    "marker": "READY_R18_1"
  },
  {
    "kind": "child-ready",
    "utc": "2026-10-27T14:26:18.651Z",
    "mono_ns": "921767774599445",
    "ms": 6998.886168,
    "threadId": "fixture-thread-b",
    "terminalId": "term-10",
    "childPid": 3403373,
    "marker": "READY_R18_10"
  },
  {
    "kind": "control",
    "utc": "2026-10-27T14:26:18.852Z",
    "mono_ns": "921767975316103",
    "ms": 7199.604168,
    "phase": "R18-settle",
    "mode": "valid",
    "delay": 0
  },
  {
    "kind": "settlement-cleanup-completed",
    "utc": "2026-10-27T14:26:19.522Z",
    "mono_ns": "921768645451703",
    "ms": 7869.739779,
    "threadId": "fixture-thread-a",
    "span": {
      "type": "effect-span",
      "name": "ThreadSettlementServiceV2.cleanUpSettledThread",
      "traceId": "815861c3f0f214ac73dbfe0731e7e762",
      "spanId": "0c03321969ef3a3f",
      "parentSpanId": "de8b83ac8d097ecf",
      "sampled": true,
      "kind": "internal",
      "startTimeUnixNano": "1793111178881860206",
      "endTimeUnixNano": "1793111179291044241",
      "durationMs": 409.184035,
      "attributes": {},
      "events": [],
      "links": [],
      "exit": {
        "_tag": "Success"
      }
    }
  },
  {
    "kind": "scenario-complete",
    "utc": "2026-10-27T14:26:20.127Z",
    "mono_ns": "921769250363498",
    "ms": 8474.650271
  },
  {
    "kind": "cleanup-start",
    "utc": "2026-10-27T14:26:20.256Z",
    "mono_ns": "921769379523422",
    "ms": 8603.814212,
    "pids": [
      3402379,
      3402382,
      3403353,
      3403355,
      3403371,
      3403373
    ],
    "ptyPids": [
      3403349,
      3403353,
      3403355,
      3403371,
      3403373
    ]
  },
  {
    "kind": "cleanup-end",
    "utc": "2026-10-27T14:26:22.407Z",
    "mono_ns": "921771530784832",
    "ms": 10755.078337,
    "survivorsBeforeFinalKill": [],
    "remaining": []
  }
]
```
Metadata snapshots taken before and after driver cleanup:
```json
[
  {
    "kind": "state",
    "utc": "2026-10-27T14:26:20.127Z",
    "mono_ns": "921769250211844",
    "ms": 8474.50088,
    "terminals": [
      {
        "threadId": "fixture-thread-a",
        "terminalId": "term-1",
        "cwd": "/tmp/lead-24640-r1/capture/2026-10-27T14-26-11.650Z-after-R18",
        "worktreePath": null,
        "status": "running",
        "pid": 3403353,
        "exitCode": null,
        "exitSignal": null,
        "hasRunningSubprocess": true,
        "label": "fixture-child",
        "updatedAt": "2026-10-27T14:26:19.524Z"
      },
      {
        "threadId": "fixture-thread-b",
        "terminalId": "term-10",
        "cwd": "/tmp/lead-24640-r1/capture/2026-10-27T14-26-11.650Z-after-R18",
        "worktreePath": null,
        "status": "running",
        "pid": 3403371,
        "exitCode": null,
        "exitSignal": null,
        "hasRunningSubprocess": true,
        "label": "fixture-child",
        "updatedAt": "2026-10-27T14:26:19.508Z"
      }
    ]
  },
  {
    "kind": "state",
    "utc": "2026-10-27T14:26:20.239Z",
    "mono_ns": "921769362452077",
    "ms": 8586.740112,
    "terminals": []
  }
]
```

## Behaviour difference
Same idle-only/thread selection
Raw UTC/monotonic RPC, query, signal, metadata, warnings, and cleanup records are in the paired transcripts. PID, randomly generated script terminal suffix, database path, UTC, sequence batching, and scheduler jitter naturally vary between fresh runs. No visual comparison is applicable.