# R21: Concurrent background and settlement queries

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-28-15.083Z-before-R21`. After run: `/tmp/lead-24640-r1/capture/2026-10-27T14-28-15.078Z-after-R21`.

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.

Both query owners are identified from trace ancestry. Peak total concurrency may be 2: one background and one independent cleanup query. Background concurrency remains 1. After background succeeds near 5s while cleanup retains its 1s timeout and releases resize.

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 | R21-seed, background; shim START 2026-10-27T14:28:23.353Z (mono 921892475524276); END 2026-10-27T14:28:23.366Z (mono 921892488710453); 13.2ms, exit 0; acquisition 2026-10-27T14:28:23.311Z→2026-10-27T14:28:23.372Z, 61.5ms | R21-seed, background; shim START 2026-10-27T14:28:23.252Z (mono 921892374573370); END 2026-10-27T14:28:23.270Z (mono 921892392731333); 18.2ms, exit 0; acquisition 2026-10-27T14:28:23.201Z→2026-10-27T14:28:23.278Z, 77.3ms | Background 1s timeout → 5s success; cleanup ~1s |
| Fallback query 2 | R21-slow, background; shim START 2026-10-27T14:28:25.414Z (mono 921894536807445); END 2026-10-27T14:28:26.388Z (mono 921895510811837); 974.0ms, signal 15; acquisition 2026-10-27T14:28:25.377Z→2026-10-27T14:28:26.395Z, 1018.7ms | R21-slow, background; shim START 2026-10-27T14:28:25.324Z (mono 921894446662691); END 2026-10-27T14:28:30.340Z (mono 921899462206510); 5015.5ms, exit 0; acquisition 2026-10-27T14:28:25.282Z→2026-10-27T14:28:30.350Z, 5067.5ms | Measured duration/result above |
| Fallback query 3 | R21-slow, closeIdle; shim START 2026-10-27T14:28:25.504Z (mono 921894626680819); END 2026-10-27T14:28:26.467Z (mono 921895589501893); 962.8ms, signal 15; acquisition 2026-10-27T14:28:25.455Z→2026-10-27T14:28:26.474Z, 1018.8ms | R21-slow, closeIdle; shim START 2026-10-27T14:28:25.402Z (mono 921894524349965); END 2026-10-27T14:28:26.376Z (mono 921895498794544); 974.4ms, signal 15; acquisition 2026-10-27T14:28:25.363Z→2026-10-27T14:28:26.383Z, 1019.6ms | Measured duration/result above |
| Background completion→next-query gaps | R21-seed→R21-slow: shim gap 2.048s, acquisition gap 2.004s | R21-seed→R21-slow: shim gap 2.054s, acquisition gap 2.004s | Background 1s timeout → 5s success; cleanup ~1s |
| Terminal event 1 | 2026-10-27T14:28:23.378Z `fixture-thread-a/term-0` activity, seq=5, busy=True, label=`fixture-child` | 2026-10-27T14:28:23.283Z `fixture-thread-a/term-0` activity, seq=8, busy=True, label=`fixture-child` | Compare identity/sequence; subscriptions deduplicated |
| Terminal event 2 | 2026-10-27T14:28:23.379Z `fixture-thread-b/term-10` activity, seq=6, busy=True, label=`fixture-child` | 2026-10-27T14:28:23.284Z `fixture-thread-b/term-10` activity, seq=6, busy=True, label=`fixture-child` | Compare identity/sequence; subscriptions deduplicated |
| Terminal event 3 | 2026-10-27T14:28:27.120Z `fixture-thread-a/term-0` closed, seq=6 | 2026-10-27T14:28:30.459Z `fixture-thread-a/term-0` closed, seq=9 | Compare identity/sequence; subscriptions deduplicated |
| Terminal event 4 | 2026-10-27T14:28:27.128Z `fixture-thread-b/term-10` closed, seq=7 | 2026-10-27T14:28:30.466Z `fixture-thread-b/term-10` closed, seq=7 | Compare identity/sequence; subscriptions deduplicated |
| Peak all fallback queries | 2 | 2 | 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:28:15.242Z",
    "mono_ns": "921884364513704",
    "ms": 157.259125,
    "phase": "R21",
    "mode": "valid",
    "delay": 5000
  },
  {
    "kind": "server-ready",
    "utc": "2026-10-27T14:28:22.698Z",
    "mono_ns": "921891820228054",
    "ms": 7612.979868
  },
  {
    "kind": "rpc-connected",
    "utc": "2026-10-27T14:28:22.842Z",
    "mono_ns": "921891964607017",
    "ms": 7757.355434
  },
  {
    "kind": "control",
    "utc": "2026-10-27T14:28:22.963Z",
    "mono_ns": "921892085926603",
    "ms": 7878.670111,
    "phase": "R21-seed",
    "mode": "valid",
    "delay": 0
  },
  {
    "kind": "child-ready",
    "utc": "2026-10-27T14:28:23.080Z",
    "mono_ns": "921892202870470",
    "ms": 7995.613908,
    "threadId": "fixture-thread-a",
    "terminalId": "term-0",
    "childPid": 3414457,
    "marker": "READY_R21_0"
  },
  {
    "kind": "child-ready",
    "utc": "2026-10-27T14:28:23.172Z",
    "mono_ns": "921892294556024",
    "ms": 8087.301216,
    "threadId": "fixture-thread-b",
    "terminalId": "term-10",
    "childPid": 3414508,
    "marker": "READY_R21_10"
  },
  {
    "kind": "control",
    "utc": "2026-10-27T14:28:23.380Z",
    "mono_ns": "921892502230224",
    "ms": 8294.973492,
    "phase": "R21-slow",
    "mode": "valid",
    "delay": 5000
  },
  {
    "kind": "settle-start",
    "utc": "2026-10-27T14:28:25.428Z",
    "mono_ns": "921894550549233",
    "ms": 10343.294144
  },
  {
    "kind": "other-thread-resize-complete",
    "utc": "2026-10-27T14:28:25.534Z",
    "mono_ns": "921894656585418",
    "ms": 10449.328115
  },
  {
    "kind": "settlement-cleanup-completed",
    "utc": "2026-10-27T14:28:27.007Z",
    "mono_ns": "921896130033396",
    "ms": 11922.778327,
    "threadId": "fixture-thread-a",
    "span": {
      "type": "effect-span",
      "name": "ThreadSettlementServiceV2.cleanUpSettledThread",
      "traceId": "7fc265eab24158d0fefa2e6822499fab",
      "spanId": "38d4fa08ca726546",
      "parentSpanId": "e94f64215839acf4",
      "sampled": true,
      "kind": "internal",
      "startTimeUnixNano": "1793111305452391504",
      "endTimeUnixNano": "1793111306475419230",
      "durationMs": 1023.027726,
      "attributes": {},
      "events": [
        {
          "name": "[\n  \"failed to close idle terminals\",\n  {\n    \"threadId\": \"fixture-thread-a\",\n    \"error\": \"Failed to inspect terminal subprocesses with ps (timed out)\"\n  }\n]",
          "timeUnixNano": "1793111306475403440",
          "attributes": {
            "effect.fiberId": 6818,
            "effect.logLevel": "WARN"
          }
        }
      ],
      "links": [],
      "exit": {
        "_tag": "Success"
      }
    }
  },
  {
    "kind": "same-thread-resize-complete",
    "utc": "2026-10-27T14:28:27.007Z",
    "mono_ns": "921896130150245",
    "ms": 11922.893403
  },
  {
    "kind": "scenario-complete",
    "utc": "2026-10-27T14:28:27.109Z",
    "mono_ns": "921896231454964",
    "ms": 12024.198423
  },
  {
    "kind": "cleanup-start",
    "utc": "2026-10-27T14:28:27.260Z",
    "mono_ns": "921896382860359",
    "ms": 12175.60536,
    "pids": [
      3413298,
      3413300,
      3414445,
      3414457,
      3414503,
      3414508
    ],
    "ptyPids": [
      3414445,
      3414457,
      3414503,
      3414508
    ]
  },
  {
    "kind": "cleanup-end",
    "utc": "2026-10-27T14:28:29.452Z",
    "mono_ns": "921898574649336",
    "ms": 14367.400308,
    "survivorsBeforeFinalKill": [],
    "remaining": []
  }
]
```
Metadata snapshots taken before and after driver cleanup:
```json
[
  {
    "kind": "state",
    "utc": "2026-10-27T14:28:27.109Z",
    "mono_ns": "921896231325061",
    "ms": 12024.0692,
    "terminals": [
      {
        "threadId": "fixture-thread-a",
        "terminalId": "term-0",
        "cwd": "/tmp/lead-24640-r1/capture/2026-10-27T14-28-15.083Z-before-R21",
        "worktreePath": null,
        "status": "running",
        "pid": 3414445,
        "exitCode": null,
        "exitSignal": null,
        "hasRunningSubprocess": true,
        "label": "fixture-child",
        "updatedAt": "2026-10-27T14:28:26.476Z"
      },
      {
        "threadId": "fixture-thread-b",
        "terminalId": "term-10",
        "cwd": "/tmp/lead-24640-r1/capture/2026-10-27T14-28-15.083Z-before-R21",
        "worktreePath": null,
        "status": "running",
        "pid": 3414503,
        "exitCode": null,
        "exitSignal": null,
        "hasRunningSubprocess": true,
        "label": "fixture-child",
        "updatedAt": "2026-10-27T14:28:25.532Z"
      }
    ]
  },
  {
    "kind": "state",
    "utc": "2026-10-27T14:28:27.229Z",
    "mono_ns": "921896351328517",
    "ms": 12144.073288,
    "terminals": []
  }
]
```

