hird 0.3.1

Cross-harness agent work queue and shared assertion memory, backed by one local SQLite database
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>hird — how the central queue works</title>
<meta name="description" content="An interactive walkthrough of hird-server: a bearer token becomes a person, one endpoint per identity, and one SQLite compare-and-set decides who claims a task.">
<link rel="stylesheet" href="style.css">
<style>
  /* ------------------------------------------------ the demo, page-local */
  .demo {
    background: var(--bg-raised);
    border: 1px solid var(--rule);
    border-radius: 10px;
    margin: 1.5rem 0;
    overflow: hidden;
  }
  .demo > .controls {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem 0.75rem;
    padding: 0.75rem 1rem;
    background: var(--bg-sunken);
    border-bottom: 1px solid var(--rule);
    font-size: 0.875rem;
  }
  .demo .controls label { color: var(--ink-faint); display: inline-flex; align-items: center; gap: 0.35rem; }
  .demo .controls label[hidden] { display: none; }
  .demo select { max-width: 15rem; }
  .demo select, .demo button {
    font: inherit;
    font-size: 0.875rem;
    color: var(--ink);
    background: var(--bg-raised);
    border: 1px solid var(--rule);
    border-radius: 6px;
    padding: 0.25rem 0.5rem;
  }
  .demo button { cursor: pointer; }
  .demo button:hover:not(:disabled) { border-color: var(--accent); }
  .demo button:disabled { opacity: 0.5; cursor: default; }
  .demo button.go { background: var(--accent); border-color: var(--accent); color: var(--bg-raised); font-weight: 500; padding: 0.25rem 0.9rem; }
  .demo .spacer { flex: 1 1 auto; }
  .demo .body { padding: 1rem; }

  /* the pipeline: five stages a request walks, and the packet walking them */
  .pipe { position: relative; display: grid; gap: 0.6rem; grid-template-columns: repeat(5, minmax(0, 1fr)); }
  @media (max-width: 58rem) { .pipe { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
  @media (max-width: 26rem) { .pipe { grid-template-columns: minmax(0, 1fr); } }
  .stage {
    border: 1px solid var(--rule);
    border-radius: 8px;
    padding: 0.6rem 0.7rem;
    background: var(--bg);
    min-height: 7.5rem;
    transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
  }
  .stage h5 {
    margin: 0 0 0.35rem;
    font: 600 0.6875rem/1.3 var(--sans);
    text-transform: uppercase;
    letter-spacing: 0.07em;
    color: var(--ink-faint);
  }
  .stage .code { font-family: var(--mono); font-size: 0.6875rem; line-height: 1.45; color: var(--ink-soft); overflow-wrap: anywhere; }
  .stage .code b { color: var(--ink); font-weight: 600; }
  .stage .code .no { color: var(--red); }
  .stage .code .yes { color: var(--green); }
  .stage.live { border-color: var(--accent); background: var(--accent-soft); box-shadow: 0 0 0 1px var(--accent) inset; }
  .stage.pass { border-color: var(--green); }
  .stage.stop { border-color: var(--red); background: var(--bg); background: color-mix(in srgb, var(--red) 8%, var(--bg)); }
  .packet {
    position: absolute;
    top: -0.55rem;
    left: 0;
    font: 600 0.6875rem/1 var(--mono);
    padding: 0.25rem 0.5rem;
    border-radius: 999px;
    background: var(--accent);
    color: var(--bg-raised);
    box-shadow: 0 1px 6px rgba(0,0,0,0.25);
    opacity: 0;
    transform: translate(0, 0);
    transition: transform 0.42s cubic-bezier(.4,.1,.3,1), opacity 0.2s, background 0.2s;
    pointer-events: none;
    white-space: nowrap;
    z-index: 2;
  }
  .packet.flying { opacity: 1; }
  .packet.refused { background: var(--red); }

  /* transcript, board, trail */
  .out { margin-top: 1rem; display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(18rem, 1fr)); }
  .out h5, .board-wrap h5 {
    margin: 0 0 0.4rem;
    font: 600 0.6875rem/1.3 var(--sans);
    text-transform: uppercase;
    letter-spacing: 0.07em;
    color: var(--ink-faint);
  }
  .out pre { margin: 0; max-height: 17rem; overflow: auto; }
  .status-line { font-family: var(--mono); font-size: 0.8125rem; margin: 0 0 0.4rem; }
  .status-line.ok { color: var(--green); }
  .status-line.bad { color: var(--red); }

  .grid-2 { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(19rem, 1fr)); }
  table.board { font-size: 0.8125rem; width: 100%; }
  table.board td, table.board th { padding: 0.3rem 0.5rem; }
  table.board td.seq { font-family: var(--mono); color: var(--ink-faint); }
  table.board td.who { font-family: var(--mono); font-size: 0.75rem; color: var(--ink-soft); }
  table.board tr.moved td { background: var(--accent-soft); transition: background 1.2s; }
  .pill {
    display: inline-block;
    font: 500 0.6875rem/1.5 var(--mono);
    border-radius: 999px;
    padding: 0 0.5rem;
    border: 1px solid var(--rule);
    white-space: nowrap;
  }
  .pill.open { background: var(--bg-sunken); color: var(--ink-soft); }
  .pill.claimed { background: var(--bg-sunken); background: color-mix(in srgb, var(--yellow) 22%, var(--bg-raised)); color: var(--yellow); border-color: var(--yellow); }
  .pill.in_progress { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
  .pill.done { background: var(--bg-sunken); background: color-mix(in srgb, var(--green) 18%, var(--bg-raised)); color: var(--green); border-color: var(--green); }
  .pill.failed { background: var(--bg-sunken); background: color-mix(in srgb, var(--red) 18%, var(--bg-raised)); color: var(--red); border-color: var(--red); }
  .pill.cancelled { background: var(--bg-sunken); color: var(--ink-faint); }
  .lease { font-family: var(--mono); font-size: 0.75rem; color: var(--ink-soft); }
  .lease.lapsed { color: var(--red); }

  ul.trail { list-style: none; margin: 0; padding: 0; font-family: var(--mono); font-size: 0.75rem; max-height: 17rem; overflow: auto; }
  ul.trail li { display: grid; grid-template-columns: 4.5rem 3rem 7rem 1fr; gap: 0.5rem; margin: 0 0 0.15rem; white-space: nowrap; }
  ul.trail .t { color: var(--ink-faint); }
  ul.trail .k { color: var(--ink); }
  ul.trail .k.lease_expired, ul.trail .k.failed { color: var(--red); }
  ul.trail .k.completed { color: var(--green); }
  ul.trail .a { color: var(--accent); overflow: hidden; text-overflow: ellipsis; }

  /* the race */
  .lanes { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(17rem, 1fr)); }
  .lane { border: 1px solid var(--rule); border-radius: 8px; padding: 0.75rem 0.85rem; background: var(--bg); }
  .lane h5 { margin: 0 0 0.5rem; font: 600 0.75rem/1.3 var(--mono); color: var(--ink-soft); }
  .lane pre { margin: 0 0 0.5rem; font-size: 0.7rem; }
  .lane .rows { font-family: var(--mono); font-size: 0.8125rem; }
  .lane.won { border-color: var(--green); }
  .lane.lost { border-color: var(--red); }
  .lane .verdict { font-size: 0.8125rem; margin: 0.35rem 0 0; }
  .lane.won .verdict { color: var(--green); }
  .lane.lost .verdict { color: var(--red); }

  /* the roster table used as a live map */
  table.roster { font-size: 0.8125rem; width: 100%; }
  table.roster td { font-family: var(--mono); font-size: 0.75rem; }
  table.roster tr.hit td { background: var(--accent-soft); }
  .clock { font-family: var(--mono); font-size: 0.75rem; color: var(--ink-faint); }
  .toggles { display: flex; flex-wrap: wrap; gap: 0.75rem 1.25rem; font-size: 0.8125rem; color: var(--ink-soft); margin: 0.25rem 0 0; }
  .toggles label { display: inline-flex; align-items: center; gap: 0.4rem; }
  @media (prefers-reduced-motion: reduce) {
    .packet { transition: opacity 0.2s; }
  }
