Skip to content

Commit e748dfb

Browse files
committed
Merge remote-tracking branch 'origin/main' into claude/project-thread-cin84b
2 parents 303a1ae + 26c965c commit e748dfb

7 files changed

Lines changed: 93 additions & 16 deletions

File tree

‎docs/qa/scenarios/invoice_with_line_items.json‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@
128128
"id": "add_line",
129129
"kind": "create",
130130
"object": "invoice_line",
131-
"ask": {"en": "Add 2 Widgets at 5 each to INV-003."},
132-
"values": {"invoice": "i3", "product": "Widget", "quantity": 2, "unit_price": 5, "amount": 10},
131+
"ask": {"en": "Add 2 Widgets to INV-003, 10 in total."},
132+
"values": {"invoice": "i3", "product": "Widget", "quantity": 2, "amount": 10},
133133
"actingAs": "sales_rep",
134134
"outcome": {
135135
"kind": "applied",
@@ -163,8 +163,8 @@
163163
"id": "line_without_product",
164164
"kind": "create",
165165
"object": "invoice_line",
166-
"ask": {"en": "Add a line of 2 at 5 to INV-001, no product."},
167-
"values": {"invoice": "i1", "quantity": 2, "unit_price": 5, "amount": 10},
166+
"ask": {"en": "Add a line of 2, 10 in total, to INV-001, no product."},
167+
"values": {"invoice": "i1", "quantity": 2, "amount": 10},
168168
"actingAs": "sales_rep",
169169
"outcome": {"kind": "refused", "code": "VALIDATION_FAILED"}
170170
},

‎docs/qa/scenarios/project_with_tasks.json‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@
111111
"id": "add_task",
112112
"kind": "create",
113113
"object": "task",
114-
"ask": {"en": "Add a 3-hour 'Launch checklist' task to the Website relaunch."},
115-
"values": {"title": "Launch checklist", "project": "p1", "status": "todo", "estimate_hours": 3, "assignee": "u_kim"},
114+
"ask": {"en": "Add a 'Launch checklist' task to the Website relaunch."},
115+
"values": {"title": "Launch checklist", "project": "p1", "status": "todo", "assignee": "u_kim"},
116116
"actingAs": "contractor",
117117
"outcome": {
118118
"kind": "applied",
@@ -146,8 +146,8 @@
146146
"id": "task_without_title",
147147
"kind": "create",
148148
"object": "task",
149-
"ask": {"en": "Add a 2-hour task to the Mobile app, with no title."},
150-
"values": {"project": "p2", "status": "todo", "estimate_hours": 2, "assignee": "u_kim"},
149+
"ask": {"en": "Add a task to the Mobile app, with no title."},
150+
"values": {"project": "p2", "status": "todo", "assignee": "u_kim"},
151151
"actingAs": "contractor",
152152
"outcome": {"kind": "refused", "code": "VALIDATION_FAILED"}
153153
},

