@shumkov/orchestra 0.10.19 → 0.10.21

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.
@@ -0,0 +1,317 @@
1
+ # Folded reply attribution and duplicate fallback suppression
2
+
3
+ Status: independently reviewed; option 1 selected by the operator on
4
+ 2026-08-18 and implemented pending release verification.
5
+
6
+ ## Problem
7
+
8
+ On 2026-08-17 at 22:31 Asia/Bangkok, Shumabit@home topic `routine`
9
+ received a primary Telegram message (6406) followed by an autosteered message
10
+ (6407). Claude answered both in one rich `reply` tool call. The call echoed the
11
+ autosteer turn ID and named both the primary and autosteer IDs in
12
+ `consumed_turn_ids`.
13
+
14
+ The combined rich answer was sent as Telegram message 6408, quoting 6407.
15
+ Orchestra then classified that already-delivered reply as a late reply from an
16
+ older cycle instead of recording it on the active primary turn. The primary
17
+ therefore still had zero recorded replies. At Stop, its different
18
+ `last_assistant_message` was treated as an undelivered fallback and Polygram
19
+ sent message 6409 quoting 6406. The user saw two substantially overlapping
20
+ answers with reversed ownership.
21
+
22
+ The incident was not caused by Telegram rich-text rendering. Production runs
23
+ Polygram 0.38.0 with Orchestra 0.10.14, and the installed Polygram contains the
24
+ non-streamed rich-delivery path. Message 6408 rendered rich (13 blocks, 1,718
25
+ characters). Message 6409 came from the zero-reply fallback branch and its
26
+ content did not qualify for rich rendering, so it fell back to the plain
27
+ sender. Fixing attribution removes that accidental second message; the existing
28
+ non-streamed rich path remains the safety net for genuine zero-reply turns.
29
+
30
+ ## Production evidence
31
+
32
+ Read-only SQLite queries against the user-authorized Shumabit@home production
33
+ chat found, over the 14 days ending 2026-08-17:
34
+
35
+ - 37 `cli-late-reply-correlated` events whose ledger source was `autosteer`
36
+ (30 distinct echoed turn IDs).
37
+ - 12 distinct cases immediately followed by
38
+ `cli-consumed-ack-fallback-rescued`; 13 resolved with zero recorded replies.
39
+ - In topic `routine`, 7 late-autosteer events covered 6 distinct turn IDs; 3
40
+ distinct turns produced a zero-reply fallback rescue.
41
+ - The reported turn's event chain was:
42
+ `autosteer` → both inputs `cli-input-acked` →
43
+ `stream-preview-turn-mismatch` → rich 6408 →
44
+ `cli-late-reply-correlated(source=autosteer)` →
45
+ `cli-turn-resolved-by-stop(reply_count=0)` →
46
+ `cli-consumed-ack-fallback-rescued` → plain 6409.
47
+
48
+ Content review of all 12 Shumabit@home rescue cases found that every rescued
49
+ Stop text was a shorter recap, translation, or subset of the already-delivered
50
+ channels reply. None contained the only copy of a final outcome. This corrects
51
+ the older fallback design's assumption that differing text necessarily means
52
+ new user-visible content: in current production, 12/12 differing texts in this
53
+ fold shape were duplicates.
54
+
55
+ The last two `routine` rescue cases before and including the report produced
56
+ the same structural outcome: an already-delivered channels reply followed by a
57
+ primary zero-reply fallback. This is a lifecycle classification defect, not a
58
+ message-specific content error.
59
+
60
+ ## Operator decision
61
+
62
+ The user's literal report asks for the two existing messages to have their
63
+ anchors swapped. The recommended behavior below instead removes the accidental
64
+ second message and keeps one combined answer anchored to the primary. Product
65
+ and interaction review agree that this visible behavior change requires
66
+ explicit operator alignment before implementation.
67
+
68
+ There is one reliability trade-off. A successful `reply` call is final unless
69
+ it explicitly sets `interim:true`; that is the bridge's current hard contract.
70
+ In June 2026, Claude violated the contract once: it sent “Researching now…”
71
+ without `interim:true`, then left the real answer only in Stop. The existing
72
+ different-text rescue was added for that incident. Recording a proven folded
73
+ non-interim reply on the primary trusts the protocol again and would suppress
74
+ such an unmarked status if it recurred. Against that historical risk, the new
75
+ production review found 12/12 current rescues were duplicates, including the
76
+ reported case.
77
+
78
+ The operator selected option 1 from these product contracts:
79
+
80
+ 1. Recommended: one combined answer anchored to the primary; a successful
81
+ non-interim reply is authoritative final, while `interim:true` retains the
82
+ produced-final rescue.
83
+ 2. Conservative: preserve two outputs and explicitly map the Stop fallback to
84
+ the folded source. This protects unmarked statuses but intentionally keeps
85
+ the 12/12 observed duplicate shape and requires a cross-package source
86
+ override.
87
+
88
+ Option 2 matches the screenshot's requested anchor swap, but assigns the Stop
89
+ transcript to the autosteer without protocol evidence. It makes model
90
+ transcript leakage part of normal steering semantics and adds a cross-layer
91
+ source override for result delivery. Those costs make it the conservative
92
+ reliability option, not the recommendation, but it remains a legitimate choice
93
+ at this gate.
94
+
95
+ The remainder of this spec describes option 1.
96
+
97
+ ## Intended behavior
98
+
99
+ Polygram's autosteer contract is “merge into active”: a follow-up injected
100
+ while a primary turn is in flight is context absorbed by that primary turn.
101
+ The bridge contract explicitly supports one combined reply covering several
102
+ inputs through `consumed_turn_ids`, while `turn_id` can echo only one of them.
103
+
104
+ Therefore:
105
+
106
+ 1. A combined reply proven to consume the active primary belongs to the active
107
+ primary turn, even when Claude echoes a folded input's ID.
108
+ 2. Its first Telegram bubble quotes the primary source message.
109
+ 3. It is recorded as a delivered reply on the primary, so a different Stop
110
+ transcript does not become a second fallback answer.
111
+ 4. The folded input is still resolved through `consumed_turn_ids`; it does not
112
+ receive a separate answer because it was merged rather than queued as a new
113
+ turn.
114
+ 5. A genuine later cycle for a non-folded autosteer remains independently
115
+ attributable to that autosteer message.
116
+
117
+ For the reported incident, the intended visible result is one combined rich
118
+ answer quoting 6406. It is not two existing answers with their quotes swapped.
119
+ Preserving the second answer would elevate an accidental Stop fallback into a
120
+ first-class steering response even though it has no protocol identity proving
121
+ that ownership.
122
+
123
+ ## Recommended option 1
124
+
125
+ ### 1. Resolve one effective owner before dispatch
126
+
127
+ In `CliProcess`, resolve reply ownership once and reuse it for Telegram
128
+ dispatch and pending-turn bookkeeping. A reply that echoes a ledgered,
129
+ non-pending fold entry is remapped to a live pending turn only when all of the
130
+ following deterministic evidence is present:
131
+
132
+ - the echoed ledger entry source is a fold source (`autosteer` or
133
+ `edit-fold`);
134
+ - `consumed_turn_ids` contains the echoed fold ID;
135
+ - `consumed_turn_ids` contains exactly one currently pending turn ID; and
136
+ - the chat ID has already passed the existing session chat guard.
137
+
138
+ The uniquely consumed pending turn is the effective owner. Every other shape
139
+ keeps current behavior, including the late-reply safeguard for an old ledgered
140
+ ID. The implementation must not infer folding merely from “one pending turn.”
141
+
142
+ ### 2. Use the effective owner consistently
143
+
144
+ For a proven fold reply, use the effective pending owner for:
145
+
146
+ - dispatcher `turnId`, so the active stream preview accepts the final reply;
147
+ - `sourceMsgId` and `participantJid`, so the first bubble quotes/reacts to the
148
+ primary source;
149
+ - the entry whose `_quoteUsed` flag is spent after successful delivery; and
150
+ - `_recordReplyForPendingTurn`, so the active primary records the delivered
151
+ final or interim reply.
152
+
153
+ Keep the model-echoed ID for ledger acknowledgement, content deduplication, and
154
+ diagnostic fields. After successful dispatch and pending-turn recording, emit a
155
+ metadata-only `cli-fold-reply-attributed` event with the echoed fold ID,
156
+ effective pending ID, fold source, and whether the reply was interim. Thus the
157
+ event means a delivered attribution, not merely a pre-dispatch decision. Do not
158
+ log message content.
159
+
160
+ The attribution resolver should return explicit fields rather than mutate the
161
+ tool arguments. This preserves the wire receipt and makes it difficult for
162
+ dispatch attribution and bookkeeping attribution to drift apart again.
163
+
164
+ ### 3. Let existing finalization suppress the accidental fallback
165
+
166
+ Once the combined final reply is recorded on the primary,
167
+ `_computeTurnDelivery` takes its existing final-reply/already-delivered branch.
168
+ The Stop hook's different `last_assistant_message` is not re-sent. No new
169
+ fallback heuristic is needed.
170
+
171
+ If the proven fold reply is `interim:true`, it remains interim on the primary;
172
+ the existing interim-final rescue rules continue to apply. This change must
173
+ not turn a status message into a final answer.
174
+
175
+ If the reply omitted `interim:true`, it is authoritative final under the
176
+ bridge contract even when Stop later contains different text. This deliberately
177
+ revises the older “different means new” fallback assumption for proven current
178
+ folds; the 12-case production classification above is the evidence. The hard
179
+ prompt and schema wording for `interim` remain in place, and the rollout watches
180
+ for missing-final reports.
181
+
182
+ ### 4. Release through Orchestra, then consume in Polygram
183
+
184
+ The behavioral fix belongs in Orchestra because it owns the input ledger,
185
+ pending turns, reply-tool dispatch, and Stop finalization. After its focused and
186
+ full tests pass, release a new exact Orchestra version, update Polygram's exact
187
+ dependency, run Polygram's focused rich/non-streamed tests and full suite, then
188
+ roll out through the normal staged deploy path.
189
+
190
+ No database migration, Telegram API change, prompt change, or configuration
191
+ change is required.
192
+
193
+ ## Alternatives rejected
194
+
195
+ ### Trust the echoed `turn_id` unconditionally
196
+
197
+ Rejected. The bridge schema can express only one `turn_id`, while the existing
198
+ contract explicitly allows one reply to consume several inputs. Production
199
+ shows Claude can echo the fold ID for a combined reply.
200
+
201
+ ### Prompt Claude to echo the primary ID
202
+
203
+ Rejected. This is deterministic routing and must not depend on model
204
+ compliance. It would also regress silently when model behavior changes.
205
+
206
+ ### Route any ledgered fold ID to the sole pending turn
207
+
208
+ Rejected. This reopens the late-reply cross-attribution defect that the input
209
+ ledger was designed to close. Explicit `consumed_turn_ids` membership is the
210
+ proof that distinguishes a current fold from an old reply.
211
+
212
+ ### Suppress every different Stop fallback after a consumed ack
213
+
214
+ Rejected. A prior production incident established that a short consuming
215
+ status can be followed by a distinct real final answer in the Stop transcript.
216
+ The current rescue protects that case. Recording a proven combined final reply
217
+ on its actual owner solves this incident without weakening the rescue for
218
+ correctly marked `interim:true` replies.
219
+
220
+ The narrower chosen rule is different: only a successfully delivered reply
221
+ that the protocol marks final is authoritative. `interim:true` continues to
222
+ rescue a distinct produced final. The remaining risk is a model contract
223
+ violation that omits `interim:true`; it is called out in the operator decision
224
+ rather than hidden behind a text-difference heuristic that produced 12/12
225
+ duplicates in the measured current population.
226
+
227
+ ## Failure modes and boundaries
228
+
229
+ - Missing, empty, foreign, or ambiguous `consumed_turn_ids`: do not remap; keep
230
+ current late/orphan behavior and telemetry.
231
+ - More than one consumed pending turn: do not guess an owner.
232
+ - A ledgered `primary` or `system` ID that is not pending: remains a late reply.
233
+ - A fold reply after the primary finalized and with no consumed live pending
234
+ owner: remains independently attributed to the fold source.
235
+ - Dispatcher failure: do not spend the effective owner's quote, record the
236
+ consuming text, or record a delivered reply; preserve existing retry and
237
+ Stop-fallback behavior. Turn finalization waits while the consuming reply is
238
+ in flight so neither a successful delivery nor a failed-delivery fallback is
239
+ lost to the race.
240
+ - Multiple folded inputs in one reply: one uniquely consumed live primary may
241
+ own the combined reply; all named folds are acknowledged normally.
242
+ - Interim fold reply: quote ownership is corrected, but finalization remains
243
+ interim-aware.
244
+ - Non-interim fold reply followed by different Stop text: the reply is final by
245
+ contract and Stop is suppressed. This removes the measured duplicates but
246
+ no longer compensates for an omitted `interim:true` in this proven-fold
247
+ shape; the operator decision explicitly accepts or rejects that trade-off.
248
+ - WhatsApp opaque source IDs: preserve the existing numeric-or-opaque
249
+ conversion and participant binding by selecting an entry, not coercing IDs
250
+ differently.
251
+ - A model that falsely names the current primary in `consumed_turn_ids` can
252
+ still misattribute a reply. That field is already the system's explicit
253
+ fold-ack contract and is the strongest available protocol evidence; the new
254
+ telemetry makes its use measurable.
255
+
256
+ ## Test and verification plan
257
+
258
+ Follow bug-fix TDD: add the exact regression first, run it against unmodified
259
+ Orchestra and record the failure, then implement and rerun it green.
260
+
261
+ Focused regression cases:
262
+
263
+ 1. Primary source 6406 + autosteer source 6407; a successful final reply echoes
264
+ the fold ID and consumes both. Before the fix, dispatcher source is 6407,
265
+ its turn ID is the fold ID, the primary has zero replies, and a distinct Stop
266
+ fallback is deliverable. After the fix, dispatcher source is 6406, effective
267
+ turn ID is the primary, the primary records one final reply, and finalization
268
+ is already-delivered with no fallback send.
269
+ 2. The same shape with an active stream preview must not emit a turn-mismatch
270
+ and must finalize the primary preview.
271
+ 3. An old ledgered autosteer reply that does not consume the current pending ID
272
+ remains late and never quotes or records against the current primary.
273
+ 4. A true extra-turn autosteer with no live primary still quotes its own source.
274
+ 5. An `edit-fold` combined reply follows the same proven-fold rule.
275
+ 6. Ambiguous consumed pending IDs do not remap.
276
+ 7. A failed delivery neither spends the primary quote nor records the reply.
277
+ 8. An interim fold reply stays interim and still permits the existing produced-
278
+ final rescue.
279
+ 9. A non-interim fold reply followed by distinct Stop text stays
280
+ already-delivered and does not produce a duplicate fallback.
281
+
282
+ Verification gates:
283
+
284
+ - Focused Orchestra regression test shows red before and green after.
285
+ - All Orchestra unit/integration tests pass with no unexpected skips.
286
+ - Polygram's non-streamed rich tests pass against the new exact Orchestra pin.
287
+ - Full Polygram test suite passes with no unexpected skips.
288
+ - A controlled Shumorobot fold scenario produces one combined rich reply
289
+ quoting the primary and a `cli-fold-reply-attributed` event, with no
290
+ `stream-preview-turn-mismatch`, `cli-late-reply-correlated`, or
291
+ `cli-consumed-ack-fallback-rescued` for that turn.
292
+ - Deploy Shumabit only after the canary passes; verify unrelated UMI Assistant,
293
+ Water, and tmux owners are unchanged according to the normal deploy gate.
294
+
295
+ ## Soak and rollback
296
+
297
+ For 24 hours after Shumabit rollout, compare against the 14-day baseline:
298
+
299
+ - proven fold replies produce `cli-fold-reply-attributed`;
300
+ - late-autosteer events followed by zero-reply fallback rescue trend to zero;
301
+ - no rise in `channels-orphan-reply-dropped`, turn timeouts, or missing final
302
+ answers; and
303
+ - no duplicate-answer report in the affected chat.
304
+
305
+ Rollback is an exact Polygram/Orchestra version rollback through the normal
306
+ release/deploy procedure. There is no persistent state to unwind.
307
+
308
+ ## Success criteria
309
+
310
+ - The reported production shape yields one combined answer quoting the primary
311
+ message.
312
+ - A proven folded reply is owned consistently by dispatch, preview, quoting,
313
+ and pending-turn finalization.
314
+ - True late replies remain isolated from a newer pending turn.
315
+ - Genuine zero-reply and interim-only fallbacks retain their current rescue.
316
+ - Rich formatting remains available on both reply-tool and genuine
317
+ non-streamed paths.