# R22: Input and independent PTY output during settlement query

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-42-57.524Z-before-R22`. After run: `/tmp/lead-24640-r1/capture/2026-10-27T14-42-58.735Z-after-R22`.

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.

Two real settlement phases separately change the activity mark through terminal.write and through an independent write to the owned PTY. The active terminal survives each stale idle result while unchanged idle siblings close; final writes still work.

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 | R22, background; shim START 2026-10-27T14:43:06.624Z (mono 922775746489919); END 2026-10-27T14:43:07.141Z (mono 922776263181810); 516.7ms, exit 0; acquisition 2026-10-27T14:43:06.576Z→2026-10-27T14:43:07.148Z, 571.6ms | R22, background; shim START 2026-10-27T14:43:07.561Z (mono 922776683482770); END 2026-10-27T14:43:08.075Z (mono 922777197462394); 514.0ms, exit 0; acquisition 2026-10-27T14:43:07.524Z→2026-10-27T14:43:08.083Z, 558.3ms | Same concurrent-activity retention |
| Fallback query 2 | R22-settle, closeIdle; shim START 2026-10-27T14:43:07.433Z (mono 922776555187920); END 2026-10-27T14:43:07.947Z (mono 922777069187952); 514.0ms, exit 0; acquisition 2026-10-27T14:43:07.394Z→2026-10-27T14:43:07.957Z, 562.6ms | R22-settle, closeIdle; shim START 2026-10-27T14:43:08.366Z (mono 922777488090379); END 2026-10-27T14:43:08.880Z (mono 922778002409890); 514.3ms, exit 0; acquisition 2026-10-27T14:43:08.324Z→2026-10-27T14:43:08.888Z, 563.8ms | Measured duration/result above |
| Fallback query 3 | R22-settle, background; shim START 2026-10-27T14:43:09.191Z (mono 922778313747284); END 2026-10-27T14:43:09.705Z (mono 922778827352865); 513.6ms, exit 0; acquisition 2026-10-27T14:43:09.150Z→2026-10-27T14:43:09.713Z, 562.9ms | R22-settle, background; shim START 2026-10-27T14:43:10.361Z (mono 922779483057203); END 2026-10-27T14:43:10.876Z (mono 922779998266454); 515.2ms, exit 0; acquisition 2026-10-27T14:43:10.320Z→2026-10-27T14:43:10.884Z, 563.9ms | Measured duration/result above |
| Fallback query 4 | R22-output-during-query, closeIdle; shim START 2026-10-27T14:43:09.783Z (mono 922778905073413); END 2026-10-27T14:43:10.298Z (mono 922779420568119); 515.5ms, exit 0; acquisition 2026-10-27T14:43:09.740Z→2026-10-27T14:43:10.308Z, 567.8ms | R22-output-during-query, closeIdle; shim START 2026-10-27T14:43:10.946Z (mono 922780067954182); END 2026-10-27T14:43:11.460Z (mono 922780582255359); 514.3ms, exit 0; acquisition 2026-10-27T14:43:10.904Z→2026-10-27T14:43:11.468Z, 564.1ms | Measured duration/result above |
| Background completion→next-query gaps | R22→R22-settle: shim gap 2.051s, acquisition gap 2.003s | R22→R22-settle: shim gap 2.286s, acquisition gap 2.237s | Same concurrent-activity retention |
| Terminal event 1 | 2026-10-27T14:43:07.155Z `fixture-thread-b/term-10` activity, seq=6, busy=True, label=`fixture-child` | 2026-10-27T14:43:08.087Z `fixture-thread-b/term-10` activity, seq=5, busy=True, label=`fixture-child` | Compare identity/sequence; subscriptions deduplicated |
| Terminal event 2 | 2026-10-27T14:43:07.964Z `fixture-thread-a/term-1` closed, seq=3 | 2026-10-27T14:43:08.893Z `fixture-thread-a/term-1` closed, seq=3 | Compare identity/sequence; subscriptions deduplicated |
| Terminal event 3 | 2026-10-27T14:43:10.312Z `fixture-thread-a/term-2` closed, seq=3 | 2026-10-27T14:43:11.471Z `fixture-thread-a/term-2` closed, seq=3 | Compare identity/sequence; subscriptions deduplicated |
| Terminal event 4 | 2026-10-27T14:43:11.171Z `fixture-thread-a/term-0` closed, seq=7 | 2026-10-27T14:43:12.570Z `fixture-thread-a/term-0` closed, seq=8 | Compare identity/sequence; subscriptions deduplicated |
| Terminal event 5 | 2026-10-27T14:43:11.176Z `fixture-thread-b/term-10` closed, seq=7 | 2026-10-27T14:43:12.576Z `fixture-thread-b/term-10` closed, seq=6 | 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:42:57.717Z",
    "mono_ns": "922766839115142",
    "ms": 188.964913,
    "phase": "R22",
    "mode": "valid",
    "delay": 500
  },
  {
    "kind": "server-ready",
    "utc": "2026-10-27T14:43:05.625Z",
    "mono_ns": "922774747731329",
    "ms": 8097.582732
  },
  {
    "kind": "rpc-connected",
    "utc": "2026-10-27T14:43:05.787Z",
    "mono_ns": "922774909491989",
    "ms": 8259.344725
  },
  {
    "kind": "child-ready",
    "utc": "2026-10-27T14:43:05.997Z",
    "mono_ns": "922775119337102",
    "ms": 8469.184798,
    "threadId": "fixture-thread-b",
    "terminalId": "term-10",
    "childPid": 3425651,
    "marker": "READY_R22_10"
  },
  {
    "kind": "first-background-completed",
    "utc": "2026-10-27T14:43:07.160Z",
    "mono_ns": "922776282519967",
    "ms": 9632.367493
  },
  {
    "kind": "control",
    "utc": "2026-10-27T14:43:07.361Z",
    "mono_ns": "922776483235924",
    "ms": 9833.085273,
    "phase": "R22-settle",
    "mode": "valid",
    "delay": 500
  },
  {
    "kind": "settlement-cleanup-completed",
    "utc": "2026-10-27T14:43:08.905Z",
    "mono_ns": "922778027410990",
    "ms": 11377.259808,
    "threadId": "fixture-thread-a",
    "span": {
      "type": "effect-span",
      "name": "ThreadSettlementServiceV2.cleanUpSettledThread",
      "traceId": "8c81bdd66a2d4a5c0ecc0aa64a33313b",
      "spanId": "bff297a8bff27cd7",
      "parentSpanId": "61964feb5ad99d7c",
      "sampled": true,
      "kind": "internal",
      "startTimeUnixNano": "1793112187391151650",
      "endTimeUnixNano": "1793112187963222328",
      "durationMs": 572.070678,
      "attributes": {},
      "events": [],
      "links": [],
      "exit": {
        "_tag": "Success"
      }
    }
  },
  {
    "kind": "second-background-completed",
    "utc": "2026-10-27T14:43:09.716Z",
    "mono_ns": "922778838050303",
    "ms": 12187.89858
  },
  {
    "kind": "control",
    "utc": "2026-10-27T14:43:09.716Z",
    "mono_ns": "922778838270727",
    "ms": 12188.118623,
    "phase": "R22-output-during-query",
    "mode": "valid",
    "delay": 500
  },
  {
    "kind": "external-pty-output",
    "utc": "2026-10-27T14:43:09.787Z",
    "mono_ns": "922778909849212",
    "ms": 12259.697479,
    "tty": "/dev/pts/7"
  },
  {
    "kind": "settlement-cleanup-completed",
    "utc": "2026-10-27T14:43:10.914Z",
    "mono_ns": "922780035908307",
    "ms": 13385.758017,
    "threadId": "fixture-thread-a",
    "span": {
      "type": "effect-span",
      "name": "ThreadSettlementServiceV2.cleanUpSettledThread",
      "traceId": "8c81bdd66a2d4a5c0ecc0aa64a33313b",
      "spanId": "284193a1fdcc848e",
      "parentSpanId": "61964feb5ad99d7c",
      "sampled": true,
      "kind": "internal",
      "startTimeUnixNano": "1793112189732615815",
      "endTimeUnixNano": "1793112190311142364",
      "durationMs": 578.526549,
      "attributes": {},
      "events": [],
      "links": [],
      "exit": {
        "_tag": "Success"
      }
    }
  },
  {
    "kind": "scenario-complete",
    "utc": "2026-10-27T14:43:11.019Z",
    "mono_ns": "922780141053791",
    "ms": 13490.900996
  },
  {
    "kind": "cleanup-start",
    "utc": "2026-10-27T14:43:11.298Z",
    "mono_ns": "922780420574118",
    "ms": 13770.426924,
    "pids": [
      3423944,
      3423945,
      3425644,
      3425649,
      3425651,
      3425912
    ],
    "ptyPids": [
      3425644,
      3425646,
      3425649,
      3425651,
      3425912
    ]
  },
  {
    "kind": "cleanup-end",
    "utc": "2026-10-27T14:43:13.442Z",
    "mono_ns": "922782564602490",
    "ms": 15914.455567,
    "survivorsBeforeFinalKill": [],
    "remaining": []
  }
]
```
Metadata snapshots taken before and after driver cleanup:
```json
[
  {
    "kind": "state",
    "utc": "2026-10-27T14:43:09.510Z",
    "mono_ns": "922778632549200",
    "ms": 11982.397267,
    "terminals": [
      {
        "threadId": "fixture-thread-a",
        "terminalId": "term-0",
        "cwd": "/tmp/lead-24640-r1/capture/2026-10-27T14-42-57.524Z-before-R22",
        "worktreePath": null,
        "status": "running",
        "pid": 3425644,
        "exitCode": null,
        "exitSignal": null,
        "hasRunningSubprocess": false,
        "label": "Terminal 0",
        "updatedAt": "2026-10-27T14:43:08.907Z"
      },
      {
        "threadId": "fixture-thread-b",
        "terminalId": "term-10",
        "cwd": "/tmp/lead-24640-r1/capture/2026-10-27T14-42-57.524Z-before-R22",
        "worktreePath": null,
        "status": "running",
        "pid": 3425649,
        "exitCode": null,
        "exitSignal": null,
        "hasRunningSubprocess": true,
        "label": "fixture-child",
        "updatedAt": "2026-10-27T14:43:07.149Z"
      }
    ]
  },
  {
    "kind": "state",
    "utc": "2026-10-27T14:43:11.018Z",
    "mono_ns": "922780140887559",
    "ms": 13490.738061,
    "terminals": [
      {
        "threadId": "fixture-thread-a",
        "terminalId": "term-0",
        "cwd": "/tmp/lead-24640-r1/capture/2026-10-27T14-42-57.524Z-before-R22",
        "worktreePath": null,
        "status": "running",
        "pid": 3425644,
        "exitCode": null,
        "exitSignal": null,
        "hasRunningSubprocess": false,
        "label": "Terminal 0",
        "updatedAt": "2026-10-27T14:43:10.916Z"
      },
      {
        "threadId": "fixture-thread-b",
        "terminalId": "term-10",
        "cwd": "/tmp/lead-24640-r1/capture/2026-10-27T14-42-57.524Z-before-R22",
        "worktreePath": null,
        "status": "running",
        "pid": 3425649,
        "exitCode": null,
        "exitSignal": null,
        "hasRunningSubprocess": true,
        "label": "fixture-child",
        "updatedAt": "2026-10-27T14:43:07.149Z"
      }
    ]
  },
  {
    "kind": "state",
    "utc": "2026-10-27T14:43:11.278Z",
    "mono_ns": "922780400815192",
    "ms": 13750.664512,
    "terminals": []
  }
]
```