</style>
</head>
<body>

<button id="theme" type="button" aria-label="Toggle colour theme">theme</button>

<div class="shell">
<nav class="toc" aria-label="Contents">
  <a class="brand" href="#top">hird</a>
  <p class="tagline">How the central queue turns a token into a person, and a race into one winner.</p>
  <details class="nav-body" open>
  <summary>Contents</summary>
  <ol>
    <li><a href="#shape">The shape of it</a></li>
    <li><a href="#request">Watch a request land</a></li>
    <li><a href="#roster-map">The roster is the whole map</a></li>
    <li><a href="#identity">A client may not name the person</a></li>
    <li><a href="#race">The race, and who wins it</a></li>
    <li><a href="#lease">The lease, and what happens when a session goes quiet</a></li>
    <li><a href="#refuses">What it refuses to do</a></li>
    <li><a href="#run">Running one yourself</a></li>
  </ol>
  <div class="away">
    <a href="index.html">The usage guide &rarr;</a>
    <a href="swarm.html">How a swarm collaborates &rarr;</a>
    <a href="factory.html">The software factory &rarr;</a>
    <a href="https://github.com/aoprisan/hird">Source on GitHub &rarr;</a>
    <a href="https://github.com/aoprisan/hird/blob/main/DESIGN.md">DESIGN.md §31 &rarr;</a>
  </div>
  </details>
</nav>

<main id="top">

<header class="hero">
  <div class="badges">
    <span class="badge">MCP over streamable HTTP</span>
    <span class="badge">one endpoint per identity</span>
    <span class="badge">a roster file, not accounts</span>
    <span class="badge">still one SQLite file</span>
  </div>
  <h1>How the central queue works</h1>
  <p class="lede">
    <code>hird</code> is a local queue: one process per harness session, stdio,
    no network. <strong>hird-server</strong> is the other thing — a process on a
    host that two people reach from two machines, speaking MCP over HTTP, with
    the queue's SQLite file underneath it. This page is that server, simulated
    in your browser: every panel below runs against one shared board, the way
    every connection runs against one shared file.
  </p>
  <div class="cta">
    <a class="primary" href="#request">Send a request</a>
    <a href="#race">Watch two agents race</a>
    <a href="index.html">Back to the guide</a>
  </div>
</header>

<section id="shape">
<h2>The shape of it</h2>

<pre class="diagram" aria-label="Topology of the central queue"><code>  Ana's laptop                     Ben's cloud container
┌──────────────┐                  ┌──────────────┐
│ Claude Code  │                  │  Codex CLI   │
└──────┬───────┘                  └──────┬───────┘
       │ MCP / HTTP                      │ MCP / HTTP
       │ Authorization: Bearer 3f9c…     │ Authorization: Bearer a70b…
       └───────────────┬─────────────────┘
                       │
        ┌──────────────▼───────────────────────────────┐
        │ hird-server                                  │
        │                                              │
        │  bearer()  →  roster lookup  →  endpoint     │
        │                                 (one per     │
        │                                  identity)   │
        │                                      │       │
        │                       Shared { Mutex&lt;Db&gt; }   │
        └──────────────────────┬───────────────────────┘
                               │
                        ┌──────▼──────┐
                        │   SQLite    │  one file, one
                        │    (WAL)    │  compare-and-set
                        └─────────────┘</code></pre>

<p>
  Four facts do all the work on this page, and the panels below are each one of
  them made visible:
</p>

<ol class="numbered">
  <li>
    <strong>Nothing a connection says about who it is gets believed.</strong> A
    request carries a bearer token, the roster maps that token to a person, and
    the session is built from the roster's entry.
  </li>
  <li>
    <strong>Identity is fixed when the endpoint is built, not per call.</strong>
    The server assembles one MCP service per roster entry up front; dispatch is
    a map lookup on the token.
  </li>
  <li>
    <strong>There is still one SQLite file.</strong> Two people on two machines
    race through the same compare-and-set, and exactly one wins.
  </li>
  <li>
    <strong>Nothing routes.</strong> The server hands nobody anything. Work
    crosses machines because a human says <em>file this</em> on one side and
    <em>pick up 42</em> on the other.
  </li>
</ol>
</section>

<section id="request">
<h2>Watch a request land</h2>

<p>
  Pick who is calling, which token they present, and what they ask for. The
  packet walks the same five stages the real server walks it through — and the
  board and trail at the bottom are the queue's own state, shared by every
  panel on this page.
</p>

<div class="demo" id="demo-request">
  <div class="controls">
    <label>caller
      <select id="who">
        <option value="ana">Ana — Claude Code, her laptop</option>
        <option value="ben">Ben — Codex, a cloud container</option>
        <option value="stranger">a stranger on the internet</option>
      </select>
    </label>
    <label>token
      <select id="tok">
        <option value="own">the one the roster gave them</option>
        <option value="wrong">a token nobody issued</option>
        <option value="none">no Authorization header</option>
      </select>
    </label>
    <label>call
      <select id="call">
        <option value="task_next">task_next</option>
        <option value="task_claim">task_claim</option>
        <option value="task_update">task_update &mdash; status=in_progress</option>
        <option value="task_complete">task_complete</option>
        <option value="task_list">task_list</option>
        <option value="mem_store">mem_store</option>
      </select>
    </label>
    <label id="seq-wrap" hidden>task
      <select id="seq"></select>
    </label>
    <span class="spacer"></span>
    <button class="go" id="send" type="button">Send</button>
    <button id="reset" type="button">Reset the board</button>
  </div>

  <div class="body">
    <div class="pipe" id="pipe">
      <div class="stage" data-stage="wire">
        <h5>1 · the wire</h5>
        <div class="code" id="s-wire">POST / HTTP/1.1<br>waiting…</div>
      </div>
      <div class="stage" data-stage="bearer">
        <h5>2 · bearer()</h5>
        <div class="code" id="s-bearer">strip_prefix<br>("Bearer ")</div>
      </div>
      <div class="stage" data-stage="roster">
        <h5>3 · roster lookup</h5>
        <div class="code" id="s-roster">endpoints.get(token)</div>
      </div>
      <div class="stage" data-stage="endpoint">
        <h5>4 · the endpoint</h5>
        <div class="code" id="s-endpoint">one service per identity</div>
      </div>
      <div class="stage" data-stage="queue">
        <h5>5 · the queue</h5>
        <div class="code" id="s-queue">Shared { Mutex&lt;Db&gt; }<br>one SQLite file</div>
      </div>
      <div class="packet" id="packet">POST</div>
    </div>

    <div class="out">
      <div>
        <h5>What came back</h5>
        <p class="status-line" id="status">—</p>
        <pre><code id="response">Press <b>Send</b>.</code></pre>
      </div>
      <div>
        <h5>What the queue ran</h5>
        <pre><code id="sql">—</code></pre>
      </div>
    </div>
  </div>
</div>