## After scripted milestones and final observation states
```json
[
  {
    "kind": "control",
    "utc": "2026-10-27T14:28:15.245Z",
    "mono_ns": "921884367438611",
    "ms": 164.465854,
    "phase": "R21",
    "mode": "valid",
    "delay": 5000
  },
  {
    "kind": "server-ready",
    "utc": "2026-10-27T14:28:22.091Z",
    "mono_ns": "921891213989400",
    "ms": 7010.916084
  },
  {
    "kind": "rpc-connected",
    "utc": "2026-10-27T14:28:22.365Z",
    "mono_ns": "921891487590092",
    "ms": 7284.51831
  },
  {
    "kind": "control",
    "utc": "2026-10-27T14:28:22.525Z",
    "mono_ns": "921891647991502",
    "ms": 7444.913939,
    "phase": "R21-seed",
    "mode": "valid",
    "delay": 0
  },
  {
    "kind": "child-ready",
    "utc": "2026-10-27T14:28:22.722Z",
    "mono_ns": "921891844241642",
    "ms": 7641.164189,
    "threadId": "fixture-thread-a",
    "terminalId": "term-0",
    "childPid": 3414402,
    "marker": "READY_R21_0"
  },
  {
    "kind": "child-ready",
    "utc": "2026-10-27T14:28:22.883Z",
    "mono_ns": "921892006098833",
    "ms": 7803.02177,
    "threadId": "fixture-thread-b",
    "terminalId": "term-10",
    "childPid": 3414412,
    "marker": "READY_R21_10"
  },
  {
    "kind": "control",
    "utc": "2026-10-27T14:28:23.300Z",
    "mono_ns": "921892422825899",
    "ms": 8219.749427,
    "phase": "R21-slow",
    "mode": "valid",
    "delay": 5000
  },
  {
    "kind": "settle-start",
    "utc": "2026-10-27T14:28:25.334Z",
    "mono_ns": "921894456406589",
    "ms": 10253.330218
  },
  {
    "kind": "other-thread-resize-complete",
    "utc": "2026-10-27T14:28:25.416Z",
    "mono_ns": "921894539112619",
    "ms": 10336.034465
  },
  {
    "kind": "settlement-cleanup-completed",
    "utc": "2026-10-27T14:28:26.468Z",
    "mono_ns": "921895590202488",
    "ms": 11387.126457,
    "threadId": "fixture-thread-a",
    "span": {
      "type": "effect-span",
      "name": "ThreadSettlementServiceV2.cleanUpSettledThread",
      "traceId": "317d635952412a501e1229fb55639f63",
      "spanId": "89610e765fdad415",
      "parentSpanId": "779e58fbe483114e",
      "sampled": true,
      "kind": "internal",
      "startTimeUnixNano": "1793111305360588139",
      "endTimeUnixNano": "1793111306384663310",
      "durationMs": 1024.075171,
      "attributes": {},
      "events": [
        {
          "name": "[\n  \"failed to close idle terminals\",\n  {\n    \"threadId\": \"fixture-thread-a\",\n    \"error\": \"Failed to inspect terminal subprocesses with ps (timed out)\"\n  }\n]",
          "timeUnixNano": "1793111306384634426",
          "attributes": {
            "effect.fiberId": 6806,
            "effect.logLevel": "WARN"
          }
        }
      ],
      "links": [],
      "exit": {
        "_tag": "Success"
      }
    }
  },
  {
    "kind": "same-thread-resize-complete",
    "utc": "2026-10-27T14:28:26.468Z",
    "mono_ns": "921895590295653",
    "ms": 11387.217578
  },
  {
    "kind": "scenario-complete",
    "utc": "2026-10-27T14:28:30.448Z",
    "mono_ns": "921899570607923",
    "ms": 15367.530059
  },
  {
    "kind": "cleanup-start",
    "utc": "2026-10-27T14:28:30.584Z",
    "mono_ns": "921899706684662",
    "ms": 15503.615644,
    "pids": [
      3413299,
      3413302,
      3414400,
      3414402,
      3414410,
      3414412
    ],
    "ptyPids": [
      3414400,
      3414402,
      3414410,
      3414412
    ]
  },
  {
    "kind": "cleanup-end",
    "utc": "2026-10-27T14:28:32.726Z",
    "mono_ns": "921901848756493",
    "ms": 17645.679691,
    "survivorsBeforeFinalKill": [],
    "remaining": []
  }
]
```
Metadata snapshots taken before and after driver cleanup:
```json
[
  {
    "kind": "state",
    "utc": "2026-10-27T14:28:30.448Z",
    "mono_ns": "921899570519877",
    "ms": 15367.443196,
    "terminals": [
      {
        "threadId": "fixture-thread-a",
        "terminalId": "term-0",
        "cwd": "/tmp/lead-24640-r1/capture/2026-10-27T14-28-15.078Z-after-R21",
        "worktreePath": null,
        "status": "running",
        "pid": 3414400,
        "exitCode": null,
        "exitSignal": null,
        "hasRunningSubprocess": true,
        "label": "fixture-child",
        "updatedAt": "2026-10-27T14:28:26.385Z"
      },
      {
        "threadId": "fixture-thread-b",
        "terminalId": "term-10",
        "cwd": "/tmp/lead-24640-r1/capture/2026-10-27T14-28-15.078Z-after-R21",
        "worktreePath": null,
        "status": "running",
        "pid": 3414410,
        "exitCode": null,
        "exitSignal": null,
        "hasRunningSubprocess": true,
        "label": "fixture-child",
        "updatedAt": "2026-10-27T14:28:25.415Z"
      }
    ]
  },
  {
    "kind": "state",
    "utc": "2026-10-27T14:28:30.566Z",
    "mono_ns": "921899688659929",
    "ms": 15485.583017,
    "terminals": []
  }
]
```

## Behaviour difference
Background 1s timeout → 5s success; cleanup ~1s
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.