@0xmaxma/claude-gateway 1.3.31 → 1.3.32
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/README.md +3 -1
- package/dist/agent/runner.d.ts +22 -6
- package/dist/agent/runner.d.ts.map +1 -1
- package/dist/agent/runner.js +60 -25
- package/dist/agent/runner.js.map +1 -1
- package/dist/api/router.d.ts.map +1 -1
- package/dist/api/router.js +104 -4
- package/dist/api/router.js.map +1 -1
- package/dist/history/db.d.ts +13 -1
- package/dist/history/db.d.ts.map +1 -1
- package/dist/history/db.js +67 -9
- package/dist/history/db.js.map +1 -1
- package/dist/history/types.d.ts +10 -0
- package/dist/history/types.d.ts.map +1 -1
- package/dist/session/process.d.ts +8 -0
- package/dist/session/process.d.ts.map +1 -1
- package/dist/session/process.js +16 -0
- package/dist/session/process.js.map +1 -1
- package/dist/types.d.ts +1 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -230,6 +230,7 @@ Global default retention policy. Can be overridden per-agent with an `history` k
|
|
|
230
230
|
"gateway": {
|
|
231
231
|
"history": {
|
|
232
232
|
"retentionDays": 90,
|
|
233
|
+
"maxHistoryMessages": 30,
|
|
233
234
|
"cleanupHour": 3,
|
|
234
235
|
"cleanupTimezone": "Asia/Bangkok"
|
|
235
236
|
}
|
|
@@ -240,6 +241,7 @@ Global default retention policy. Can be overridden per-agent with an `history` k
|
|
|
240
241
|
| Field | Default | Description |
|
|
241
242
|
|-------|---------|-------------|
|
|
242
243
|
| `retentionDays` | `null` (keep forever) | Delete messages older than N days on each cleanup cycle |
|
|
244
|
+
| `maxHistoryMessages` | `50` | Max history messages re-injected into a session at spawn. Lower it to shrink the context loaded at session start. `0` = inject no history |
|
|
243
245
|
| `cleanupHour` | `3` | Hour of day to run cleanup (24h, in `cleanupTimezone`) |
|
|
244
246
|
| `cleanupTimezone` | `"UTC"` | IANA timezone for the cleanup schedule |
|
|
245
247
|
|
|
@@ -249,7 +251,7 @@ Per-agent override example:
|
|
|
249
251
|
"agents": [
|
|
250
252
|
{
|
|
251
253
|
"id": "alfred",
|
|
252
|
-
"history": { "retentionDays": 30 }
|
|
254
|
+
"history": { "retentionDays": 30, "maxHistoryMessages": 30 }
|
|
253
255
|
}
|
|
254
256
|
]
|
|
255
257
|
}
|
package/dist/agent/runner.d.ts
CHANGED
|
@@ -165,6 +165,21 @@ export declare class AgentRunner extends EventEmitter {
|
|
|
165
165
|
* The process will be lazily re-spawned on the next incoming message.
|
|
166
166
|
*/
|
|
167
167
|
private restartProcess;
|
|
168
|
+
/**
|
|
169
|
+
* The 32MB-recovery rungs for a given healthy cap: the ladder sizes STRICTLY
|
|
170
|
+
* below the cap, in descending order. Filtering by `< cap` (not `<=`) drops any
|
|
171
|
+
* rung equal to or above the cap so a lowered cap never yields a recovery step
|
|
172
|
+
* that re-injects the same (or more) history — every step actually shrinks.
|
|
173
|
+
*/
|
|
174
|
+
private recoveryRungs;
|
|
175
|
+
/**
|
|
176
|
+
* History re-injection cap for a spawn, given the configured healthy cap and how
|
|
177
|
+
* many consecutive 32MB recoveries have happened on the session. recoveryCount 0
|
|
178
|
+
* = healthy → the full configured cap. Each later recovery drops to the next
|
|
179
|
+
* rung strictly below the cap; once those are exhausted it stays at 0 (no
|
|
180
|
+
* history). Kept in sync with the exhaustion threshold in handleRequestTooLarge.
|
|
181
|
+
*/
|
|
182
|
+
private spawnHistoryLimit;
|
|
168
183
|
/**
|
|
169
184
|
* Unified recovery for the recoverable "Request too large (max 32MB)" error.
|
|
170
185
|
* Reached from two backends that surface the SAME error differently:
|
|
@@ -174,12 +189,13 @@ export declare class AgentRunner extends EventEmitter {
|
|
|
174
189
|
* (is_error + "Request too large (max"); the long-lived process otherwise
|
|
175
190
|
* stays alive and rejects every subsequent turn forever (Bug B).
|
|
176
191
|
*
|
|
177
|
-
* Each consecutive recovery shrinks the history re-injected on the next spawn
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
192
|
+
* Each consecutive recovery shrinks the history re-injected on the next spawn,
|
|
193
|
+
* stepping down the TOO_LARGE_HISTORY_LADDER rungs strictly below the configured
|
|
194
|
+
* cap (default 50 → 40→30→20→10→0), so a pathological context drops under the
|
|
195
|
+
* 32MB ceiling. The respawn happens on the user's NEXT message (no auto-loop),
|
|
196
|
+
* and the counter resets on the next successful result. Once even a zero-history
|
|
197
|
+
* spawn still trips 32MB, stop escalating and ask the user to /clear rather than
|
|
198
|
+
* climb the ladder again.
|
|
183
199
|
*/
|
|
184
200
|
private handleRequestTooLarge;
|
|
185
201
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runner.d.ts","sourceRoot":"","sources":["../../src/agent/runner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAMtC,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,EAAW,WAAW,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAShH,OAAO,EAA0C,KAAK,aAAa,EAAE,MAAM,WAAW,CAAC;AAQvF,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"runner.d.ts","sourceRoot":"","sources":["../../src/agent/runner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAMtC,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,EAAW,WAAW,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAShH,OAAO,EAA0C,KAAK,aAAa,EAAE,MAAM,WAAW,CAAC;AAQvF,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAqB1C,eAAO,MAAM,oBAAoB,QAAmB,CAAC;AAErD,eAAO,MAAM,cAAc,EAAE,WAAW,EAUvC,CAAC;AAqBF,eAAO,MAAM,0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AAI,MAAM;IAIzB;;;;;;OAMG;IACH,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;CAoBnC"}
|
package/dist/agent/runner.js
CHANGED
|
@@ -61,14 +61,16 @@ const cleanup_1 = require("../history/cleanup");
|
|
|
61
61
|
const DEFAULT_IDLE_TIMEOUT_MINUTES = 30;
|
|
62
62
|
const DEFAULT_MAX_CONCURRENT = 20;
|
|
63
63
|
const ANTHROPIC_SOCKET_ERROR = 'socket connection was closed unexpectedly';
|
|
64
|
-
// History re-injection ladder for request_too_large (32MB) recovery
|
|
65
|
-
//
|
|
66
|
-
//
|
|
67
|
-
//
|
|
68
|
-
//
|
|
69
|
-
//
|
|
70
|
-
//
|
|
71
|
-
// /clear instead of looping forever.
|
|
64
|
+
// History re-injection ladder for request_too_large (32MB) recovery: the
|
|
65
|
+
// candidate history sizes a recovering session can step down to. The HEALTHY
|
|
66
|
+
// spawn uses the configured cap (resolveMaxHistoryMessages), not an index into
|
|
67
|
+
// this array; on each consecutive 32MB recovery the session drops to the next
|
|
68
|
+
// ladder rung STRICTLY BELOW that cap (see spawnHistoryLimit), so every retry
|
|
69
|
+
// actually shrinks the re-loaded context instead of re-trying the same size.
|
|
70
|
+
// Once even the 0-history rung still trips 32MB the runner stops escalating and
|
|
71
|
+
// asks the user to /clear instead of looping forever. The leading
|
|
72
|
+
// MAX_HISTORY_MESSAGES only acts as a recovery rung when an operator configures
|
|
73
|
+
// a cap higher than it.
|
|
72
74
|
const TOO_LARGE_HISTORY_LADDER = [process_1.MAX_HISTORY_MESSAGES, 40, 30, 20, 10, 0];
|
|
73
75
|
exports.MAX_IMAGE_SIZE_BYTES = 10 * 1024 * 1024;
|
|
74
76
|
exports.DEFAULT_MODELS = [
|
|
@@ -956,14 +958,18 @@ class AgentRunner extends events_1.EventEmitter {
|
|
|
956
958
|
// Apply per-session model override (caller already normalized to undefined when == agent default)
|
|
957
959
|
if (modelOverride)
|
|
958
960
|
proc.modelOverride = modelOverride;
|
|
961
|
+
// Per-agent → global → MAX_HISTORY_MESSAGES: the configured cap on how many
|
|
962
|
+
// history messages a healthy spawn re-injects. Lets an operator lower the
|
|
963
|
+
// context loaded at session start (e.g. 50 → 30) without touching code.
|
|
964
|
+
const configuredMax = (0, process_1.resolveMaxHistoryMessages)(this.agentConfig.history?.maxHistoryMessages, this.gatewayConfig.gateway.history?.maxHistoryMessages);
|
|
959
965
|
// request_too_large (32MB) recovery: shrink the re-injected history on each
|
|
960
966
|
// consecutive retry so a pathological context eventually fits. recoveryCount
|
|
961
|
-
// is 0 for healthy sessions →
|
|
962
|
-
//
|
|
963
|
-
//
|
|
967
|
+
// is 0 for healthy sessions → use the configured cap directly; a recovering
|
|
968
|
+
// session steps down to the ladder rungs STRICTLY BELOW that cap, so each
|
|
969
|
+
// retry genuinely shrinks instead of re-trying the same (already-too-large)
|
|
970
|
+
// size when the cap has been lowered.
|
|
964
971
|
const recoveryCount = this.tooLargeRecoveries.get(mapKey) ?? 0;
|
|
965
|
-
|
|
966
|
-
proc.historyLimit = TOO_LARGE_HISTORY_LADDER[ladderIdx];
|
|
972
|
+
proc.historyLimit = this.spawnHistoryLimit(configuredMax, recoveryCount);
|
|
967
973
|
if (recoveryCount > 0) {
|
|
968
974
|
this.logger.info('Spawning with reduced history after request_too_large', {
|
|
969
975
|
mapKey, recoveryCount, historyLimit: proc.historyLimit,
|
|
@@ -1531,6 +1537,30 @@ class AgentRunner extends events_1.EventEmitter {
|
|
|
1531
1537
|
}
|
|
1532
1538
|
// Process will be re-spawned on next incoming message
|
|
1533
1539
|
}
|
|
1540
|
+
/**
|
|
1541
|
+
* The 32MB-recovery rungs for a given healthy cap: the ladder sizes STRICTLY
|
|
1542
|
+
* below the cap, in descending order. Filtering by `< cap` (not `<=`) drops any
|
|
1543
|
+
* rung equal to or above the cap so a lowered cap never yields a recovery step
|
|
1544
|
+
* that re-injects the same (or more) history — every step actually shrinks.
|
|
1545
|
+
*/
|
|
1546
|
+
recoveryRungs(configuredMax) {
|
|
1547
|
+
return TOO_LARGE_HISTORY_LADDER.filter(r => r < configuredMax);
|
|
1548
|
+
}
|
|
1549
|
+
/**
|
|
1550
|
+
* History re-injection cap for a spawn, given the configured healthy cap and how
|
|
1551
|
+
* many consecutive 32MB recoveries have happened on the session. recoveryCount 0
|
|
1552
|
+
* = healthy → the full configured cap. Each later recovery drops to the next
|
|
1553
|
+
* rung strictly below the cap; once those are exhausted it stays at 0 (no
|
|
1554
|
+
* history). Kept in sync with the exhaustion threshold in handleRequestTooLarge.
|
|
1555
|
+
*/
|
|
1556
|
+
spawnHistoryLimit(configuredMax, recoveryCount) {
|
|
1557
|
+
if (recoveryCount <= 0)
|
|
1558
|
+
return configuredMax;
|
|
1559
|
+
const rungs = this.recoveryRungs(configuredMax);
|
|
1560
|
+
if (rungs.length === 0)
|
|
1561
|
+
return 0;
|
|
1562
|
+
return rungs[Math.min(recoveryCount - 1, rungs.length - 1)];
|
|
1563
|
+
}
|
|
1534
1564
|
/**
|
|
1535
1565
|
* Unified recovery for the recoverable "Request too large (max 32MB)" error.
|
|
1536
1566
|
* Reached from two backends that surface the SAME error differently:
|
|
@@ -1540,21 +1570,26 @@ class AgentRunner extends events_1.EventEmitter {
|
|
|
1540
1570
|
* (is_error + "Request too large (max"); the long-lived process otherwise
|
|
1541
1571
|
* stays alive and rejects every subsequent turn forever (Bug B).
|
|
1542
1572
|
*
|
|
1543
|
-
* Each consecutive recovery shrinks the history re-injected on the next spawn
|
|
1544
|
-
*
|
|
1545
|
-
*
|
|
1546
|
-
*
|
|
1547
|
-
*
|
|
1548
|
-
*
|
|
1573
|
+
* Each consecutive recovery shrinks the history re-injected on the next spawn,
|
|
1574
|
+
* stepping down the TOO_LARGE_HISTORY_LADDER rungs strictly below the configured
|
|
1575
|
+
* cap (default 50 → 40→30→20→10→0), so a pathological context drops under the
|
|
1576
|
+
* 32MB ceiling. The respawn happens on the user's NEXT message (no auto-loop),
|
|
1577
|
+
* and the counter resets on the next successful result. Once even a zero-history
|
|
1578
|
+
* spawn still trips 32MB, stop escalating and ask the user to /clear rather than
|
|
1579
|
+
* climb the ladder again.
|
|
1549
1580
|
*/
|
|
1550
1581
|
handleRequestTooLarge(mapKey, proc) {
|
|
1551
1582
|
proc.setProcessing(false);
|
|
1583
|
+
// Recovery steps through the ladder rungs strictly below the configured cap;
|
|
1584
|
+
// the number of those rungs is how many shrink attempts exist before even the
|
|
1585
|
+
// smallest (0-history) spawn has been tried and still trips 32MB.
|
|
1586
|
+
const configuredMax = (0, process_1.resolveMaxHistoryMessages)(this.agentConfig.history?.maxHistoryMessages, this.gatewayConfig.gateway.history?.maxHistoryMessages);
|
|
1587
|
+
const stepCount = this.recoveryRungs(configuredMax).length; // shrink attempts available
|
|
1552
1588
|
const count = (this.tooLargeRecoveries.get(mapKey) ?? 0) + 1;
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
//
|
|
1556
|
-
|
|
1557
|
-
this.tooLargeRecoveries.set(mapKey, lastRung);
|
|
1589
|
+
if (count > stepCount) {
|
|
1590
|
+
// Even the smallest rung tripped 32MB — context can't shrink further.
|
|
1591
|
+
// Pin the counter at the last step and always surface the /clear next step.
|
|
1592
|
+
this.tooLargeRecoveries.set(mapKey, stepCount);
|
|
1558
1593
|
this.logger.error('Request too large persists with zero re-injected history', { mapKey, count });
|
|
1559
1594
|
this.writeAutoForward(mapKey, '⚠️ ยังเกิน 32MB แม้จะล้าง context จนว่างแล้ว — พิมพ์ /clear เพื่อเริ่มเซสชันใหม่ หรือ /restart ค่ะ');
|
|
1560
1595
|
// Restart ONCE to clear the wedged process the first time the ladder is
|
|
@@ -1567,7 +1602,7 @@ class AgentRunner extends events_1.EventEmitter {
|
|
|
1567
1602
|
}
|
|
1568
1603
|
this.tooLargeRecoveries.set(mapKey, count);
|
|
1569
1604
|
this.logger.warn('Request too large (32MB) — restarting with reduced history', {
|
|
1570
|
-
mapKey, attempt: count, nextHistoryLimit:
|
|
1605
|
+
mapKey, attempt: count, nextHistoryLimit: this.spawnHistoryLimit(configuredMax, count),
|
|
1571
1606
|
});
|
|
1572
1607
|
// Ordering matters and makes the notice delivery race-free: writeAutoForward
|
|
1573
1608
|
// persists the `.forward` file synchronously HERE, before restartProcess()
|