<div class="grid-2 board-wrap">
  <div>
    <h5>The board — project /srv/acme <span class="clock" id="clock"></span></h5>
    <div class="table-wrap">
      <table class="board">
        <thead><tr><th>#</th><th>task</th><th>status</th><th>holder</th><th>lease</th></tr></thead>
        <tbody id="board"></tbody>
      </table>
    </div>
    <p class="quiet">
      The queue's clock runs at &times;60 here, so a fifteen-minute lease lapses
      in fifteen seconds. Nothing else is sped up.
    </p>
  </div>
  <div>
    <h5>The trail — every event, append-only</h5>
    <ul class="trail" id="trail"></ul>
    <p class="quiet">
      Same rows <code>hird events</code> streams and the board's replay slider
      reads. Assertions filed by <code>mem_store</code> are memory, not task
      events, so they do not appear here.
    </p>
  </div>
</div>
</section>

<section id="roster-map">
<h2>The roster is the whole map</h2>

<p>
  Authorization is a TOML file the operator edits, deliberately outside the
  queue's own database — the queue records <em>what happened</em>; who is
  permitted to make things happen is a different kind of fact, and keeping it
  outside means a compromised queue cannot grant access to itself. The last
  request you sent highlights the row it resolved to:
</p>

<div class="table-wrap">
  <table class="roster" id="roster-table">
    <thead><tr><th>token</th><th>identity</th><th>harness</th><th>project</th><th>capabilities</th></tr></thead>
    <tbody></tbody>
  </table>
</div>

<span class="filename">roster.toml</span>
<pre><code><span class="c"># The project every worker files into unless it names its own.</span>
project = <span class="s">"/srv/acme"</span>

[[worker]]
<span class="c"># Generate with `openssl rand -hex 32`. Refused under 24 characters:</span>
<span class="c"># this is the only thing between a stranger and the queue.</span>
token = <span class="s">"3f9c1d0e…7b21"</span>
identity = <span class="s">"ana"</span>
capabilities = [<span class="s">"browser"</span>, <span class="s">"linux"</span>]

[[worker]]
token = <span class="s">"a70b55c8…44de"</span>
identity = <span class="s">"ben"</span>
harness = <span class="s">"codex"</span>
capabilities = [<span class="s">"linux"</span>]</code></pre>

<p>
  Parsing refuses the mistakes that would quietly mis-attribute work rather than
  accepting them: a duplicated token — two people the queue could not tell apart
  — a missing project, a token under 24&nbsp;characters, an empty roster, a
  capability label the session would refuse. Each entry becomes its own MCP
  service at start-up, so by the time a request arrives there is nothing left
  to decide but which service to hand it to.
</p>

<p>
  Capabilities are per token, and a worker that pins a harness may inherit a
  set from a <code>[harness.&lt;name&gt;]</code> table — <code>[harness.opencode]</code>
  says what every pinned OpenCode worker starts with, and the worker's own
  labels add to it. Only the harness the <em>operator</em> pinned reaches that
  table; the name a client gives itself never does, because a label is
  something a human grants and the client's name for itself is the one thing
  it is allowed to be wrong about.
</p>

<div class="note">
  <span class="label">Refused before the database is touched</span>
  <p>
    A request with no token, and a request with a token the roster does not
    know, get the same answer:
    <code>401</code>, <code>WWW-Authenticate: Bearer realm="hird"</code>, and
    <code>this queue is not open to unnamed callers</code>. The same answer
    either way, so the response does not say which tokens are real.
  </p>
</div>
</section>

<section id="identity">
<h2>A client may not name the person</h2>

<p>
  A local <code>hird</code> reads its identity from the environment it was
  started in, because that environment belongs to the human running the
  harness. A server has no such luxury: the environment is the server's, and
  the far end of a socket can claim anything. So the rule is split. A client
  may say <em>what</em> it is — being wrong there costs a badge — and may never
  say <em>who</em> it is. Change what the client claims and watch the session
  the server actually builds:
</p>

<div class="demo" id="demo-identity">
  <div class="controls">
    <label>connecting with
      <select id="id-token">
        <option value="ana">Ana's token (harness unpinned)</option>
        <option value="ben">Ben's token (harness pinned to codex)</option>
      </select>
    </label>
    <label>the client calls itself
      <select id="id-harness">
        <option value="claude-code">claude-code</option>
        <option value="copilot">copilot</option>
        <option value="">nothing at all</option>
      </select>
    </label>
    <label>and claims to be
      <select id="id-claim">
        <option value="">nobody in particular</option>
        <option value="ben">ben</option>
        <option value="root">root</option>
      </select>
    </label>
  </div>
  <div class="body">
    <div class="grid-2">
      <div>
        <h5>What the client sent</h5>
        <pre><code id="id-sent"></code></pre>
      </div>
      <div>
        <h5>The session the server built</h5>
        <pre><code id="id-built"></code></pre>
        <ul id="id-why" class="quiet" style="margin-top:0.75rem"></ul>
      </div>
    </div>
  </div>
</div>

<p>
  The actor string — <code>ana/claude-code:z55d</code> — is what lands on every
  claim, event and assertion the session makes: the person from the roster, the
  harness from wherever it was allowed to come from, and a fresh session suffix
  per connection. There is no path from a connection to a name it was not
  given.
</p>
</section>

<section id="race">
<h2>The race, and who wins it</h2>

<p>
  This is the load-bearing fact of the whole design, and going over HTTP does
  not bend it. Claiming is a single compare-and-set. Two agents on two machines
  reach for the same task in the same instant; both statements run against one
  file; one updates a row and the other updates none.
</p>

<div class="demo" id="demo-race">
  <div class="controls">
    <label>both reach for
      <select id="race-seq"></select>
    </label>
    <span class="spacer"></span>
    <button class="go" id="race-go" type="button">Claim it at the same instant</button>
  </div>
  <div class="body">
    <div class="lanes">
      <div class="lane" id="lane-ana">
        <h5>ana/claude-code — laptop</h5>
        <pre><code class="sql-a"></code></pre>
        <p class="rows">rows changed: <b>—</b></p>
        <p class="verdict"></p>
      </div>
      <div class="lane" id="lane-ben">
        <h5>ben/codex — cloud container</h5>
        <pre><code class="sql-b"></code></pre>
        <p class="rows">rows changed: <b>—</b></p>
        <p class="verdict"></p>
      </div>
    </div>
    <p class="quiet" id="race-note" style="margin-top:0.75rem">
      Both requests are already inside the server when this runs — different
      tokens, different endpoints, different sessions. The only thing they
      share is the file.
    </p>
  </div>
</div>

<div class="note">
  <span class="label">Why this survives the network and sync would not</span>
  <p>
    Zero rows updated means somebody else won. An append-only trail merges
    without coordination, but a claim is not an append — it is a consensus. Two
    machines with async-replicated copies would both see <code>open</code> and
    both win. One server in front of one file is why the swarm model still
    holds across machines, and it is the reason the transport was built before
    any sync was.
  </p>
</div>
</section>

<section id="lease">
<h2>The lease, and what happens when a session goes quiet</h2>

<p>
  A claim is a lease, not a lock. It lasts fifteen minutes by default, every
  <code>task_update</code> renews it, and if the agent holding it crashes, is
  killed, or simply wanders off, the task returns to <code>open</code> on its
  own. Nothing sweeps in the background: expiry is enforced lazily, by whoever
  calls next.
</p>

<div class="demo" id="demo-lease">
  <div class="controls">
    <button id="lease-claim" type="button">Ben claims a task, then his container dies</button>
    <button id="lease-tick" type="button">A call arrives from somebody else</button>
    <span class="spacer"></span>
    <span class="clock" id="lease-state">nothing held</span>
  </div>
  <div class="body">
    <pre><code id="lease-log">Nothing yet.</code></pre>
    <p class="quiet">
      A lapsed lease is not swept by a timer — there is no timer. It sits there,
      overdue, until the next call runs <code>sweep_announcing()</code> on its
      way in, which is exactly what the second button does. Self-healing within
      the TTL, and no background process to keep alive.
    </p>
  </div>
