Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

feat(coach): make the output-token ceiling an admin setting

danifuuu62baad1549ef
diff --git a/api/coach/config.js b/api/coach/config.js
index 1ff692f2a..364d63173 100644
--- a/api/coach/config.js
+++ b/api/coach/config.js
@@ -27,6 +27,7 @@ import crypto from 'node:crypto';
import fs from 'node:fs';
import path from 'node:path';
import { HTTP_PROVIDERS, baseUrlFor } from './core/providers.js';
+import { MAX_OUTPUT_TOKENS } from './core/adapters/http.js';
const DATA = process.env.DATA_DIR || '/data';
const FILE = path.join(DATA, 'coach.json');
@@ -91,10 +92,18 @@ const DEFAULTS = {
// payload.js's own MAX_NOTE_CHARS ceiling has to stay at or above the latter, or a raised
// limit here would still get clipped back down at build().
maxMessageLen: 1000,
+ // The ceiling on one provider answer. A reasoning model — a DeepSeek V3/R1-class endpoint, or
+ // a "thinking" Gemini/OpenAI model — counts its hidden reasoning against this, so a plan can be
+ // cut off at the old 16000 constant even though the plan itself is short (one report spent
+ // ~12k of the 16k thinking). The admin route clamps a write to [floor, ceiling]; the adapter's
+ // own MAX_OUTPUT_TOKENS is the fallback for callers that configure nothing (the phone, warmup).
+ maxOutputTokens: MAX_OUTPUT_TOKENS,
log: []
};
export const MAX_MESSAGE_LEN_FLOOR = 200;
export const MAX_MESSAGE_LEN_CEILING = 4000;
+export const MAX_OUTPUT_TOKENS_FLOOR = 1024;
+export const MAX_OUTPUT_TOKENS_CEILING = 65536;
const PER_PROVIDER = ['auth', 'models', 'providerOptions', 'boundUid'];
const isPlainObj = v => !!v && typeof v === 'object' && !Array.isArray(v);
const LOG_MAX = 100;
diff --git a/api/coach/core/adapters/http.js b/api/coach/core/adapters/http.js
index 76c84d0fe..ad96fbd2a 100644
--- a/api/coach/core/adapters/http.js
+++ b/api/coach/core/adapters/http.js
@@ -119,7 +119,7 @@ export function httpAdapter(spec) {
* native HTTP and a test can hand in a fake.
*/
async invoke(opts = {}) {
- const { cfg, prompt, system, schema, env, model, timeoutMs = DEFAULT_TIMEOUT_MS, fetch: fetchImpl = globalThis.fetch, signal } = opts;
+ const { cfg, prompt, system, schema, env, model, timeoutMs = DEFAULT_TIMEOUT_MS, maxTokens = MAX_OUTPUT_TOKENS, fetch: fetchImpl = globalThis.fetch, signal } = opts;
const base = adapter.baseUrl(cfg);
if (!base) return { code: -1, text: '', stderr: `no endpoint configured for ${id}`, spawnError: true };
const key = keyOf(env);
@@ -127,7 +127,7 @@ export function httpAdapter(spec) {
const chosen = model || meta.defaultModel;
if (!chosen) return { code: 1, text: '', stderr: `no model chosen for ${id} — pick one from the list the endpoint serves` };
- let body = spec.body({ model: chosen, prompt, system: system || null, schema: schema || null, maxTokens: MAX_OUTPUT_TOKENS });
+ let body = spec.body({ model: chosen, prompt, system: system || null, schema: schema || null, maxTokens });
let retriedWithoutJsonMode = false;
let transientRetries = 0;
for (;;) {
diff --git a/api/coach/jobs.js b/api/coach/jobs.js
index cb4797a52..16091e524 100644
--- a/api/coach/jobs.js
+++ b/api/coach/jobs.js
@@ -358,7 +358,7 @@ async function execute(job) {
adapter, cfg, kind: job.kind, payload, model: cfgStore.modelFor(cfg), timeoutMs: TIMEOUT_MS,
// The HTTP adapters take the fetch and the abort signal they are given; the runtime
// adapters ignore both.
- invokeOpts: { jobDir, env, fetch: fetchFor(TIMEOUT_MS), signal: ctl.signal }
+ invokeOpts: { jobDir, env, fetch: fetchFor(TIMEOUT_MS), signal: ctl.signal, maxTokens: cfg.maxOutputTokens }
});
if (!attempt.ok) {
// Cancelled by a forget, not failed by the provider: the log must not blame the job budget.
diff --git a/api/coach/routes.js b/api/coach/routes.js
index bcf992b59..b8067e26a 100644
--- a/api/coach/routes.js
+++ b/api/coach/routes.js
@@ -166,6 +166,7 @@ export function coachRoutes({ json, readBody, readSession, requireAdmin }) {
knownModels: check.models || null,
caps: cfg.caps,
maxMessageLen: cfg.maxMessageLen,
+ maxOutputTokens: cfg.maxOutputTokens,
community: !!cfg.community,
runtime: { ok: !!check.ok, version: check.version || null, error: check.error || null, needsKey: !!check.needsKey },
authMode: cfg.authMode,
@@ -238,6 +239,12 @@ export function coachRoutes({ json, readBody, readSession, requireAdmin }) {
Math.min(cfgStore.MAX_MESSAGE_LEN_CEILING, +body.maxMessageLen || current.maxMessageLen)
);
}
+ if (body.maxOutputTokens !== undefined) {
+ patch.maxOutputTokens = Math.max(
+ cfgStore.MAX_OUTPUT_TOKENS_FLOOR,
+ Math.min(cfgStore.MAX_OUTPUT_TOKENS_CEILING, +body.maxOutputTokens || current.maxOutputTokens)
+ );
+ }
cfgStore.save(patch);
json(res, 200, { ok: true });
},
diff --git a/api/test/adapters-http.test.js b/api/test/adapters-http.test.js
index 05261579e..517c66c44 100644
--- a/api/test/adapters-http.test.js
+++ b/api/test/adapters-http.test.js
@@ -305,3 +305,25 @@ test('models(): OpenAI’s list is cut to what Chat Completions can use; a compa
const co = fakeFetch([ok({ data: [{ id: 'qwen2.5:3b' }, { id: 'llama3.2' }] })]);
assert.deepEqual((await compatible.models({ providerOptions: { compatible: { baseUrl: 'http://ollama:11434' } } }, {}, { fetch: co })).models, ['llama3.2', 'qwen2.5:3b']);
});
+
+test('invoke sends the configured maxTokens override, and the adapter default when none is given', async () => {
+ const { MAX_OUTPUT_TOKENS } = await import('../coach/core/adapters/http.js');
+
+ // No override: every caller that configures nothing (the phone, warmup) still gets the default.
+ const d = fakeFetch([ok({ choices: [{ message: { content: ANSWER }, finish_reason: 'stop' }] })]);
+ await compatible.invoke({ cfg: cfgCompat, prompt: 'P', env: {}, model: 'm', fetch: d });
+ assert.equal(d.calls[0].body.max_tokens, MAX_OUTPUT_TOKENS);
+
+ // An override, on each wire shape that carries the ceiling.
+ const o = fakeFetch([ok({ choices: [{ message: { content: ANSWER }, finish_reason: 'stop' }] })]);
+ await openai.invoke({ cfg: {}, prompt: 'P', env, model: 'gpt-x', fetch: o, maxTokens: 48000 });
+ assert.equal(o.calls[0].body.max_completion_tokens, 48000);
+
+ const a = fakeFetch([ok({ content: [{ type: 'text', text: ANSWER }], stop_reason: 'end_turn' })]);
+ await anthropic.invoke({ cfg: {}, prompt: 'P', env, fetch: a, maxTokens: 48000 });
+ assert.equal(a.calls[0].body.max_tokens, 48000);
+
+ const g = fakeFetch([ok({ candidates: [{ content: { parts: [{ text: ANSWER }] }, finishReason: 'STOP' }] })]);
+ await gemini.invoke({ cfg: {}, prompt: 'P', env, model: 'gemini-z', fetch: g, maxTokens: 48000 });
+ assert.equal(g.calls[0].body.generationConfig.maxOutputTokens, 48000);
+});
diff --git a/api/test/jobs.test.js b/api/test/jobs.test.js
index 806dbf28d..9eae8fc6b 100644
--- a/api/test/jobs.test.js
+++ b/api/test/jobs.test.js
@@ -540,3 +540,38 @@ test('a note is truncated to the admin\'s configured max, not clipped to the old
server.close();
}
});
+
+test('a job sends the admin\'s configured output cap, so a reasoning model is not cut off', async () => {
+ // Same shape as the note-length test: a local server on the Chat Completions wire, so the
+ // exact ceiling that reached the provider can be read back out of the request body.
+ const http = await import('node:http');
+ const seen = [];
+ const server = http.createServer((req, res) => {
+ let body = '';
+ req.on('data', c => { body += c; });
+ req.on('end', () => {
+ seen.push(JSON.parse(body || '{}'));
+ res.setHeader('content-type', 'application/json');
+ res.end(JSON.stringify({ choices: [{ finish_reason: 'stop', message: { content: '{"coach_contract":1,"nochange":true,"reading":"ok"}' } }] }));
+ });
+ });
+ await new Promise(r => server.listen(0, '127.0.0.1', r));
+ const base = `http://127.0.0.1:${server.address().port}`;
+
+ const uid = 'u-max-output';
+ writeState(DIR, uid, sampleState());
+ cfg.save({
+ enabled: true, provider: 'compatible', providerOptions: { compatible: { baseUrl: base } },
+ models: { compatible: 'local-model' }, maxOutputTokens: 48000
+ });
+ forcePrivilegeVerdict({ ok: false, dropped: false, why: 'no `coach` user exists in this image' });
+ try {
+ jobs.enqueue(uid, { kind: 'review' });
+ await settle(uid);
+ assert.equal(seen[0].max_tokens, 48000, 'the configured ceiling rode on the request');
+ } finally {
+ forcePrivilegeVerdict({ ok: true, dropped: false, why: 'pinned by the test suite' });
+ cfg.save({ provider: 'fixture', maxOutputTokens: 16000 });
+ server.close();
+ }
+});
diff --git a/api/test/routes.test.js b/api/test/routes.test.js
index 93d457214..308c40394 100644
--- a/api/test/routes.test.js
+++ b/api/test/routes.test.js
@@ -234,3 +234,23 @@ test('an admin can raise the Coach message length past the old 1000-char default
const status = await call('GET /api/coach/status');
assert.equal(status.body.maxMessageLen, 1800);
});
+
+/* ---------- configurable max output tokens (reasoning models) ---------- */
+test('an admin can raise the Coach output cap for a reasoning model, clamped to a sane range', async () => {
+ fresh();
+ const { call } = harness();
+
+ // The old hard-coded constant, for an instance that has never touched the setting.
+ assert.equal((await call('GET /api/admin/coach')).body.maxOutputTokens, 16000);
+
+ const set = await call('POST /api/admin/coach/config', { maxOutputTokens: 48000 });
+ assert.equal(set.status, 200);
+ assert.equal((await call('GET /api/admin/coach')).body.maxOutputTokens, 48000);
+ assert.equal(cfg.load().maxOutputTokens, 48000, 'persists with the rest of the Coach config');
+
+ // Clamped, not trusted outright — a reasoning model needs more than 16000, but not a million.
+ await call('POST /api/admin/coach/config', { maxOutputTokens: 999999 });
+ assert.equal((await call('GET /api/admin/coach')).body.maxOutputTokens, 65536, 'ceiling');
+ await call('POST /api/admin/coach/config', { maxOutputTokens: 1 });
+ assert.equal((await call('GET /api/admin/coach')).body.maxOutputTokens, 1024, 'floor');
+});
diff --git a/docs/AI_COACH.md b/docs/AI_COACH.md
index fed0ccbc3..75c59dfdb 100644
--- a/docs/AI_COACH.md
+++ b/docs/AI_COACH.md
@@ -127,6 +127,16 @@ that way:
Servers that reject schemas get plain JSON mode automatically, then no JSON mode — the
validator is the gate either way.
+#### When a plan comes back cut off
+
+Every answer has a ceiling on how much the provider may write, set on **Settings → Admin
+dashboard → AI Coach → Advanced → Max output tokens** (16000 by default). A reasoning model — a
+DeepSeek V3/R1-class endpoint, or a "thinking" Gemini or OpenAI model — counts its hidden
+reasoning against that same ceiling, so a `create` or `refine` can be cut off even though the
+plan itself is short. If a job fails with *"the answer was cut off at the output limit"*, raise
+it; keep it within the model's own output limit (65536 on most current hosted models, lower on a
+small local one).
+
### With a runtime in the container (Claude Agent SDK, Codex CLI)
**1. Build the image that has an AI runtime in it.** The default image deliberately has none:
diff --git a/frontend/src/views/AdminCoach.jsx b/frontend/src/views/AdminCoach.jsx
index 2adccd798..68bb11b36 100644
--- a/frontend/src/views/AdminCoach.jsx
+++ b/frontend/src/views/AdminCoach.jsx
@@ -272,6 +272,10 @@ export default function AdminCoach() {
<div className="adm-kv"><span className="k">Max message length</span>
<span className="v"><input className="num" type="number" min="200" max="4000" defaultValue={d.maxMessageLen} disabled={busy}
onBlur={e => +e.target.value !== d.maxMessageLen && patch({ maxMessageLen: +e.target.value })} /></span></div>
+ <div className="adm-hint" style={{ marginTop: 10 }}>The ceiling on a single provider answer. Reasoning models — DeepSeek-style endpoints, or a "thinking" Gemini/OpenAI model — count their hidden reasoning against it, so a plan can fail with "the answer was cut off at the output limit" even though the plan itself is short. Raise it if that happens; keep it within the model's own output limit.</div>
+ <div className="adm-kv"><span className="k">Max output tokens</span>
+ <span className="v"><input className="num" type="number" min="1024" max="65536" defaultValue={d.maxOutputTokens} disabled={busy}
+ onBlur={e => +e.target.value !== d.maxOutputTokens && patch({ maxOutputTokens: +e.target.value })} /></span></div>
<div className="adm-group-t" style={{ marginTop: 14 }}>Compare with others</div>
<div className="row between" style={{ gap: 12, alignItems: 'flex-start' }}>