## After scripted milestones and final observation states
```json
[
  {
    "kind": "control",
    "utc": "2026-10-27T14:42:58.928Z",
    "mono_ns": "922768050401617",
    "ms": 189.490849,
    "phase": "R22",
    "mode": "valid",
    "delay": 500
  },
  {
    "kind": "server-ready",
    "utc": "2026-10-27T14:43:07.120Z",
    "mono_ns": "922776242711680",
    "ms": 8381.801754
  },
  {
    "kind": "rpc-connected",
    "utc": "2026-10-27T14:43:07.202Z",
    "mono_ns": "922776324871325",
    "ms": 8463.964104
  },
  {
    "kind": "child-ready",
    "utc": "2026-10-27T14:43:07.399Z",
    "mono_ns": "922776521600903",
    "ms": 8660.688674,
    "threadId": "fixture-thread-b",
    "terminalId": "term-10",
    "childPid": 3425886,
    "marker": "READY_R22_10"
  },
  {
    "kind": "first-background-completed",
    "utc": "2026-10-27T14:43:08.089Z",
    "mono_ns": "922777211568927",
    "ms": 9350.655876
  },
  {
    "kind": "control",
    "utc": "2026-10-27T14:43:08.290Z",
    "mono_ns": "922777412210554",
    "ms": 9551.299055,
    "phase": "R22-settle",
    "mode": "valid",
    "delay": 500
  },
  {
    "kind": "settlement-cleanup-completed",
    "utc": "2026-10-27T14:43:09.310Z",
    "mono_ns": "922778432725176",
    "ms": 10571.814279,
    "threadId": "fixture-thread-a",
    "span": {
      "type": "effect-span",
      "name": "ThreadSettlementServiceV2.cleanUpSettledThread",
      "traceId": "c034cb5dc065fa8a7285651dc0a1441c",
      "spanId": "e8ae74f1bce38e2d",
      "parentSpanId": "6b7908b3087a59c1",
      "sampled": true,
      "kind": "internal",
      "startTimeUnixNano": "1793112188321018160",
      "endTimeUnixNano": "1793112188892242550",
      "durationMs": 571.22439,
      "attributes": {},
      "events": [],
      "links": [],
      "exit": {
        "_tag": "Success"
      }
    }
  },
  {
    "kind": "second-background-completed",
    "utc": "2026-10-27T14:43:10.879Z",
    "mono_ns": "922780001870685",
    "ms": 12140.958625
  },
  {
    "kind": "control",
    "utc": "2026-10-27T14:43:10.880Z",
    "mono_ns": "922780002110575",
    "ms": 12141.197212,
    "phase": "R22-output-during-query",
    "mode": "valid",
    "delay": 500
  },
  {
    "kind": "external-pty-output",
    "utc": "2026-10-27T14:43:10.954Z",
    "mono_ns": "922780076743410",
    "ms": 12215.83119,
    "tty": "/dev/pts/10"
  },
  {
    "kind": "settlement-cleanup-completed",
    "utc": "2026-10-27T14:43:12.313Z",
    "mono_ns": "922781435859060",
    "ms": 13574.946449,
    "threadId": "fixture-thread-a",
    "span": {
      "type": "effect-span",
      "name": "ThreadSettlementServiceV2.cleanUpSettledThread",
      "traceId": "c034cb5dc065fa8a7285651dc0a1441c",
      "spanId": "7ff6d84a01ec6744",
      "parentSpanId": "6b7908b3087a59c1",
      "sampled": true,
      "kind": "internal",
      "startTimeUnixNano": "1793112190902352251",
      "endTimeUnixNano": "1793112191470531048",
      "durationMs": 568.178797,
      "attributes": {},
      "events": [],
      "links": [],
      "exit": {
        "_tag": "Success"
      }
    }
  },
  {
    "kind": "scenario-complete",
    "utc": "2026-10-27T14:43:12.418Z",
    "mono_ns": "922781539905933",
    "ms": 13678.992901
  },
  {
    "kind": "cleanup-start",
    "utc": "2026-10-27T14:43:12.700Z",
    "mono_ns": "922781821952730",
    "ms": 13961.0406,
    "pids": [
      3423993,
      3423994,
      3425880,
      3425884,
      3425886
    ],
    "ptyPids": [
      3425880,
      3425882,
      3425884,
      3425886,
      3425916
    ]
  },
  {
    "kind": "cleanup-end",
    "utc": "2026-10-27T14:43:14.823Z",
    "mono_ns": "922783945146237",
    "ms": 16084.240539,
    "survivorsBeforeFinalKill": [],
    "remaining": []
  }
]
```
Metadata snapshots taken before and after driver cleanup:
```json
[
  {
    "kind": "state",
    "utc": "2026-10-27T14:43:09.916Z",
    "mono_ns": "922779038194297",
    "ms": 11177.282558,
    "terminals": [
      {
        "threadId": "fixture-thread-a",
        "terminalId": "term-0",
        "cwd": "/tmp/lead-24640-r1/capture/2026-10-27T14-42-58.735Z-after-R22",
        "worktreePath": null,
        "status": "running",
        "pid": 3425880,
        "exitCode": null,
        "exitSignal": null,
        "hasRunningSubprocess": false,
        "label": "Terminal 0",
        "updatedAt": "2026-10-27T14:43:09.313Z"
      },
      {
        "threadId": "fixture-thread-b",
        "terminalId": "term-10",
        "cwd": "/tmp/lead-24640-r1/capture/2026-10-27T14-42-58.735Z-after-R22",
        "worktreePath": null,
        "status": "running",
        "pid": 3425884,
        "exitCode": null,
        "exitSignal": null,
        "hasRunningSubprocess": true,
        "label": "fixture-child",
        "updatedAt": "2026-10-27T14:43:08.085Z"
      }
    ]
  },
  {
    "kind": "state",
    "utc": "2026-10-27T14:43:12.417Z",
    "mono_ns": "922781539799553",
    "ms": 13678.888246,
    "terminals": [
      {
        "threadId": "fixture-thread-a",
        "terminalId": "term-0",
        "cwd": "/tmp/lead-24640-r1/capture/2026-10-27T14-42-58.735Z-after-R22",
        "worktreePath": null,
        "status": "running",
        "pid": 3425880,
        "exitCode": null,
        "exitSignal": null,
        "hasRunningSubprocess": false,
        "label": "Terminal 0",
        "updatedAt": "2026-10-27T14:43:12.315Z"
      },
      {
        "threadId": "fixture-thread-b",
        "terminalId": "term-10",
        "cwd": "/tmp/lead-24640-r1/capture/2026-10-27T14-42-58.735Z-after-R22",
        "worktreePath": null,
        "status": "running",
        "pid": 3425884,
        "exitCode": null,
        "exitSignal": null,
        "hasRunningSubprocess": true,
        "label": "fixture-child",
        "updatedAt": "2026-10-27T14:43:08.085Z"
      }
    ]
  },
  {
    "kind": "state",
    "utc": "2026-10-27T14:43:12.683Z",
    "mono_ns": "922781805701825",
    "ms": 13944.789885,
    "terminals": []
  }
]
```

## Behaviour difference
Same concurrent-activity retention
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.