minicode-ai 0.21.0 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/cli/commands/exec.ts +44 -1
- package/cli/commands.ts +15 -2
- package/cli/index.ts +11 -0
- package/cli/setup.ts +88 -33
- package/cli/tui.ts +3 -3
- package/docs/TERMINAL_CONTRACT.md +69 -13
- package/docs/architecture.md +9 -5
- package/docs/concepts.md +1 -1
- package/docs/contributing.md +2 -1
- package/docs/environment.md +4 -0
- package/docs/policy-sandbox.md +6 -1
- package/docs/security.md +12 -4
- package/docs/skills.md +1 -1
- package/package.json +2 -2
- package/src/app/session.ts +129 -20
- package/src/config.ts +108 -8
- package/src/constants.ts +29 -4
- package/src/daemon/discovery.ts +24 -0
- package/src/policy/bash-guard.ts +170 -6
- package/src/policy/compaction.ts +61 -7
- package/src/policy/context-budget.ts +132 -0
- package/src/policy/context-escalation.ts +140 -0
- package/src/policy/context-window.ts +47 -9
- package/src/policy/context.ts +10 -1
- package/src/policy/jail.ts +10 -22
- package/src/policy/microcompact.ts +162 -0
- package/src/policy/permission.ts +9 -6
- package/src/presentation/projection.ts +1 -1
- package/src/providers/anthropic.ts +31 -7
- package/src/providers/detect.ts +128 -13
- package/src/providers/provision.ts +78 -1
- package/src/providers/responses.ts +14 -2
- package/src/providers/sse-bounds.ts +16 -2
- package/src/session/persistence.ts +56 -0
- package/src/ui/approval/prompt.ts +17 -6
- package/src/ui/assistant/simple.ts +1 -1
- package/src/ui/footer.ts +2 -2
- package/src/ui/i18n/en.ts +28 -5
- package/src/ui/i18n/id.ts +30 -5
- package/src/ui/input/input.ts +2 -2
- package/src/ui/input/prompt-engine.ts +6 -0
- package/src/ui/render/activity-panel.ts +21 -3
- package/src/ui/render/diff.ts +51 -2
- package/src/ui/render/highlight.ts +68 -37
- package/src/ui/render/hint.ts +28 -0
- package/src/ui/render/logo.ts +91 -0
- package/src/ui/render/sanitize.ts +5 -1
- package/src/ui/render/session-summary.ts +28 -5
- package/src/ui/render/table-grid.ts +123 -13
- package/src/ui/render/table.ts +4 -2
- package/src/ui/render/theme.ts +16 -3
- package/src/ui/render/width.ts +35 -6
- package/src/ui/render/wrap.ts +28 -6
- package/src/ui/runtime/loading-bar.ts +17 -5
- package/src/ui/runtime/screen.ts +4 -2
- package/src/ui/screens/dialog.ts +23 -2
- package/src/ui/screens/form.ts +71 -33
- package/src/ui/screens/lifecycle.ts +80 -0
- package/src/ui/screens/model-manager.ts +85 -83
- package/src/ui/screens/picker.ts +39 -25
- package/src/ui/screens/provider-manager.ts +58 -55
- package/src/ui/screens/watchdog.ts +27 -0
- package/src/ui/screens/wizard.ts +12 -1
- package/src/ui/tui/app.ts +62 -22
- package/src/ui/tui/transcript.ts +93 -23
- package/vendor/minicore/src/core/compact.ts +10 -0
- package/vendor/minicore/src/core/errors.ts +11 -1
- package/vendor/minicore/src/core/executor.ts +6 -1
- package/vendor/minicore/src/core/loop.ts +92 -2
- package/vendor/minicore/src/core/session.ts +8 -0
- package/vendor/minicore/src/providers/openai-compat.ts +45 -3
package/src/policy/bash-guard.ts
CHANGED
|
@@ -97,6 +97,70 @@ export function stripCaretEscapes(cmd: string): string {
|
|
|
97
97
|
return out
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
+
/**
|
|
101
|
+
* Ekspansi `$IFS` / `${IFS...}` menjadi pemisah kata.
|
|
102
|
+
*
|
|
103
|
+
* Bash memecah kata pada karakter di `$IFS` (default: spasi/tab/newline),
|
|
104
|
+
* sehingga `cat${IFS}.env` dieksekusi PERSIS seperti `cat .env` — tapi tanpa
|
|
105
|
+
* normalisasi guard hanya melihat satu kata `cat${IFS}.env` dan tak pernah
|
|
106
|
+
* mencocokkan READERS/SENSITIVE_TARGET. Bentuk `$IFS`, `${IFS}` dan
|
|
107
|
+
* `${IFS:0:1}` semuanya menghasilkan pemisah, jadi semuanya dipetakan ke
|
|
108
|
+
* spasi. Diterapkan dua kali dalam pipeline (sebelum dan sesudah quote
|
|
109
|
+
* dibuang) karena `${I"F"S}` juga sah di bash.
|
|
110
|
+
*
|
|
111
|
+
* Diekspor untuk test.
|
|
112
|
+
*/
|
|
113
|
+
export function expandIfsRefs(cmd: string): string {
|
|
114
|
+
return cmd.replace(/\$\{\s*IFS\b[^}]*\}/g, " ").replace(/\$IFS\b/g, " ")
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Ekspansi brace `{a,b}` → semua alternatifnya, sebagai kata-kata terpisah.
|
|
119
|
+
*
|
|
120
|
+
* Brace expansion terjadi SEBELUM perintah dijalankan: `cat .{env,}` dan
|
|
121
|
+
* `cat {.env,x}` membaca `.env`. Karena hasilnya beberapa kata, untuk
|
|
122
|
+
* pemeriksaan alternatif digabung sebagai kata terpisah sehingga aturan
|
|
123
|
+
* berbasis nama (`SENSITIVE_TARGET`) melihat setiap kandidat nyata.
|
|
124
|
+
*
|
|
125
|
+
* Hanya grup yang MENGANDUNG koma yang dianggap ekspansi: `awk '{print $1}'`
|
|
126
|
+
* (tanpa koma) adalah argumen biasa dan tidak boleh ikut dipecah — itu
|
|
127
|
+
* mencegah over-block pada pola awk/sed yang lazim. Kedalaman dan jumlah
|
|
128
|
+
* alternatif dibatasi karena ini heuristik deteksi, bukan evaluator shell.
|
|
129
|
+
*/
|
|
130
|
+
function expandWordBraces(w: string, depth = 0): string[] {
|
|
131
|
+
if (depth > 4 || w.length > 512) return [w]
|
|
132
|
+
const m = /\{([^{}]*)\}/.exec(w)
|
|
133
|
+
if (!m) return [w]
|
|
134
|
+
const body = m[1]!
|
|
135
|
+
if (!body.includes(",")) return [w]
|
|
136
|
+
const pre = w.slice(0, m.index)
|
|
137
|
+
const post = w.slice(m.index + m[0].length)
|
|
138
|
+
const out: string[] = []
|
|
139
|
+
for (const alt of body.split(",")) {
|
|
140
|
+
for (const r of expandWordBraces(pre + alt + post, depth + 1)) {
|
|
141
|
+
out.push(r)
|
|
142
|
+
if (out.length >= 64) return out
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return out
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Diekspor untuk test.
|
|
150
|
+
*
|
|
151
|
+
* Tanpa grup berkoma, perintah dikembalikan APA ADANYA: pemecahan kata di
|
|
152
|
+
* sini mengganti tab/newline dengan spasi, dan kontrak normalisasi menuntut
|
|
153
|
+
* makna (termasuk "perintah ini mencari dari cwd") tetap terbaca —
|
|
154
|
+
* `C=find\t/; ${C} -name x` harus tetap terlihat sebagai tab, bukan `find /`.
|
|
155
|
+
*/
|
|
156
|
+
export function expandBraceAlternatives(cmd: string): string {
|
|
157
|
+
if (!/\{[^{}]*,[^{}]*\}/.test(cmd)) return cmd
|
|
158
|
+
return cmd
|
|
159
|
+
.split(/\s+/)
|
|
160
|
+
.flatMap((w) => (w.includes("{") ? expandWordBraces(w) : [w]))
|
|
161
|
+
.join(" ")
|
|
162
|
+
}
|
|
163
|
+
|
|
100
164
|
/**
|
|
101
165
|
* Substitusi assignment variabel sederhana dalam satu perintah.
|
|
102
166
|
* `X=.env; cat $X` → `X=.env; cat .env`
|
|
@@ -205,16 +269,89 @@ export function normalizeCommand(cmd: string): string {
|
|
|
205
269
|
// [SEC-1] Gabungkan backslash line-continuation dulu agar perintah multi-baris
|
|
206
270
|
// (`curl ... \\\n -d $KEY`) tidak membuat regex satu-baris `[^\n]*` meleset.
|
|
207
271
|
let c = cmd.replace(/\\\r?\n/g, " ")
|
|
272
|
+
// [SEC-3] `$IFS`/`${IFS…}` adalah PEMISAH KATA bagi shell: tanpa ini
|
|
273
|
+
// `cat${IFS}.env` lolos seluruh aturan berbasis nama perintah.
|
|
274
|
+
c = expandIfsRefs(c)
|
|
208
275
|
// [SEC-2] Dekode ANSI-C quoting sebelum quote dibuang.
|
|
209
276
|
c = decodeAnsiCQuotes(c)
|
|
210
|
-
|
|
277
|
+
// IFS dan brace diekspansi sekali lagi setelah quote dibuang: `${I"F"S}`
|
|
278
|
+
// dan `".{env,}"` sama-sama sah di bash.
|
|
279
|
+
return stripCommandWrappers(
|
|
280
|
+
inlineSimpleVars(expandBraceAlternatives(expandIfsRefs(stripQuotes(stripCaretEscapes(c))))),
|
|
281
|
+
)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Kosongkan isi kutip yang merupakan DATA untuk perintah pencetak/pencari.
|
|
286
|
+
*
|
|
287
|
+
* `echo "rm -rf /"` dan `git commit -m "docs: sudo rm"` tidak mengeksekusi
|
|
288
|
+
* apa pun, tetapi karena normalisasi membuang kutip keduanya terlihat persis
|
|
289
|
+
* seperti perintah berbahaya dan diblokir (over-block yang mengganggu alur
|
|
290
|
+
* dokumentasi/commit). Argumen kutip milik echo/printf/grep/rg dan
|
|
291
|
+
* `-m`/`--message` git adalah teks, jadi isinya dijadikan spasi SEBELUM aturan
|
|
292
|
+
* statis dievaluasi.
|
|
293
|
+
*
|
|
294
|
+
* Pengaman penting: kutip yang memuat substitusi/ekspansi (`$(`, backtick,
|
|
295
|
+
* `${`, `$VAR`) TIDAK dikosongkan — `git commit -m "$(rm -rf /)"` benar-benar
|
|
296
|
+
* mengeksekusi perintah di dalamnya, jadi teks itu bukan data inert.
|
|
297
|
+
*
|
|
298
|
+
* Blanking HANYA dipakai untuk aturan statis (STATIC_DENY/RM_*); pemeriksaan
|
|
299
|
+
* target berkas (SENSITIVE_TARGET, READERS, redirect, jail) tetap memakai
|
|
300
|
+
* bentuk ternormalisasi penuh.
|
|
301
|
+
*
|
|
302
|
+
* Diekspor untuk test.
|
|
303
|
+
*/
|
|
304
|
+
export function blankQuotedDataArgs(cmd: string): string {
|
|
305
|
+
const INERT_HEADS = /^(?:echo|printf|grep|egrep|fgrep|rg|ag)$/i
|
|
306
|
+
const out = cmd.split("")
|
|
307
|
+
let i = 0
|
|
308
|
+
// Awal segmen perintah terakhir: head ditentukan dari sini, bukan dari awal
|
|
309
|
+
// seluruh baris (`git status && echo "x"` → head segmen kedua = echo).
|
|
310
|
+
let segStart = 0
|
|
311
|
+
while (i < cmd.length) {
|
|
312
|
+
const ch = cmd[i]!
|
|
313
|
+
if (ch === "\\" && i + 1 < cmd.length) {
|
|
314
|
+
i += 2
|
|
315
|
+
continue
|
|
316
|
+
}
|
|
317
|
+
if (ch === ";" || ch === "&" || ch === "|" || ch === "\n") {
|
|
318
|
+
segStart = i + 1
|
|
319
|
+
i++
|
|
320
|
+
continue
|
|
321
|
+
}
|
|
322
|
+
if (ch === "'" || ch === '"') {
|
|
323
|
+
const closer = ch
|
|
324
|
+
let j = i + 1
|
|
325
|
+
while (j < cmd.length && cmd[j] !== closer) {
|
|
326
|
+
if (closer === '"' && cmd[j] === "\\" && j + 1 < cmd.length) j++
|
|
327
|
+
if (cmd[j] === "\n") break
|
|
328
|
+
j++
|
|
329
|
+
}
|
|
330
|
+
const closed = j < cmd.length && cmd[j] === closer
|
|
331
|
+
const body = cmd.slice(i + 1, j)
|
|
332
|
+
if (closed && body !== "") {
|
|
333
|
+
const words = cmd.slice(segStart, i).trim().split(/\s+/).filter(Boolean)
|
|
334
|
+
const head = words[0] ?? ""
|
|
335
|
+
const prev = words[words.length - 1] ?? ""
|
|
336
|
+
const inertTarget =
|
|
337
|
+
prev !== head && (/^--message$/i.test(prev) || /^-[a-zA-Z]{0,2}m$/.test(prev))
|
|
338
|
+
const isData =
|
|
339
|
+
(INERT_HEADS.test(head) || inertTarget) && !/\$\(|`|\$\{|\$[A-Za-z_]/.test(body)
|
|
340
|
+
if (isData) for (let k = i + 1; k < j; k++) out[k] = " "
|
|
341
|
+
}
|
|
342
|
+
i = closed ? j + 1 : j
|
|
343
|
+
continue
|
|
344
|
+
}
|
|
345
|
+
i++
|
|
346
|
+
}
|
|
347
|
+
return out.join("")
|
|
211
348
|
}
|
|
212
349
|
|
|
213
350
|
// ── Aturan ──
|
|
214
351
|
|
|
215
352
|
/** Path/berkas kredensial, dicek pada bentuk ternormalisasi. */
|
|
216
353
|
const SENSITIVE_TARGET =
|
|
217
|
-
/(?:^|[\s/\\=@"'([:<>])(?:\.env(?:\.[\w.-]+)?|\.git-credentials|\.npmrc|\.netrc|id_(?:rsa|dsa|ecdsa|ed25519)|credentials(?:\.json)?|\.pem|\.p12|\.pfx|shadow|master\.key|config[/\\](?:sam|system|security|software|default)(?=[/\\]|$|[\s;"'&|])|ntds\.dit\b|hklm[/\\](?:sam|system|security)(?=[/\\]|$|[\s;"'&|]))\b/i
|
|
354
|
+
/(?:^|[\s/\\=@"'([:<>{,])(?:\.env(?:\.[\w.-]+)?|\.git-credentials|\.npmrc|\.netrc|id_(?:rsa|dsa|ecdsa|ed25519)|credentials(?:\.json)?|\.pem|\.p12|\.pfx|shadow|master\.key|config[/\\](?:sam|system|security|software|default)(?=[/\\]|$|[\s;"'&|])|ntds\.dit\b|hklm[/\\](?:sam|system|security)(?=[/\\]|$|[\s;"'&|]))\b/i
|
|
218
355
|
|
|
219
356
|
/** Direktori kredensial: ~/.ssh, $HOME/.aws, /etc/shadow, dst. */
|
|
220
357
|
const SENSITIVE_DIR = /(?:~|\$HOME|\$\{HOME\}|\/etc|\/root|\/proc\/self)[/\\](?:\.?[\w.-]+)/i
|
|
@@ -452,8 +589,11 @@ const ROOT_SCAN = /\b(?:find|fd|ls|dir|du|tree|grep|rg)\b[^\n]*\s\/(?:\s|$)/i
|
|
|
452
589
|
* -r menghapus isi dotfile root.
|
|
453
590
|
*/
|
|
454
591
|
const RM_RECURSIVE = /\brm\b[^\n]*(?:\s-[a-z]*[rR]|\s--recursive\b|\s--dir\b)/i
|
|
592
|
+
// Terminator target root ikut mencakup `)`, `,` dan backtick: bentuk
|
|
593
|
+
// bersubstitusi (`rm -rf $(pwd)`) berakhir dengan `)` sehingga `\s\/(?:\s|…)`
|
|
594
|
+
// dulu meleset dan seluruh perintah lolos.
|
|
455
595
|
const RM_DANGEROUS_TARGET =
|
|
456
|
-
/(?:\s\/(?:\s
|
|
596
|
+
/(?:\s\/(?:\s|$|\*|;|&|\/|\.|\)|,|`)|\s~(?:[/\\]\s*)?(?:\s|$|;|&|\))|\$\{?HOME\}?|\.\.(?:[/\\]|\s|$|;|&)|\s\*\s*(?:$|;|&)|--no-preserve-root)/
|
|
457
597
|
|
|
458
598
|
const STATIC_DENY: [RegExp, string][] = [
|
|
459
599
|
// Fork bomb: definisi fungsi rekursif yang memanggil dirinya lewat pipe.
|
|
@@ -553,11 +693,17 @@ export function inspectBashCommand(rawCmd: string, cwd?: string): BashVerdict {
|
|
|
553
693
|
const raw = rawCmd
|
|
554
694
|
const norm = normalizeCommand(rawCmd)
|
|
555
695
|
const both = (re: RegExp): boolean => re.test(norm) || re.test(raw)
|
|
696
|
+
// Aturan statis dievaluasi pada bentuk yang teks-kutip-inert-nya sudah
|
|
697
|
+
// dikosongkan (blankQuotedDataArgs), sehingga `echo "rm -rf /"` tidak lagi
|
|
698
|
+
// diblokir sementara `rm -rf /` dan `bash -c "rm -rf /"` tetap.
|
|
699
|
+
const inert = blankQuotedDataArgs(raw)
|
|
700
|
+
const inertNorm = normalizeCommand(inert)
|
|
701
|
+
const staticHit = (re: RegExp): boolean => re.test(inertNorm) || re.test(inert)
|
|
556
702
|
|
|
557
703
|
for (const [re, reason] of STATIC_DENY) {
|
|
558
|
-
if (
|
|
704
|
+
if (staticHit(re)) return { denied: true, reason }
|
|
559
705
|
}
|
|
560
|
-
if (
|
|
706
|
+
if (staticHit(RM_RECURSIVE) && staticHit(RM_DANGEROUS_TARGET)) {
|
|
561
707
|
return { denied: true, reason: "destructive rm" }
|
|
562
708
|
}
|
|
563
709
|
if (both(PROCESS_SUB)) return { denied: true, reason: "process substitution" }
|
|
@@ -721,6 +867,16 @@ export function inspectBashCommand(rawCmd: string, cwd?: string): BashVerdict {
|
|
|
721
867
|
|
|
722
868
|
// Berkas sensitif: berbahaya bila dibaca/disalin ATAU dijadikan argumen
|
|
723
869
|
// perintah jaringan. Menyebut `.env` dalam `echo` saja tidak diblokir.
|
|
870
|
+
// Pembaca yang argumennya langsung substitusi perintah (`cat$(printf ' ')`)
|
|
871
|
+
// menyembunyikan nama berkas sepenuhnya dari analisis statis.
|
|
872
|
+
if (
|
|
873
|
+
/\b(?:cat|bat|less|more|head|tail|nl|od|xxd|strings|type|Get-Content|gc|cp|copy|mv|move|tar|base64|openssl|awk|sed|grep|egrep|fgrep|rg|findstr)\b\s*(?:\$\(|`)/i.test(
|
|
874
|
+
inertNorm,
|
|
875
|
+
)
|
|
876
|
+
) {
|
|
877
|
+
return { denied: true, reason: "reader command substitution" }
|
|
878
|
+
}
|
|
879
|
+
|
|
724
880
|
const touchesSensitive = both(SENSITIVE_TARGET) || both(SENSITIVE_DIR)
|
|
725
881
|
if (touchesSensitive && (both(READERS) || /\b(?:curl|wget|nc|ncat|socat|scp)\b/i.test(norm))) {
|
|
726
882
|
return { denied: true, reason: "sensitive file access" }
|
|
@@ -773,12 +929,20 @@ export function inspectBashCommand(rawCmd: string, cwd?: string): BashVerdict {
|
|
|
773
929
|
// `%` tunggal (`100%.txt`) tetap lolos karena bukan pola ekspansi.
|
|
774
930
|
if (/%[^%\s]+%/.test(t))
|
|
775
931
|
return { denied: true, reason: "reader target with env expansion (%VAR%)" }
|
|
776
|
-
|
|
932
|
+
// Glob `?` dan `[abc]` sama-sama mengembang ke nama lain: `cat .?nv`
|
|
933
|
+
// membaca `.env` walau tak ada `*`. Hanya diperiksa untuk token yang
|
|
934
|
+
// berbentuk path (berawalan `.`/`~`/`/`, drive letter, atau memuat
|
|
935
|
+
// pemisah path) supaya pola grep/awk seperti `a[0-9]` tidak terblokir.
|
|
936
|
+
const pathLike = /^[.~/]|^[A-Za-z]:[\\/]|[/\\]/.test(t)
|
|
937
|
+
if (t.includes("*") || (pathLike && /[*?[]/.test(t))) {
|
|
777
938
|
// Wildcard di READER bisa mengembang ke .env/.minicode — tolak bila
|
|
778
939
|
// cwd tersedia (butuh path eksplisit). Tanpa cwd, heuristik sensitif.
|
|
779
940
|
if (cwd != null) return { denied: true, reason: "reader wildcard" }
|
|
780
941
|
if (isSensitive(t) || isOwnedState(t)) return { denied: true, reason: "reader wildcard" }
|
|
781
942
|
}
|
|
943
|
+
// Nama target yang dihitung shell tak bisa dipastikan statis:
|
|
944
|
+
// `cat$(printf ' ')` membaca `.env` tanpa pernah menampakkan nama itu.
|
|
945
|
+
if (/^\$\(|^`/.test(t)) return { denied: true, reason: "reader command substitution" }
|
|
782
946
|
if (isSensitive(t) || isSensitive(resolve(cwd ?? ".", t))) {
|
|
783
947
|
return { denied: true, reason: "sensitive file access" }
|
|
784
948
|
}
|
package/src/policy/compaction.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { createOpenAICompatProvider } from "#minicore/providers/openai-compat.ts
|
|
|
6
6
|
import { LIMITS } from "../constants.ts"
|
|
7
7
|
import { isPrivateHostWithDns } from "../lib/net.ts"
|
|
8
8
|
import { minicodeEstimator } from "./context.ts"
|
|
9
|
+
import { applyMicrocompact } from "./microcompact.ts"
|
|
9
10
|
import { scrubSecrets } from "./scrub.ts"
|
|
10
11
|
|
|
11
12
|
/** F3.2: parse MINICODE_COMPACT_KEEP_TURNS (murni, diekspor untuk test).
|
|
@@ -280,9 +281,14 @@ function boundCompactionResult(
|
|
|
280
281
|
store: ContextStore,
|
|
281
282
|
result: readonly import("#minicore/core/types.ts").Message[],
|
|
282
283
|
keepRecentTurns: number,
|
|
283
|
-
targetTokens
|
|
284
|
+
targetTokens: number | undefined,
|
|
285
|
+
maxSummaryChars: number,
|
|
284
286
|
): readonly import("#minicore/core/types.ts").Message[] {
|
|
285
|
-
|
|
287
|
+
// R9 — pagu ringkasan mengikuti jendela (summaryCharBudgetForWindow), bukan
|
|
288
|
+
// 4k konstan. Tanpa ini ringkasan LLM untuk model berjendela raksasa
|
|
289
|
+
// (1M token) yang SUDAH diproduksi dengan budget skala 32k dipangkas ulang
|
|
290
|
+
// ke COMPACTION_SUMMARY_MAX_CHARS, sehingga fakta lama over-discard.
|
|
291
|
+
let bounded = boundSummary(result, maxSummaryChars)
|
|
286
292
|
if (targetTokens !== undefined && estimateMessages(bounded, minicodeEstimator) > targetTokens) {
|
|
287
293
|
bounded = boundedMechanical(store, Math.min(1, keepRecentTurns), targetTokens)
|
|
288
294
|
}
|
|
@@ -290,6 +296,10 @@ function boundCompactionResult(
|
|
|
290
296
|
}
|
|
291
297
|
|
|
292
298
|
export function createLlmCompaction(opts: LlmCompactionOptions = {}): CompactionStrategy {
|
|
299
|
+
// Kill-switch: audit yang butuh output tool verbatim sepanjang sesi bisa
|
|
300
|
+
// mematikan lapisan ini. Default aktif — tanpa microcompact, payload sesi
|
|
301
|
+
// panjang dikuasai hasil tool lama yang sudah tak relevan.
|
|
302
|
+
const microcompactEnabled = (process.env.MINICODE_MICROCOMPACT ?? "") !== "0"
|
|
293
303
|
const fallback = opts.fallback ?? {
|
|
294
304
|
kind: "bounded-mechanical",
|
|
295
305
|
compact(store: ContextStore, cOpts: { keepRecentTurns: number }) {
|
|
@@ -307,6 +317,9 @@ export function createLlmCompaction(opts: LlmCompactionOptions = {}): Compaction
|
|
|
307
317
|
? Math.floor((limit as number) * LIMITS.COMPACTION_SUMMARY_TARGET_RATIO)
|
|
308
318
|
: undefined
|
|
309
319
|
}
|
|
320
|
+
// R9 — pagu karakter ringkasan mengikuti jendela (satu sumber, dipakai juga
|
|
321
|
+
// oleh boundCompactionResult agar ringkasan LLM tidak di-clamp balik ke 4k).
|
|
322
|
+
const summaryChars = () => summaryCharBudgetForWindow(opts.getContextWindowTokens?.())
|
|
310
323
|
// F3.1 anti-thrash (state per strategi = per sesi, karena strategy dibuat
|
|
311
324
|
// sekali di setup): kernel memicu kompaksi budget sekali PER TURN, jadi
|
|
312
325
|
// turn panjang dengan jendela penuh-kembali langsung (= satu output raksasa)
|
|
@@ -326,6 +339,28 @@ export function createLlmCompaction(opts: LlmCompactionOptions = {}): Compaction
|
|
|
326
339
|
): readonly import("#minicore/core/types.ts").Message[] {
|
|
327
340
|
return fallback.compact(store, cOpts)
|
|
328
341
|
},
|
|
342
|
+
/**
|
|
343
|
+
* Lapisan termurah: pangkas ISI hasil tool lama, tanpa LLM, tanpa membuang
|
|
344
|
+
* pesan. Dijalankan kernel sebelum kompaksi penuh; bila cukup menurunkan
|
|
345
|
+
* token, ringkasan mahal tak perlu dibuat sama sekali.
|
|
346
|
+
*
|
|
347
|
+
* `MINICODE_MICROCOMPACT=0` mematikannya (mis. saat audit butuh seluruh
|
|
348
|
+
* output tool verbatim sepanjang sesi).
|
|
349
|
+
*/
|
|
350
|
+
microcompact(
|
|
351
|
+
store: ContextStore,
|
|
352
|
+
mOpts: { budgetTokens?: number },
|
|
353
|
+
): readonly import("#minicore/core/types.ts").Message[] {
|
|
354
|
+
if (!microcompactEnabled) return store.messages
|
|
355
|
+
const result = applyMicrocompact(store.messages, {
|
|
356
|
+
keepRecent: LIMITS.MICROCOMPACT_KEEP_RECENT,
|
|
357
|
+
minSavingTokens: LIMITS.MICROCOMPACT_MIN_SAVING_TOKENS,
|
|
358
|
+
minToolShare: LIMITS.MICROCOMPACT_MIN_TOOL_SHARE,
|
|
359
|
+
estimateTokens: (text) => minicodeEstimator(text),
|
|
360
|
+
budgetTokens: mOpts.budgetTokens,
|
|
361
|
+
})
|
|
362
|
+
return result.clearedCount > 0 ? result.messages : store.messages
|
|
363
|
+
},
|
|
329
364
|
async compactAsync(
|
|
330
365
|
store: ContextStore,
|
|
331
366
|
cOpts: { keepRecentTurns: number },
|
|
@@ -384,6 +419,7 @@ export function createLlmCompaction(opts: LlmCompactionOptions = {}): Compaction
|
|
|
384
419
|
fallback.compact(store, { keepRecentTurns: 1 }),
|
|
385
420
|
1,
|
|
386
421
|
targetTokens(),
|
|
422
|
+
summaryChars(),
|
|
387
423
|
)
|
|
388
424
|
}
|
|
389
425
|
const progress = before > 0 ? (before - out.length) / before : 1
|
|
@@ -399,7 +435,13 @@ export function createLlmCompaction(opts: LlmCompactionOptions = {}): Compaction
|
|
|
399
435
|
)
|
|
400
436
|
}
|
|
401
437
|
}
|
|
402
|
-
return boundCompactionResult(
|
|
438
|
+
return boundCompactionResult(
|
|
439
|
+
store,
|
|
440
|
+
out,
|
|
441
|
+
cOpts.keepRecentTurns,
|
|
442
|
+
targetTokens(),
|
|
443
|
+
summaryChars(),
|
|
444
|
+
)
|
|
403
445
|
} finally {
|
|
404
446
|
clearTimeout(timer)
|
|
405
447
|
signal.removeEventListener("abort", onAbort)
|
|
@@ -646,11 +688,23 @@ export async function compactWithLlm(
|
|
|
646
688
|
: ""
|
|
647
689
|
return `- tool(${m.name}): ${head(raw, toolHeadLen(m.name, 250))}`
|
|
648
690
|
}
|
|
691
|
+
// F6 — pertahankan instruksi terkini verbatim saat active-turn fold. Jalur
|
|
692
|
+
// mekanikal (compactActiveTurn) menyimpan "Current task/request" mentah; jalur
|
|
693
|
+
// LLM merangkum seluruh prefix lewat summarizer sehingga request persis bisa
|
|
694
|
+
// terparafrase atau hilang — terutama saat satu turn didominasi output tool
|
|
695
|
+
// raksasa. Sertakan request terakhir verbatim (dengan pagu) di awal input
|
|
696
|
+
// summarizer supaya apa yang sebenarnya diminta user tidak menyusut.
|
|
697
|
+
const currentRequest = (() => {
|
|
698
|
+
if (!activeTurn) return ""
|
|
699
|
+
const lastUser = [...prefix].reverse().find((m) => m.role === "user")
|
|
700
|
+
const text = lastUser ? contentToText(lastUser.content).trim() : ""
|
|
701
|
+
return text ? `Current request (verbatim):\n${head(text, 1200)}\n\n` : ""
|
|
702
|
+
})()
|
|
649
703
|
const scrubbedPrefix = scrubSecrets(
|
|
650
|
-
prefix
|
|
651
|
-
|
|
652
|
-
.
|
|
653
|
-
|
|
704
|
+
(currentRequest + prefix.map(lineFor).join("\n")).slice(
|
|
705
|
+
0,
|
|
706
|
+
opts.maxInputChars ?? LIMITS.COMPACTION_LLM_INPUT_MIN_CHARS,
|
|
707
|
+
),
|
|
654
708
|
)
|
|
655
709
|
// Guard anti-injeksi (temuan audit #02): prefix berisi output tool/web/repo
|
|
656
710
|
// tak-terpercaya yang bisa memuat "abaikan instruksi". Tanpa pagar, payload
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { LIMITS } from "../constants.ts"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Anggaran konteks — satu sumber angka untuk reserve output, ambang kompaksi,
|
|
5
|
+
* dan ambang reklasifikasi error. Murni (tanpa IO/state modul) supaya bisa
|
|
6
|
+
* diuji langsung dan tak "membeku" saat import.
|
|
7
|
+
*
|
|
8
|
+
* KENAPA terpusat: reserve (`min(256, limit/10)`), ambang kompaksi, dan ambang
|
|
9
|
+
* reklasifikasi 400 (50.000 mutlak) dulu hidup terpisah dengan asumsi yang
|
|
10
|
+
* bertentangan — reserve 256 praktis nol untuk jendela 1M, sementara 50.000
|
|
11
|
+
* mustahil dicapai model berjendela di bawah 50k.
|
|
12
|
+
*
|
|
13
|
+
* KENAPA tidak per-model: rumus hanya memakai jendela + batas output yang
|
|
14
|
+
* DILAPORKAN endpoint. Semua model/provider mendapat anggaran proporsional sama.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** Batas atas reserve output. 20.000 = p99.99 ukuran ringkasan kompaksi di
|
|
18
|
+
* lapangan; reserve lebih besar hanya mengurangi jendela berguna. */
|
|
19
|
+
export const OUTPUT_RESERVE_MAX_TOKENS = 20_000
|
|
20
|
+
|
|
21
|
+
/** Lantai reserve output: model ber-`max_output_tokens` kecil (4k) tetap butuh
|
|
22
|
+
* ruang jawaban bermakna. Reserve 0 = request tepat di batas langsung ditolak. */
|
|
23
|
+
export const OUTPUT_RESERVE_MIN_TOKENS = 4_096
|
|
24
|
+
|
|
25
|
+
/** Dipakai saat `max_output_tokens` tidak dilaporkan endpoint. */
|
|
26
|
+
export const OUTPUT_RESERVE_DEFAULT_TOKENS = 20_000
|
|
27
|
+
|
|
28
|
+
/** Jarak ambang kompaksi dari effective window (lihat `compactBufferFor`
|
|
29
|
+
* untuk penyesuaian jendela kecil). */
|
|
30
|
+
export const COMPACT_BUFFER_TOKENS = 13_000
|
|
31
|
+
|
|
32
|
+
/** Buffer peringatan (warning) sebelum kompaksi, dan jarak blocking. */
|
|
33
|
+
export const COMPACT_WARNING_BUFFER_TOKENS = 20_000
|
|
34
|
+
export const COMPACT_BLOCKING_BUFFER_TOKENS = 3_000
|
|
35
|
+
|
|
36
|
+
/** Di bawah jendela ini buffer diskalakan: 13k dari 32k = 40% jendela hilang. */
|
|
37
|
+
export const SMALL_WINDOW_THRESHOLD_TOKENS = 64_000
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Reserve output untuk sebuah jendela konteks.
|
|
41
|
+
*
|
|
42
|
+
* Rumus: `clamp(max(4_096, min(maxOutput, 20_000)), lantai 2% jendela)`.
|
|
43
|
+
* Model yang melaporkan batas output kecil (4k) tetap mendapat reserve 4k;
|
|
44
|
+
* model besar mendapat 20k. Jendela raksasa tidak pernah mendapat reserve 0
|
|
45
|
+
* seperti perilaku lama `min(256, limit/10)`.
|
|
46
|
+
*/
|
|
47
|
+
export function outputReserveFor(windowTokens: number, maxOutputTokens?: number): number {
|
|
48
|
+
const window = Number.isFinite(windowTokens) && windowTokens > 0 ? windowTokens : 0
|
|
49
|
+
const reported =
|
|
50
|
+
typeof maxOutputTokens === "number" && Number.isFinite(maxOutputTokens) && maxOutputTokens > 0
|
|
51
|
+
? maxOutputTokens
|
|
52
|
+
: OUTPUT_RESERVE_DEFAULT_TOKENS
|
|
53
|
+
const wanted = Math.max(OUTPUT_RESERVE_MIN_TOKENS, Math.min(reported, OUTPUT_RESERVE_MAX_TOKENS))
|
|
54
|
+
// Lantai 2% jendela: jendela raksasa (1M) tak boleh reserve lebih kecil dari
|
|
55
|
+
// proporsi wajar; jendela kecil tak boleh reserve memakan seluruhnya.
|
|
56
|
+
const floor = Math.floor(window * 0.02)
|
|
57
|
+
// Reserve tak pernah boleh melebihi 40% jendela — di atas itu tak ada lagi
|
|
58
|
+
// ruang berguna untuk input dan kompaksi menjadi mustahil.
|
|
59
|
+
const ceiling = Math.floor(window * 0.4)
|
|
60
|
+
// Lantai 2% TIDAK BOLEH melewati plafon 20k: reserve lebih besar dari plafon
|
|
61
|
+
// hanya mengurangi jendela berguna tanpa manfaat (p99.99 ringkasan muat di
|
|
62
|
+
// bawah 20k), dan membuat anggaran bergantung pada ukuran jendela dengan cara
|
|
63
|
+
// yang tak diminta siapa pun.
|
|
64
|
+
const target = Math.max(wanted, Math.min(floor, OUTPUT_RESERVE_MAX_TOKENS))
|
|
65
|
+
return Math.max(
|
|
66
|
+
OUTPUT_RESERVE_MIN_TOKENS,
|
|
67
|
+
Math.min(target, Math.max(OUTPUT_RESERVE_MIN_TOKENS, ceiling)),
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Buffer kompaksi, diskalakan untuk jendela kecil. */
|
|
72
|
+
export function compactBufferFor(windowTokens: number): number {
|
|
73
|
+
const window = Number.isFinite(windowTokens) && windowTokens > 0 ? windowTokens : 0
|
|
74
|
+
if (window >= SMALL_WINDOW_THRESHOLD_TOKENS) return COMPACT_BUFFER_TOKENS
|
|
75
|
+
// Jendela kecil: buffer proporsional (20% effective window), tak pernah lebih
|
|
76
|
+
// dari buffer penuh.
|
|
77
|
+
return Math.min(COMPACT_BUFFER_TOKENS, Math.max(1_000, Math.floor(window * 0.2)))
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface ContextBudget {
|
|
81
|
+
readonly windowTokens: number
|
|
82
|
+
readonly outputReserve: number
|
|
83
|
+
/** Jendela yang benar-benar bisa dipakai untuk input + riwayat. */
|
|
84
|
+
readonly effectiveWindow: number
|
|
85
|
+
/** Ambang kompaksi: effectiveWindow - buffer. */
|
|
86
|
+
readonly compactTrigger: number
|
|
87
|
+
/** Ambang peringatan user. */
|
|
88
|
+
readonly warningLevel: number
|
|
89
|
+
/** Batas blokir: di atas ini request pasti ditolak provider. */
|
|
90
|
+
readonly blockingLimit: number
|
|
91
|
+
/** Ambang reklasifikasi 400 = min(50_000, window*0.5). */
|
|
92
|
+
readonly reclassifyThreshold: number
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Hitung seluruh angka anggaran dari satu jendela. Semua nilai non-negatif dan
|
|
97
|
+
* monotonik terhadap jendela, sehingga jendela lebih besar tidak pernah
|
|
98
|
+
* menghasilkan ambang kompaksi lebih kecil.
|
|
99
|
+
*/
|
|
100
|
+
export function contextBudgetFor(windowTokens: number, maxOutputTokens?: number): ContextBudget {
|
|
101
|
+
const window = Number.isFinite(windowTokens) && windowTokens > 0 ? windowTokens : 1
|
|
102
|
+
const outputReserve = outputReserveFor(window, maxOutputTokens)
|
|
103
|
+
const effectiveWindow = Math.max(0, window - outputReserve)
|
|
104
|
+
const buffer = compactBufferFor(window)
|
|
105
|
+
const warningBuffer = Math.min(COMPACT_WARNING_BUFFER_TOKENS, buffer + 7_000)
|
|
106
|
+
return {
|
|
107
|
+
windowTokens: window,
|
|
108
|
+
outputReserve,
|
|
109
|
+
effectiveWindow,
|
|
110
|
+
compactTrigger: Math.max(0, effectiveWindow - buffer),
|
|
111
|
+
warningLevel: Math.max(0, effectiveWindow - warningBuffer),
|
|
112
|
+
blockingLimit: Math.max(0, window - outputReserve - COMPACT_BLOCKING_BUFFER_TOKENS),
|
|
113
|
+
reclassifyThreshold: contextReclassifyThreshold(window),
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Ambang volume lokal untuk mereklasifikasi 400 generik sebagai kehabisan
|
|
119
|
+
* konteks.
|
|
120
|
+
*
|
|
121
|
+
* `min(CONTEXT_RECLASSIFY_400_MIN_LOCAL_TOKENS, window * ratio)`: jendela besar
|
|
122
|
+
* tetap memakai 50.000 (aman dari salah klasifikasi argumen tool yang panjang),
|
|
123
|
+
* sedangkan jendela kecil bisa mencapainya sebelum benar-benar penuh. Tanpa
|
|
124
|
+
* lantai proporsional, model 32k mustahil menempuh recovery.
|
|
125
|
+
*/
|
|
126
|
+
export function contextReclassifyThreshold(windowTokens: number): number {
|
|
127
|
+
const cap = LIMITS.CONTEXT_RECLASSIFY_400_MIN_LOCAL_TOKENS
|
|
128
|
+
const window = Number.isFinite(windowTokens) && windowTokens > 0 ? windowTokens : 0
|
|
129
|
+
const scaled = Math.floor(window * LIMITS.CONTEXT_RECLASSIFY_400_WINDOW_RATIO)
|
|
130
|
+
if (scaled <= 0) return cap
|
|
131
|
+
return Math.min(cap, scaled)
|
|
132
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Eskalasi adaptif jendela konteks.
|
|
3
|
+
*
|
|
4
|
+
* KENAPA: metadata bisa tak lengkap (endpoint tak melaporkan `context_length`,
|
|
5
|
+
* model terlalu baru untuk katalog). Tanpa jalur ini model berjendela besar
|
|
6
|
+
* tetap dipakai dengan tebakan kecil — sesi panjang mati padahal kapasitasnya
|
|
7
|
+
* berlipat.
|
|
8
|
+
*
|
|
9
|
+
* UNIVERSAL: tak pernah bercabang pada nama model, vendor, atau status
|
|
10
|
+
* berbayar. Semua request berbiaya, jadi anggaran probe berlaku untuk semua.
|
|
11
|
+
*
|
|
12
|
+
* KEAMANAN: probe HANYA dari error konteks terverifikasi — tak pernah
|
|
13
|
+
* 429/402/401/403/5xx/jaringan. `max_tokens: 1` + padding sintetis, bukan
|
|
14
|
+
* riwayat percakapan.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** Tangga eskalasi. Satu anak tangga per overflow terverifikasi. */
|
|
18
|
+
export const CONTEXT_WINDOW_LADDER: readonly number[] = [
|
|
19
|
+
131_072, 262_144, 524_288, 1_048_576, 2_097_152,
|
|
20
|
+
] as const
|
|
21
|
+
|
|
22
|
+
/** Batas atas absolut. Menutupi seluruh jendela model publik saat ini
|
|
23
|
+
* (terbesar 2M) dan aman untuk aritmetika bilangan apa pun. */
|
|
24
|
+
export const CONTEXT_WINDOW_ESCALATION_CEILING = 2_097_152
|
|
25
|
+
|
|
26
|
+
/** Anak tangga berikutnya di atas `current`, atau `undefined` bila sudah di
|
|
27
|
+
* batas atas. Tidak melompat: satu langkah per kejadian. */
|
|
28
|
+
export function nextLadderStep(current: number): number | undefined {
|
|
29
|
+
const cur = Number.isFinite(current) && current > 0 ? current : 0
|
|
30
|
+
for (const step of CONTEXT_WINDOW_LADDER) {
|
|
31
|
+
if (step > cur) return Math.min(step, CONTEXT_WINDOW_ESCALATION_CEILING)
|
|
32
|
+
}
|
|
33
|
+
return undefined
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Apakah nilai ini boleh dipakai sebagai jendela hasil probe. */
|
|
37
|
+
export function isValidWindow(tokens: unknown): tokens is number {
|
|
38
|
+
return (
|
|
39
|
+
typeof tokens === "number" &&
|
|
40
|
+
Number.isSafeInteger(tokens) &&
|
|
41
|
+
tokens > 0 &&
|
|
42
|
+
tokens <= CONTEXT_WINDOW_ESCALATION_CEILING
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Apakah error ini boleh MEMICU probe.
|
|
48
|
+
*
|
|
49
|
+
* Whitelist ketat: menaikkan jendela pada 429/402/401/403/5xx/jaringan adalah
|
|
50
|
+
* diagnosis salah yang membakar request pada kondisi yang butuh berhenti.
|
|
51
|
+
* Hanya kehabisan konteks = "jendela kita salah tebak".
|
|
52
|
+
*/
|
|
53
|
+
export function isContextOverflowError(error: unknown): boolean {
|
|
54
|
+
if (!error || typeof error !== "object") return false
|
|
55
|
+
const category = (error as { category?: unknown }).category
|
|
56
|
+
if (category === "context_length_exceeded") return true
|
|
57
|
+
const message = String((error as { message?: unknown }).message ?? "")
|
|
58
|
+
// Sebagian gateway hanya mengirim prosa. Pola sempit ini menangkap kasus
|
|
59
|
+
// umum tanpa menangkap rate-limit/kuota.
|
|
60
|
+
return /context[ _-]?(length|window)|maximum context|too many tokens|prompt is too long/i.test(
|
|
61
|
+
message,
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface ProbeBudgetState {
|
|
66
|
+
/** Jumlah probe per kunci `provider::model` dalam 24 jam. */
|
|
67
|
+
perModel: Record<string, { count: number; lastAt: number; rejectedUntil?: number }>
|
|
68
|
+
/** Jumlah probe sejak proses mulai. */
|
|
69
|
+
processCount: number
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface ProbeBudgetLimits {
|
|
73
|
+
perModelPerDay?: number
|
|
74
|
+
perProcess?: number
|
|
75
|
+
backoffMs?: number
|
|
76
|
+
now?: () => number
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const DAY_MS = 24 * 60 * 60 * 1000
|
|
80
|
+
const DEFAULT_BACKOFF_MS = 6 * 60 * 60 * 1000
|
|
81
|
+
|
|
82
|
+
export function emptyProbeBudget(): ProbeBudgetState {
|
|
83
|
+
return { perModel: {}, processCount: 0 }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Boleh probe sekarang? Tidak memutasi state (predikat murni). */
|
|
87
|
+
export function canProbe(
|
|
88
|
+
state: ProbeBudgetState,
|
|
89
|
+
key: string,
|
|
90
|
+
limits: ProbeBudgetLimits = {},
|
|
91
|
+
): { allowed: boolean; reason?: "kill-switch" | "per-model" | "per-process" | "backoff" } {
|
|
92
|
+
if (process.env.MINICODE_CONTEXT_PROBE === "0") return { allowed: false, reason: "kill-switch" }
|
|
93
|
+
const now = limits.now?.() ?? Date.now()
|
|
94
|
+
const perModelLimit = limits.perModelPerDay ?? 2
|
|
95
|
+
const perProcessLimit = limits.perProcess ?? 5
|
|
96
|
+
if (state.processCount >= perProcessLimit) return { allowed: false, reason: "per-process" }
|
|
97
|
+
const entry = state.perModel[key]
|
|
98
|
+
if (entry) {
|
|
99
|
+
if (entry.rejectedUntil !== undefined && now < entry.rejectedUntil)
|
|
100
|
+
return { allowed: false, reason: "backoff" }
|
|
101
|
+
// Jendela 24 jam bergulir: hitungan lama dianggap 0.
|
|
102
|
+
if (now - entry.lastAt < DAY_MS && entry.count >= perModelLimit)
|
|
103
|
+
return { allowed: false, reason: "per-model" }
|
|
104
|
+
}
|
|
105
|
+
return { allowed: true }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Catat satu probe. `rejected` = endpoint menolak (401/403/404) → backoff. */
|
|
109
|
+
export function recordProbe(
|
|
110
|
+
state: ProbeBudgetState,
|
|
111
|
+
key: string,
|
|
112
|
+
outcome: "attempt" | "rejected",
|
|
113
|
+
limits: ProbeBudgetLimits = {},
|
|
114
|
+
): ProbeBudgetState {
|
|
115
|
+
const now = limits.now?.() ?? Date.now()
|
|
116
|
+
const backoff = limits.backoffMs ?? DEFAULT_BACKOFF_MS
|
|
117
|
+
const prev = state.perModel[key]
|
|
118
|
+
const countInWindow = prev && now - prev.lastAt < DAY_MS ? prev.count : 0
|
|
119
|
+
const next: ProbeBudgetState = {
|
|
120
|
+
perModel: {
|
|
121
|
+
...state.perModel,
|
|
122
|
+
[key]: {
|
|
123
|
+
count: countInWindow + 1,
|
|
124
|
+
lastAt: now,
|
|
125
|
+
...(outcome === "rejected" ? { rejectedUntil: now + backoff } : {}),
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
processCount: state.processCount + 1,
|
|
129
|
+
}
|
|
130
|
+
return next
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Padding sintetis sepanjang `tokens` token (≈4 char/token). SENGAJA bukan
|
|
134
|
+
* riwayat asli: probe mengukur kapasitas endpoint, bukan isi sesi, dan riwayat
|
|
135
|
+
* asli membocorkan data sesi. */
|
|
136
|
+
export function syntheticPadding(tokens: number, charsPerToken = 4): string {
|
|
137
|
+
const chars = Math.max(0, Math.floor(tokens * charsPerToken))
|
|
138
|
+
const unit = "context-capacity-probe "
|
|
139
|
+
return unit.repeat(Math.ceil(chars / unit.length)).slice(0, chars)
|
|
140
|
+
}
|