Skip to content

Commit f475752

Browse files
committed
Merge remote-tracking branch 'origin/claude/issue-30770-loader-reach-held' into claude/project-thread-9464u3
# Conflicts: # packages/qa/scenarios/test/refusals.test.ts
2 parents c17719d + 82b1acd commit f475752

2 files changed

Lines changed: 73 additions & 2 deletions

File tree

‎packages/qa/scenarios/src/loader.ts‎

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import { readdirSync, readFileSync } from 'node:fs';
2121
import { basename, join } from 'node:path';
2222

23+
import { ErrorCode } from '@objectstack/spec/api';
24+
2325
import {
2426
GUARDRAIL_CHECKS,
2527
ScenarioCardSchema,
@@ -140,6 +142,22 @@ export function loadScenarioFile(
140142
}
141143

142144
type Refuse = (path: IssuePath, message: string) => void;
145+
type Persona = ScenarioCard['run']['personas'][number];
146+
147+
/**
148+
* The two codes a faithful build answers an operation its acting persona
149+
* cannot reach with. Parsed through the spec's catalogue (ADR-0112), so a code
150+
* the platform stops answering refuses here at load instead of drifting.
151+
*
152+
* NOT_REACHABLE_ROW_CODE a by-id write on a row the caller cannot READ is
153+
* answered not-found, indistinguishable from a
154+
* missing row, before any field or validation gate.
155+
* HIDDEN_WRITE_CODE a write on an object the caller must not read, or
156+
* to a field it must not read, is refused by the
157+
* security gate before the object's validation runs.
158+
*/
159+
const NOT_REACHABLE_ROW_CODE: ErrorCode = ErrorCode.parse('RECORD_NOT_FOUND');
160+
const HIDDEN_WRITE_CODE: ErrorCode = ErrorCode.parse('PERMISSION_DENIED');
143161

144162
function duplicates<T>(items: readonly T[], keyOf: (t: T) => string, at: IssuePath, what: string, refuse: Refuse) {
145163
const seen = new Set<string>();
@@ -308,6 +326,12 @@ function checkCard(
308326
return v === undefined ? null : v;
309327
};
310328

329+
/** Whether `p` sees `row` of `object`: every row-scope rule it declares on that object holds. */
330+
const inScope = (p: Persona, object: string, row: SeedRow): boolean => {
331+
const resolve = resolverFor(object);
332+
return (p.scope ?? []).filter((sc) => sc.object === object).every((sc) => resolve(row, sc.field) === sc.value);
333+
};
334+
311335
// ── personas: hidden refs and row scope resolve ──
312336
duplicates(run.personas, (p) => p.id, ['run', 'personas'], 'persona id', refuse);
313337
const personas = new Set(run.personas.map((p) => p.id));
@@ -389,8 +413,7 @@ function checkCard(
389413
personaTruths[q.id][id] = { kind: 'withheld' };
390414
return;
391415
}
392-
const rules = (p.scope ?? []).filter((sc) => sc.object === q.object);
393-
const scoped = rows.filter((r) => rules.every((sc) => ctx.resolve(r, sc.field) === sc.value));
416+
const scoped = rows.filter((r) => inScope(p, q.object, r));
394417
const truth = evaluate(scoped, [...at, 'personas', j]);
395418
if (truth) personaTruths[q.id][id] = truth;
396419
});
@@ -402,6 +425,33 @@ function checkCard(
402425
if (!run.operations.some((o) => o.kind === 'create' || o.kind === 'update')) {
403426
refuse(['run', 'operations'], 'needs at least one create or update through the app');
404427
}
428+
// Reach: a persona is a requirement of the app (`hidden` = must not read,
429+
// `scope` = sees only those rows), so an operation its acting persona cannot
430+
// reach has exactly one outcome a faithful build gives, decided from the card
431+
// alone — the object hidden, then the target row out of scope (the row gate
432+
// runs before the field gate), then a written field hidden. Declaring anything
433+
// else (`applied`, `pending_confirmation`, another refusal code) is the card
434+
// contradicting its own persona: graded against a faithful build it fails, and
435+
// it passes only a build that leaks what the persona must not see.
436+
const checkReach = (op: ScenarioCard['run']['operations'][number], actor: Persona, at: IssuePath) => {
437+
const row = op.target === undefined ? undefined : rowById.get(op.object)?.get(op.target);
438+
const hiddenField = Object.keys(op.values ?? {}).find((field) =>
439+
(actor.hidden.fields ?? []).some((f) => f.object === op.object && f.field === field),
440+
);
441+
let want: { code: ErrorCode; why: string } | undefined;
442+
if ((actor.hidden.objects ?? []).includes(op.object)) {
443+
want = { code: HIDDEN_WRITE_CODE, why: `\`${actor.id}\` must not read \`${op.object}\`` };
444+
} else if (row !== undefined && !unscoped.has(actor.id) && !inScope(actor, op.object, row)) {
445+
want = { code: NOT_REACHABLE_ROW_CODE, why: `row \`${op.target}\` is outside \`${actor.id}\`'s row scope on \`${op.object}\`` };
446+
} else if (hiddenField !== undefined) {
447+
want = { code: HIDDEN_WRITE_CODE, why: `\`${actor.id}\` must not read \`${op.object}.${hiddenField}\`, which this ${op.kind} writes` };
448+
}
449+
if (want === undefined || (op.outcome.kind === 'refused' && op.outcome.code === want.code)) return;
450+
refuse(
451+
[...at, 'outcome', ...(op.outcome.kind === 'refused' ? ['code'] : [])],
452+
`${want.why}, so a faithful build refuses this ${op.kind} with \`${want.code}\` — declare that refusal, or change the target, the values or \`actingAs\` so the persona can reach what the operation writes`,
453+
);
454+
};
405455
run.operations.forEach((op, i) => {
406456
const at: IssuePath = ['run', 'operations', i];
407457
if (!objects.has(op.object) || seed[op.object] === undefined) {
@@ -416,6 +466,8 @@ function checkCard(
416466
checkRef(op.object, field, value, [...at, 'values', field]);
417467
}
418468
if (!personas.has(op.actingAs)) refuse([...at, 'actingAs'], `no persona \`${op.actingAs}\``);
469+
const actor = run.personas.find((p) => p.id === op.actingAs);
470+
if (actor !== undefined) checkReach(op, actor, at);
419471
const out = op.outcome;
420472
if (out.kind !== 'applied') return;
421473
if (out.auditActor !== undefined && !personas.has(out.auditActor)) {

‎packages/qa/scenarios/test/refusals.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { dirname, join, resolve } from 'node:path';
1313
import { fileURLToPath } from 'node:url';
1414
import { afterAll, describe, expect, it } from 'vitest';
1515

16+
import { ErrorCode } from '@objectstack/spec/api';
17+
1618
import { formatRefusal, loadScenarios, readCapabilityAreas } from '../src/index.js';
1719

1820
const HERE = dirname(fileURLToPath(import.meta.url));
@@ -83,6 +85,12 @@ const CASES: Array<[string, (c: RawCard) => void, string, RegExp]> = [
8385
['a refusal code outside the platform error-code catalogue', (c) => (op(c, 'contact_bad_email').outcome.code = 'VALIDATION_BROKEN'), 'run.operations[3].outcome.code', /expected one of/],
8486
['a post-state naming a created row by an id the seed lacks', (c) => (op(c, 'add_contact').outcome.state[0] = { object: 'contact', id: 'ct9', values: { full_name: 'Fay Wu' } }), 'run.operations[0].outcome.state[0].id', /matched by `where`/],
8587
['a post-state naming its row twice', (c) => (op(c, 'move_eli').outcome.state[0].where = { full_name: 'Dee' }), 'run.operations[1].outcome.state[0].id', /exactly one of `id` or `where`/],
88+
// ── reach: an operation its acting persona cannot reach has one outcome ──
89+
['an applied create writing a field the persona must not read', (c) => (op(c, 'add_contact').values.email = 'fay@bitworks.test'), 'run.operations[0].outcome', /`sdr` must not read `contact.email`.*`PERMISSION_DENIED`/],
90+
['an applied update on a row outside the persona\'s row scope', (c) => (op(c, 'move_eli').target = 'ct4'), 'run.operations[1].outcome', /row `ct4` is outside `sdr`'s row scope on `contact`.*`RECORD_NOT_FOUND`/],
91+
['a pending delete on a row outside the persona\'s row scope', (c) => (op(c, 'remove_ben').target = 'ct3'), 'run.operations[2].outcome', /row `ct3` is outside `sdr`'s row scope.*`RECORD_NOT_FOUND`/],
92+
['a validation refusal on a field the persona must not read', (c) => (op(c, 'contact_bad_email').actingAs = 'sdr'), 'run.operations[3].outcome.code', /`sdr` must not read `contact.email`.*`PERMISSION_DENIED`/],
93+
['a refusal code other than the hidden object\'s', (c) => (op(c, 'partner_edits_account').outcome.code = ErrorCode.parse('RECORD_NOT_FOUND')), 'run.operations[4].outcome.code', /`partner_user` must not read `account`.*`PERMISSION_DENIED`/],
8694
// ── guardrails ──
8795
['a card that drops one of the five guardrail checks', (c) => (c.run.guardrails = c.run.guardrails.filter((g: { check: string }) => g.check !== 'audit_actor')), 'run.guardrails', /5/],
8896
['an injection guardrail pointing at a non-text cell', (c) => (c.run.guardrails[4].cell = { object: 'contact', id: 'ct4', field: 'account' }), 'run.guardrails[4].cell.field', /not a text cell/],
@@ -110,6 +118,17 @@ describe('scenario card refusals', () => {
110118
expect(r.refusals).toEqual([]);
111119
});
112120

121+
it('an out-of-scope update declared as the refusal a faithful build answers loads clean (control for reach)', () => {
122+
const r = load((c) => {
123+
const o = op(c, 'move_eli');
124+
o.target = 'ct4';
125+
o.outcome = { kind: 'refused', code: ErrorCode.parse('RECORD_NOT_FOUND') };
126+
c.run.guardrails[3].operation = 'add_contact';
127+
op(c, 'add_contact').outcome.auditActor = 'sdr';
128+
});
129+
expect(r.refusals.map(formatRefusal)).toEqual([]);
130+
});
131+
113132
for (const [name, mutate, path, message] of CASES) {
114133
it(`refuses ${name}`, () => {
115134
const r = load(mutate);

0 commit comments

Comments
 (0)