@chen_hu_hu/pi-qq 1.7.0 → 1.7.1
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.
- package/CHANGELOG.md +18 -1
- package/doc/archive/delivery.1.7.1.md +215 -0
- package/doc/todo.md +31 -119
- package/index.ts +17 -15
- package/package.json +1 -1
- package/src/infra/file-logger.ts +73 -29
- package/src/qq/inbound-command.ts +15 -5
- package/src/qq/qq-bot.ts +3 -9
- package/src/qq/qq-message-handler.ts +3 -8
- package/src/qq/qr-login.ts +1 -1
- package/src/qq/reply-sender.ts +0 -1
- package/src/relay/bubble-writer.ts +41 -10
- package/src/relay/run-stream-relay.ts +42 -1
- package/src/relay/stream-session.ts +71 -14
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [1.7.1] - 2026-11-18
|
|
11
|
+
|
|
12
|
+
### Fixed
|
|
13
|
+
|
|
14
|
+
- Stream frames are now split at line boundaries below the QQ markdown shard limit, preventing `40034017` on large tool commands or text.
|
|
15
|
+
- An expired stream (`40034020`) now opens a fresh stream instead of resuming a dead `stream_msg_id`; if it still cannot continue, the run automatically rebinds to a recent inbound message when available.
|
|
16
|
+
- `/stop` now replies immediately after aborting the run instead of waiting for the tool/run to finish; `waitForEnd()` is only a background diagnostic.
|
|
17
|
+
- Debug logs now record extension runtime metadata instead of message text, tool arguments/results, assistant text, or SDK bodies; credentials in error text remain redacted.
|
|
18
|
+
- The message that starts a QQ run is now logged before the run becomes active, so trigger metadata is available for debugging.
|
|
19
|
+
- `append: while flushing` diagnostics are aggregated to the first and every 50th append per flush.
|
|
20
|
+
- Typing notifications are skipped while a stream is active to avoid competing with stream requests on slow networks.
|
|
21
|
+
|
|
22
|
+
### Changed
|
|
23
|
+
|
|
24
|
+
- Consolidated the 1.7.1 stream investigation, real-QQ test report, and task records into `doc/archive/delivery.1.7.1.md`; removed `doc/investigate.md`, `doc/task.md`, and `doc/test-report.md`.
|
|
25
|
+
|
|
10
26
|
## [1.7.0] - 2026-11-11
|
|
11
27
|
|
|
12
28
|
### Fixed
|
|
@@ -185,7 +201,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
185
201
|
- Added thinking labels, per-tool icons, model-error recovery, and idle-safe session handling.
|
|
186
202
|
- Reorganized modules by bounded context and updated settings and delivery documentation.
|
|
187
203
|
|
|
188
|
-
[Unreleased]: https://github.com/CQ-Developer/pi-qq/compare/v1.7.
|
|
204
|
+
[Unreleased]: https://github.com/CQ-Developer/pi-qq/compare/v1.7.1...HEAD
|
|
205
|
+
[1.7.1]: https://github.com/CQ-Developer/pi-qq/compare/v1.7.0...v1.7.1
|
|
189
206
|
[1.7.0]: https://github.com/CQ-Developer/pi-qq/compare/v1.6.2...v1.7.0
|
|
190
207
|
[1.6.2]: https://github.com/CQ-Developer/pi-qq/compare/v1.6.1...v1.6.2
|
|
191
208
|
[1.6.1]: https://github.com/CQ-Developer/pi-qq/compare/v1.6.0...v1.6.1
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
# pi-qq delivery 1.7.1
|
|
2
|
+
|
|
3
|
+
> **Delivery record.** 1.7.1 hardens the QQ stream relay against oversized and expired streams, makes `/stop` non-blocking, and reduces debug logs to runtime metadata. See [README](../../README.md) for current user-facing behavior.
|
|
4
|
+
> Version: 1.7.1
|
|
5
|
+
> Date: 2026-11-18
|
|
6
|
+
> Previous: [delivery.1.7.0.md](./delivery.1.7.0.md).
|
|
7
|
+
> Open work: [doc/todo.md](../todo.md).
|
|
8
|
+
|
|
9
|
+
## 1. Scope
|
|
10
|
+
|
|
11
|
+
1.7.0 introduced a monotonic `msg_seq` allocator. 1.7.1 addresses three production failures found in long QQ runs:
|
|
12
|
+
|
|
13
|
+
- `40034017`: a markdown frame exceeded the platform shard limit.
|
|
14
|
+
- `40034020`: a stream exceeded its total lifetime during a long tool; the relay kept resuming a dead `stream_msg_id` and lost tool output.
|
|
15
|
+
- `/stop`: a tool that ignores abort kept `/stop` waiting for the tool/run to end.
|
|
16
|
+
|
|
17
|
+
The release also:
|
|
18
|
+
|
|
19
|
+
- records extension runtime metadata instead of message text, tool arguments/results, assistant text, and SDK bodies;
|
|
20
|
+
- logs the inbound trigger before a run becomes active;
|
|
21
|
+
- aggregates `append: while flushing` diagnostics;
|
|
22
|
+
- skips typing while a stream is active.
|
|
23
|
+
|
|
24
|
+
Out of scope:
|
|
25
|
+
|
|
26
|
+
- a default watchdog or stream rotation;
|
|
27
|
+
- changing QQ protocol limits or the SDK;
|
|
28
|
+
- full ID masking (debug logs retain IDs for correlation; see section 7);
|
|
29
|
+
- protocol reductions for trailing newline frames and the first text frame (see sections 7 and 8).
|
|
30
|
+
|
|
31
|
+
## 2. Observed failures
|
|
32
|
+
|
|
33
|
+
The original 18 MB log (18,406 lines) is no longer retained; the key frames are summarized here.
|
|
34
|
+
|
|
35
|
+
### 2.1 Expired stream during a long tool
|
|
36
|
+
|
|
37
|
+
- 12:25:13: `seq=237`, first frame 442 chars, tool header `docker compose ... up -d --build`; the build ran for 23m28s.
|
|
38
|
+
- 12:48:40: the next frame returned `40034020` (`同一流式消息发送超过时间限制`).
|
|
39
|
+
- Two resumes with the same `stream_msg_id` failed; the writer went dead.
|
|
40
|
+
- 36 tools completed while the stream was dead; `/last` replayed only the most recent 3 finished tools, so 33 results never appeared.
|
|
41
|
+
|
|
42
|
+
### 2.2 Oversized frame
|
|
43
|
+
|
|
44
|
+
- 13:21:36: successful frame 3,706 chars / 3,743 B.
|
|
45
|
+
- 13:23:37: `seq=30` first frame 4,672 chars / 4,719 B returned `40034017` (`流式消息md分片需要\n结束`).
|
|
46
|
+
- Resumes grew the buffer to 5,044 / 5,145 B and 5,062 / 5,170 B, still `40034017`; the stream went dead.
|
|
47
|
+
- 9 read tools completed in the dead window; 3 were replayed by `/last`, 6 were not.
|
|
48
|
+
|
|
49
|
+
### 2.3 `/stop` blocking
|
|
50
|
+
|
|
51
|
+
- A 300 s tool that ignored abort kept `/stop` waiting for about 2m57s.
|
|
52
|
+
- An abortable 300 s tool returned `aborted` after about 61 s.
|
|
53
|
+
- The blocking path was the cause; the fixed behavior is described in section 5.
|
|
54
|
+
|
|
55
|
+
### 2.4 Integrity and event loop
|
|
56
|
+
|
|
57
|
+
- Run-end `currentTextLen` was 2,202 / 1,831, matching the stream-reconstructed text; no final answer was lost.
|
|
58
|
+
- Event-loop max delay was 618 ms; no seconds-level stall was involved.
|
|
59
|
+
- The log had no `40054005`; per-message `msg_seq` allocation was already effective.
|
|
60
|
+
|
|
61
|
+
## 3. Investigation conclusions
|
|
62
|
+
|
|
63
|
+
Hypotheses tested against the official pages and the real QQ experiments:
|
|
64
|
+
|
|
65
|
+
| # | Hypothesis | Evidence | Verdict |
|
|
66
|
+
| --- | ---------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- |
|
|
67
|
+
| H1 | The platform uses an idle timeout for one stream. | `idle 2` and `idle 5` pass, but `heartbeat 10/1` still fails at about 600 s. | Refuted as the primary limit; the 600 s total lifetime explains `idle 10`. |
|
|
68
|
+
| H2 | The platform uses a total lifetime limit. | `heartbeat 10/1` succeeds 9 times but still fails at about 600 s; `idle 10` fails at the same boundary. | Confirmed; about 600 s total. |
|
|
69
|
+
| H3 | Long silence lets the server clean up the `stream_msg_id`. | Resuming the same `stream_msg_id` fails after the limit; a fresh stream succeeds. | Consistent; recovery must open a new stream. |
|
|
70
|
+
| H4 | The inbound `msg_id` expires independently. | A new stream with the same `msg_id` works at 20 min; the original log created one after about 22 min. | Not a blocker within the tested window; 1 h not verified. |
|
|
71
|
+
| H5 | `40034017` is a single-frame shard limit. | Successful frames stayed below 3,743 B; failures started at 4,719 B; a 3,500 B split passes on real QQ. | Confirmed; 3,500 B is the chosen safe limit. |
|
|
72
|
+
|
|
73
|
+
Official documentation:
|
|
74
|
+
|
|
75
|
+
- The stream endpoint lists 50 QPS and only public error codes; it does not document `40034020`, `40034017`, `40034128`, heartbeats, or a 30-minute limit.
|
|
76
|
+
- `is_wakeup=true` skips `msg_id` / `event_id` validation, but it is a recall-message semantic, not a general long-task continuation mechanism.
|
|
77
|
+
- External material claiming a 30-minute limit was not used; it was unsupported or referred to another platform.
|
|
78
|
+
|
|
79
|
+
## 4. Final decisions
|
|
80
|
+
|
|
81
|
+
| # | Question | Decision |
|
|
82
|
+
| --- | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
83
|
+
| D1 | How are oversized frames handled? | Limit a frame to 3,500 B; split at the last `\n`; hard-cut a single long line and append `\n`. |
|
|
84
|
+
| D2 | What is the stream lifetime? | About 600 s total; heartbeats do not extend it; the boundary fluctuates by about 1 s. |
|
|
85
|
+
| D3 | What happens on `40034020`? | Open a fresh stream: new `msg_seq`, `index=0`, no old `stream_msg_id`; keep the unacknowledged buffer. |
|
|
86
|
+
| D4 | What is the recovery order? | Same `msg_id` fresh stream first; auto-rebind to the current target or a recent inbound target (within 5 minutes) as fallback (at most once per run); `/last` remains the manual fallback. |
|
|
87
|
+
| D5 | Should a watchdog be added? | Not by default. Five- and eight-minute rotations work; use eight minutes if a rotation option is added later. |
|
|
88
|
+
| D6 | How does `/stop` behave? | Abort, reply immediately, and keep `waitForEnd()` as background logging only. |
|
|
89
|
+
| D7 | What do debug logs contain? | Runtime metadata only: event, time, IDs, lengths, counts, durations, status, error codes, and stream indexes. No message text, tool arguments/results, assistant text, or SDK bodies. |
|
|
90
|
+
| D8 | Are IDs masked? | No. `messageId`, `senderId`, `openid`, and `appId` remain in debug logs for correlation; logs are local and must be treated as sensitive. Masking remains an open decision. |
|
|
91
|
+
| D9 | Should typing run during a stream? | No. Typing is skipped while a stream is active. |
|
|
92
|
+
| D10 | How are `while flushing` logs limited? | Log the first append and every 50th append per flush. |
|
|
93
|
+
| D11 | How long is `msg_id` valid? | A fresh stream with the same `msg_id` works 20 minutes after inbound; one hour is not verified. |
|
|
94
|
+
|
|
95
|
+
## 5. Implementation
|
|
96
|
+
|
|
97
|
+
| Area | Files | Change |
|
|
98
|
+
| -------------- | -------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
99
|
+
| Frame sizing | `src/relay/stream-session.ts` | `MAX_FRAME_BYTES = 3500`; UTF-8 byte-aware `takeFrame()`; newline-preferred split; hard cut with trailing `\n`; flush/done loops with 300 ms minimum spacing. |
|
|
100
|
+
| Expired stream | `src/relay/bubble-writer.ts` | Treat `40034020` and `40054005` as non-resumable; open a fresh stream with a new `msg_seq` and `index=0`; replay the unacknowledged buffer; expose `hasActiveStream`. |
|
|
101
|
+
| Auto-rebind | `src/relay/run-stream-relay.ts` | After a `40034020`, rebind to the current target or a recent inbound target (within 5 minutes), replay thinking, the last three finished tools plus any active tool (five tools at most), and the current text; at most one automatic rebind per run. |
|
|
102
|
+
| `/stop` | `src/qq/inbound-command.ts` | Call `ctx.abort()` and reply immediately; `waitForEnd()` only writes background diagnostics. |
|
|
103
|
+
| Debug logging | `src/infra/file-logger.ts`, `index.ts`, `src/qq/qq-bot.ts`, `src/qq/qq-message-handler.ts`, `src/qq/reply-sender.ts` | Scope-aware log gate; inbound allowed after login; SDK bodies reduced to `<N chars>`; `logValueLength()` for arguments/results; metadata fields on inbound, tool, and assistant events. |
|
|
104
|
+
| Typing | `src/relay/run-stream-relay.ts` | Skip typing while `BubbleWriter.hasActiveStream` is true. |
|
|
105
|
+
| `msg_seq` | `src/relay/msg-seq.ts` | Monotonic per-`msgId` allocator introduced in 1.7.0; still used for streams, passive text, media, and diagnostics. |
|
|
106
|
+
|
|
107
|
+
Behavioral notes:
|
|
108
|
+
|
|
109
|
+
- A new stream keeps the old `msg_id` and `event_id`; only `stream_msg_id`, `msg_seq`, and `index` restart.
|
|
110
|
+
- `40034020` after a long idle is recovered by the same path; `is_wakeup=true` and a fresh inbound target also work.
|
|
111
|
+
- Credential redaction remains a fallback for error text and a small set of fields; the primary guarantee is that message content is not logged.
|
|
112
|
+
- The relay still rotates bubbles at tool and thinking boundaries; this is unchanged from 1.7.0.
|
|
113
|
+
|
|
114
|
+
## 6. Verification
|
|
115
|
+
|
|
116
|
+
### 6.1 Automated
|
|
117
|
+
|
|
118
|
+
- 24 test files, **251 tests passed**.
|
|
119
|
+
- `npm run lint`, `npm run typecheck`, `npm run format:check`, and `npm pack --dry-run` pass.
|
|
120
|
+
- Coverage includes oversized and no-newline frames, `40034020` fresh-stream replay, auto-rebind, `/stop` with a run that never ends, log gating, SDK body summarization, and tool logs without arguments/results.
|
|
121
|
+
|
|
122
|
+
### 6.2 Real QQ stream experiments
|
|
123
|
+
|
|
124
|
+
Environment: Linux; pi 1.5.2; Node.js 24.22.0; single C2C user; local pi-qq 1.7.0 plus these fixes. The temporary experiment extension, scripts, and logs were removed after testing.
|
|
125
|
+
|
|
126
|
+
All times below are UTC+8. The experiment hook is not part of the release.
|
|
127
|
+
|
|
128
|
+
| Case | Key duration | Result |
|
|
129
|
+
| --------------------- | ------------------- | -------------------------------------------------------------------------------------- |
|
|
130
|
+
| `idle 2` | 120.0 s idle | Probe and DONE succeed |
|
|
131
|
+
| `idle 5` | 300.0 s idle | Probe and DONE succeed |
|
|
132
|
+
| `idle 10` | 600.0 s idle | Probe returns `40034020` |
|
|
133
|
+
| `heartbeat 10/1` | 10th at about 600 s | First 9 succeed; 10th returns `40034020`; heartbeats do not extend the lifetime |
|
|
134
|
+
| `rotate 5` | 300 s | Old stream DONE succeeds; same `msg_id` new stream succeeds (`msg_seq=2`) |
|
|
135
|
+
| `rotate 8` | 480 s | Same as `rotate 5`; about 2 minutes below the limit |
|
|
136
|
+
| `expire 10` | 600 s | Old stream `40034020`; same `msg_id` new stream succeeds; 604.5 s total |
|
|
137
|
+
| `expire 20` | 1,200 s | Old stream `40034020`; same `msg_id` new stream succeeds; 1,203.5 s total |
|
|
138
|
+
| `expire-full 10` | 600 / 601 s | Same `msg_id`, `is_wakeup=true`, and fresh inbound target all succeed; 605.6 s total |
|
|
139
|
+
| `large 5` | 5,568 chars | Split 3,371 + 2,197 chars; both 200; 2.8 s; no `40034017` |
|
|
140
|
+
| `large-line 5` | 5,121 B, no breaks | Hard split 3,500 + 1,622 B; both 200; 2.6 s; no `40034017` |
|
|
141
|
+
| `relay 10.5` | 10.5 min | Old stream `40034020`; new stream and DONE succeed; `streamTookOver=true` |
|
|
142
|
+
| `budget 6` / `10` | 6.7 / 10.6 s | 6 / 10 plain texts all succeed; no `40034128` |
|
|
143
|
+
| `LONG_TOOL_LONG_TEST` | 720 s | Old stream `40034020`; fresh stream delivers tool result and final text; 725.5 s total |
|
|
144
|
+
|
|
145
|
+
Key conclusions:
|
|
146
|
+
|
|
147
|
+
- The stream lifetime is about 600 s from creation, not an idle window; the boundary jitters by about 1 s.
|
|
148
|
+
- A fresh stream with the same `msg_id` works after 10 and 20 minutes; `is_wakeup=true` and a fresh inbound target also work.
|
|
149
|
+
- The fresh-stream `40034020` path was verified through the real `RunFeedback` / `BubbleWriter` path and a 12-minute agent tool.
|
|
150
|
+
- Frame splitting works for both newline-containing and single-line oversized content.
|
|
151
|
+
|
|
152
|
+
### 6.3 `/stop` and steering
|
|
153
|
+
|
|
154
|
+
- A 300 s tool that ignores abort: `/stop` waited about 2m57s before the fix; after the fix the reply was sent in about 1 ms while the tool kept running.
|
|
155
|
+
- An abortable 300 s tool: returned `aborted` after about 61 s.
|
|
156
|
+
- Steering is delivered after the tool batch ends, not during execution.
|
|
157
|
+
- `ctx.abort()` causes pi to restore queued steering messages to the editor; this is TUI behavior, not lost input.
|
|
158
|
+
|
|
159
|
+
### 6.4 Logging verification
|
|
160
|
+
|
|
161
|
+
Before the fix (v1.7.0, 2026-11-17 real usage log): 906 KiB in 15 minutes, 4 runs, 24 tools, 175 stream frames. The log contained 37,309 chars of streamed content (thinking 27,154, tool markdown 9,294, final answers 861), full tool arguments (6.6K chars) and results (30K chars, capped at 2,000 per entry), assistant `textPreview`, and full SDK bodies. It had 1,478 `append: while flushing` lines / 190 KiB and no inbound/outbound records.
|
|
162
|
+
|
|
163
|
+
After the fix (main build, 2026-11-18): 1 run, 4.3 s, 1 `bash` call, 22-char inbound, 62-char final answer. The log is 80 lines / 21.5 KiB; all 7 requests returned 200.
|
|
164
|
+
|
|
165
|
+
Checks that passed:
|
|
166
|
+
|
|
167
|
+
- SDK bodies are only `Body: <N chars>`; no `content_raw`, `content`, `text`, or request-body JSON.
|
|
168
|
+
- `tool start` has only `argsLength` (20); `tool end` has only `resultLength` (417); `tool update` has `partialLength` (14); no arguments or results.
|
|
169
|
+
- Assistant events have `textLength` (0 / 62); no `textPreview`.
|
|
170
|
+
- `inbound` has `contentLength`, `attachmentCount`, and `msgType`; `sent to pi` has `textLength`; neither contains the message body.
|
|
171
|
+
- No `>>> Body: {`, `"args"`, `"result"`, `"firstPartial"`, `"partial"`, or `"textPreview"` appears in the log.
|
|
172
|
+
|
|
173
|
+
Limits of this verification:
|
|
174
|
+
|
|
175
|
+
- `append: while flushing` was not exercised by the short run; `test/relay/stream-session.test.ts` covers the aggregation.
|
|
176
|
+
- Full message and user IDs are still logged (see section 7).
|
|
177
|
+
|
|
178
|
+
## 7. Risks and limitations
|
|
179
|
+
|
|
180
|
+
| Risk | Impact | Handling |
|
|
181
|
+
| ----------------------------------- | -------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
|
|
182
|
+
| 600 s boundary jitter | A frame sent at the boundary can still fail once before recovery | Do not schedule rotation at the limit; leave margin; the fresh-stream path recovers the next frame |
|
|
183
|
+
| `msg_id` validity beyond 20 minutes | One-hour-old inbound messages are unverified | Long-term observation |
|
|
184
|
+
| Platform variability | Lifetime, error mapping, and rendering may differ by account, region, or client | Keep recovery defensive; collect real logs |
|
|
185
|
+
| Typing timeout | A 10 s typing timeout was seen in earlier logs but not reproduced | Typing is skipped while a stream is active; the timeout path remains unverified |
|
|
186
|
+
| Full IDs in debug logs | `messageId`, `senderId`, `openid`, and `appId` remain in local logs | Logs are debug-only and local; treat as sensitive; masking is an open decision |
|
|
187
|
+
| Log volume | `run` diagnostics repeat the tool list on every event; stream timing records are verbose | Metadata only; no content. Compaction is an open optimization |
|
|
188
|
+
| Trailing newline frames | A lone `\n` can be sent before a bubble's DONE, adding a request and latency | Known; needs a protocol test before merging the newline into DONE |
|
|
189
|
+
| First text frame | A new text bubble can show 1–4 characters before the rest arrives | Known; needs a flush policy for text deltas while keeping tool headers immediate |
|
|
190
|
+
| `run ended` ordering | The run-end log precedes the final `flush` / `done` | Known; does not affect delivery, but the log cannot prove the last frame succeeded |
|
|
191
|
+
| `lastFlushAt = 0` | The first `append: immediate` in a new stream logs an epoch-sized `elapsed` value | Log-only noise |
|
|
192
|
+
| Passive reply budget | The platform accepted 10 probes, but the local limit remains 4; each media send consumes one reply | Keep the conservative local limit; stream bubbles are not counted the same way |
|
|
193
|
+
| Auto-rebind limit | At most one automatic rebind per run | `/last` remains the manual fallback |
|
|
194
|
+
| Watchdog not implemented | A long stream can still hit `40034020` once before recovery | The recovery path is verified; an optional eight-minute rotation can be added later |
|
|
195
|
+
| Real QQ coverage | One account, one region, one network, single-user C2C | Results are strong for this setup, not a platform guarantee |
|
|
196
|
+
|
|
197
|
+
## 8. Open items
|
|
198
|
+
|
|
199
|
+
Active work is tracked in [doc/todo.md](../todo.md):
|
|
200
|
+
|
|
201
|
+
- confirm how the QQ client renders long `bash` tool start lines;
|
|
202
|
+
- observe stream lifetime and `msg_id` validity beyond 20 minutes and one hour;
|
|
203
|
+
- reproduce the typing timeout and decide whether client isolation is needed;
|
|
204
|
+
- decide whether to mask IDs in debug logs;
|
|
205
|
+
- compact `run` diagnostics and stream timing logs;
|
|
206
|
+
- evaluate merging the trailing `\n` into the DONE frame and throttling the first text delta.
|
|
207
|
+
|
|
208
|
+
## 9. References
|
|
209
|
+
|
|
210
|
+
- Stream endpoint: `https://bot.q.qq.com/wiki/develop/api-v2/autogen/api/v2_users_user_openid_stream_messages.post.html`
|
|
211
|
+
- Error codes: `https://bot.q.qq.com/wiki/develop/api-v2/openapi/error/error.html`
|
|
212
|
+
- Code: `src/relay/stream-session.ts`, `src/relay/bubble-writer.ts`, `src/relay/run-stream-relay.ts`, `src/relay/msg-seq.ts`, `src/qq/inbound-command.ts`, `src/infra/file-logger.ts`.
|
|
213
|
+
- Tests: `test/relay/stream-session.test.ts`, `test/relay/bubble-writer.test.ts`, `test/relay/run-stream-relay.test.ts`, `test/qq/qq-message-handler.test.ts`, `test/infra/file-logger.test.ts`, `test/index.test.ts`.
|
|
214
|
+
- Previous delivery: [delivery.1.7.0.md](./delivery.1.7.0.md).
|
|
215
|
+
- User-facing behavior: [README.md](../../README.md), [README-zh.md](../../README-zh.md).
|
package/doc/todo.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# TODO
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> 只记录未完成项目;已完成项目直接删除,不使用勾选状态。
|
|
4
|
+
> 1.7.1 交付记录与验证:`doc/archive/delivery.1.7.1.md`
|
|
4
5
|
|
|
5
6
|
## 1. 长运行 bash 工具的参数延迟显示
|
|
6
7
|
|
|
@@ -74,6 +75,7 @@
|
|
|
74
75
|
|
|
75
76
|
- QQ 客户端如何渲染这个 fenced code block。日志只能证明请求体里有完整命令,不能证明手机 QQ 一定把代码块展开显示。
|
|
76
77
|
- 如果手机 QQ 仍只显示“bash 和一个点”,优先检查 QQ 对带 `bash` 语言标记的 fenced block、长代码块或新气泡的折叠/渲染,而不是继续排查 relay。
|
|
78
|
+
- 2026-11-18 main 构建:`tool start` 后 1.1–3.2s(中位数约 1.4s)工具行才发出;`waitedMs` 多为 0,延迟来自旧气泡 DONE + 新流 RTT,不是队列积压。仍需确认 QQ 客户端是否把长命令折叠成“bash 和一个点”。
|
|
77
79
|
|
|
78
80
|
### 下一步
|
|
79
81
|
|
|
@@ -82,131 +84,41 @@
|
|
|
82
84
|
- 如果确认是 QQ 渲染问题,再考虑缩短命令摘要或改用普通文本/行内代码;如果确认正常,可直接关闭 TODO 1。
|
|
83
85
|
- 若最终需要改格式,补测试并更新 `CHANGELOG.md`,再发布版本。
|
|
84
86
|
|
|
85
|
-
## 2.
|
|
87
|
+
## 2. 流式消息生命周期的长期观察
|
|
86
88
|
|
|
87
|
-
>
|
|
88
|
-
> 记录日期:2026-11-04
|
|
89
|
-
> 分析日期:2026-11-06
|
|
90
|
-
> 相关版本:pi-qq 1.4.0
|
|
91
|
-
> 详细调研:`doc/archive/delivery.1.4.0.md`
|
|
89
|
+
> 状态:持续观察;仅保留尚未验证的长期风险
|
|
92
90
|
|
|
93
|
-
###
|
|
91
|
+
### 背景
|
|
94
92
|
|
|
95
|
-
|
|
93
|
+
2026-11-12 的真实 QQ 实验确认:
|
|
96
94
|
|
|
97
|
-
-
|
|
98
|
-
-
|
|
99
|
-
-
|
|
100
|
-
-
|
|
95
|
+
- 流的总生命周期约 600 秒,边界有约 1 秒抖动;
|
|
96
|
+
- 心跳不能延长生命周期;
|
|
97
|
+
- 过期后同 `msg_id` 新流、`is_wakeup=true`、新入站目标都能恢复;
|
|
98
|
+
- 12 分钟真实 agent 工具已验证自动新开流。
|
|
101
99
|
|
|
102
|
-
###
|
|
100
|
+
### 进度
|
|
103
101
|
|
|
104
|
-
-
|
|
105
|
-
-
|
|
106
|
-
-
|
|
107
|
-
- 长静默后的结果帧如果因 `msg_id` 过期或被动次数超限失败,`BubbleWriter` 会沿用同一个过期 `msg_id` / `stream_msg_id` resume,最终 `killStream()`;之后 `onToolStart()` / `onToolEnd()` 直接早退,后续工具输出丢失。
|
|
108
|
-
- 运行中收到的普通消息以 `steer` 进入当前 run,要等当前工具批次结束才处理。
|
|
109
|
-
- `/stop` 目前要等 `waitForEnd()` 才回复;如果工具不响应 abort 或流式请求在重试,会占住 SDK 的并发锁。
|
|
110
|
-
|
|
111
|
-
### 临时恢复手段
|
|
112
|
-
|
|
113
|
-
QQ 侧的 `/last` 已可用于手动恢复:
|
|
114
|
-
|
|
115
|
-
- **run 运行中**:`/last` 调用 `RunFeedback.repushCurrent()`,把当前输出重推到新消息的 `msg_id` 上,并让后续 delta 继续写入新气泡;
|
|
116
|
-
- **run 已结束**:`/last` 用流式重新推送最近一次完成的 assistant 文本。
|
|
117
|
-
|
|
118
|
-
因此本次不实现行为修复;`/last` 作为用户可用的手动恢复路径。
|
|
119
|
-
|
|
120
|
-
### 本次决策
|
|
121
|
-
|
|
122
|
-
- 暂不实施 v4 归档中的错误分类、预算、心跳、自动重绑等行为修改。
|
|
123
|
-
- 只追加可验证的 debug 日志,等待长期使用日志后再决定修复范围。
|
|
124
|
-
- 2026-11-06 首轮日志未复现失败路径,继续维持“只加日志、不改行为”的决定;详见“日志验证结果”。
|
|
125
|
-
- 新增诊断日志在 `logLevel: "debug"` 时写入;`stream error` / `stream unavailable` 原本就是 `warn`,保留为错误记录,不改变行为。
|
|
126
|
-
|
|
127
|
-
### 已追加的诊断日志
|
|
128
|
-
|
|
129
|
-
- `scope: "tool"`:
|
|
130
|
-
- `start` / `end` 增加 `parentToolCallId`、`durationMs`;
|
|
131
|
-
- `end` 增加 `updateCount`、`updateSpanMs`、`firstPartial`;
|
|
132
|
-
- `update` 记录首次部分输出,之后每 50 次记录一次进度。
|
|
133
|
-
- `scope: "run"`:
|
|
134
|
-
- `run started`:`phase: "no qq target"`、`source: pending/recovery`、`targetAgeMs`;
|
|
135
|
-
- `tool start` / `tool end`:`phase: queued / output started / result recorded / output finished / ignored: stream dead / ignored: no active run`;
|
|
136
|
-
- `stream takeover decision`:`textLength`、`deltaLength`、`hadPendingTools`、`flushOk`、`hasPushed`、`streamTookOver`、`deferredTextLen`;
|
|
137
|
-
- `run ended`:`hasPendingTools`、`hasPushed`、`lastCompletedTextLength`;
|
|
138
|
-
- `stream timing`:`phase: last repush / last repush ignored`;
|
|
139
|
-
- 工具、最终消息和 cleanup 相关日志都带 `diagnostics`:`activeTool`、`tools`、`deferredTextLen`、`writer stats`、`budget`。
|
|
140
|
-
- `scope: "stream"`:
|
|
141
|
-
- `stream timing` 的 `stats` 包含 `dead`、`hasStream`、`hasPendingResume`、`resumeAttempts`、`bubbleChars`、`hasPushed`、`lastKind`、`toolOpen`、`lastFrameAgeMs`;
|
|
142
|
-
- `stream open` 记录是否 resume、checkpoint 的 `index` / `hasStreamMsgId` / `bufferLength`、`budget`;
|
|
143
|
-
- `stream error` 增加 `phase: "frame failed"`,记录 `httpStatus`、`bizCode`、`path`、`msgSeq`、`streamMsgId`、`bufferLength`;
|
|
144
|
-
- `stream unavailable` 记录 writer `stats` 和 `budget`;
|
|
145
|
-
- `sendStreamReply()`(idle `/last`)记录 `phase: "last reply stream"`、`msgId`、`textLength`。
|
|
146
|
-
- `AppendStream` 的 `flush start/done` 增加 `msgSeq` 和 `streamMsgId`(存在时)。
|
|
147
|
-
- `scope: "inbound"`:`/stop` 记录 `wait start` / `wait done` 及 `waitedMs`。
|
|
148
|
-
- `scope: "event-loop"`:增加 `driftMs`,用于发现 timer 被事件循环延迟。
|
|
102
|
+
- 20 分钟同 `msg_id` 新流:已验证(`expire 20`,1203.5s);
|
|
103
|
+
- 超过 20 分钟的同 `msg_id` 新流:未验证;
|
|
104
|
+
- 1 小时以上 `msg_id`:未验证。
|
|
149
105
|
|
|
150
|
-
###
|
|
106
|
+
### 需要观察
|
|
151
107
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
- 最长的单条命令是 94.2 秒的 docker build;其余 e2e jest 命令为 4.2–6.8 秒。日志中没有“数分钟到数十分钟”的单条 bash。
|
|
157
|
-
- 94s 工具期间从启动帧到结果帧有 93.7 秒没有新流式帧,说明长工具确实没有进度帧;但结果帧在工具结束后 0.6 秒发出并收到 200,没有丢失。
|
|
158
|
-
- `stream error` / `stream unavailable` / `phase: "frame failed"`:0 条。
|
|
159
|
-
- TODO 点名的错误码 `304103`、`40034005`、`40034128`、`40034024`、`429`、`50002`:0 次。
|
|
160
|
-
- 529 个工具全部有 `queued → output started → result recorded → output finished`,没有 `ignored: stream dead`。
|
|
161
|
-
- 4 个 run 结束时均为 `dead=false`、`hasPendingResume=false`、`resumeAttempts=0`、`hasPendingTools=false`、`hadError=false`;2055 组 `AppendStream` 的 `flush start/done` 一一对应。
|
|
162
|
-
- 2026-11-05 有 4 个 QQ run:约 8 / 21 / 29 / 23 分钟;29 分钟的 run 结束时 `streamBubbles=184`、`streamBubblesTotal=502`,但 `budget.remaining=4`。这验证了“流式气泡没有计入 4 次被动回复上限”这个前提,但没有观察到因此失败;同一 `msg_id` 在数十分钟内仍能继续发流式帧。
|
|
163
|
-
- `/last` 有 3 次有效调用,全部在 run 已结束后:分别重发 1410、2320、1827 字符,均收到 200 + done。没有 `last repush`(运行中恢复)记录。日志中没有运行中收到的普通消息,也没有 `/stop`,所以 steer 阻塞和非阻塞 `/stop` 仍未验证。
|
|
164
|
-
- 2026-11-06 的 `event-loop maxMs=41,266,045.8` 样本与机器休眠/恢复一致,不是事件循环被阻塞 11 小时:它出现在 23:32:06.970,与 WebSocket 1006 关闭/重连同一时刻,下一次采样已回到 259ms。
|
|
165
|
-
|
|
166
|
-
**结论:** 这些日志不能验证“长命令后 QQ 无输出/失去响应”的失败路径,也不能据此实施错误分类、预算或自动重绑修复。它们只支持两个前提:长工具期间没有进度帧;流式气泡没有计入被动回复预算。真正的触发条件仍需一次单条命令运行数分钟(最好 >5 分钟,甚至 >1 小时)的日志。
|
|
167
|
-
|
|
168
|
-
### 复现时仍需采集/分析
|
|
169
|
-
|
|
170
|
-
本次日志没有出现这些错误,以下步骤保留给下次复现时使用。
|
|
171
|
-
|
|
172
|
-
1. 找到长命令的 `scope: "tool"` `start` / `end`:
|
|
173
|
-
- `durationMs` 是否与用户感知一致;
|
|
174
|
-
- `updateCount` 是否为 0,判断长命令期间是否完全没有部分输出。
|
|
175
|
-
2. 看 `scope: "stream"` 的 `stream timing`:
|
|
176
|
-
- `tool queued` 时间;
|
|
177
|
-
- `append start` 的 `waitedMs` / `lastFrameAgeMs`;
|
|
178
|
-
- `append done` 的 `totalMs`;
|
|
179
|
-
- 工具结束到结果帧之间隔了多久。
|
|
180
|
-
3. 看 `stream error` 的 `phase: "frame failed"`:
|
|
181
|
-
- `httpStatus` / `bizCode` 是否为 `304103`、`40034005`、`40034128`、`40034024`、`429`、`50002`;
|
|
182
|
-
- `streamMsgId` 是否存在,`bufferLength` 是否持续增长。
|
|
183
|
-
4. 看 `stream unavailable` 之后 `scope: "run"` 的:
|
|
184
|
-
- `tool start/end` 是否出现 `ignored: stream dead`;
|
|
185
|
-
- `stream takeover decision` 的 `flushOk` / `hasPushed` / `hadPendingTools`;
|
|
186
|
-
- `run ended` 的 `hasPendingTools`。
|
|
187
|
-
5. 如果用户使用了 `/last`,看 `phase: "last repush"` 或 `"last repush ignored"`,确认是否恢复、恢复后是否仍有输出。
|
|
188
|
-
6. 看 `scope: "event-loop"` 的 `maxMs` / `p99Ms` / `driftMs`:
|
|
189
|
-
- 如果达到数秒甚至更高,优先排查 CPU 饥饿;
|
|
190
|
-
- 如果正常,则更可能是 QQ 被动窗口/配额或 relay 状态问题。
|
|
191
|
-
7. 对照 `/qq-test-budget` 的 `remaining`、`streamBubbles`、`streamBubblesTotal`,确认是否早已超过 4 次。
|
|
192
|
-
|
|
193
|
-
### 需要提供的信息
|
|
194
|
-
|
|
195
|
-
本次日志没有覆盖多分钟单命令、运行中收到的普通消息、`/stop` 和 `/qq-test-budget` 输出;复现时请按以下清单提供:
|
|
196
|
-
|
|
197
|
-
- `<cwd>/.pi/pi-qq/logs/pi-qq-YYYY-MM-DD.log`;
|
|
198
|
-
- 长命令内容、开始时间和结束时间;
|
|
199
|
-
- QQ 上最后一条可见消息及时间;
|
|
200
|
-
- 期间是否发送过 `/status`、`/stop`、普通文本、`/last`;
|
|
201
|
-
- `/qq-test-budget` 和 `/qq-status` 输出;
|
|
202
|
-
- 手机 QQ 是否在前台,是否允许主动消息。
|
|
108
|
+
- 平台生命周期是否随账号、地区、客户端版本变化;
|
|
109
|
+
- 真实网络抖动、重连、限流下的表现;
|
|
110
|
+
- 超过 20 分钟的同 `msg_id` 新流是否仍可用;
|
|
111
|
+
- 真实使用中 `40034020` 的恢复是否仍能稳定送达结果。
|
|
203
112
|
|
|
204
|
-
|
|
113
|
+
实验记录见 `doc/archive/delivery.1.7.1.md`。
|
|
114
|
+
|
|
115
|
+
## 3. 调试日志与流帧的剩余优化
|
|
116
|
+
|
|
117
|
+
> 状态:不阻塞 1.7.1;等真实使用数据再评估
|
|
118
|
+
> 记录日期:2026-11-18
|
|
205
119
|
|
|
206
|
-
-
|
|
207
|
-
-
|
|
208
|
-
-
|
|
209
|
-
-
|
|
210
|
-
-
|
|
211
|
-
- `/last` 目前作为手动兜底可用,保持现状,不急于自动重绑。
|
|
212
|
-
- 修复后补测试,更新 `doc/todo.md` / `CHANGELOG.md`,再发布版本。
|
|
120
|
+
- typing 真实网络超时:1.7.1 已在流活跃时跳过 typing,但 10s 超时尚未在真实网络复现。
|
|
121
|
+
- 日志体积:`run` 诊断在每个工具事件重复完整 `tools` 列表;1.7.1 的单工具 run 中占 7 KiB / 21.5 KiB。
|
|
122
|
+
- 完整 ID 未掩码:`messageId` / `senderId` / `openid` / `appId` 仍在 debug 日志中;需要决定掩码或保留。
|
|
123
|
+
- 流帧开销:气泡结束前补的 `\n` 可能单独成帧;最终回答首帧可能只有 1–4 个字符;`run ended` 日志早于最终 DONE。
|
|
124
|
+
- 这些项不影响 1.7.1 的正确性;改动前优先用真实 QQ 日志确认收益。
|
package/index.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { registerQQDiagnostics } from "./src/qq/qq-diagnostics";
|
|
|
9
9
|
import { markdownToPlain, sendReply } from "./src/qq/reply-sender";
|
|
10
10
|
import { ReplyBudget } from "./src/quota/reply-quota";
|
|
11
11
|
import { RunFeedback } from "./src/relay/run-stream-relay";
|
|
12
|
-
import { configureLogger, flushLogs, isLogEnabled, log,
|
|
12
|
+
import { configureLogger, flushLogs, isLogEnabled, log, logValueLength, setLogGate } from "./src/infra/file-logger";
|
|
13
13
|
import { configureI18n, t } from "./src/infra/i18n";
|
|
14
14
|
import { getPowerStatus } from "./src/infra/power-status";
|
|
15
15
|
import { formatStatusReport } from "./src/qq/status-report";
|
|
@@ -56,7 +56,6 @@ interface ToolUpdateStats {
|
|
|
56
56
|
count: number;
|
|
57
57
|
firstAt: number;
|
|
58
58
|
lastAt: number;
|
|
59
|
-
firstPartial: string;
|
|
60
59
|
}
|
|
61
60
|
|
|
62
61
|
const toolUpdateStats = new Map<string, ToolUpdateStats>();
|
|
@@ -98,7 +97,14 @@ function stopEventLoopMonitor(): void {
|
|
|
98
97
|
}
|
|
99
98
|
|
|
100
99
|
export default function (pi: ExtensionAPI) {
|
|
101
|
-
setLogGate(() =>
|
|
100
|
+
setLogGate((scope) => {
|
|
101
|
+
if (scope === "login") return true;
|
|
102
|
+
if (!getLoginState()) return false;
|
|
103
|
+
// The message that starts a run arrives before the run becomes active.
|
|
104
|
+
if (scope === "inbound") return true;
|
|
105
|
+
if (scope === "session" || scope === "bot") return true;
|
|
106
|
+
return runFeedback.isQQRunActive();
|
|
107
|
+
});
|
|
102
108
|
|
|
103
109
|
const notifyConfigError = (ctx: ExtensionContext, error: string | undefined, dedupe: boolean) => {
|
|
104
110
|
if (!error) {
|
|
@@ -282,7 +288,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
282
288
|
toolCallId: event.toolCallId,
|
|
283
289
|
parentToolCallId: event.parentToolCallId ?? null,
|
|
284
290
|
at: Date.now(),
|
|
285
|
-
|
|
291
|
+
argsLength: logValueLength(event.args),
|
|
286
292
|
});
|
|
287
293
|
}
|
|
288
294
|
void runFeedback.onToolStart(event.toolName, event.args, event.toolCallId);
|
|
@@ -297,7 +303,6 @@ export default function (pi: ExtensionAPI) {
|
|
|
297
303
|
count: 1,
|
|
298
304
|
firstAt: now,
|
|
299
305
|
lastAt: now,
|
|
300
|
-
firstPartial: previewForLog(event.partialResult, 300),
|
|
301
306
|
};
|
|
302
307
|
toolUpdateStats.set(event.toolCallId, first);
|
|
303
308
|
log.debug("tool", "update", {
|
|
@@ -305,7 +310,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
305
310
|
toolName: event.toolName,
|
|
306
311
|
toolCallId: event.toolCallId,
|
|
307
312
|
count: 1,
|
|
308
|
-
|
|
313
|
+
partialLength: logValueLength(event.partialResult),
|
|
309
314
|
});
|
|
310
315
|
return;
|
|
311
316
|
}
|
|
@@ -334,8 +339,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
334
339
|
durationMs: event.durationMs ?? null,
|
|
335
340
|
updateCount: updates?.count ?? 0,
|
|
336
341
|
updateSpanMs: updates ? updates.lastAt - updates.firstAt : null,
|
|
337
|
-
|
|
338
|
-
result: previewForLog(event.result),
|
|
342
|
+
resultLength: logValueLength(event.result),
|
|
339
343
|
});
|
|
340
344
|
}
|
|
341
345
|
runFeedback.onToolEnd(event.toolName, event.isError, event.result, event.toolCallId);
|
|
@@ -367,12 +371,10 @@ export default function (pi: ExtensionAPI) {
|
|
|
367
371
|
hasLogin: Boolean(getLoginState()),
|
|
368
372
|
stopReason: msg.stopReason,
|
|
369
373
|
contentTypes: msg.content.map((block) => block.type),
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
.join("\n"),
|
|
375
|
-
),
|
|
374
|
+
textLength: msg.content
|
|
375
|
+
.filter((block) => block.type === "text")
|
|
376
|
+
.map((block) => block.text)
|
|
377
|
+
.join("\n").length,
|
|
376
378
|
});
|
|
377
379
|
}
|
|
378
380
|
if (!running || !fromQQ) return;
|
|
@@ -397,7 +399,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
397
399
|
|
|
398
400
|
const streamTookOver = await runFeedback.onAssistantFinal(text, isError);
|
|
399
401
|
if (isLogEnabled("debug")) {
|
|
400
|
-
log.debug("pi", "stream takeover decision", { streamTookOver, isError,
|
|
402
|
+
log.debug("pi", "stream takeover decision", { streamTookOver, isError, textLength: text.length });
|
|
401
403
|
}
|
|
402
404
|
if (streamTookOver) return;
|
|
403
405
|
|
package/package.json
CHANGED
package/src/infra/file-logger.ts
CHANGED
|
@@ -18,7 +18,7 @@ const DEFAULT_LOG_LEVEL: LogLevel = "silent";
|
|
|
18
18
|
let activeLevel: LogLevel = DEFAULT_LOG_LEVEL;
|
|
19
19
|
let activeDir: string | undefined;
|
|
20
20
|
let writeQueue: Promise<void> = Promise.resolve();
|
|
21
|
-
let logGate: (() => boolean) | undefined;
|
|
21
|
+
let logGate: ((scope: string) => boolean) | undefined;
|
|
22
22
|
|
|
23
23
|
function isLogLevel(value: unknown): value is LogLevel {
|
|
24
24
|
return typeof value === "string" && Object.hasOwn(LEVEL_WEIGHT, value);
|
|
@@ -45,19 +45,61 @@ export function configureLogger(level: LogLevel, dir?: string): void {
|
|
|
45
45
|
activeDir = dir;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
export function setLogGate(gate: (() => boolean) | undefined): void {
|
|
48
|
+
export function setLogGate(gate: ((scope: string) => boolean) | undefined): void {
|
|
49
49
|
logGate = gate;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
function isLogAllowed(): boolean {
|
|
52
|
+
function isLogAllowed(scope: string): boolean {
|
|
53
53
|
if (!logGate) return true;
|
|
54
54
|
try {
|
|
55
|
-
return logGate();
|
|
55
|
+
return logGate(scope);
|
|
56
56
|
} catch {
|
|
57
57
|
return false;
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
const REDACTED = "<redacted>";
|
|
62
|
+
const SECRET_KEY_RE =
|
|
63
|
+
/^(?:password|passwd|pwd|secret|token|session|sessionId|cookie|authorization|api[_-]?key|client[_-]?secret|app[_-]?secret|access[_-]?token|refresh[_-]?token|private[_-]?key)$/i;
|
|
64
|
+
|
|
65
|
+
/** Redact common credential shapes while keeping the surrounding log text. */
|
|
66
|
+
export function redactLogText(text: string): string {
|
|
67
|
+
return text
|
|
68
|
+
.replace(/(\bAuthorization\s*:\s*(?:Bearer\s+)?)([^\s,;]+)/gi, `$1${REDACTED}`)
|
|
69
|
+
.replace(/(\bCookie\s*:\s*)([^\r\n]+)/gi, `$1${REDACTED}`)
|
|
70
|
+
.replace(
|
|
71
|
+
/(\b["']?(?:password|passwd|pwd|secret|token|session|sessionId|api[_-]?key|client[_-]?secret|app[_-]?secret|access[_-]?token|refresh[_-]?token|private[_-]?key)["']?\s*[:=]\s*)(?:"[^"]*"|'[^']*'|[^\s,;&]+)/gi,
|
|
72
|
+
`$1${REDACTED}`,
|
|
73
|
+
)
|
|
74
|
+
.replace(/(\bBearer\s+)([A-Za-z0-9._~+/=-]+)/gi, `$1${REDACTED}`)
|
|
75
|
+
.replace(/(\bmysql\b[^\r\n]*?\s-p)([^\s]+)/gi, `$1${REDACTED}`)
|
|
76
|
+
.replace(/((?:[A-Za-z0-9_-]+:)+session:)([A-Za-z0-9._:-]{8,})/gi, `$1${REDACTED}`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function redactValue(value: unknown, key?: string): unknown {
|
|
80
|
+
if (key && SECRET_KEY_RE.test(key)) return REDACTED;
|
|
81
|
+
if (typeof value === "string") return redactLogText(value);
|
|
82
|
+
if (Array.isArray(value)) return value.map((item) => redactValue(item));
|
|
83
|
+
if (value instanceof Error) {
|
|
84
|
+
return {
|
|
85
|
+
name: value.name,
|
|
86
|
+
message: redactLogText(value.message),
|
|
87
|
+
stack: value.stack ? redactLogText(value.stack) : undefined,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
if (value instanceof Date) return value;
|
|
91
|
+
if (value !== null && typeof value === "object") {
|
|
92
|
+
const prototype = Object.getPrototypeOf(value);
|
|
93
|
+
if (prototype !== Object.prototype && prototype !== null) return value;
|
|
94
|
+
const redacted: LogFields = {};
|
|
95
|
+
for (const [childKey, childValue] of Object.entries(value)) {
|
|
96
|
+
redacted[childKey] = redactValue(childValue, childKey);
|
|
97
|
+
}
|
|
98
|
+
return redacted;
|
|
99
|
+
}
|
|
100
|
+
return value;
|
|
101
|
+
}
|
|
102
|
+
|
|
61
103
|
function resolveLogDir(): string {
|
|
62
104
|
return process.env.PI_QQ_LOG_DIR ?? activeDir ?? join(process.cwd(), ".pi", "pi-qq", "logs");
|
|
63
105
|
}
|
|
@@ -91,7 +133,7 @@ function stringifyError(err: unknown): string {
|
|
|
91
133
|
|
|
92
134
|
function writeRecord(level: Exclude<LogLevel, "silent">, scope: string, message: string, data?: unknown): void {
|
|
93
135
|
if (!isLevelEnabled(activeLevel, level)) return;
|
|
94
|
-
if (!isLogAllowed()) return;
|
|
136
|
+
if (!isLogAllowed(scope)) return;
|
|
95
137
|
|
|
96
138
|
const msgKey = logMessageKey(message);
|
|
97
139
|
const record: LogFields = {
|
|
@@ -99,12 +141,12 @@ function writeRecord(level: Exclude<LogLevel, "silent">, scope: string, message:
|
|
|
99
141
|
level,
|
|
100
142
|
scope,
|
|
101
143
|
...(msgKey ? { msgKey } : {}),
|
|
102
|
-
msg: message,
|
|
144
|
+
msg: redactLogText(message),
|
|
103
145
|
};
|
|
104
146
|
const fields = normalizeData(data);
|
|
105
147
|
if (fields) {
|
|
106
148
|
for (const [key, value] of Object.entries(fields)) {
|
|
107
|
-
if (!(key in record)) record[key] = value;
|
|
149
|
+
if (!(key in record)) record[key] = redactValue(value, key);
|
|
108
150
|
}
|
|
109
151
|
}
|
|
110
152
|
|
|
@@ -123,7 +165,7 @@ function writeRecord(level: Exclude<LogLevel, "silent">, scope: string, message:
|
|
|
123
165
|
level,
|
|
124
166
|
scope,
|
|
125
167
|
...(msgKey ? { msgKey } : {}),
|
|
126
|
-
msg: message,
|
|
168
|
+
msg: redactLogText(message),
|
|
127
169
|
serializationError: stringifyError(err),
|
|
128
170
|
});
|
|
129
171
|
}
|
|
@@ -140,6 +182,25 @@ function writeRecord(level: Exclude<LogLevel, "silent">, scope: string, message:
|
|
|
140
182
|
.catch(() => {});
|
|
141
183
|
}
|
|
142
184
|
|
|
185
|
+
export function logValueLength(value: unknown): number {
|
|
186
|
+
if (value === undefined || value === null) return 0;
|
|
187
|
+
if (typeof value === "string") return value.length;
|
|
188
|
+
try {
|
|
189
|
+
const text = JSON.stringify(value);
|
|
190
|
+
return typeof text === "string" ? text.length : 0;
|
|
191
|
+
} catch {
|
|
192
|
+
return 0;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function summarizeSdkMessage(message: string): string {
|
|
197
|
+
const marker = "Body: ";
|
|
198
|
+
const index = message.indexOf(marker);
|
|
199
|
+
if (index < 0) return message;
|
|
200
|
+
const bodyLength = message.length - index - marker.length;
|
|
201
|
+
return `${message.slice(0, index + marker.length)}<${bodyLength} chars>`;
|
|
202
|
+
}
|
|
203
|
+
|
|
143
204
|
export const log = {
|
|
144
205
|
debug(scope: string, message: string, data?: unknown): void {
|
|
145
206
|
writeRecord("debug", scope, message, data);
|
|
@@ -157,32 +218,15 @@ export const log = {
|
|
|
157
218
|
|
|
158
219
|
export const sdkLogger = {
|
|
159
220
|
debug(message: string, meta?: Record<string, unknown>): void {
|
|
160
|
-
log.debug("qqbot", message, meta);
|
|
221
|
+
log.debug("qqbot", summarizeSdkMessage(message), meta);
|
|
161
222
|
},
|
|
162
223
|
info(message: string, meta?: Record<string, unknown>): void {
|
|
163
|
-
log.info("qqbot", message, meta);
|
|
224
|
+
log.info("qqbot", summarizeSdkMessage(message), meta);
|
|
164
225
|
},
|
|
165
226
|
warn(message: string, meta?: Record<string, unknown>): void {
|
|
166
|
-
log.warn("qqbot", message, meta);
|
|
227
|
+
log.warn("qqbot", summarizeSdkMessage(message), meta);
|
|
167
228
|
},
|
|
168
229
|
error(message: string, meta?: Record<string, unknown>): void {
|
|
169
|
-
log.error("qqbot", message, meta);
|
|
230
|
+
log.error("qqbot", summarizeSdkMessage(message), meta);
|
|
170
231
|
},
|
|
171
232
|
};
|
|
172
|
-
|
|
173
|
-
export function previewForLog(value: unknown, maxChars = 2000): string {
|
|
174
|
-
let text: string;
|
|
175
|
-
if (typeof value === "string") {
|
|
176
|
-
text = value;
|
|
177
|
-
} else if (value instanceof Error) {
|
|
178
|
-
text = `${value.name}: ${value.message}`;
|
|
179
|
-
} else {
|
|
180
|
-
try {
|
|
181
|
-
const stringified = JSON.stringify(value);
|
|
182
|
-
text = typeof stringified === "string" ? stringified : String(value);
|
|
183
|
-
} catch {
|
|
184
|
-
text = String(value);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
return text.length <= maxChars ? text : `${text.slice(0, maxChars)}…(${text.length} chars)`;
|
|
188
|
-
}
|
|
@@ -60,12 +60,22 @@ export async function handleInboundCommand(
|
|
|
60
60
|
deps.ctx.abort();
|
|
61
61
|
const waitStartedAt = Date.now();
|
|
62
62
|
log.debug("inbound", "stream timing", { phase: "/stop wait start" });
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
63
|
+
const waiting = Promise.resolve()
|
|
64
|
+
.then(() => deps.waitForEnd?.())
|
|
65
|
+
.then(() => {
|
|
66
|
+
log.debug("inbound", "stream timing", {
|
|
67
|
+
phase: "/stop wait done",
|
|
68
|
+
waitedMs: Date.now() - waitStartedAt,
|
|
69
|
+
});
|
|
70
|
+
})
|
|
71
|
+
.catch((err) => {
|
|
72
|
+
log.warn("inbound", "/stop wait failed", {
|
|
73
|
+
waitedMs: Date.now() - waitStartedAt,
|
|
74
|
+
err,
|
|
75
|
+
});
|
|
76
|
+
});
|
|
68
77
|
await reply(t("commands.stop.abort"));
|
|
78
|
+
void waiting;
|
|
69
79
|
}
|
|
70
80
|
break;
|
|
71
81
|
case "/compact":
|
package/src/qq/qq-bot.ts
CHANGED
|
@@ -65,18 +65,12 @@ export function startBot(opts: StartBotOptions): RunningBot {
|
|
|
65
65
|
rawEventType: msg.rawEventType,
|
|
66
66
|
messageId: msg.messageId,
|
|
67
67
|
senderId: msg.senderId,
|
|
68
|
-
senderName: msg.senderName,
|
|
69
68
|
senderIsBot: msg.senderIsBot,
|
|
70
|
-
content: msg.content,
|
|
71
69
|
msgType: msg.msgType,
|
|
72
70
|
timestamp: msg.timestamp,
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
messageScene: msg.messageScene,
|
|
77
|
-
attachments: msg.attachments,
|
|
78
|
-
msgElements: msg.msgElements,
|
|
79
|
-
raw: msg.raw,
|
|
71
|
+
contentLength: (msg.content ?? "").length,
|
|
72
|
+
attachmentCount: (msg.attachments ?? []).length,
|
|
73
|
+
elementCount: (msg.msgElements ?? []).length,
|
|
80
74
|
});
|
|
81
75
|
try {
|
|
82
76
|
await opts.onMessage?.(bot, msg);
|
|
@@ -36,17 +36,12 @@ export function createInboundHandler(deps: InboundDeps) {
|
|
|
36
36
|
rawEventType: msg.rawEventType,
|
|
37
37
|
messageId: msg.messageId,
|
|
38
38
|
senderId: msg.senderId,
|
|
39
|
-
senderName: msg.senderName,
|
|
40
39
|
senderIsBot: msg.senderIsBot,
|
|
41
|
-
content: msg.content,
|
|
42
40
|
msgType: msg.msgType,
|
|
43
41
|
timestamp: msg.timestamp,
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
attachments: msg.attachments,
|
|
48
|
-
msgElements: msg.msgElements,
|
|
49
|
-
raw: msg.raw,
|
|
42
|
+
contentLength: (msg.content ?? "").length,
|
|
43
|
+
attachmentCount: (msg.attachments ?? []).length,
|
|
44
|
+
elementCount: (msg.msgElements ?? []).length,
|
|
50
45
|
});
|
|
51
46
|
|
|
52
47
|
if (msg.kind !== "c2c") {
|
package/src/qq/qr-login.ts
CHANGED
|
@@ -47,7 +47,7 @@ export async function runQrLogin(ctx: ExtensionContext): Promise<QrLoginResult |
|
|
|
47
47
|
causeCode: cause && typeof cause === "object" ? (cause as NodeJS.ErrnoException).code : undefined,
|
|
48
48
|
causeMessage: cause instanceof Error ? cause.message : undefined,
|
|
49
49
|
});
|
|
50
|
-
ctx.ui.notify(t("login.failed")
|
|
50
|
+
ctx.ui.notify(`${t("login.failed")}: ${err.message}`, "error");
|
|
51
51
|
finish();
|
|
52
52
|
closeOverlay?.();
|
|
53
53
|
},
|
package/src/qq/reply-sender.ts
CHANGED
|
@@ -121,7 +121,6 @@ export async function sendReply(bot: QQBot, text: string, ch: ReplyChannel): Pro
|
|
|
121
121
|
targetId,
|
|
122
122
|
msgId: msgId ?? null,
|
|
123
123
|
length: chunk.length,
|
|
124
|
-
text: chunk,
|
|
125
124
|
});
|
|
126
125
|
await sendWithMsgSeq(bot, { target: replyTarget, content: chunk });
|
|
127
126
|
if (mode === "passive" && msgId) ch.budget.commitPassive(msgId);
|
|
@@ -26,6 +26,15 @@ function isDuplicateMsgSeqError(err: unknown): boolean {
|
|
|
26
26
|
);
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
function errorCode(err: unknown): number | undefined {
|
|
30
|
+
const e = err as { code?: number; err_code?: number; bizCode?: number };
|
|
31
|
+
return e?.code ?? e?.err_code ?? e?.bizCode;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isExpiredStreamError(err: unknown): boolean {
|
|
35
|
+
return errorCode(err) === 40034020 || /40034020|超过时间限制|time limit/i.test(String((err as Error)?.message ?? ""));
|
|
36
|
+
}
|
|
37
|
+
|
|
29
38
|
export interface RepushTool {
|
|
30
39
|
name: string;
|
|
31
40
|
args?: unknown;
|
|
@@ -63,6 +72,8 @@ export class BubbleWriter {
|
|
|
63
72
|
private toolOpen = false;
|
|
64
73
|
private queue: Promise<void> = Promise.resolve();
|
|
65
74
|
private disposed = false;
|
|
75
|
+
private terminalCode?: number;
|
|
76
|
+
private lastErrorCode?: number;
|
|
66
77
|
|
|
67
78
|
constructor(private readonly deps: BubbleWriterDeps) {}
|
|
68
79
|
|
|
@@ -74,6 +85,14 @@ export class BubbleWriter {
|
|
|
74
85
|
return this.streamDead;
|
|
75
86
|
}
|
|
76
87
|
|
|
88
|
+
get deadCode(): number | undefined {
|
|
89
|
+
return this.terminalCode;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
get hasActiveStream(): boolean {
|
|
93
|
+
return Boolean(this.cur) && !this.streamDead && !this.disposed;
|
|
94
|
+
}
|
|
95
|
+
|
|
77
96
|
stats(now = Date.now()): {
|
|
78
97
|
dead: boolean;
|
|
79
98
|
disposed: boolean;
|
|
@@ -202,6 +221,8 @@ export class BubbleWriter {
|
|
|
202
221
|
this.streamDead = false;
|
|
203
222
|
this.pendingResume = undefined;
|
|
204
223
|
this.resumeAttempts = 0;
|
|
224
|
+
this.terminalCode = undefined;
|
|
225
|
+
this.lastErrorCode = undefined;
|
|
205
226
|
this.lastKind = "none";
|
|
206
227
|
this.blockClosed = false;
|
|
207
228
|
this.toolOpen = false;
|
|
@@ -225,13 +246,14 @@ export class BubbleWriter {
|
|
|
225
246
|
await this.queue;
|
|
226
247
|
if (this.streamDead) return false;
|
|
227
248
|
if (this.cur !== stream) continue;
|
|
249
|
+
this.lastErrorCode = undefined;
|
|
228
250
|
return true;
|
|
229
251
|
} catch (err) {
|
|
230
252
|
this.handleStreamError(err as Error);
|
|
231
253
|
if (this.streamDead) return false;
|
|
232
254
|
}
|
|
233
255
|
}
|
|
234
|
-
this.killStream("resume attempts exhausted");
|
|
256
|
+
this.killStream("resume attempts exhausted", this.lastErrorCode);
|
|
235
257
|
return false;
|
|
236
258
|
}
|
|
237
259
|
|
|
@@ -361,6 +383,7 @@ export class BubbleWriter {
|
|
|
361
383
|
const outgoing = opts.thinking && (!this.cur || this.bubbleChars === 0) ? `${thinkingHeader()}${text}` : text;
|
|
362
384
|
try {
|
|
363
385
|
await stream.append(outgoing);
|
|
386
|
+
this.lastErrorCode = undefined;
|
|
364
387
|
this.bubbleChars += outgoing.length;
|
|
365
388
|
this.lastFrameAt = Date.now();
|
|
366
389
|
this.trackFences(outgoing);
|
|
@@ -457,6 +480,7 @@ export class BubbleWriter {
|
|
|
457
480
|
if (!bot || !this.deps.msgId) return null;
|
|
458
481
|
const resume = this.pendingResume;
|
|
459
482
|
this.pendingResume = undefined;
|
|
483
|
+
const fresh = !resume || (resume.msgSeq === undefined && !resume.streamMsgId);
|
|
460
484
|
const stream = new AppendStream(bot.messageApi, bot.getCredentials(), this.deps.openid, this.deps.msgId, {
|
|
461
485
|
resume,
|
|
462
486
|
onError: (err) => {
|
|
@@ -484,7 +508,7 @@ export class BubbleWriter {
|
|
|
484
508
|
onDebug: (message) => log.debug("stream", message, { msgId: this.logMsgId() }),
|
|
485
509
|
});
|
|
486
510
|
this.cur = stream;
|
|
487
|
-
if (
|
|
511
|
+
if (fresh) this.deps.budget.noteStreamBubble(this.deps.msgId);
|
|
488
512
|
if (isLogEnabled("debug")) {
|
|
489
513
|
log.debug("stream", "stream timing", {
|
|
490
514
|
phase: "stream open",
|
|
@@ -510,11 +534,15 @@ export class BubbleWriter {
|
|
|
510
534
|
const checkpoint = stream?.checkpoint() ?? null;
|
|
511
535
|
stream?.dispose();
|
|
512
536
|
this.cur = null;
|
|
537
|
+
const code = errorCode(err);
|
|
513
538
|
const resetSeq = isDuplicateMsgSeqError(err);
|
|
539
|
+
const expired = isExpiredStreamError(err);
|
|
540
|
+
this.lastErrorCode = code;
|
|
514
541
|
log.warn("stream", "stream error", {
|
|
515
542
|
msgId: this.logMsgId(),
|
|
516
543
|
elapsedMs: Date.now() - this.deps.startedAt,
|
|
517
544
|
resetSeq,
|
|
545
|
+
expired,
|
|
518
546
|
checkpoint: checkpoint
|
|
519
547
|
? {
|
|
520
548
|
index: checkpoint.index,
|
|
@@ -528,26 +556,29 @@ export class BubbleWriter {
|
|
|
528
556
|
});
|
|
529
557
|
if (checkpoint && this.resumeAttempts < MAX_RESUME_ATTEMPTS) {
|
|
530
558
|
this.resumeAttempts += 1;
|
|
531
|
-
this.pendingResume =
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
559
|
+
this.pendingResume =
|
|
560
|
+
resetSeq || expired
|
|
561
|
+
? {
|
|
562
|
+
index: 0,
|
|
563
|
+
...(checkpoint.buffer ? { buffer: checkpoint.buffer } : {}),
|
|
564
|
+
}
|
|
565
|
+
: checkpoint;
|
|
537
566
|
return;
|
|
538
567
|
}
|
|
539
|
-
this.killStream(err.message);
|
|
568
|
+
this.killStream(err.message, code);
|
|
540
569
|
}
|
|
541
570
|
|
|
542
|
-
private killStream(reason: string): void {
|
|
571
|
+
private killStream(reason: string, code = this.lastErrorCode): void {
|
|
543
572
|
if (this.streamDead) return;
|
|
544
573
|
this.streamDead = true;
|
|
574
|
+
this.terminalCode = code;
|
|
545
575
|
this.pendingResume = undefined;
|
|
546
576
|
this.cur?.dispose();
|
|
547
577
|
this.cur = null;
|
|
548
578
|
log.warn("stream", "stream unavailable", {
|
|
549
579
|
msgId: this.logMsgId(),
|
|
550
580
|
reason,
|
|
581
|
+
code: code ?? null,
|
|
551
582
|
stats: this.stats(),
|
|
552
583
|
budget: this.deps.budget.snapshot(),
|
|
553
584
|
});
|
|
@@ -39,6 +39,7 @@ export type RepushResult =
|
|
|
39
39
|
const TYPING_INTERVAL_MS = 50_000;
|
|
40
40
|
const PENDING_TTL_MS = 15_000;
|
|
41
41
|
const RECOVERY_TTL_MS = 60_000;
|
|
42
|
+
const AUTO_REBIND_TTL_MS = 5 * 60_000;
|
|
42
43
|
|
|
43
44
|
function toRepushTool(tool: TimelineTool): RepushTool {
|
|
44
45
|
return {
|
|
@@ -55,6 +56,7 @@ export class RunFeedback {
|
|
|
55
56
|
private recovery: { openid: string; msgId: string; at: number } | null = null;
|
|
56
57
|
private run: RunState | null = null;
|
|
57
58
|
private lastCompletedText: string | undefined;
|
|
59
|
+
private autoRebinds = 0;
|
|
58
60
|
private endPromise: Promise<void> = Promise.resolve();
|
|
59
61
|
private endResolve?: () => void;
|
|
60
62
|
|
|
@@ -144,6 +146,7 @@ export class RunFeedback {
|
|
|
144
146
|
}
|
|
145
147
|
|
|
146
148
|
onAgentStart(): void {
|
|
149
|
+
this.autoRebinds = 0;
|
|
147
150
|
this.endPromise = new Promise((resolve) => {
|
|
148
151
|
this.endResolve = resolve;
|
|
149
152
|
});
|
|
@@ -205,6 +208,9 @@ export class RunFeedback {
|
|
|
205
208
|
}
|
|
206
209
|
|
|
207
210
|
const keepTyping = () => {
|
|
211
|
+
// A live stream already shows activity; do not compete with it for the
|
|
212
|
+
// serialized API client, especially when the network is slow.
|
|
213
|
+
if (run.writer.hasActiveStream) return;
|
|
208
214
|
void bot.sendTyping({ scope: "c2c", targetId: run.openid }, 60).catch(() => {});
|
|
209
215
|
};
|
|
210
216
|
keepTyping();
|
|
@@ -294,6 +300,7 @@ export class RunFeedback {
|
|
|
294
300
|
run.finalizedText = [run.finalizedText, text].filter((part) => part.trim()).join("\n\n");
|
|
295
301
|
const delta = run.finalizedText.slice(run.resultSentLen);
|
|
296
302
|
run.resultSentLen = run.finalizedText.length;
|
|
303
|
+
if (!isError) this.tryAutoRebind(run);
|
|
297
304
|
if (!isError && text) this.emitTextDelta(run, delta, policy);
|
|
298
305
|
if (!isError && text) run.writer.closeBlock();
|
|
299
306
|
|
|
@@ -303,7 +310,11 @@ export class RunFeedback {
|
|
|
303
310
|
}
|
|
304
311
|
|
|
305
312
|
const hadPendingTools = this.hasPendingTools(run);
|
|
306
|
-
|
|
313
|
+
let ok = await run.writer.flush();
|
|
314
|
+
if (!isError && !ok && run.writer.deadCode === 40034020) {
|
|
315
|
+
this.tryAutoRebind(run);
|
|
316
|
+
if (this.autoRebinds > 0) ok = await run.writer.flush();
|
|
317
|
+
}
|
|
307
318
|
const streamTookOver = !isError && ok && run.writer.hasPushed;
|
|
308
319
|
log.debug("run", "stream takeover decision", {
|
|
309
320
|
phase: "assistant final",
|
|
@@ -387,6 +398,36 @@ export class RunFeedback {
|
|
|
387
398
|
return run.timeline.hasPendingTools();
|
|
388
399
|
}
|
|
389
400
|
|
|
401
|
+
private tryAutoRebind(run: RunState): void {
|
|
402
|
+
if (this.autoRebinds > 0 || run.writer.deadCode !== 40034020) return;
|
|
403
|
+
|
|
404
|
+
let openid = run.openid;
|
|
405
|
+
let msgId = run.msgId;
|
|
406
|
+
let freshTarget = false;
|
|
407
|
+
let targetAgeMs: number | null = null;
|
|
408
|
+
const target = this.deps.budget.lastTarget;
|
|
409
|
+
if (target && Date.now() - target.arrivedAt <= AUTO_REBIND_TTL_MS) {
|
|
410
|
+
openid = target.openid;
|
|
411
|
+
msgId = target.msgId;
|
|
412
|
+
freshTarget = true;
|
|
413
|
+
targetAgeMs = Date.now() - target.arrivedAt;
|
|
414
|
+
}
|
|
415
|
+
if (!msgId) return;
|
|
416
|
+
|
|
417
|
+
const result = this.repushCurrent(openid, msgId);
|
|
418
|
+
if (!result.ok) return;
|
|
419
|
+
this.autoRebinds += 1;
|
|
420
|
+
log.debug("run", "stream timing", {
|
|
421
|
+
phase: "auto rebind",
|
|
422
|
+
msgId: msgId.slice(0, 8),
|
|
423
|
+
freshTarget,
|
|
424
|
+
targetAgeMs,
|
|
425
|
+
textLength: result.textLength,
|
|
426
|
+
thinkingLength: result.thinkingLength,
|
|
427
|
+
toolCount: result.toolCount,
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
|
|
390
431
|
private emitTextDelta(run: RunState, delta: string, policy: ReplyPolicy): void {
|
|
391
432
|
if (!delta || run.writer.dead) return;
|
|
392
433
|
if (policy.deferTextWhileToolsPending && this.hasPendingTools(run)) {
|
|
@@ -10,6 +10,10 @@ import type {
|
|
|
10
10
|
const MIN_INTERVAL_MS = 300;
|
|
11
11
|
const RATE_LIMIT_BASE_MS = 1000;
|
|
12
12
|
const MAX_RETRIES = 3;
|
|
13
|
+
// QQ rejects oversized markdown shards with 40034017. The platform appears to
|
|
14
|
+
// split frames larger than roughly 4k and then requires every shard to end at
|
|
15
|
+
// a newline; 3500 bytes leaves room for that limit and for multi-byte text.
|
|
16
|
+
export const MAX_FRAME_BYTES = 3500;
|
|
13
17
|
|
|
14
18
|
export interface AppendStreamCheckpoint {
|
|
15
19
|
msgSeq?: number;
|
|
@@ -43,6 +47,7 @@ export class AppendStream {
|
|
|
43
47
|
private buffer = "";
|
|
44
48
|
private flushTimer: ReturnType<typeof setTimeout> | null = null;
|
|
45
49
|
private flushing = false;
|
|
50
|
+
private flushingAppends = 0;
|
|
46
51
|
private lastFlushAt = 0;
|
|
47
52
|
|
|
48
53
|
constructor(
|
|
@@ -73,15 +78,16 @@ export class AppendStream {
|
|
|
73
78
|
|
|
74
79
|
async flush(): Promise<void> {
|
|
75
80
|
if (this.doneSent) return;
|
|
76
|
-
|
|
77
|
-
clearTimeout(this.flushTimer);
|
|
78
|
-
this.flushTimer = null;
|
|
79
|
-
}
|
|
81
|
+
this.clearFlushTimer();
|
|
80
82
|
while (this.flushing) {
|
|
81
83
|
await new Promise((r) => setTimeout(r, 20));
|
|
82
84
|
}
|
|
83
85
|
if (this.doneSent) return;
|
|
84
|
-
|
|
86
|
+
while (this.buffer) {
|
|
87
|
+
await this.doFlush("generating");
|
|
88
|
+
this.clearFlushTimer();
|
|
89
|
+
if (this.buffer) await this.waitForThrottle();
|
|
90
|
+
}
|
|
85
91
|
}
|
|
86
92
|
|
|
87
93
|
private throttleMs(): number {
|
|
@@ -94,7 +100,10 @@ export class AppendStream {
|
|
|
94
100
|
const elapsed = Date.now() - this.lastFlushAt;
|
|
95
101
|
const throttle = this.throttleMs();
|
|
96
102
|
if (this.flushing) {
|
|
97
|
-
this.
|
|
103
|
+
this.flushingAppends += 1;
|
|
104
|
+
if (this.flushingAppends === 1 || this.flushingAppends % 50 === 0) {
|
|
105
|
+
this.opts.onDebug?.(`append: while flushing, count=${this.flushingAppends}, buffer=${this.buffer.length}`);
|
|
106
|
+
}
|
|
98
107
|
return;
|
|
99
108
|
}
|
|
100
109
|
if (elapsed >= throttle) {
|
|
@@ -115,35 +124,83 @@ export class AppendStream {
|
|
|
115
124
|
|
|
116
125
|
async done(): Promise<void> {
|
|
117
126
|
if (this.doneSent) return;
|
|
118
|
-
|
|
119
|
-
clearTimeout(this.flushTimer);
|
|
120
|
-
this.flushTimer = null;
|
|
121
|
-
}
|
|
127
|
+
this.clearFlushTimer();
|
|
122
128
|
while (this.flushing) {
|
|
123
129
|
await new Promise((r) => setTimeout(r, 20));
|
|
124
130
|
}
|
|
125
131
|
if (this.doneSent) return;
|
|
126
|
-
|
|
132
|
+
while (this.buffer) {
|
|
133
|
+
await this.doFlush("generating");
|
|
134
|
+
this.clearFlushTimer();
|
|
135
|
+
if (this.buffer) await this.waitForThrottle();
|
|
136
|
+
}
|
|
127
137
|
await this.doFlush("done");
|
|
128
138
|
this.doneSent = true;
|
|
129
139
|
}
|
|
130
140
|
|
|
131
141
|
dispose(): void {
|
|
142
|
+
this.clearFlushTimer();
|
|
143
|
+
this.doneSent = true;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private clearFlushTimer(): void {
|
|
132
147
|
if (this.flushTimer) {
|
|
133
148
|
clearTimeout(this.flushTimer);
|
|
134
149
|
this.flushTimer = null;
|
|
135
150
|
}
|
|
136
|
-
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private async waitForThrottle(): Promise<void> {
|
|
154
|
+
const waitMs = this.throttleMs() - (Date.now() - this.lastFlushAt);
|
|
155
|
+
if (waitMs > 0) await new Promise((r) => setTimeout(r, waitMs));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private takeFrame(): { text: string; consumed: number } {
|
|
159
|
+
const buffer = this.buffer;
|
|
160
|
+
if (!buffer) return { text: "", consumed: 0 };
|
|
161
|
+
if (Buffer.byteLength(buffer, "utf8") <= MAX_FRAME_BYTES) {
|
|
162
|
+
return { text: buffer, consumed: buffer.length };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
let bytes = 0;
|
|
166
|
+
let consumed = 0;
|
|
167
|
+
let lastNewline = -1;
|
|
168
|
+
// Reserve one byte for the newline added when no line break is in range.
|
|
169
|
+
const scanLimit = MAX_FRAME_BYTES - 1;
|
|
170
|
+
for (const char of buffer) {
|
|
171
|
+
const size = Buffer.byteLength(char, "utf8");
|
|
172
|
+
if (bytes + size > scanLimit) break;
|
|
173
|
+
bytes += size;
|
|
174
|
+
consumed += char.length;
|
|
175
|
+
if (char === "\n") lastNewline = consumed;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (lastNewline > 0) {
|
|
179
|
+
return { text: buffer.slice(0, lastNewline), consumed: lastNewline };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// A single line longer than the platform shard limit: hard-cut on a code
|
|
183
|
+
// point boundary and terminate the shard with a newline. This is the only
|
|
184
|
+
// way to satisfy the API without dropping content.
|
|
185
|
+
if (consumed === 0) {
|
|
186
|
+
const first = buffer.codePointAt(0) ?? 0;
|
|
187
|
+
const char = String.fromCodePoint(first);
|
|
188
|
+
return { text: `${char}\n`, consumed: char.length };
|
|
189
|
+
}
|
|
190
|
+
return { text: `${buffer.slice(0, consumed)}\n`, consumed };
|
|
137
191
|
}
|
|
138
192
|
|
|
139
193
|
private async doFlush(state: "generating" | "done"): Promise<void> {
|
|
140
194
|
if (this.flushing) return;
|
|
141
|
-
|
|
195
|
+
this.clearFlushTimer();
|
|
196
|
+
const frame = state === "done" ? { text: "", consumed: 0 } : this.takeFrame();
|
|
197
|
+
const content = frame.text;
|
|
142
198
|
if (!content && state === "generating") return;
|
|
143
199
|
this.opts.onDebug?.(
|
|
144
200
|
`flush: start state=${state}, content=${content.length}, index=${this.index}, msgSeq=${this.msgSeq ?? "none"}, streamMsgId=${this.streamMsgId ?? "none"}`,
|
|
145
201
|
);
|
|
146
202
|
this.flushing = true;
|
|
203
|
+
this.flushingAppends = 0;
|
|
147
204
|
let failed = false;
|
|
148
205
|
try {
|
|
149
206
|
if (this.msgSeq === null) this.msgSeq = allocateMsgSeq(this.msgId);
|
|
@@ -161,7 +218,7 @@ export class AppendStream {
|
|
|
161
218
|
} as unknown as StreamMessageRequest;
|
|
162
219
|
const resp = await this.sendWithRetry(req, state);
|
|
163
220
|
if (resp?.id && !this.streamMsgId) this.streamMsgId = resp.id;
|
|
164
|
-
if (state === "generating") this.buffer = this.buffer.slice(
|
|
221
|
+
if (state === "generating") this.buffer = this.buffer.slice(frame.consumed);
|
|
165
222
|
this.lastFlushAt = Date.now();
|
|
166
223
|
this.opts.onDebug?.(
|
|
167
224
|
`flush: done state=${state}, remaining=${this.buffer.length}, streamMsgId=${this.streamMsgId ?? "none"}`,
|