@hotmeshio/long-tail 0.25.4 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/README.md +1 -0
  2. package/build/index.d.ts +1 -0
  3. package/build/index.js +8 -1
  4. package/build/lib/machines/index.d.ts +5 -0
  5. package/build/lib/machines/index.js +21 -0
  6. package/build/lib/machines/line.d.ts +44 -0
  7. package/build/lib/machines/line.js +114 -0
  8. package/build/lib/machines/protocol/codec.d.ts +92 -0
  9. package/build/lib/machines/protocol/codec.js +79 -0
  10. package/build/lib/machines/protocol/constants.d.ts +120 -0
  11. package/build/lib/machines/protocol/constants.js +97 -0
  12. package/build/lib/machines/protocol/contract.d.ts +19 -0
  13. package/build/lib/machines/protocol/contract.js +132 -0
  14. package/build/lib/machines/protocol/envelope.d.ts +14 -0
  15. package/build/lib/machines/protocol/envelope.js +43 -0
  16. package/build/lib/machines/protocol/index.d.ts +5 -0
  17. package/build/lib/machines/protocol/index.js +21 -0
  18. package/build/lib/machines/protocol/topics.d.ts +33 -0
  19. package/build/lib/machines/protocol/topics.js +79 -0
  20. package/build/lib/machines/site/mosquitto-conf.d.ts +27 -0
  21. package/build/lib/machines/site/mosquitto-conf.js +81 -0
  22. package/build/lib/machines/site/site-conf.d.ts +20 -0
  23. package/build/lib/machines/site/site-conf.js +55 -0
  24. package/build/lib/machines/transport/callback-feed.d.ts +18 -0
  25. package/build/lib/machines/transport/callback-feed.js +31 -0
  26. package/build/lib/machines/transport/delivery.d.ts +21 -0
  27. package/build/lib/machines/transport/delivery.js +42 -0
  28. package/build/lib/machines/transport/index.d.ts +11 -0
  29. package/build/lib/machines/transport/index.js +27 -0
  30. package/build/lib/machines/transport/memory-broker.d.ts +35 -0
  31. package/build/lib/machines/transport/memory-broker.js +145 -0
  32. package/build/lib/machines/transport/memory.d.ts +40 -0
  33. package/build/lib/machines/transport/memory.js +79 -0
  34. package/build/lib/machines/transport/mqtt-bus.d.ts +24 -0
  35. package/build/lib/machines/transport/mqtt-bus.js +43 -0
  36. package/build/lib/machines/transport/mqtt-link.d.ts +16 -0
  37. package/build/lib/machines/transport/mqtt-link.js +37 -0
  38. package/build/lib/machines/transport/mqtt-session.d.ts +34 -0
  39. package/build/lib/machines/transport/mqtt-session.js +127 -0
  40. package/build/lib/machines/transport/nats-bus.d.ts +42 -0
  41. package/build/lib/machines/transport/nats-bus.js +67 -0
  42. package/build/lib/machines/transport/nats-durable.d.ts +11 -0
  43. package/build/lib/machines/transport/nats-durable.js +28 -0
  44. package/build/lib/machines/transport/nats-feed.d.ts +26 -0
  45. package/build/lib/machines/transport/nats-feed.js +49 -0
  46. package/build/lib/machines/transport/serial.d.ts +21 -0
  47. package/build/lib/machines/transport/serial.js +54 -0
  48. package/build/lib/machines/transport/streams.d.ts +28 -0
  49. package/build/lib/machines/transport/streams.js +92 -0
  50. package/build/scripts/machine-line.d.ts +33 -0
  51. package/build/scripts/machine-line.js +301 -0
  52. package/build/services/machines/commands.d.ts +29 -0
  53. package/build/services/machines/commands.js +37 -0
  54. package/build/services/machines/config.d.ts +43 -0
  55. package/build/services/machines/config.js +84 -0
  56. package/build/services/machines/contracts.d.ts +193 -0
  57. package/build/services/machines/contracts.js +115 -0
  58. package/build/services/machines/forms.d.ts +55 -0
  59. package/build/services/machines/forms.js +37 -0
  60. package/build/services/machines/gateway/actions.d.ts +111 -0
  61. package/build/services/machines/gateway/actions.js +8 -0
  62. package/build/services/machines/gateway/address-book.d.ts +16 -0
  63. package/build/services/machines/gateway/address-book.js +24 -0
  64. package/build/services/machines/gateway/admission.d.ts +35 -0
  65. package/build/services/machines/gateway/admission.js +66 -0
  66. package/build/services/machines/gateway/api-escalations.d.ts +23 -0
  67. package/build/services/machines/gateway/api-escalations.js +101 -0
  68. package/build/services/machines/gateway/dispatcher.d.ts +63 -0
  69. package/build/services/machines/gateway/dispatcher.js +157 -0
  70. package/build/services/machines/gateway/escalations.d.ts +51 -0
  71. package/build/services/machines/gateway/escalations.js +40 -0
  72. package/build/services/machines/gateway/executor.d.ts +55 -0
  73. package/build/services/machines/gateway/executor.js +195 -0
  74. package/build/services/machines/gateway/gateway.d.ts +82 -0
  75. package/build/services/machines/gateway/gateway.js +247 -0
  76. package/build/services/machines/gateway/index.d.ts +10 -0
  77. package/build/services/machines/gateway/index.js +26 -0
  78. package/build/services/machines/gateway/jobs.d.ts +16 -0
  79. package/build/services/machines/gateway/jobs.js +24 -0
  80. package/build/services/machines/gateway/log.d.ts +41 -0
  81. package/build/services/machines/gateway/log.js +42 -0
  82. package/build/services/machines/gateway/lt-http.d.ts +51 -0
  83. package/build/services/machines/gateway/lt-http.js +120 -0
  84. package/build/services/machines/gateway/pacing.d.ts +29 -0
  85. package/build/services/machines/gateway/pacing.js +134 -0
  86. package/build/services/machines/gateway/plan-escalation.d.ts +19 -0
  87. package/build/services/machines/gateway/plan-escalation.js +201 -0
  88. package/build/services/machines/gateway/plan-machine.d.ts +16 -0
  89. package/build/services/machines/gateway/plan-machine.js +242 -0
  90. package/build/services/machines/gateway/plan-shared.d.ts +18 -0
  91. package/build/services/machines/gateway/plan-shared.js +36 -0
  92. package/build/services/machines/gateway/provision.d.ts +22 -0
  93. package/build/services/machines/gateway/provision.js +79 -0
  94. package/build/services/machines/gateway/receipts.d.ts +33 -0
  95. package/build/services/machines/gateway/receipts.js +65 -0
  96. package/build/services/machines/gateway/reconcile.d.ts +33 -0
  97. package/build/services/machines/gateway/reconcile.js +86 -0
  98. package/build/services/machines/gateway/registration.d.ts +38 -0
  99. package/build/services/machines/gateway/registration.js +121 -0
  100. package/build/services/machines/gateway/reply-waiter.d.ts +24 -0
  101. package/build/services/machines/gateway/reply-waiter.js +65 -0
  102. package/build/services/machines/gateway/state.d.ts +125 -0
  103. package/build/services/machines/gateway/state.js +228 -0
  104. package/build/services/machines/gateway/trace.d.ts +16 -0
  105. package/build/services/machines/gateway/trace.js +43 -0
  106. package/build/services/machines/identities.d.ts +25 -0
  107. package/build/services/machines/identities.js +77 -0
  108. package/build/services/machines/index.d.ts +11 -0
  109. package/build/services/machines/index.js +27 -0
  110. package/build/services/machines/leadership.d.ts +22 -0
  111. package/build/services/machines/leadership.js +75 -0
  112. package/build/services/machines/roles.d.ts +13 -0
  113. package/build/services/machines/roles.js +62 -0
  114. package/build/services/machines/rows.d.ts +30 -0
  115. package/build/services/machines/rows.js +56 -0
  116. package/build/services/machines/select.d.ts +18 -0
  117. package/build/services/machines/select.js +63 -0
  118. package/build/services/machines/surface.d.ts +33 -0
  119. package/build/services/machines/surface.js +98 -0
  120. package/build/services/machines/testing/fake-long-tail.d.ts +78 -0
  121. package/build/services/machines/testing/fake-long-tail.js +155 -0
  122. package/build/services/machines/testing/index.d.ts +3 -0
  123. package/build/services/machines/testing/index.js +19 -0
  124. package/build/services/machines/testing/recording-log.d.ts +14 -0
  125. package/build/services/machines/testing/recording-log.js +22 -0
  126. package/build/services/machines/testing/wait.d.ts +3 -0
  127. package/build/services/machines/testing/wait.js +17 -0
  128. package/build/services/mcp/db-server/schemas.d.ts +2 -2
  129. package/build/start/index.js +13 -0
  130. package/build/system/mcp-servers/admin/schemas.d.ts +221 -221
  131. package/build/system/mcp-servers/db-query/schemas.d.ts +2 -2
  132. package/build/system/mcp-servers/human-queue-schemas.d.ts +6 -6
  133. package/build/tsconfig.tsbuildinfo +1 -1
  134. package/build/types/index.d.ts +2 -1
  135. package/build/types/machines.d.ts +166 -0
  136. package/build/types/machines.js +7 -0
  137. package/build/types/startup.d.ts +23 -0
  138. package/docs/machines.md +293 -0
  139. package/package.json +11 -1
