Coach: der Kern, den beide Laufzeiten laden koennen
payload, validate, parse, die Prompt-Montage, der Plan-Fingerprint und die invoke→parse→validate→repair-Schleife ziehen nach api/coach/core/ — ohne node:*, damit das Handy sie spaeter genauso importiert wie der Server. Die Bibliothek und die Prompts werden als ES-Module generiert (scripts/build-coach-assets.mjs), weil weder createRequire noch ?raw unter beiden Laufzeiten laden. Das Pseudonym wird uebergeben statt abgeleitet (handle.js behaelt den HMAC). check-core-loadable prueft unter blankem node, was vitest strukturell nicht pruefen kann.
This commit is contained in:
1 parent
4a9e1e3456
commit
697f1b1269
27 files changed
+402
-204
No files matched your search
@@ -13,8 +13,8 @@ import { tempData, writeState, sampleState } from './helpers.mjs';
|
||||
const DIR = tempData();
|
||||
const cfg = await import('../coach/config.js');
|
||||
const jobs = await import('../coach/jobs.js');
|
||||
const payload = await import('../coach/payload.js');
|
||||
const { CHANGE_TYPES } = await import('../coach/validate.js');
|
||||
const payload = await import('../coach/core/payload.js');
|
||||
const { CHANGE_TYPES } = await import('../coach/core/validate.js');
|
||||
const { forcePrivilegeVerdict } = await import('../coach/adapters/spawn.js');
|
||||
|
||||
cfg.save({ enabled: true, provider: 'fixture' });
|
||||
|
||||
@@ -10,7 +10,7 @@ import assert from 'node:assert/strict';
|
||||
import { tempData } from './helpers.mjs';
|
||||
|
||||
tempData();
|
||||
const { extractJSON, contractOK } = await import('../coach/parse.js');
|
||||
const { extractJSON, contractOK } = await import('../coach/core/parse.js');
|
||||
|
||||
test('JSON is recovered from whatever the model wrapped it in', () => {
|
||||
assert.deepEqual(extractJSON('{"a":1}').value, { a: 1 });
|
||||
|
||||
+23
-11
@@ -3,7 +3,19 @@ import assert from 'node:assert/strict';
|
||||
import { tempData, sampleState } from './helpers.mjs';
|
||||
|
||||
tempData();
|
||||
const payload = await import('../coach/payload.js');
|
||||
const payload = await import('../coach/core/payload.js');
|
||||
const { handleFor, HANDLE_LENGTH } = await import('../coach/handle.js');
|
||||
|
||||
test('the server handle is stable per uid, distinct across uids, and never the uid', () => {
|
||||
assert.equal(handleFor('uid-a'), handleFor('uid-a'));
|
||||
assert.notEqual(handleFor('uid-a'), handleFor('uid-b'));
|
||||
assert.equal(handleFor('uid-a').length, HANDLE_LENGTH);
|
||||
assert.ok(!handleFor('uid-a').includes('uid-a'));
|
||||
});
|
||||
|
||||
test('build refuses to run without a handle — a payload must never fall back to the uid', () => {
|
||||
assert.throws(() => payload.build(sampleState(), { kind: 'review' }), /handle/);
|
||||
});
|
||||
|
||||
/* The promise the consent screen makes is only as good as this test. It asserts on the
|
||||
*absence* of things, which is the awkward direction to test and the only one that matters:
|
||||
@@ -16,7 +28,7 @@ test('payload never carries identity, credentials or device data', () => {
|
||||
reminder: { on: true, time: '08:00', tz: 'Europe/Lisbon' },
|
||||
_ts: Date.now()
|
||||
});
|
||||
const p = payload.build(S, 'user-abc-123', { kind: 'review' });
|
||||
const p = payload.build(S, { handle: handleFor('user-abc-123'), kind: 'review' });
|
||||
const json = JSON.stringify(p);
|
||||
|
||||
assert.ok(!json.includes('user-abc-123'), 'the uid must never appear');
|
||||
@@ -28,15 +40,15 @@ test('payload never carries identity, credentials or device data', () => {
|
||||
|
||||
test('the same profile always gets the same handle, and two profiles never share one', () => {
|
||||
const S = sampleState();
|
||||
const a1 = payload.build(S, 'uid-a', { kind: 'review' }).meta.profile;
|
||||
const a2 = payload.build(S, 'uid-a', { kind: 'review' }).meta.profile;
|
||||
const b = payload.build(S, 'uid-b', { kind: 'review' }).meta.profile;
|
||||
const a1 = payload.build(S, { handle: handleFor('uid-a'), kind: 'review' }).meta.profile;
|
||||
const a2 = payload.build(S, { handle: handleFor('uid-a'), kind: 'review' }).meta.profile;
|
||||
const b = payload.build(S, { handle: handleFor('uid-b'), kind: 'review' }).meta.profile;
|
||||
assert.equal(a1, a2);
|
||||
assert.notEqual(a1, b);
|
||||
});
|
||||
|
||||
test('review payload carries the plan, the window, effort and aggregates', () => {
|
||||
const p = payload.build(sampleState(), 'u1', { kind: 'review', note: 'shoulder pinches' });
|
||||
const p = payload.build(sampleState(), { handle: handleFor('u1'), kind: 'review', note: 'shoulder pinches' });
|
||||
assert.equal(p.task, 'review');
|
||||
assert.equal(p.plan.routines.length, 1);
|
||||
assert.equal(p.plan.routines[0].ex[0].name, '3/4 sit-up', 'exercise names are resolved for the model');
|
||||
@@ -57,7 +69,7 @@ test('a stalling exercise shows up in the aggregates the way the engine counts i
|
||||
sets: [{ w: 20, r: 9, done: true }, { w: 20, r: 8, done: true }, { w: 20, r: 7, done: true }]
|
||||
}]
|
||||
}));
|
||||
const p = payload.build(S, 'u1', { kind: 'review' });
|
||||
const p = payload.build(S, { handle: handleFor('u1'), kind: 'review' });
|
||||
const ex = p.aggregates.exercises.find(e => e.id === '0001');
|
||||
assert.equal(ex.stalls, 3, 'three misses in a row is a stall of three');
|
||||
assert.equal(ex.lastOk, false);
|
||||
@@ -71,7 +83,7 @@ test('a set that was never ticked off is a miss, not a gap', () => {
|
||||
sets: [{ w: 20, r: 10, done: true }, { w: 20, r: 10, done: true }, { w: 20, r: 10, done: false }]
|
||||
}]
|
||||
}];
|
||||
const p = payload.build(S, 'u1', { kind: 'review' });
|
||||
const p = payload.build(S, { handle: handleFor('u1'), kind: 'review' });
|
||||
assert.equal(p.aggregates.exercises.find(e => e.id === '0001').stalls, 1);
|
||||
});
|
||||
|
||||
@@ -81,7 +93,7 @@ test('the review window is bounded even for someone with years of history', () =
|
||||
const d = new Date(); d.setDate(d.getDate() - i);
|
||||
return { id: 'w' + i, d: d.toISOString().slice(0, 10), name: 'A', start: 0, end: 60000, entries: [] };
|
||||
}).reverse();
|
||||
const p = payload.build(S, 'u1', { kind: 'review' });
|
||||
const p = payload.build(S, { handle: handleFor('u1'), kind: 'review' });
|
||||
assert.ok(p.window.workouts.length <= payload.MAX_SESSIONS, 'session cap holds');
|
||||
const oldest = new Date(p.window.workouts[0].d);
|
||||
const limit = new Date(); limit.setDate(limit.getDate() - payload.MAX_WEEKS * 7 - 1);
|
||||
@@ -89,7 +101,7 @@ test('the review window is bounded even for someone with years of history', () =
|
||||
});
|
||||
|
||||
test('creation payload carries working weights so baselines start from evidence', () => {
|
||||
const p = payload.build(sampleState(), 'u1', { kind: 'create' });
|
||||
const p = payload.build(sampleState(), { handle: handleFor('u1'), kind: 'create' });
|
||||
assert.equal(p.task, 'create');
|
||||
assert.ok(!p.window, 'creation does not ship the training window');
|
||||
assert.equal(p.history.workingWeights.find(w => w.id === '0001').best, 20);
|
||||
@@ -112,7 +124,7 @@ test('equipment nobody in the library has still yields a usable library', () =>
|
||||
test('declined changes are carried forward so the Coach does not nag', () => {
|
||||
const S = sampleState();
|
||||
S.coach.log = [{ decisions: [{ status: 'rejected', type: 'sets', why: 'bench accessory volume -1 set' }] }];
|
||||
const p = payload.build(S, 'u1', { kind: 'review' });
|
||||
const p = payload.build(S, { handle: handleFor('u1'), kind: 'review' });
|
||||
assert.equal(p.previouslyDeclined.length, 1);
|
||||
assert.equal(p.previouslyDeclined[0].type, 'sets');
|
||||
});
|
||||
@@ -0,0 +1,39 @@
|
||||
/* The generated prompt module is what both runtimes read; the .md files are what people edit.
|
||||
* If they disagree, a prompt change silently never reaches the model. The generator's --check
|
||||
* makes the same assertion in CI; this one fails faster, inside the unit suite. */
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const { PROMPTS } = await import('../coach/core/prompts.js');
|
||||
const { buildPrompt } = await import('../coach/core/prompt.js');
|
||||
|
||||
const dir = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'coach', 'prompts');
|
||||
|
||||
test('core/prompts.js matches api/coach/prompts/*.md byte for byte', () => {
|
||||
const files = fs.readdirSync(dir).filter(f => f.endsWith('.md')).sort();
|
||||
assert.deepEqual(Object.keys(PROMPTS).sort(), files.map(f => f.replace(/\.md$/, '')));
|
||||
for (const f of files) {
|
||||
assert.equal(PROMPTS[f.replace(/\.md$/, '')], fs.readFileSync(path.join(dir, f), 'utf8'), `${f} is stale — run node scripts/build-coach-assets.mjs`);
|
||||
}
|
||||
});
|
||||
|
||||
test('buildPrompt picks the task by kind and refine, and only adds the repair block when asked', () => {
|
||||
const payload = { coach_contract: 1, plan: { routines: [], week: {} } };
|
||||
const review = buildPrompt('review', payload, null);
|
||||
assert.ok(review.startsWith(PROMPTS.common));
|
||||
assert.ok(review.includes(PROMPTS.review));
|
||||
assert.ok(!review.includes(PROMPTS.repair.slice(0, 40)));
|
||||
|
||||
const create = buildPrompt('create', payload, null);
|
||||
assert.ok(create.includes(PROMPTS.create) && !create.includes(PROMPTS.refine));
|
||||
const refine = buildPrompt('create', { ...payload, refine: { text: 'x' } }, null);
|
||||
assert.ok(refine.includes(PROMPTS.refine) && !refine.includes(PROMPTS.create));
|
||||
|
||||
const repaired = buildPrompt('review', payload, { previous: '{"bad": true}', errors: ['first', 'second'] });
|
||||
assert.ok(repaired.includes('{"bad": true}'));
|
||||
assert.ok(repaired.includes('- first\n- second'));
|
||||
assert.ok(!repaired.includes('{{PREVIOUS}}') && !repaired.includes('{{ERRORS}}'));
|
||||
});
|
||||
@@ -14,7 +14,7 @@ import assert from 'node:assert/strict';
|
||||
import { tempData } from './helpers.mjs';
|
||||
|
||||
tempData();
|
||||
const { validatePlan, validateReview, CHANGE_TYPES } = await import('../coach/validate.js');
|
||||
const { validatePlan, validateReview, CHANGE_TYPES } = await import('../coach/core/validate.js');
|
||||
|
||||
const PLAN = {
|
||||
routines: [{
|
||||
|
||||
Reference in new issue
Block a user