char-slop/berry

multipurpose self-modifiable agentic harness

git clone https://git.t4t.associates/char-slop/berry

Charlotte Sommisc: disable the context cap by defaulta988092

main
3.2 KiB85 linesraw
1import type { Command, CompactionCap, ExtensionApi, JsonValue } from "@berry/api";
2
3/** Blocking compaction at this threshold unless `/context-cap` changes it. */
4const DEFAULT_MAX_CONTEXT_TOKENS = 2 ** 18;
5/** Background compaction this far below the blocking threshold by default. */
6const DEFAULT_BACKGROUND_TOKENS = 32_768;
7
8const K = 1024;
9const M = 1024 * 1024;
10
11// server-wide, so every conversation compacts at one threshold; a reload restores the defaults
12let enabled = false;
13let maxContextTokens = DEFAULT_MAX_CONTEXT_TOKENS;
14let backgroundTokens = DEFAULT_BACKGROUND_TOKENS;
15
16/** A token count as an integer with an optional `k` or `m` suffix, so 2^18 reads `256k`. */
17function parseTokens(text: string): number | undefined {
18  const match = /^(\d+(?:\.\d+)?)\s*([kKmM])?$/.exec(text.trim());
19  if (!match) return undefined;
20  const scale = match[2] === undefined ? 1 : match[2].toLowerCase() === "m" ? M : K;
21  const value = Math.floor(Number(match[1]) * scale);
22  return Number.isFinite(value) && value > 0 ? value : undefined;
23}
24
25function status(): string {
26  if (!enabled) return "context cap off: compaction follows each model's own window";
27  const backgroundAt = maxContextTokens - backgroundTokens;
28  return `context cap: blocking compaction at ${maxContextTokens} tokens, ` +
29    `background at ${backgroundAt}; models with smaller windows keep their own policy`;
30}
31
32/** Cap effective context windows for this load; tune it with the context-cap command. */
33export function registerContextCap<S extends JsonValue, E extends JsonValue>(
34  ext: ExtensionApi<S, E>,
35): void {
36  ext.compactionCap((): CompactionCap | undefined =>
37    enabled ? { maxContextTokens, backgroundTokens } : undefined
38  );
39}
40
41/** Show or tune the context cap: `/context-cap [tokens] [background]`, `on`, or `off`. */
42export function contextCapCommand(): Command<JsonValue, JsonValue> {
43  return {
44    name: "context-cap",
45    description:
46      "show or tune the context cap: `/context-cap [tokens] [background]`, `on`, or `off`",
47    execute: (args) => {
48      const parts = args.trim().split(/\s+/).filter((part) => part !== "");
49      if (parts.length > 2) throw new Error("expected at most a threshold and a background offset");
50      const [first = "", second = ""] = parts;
51      if (first === "") return status();
52      if (first === "off") {
53        enabled = false;
54        return status();
55      }
56      if (first === "on") {
57        enabled = true;
58        return status();
59      }
60      const max = parseTokens(first);
61      if (max === undefined) {
62        throw new Error(`expected a token count like 262144 or 256k, got ${JSON.stringify(first)}`);
63      }
64      let background = backgroundTokens;
65      if (second !== "") {
66        const parsed = parseTokens(second);
67        if (parsed === undefined) {
68          throw new Error(
69            `expected a token count like 32768 or 32k, got ${JSON.stringify(second)}`,
70          );
71        }
72        background = parsed;
73      }
74      if (background >= max) {
75        throw new Error(
76          `background offset ${background} must stay below the ${max}-token threshold`,
77        );
78      }
79      maxContextTokens = max;
80      backgroundTokens = background;
81      enabled = true;
82      return status();
83    },
84  };
85}