@@ -0,0 +1,293 @@
1
+ # Machines as resolvers
2
+
3
+ A workflow step parks an escalation row and waits. A person resolves it on the dashboard, an API caller resolves it over HTTP, and with the machine surface a machine resolves it over MQTT. The row, the wait and the result are the same in every case. By the time the row resolves, the protocol has been turned into data on the escalation, the same way a form submission is.
4
+
5
+ This page is the contract for a host. A runnable factory lives in [examples/workflows/machine-line](../examples/workflows/machine-line/README.md).
6
+
7
+ ## Declaration
8
+
9
+ ```typescript
10
+ import { start, Machines } from '@hotmeshio/long-tail';
11
+
12
+ await start({
13
+ // ...database, workers, roles
14
+ machines: {
15
+ lines: [Machines.loadLine('/etc/line.json')],
16
+ codec: myCodec, // optional: the machines' wire protocol (default: the generic line protocol)
17
+ rows: myRows, // optional: how your rows read as handoffs (default: the standard handoff facets)
18
+ },
19
+ });
20
+ ```
21
+
22
+ Transports come from the environment (see [Configuration](#configuration)). `mqtt` is an optional peer dependency; install it in the host when a line runs.
23
+
24
+ For each line, one replica leads (a Postgres advisory lock held on its own connection), makes sure the line's bot accounts exist, and runs the gateway. Other replicas wait on the lock and take over when the leader's session ends.
25
+
26
+ ## The line
27
+
28
+ ```json
29
+ {
30
+ "siteId": "site-1",
31
+ "lineId": "line-1",
32
+ "roles": { "exit": "inspector", "operator": "line-operator", "receivers": ["harvester"] },
33
+ "machines": [
34
+ { "machineId": "bond-cell-1", "process": "bond", "role": "bond-cell", "carrier": "none",
35
+ "cycleSec": 8, "queueDepth": 4, "recipes": { "RCP-BOND-A": ["2"] }, "allowedSoftware": ["1.4.0"] }
36
+ ]
37
+ }
38
+ ```
39
+
40
+ | Field | Meaning |
41
+ |---|---|
42
+ | `machines[].role` | The role whose handoff rows the machine resolves. Default `line-<process>` |
43
+ | `machines[].address` | The physical machine on the wire: its topic level, client id and certificate name, such as a permanent UUID that stays with the machine if it moves. Default `machineId`, which stays the station that rows, roles and routes name |
44
+ | `addressing` | `declared` (default): each address is `machines[].address`, or the station's id. `learned`: a station with no `address` learns it from the machine that claims it. See [Learned addresses](#learned-addresses) |
45
+ | `roles.exit` | The role of the person who receives units at the end of a route. Default `line-exit` |
46
+ | `roles.operator` | The role that gets failed handoffs, stopping alarms, and offline or quiet machines. Default `line-operator` |
47
+ | `roles.receivers` | Other roles of people who take units straight from a machine. Machine bots join these roles and the exit role, so they can record their releases on those rows |
48
+ | `machines[].allowedSoftware` | Versions the line accepts. A machine reporting another version is refused before any dispatch |
49
+ | `machines[].queueDepth` | Most jobs the machine is sent or holds at once. Unset: no limit. See [Pacing](#pacing) |
50
+ | `machines[].requireArtifact` | The machine needs a job file. A handoff with neither the job's `artifactUrl` and `artifactSha256` nor an `artifact` on every unit is refused before anything is sent |
51
+
52
+ `start({ machines })` declares the station, exit and operator roles unless `roles` already declares them; set `declareRoles: false` to own them all.
53
+
54
+ ## Rows
55
+
56
+ A machine handoff is a row on the receiving machine's role (a handoff to a person goes on one of the line's receiver roles) whose metadata says what to move:
57
+
58
+ | Facet | Meaning |
59
+ |---|---|
60
+ | `siteId`, `lineId` | The line; a gateway acts only on its own line's rows |
61
+ | `units` | `[{ unitId, carrierId, artifact?, parameters? }]`: the units that move together; any of a route's units, all or some. A unit's `artifact` (`{ url, sha256 }`) is its own file, such as a per-unit model; its `parameters` are its own settings |
62
+ | `jobId`, `senderJobId` | The receiver's job and the sender's. Each open handoff into a machine names its own job; a second open row naming the same job there resolves `dispatch-refused`. A row for some of a sender job's units still receives their releases, however the codec split that job |
63
+ | `fromMachine`, `toMachine` | Machine ids, or null for line entry and for a person |
64
+ | `recipeId`, `recipeVersion`, `process` | What the receiver runs |
65
+ | `artifactUrl`, `artifactSha256` | The job file, by reference |
66
+ | `parameters` | Settings for the job, sent to the machine as given. Long Tail does not read them; what they mean is between the host and its codec |
67
+ | `attempt` | Retry number |
68
+
69
+ The gateway dispatches to `toMachine` when the row is created, or, under a codec that dispatches on arrival, once every unit has arrived there. It claims the row as that machine once the machine accepts. It records each unit's release by the sender and each arrival at the receiver as items. It resolves the row as the receiver once every unit has arrived:
70
+
71
+ ```typescript
72
+ { received: true, receiver: 'cut-01', units: ['U1', 'U2'] }
73
+ ```
74
+
75
+ When a handoff ends without every unit (a failure, a timeout, a cancel), the gateway withdraws only the jobs none of whose units reached the receiver, so nothing queued for an absent unit runs later. A job some of whose units already arrived is under way: it stays, and what happens to it is the workflow's next decision. A workflow that wants it stopped parks a `cancel` command row (see Commands).
76
+
77
+ `Machines.handoffProgress(units, items)` reads where a handoff's units stand from its items (a row's items, or `$accumulated` on a finished wait): `{ released, arrived, missing }`. A workflow reads it to decide what a split means for its own process.
78
+
79
+ On arrival dispatch, the units are already at the receiver, so its acceptance resolves the row the same way.
80
+
81
+ When the handoff ends without a receipt, the row resolves `{ received: false, errorCode, unitId?, reason?, detail }`:
82
+
83
+ | `errorCode` | Cause |
84
+ |---|---|
85
+ | `dispatch-refused` | The machine refused or failed the dispatch (mode, recipe, queue, artifact), or the machine requires an artifact and the row has none. The reason is in `detail` |
86
+ | `dispatch-failed` | The machine accepted the job, then could not prepare it (artifact, recipe). The reason is in `detail` |
87
+ | `no-reply` | No reply after every attempt with the same command id, and the machine's snapshot does not list the job, or it sent none (`detail` then says the state is unknown). A job the snapshot lists counts as accepted. The dispatch is cancelled, so a copy still queued for an absent machine never runs |
88
+ | `software-unsupported` | The machine runs a version the line does not allow |
89
+ | `sender-rejected` | The sender finished a unit as reject, hold, scrap or rework. Its reason is in `reason` and `detail` |
90
+ | `jam`, `incomplete-handshake`, `carrier-mismatch`, `unreadable-id` | The move between machines failed |
91
+ | `withdrawn` | The receiver gave the job back |
92
+
93
+ A row on a person's role resolves the same way, from the dashboard. A workflow can mix both in one route and never tell them apart.
94
+
95
+ ### Traceability
96
+
97
+ When a sender finishes a unit, the gateway records the release on the row and, in the same statement, merges two facets into the row's metadata:
98
+
99
+ | Facet | Holds |
100
+ |---|---|
101
+ | `trace` | One entry per unit: `{ unitId, machineId, jobId, result, materials: [{ material, lot }] }` |
102
+ | `lots` | Every lot those units consumed, once each |
103
+
104
+ Both are containment-searchable, so the rows that used a lot, and with them the units, are one query: `GET /escalations?facets={"lots":["LOT-7"]}`. A unit's whole path is every handoff row whose `units` holds it. The sender's full finished record stays on the row as its `released:<unit>` item. A codec maps its own lot fields into `materials`.
105
+
106
+ ### Decisions on a failed handoff
107
+
108
+ `Machines.handoffDecisions(errorCode)` gives a default set of decisions for a failed handoff. A failure in the move leaves the units on the line, so `retry`, `manual` and `scrap` all fit. Units the sender finished as not ok (`sender-rejected`) leave the line and never re-enter it, so only `manual` and `scrap` fit. After a `partial-arrival` (a timeout once only some units arrived) the units are split between two stations, and a retry of the same hop would ask the sender for units it no longer holds, so the default offers `manual` and `scrap`. A workflow is free to offer its own decisions instead, such as moving only the missing units on; the platform reports the facts and does not choose.
109
+
110
+ The example route's policy for a split: it stops the receiver's job with a `cancel` command row, then a person collects each group of units where it stopped.
111
+
112
+ A host whose rows already carry their own facets supplies a `HandoffRowAdapter`:
113
+
114
+ ```typescript
115
+ const rows: Machines.HandoffRowAdapter = {
116
+ openRows: () => [{ type: 'station-step' }], // read when a replica takes the line
117
+ toHandoff: (row) => row.type === 'station-step' ? { siteId: row.metadata.plant, /* ... */ } : null,
118
+ };
119
+ ```
120
+
121
+ ## Pacing
122
+
123
+ A machine with a `queueDepth` is sent no more jobs than that at once. A handoff beyond it waits in the machine's role queue, pending and unclaimed, the way work waits for a busy person. The oldest waiting row goes out when the machine has room, and a full machine holds back its later rows even when a smaller one would fit. A machine with nothing in hand always takes the next row, even one with more jobs than the depth.
124
+
125
+ A machine's load is every job it has been sent or still holds:
126
+ - rows dispatched into it;
127
+ - rows whose units still wait on it to move on;
128
+ - units that arrived and have not left.
129
+
130
+ A unit leaves when the machine releases it, when it arrives at the next machine, or when the machine's snapshot no longer lists its job. A replica taking the line reads every open row back before it sends anything, so pacing survives a restart.
131
+
132
+ ## Commands
133
+
134
+ A host asks a machine to start, stop, pause, resume, change over or cancel a job (`cancel` needs `args.jobId`) by parking a `machine-command` row on the machine's role, from a workflow step or through the API, the way it asks a person for anything:
135
+
136
+ ```typescript
137
+ const reply = await conditional<Machines.CommandResolution>(`changeover-${orderId}`, {
138
+ ...Machines.commandRow(Machines.commandTarget(line, 'bond-cell-1'), 'changeover', { recipe: { id: 'RCP-BOND-B', version: '1' } }),
139
+ timeout: '10m',
140
+ });
141
+ ```
142
+
143
+ Asking a machine for a command takes the same access as parking any row on its role: write access to that role, or a workflow that parks the row. A command row on any other role resolves `unsupported`.
144
+
145
+ The gateway sends the command and resolves the row with the reply, as the machine that gave it:
146
+
147
+ ```typescript
148
+ { accepted: true, machineId: 'bond-cell-1', op: 'changeover', status: 'accepted', reason: 'changing over to RCP-BOND-B v1' }
149
+ ```
150
+
151
+ | `status` | Meaning |
152
+ |---|---|
153
+ | `accepted` | The machine took the command |
154
+ | `refused`, `failed` | The machine said no; its reason is in `reason` (a changeover to a version it does not hold is refused) |
155
+ | `no-reply` | No reply after every attempt. The command expired at the end of the asking, so a copy that reaches the machine later is refused, never run |
156
+ | `unsupported` | Not a command a row may ask for, not a machine on this line, or a command the line's codec has no message for; nothing was sent |
157
+
158
+ The example's `machineCommand` workflow is this one step.
159
+
160
+ ## The wire
161
+
162
+ The gateway works in the generic line protocol. A codec translates:
163
+
164
+ ```typescript
165
+ interface LineCodec {
166
+ root: string;
167
+ channels: { events: string[]; telemetry: string[]; commands: string[] }; // MQTT filters under each machine
168
+ topicFor?(scope: { siteId?, lineId?, address? }, channel: string): string; // the topic layout; absent levels are +
169
+ unitsPerDispatch: number | null; // 1: one dispatch per unit
170
+ dispatchOn?: 'created' | 'arrival'; // when a handoff's job goes out
171
+ strict?: boolean; // broken messages go to a person, never acted on
172
+ decode(topic: string, payload: string, retained?: boolean): Inspection[];
173
+ encode(ident: MachineIdent, command: MachineCommand): WireMessage[]; // [] when the machines lack the command
174
+ receipt?(ident: MachineIdent, messageIds: string[]): WireMessage[]; // for machines that keep an outbox
175
+ }
176
+ ```
177
+
178
+ - `decode` may return several events for one message. That is how a codec infers a receipt from a different event when the machines report no arrival. `Machines.genericCodec({ root, receiptFrom: 'started' | 'bound' })` does this for the generic protocol.
179
+ - With `unitsPerDispatch: 1` the gateway sends one job per unit, claims the row once every job is accepted, and cancels the accepted jobs if any is refused.
180
+ - `topicFor` lays out the topics, for a protocol with its own levels (a direction before the machine, say). The layout begins with `{root}/{site}/{line}/`. The broker ACL and the stream subjects follow it. Without it the layout is `{root}/{site}/{line}/{address}/{channel}`.
181
+ - `dispatchOn: 'arrival'` is for machines that take work only for units they have scanned in. The job goes out once every unit of the row has arrived, and each unit carries the arrival event its receiver reported (`units[].arrival`), so `encode` can echo the receiver's own visit id.
182
+ - A reply is `accepted` (recorded), and a machine that prepares its jobs may later reply `completed` (ready) or `failed` under the same `cmdId`. A failure after acceptance ends the handoff as `dispatch-failed`. When no reply comes, the gateway asks the machine for its snapshot before deciding.
183
+ - `receipt` confirms the messages the gateway is done with, for machines that resend an event until it is confirmed. `decode` marks each such message with a `receiptId`. The ids go out per machine in batches of up to 100 after `MACHINE_RECEIPT_DELAY_MS`, including messages already applied. A message still waiting for its row is confirmed once it lands.
184
+ - `strict: true` holds a message with contract violations: it is confirmed, never acted on, and one `contract-violation` row shows it to the operator role. Without it, known fields are acted on and the violations are logged.
185
+ - An alarm body may name the catalog edition its code comes from, `catalog: { domain, key, version }`. The alarm row then carries it as a lookup ref, so the row reads the code's meaning as of that edition.
186
+ - Event channels go to the durable stream and telemetry channels are read live. A machine reads its commands on the command channels. The broker ACL is rendered from these channels, so a codec with its own topics gets its own ACL. Unknown body fields are allowed and known ones are checked. Payloads may be up to 256 KB.
187
+ - When `encode` returns no messages, the machines have no such command. The gateway sends nothing and waits for nothing: a machine-command row resolves `unsupported`, and a cancel or snapshot the machines lack is logged as unsupported.
188
+
189
+ The generic protocol's topics are `{root}/{site}/{line}/{machine}/{channel}`. Every message is wrapped in `{ v, id, at, source, body }`.
190
+
191
+ | Channel | Purpose |
192
+ |---|---|
193
+ | `presence` (retained) | Birth and will, with the software version |
194
+ | `snapshot` (retained) | Mode, recipe, jobs, alarms, counters, boot id |
195
+ | `job` | Per unit: `queued`, `ready`, `arrived`, `bound`, `started`, `progress`, `finished`, `released`, `transfer-failed`, `withdrawn` |
196
+ | `alarm/<id>` (retained while active) | Raise, acknowledgement, clear (with who cleared it) |
197
+ | `telemetry` | Readings |
198
+ | `report/<name>` | Production, availability, inventory and other reporting, kept in the stream |
199
+ | `cmd` / `cmd/reply` | `dispatch`, `cancel`, `start`, `stop`, `pause`, `resume`, `changeover`, `ack-alarm`, `snapshot`; replies `accepted`, `completed`, `refused`, `failed`. Every command carries a `cmdId` (a repeat is answered, never run twice) and an expiry |
200
+
201
+ ## Rows for people
202
+
203
+ | Row | Raised when | Resolved when |
204
+ |---|---|---|
205
+ | `line-alarm` | A stopping alarm (critical or emergency) is raised | The machine clears it (with who cleared it). If a person resolves it first, the gateway sends `ack-alarm` |
206
+ | `machine-offline` | Presence goes offline past a debounce | The machine is heard again |
207
+ | `machine-quiet` | A connected machine sends nothing for `MACHINE_QUIET_MS` | It is heard again |
208
+ | `contract-violation` | A strict codec decodes a message that breaks the contract. The row holds the raw message and the violations | A person resolves it |
209
+ | `machine-registration` | On a line that learns addresses, an address claims a station bound to another address, or an address bound to one station claims another | A person resolves it with `{ decision: 'approve' }`, which binds the station to the claimant, or `reject` |
210
+
211
+ A replica taking a line reads back its learned addresses, its open handoff, command, alarm, offline, quiet and registration rows, and each open handoff's arrivals, so the receiver holds those units again. It then reads the machines' retained state, so a clear or a return that happened while no gateway was listening still resolves its row. A machine that restarts with a new boot id gets its accepted jobs dispatched again under the same command ids. An alarm id reused with a new raise time counts as a new alarm.
212
+
213
+ ## Topology and identity
214
+
215
+ ```
216
+ machines ──MQTT, TLS, client cert──▶ machine broker ──bridge──▶ site NATS
217
+ (Mosquitto) JetStream
218
+ ▲ ▲
219
+ └── gateway (commands) └── gateway (streams, read only)
220
+ ```
221
+
222
+ - **Machines:** each machine logs in over MQTT 3.1.1 or 5 with its own certificate; the CN (`{site}-{line}-{address}`) is its username. The broker ACL lets it write only its own channels and read only its own commands. The line controller may command every machine on the line.
223
+ - **Durable streams:** the bridge carries the line's topics one way, into the site NATS, where only the bridge may publish them. JetStream keeps `LINE_EVENTS` (30 days) and `LINE_TELEMETRY` (3 days). The gateway reads through a durable consumer and may publish only JetStream requests and acks.
224
+ - **Escalation events:** the gateway inside Long Tail reads them in process. The site NATS is a server of its own; Long Tail's events and NATS stay inside Long Tail.
225
+ - **Commands:** the gateway publishes commands to the machine broker at QoS 1, so they queue for a machine that is away.
226
+ - **Bot accounts:** in Long Tail each machine is a bot account on its station and receiver roles, so `claimed_by` and `resolved_by` name the machine. The controller bot holds those roles and the operator role, and raises the operator rows. The bots are member accounts with no password, under names the line derives; the line will not start while a person's account holds one of those names.
227
+ - **Rows are the authority:** an escalation event only tells the gateway which row to look at. It reads the row back as the controller and acts on the stored role, status and metadata. A row on a role the controller does not hold, or an event its stored row contradicts, does nothing.
228
+ - **Roles carry the access:** dispatching to a machine or commanding one means parking a row on that machine's role, so Long Tail's role access decides who can. The gateway acts on a handoff into a machine only on that machine's role, and on a handoff to a person only on a receiver role. A row commands only the machine whose role it is on: cancelling a handoff withdraws the receiver's dispatch, and the sender's job is stopped only by a `cancel` command row on the sender's role.
229
+ - **Rendering:** `Machines.renderSiteConf`, `renderMosquittoConf` and `renderMosquittoAcl(line, codec)` render the configuration from the line; the ACL takes its topics from the codec's channels.
230
+
231
+ ### Learned addresses
232
+
233
+ A line with `addressing: 'learned'` names its stations and lets each machine tell it who it is, so a machine can be commissioned, moved or replaced without editing the line.
234
+
235
+ - **The claim:** every message carries the machine's address (its topic level, which the broker ties to its certificate) and the station it says it is. A codec reports that station as the inspection's `claim`; the line protocol carries it as the envelope's `source.station`.
236
+ - **First contact binds:** a station with no address yet is bound to the first address that claims it. The gateway logs it, keeps it in its `AddressBook`, and sends the station's waiting work. Until then, handoffs into the station wait in its role queue, and command rows for it resolve `unsupported`.
237
+ - **A person settles a disagreement:** a claim on a station already bound to another address, or by an address bound to another station, raises one `machine-registration` row on the operator role. The claimant's messages have no effect, and get no receipt, until a person approves it. Approval binds only a claim the gateway has heard from that address since it took the line, for a station that learns its address; any other approval is logged (`machine_gateway.address.refused`) and changes nothing, and the claimant's next message raises a new row.
238
+ - **Where bindings live:** in process, on each station's bot account as the user property `machineAddress`; `MemoryAddressBook` otherwise. A replica taking the line reads them back before it sends anything.
239
+ - **Broker access:** stations that learn their address get no broker user of their own. `renderMosquittoAcl` writes `pattern` rules on `%u`, so every certificate may use the channels under its own name; each machine's certificate common name must therefore be its address.
240
+ - **Mixed lines:** a station that declares `address` on a learning line keeps it; claims on that station are ignored, and the gateway knows it only on its declared address. `renderMosquittoAcl` denies that station's channels to a certificate named for its address, which the `pattern` rules would otherwise grant.
241
+
242
+ ## Configuration
243
+
244
+ | Variable | Default | Meaning |
245
+ |---|---|---|
246
+ | `MACHINE_TRANSPORT` | | `nats` (site NATS streams) or `mqtt` (any MQTT broker, no replay beyond the session) |
247
+ | `MACHINE_ESCALATION_FEED` | `in-process` inside Long Tail, `core` for a standalone gateway | Where escalation events come from; `core` subscribes to Long Tail's NATS (`NATS_URL`, `NATS_TOKEN`) |
248
+ | `MACHINE_TOPIC_ROOT` | `line/v1` | The generic codec's root |
249
+ | `MACHINE_LINE_CONFIG` | | Path of the line JSON |
250
+ | `MACHINE_SITE_NATS_URL`, `_USER`, `_PASSWORD` | | Site NATS for the streams |
251
+ | `MACHINE_MQTT_URL`, `_PROTOCOL` | `_PROTOCOL` 5 | Machine broker, for commands and retained reads; 4 for a broker that speaks only MQTT 3.1.1 |
252
+ | `MACHINE_MQTT_CA`, `_CERT`, `_KEY` | | TLS files; `{clientId}` in a path picks each identity's certificate |
253
+ | `MACHINE_REPLY_TIMEOUT_MS` | `5000` | Wait for a command reply |
254
+ | `MACHINE_DISPATCH_ATTEMPTS` | `3` | Tries per command before giving up |
255
+ | `MACHINE_DISPATCH_TTL_MS` | `600000` | How long a dispatch stays valid at the machine |
256
+ | `MACHINE_OFFLINE_DEBOUNCE_MS` | `15000` | Offline this long before a row |
257
+ | `MACHINE_QUIET_MS` | `90000` | Silent this long before a row; `0` turns it off |
258
+ | `MACHINE_RECEIPT_DELAY_MS` | `250` | How long confirmed message ids gather before a receipt goes out |
259
+
260
+ ## Testing a codec or adapter
261
+
262
+ A host proves its codec and row adapter against the real gateway in memory. The package provides the in-memory transports (`Machines.MemoryBroker`, `MemoryMachineBus`, `MemoryEscalationFeed`), and `@hotmeshio/long-tail/build/services/machines/testing` provides an in-memory Long Tail (`FakeLongTail`, with claim rules and simulated outages), a recording log and `until`. The test plays the machine: it answers the commands the gateway sends and publishes the messages a machine would, for example ones captured from a real machine or a simulator.
263
+
264
+ ```typescript
265
+ import { Machines } from '@hotmeshio/long-tail';
266
+ import { FakeLongTail, until } from '@hotmeshio/long-tail/build/services/machines/testing';
267
+
268
+ const line = Machines.loadLine('./line.json');
269
+ const codec = myCodec;
270
+
271
+ // 1. Broker, escalation feed, Long Tail and the gateway, all in memory
272
+ const broker = new Machines.MemoryBroker({ redeliverMs: 5 });
273
+ const feed = new Machines.MemoryEscalationFeed(broker);
274
+ const lt = new FakeLongTail(feed);
275
+ const gateway = new Machines.MachineGateway({
276
+ bus: new Machines.MemoryMachineBus(broker, Machines.lineFilter(codec.root, line.siteId, line.lineId)),
277
+ feed, lt, line, codec, rows: myRows,
278
+ settings: { topicRoot: codec.root, replyTimeoutMs: 50, dispatchAttempts: 2, offlineDebounceMs: 10, dispatchTtlMs: 60_000, quietMs: 0 },
279
+ });
280
+ await gateway.start();
281
+
282
+ // 2. Play the machine: see what the gateway sends, answer as the machine would
283
+ broker.connect('bond-cell-1');
284
+ broker.subscribe('bond-cell-1', myCommandTopic('bond-cell-1'), (msg) => broker.publish(myReplyTopic, myAcceptedReply(msg)));
285
+
286
+ // 3. Park a row the way a workflow step would, publish the machine's report, read the outcome
287
+ const row = lt.parkRow({ type: 'station-step', role: 'bond-cell', metadata: { /* the host's own facets */ } });
288
+ await until(() => lt.rows.get(row.id)?.assigned_to === 'bond-cell-1', 2_000);
289
+ broker.publish(capturedArrival.topic, capturedArrival.payload);
290
+ expect((await lt.ended(row.id)).resolver_payload).toMatchObject({ received: true, receiver: 'bond-cell-1' });
291
+ ```
292
+
293
+ The same rig covers the unhappy paths: cut the machine off the network for `no-reply`, drop only its replies to see the snapshot keep the job, answer with a refusal for `dispatch-refused`, publish a not-ok finish for `sender-rejected`, or stop and start the gateway to prove a takeover reads its rows back.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hotmeshio/long-tail",
3
- "version": "0.25.4",
3
+ "version": "0.26.0",
4
4
  "description": "Long Tail Workflows — Durable AI workflows with human-in-the-loop escalation. Powered by PostgreSQL.",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -49,6 +49,7 @@
49
49
  "demo": "bash scripts/demo.sh",
50
50
  "ortho:populate": "ts-node scripts/ortho-populate.ts",
51
51
  "twin:farm": "ts-node scripts/twin-farm.ts",
52
+ "machine:line": "ts-node scripts/machine-line.ts",
52
53
  "printers:cycle": "ts-node scripts/printer-cycle.ts",
53
54
  "consolidation:sim": "ts-node scripts/consolidation-sim.ts",
54
55
  "dashboard:install": "cd dashboard && npm install",
@@ -111,11 +112,20 @@
111
112
  "@types/node": "^20.19.33",
112
113
  "@types/pg": "^8.10.0",
113
114
  "dotenv": "^16.3.1",
115
+ "mqtt": "^5.16.1",
114
116
  "rimraf": "^4.4.1",
115
117
  "ts-node": "^10.9.1",
116
118
  "ts-node-dev": "^2.0.0",
117
119
  "tsx": "^4.21.0",
118
120
  "typescript": "^5.0.4",
119
121
  "vitest": "^3.2.4"
122
+ },
123
+ "peerDependencies": {
124
+ "mqtt": "^5.16.1"
125
+ },
126
+ "peerDependenciesMeta": {
127
+ "mqtt": {
128
+ "optional": true
129
+ }
120
130
  }
121
131
  }