fix(llmrec): context size telemetry ON by default

The llm_context table carries the per-call context breakdown (system, tools,
tool_result, assistant) but was EMPTY on a stand that had already run 15 tasks:
the flag was opt-in and off. Context size was therefore discussed from per-task
token sums, and any fix to it was a guess.

limits: telemetry row 1 per model call - byte counting only, no content kept;
наблюдаемо до ~300 вызовов на прогон, то есть до ~300 строк
limits: disable via explicit falsy value only (0|false|off|no) - the point of the
change is that an unset env means ON

Tests: default-on and explicit-off are both asserted (RED first: the old test
asserted the opposite default).
This commit is contained in:
Николай Гусев
2026-11-23 17:22:35 +03:00
parent a3e44d3f05
commit b708ea6b9a
2 changed files with 26 additions and 11 deletions
+13 -8
View File
@@ -10,19 +10,24 @@ import (
"github.com/Autumn-27/norma/llm"
)
// ContextTelemetryEnv is the opt-in environment flag enabling per-call context
// size telemetry into llm_context. Off by default: when unset/false, no sizing
// work runs and no rows are written.
// ContextTelemetryEnv switches per-call context size telemetry into llm_context.
//
// ON by default. Замер: на стенде, где прошло 15 прогонов, таблица llm_context была
// ПУСТА (0 строк), потому что флаг выключен по умолчанию — то есть размер контекста
// воркера обсуждали по суммарным токенам задачи, а не по разбивке (2026-11-22).
// Стоимость: одна строка на вызов модели и подсчёт длин, содержимое не сохраняется
// (по логам стенда — до ~300 вызовов на прогон, то есть до ~300 строк).
// Выключение явное: ARTEX_CONTEXT_TELEMETRY=0|false|off|no.
const ContextTelemetryEnv = "ARTEX_CONTEXT_TELEMETRY"
// ContextTelemetryEnabled reports whether ARTEX_CONTEXT_TELEMETRY is enabled
// (1, true, on, yes; case-insensitive). Default is false.
// ContextTelemetryEnabled reports whether context telemetry is on. Default true;
// only an explicit falsy value (0, false, off, no; case-insensitive) turns it off.
func ContextTelemetryEnabled() bool {
switch strings.ToLower(strings.TrimSpace(os.Getenv(ContextTelemetryEnv))) {
case "1", "true", "on", "yes":
return true
case "0", "false", "off", "no":
return false
}
return false
return true
}
// ContextMetrics holds size-only measurements of an assembled CompletionRequest
+13 -3
View File
@@ -233,11 +233,21 @@ func TestContextTelemetryFlagOffAndOnPostgres(t *testing.T) {
wantMetrics := MeasureContext(req)
wantUsage := llm.Usage{InputTokens: 1200, OutputTokens: 150, CacheReadTokens: 800, CacheWriteTokens: 100}
// 1. Flag OFF (default): zero rows in llm_context on both Stream and Complete,
// identical behavior regardless of recordBodies.
// 0. Default is ON: unset means enabled, only an explicit falsy value disables.
t.Setenv(ContextTelemetryEnv, "")
if !ContextTelemetryEnabled() {
t.Fatal("expected telemetry enabled by default when env unset")
}
t.Setenv(ContextTelemetryEnv, "off")
if ContextTelemetryEnabled() {
t.Fatal("expected telemetry disabled when env unset")
t.Fatal("expected telemetry disabled when ARTEX_CONTEXT_TELEMETRY=off")
}
// 1. Flag OFF explicitly: zero rows in llm_context on both Stream and Complete,
// identical behavior regardless of recordBodies.
t.Setenv(ContextTelemetryEnv, "0")
if ContextTelemetryEnabled() {
t.Fatal("expected telemetry disabled when ARTEX_CONTEXT_TELEMETRY=0")
}
recOff := Wrap(syntheticTelemetryProvider{usage: wantUsage}, pg, "claude-test", "prof-a", "", "", func() bool { return false })
ctxOff := transcript.WithSessionID(WithTaskID(context.Background(), taskOff), "exp1-worker-i1")