</div>
</section>

<section id="refuses">
<h2>What it refuses to do</h2>

<div class="cols">
  <div class="card">
    <h4>No TLS</h4>
    <p>
      It speaks plain HTTP and says so. Terminate TLS in a reverse proxy in
      front of it — a bearer token on an unencrypted connection is a token you
      have given away. Rolling certificates into the binary is how a server
      grows a configuration language.
    </p>
  </div>
  <div class="card">
    <h4>No accounts, no token issuance</h4>
    <p>
      The roster is a file you edit. OAuth&nbsp;2.1 is the right answer for a
      queue open to more than a few known people; a file is the right answer
      for two colleagues, and shipping the file keeps the question open
      honestly.
    </p>
  </div>
  <div class="card">
    <h4>No witness</h4>
    <p>
      The witness reads a working tree, and the server's tree is nobody's
      checkout. It prints a warning when the witness is on, because reporting
      about the host's own directory would be the design's one genuinely false
      report. Set <code>witness = false</code>.
    </p>
  </div>
  <div class="card">
    <h4>Nothing routes</h4>
    <p>
      No scheduler, no placement, no assignment. <code>task_next</code> hands
      out the next task worth doing <em>to a caller who asked for one</em>. The
      queue chooses nobody.
    </p>
  </div>
</div>

<p>
  It also carries no new surface: the same twelve MCP tools the local binary
  exposes — <code>task_list</code>, <code>task_get</code>,
  <code>task_claim</code>, <code>task_next</code>, <code>task_scope</code>,
  <code>task_split</code>, <code>task_release</code>, <code>task_update</code>,
  <code>task_complete</code>, <code>task_fail</code>, <code>mem_store</code>,
  <code>mem_search</code> — and the same six statuses. A transport may not grow
  the surface.
</p>
</section>

<section id="run">
<h2>Running one yourself</h2>

<pre><code><span class="k">hird-server</span> --roster roster.toml --bind 127.0.0.1 --port 7474
<span class="c">hird-server on http://127.0.0.1:7474  (2 in the roster)</span>
<span class="c">database: /var/lib/hird/hird.db</span></code></pre>

<p>
  It is a second binary rather than a flag on the first, and that is load
  bearing: <code>hird-server</code> carries axum, hyper and tower on its own
  side of the line, while <code>cargo tree -p hird</code> names no HTTP
  dependency at all. CI asserts exactly that, so the separation fails loudly
  rather than eroding.
</p>

<p>
  A harness then points at the URL with its token, and the server builds the
  session from the roster:
</p>

<pre><code>{ <span class="s">"mcpServers"</span>: { <span class="s">"hird"</span>: {
  <span class="s">"type"</span>: <span class="s">"http"</span>,
  <span class="s">"url"</span>: <span class="s">"https://queue.internal/"</span>,
  <span class="s">"headers"</span>: { <span class="s">"Authorization"</span>: <span class="s">"Bearer 3f9c1d0e…7b21"</span> }
} } }</code></pre>

<p>
  The longer written account is <a href="index.html">the usage guide</a>,
  <a href="https://github.com/aoprisan/hird/blob/main/DESIGN.md">DESIGN.md §31</a>
  for why it is shaped this way, and
  <a href="https://github.com/aoprisan/hird/blob/main/REMOTE.md">REMOTE.md</a>
  for what was assessed and rejected on the way here.
</p>
</section>

</main>
</div>

<footer class="page">
  <span>hird — one work queue and one memory, shared by every agent you run.</span>
  <span><a href="index.html">Usage guide</a></span>
  <span><a href="https://github.com/aoprisan/hird">github.com/aoprisan/hird</a></span>
</footer>

<script>
// ---------------------------------------------------------------- chrome
(function () {
  // Theme toggle: follow the OS by default, remember an explicit choice.
  var root = document.documentElement;
  var saved = null;
  try { saved = localStorage.getItem("hird-theme"); } catch (e) {}
  if (saved) root.setAttribute("data-theme", saved);

  document.getElementById("theme").addEventListener("click", function () {
    var dark = root.getAttribute("data-theme") === "dark" ||
      (!root.getAttribute("data-theme") &&
        window.matchMedia("(prefers-color-scheme: dark)").matches);
    var next = dark ? "light" : "dark";
    root.setAttribute("data-theme", next);
    try { localStorage.setItem("hird-theme", next); } catch (e) {}
  });

  // On a phone the contents fold away, so the page starts at the page.
  var nav = document.querySelector("nav.toc .nav-body");
  var narrow = window.matchMedia("(max-width: 62rem)");
  if (!nav) return;
  if (narrow.matches) nav.removeAttribute("open");
  nav.addEventListener("click", function (event) {
    if (narrow.matches && event.target.closest("a")) nav.removeAttribute("open");
  });
})();