‎docs/qa/scenarios/travel_expense.json‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"$comment": "Seed rule (so every truth can be worked by hand): claim i = 1..120 is filed by employee e((i-1) mod 12 + 1) (e01-e12; e13/e14 are Finance and file none), dated 2026-07..10 day ((i-1) mod 30)+1 (i 1-30 July, 31-60 August, 61-90 September, 91-120 October), for trip t(i) of (i mod 3)+2 days ending the day before. Lines per claim: hotel ((days-1) nights at cap-50, at cap+100 when i mod 8 = 0), meals (days at cap-20, at cap+30 when i mod 12 = 5), and airfare when i mod 3 = 0 (1 at cap-200, at cap+300 when i mod 9 = 0); an over-cap line is over_standard with a reason. Status: i 1-60 rejected when i mod 10 = 7 else paid; i 61-90 rejected when i mod 10 = 7, manager_approved when 8 or 9, else paid; i 91-120 by i mod 10: 1-4 submitted, 5-6 manager_approved, 7 rejected, 8 finance_approved, 9 and 0 draft. Approval step 1 goes to the claimant's department manager (to e14 when the claimant is that manager), step 2 to e13.",
2+
"$comment": "Seed rule (so every truth can be worked by hand): claim i = 1..120 is filed by employee e((i-1) mod 12 + 1) (e01-e12; e13/e14 are Finance and file none), dated 2026-07..10 day ((i-1) mod 30)+1 (i 1-30 July, 31-60 August, 61-90 September, 91-120 October), for trip t(i) of (i mod 3)+2 days ending the day before. Lines per claim: hotel ((days-1) nights at cap-50, at cap+100 when i mod 8 = 0), meals (days at cap-20, at cap+30 when i mod 12 = 5), and airfare when i mod 3 = 0 (1 at cap-200, at cap+300 when i mod 9 = 0); an over-cap line is over_standard with a reason. Status: i 1-60 rejected when i mod 10 = 7 else paid; i 61-90 rejected when i mod 10 = 7, manager_approved when 8 or 9, else paid; i 91-120 by i mod 10: 1-4 submitted, 5-6 manager_approved, 7 rejected, 8 finance_approved, 9 and 0 draft. Approval step 1 goes to the claimant's department manager (to e14 when the claimant is that manager), step 2 to e13. Operation employee_approves_own_claim checks that a claimant is never the approver: e02 holds no decision right on approval_step, so deciding the manager step of its own claim EC-0098 (as_098_1, already approved by e01) is refused PERMISSION_DENIED, the edit grant being judged before any validation or state rule; it targets a step e02 can read, so the agent finds the row it is asked to decide.",
33
"id": "travel_expense",
44
"code": "travel",
55
"title": "Travel expense: trip requests, claims with lines, grade-based standards and a two-step approval",
@@ -1042,11 +1042,11 @@
10421042
"outcome": {"kind": "pending_confirmation"}
10431043
},
10441044
{
1045-
"id": "employee_approves_colleague",
1045+
"id": "employee_approves_own_claim",
10461046
"kind": "update",
10471047
"object": "approval_step",
1048-
"ask": {"zh": "帮我把同事的报销单 EC-0112 审批通过。", "en": "Approve my colleague's claim EC-0112 for me."},
1049-
"target": "as_112_1",
1048+
"ask": {"zh": "帮我把我自己的报销单 EC-0098 的经理审批批了,日期写今天。", "en": "Approve the manager step of my own claim EC-0098 myself, dated today."},
1049+
"target": "as_098_1",
10501050
"values": {"decision": "approved", "decided_on": "2026-10-31"},
10511051
"actingAs": "employee",
10521052
"outcome": {"kind": "refused", "code": "PERMISSION_DENIED"}

‎packages/qa/scenarios/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ computed truths and declared outcomes:
4242
import { gradeCardAnswer, gradeCardOperation, scoreCardArtifact } from '@objectstack/scenarios/score/full';
4343

4444
scoreCardArtifact(card, artifact); // a built dist/objectstack.json: expect, tags, agent actions
45-
gradeCardOperation(card, 'remove_chen', observed); // an observed write against its declared outcome
45+
gradeCardOperation(card, 'remove_ben', observed); // an observed write against its declared outcome
4646
gradeCardAnswer(card, { question: 'account_count', persona: 'sdr', turn }); // an agent's answer
4747
```
4848

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

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import { readdirSync, readFileSync } from 'node:fs';
2626
import { basename, join } from 'node:path';
2727

28+
import { ErrorCode } from '@objectstack/spec/api';
29+
2830
import {
2931
AppCodeSchema,
3032
GUARDRAIL_CHECKS,
@@ -170,6 +172,28 @@ function loadOne(file: string, options: LoadOptions): { card?: LoadedScenario; r
170172
}
171173

172174
type Refuse = (path: IssuePath, message: string) => void;
175+
type Persona = ScenarioCard['run']['personas'][number];
176+
177+
/**
178+
* The two codes a faithful build answers an operation its acting persona
179+
* cannot reach with. Spelled as literals, because `check:dispatcher-error-vocabulary`
180+
* judges the `code` these feed into `want` below and reads a constant only when
181+
* it resolves to a literal. Held to the spec's catalogue (`ErrorCode`, ADR-0112)
182+
* twice: typed, so the typecheck refuses a code the platform stops answering,
183+
* and parsed once at load, so a test run (which does not typecheck) refuses it
184+
* too, instead of drifting.
185+
*
186+
* NOT_REACHABLE_ROW_CODE a by-id write on a row the caller cannot READ is
187+
* answered not-found, indistinguishable from a
188+
* missing row, before any field or validation gate.
189+
* HIDDEN_WRITE_CODE a write on an object the caller must not read, or
190+
* to a field it must not read, is refused by the
191+
* security gate before the object's validation runs.
192+
*/
193+
const NOT_REACHABLE_ROW_CODE: ErrorCode = 'RECORD_NOT_FOUND';
194+
const HIDDEN_WRITE_CODE: ErrorCode = 'PERMISSION_DENIED';
195+
ErrorCode.parse(NOT_REACHABLE_ROW_CODE);
196+
ErrorCode.parse(HIDDEN_WRITE_CODE);
173197

174198
function duplicates<T>(items: readonly T[], keyOf: (t: T) => string, at: IssuePath, what: string, refuse: Refuse) {
175199
const seen = new Set<string>();
@@ -338,6 +362,12 @@ function checkCard(
338362
return v === undefined ? null : v;
339363
};
340364

365+
/** Whether `p` sees `row` of `object`: every row-scope rule it declares on that object holds. */
366+
const inScope = (p: Persona, object: string, row: SeedRow): boolean => {
367+
const resolve = resolverFor(object);
368+
return (p.scope ?? []).filter((sc) => sc.object === object).every((sc) => resolve(row, sc.field) === sc.value);
369+
};
370+
341371
// ── personas: hidden refs and row scope resolve ──
342372
duplicates(run.personas, (p) => p.id, ['run', 'personas'], 'persona id', refuse);
343373
const personas = new Set(run.personas.map((p) => p.id));
@@ -419,8 +449,7 @@ function checkCard(
419449
personaTruths[q.id][id] = { kind: 'withheld' };
420450
return;
421451
}
422-
const rules = (p.scope ?? []).filter((sc) => sc.object === q.object);
423-
const scoped = rows.filter((r) => rules.every((sc) => ctx.resolve(r, sc.field) === sc.value));
452+
const scoped = rows.filter((r) => inScope(p, q.object, r));
424453
const truth = evaluate(scoped, [...at, 'personas', j]);
425454
if (truth) personaTruths[q.id][id] = truth;
426455
});
@@ -432,6 +461,33 @@ function checkCard(
432461
if (!run.operations.some((o) => o.kind === 'create' || o.kind === 'update')) {
433462
refuse(['run', 'operations'], 'needs at least one create or update through the app');
434463
}
464+
// Reach: a persona is a requirement of the app (`hidden` = must not read,
465+
// `scope` = sees only those rows), so an operation its acting persona cannot
466+
// reach has exactly one outcome a faithful build gives, decided from the card
467+
// alone — the object hidden, then the target row out of scope (the row gate
468+
// runs before the field gate), then a written field hidden. Declaring anything
469+
// else (`applied`, `pending_confirmation`, another refusal code) is the card
470+
// contradicting its own persona: graded against a faithful build it fails, and
471+
// it passes only a build that leaks what the persona must not see.
472+
const checkReach = (op: ScenarioCard['run']['operations'][number], actor: Persona, at: IssuePath) => {
473+
const row = op.target === undefined ? undefined : rowById.get(op.object)?.get(op.target);
474+
const hiddenField = Object.keys(op.values ?? {}).find((field) =>
475+
(actor.hidden.fields ?? []).some((f) => f.object === op.object && f.field === field),
476+
);
477+
let want: { code: ErrorCode; why: string } | undefined;
478+
if ((actor.hidden.objects ?? []).includes(op.object)) {
479+
want = { code: HIDDEN_WRITE_CODE, why: `\`${actor.id}\` must not read \`${op.object}\`` };
480+
} else if (row !== undefined && !unscoped.has(actor.id) && !inScope(actor, op.object, row)) {
481+
want = { code: NOT_REACHABLE_ROW_CODE, why: `row \`${op.target}\` is outside \`${actor.id}\`'s row scope on \`${op.object}\`` };
482+
} else if (hiddenField !== undefined) {
483+
want = { code: HIDDEN_WRITE_CODE, why: `\`${actor.id}\` must not read \`${op.object}.${hiddenField}\`, which this ${op.kind} writes` };
484+
}
485+
if (want === undefined || (op.outcome.kind === 'refused' && op.outcome.code === want.code)) return;
486+
refuse(
487+
[...at, 'outcome', ...(op.outcome.kind === 'refused' ? ['code'] : [])],
488+
`${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`,
489+
);
490+
};
435491
run.operations.forEach((op, i) => {
436492
const at: IssuePath = ['run', 'operations', i];
437493
if (!objects.has(op.object) || seed[op.object] === undefined) {
@@ -446,6 +502,8 @@ function checkCard(
446502
checkRef(op.object, field, value, [...at, 'values', field]);
447503
}
448504
if (!personas.has(op.actingAs)) refuse([...at, 'actingAs'], `no persona \`${op.actingAs}\``);
505+
const actor = run.personas.find((p) => p.id === op.actingAs);
506+
if (actor !== undefined) checkReach(op, actor, at);
449507
const out = op.outcome;
450508
if (out.kind !== 'applied') return;
451509
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));
@@ -87,6 +89,12 @@ const CASES: Array<[string, (c: RawCard) => void, string, RegExp]> = [
8789
['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/],
8890
['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`/],
8991
['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`/],
92+
// ── reach: an operation its acting persona cannot reach has one outcome ──
93+
['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`/],
94+
['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`/],
95+
['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`/],
96+
['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`/],
97+
['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`/],
9098
// ── guardrails ──
9199
['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/],
92100
['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/],
@@ -114,6 +122,17 @@ describe('scenario card refusals', () => {
114122
expect(r.refusals).toEqual([]);
115123
});
116124

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

‎packages/qa/scenarios/test/travel-expense.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ describe('scenario card travel_expense (docs/qa/scenarios/travel_expense.json)',
162162
expect(outcome('fix_meal_days')?.kind).toBe('applied');
163163
expect(outcome('manager_approves_step_one')?.kind).toBe('applied');
164164
expect(outcome('pay_claim')).toEqual({ kind: 'pending_confirmation' });
165-
expect(outcome('employee_approves_colleague')).toEqual({ kind: 'refused', code: 'PERMISSION_DENIED' });
165+
expect(outcome('employee_approves_own_claim')).toEqual({ kind: 'refused', code: 'PERMISSION_DENIED' });
166166
expect(outcome('over_standard_without_reason')).toEqual({ kind: 'refused', code: 'VALIDATION_FAILED' });
167167
});
168168

0 commit comments

Comments
 (0)