From 799ecd303acc4a1543010154d906ad08282f7de9 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 20:54:05 -0800 Subject: [PATCH] pa_lab: describe_program checks each precheck entry A declared precheck list must be entries of {name, needs, skippable} with a non-empty, unique name; anything else is refused as app_unavailable, like a bad prechecks field. This is the shape the app prints from fork #511. --- lab/server/README.md | 10 +++++--- lab/server/pa_lab/app_query.py | 21 +++++++++++++--- lab/server/tests/test_app_query.py | 39 ++++++++++++++++++++++++++---- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/lab/server/README.md b/lab/server/README.md index 8404c253d3..e67810ac60 100644 --- a/lab/server/README.md +++ b/lab/server/README.md @@ -200,8 +200,12 @@ up to 5 s + 2 s for the journal, and reads `state.json` again. Arguments are clo - `describe_program` parses `describe_program ` and returns it as `program`: `{"id", "options", "prechecks", "schema": 1}` (the option entries as the app prints them) with - `prechecks_supported`. `prechecks` is the string `unsupported` until the program declares a - list (#485); that is a valid reply, not an error. A list is passed through. + `prechecks_supported`. `prechecks` is the string `unsupported` when the program declares no + list; that is a valid reply, not an error, and never means the checks passed. Otherwise it + is the program's checks in run order, each `{"name", "needs", "skippable"}` (#511): `name` a + non-empty string, unique in the list; `needs` the one line of what the program needs; + `skippable` whether the user can turn the check off. An empty list means the program needs + nothing. The list is passed through. - `game_open` parses `game_open yes` / `game_open no REASON` and returns `open` and `reason` (`null`, `home`, `user_select`, `black` or `no_video`). `open: false` is an answer, not a tool error; it does not tell one game from another (`ensure_game` does). @@ -210,7 +214,7 @@ up to 5 s + 2 s for the journal, and reads `state.json` again. Arguments are clo |---|---| | `describe_program: the selected panel is not a program.` | `panel_not_program` | | `schema` other than 1 | `unsupported_schema` | -| a failed line, no single reply line and `ok` line, bad JSON, a missing `id`/`options`/`prechecks`, an unknown `game_open` reason | `app_unavailable` (the journal in `evidence`) | +| a failed line, no single reply line and `ok` line, bad JSON, a missing `id`/`options`/`prechecks`, a precheck entry without a name, `needs` or boolean `skippable`, a name twice, an unknown `game_open` reason | `app_unavailable` (the journal in `evidence`) | | a reply from another instance | `app_unavailable`, `evidence.reason` `instance_replaced` | | no journal within 7 s | `timeout`; the request is withdrawn from the inbox | diff --git a/lab/server/pa_lab/app_query.py b/lab/server/pa_lab/app_query.py index a875cd147a..02b51044a3 100644 --- a/lab/server/pa_lab/app_query.py +++ b/lab/server/pa_lab/app_query.py @@ -5,7 +5,8 @@ - `describe_program` prints `describe_program `, the selected program's id and the static contract of its options: `{"id", "options", "prechecks", "schema": 1}`. `prechecks` is the - string `unsupported` until the program declares a precheck list (#485); a list after that. + string `unsupported` when the program declares no precheck list; otherwise the list in run + order, each `{"name", "needs", "skippable"}` with a unique non-empty name (#511). - `game_open` prints `game_open yes`, or `game_open no REASON` with REASON `home`, `user_select`, `black` or `no_video`. It never fails the line and does not tell one game from another. @@ -83,6 +84,21 @@ def _one_reply(lines: List[str], pattern: 're.Pattern[str]', command: str): return replies[0] +def _good_precheck(entry: Any) -> bool: + return (isinstance(entry, dict) and isinstance(entry.get('name'), str) and bool(entry['name']) + and isinstance(entry.get('needs'), str) and isinstance(entry.get('skippable'), bool)) + + +def _good_prechecks(prechecks: Any) -> bool: + """`unsupported`, or a list of well-formed entries with unique names.""" + if prechecks == PRECHECKS_UNSUPPORTED: + return True + if not isinstance(prechecks, list) or not all(_good_precheck(entry) for entry in prechecks): + return False + names = [entry['name'] for entry in prechecks] + return len(names) == len(set(names)) + + def _check_contract(doc: Any, lines: List[str]) -> Dict[str, Any]: if not isinstance(doc, dict): raise Refused('app_unavailable', 'describe_program did not print a JSON object.', {'journal': lines}) @@ -94,8 +110,7 @@ def _check_contract(doc: Any, lines: List[str]) -> Dict[str, Any]: problems.append('id') if not isinstance(doc.get('options'), list) or not all(isinstance(o, dict) for o in doc['options']): problems.append('options') - prechecks = doc.get('prechecks') - if prechecks != PRECHECKS_UNSUPPORTED and not isinstance(prechecks, list): + if not _good_prechecks(doc.get('prechecks')): problems.append('prechecks') if problems: raise Refused('app_unavailable', 'describe_program printed a bad {}.'.format(', '.join(problems)), diff --git a/lab/server/tests/test_app_query.py b/lab/server/tests/test_app_query.py index b6553ff1a4..5a021da1c6 100644 --- a/lab/server/tests/test_app_query.py +++ b/lab/server/tests/test_app_query.py @@ -54,11 +54,40 @@ def test_the_request_is_one_fixed_line(self): self.assertEqual(lines[1:], ['describe_program']) def test_a_precheck_list_is_supported(self): - doc = dict(PROGRAM_CONTRACT, prechecks=[{'name': 'game_open', 'needs': 'a game in front'}]) - self.app.replies['describe_program'] = describe_line(doc) - result = self.ask('describe_program') - self.assertTrue(result['prechecks_supported']) - self.assertEqual(result['program']['prechecks'], doc['prechecks']) + cases = [ + dict(name='two checks, in order', + input=[{'name': 'black_border', 'needs': 'No black border.', 'skippable': False}, + {'name': 'free_party_slots', 'needs': '1 free party slot.', 'skippable': True}], + expectation=['black_border', 'free_party_slots']), + dict(name='an empty list needs nothing', input=[], expectation=[]), + ] + for case in cases: + with self.subTest(case['name']): + self.setUp() + doc = dict(PROGRAM_CONTRACT, prechecks=case['input']) + self.app.replies['describe_program'] = describe_line(doc) + result = self.ask('describe_program') + self.assertTrue(result['prechecks_supported']) + self.assertEqual([c['name'] for c in result['program']['prechecks']], case['expectation']) + self.assertEqual(result['program']['prechecks'], case['input']) + + def test_a_bad_precheck_entry_is_refused(self): + good = {'name': 'black_border', 'needs': 'No black border.', 'skippable': False} + cases = [ + dict(name='not an object', input=['black_border'], expectation='prechecks'), + dict(name='no name', input=[{k: v for k, v in good.items() if k != 'name'}], expectation='prechecks'), + dict(name='empty name', input=[dict(good, name='')], expectation='prechecks'), + dict(name='needs not a string', input=[dict(good, needs=None)], expectation='prechecks'), + dict(name='skippable not a bool', input=[dict(good, skippable='no')], expectation='prechecks'), + dict(name='a name twice', input=[good, dict(good, skippable=True)], expectation='prechecks'), + ] + for case in cases: + with self.subTest(case['name']): + self.setUp() + self.app.replies['describe_program'] = describe_line(dict(PROGRAM_CONTRACT, prechecks=case['input'])) + refusal = self.refusal('describe_program') + self.assertEqual(refusal['code'], 'app_unavailable') + self.assertIn(case['expectation'], refusal['message']) def test_refusals(self): bad_options = dict(PROGRAM_CONTRACT, options=['LANGUAGE'])