// -------------------------------------------------------------- the queue
//
// One simulated hird-server, shared by every panel on this page — because the
// point being made is that there is one file underneath all of it.
(function () {
  "use strict";

  var TTL_MIN = 15;          // lease_ttl_minutes, the default
  var HEARTBEAT = 7;         // ttl / 2, what a claim reminds you to do
  var SPEED = 60;            // one real second is one queue minute
  var PROJECT = "/srv/acme";
  var EPOCH = Date.UTC(2026, 6, 25, 14, 0, 0);
  var origin = (window.performance && performance.now) ? performance.now() : 0;

  function qnow() {
    var elapsed = ((window.performance && performance.now) ? performance.now() : 0) - origin;
    return EPOCH + elapsed * SPEED;
  }
  function iso(ms) { return new Date(Math.round(ms)).toISOString(); }
  function clock(ms) { return iso(ms).slice(11, 19); }
  function mmss(ms) {
    var total = Math.max(0, Math.round(ms / 1000));
    return Math.floor(total / 60) + ":" + String(total % 60).padStart(2, "0");
  }
  function esc(s) {
    return String(s).replace(/[&<>]/g, function (c) {
      return { "&": "&amp;", "<": "&lt;", ">": "&gt;" }[c];
    });
  }

  // --------------------------------------------------------- the roster
  var ROSTER = [
    { token: "3f9c1d0e…7b21", identity: "ana", harness: null, project: PROJECT,
      capabilities: ["browser", "linux"], session: "z55d" },
    { token: "a70b55c8…44de", identity: "ben", harness: "codex", project: PROJECT,
      capabilities: ["linux"], session: "9f2c" }
  ];
  var BAD_TOKEN = "0f0f0f0f…dead";

  // One MCP service per roster entry, assembled at start-up. `harness` is a
  // once-only cell: the pin wins, and otherwise the first client name sticks.
  var ENDPOINTS = {};
  ROSTER.forEach(function (worker) {
    ENDPOINTS[worker.token] = { worker: worker, harness: worker.harness };
  });

  function sessionOf(token, clientName) {
    var endpoint = ENDPOINTS[token];
    if (!endpoint) return null;
    var worker = endpoint.worker;
    var harness = endpoint.harness || clientName || "unknown";
    return {
      identity: worker.identity,
      harness: harness,
      project: worker.project,
      capabilities: worker.capabilities,
      actor: worker.identity + "/" + harness + ":" + worker.session,
      pinned: !!worker.harness
    };
  }

  var CLIENTS = {
    ana: { who: "Ana", token: ROSTER[0].token, names: "claude-code", host: "her laptop" },
    ben: { who: "Ben", token: ROSTER[1].token, names: "codex", host: "a cloud container" },
    stranger: { who: "a stranger", token: BAD_TOKEN, names: "curl", host: "the internet" }
  };

  // ---------------------------------------------------------- the tables
  var tasks, events, assertions;

  function seed() {
    var now = qnow();
    tasks = [
      { seq: 5, title: "Write examples/roster.toml", status: "done", holder: null,
        lease: null, priority: 1, requires: [],
        body: "Every line is a credential. Say so in the comments." },
      { seq: 6, title: "Port the lease sweep test", status: "in_progress",
        holder: "ben/codex:9f2c", lease: now + 9 * 60000, priority: 1, requires: [],
        body: "tests/cli.rs proves the sweep; the server needs the same assertion." },
      { seq: 7, title: "Wire the bearer route", status: "open", holder: null, lease: null,
        priority: 2, requires: [],
        body: "Refuse an unknown token before the database is opened, and with the same answer either way." },
      { seq: 8, title: "Add the roster loader", status: "open", holder: null, lease: null,
        priority: 1, requires: [], body: "Parse roster.toml. Refuse a duplicate token." },
      { seq: 9, title: "Screenshot the board for the README", status: "open", holder: null,
        lease: null, priority: 1, requires: ["browser"],
        body: "hird web, dark theme, the swarm mid-flight." },
      { seq: 10, title: "Terminate TLS in front of the server", status: "open", holder: null,
        lease: null, priority: 0, requires: [],
        body: "A bearer token on a plain connection is a token you have given away." }
    ];
    events = [
      { at: now - 41 * 60000, seq: 5, kind: "completed", actor: "ana/claude-code:z55d",
        note: "roster.toml, comments and all" },
      { at: now - 12 * 60000, seq: 6, kind: "claimed", actor: "ben/codex:9f2c",
        note: "lease " + TTL_MIN + "m" },
      { at: now - 6 * 60000, seq: 6, kind: "status", actor: "ben/codex:9f2c",
        note: "in_progress" }
    ];
    assertions = 3;
  }

  function task(seq) {
    for (var i = 0; i < tasks.length; i++) if (tasks[i].seq === seq) return tasks[i];
    return null;
  }
  function record(at, seq, kind, actor, note) {
    events.push({ at: at, seq: seq, kind: kind, actor: actor, note: note });
  }

  // The lazy sweep every handler runs on its way in. There is no timer: a
  // lapsed lease sits there, overdue, until somebody calls.
  function sweep(now) {
    var swept = [];
    tasks.forEach(function (t) {
      if (t.lease && t.lease <= now && (t.status === "claimed" || t.status === "in_progress")) {
        var was = t.holder;
        t.status = "open";
        t.holder = null;
        t.lease = null;
        record(now, t.seq, "lease_expired", "hird", was + " let the lease lapse");
        swept.push(t.seq);
      }
    });
    return swept;
  }

  function missingCaps(t, session) {
    return t.requires.filter(function (cap) {
      return session.capabilities.indexOf(cap) === -1;
    });
  }

  // --------------------------------------------------------- the handlers
  //
  // Each returns { code, body, sql, swept } — `body` is what the tool answers
  // with, `sql` the statement the repository layer ran.
  function claim(session, seq, now) {
    var t = task(seq);
    if (!t) return { code: 200, error: "task " + seq + " not found", sql: SELECT_ONE(seq) };
    var missing = missingCaps(t, session);
    if (missing.length) {
      return {
        code: 200,
        error: "task " + seq + " requires " + missing.join(" and ") +
          ", but this session advertises " + session.capabilities.join(", ") +
          "; start a capable harness or update HIRD_CAPABILITIES",
        sql: "SELECT capability FROM task_requirements WHERE task_id = (SELECT id FROM tasks WHERE seq = " + seq + ");"
      };
    }
    var expires = now + TTL_MIN * 60000;
    var sql = CAS(seq, session.actor, expires, now);
    if (t.status !== "open") {
      return {
        code: 200,
        rows: 0,
        sql: sql,
        error: t.holder
          ? "task " + seq + " is " + t.status + " by " + t.holder + " until " + iso(t.lease)
          : "task " + seq + " is " + t.status + ", not open, so it cannot be claimed"
      };
    }
    t.status = "claimed";
    t.holder = session.actor;
    t.lease = expires;
    record(now, seq, "claimed", session.actor, "lease " + TTL_MIN + "m");
    var recalled = seq === 7 ? [{
      content: "The 401 body must not say whether the token exists — same answer for absent and unknown.",
      why: "filed against this task's files",
      actor: "ana/claude-code:k18p",
      task_seq: 3,
      created_at: iso(now - 4 * 3600000),
      id: "01JZ8Q…",
      standing: "firm"
    }] : [];
    var reminder = "call task_update at least every " + HEARTBEAT +
      " minutes to keep this lease, then task_complete or task_fail when you are done";
    if (recalled.length) {
      reminder = "read `recalled` first — earlier agents left those notes about this work, " +
        "and each says where it came from. Then: " + reminder;
    }
    var body = {
      claimed: seq,
      holder: session.actor,
      lease_expires_at: iso(expires),
      title: t.title,
      body: t.body,
      priority: t.priority,
      project: session.project
    };
    if (t.requires.length) body.requires = t.requires;
    if (recalled.length) body.recalled = recalled;
    body.reminder = reminder;
    return { code: 200, rows: 1, body: body, sql: sql };
  }

  function CAS(seq, actor, expires, now) {
    return "UPDATE tasks\n" +
      "   SET status = 'claimed',\n" +
      "       claimed_by = '" + actor + "',\n" +
      "       lease_expires_at = '" + iso(expires) + "',\n" +
      "       updated_at = '" + iso(now) + "'\n" +
      " WHERE seq = " + seq + "\n   AND status = 'open';";
  }
  function SELECT_ONE(seq) {
    return "SELECT seq, title, status, claimed_by, lease_expires_at\n" +
      "  FROM tasks WHERE seq = " + seq + ";";
  }

  function next(session, now) {
    var ready = tasks.filter(function (t) { return t.status === "open"; });
    var capable = ready.filter(function (t) { return missingCaps(t, session).length === 0; });
    capable.sort(function (a, b) { return b.priority - a.priority || a.seq - b.seq; });
    var sql = "SELECT seq, id, project FROM tasks\n" +
      " WHERE project = '" + session.project + "' AND status = 'open'\n" +
      " ORDER BY priority DESC, seq ASC;";
    if (!capable.length) {
      var incompatible = ready.filter(function (t) { return missingCaps(t, session).length; });
      var idle = incompatible.length
        ? "nothing is workable: " + incompatible.length +
          " requiring capabilities this session does not advertise"
        : "nothing is open in this project; the queue is empty";
      var body = { idle: idle };
      if (incompatible.length) {
        body.incompatible = incompatible.map(function (t) {
          return { seq: t.seq, missing: missingCaps(t, session) };
        });
      }
      return { code: 200, body: body, sql: sql };
    }
    var picked = claim(session, capable[0].seq, now);
    picked.sql = sql + "\n\n" + picked.sql;
    if (picked.body) picked.body = { claimed: picked.body };
    return picked;
  }

  function drive(session, seq, now, kind) {
    var t = task(seq);
    if (!t) return { code: 200, error: "task " + seq + " not found", sql: SELECT_ONE(seq) };
    if (t.holder !== session.actor) {
      return {
        code: 200,
        sql: SELECT_ONE(seq),
        error: t.holder
          ? "task " + seq + " is held by " + t.holder + ", not " + session.actor +
            "; only the lease holder can update it"
          : "task " + seq + " is " + t.status + " and unclaimed; " + session.actor +
            " must claim it first"
      };
    }
    if (kind === "complete") {
      t.status = "done";
      t.holder = null;
      t.lease = null;
      record(now, seq, "completed", session.actor, "result recorded");
      return {
        code: 200,
        rows: 1,
        sql: "UPDATE tasks\n   SET status = 'done', claimed_by = NULL, lease_expires_at = NULL,\n" +
          "       result = ?, updated_at = '" + iso(now) + "'\n WHERE id = ?;",
        body: { task: seq, status: "done", result_recorded: true, project: session.project }
      };
    }
    var expires = now + TTL_MIN * 60000;
    var was = t.status;
    t.status = "in_progress";
    t.lease = expires;
    record(now, seq, was === "in_progress" ? "lease_renewed" : "status", session.actor,
      was === "in_progress" ? "lease " + TTL_MIN + "m" : "in_progress");
    return {
      code: 200,
      rows: 1,
      sql: "UPDATE tasks\n   SET status = 'in_progress', lease_expires_at = '" + iso(expires) +
        "',\n       updated_at = '" + iso(now) + "'\n WHERE id = ?;",
      body: {
        task: seq,
        status: "in_progress",
        lease_expires_at: iso(expires),
        reminder: "call task_update at least every " + HEARTBEAT + " minutes to keep this lease"
      }
    };
  }

  function list(session) {
    return {
      code: 200,
      sql: "SELECT seq, title, status, claimed_by, lease_expires_at\n" +
        "  FROM tasks WHERE project = '" + session.project + "' ORDER BY seq;",
      body: {
        project: session.project,
        tasks: tasks.map(function (t) {
          var row = { seq: t.seq, title: t.title, status: t.status };
          if (t.holder) row.holder = t.holder;
          return row;
        })
      }
    };
  }

  function remember(session, now) {
    assertions += 1;
    return {
      code: 200,
      rows: 1,
      sql: "INSERT INTO assertions (id, project, content, actor, created_at)\n" +
        "VALUES ('01K2…', '" + session.project + "', ?, '" + session.actor + "', '" + iso(now) + "');",
      body: {
        stored: "01K2R9…",
        project: session.project,
        actor: session.actor,
        note: "an assertion is memory, not a task event; it does not appear in the trail"
      }
    };
  }

  // --------------------------------------------------------- the drawing
  var boardEl = document.getElementById("board");
  var trailEl = document.getElementById("trail");
  var clockEl = document.getElementById("clock");
  var rosterBody = document.querySelector("#roster-table tbody");
  var flash = {};

  function renderBoard() {
    var now = qnow();
    var real = Date.now();
    boardEl.innerHTML = tasks.map(function (t) {
      var lease = "—";
      if (t.lease) {
        var left = t.lease - now;
        lease = left > 0
          ? '<span class="lease">' + mmss(left) + "</span>"
          : '<span class="lease lapsed">lapsed ' + mmss(-left) + " ago</span>";
      }
      var requires = t.requires.length
        ? ' <span class="quiet">· needs ' + esc(t.requires.join(", ")) + "</span>"
        : "";
      return '<tr class="' + (flash[t.seq] > real ? "moved" : "") + '">' +
        '<td class="seq">' + t.seq + "</td>" +
        "<td>" + esc(t.title) + requires + "</td>" +
        '<td><span class="pill ' + t.status + '">' + t.status + "</span></td>" +
        '<td class="who">' + (t.holder ? esc(t.holder) : "—") + "</td>" +
        "<td>" + lease + "</td></tr>";
    }).join("");
    clockEl.textContent = "· queue time " + clock(now) + " ·  ×" + SPEED + " ·  " +
      assertions + " assertions";
  }

  function renderTrail() {
    var rows = events.slice(-40).reverse();
    trailEl.innerHTML = rows.map(function (e) {
      return "<li><span class='t'>" + clock(e.at) + "</span>" +
        "<span class='s'>#" + e.seq + "</span>" +
        "<span class='k " + e.kind + "'>" + e.kind + "</span>" +
        "<span class='a'>" + esc(e.actor) + (e.note ? " · " + esc(e.note) : "") + "</span></li>";
    }).join("");
  }

  function renderRoster(hitToken) {
    rosterBody.innerHTML = ROSTER.map(function (w) {
      return '<tr class="' + (hitToken === w.token ? "hit" : "") + '">' +
        "<td>" + esc(w.token) + "</td><td>" + esc(w.identity) + "</td>" +
        "<td>" + (w.harness ? esc(w.harness) + " <span class='quiet'>(pinned)</span>"
          : "<span class='quiet'>the client names itself</span>") + "</td>" +
        "<td>" + esc(w.project) + "</td><td>" + esc(w.capabilities.join(", ")) + "</td></tr>";
    }).join("");
  }

  function touch(seq) { flash[seq] = Date.now() + 1600; }

  // ------------------------------------------------------ the pipeline UI
  var pipe = document.getElementById("pipe");
  var packet = document.getElementById("packet");
  var STAGES = ["wire", "bearer", "roster", "endpoint", "queue"];
  var stageEl = {}, stageText = {};
  STAGES.forEach(function (key) {
    stageEl[key] = pipe.querySelector('[data-stage="' + key + '"]');
    stageText[key] = document.getElementById("s-" + key);
  });
  var statusEl = document.getElementById("status");
  var responseEl = document.getElementById("response");
  var sqlEl = document.getElementById("sql");
  var busy = false;

  function clearStages() {
    STAGES.forEach(function (key) { stageEl[key].className = "stage"; });
    packet.classList.remove("flying", "refused");
    packet.style.transform = "translate(0,0)";
  }
  function mark(key, state) { stageEl[key].className = "stage " + state; }
  function flyTo(key, label, refused) {
    var el = stageEl[key];
    packet.textContent = label;
    packet.classList.toggle("refused", !!refused);
    packet.classList.add("flying");
    packet.style.transform = "translate(" + (el.offsetLeft + 10) + "px," + el.offsetTop + "px)";
  }
  function steps(list) {
    var i = 0;
    (function go() {
      if (i >= list.length) { busy = false; controls(false); return; }
      var step = list[i++];
      window.setTimeout(function () { step.run(); go(); }, step.wait);
    })();
  }

  var whoSel = document.getElementById("who");
  var tokSel = document.getElementById("tok");
  var callSel = document.getElementById("call");
  var seqSel = document.getElementById("seq");
  var seqWrap = document.getElementById("seq-wrap");
  var sendBtn = document.getElementById("send");
  var resetBtn = document.getElementById("reset");
  var raceSel = document.getElementById("race-seq");
  var raceBtn = document.getElementById("race-go");

  function controls(disabled) {
    [sendBtn, resetBtn, raceBtn, whoSel, tokSel, callSel, seqSel, raceSel].forEach(function (el) {
      if (el) el.disabled = disabled;
    });
  }

  function fillSeqSelects() {
    var options = tasks.map(function (t) {
      return '<option value="' + t.seq + '">#' + t.seq + " " + esc(t.title) + "</option>";
    }).join("");
    var keep = seqSel.value, keepRace = raceSel.value;
    seqSel.innerHTML = options;
    raceSel.innerHTML = options;
    seqSel.value = keep && task(+keep) ? keep : "7";
    raceSel.value = keepRace && task(+keepRace) ? keepRace : "7";
  }

  function needsSeq(call) {
    return call === "task_claim" || call === "task_update" || call === "task_complete";
  }
  callSel.addEventListener("change", function () {
    seqWrap.hidden = !needsSeq(callSel.value);
  });

  function requestLines(client, token, call, seq) {
    var auth = token
      ? "Authorization: Bearer <b>" + esc(token) + "</b>"
      : "<span class='no'>no Authorization header</span>";
    var args = call === "task_next" || call === "task_list" ? "{}"
      : call === "mem_store" ? '{"content": "…"}'
      : call === "task_update" ? '{"seq": ' + seq + ',<br>&nbsp;"status": "in_progress"}'
      : '{"seq": ' + seq + "}";
    return "POST / HTTP/1.1<br>Host: queue.internal<br>" + auth + "<br><br>" +
      "tools/call<br><b>" + esc(call) + "</b> " + args + "<br><br>" +
      "<span class='quiet'>" + esc(client.who) + "<br>" + esc(client.host) + "</span>";
  }

  function show(code, reason, bodyText, sqlText, bad) {
    statusEl.textContent = code + " " + reason;
    statusEl.className = "status-line " + (bad ? "bad" : "ok");
    responseEl.textContent = bodyText;
    sqlEl.textContent = sqlText;
  }

  function send() {
    if (busy) return;
    busy = true;
    controls(true);
    clearStages();

    var client = CLIENTS[whoSel.value];
    var mode = tokSel.value;
    var token = mode === "none" ? null : (mode === "wrong" ? BAD_TOKEN : client.token);
    var call = callSel.value;
    var seq = parseInt(seqSel.value, 10);
    var plan = [];

    plan.push({ wait: 40, run: function () {
      mark("wire", "live");
      stageText.wire.innerHTML = requestLines(client, token, call, seq);
      flyTo("wire", call);
      show("…", "in flight", "waiting for the server", "—", false);
      renderRoster(null);
    } });

    plan.push({ wait: 520, run: function () {
      mark("wire", "pass");
      mark("bearer", "live");
      stageText.bearer.innerHTML = token
        ? 'strip_prefix<br>("Bearer ")<br>→ <b class="yes">Some(<br>"' + esc(token) + '")</b>'
        : 'no header to<br>strip<br>→ <b class="no">None</b><br>nothing to look up';
      flyTo("bearer", token ? "token" : "no token", !token);
    } });

    if (!token) {
      plan.push({ wait: 520, run: function () {
        mark("bearer", "stop");
        stageText.roster.innerHTML = "<span class='quiet'>never reached</span>";
        stageText.endpoint.innerHTML = "<span class='quiet'>never built</span>";
        stageText.queue.innerHTML = "<span class='quiet'>the database is never opened</span>";
        flyTo("wire", "401", true);
        show(401, "Unauthorized",
          'WWW-Authenticate: Bearer realm="hird"\n\nthis queue is not open to unnamed callers',
          "-- no statement ran: the refusal happens before the database is touched",
          true);
      } });
      steps(plan);
      return;
    }

    plan.push({ wait: 520, run: function () {
      mark("bearer", "pass");
      mark("roster", "live");
      var hit = ENDPOINTS[token];
      stageText.roster.innerHTML = 'endpoints.get(<br>&nbsp;"' + esc(token) + '")<br>→ ' +
        (hit ? '<b class="yes">' + esc(hit.worker.identity) + "</b>" : '<b class="no">None</b>');
      renderRoster(hit ? token : null);
      flyTo("roster", hit ? "→ " + hit.worker.identity : "unknown", !hit);
    } });

    if (!ENDPOINTS[token]) {
      plan.push({ wait: 520, run: function () {
        mark("roster", "stop");
        stageText.endpoint.innerHTML = "<span class='quiet'>never built</span>";
        stageText.queue.innerHTML = "<span class='quiet'>the database is never opened</span>";
        flyTo("wire", "401", true);
        show(401, "Unauthorized",
          'WWW-Authenticate: Bearer realm="hird"\n\nthis queue is not open to unnamed callers',
          "-- the same answer an absent token gets, so the response does not\n" +
          "-- say which tokens are real",
          true);
      } });
      steps(plan);
      return;
    }

    var session = sessionOf(token, client.names);

    plan.push({ wait: 520, run: function () {
      mark("roster", "pass");
      mark("endpoint", "live");
      stageText.endpoint.innerHTML = "<b>" + esc(session.actor) + "</b><br>" +
        esc(session.project) + "<br>caps: " + esc(session.capabilities.join(", ")) +
        "<br><span class='quiet'>identity was fixed when this endpoint was built</span>";
      flyTo("endpoint", session.identity + "'s session");
    } });

    plan.push({ wait: 520, run: function () {
      mark("endpoint", "pass");
      mark("queue", "live");
      flyTo("queue", "run");

      var now = qnow();
      var swept = sweep(now);
      var out;
      if (call === "task_list") out = list(session);
      else if (call === "task_next") out = next(session, now);
      else if (call === "task_claim") out = claim(session, seq, now);
      else if (call === "task_update") out = drive(session, seq, now, "update");
      else if (call === "task_complete") out = drive(session, seq, now, "complete");
      else out = remember(session, now);

      var lines = [];
      if (swept.length) {
        lines.push("sweep_announcing() → " + swept.length + " lapsed lease" +
          (swept.length > 1 ? "s" : "") + " returned to open (#" + swept.join(", #") + ")");
      }
      lines.push(call);
      if (out.rows !== undefined) lines.push("rows changed: " + out.rows);
      if (out.error) lines.push("→ error");
      stageText.queue.innerHTML = lines.map(esc).join("<br>");
      mark("queue", out.error ? "stop" : "pass");

      var sqlText = out.sql;
      if (swept.length) {
        sqlText = "-- first, on the way in:\nUPDATE tasks\n   SET status = 'open', claimed_by = NULL," +
          " lease_expires_at = NULL\n WHERE status IN ('claimed','in_progress')\n" +
          "   AND lease_expires_at < '" + iso(now) + "';\n-- " + swept.length +
          " row(s) swept\n\n" + sqlText;
      }
      if (out.rows !== undefined) sqlText += "\n-- " + out.rows + " row(s) changed";
      if (out.error) {
        show(200, "OK · the tool answered with an error", out.error, sqlText, true);
      } else {
        show(200, "OK", JSON.stringify(out.body, null, 2), sqlText, false);
      }
      touch(seq);
      if (out.body && out.body.claimed) {
        touch(typeof out.body.claimed === "object" ? out.body.claimed.claimed : out.body.claimed);
      }
      renderBoard();
      renderTrail();
    } });

    plan.push({ wait: 560, run: function () {
      flyTo("wire", statusEl.className.indexOf("bad") > -1 ? "error" : "200 OK",
        statusEl.className.indexOf("bad") > -1);
    } });
    plan.push({ wait: 700, run: function () { packet.classList.remove("flying"); } });

    steps(plan);
  }

  sendBtn.addEventListener("click", send);
  resetBtn.addEventListener("click", function () {
    if (busy) return;
    seed();
    flash = {};
    clearStages();
    STAGES.forEach(function (key) { stageText[key].innerHTML = "<span class='quiet'>waiting…</span>"; });
    stageText.wire.innerHTML = "POST / HTTP/1.1<br>waiting…";
    show("—", "", "Press Send.", "—", false);
    renderRoster(null);
    fillSeqSelects();
    renderBoard();
    renderTrail();
    leaseWatch = null;
    document.getElementById("lease-log").textContent = "Nothing yet.";
  });

  // ------------------------------------------------------------- the race
  var laneAna = document.getElementById("lane-ana");
  var laneBen = document.getElementById("lane-ben");
  var raceNote = document.getElementById("race-note");

  function paintLane(lane, sqlSel, result, won) {
    lane.className = "lane " + (won ? "won" : "lost");
    lane.querySelector(sqlSel).textContent = result.sql;
    lane.querySelector(".rows b").textContent =
      result.rows === undefined ? "—" : result.rows;
    lane.querySelector(".verdict").textContent = won
      ? "claimed. The lease is theirs for " + TTL_MIN + " minutes."
      : (result.error || "lost the race");
  }

  function race() {
    if (busy) return;
    busy = true;
    controls(true);
    var seq = parseInt(raceSel.value, 10);
    var now = qnow();
    sweep(now);

    var ana = sessionOf(ROSTER[0].token, "claude-code");
    var ben = sessionOf(ROSTER[1].token, "codex");
    var anaFirst = Math.random() < 0.5;
    var first = anaFirst ? ana : ben;
    var second = anaFirst ? ben : ana;

    // Two statements, one file. SQLite's write lock serialises them; the
    // second one to arrive finds `status = 'open'` no longer true.
    var firstResult = claim(first, seq, now);
    var secondResult = claim(second, seq, now + 1);

    paintLane(anaFirst ? laneAna : laneBen, anaFirst ? ".sql-a" : ".sql-b",
      firstResult, firstResult.rows === 1);
    paintLane(anaFirst ? laneBen : laneAna, anaFirst ? ".sql-b" : ".sql-a",
      secondResult, secondResult.rows === 1);

    var somebodyWon = firstResult.rows === 1 || secondResult.rows === 1;
    raceNote.textContent = somebodyWon
      ? "The write lock let " + first.identity + "'s statement in first — that is the only " +
        "difference between them, and it is decided inside SQLite, not by anything hird chose. " +
        "Run it again and it may go the other way."
      : "Neither statement changed a row: task " + seq + " was not open when they arrived, so " +
        "the WHERE clause matched nothing for either of them. That is the same mechanism, seen " +
        "from the other side. Pick a task that is still open — or reset the board — to watch " +
        "somebody win.";

    touch(seq);
    renderBoard();
    renderTrail();
    window.setTimeout(function () { busy = false; controls(false); }, 450);
  }
  raceBtn.addEventListener("click", race);

  // --------------------------------------------- a client may not say who
  var idToken = document.getElementById("id-token");
  var idHarness = document.getElementById("id-harness");
  var idClaim = document.getElementById("id-claim");
  var idSent = document.getElementById("id-sent");
  var idBuilt = document.getElementById("id-built");
  var idWhy = document.getElementById("id-why");

  function renderIdentity() {
    var worker = idToken.value === "ana" ? ROSTER[0] : ROSTER[1];
    var declared = idHarness.value;
    var claimed = idClaim.value;

    var sent = 'initialize\n  "clientInfo": { "name": ' +
      (declared ? '"' + declared + '"' : "null") + ' }\n\n' +
      "POST / HTTP/1.1\n  Authorization: Bearer " + worker.token +
      (claimed ? '\n  X-Hird-Identity: ' + claimed + "   <- invented by the client" : "") +
      '\n\ntools/call\n  { "name": "task_claim",\n    "arguments": { "seq": 7' +
      (claimed ? ', "identity": "' + claimed + '"' : "") + " } }";
    idSent.textContent = sent;

    var harness = worker.harness || declared || "unknown";
    idBuilt.textContent = "Session {\n" +
      '  identity:     "' + worker.identity + '"\n' +
      '  harness:      "' + harness + '"\n' +
      '  project:      "' + worker.project + '"\n' +
      "  capabilities: [" + worker.capabilities.map(function (c) { return '"' + c + '"'; }).join(", ") + "]\n" +
      "}\n\nactor recorded on everything it does:\n  " +
      worker.identity + "/" + harness + ":" + worker.session;

    var why = [];
    if (claimed) {
      why.push("<b>" + esc(claimed) + " was ignored.</b> No header and no tool argument " +
        "names the person; the identity comes from the roster row the token resolved to, " +
        "and there is no code path that reads anything else.");
    }
    if (worker.harness) {
      why.push("<b>The harness was ignored too</b> — this roster entry pins <code>" +
        esc(worker.harness) + "</code>, and a pin wins over what the client calls itself.");
    } else if (declared) {
      why.push("<b>The harness was taken.</b> The entry leaves it unpinned, so the client's " +
        "own name for itself fills a once-only cell on the first call and sticks for the " +
        "life of the connection. Being wrong here costs a badge, not an attribution.");
    } else {
      why.push("<b>The client would not say what it is</b>, and nothing else can, so the " +
        "harness is filed as <code>unknown</code>. The person is unaffected: that half " +
        "never came from the client.");
    }
    idWhy.innerHTML = why.map(function (w) { return "<li>" + w + "</li>"; }).join("");
  }
  [idToken, idHarness, idClaim].forEach(function (el) {
    el.addEventListener("change", renderIdentity);
  });

  // ------------------------------------------------------------ the lease
  var leaseClaim = document.getElementById("lease-claim");
  var leaseTick = document.getElementById("lease-tick");
  var leaseState = document.getElementById("lease-state");
  var leaseLog = document.getElementById("lease-log");
  var leaseWatch = null;
  var leaseLines = [];

  function say(line) {
    leaseLines.push(line);
    if (leaseLines.length > 12) leaseLines.shift();
    leaseLog.textContent = leaseLines.join("\n");
  }

  leaseClaim.addEventListener("click", function () {
    var now = qnow();
    sweep(now);
    var open = tasks.filter(function (t) { return t.status === "open"; });
    if (!open.length) {
      say(clock(now) + "  nothing is open to claim — reset the board above.");
      return;
    }
    var ben = sessionOf(ROSTER[1].token, "codex");
    var target = open[0];
    var out = claim(ben, target.seq, now);
    if (out.error) { say(clock(now) + "  " + out.error); return; }
    leaseWatch = target.seq;
    say(clock(now) + "  task_claim {\"seq\": " + target.seq + "}  →  " + ben.actor);
    say(clock(now) + "  lease_expires_at " + out.body.lease_expires_at + "  (" + TTL_MIN + " minutes)");
    say(clock(now) + "  the container is destroyed. No FIN, no goodbye, nothing to notice.");
    touch(target.seq);
    renderBoard();
    renderTrail();
  });

  leaseTick.addEventListener("click", function () {
    var now = qnow();
    var swept = sweep(now);
    var ana = sessionOf(ROSTER[0].token, "claude-code");
    if (swept.length) {
      say(clock(now) + "  task_list from " + ana.actor);
      swept.forEach(function (seq) {
        say(clock(now) + "  sweep_announcing() → task " + seq +
          " returned to open, lease_expired recorded");
        touch(seq);
      });
      if (swept.indexOf(leaseWatch) > -1) leaseWatch = null;
    } else {
      say(clock(now) + "  task_list from " + ana.actor + "  →  nothing was overdue" +
        (leaseWatch ? "; task " + leaseWatch + " still has time on its lease" : ""));
    }
    renderBoard();
    renderTrail();
  });

  function renderLeaseState() {
    if (leaseWatch === null) { leaseState.textContent = "nothing held"; return; }
    var t = task(leaseWatch);
    if (!t || !t.lease) { leaseState.textContent = "task " + leaseWatch + " is back in the pool"; return; }
    var left = t.lease - qnow();
    leaseState.textContent = left > 0
      ? "task " + t.seq + " · " + mmss(left) + " left on the lease"
      : "task " + t.seq + " · lapsed " + mmss(-left) + " ago, and still marked " + t.status;
  }

  // -------------------------------------------------------------- running
  seed();
  fillSeqSelects();
  renderRoster(null);
  renderBoard();
  renderTrail();
  renderIdentity();
  seqWrap.hidden = !needsSeq(callSel.value);

  window.setInterval(function () {
    renderBoard();
    renderLeaseState();
  }, 250);
})();
</script>
</body>
</html>