model/context: per-model ctx window overrides + expose window size in /api/state

This commit is contained in:
damocles 2026-05-20 15:20:07 +02:00 committed by Mara
parent 9064cd3c57
commit 770cbaccf9
3 changed files with 68 additions and 23 deletions

View file

@ -37,20 +37,28 @@
};
options.hyperhive.contextWindowTokens = lib.mkOption {
type = lib.types.int;
default = 0;
example = 1000000;
type = lib.types.attrsOf lib.types.int;
default = { };
example = {
haiku = 150000;
sonnet = 900000;
};
description = ''
Context-window size in tokens for this agent's model. `0` (the
default) means "auto-derive from the model name": haiku 200 000,
sonnet / opus 1 000 000. Set an explicit value here when you are
using a model the harness does not recognise, or when Anthropic
changes the window for an existing model family.
Per-model context-window overrides. Each attribute name is a
model-family short name (e.g. `"haiku"`, `"sonnet"`) matched as a
case-insensitive substring of the active model name at runtime, so
`"sonnet"` matches `"claude-sonnet-4-5"` and any future variant.
Empty map (the default) means auto-derive: haiku 200 000,
sonnet / opus 1 000 000.
Sets the `HIVE_CONTEXT_WINDOW_TOKENS` environment variable; the
harness reads it at runtime and uses it to compute the default
compaction and auto-reset watermarks (75% and 50% of the window
respectively).
Each entry is rendered as
`HIVE_CONTEXT_WINDOW_TOKENS_<KEY_UPPER>` (e.g.
`HIVE_CONTEXT_WINDOW_TOKENS_SONNET = "900000"`). The harness checks
these per-model vars first, then the global
`HIVE_CONTEXT_WINDOW_TOKENS`, then the model-family heuristic.
At runtime, the effective window drives compaction (75%) and
auto-reset (50%) watermarks, and is exposed via `/api/state` as
`context_window_tokens`.
'';
};
@ -249,13 +257,23 @@
# Model + context-window env vars consumed by the harness at boot.
# HIVE_DEFAULT_MODEL seeds the initial model selection when no persisted
# model choice exists in the state dir. HIVE_CONTEXT_WINDOW_TOKENS
# overrides the auto-derived window size (only set when the NixOS option
# is non-zero so an unset env var lets the harness use its own heuristic).
environment.variables.HIVE_DEFAULT_MODEL = config.hyperhive.model;
environment.variables = lib.mkIf (config.hyperhive.contextWindowTokens != 0) {
HIVE_CONTEXT_WINDOW_TOKENS = toString config.hyperhive.contextWindowTokens;
};
# model choice exists in the state dir.
# HIVE_CONTEXT_WINDOW_TOKENS_<KEY_UPPER> provides per-model overrides
# (e.g. HIVE_CONTEXT_WINDOW_TOKENS_SONNET) from contextWindowTokens attrset.
# SHELL must be set so claude's Bash tool finds a POSIX shell.
environment.variables = lib.mkMerge (
[
{
HIVE_DEFAULT_MODEL = config.hyperhive.model;
SHELL = "${pkgs.bashInteractive}/bin/bash";
}
]
++ lib.mapAttrsToList
(model: tokens: {
"HIVE_CONTEXT_WINDOW_TOKENS_${lib.toUpper model}" = toString tokens;
})
config.hyperhive.contextWindowTokens
);
boot.isNspawnContainer = true;
@ -356,9 +374,6 @@
};
};
# claude's Bash tool refuses to run without a POSIX shell + $SHELL set.
environment.variables.SHELL = "${pkgs.bashInteractive}/bin/bash";
system.stateVersion = "25.11";
};
}