diff --git a/CHANGELOG.md b/CHANGELOG.md index 501a6a4..3ace465 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -165,6 +165,18 @@ The entries below describe the current prototype state._ category cannot be deleted (core's capability mapping denies the cap, and `wp_delete_term()` refuses the term). Rides the same `taxonomy_show_in_abilities` opt-in and content category as the query. +- `show_in_rest` exposure flag on the three tier-core backports + (`core/content-query`, `core/settings-get`, `core/users-query`): their + `ability_meta()` now sets the flag explicitly under a `// Package:` marker, + so the abilities stay exposed over core's REST abilities routes on every + supported WordPress (6.9+) — core's own registration omits the flag and + relies on `meta.public` seeding it, which WordPress 7.1+ only does. The same + accommodation the AI plugin's synced copies carry with their `// Plugin:` + marker, made once in the package; the coreify round-trip gate tolerates + exactly this marked line and nothing more (a `PKG_META_ACCOMMODATIONS` map: + `--reverse` re-injects the flag into regenerated backports, forward drops it + behind `// Package:`-verified markers), and the trio tests pin + `meta.show_in_rest === true` with the high-level `public` meta unchanged. ### Removed - `wp/post-create` — the self-authored experimental write-ability prototype diff --git a/src/Content/Content_Query.php b/src/Content/Content_Query.php index 9cacafd..d641156 100644 --- a/src/Content/Content_Query.php +++ b/src/Content/Content_Query.php @@ -213,23 +213,32 @@ protected function make_permission_callback(): callable { } /** - * The authored ability meta, exactly as core registers it. + * The authored ability meta: core-verbatim, plus one marked package accommodation. * * Core also carries no `required_capabilities` on this ability: the permission callback * gates the request as a whole (an authenticated user, an exposed post type, and the * capabilities for the requested statuses), and execute_content_query() adds the * row-level read checks, so the static floor stays empty here too. * + * The `show_in_rest` key below is the one addition (see its `// Package:` marker): + * core's registration omits the flag and relies on `public` seeding it, which + * WordPress 7.1+ only does, so the flag is set explicitly to stay exposed over the + * REST abilities routes on every supported WordPress (6.9+). + * * @return array The ability meta. */ protected function ability_meta(): array { return array( - 'annotations' => array( + 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true, ), - 'public' => true, + 'public' => true, + // Package: set explicitly so the ability is exposed over REST on all + // supported WordPress versions; core's registration relies on the + // `public` flag seeding `show_in_rest`, which WordPress 7.1+ only does. + 'show_in_rest' => true, ); } diff --git a/src/Media/Media_Delete.php b/src/Media/Media_Delete.php index 11504b6..c146ed9 100644 --- a/src/Media/Media_Delete.php +++ b/src/Media/Media_Delete.php @@ -114,6 +114,9 @@ protected function ability_meta(): array { 'open_world' => false, ), 'show_in_rest' => true, + 'mcp' => array( + 'public' => true, + ), ); } } diff --git a/src/Media/Media_Query.php b/src/Media/Media_Query.php index 04dd1c4..7fd37bb 100644 --- a/src/Media/Media_Query.php +++ b/src/Media/Media_Query.php @@ -182,6 +182,9 @@ protected function ability_meta(): array { 'open_world' => false, ), 'show_in_rest' => true, + 'mcp' => array( + 'public' => true, + ), ); } diff --git a/src/Media/Media_Update.php b/src/Media/Media_Update.php index 36bdffc..bf384a7 100644 --- a/src/Media/Media_Update.php +++ b/src/Media/Media_Update.php @@ -119,6 +119,9 @@ protected function ability_meta(): array { 'open_world' => false, ), 'show_in_rest' => true, + 'mcp' => array( + 'public' => true, + ), ); } } diff --git a/src/Media/Media_Upload.php b/src/Media/Media_Upload.php index 9a02517..63eb185 100644 --- a/src/Media/Media_Upload.php +++ b/src/Media/Media_Upload.php @@ -119,6 +119,9 @@ protected function ability_meta(): array { 'open_world' => true, ), 'show_in_rest' => true, + 'mcp' => array( + 'public' => true, + ), ); } } diff --git a/src/Settings/Settings_Get.php b/src/Settings/Settings_Get.php index 3dd79b6..e1c6396 100644 --- a/src/Settings/Settings_Get.php +++ b/src/Settings/Settings_Get.php @@ -118,18 +118,27 @@ protected function make_permission_callback(): ?callable { } /** - * The authored ability meta, exactly as core registers it. + * The authored ability meta: core-verbatim, plus one marked package accommodation. + * + * The `show_in_rest` key below is the one addition (see its `// Package:` marker): + * core's registration omits the flag and relies on `public` seeding it, which + * WordPress 7.1+ only does, so the flag is set explicitly to stay exposed over the + * REST abilities routes on every supported WordPress (6.9+). * * @return array The ability meta. */ protected function ability_meta(): array { return array( - 'annotations' => array( + 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true, ), - 'public' => true, + 'public' => true, + // Package: set explicitly so the ability is exposed over REST on all + // supported WordPress versions; core's registration relies on the + // `public` flag seeding `show_in_rest`, which WordPress 7.1+ only does. + 'show_in_rest' => true, ); } diff --git a/src/Terms/Term_Create.php b/src/Terms/Term_Create.php index 875248e..e784f5f 100644 --- a/src/Terms/Term_Create.php +++ b/src/Terms/Term_Create.php @@ -116,6 +116,9 @@ protected function ability_meta(): array { 'open_world' => false, ), 'show_in_rest' => true, + 'mcp' => array( + 'public' => true, + ), ); } } diff --git a/src/Terms/Term_Delete.php b/src/Terms/Term_Delete.php index 10c6ef8..e19bc66 100644 --- a/src/Terms/Term_Delete.php +++ b/src/Terms/Term_Delete.php @@ -115,6 +115,9 @@ protected function ability_meta(): array { 'open_world' => false, ), 'show_in_rest' => true, + 'mcp' => array( + 'public' => true, + ), ); } } diff --git a/src/Terms/Term_Update.php b/src/Terms/Term_Update.php index 0ecce5b..361dd53 100644 --- a/src/Terms/Term_Update.php +++ b/src/Terms/Term_Update.php @@ -117,6 +117,9 @@ protected function ability_meta(): array { 'open_world' => false, ), 'show_in_rest' => true, + 'mcp' => array( + 'public' => true, + ), ); } } diff --git a/src/Terms/Terms_Query.php b/src/Terms/Terms_Query.php index 0460a49..283a20d 100644 --- a/src/Terms/Terms_Query.php +++ b/src/Terms/Terms_Query.php @@ -199,6 +199,9 @@ protected function ability_meta(): array { 'open_world' => false, ), 'show_in_rest' => true, + 'mcp' => array( + 'public' => true, + ), ); } diff --git a/src/Users/Users_Query.php b/src/Users/Users_Query.php index 592f818..7b63f1d 100644 --- a/src/Users/Users_Query.php +++ b/src/Users/Users_Query.php @@ -168,18 +168,27 @@ protected function make_permission_callback(): callable { } /** - * The authored ability meta, exactly as core registers it. + * The authored ability meta: core-verbatim, plus one marked package accommodation. + * + * The `show_in_rest` key below is the one addition (see its `// Package:` marker): + * core's registration omits the flag and relies on `public` seeding it, which + * WordPress 7.1+ only does, so the flag is set explicitly to stay exposed over the + * REST abilities routes on every supported WordPress (6.9+). * * @return array The ability meta. */ protected function ability_meta(): array { return array( - 'annotations' => array( + 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true, ), - 'public' => true, + 'public' => true, + // Package: set explicitly so the ability is exposed over REST on all + // supported WordPress versions; core's registration relies on the + // `public` flag seeding `show_in_rest`, which WordPress 7.1+ only does. + 'show_in_rest' => true, ); } diff --git a/tests/Abilities/ContentQueryTest.php b/tests/Abilities/ContentQueryTest.php index 1c3348d..4aff438 100644 --- a/tests/Abilities/ContentQueryTest.php +++ b/tests/Abilities/ContentQueryTest.php @@ -188,6 +188,12 @@ public function test_registered_once_with_core_annotations(): void { $meta['annotations'] ?? null ); self::assertTrue( (bool) ( $meta['public'] ?? false ) ); + self::assertSame( + true, + $meta['show_in_rest'] ?? null, + 'The ability stays exposed over the REST abilities routes on every supported WordPress: core seeds show_in_rest from `public` on 7.1+ only, so the package copy ships the flag explicitly (the `// Package:` meta accommodation).' + ); + self::assertSame( true, $ability->get_meta_item( 'public', null ), 'The high-level public meta stays true.' ); if ( empty( $meta['source'] ) ) { // Trunk env: core provides the name; the package copy must have skipped. diff --git a/tests/Abilities/MediaDeleteTest.php b/tests/Abilities/MediaDeleteTest.php index f499358..827705b 100644 --- a/tests/Abilities/MediaDeleteTest.php +++ b/tests/Abilities/MediaDeleteTest.php @@ -244,6 +244,12 @@ public function test_registers_the_delete_ability(): void { self::assertSame( 'Media Delete', $ability->get_label(), 'The ability should use the PR\'s label.' ); self::assertSame( 'media', $ability->get_category(), 'The ability should use the package-authored media category.' ); self::assertTrue( (bool) ( $meta['show_in_rest'] ?? false ), 'The ability should be exposed in REST.' ); + self::assertSame( + array( 'public' => true ), + $meta['mcp'] ?? null, + 'The mcp meta opts the ability in for MCP-tool exposure; the high-level public meta is unchanged.' + ); + self::assertFalse( $ability->get_meta_item( 'public', false ), 'The high-level public meta is unchanged.' ); $annotations = $meta['annotations'] ?? array(); self::assertFalse( (bool) ( $annotations['readonly'] ?? true ), 'Deleting writes the library.' ); diff --git a/tests/Abilities/MediaQueryTest.php b/tests/Abilities/MediaQueryTest.php index 9d1dbc6..655b47f 100644 --- a/tests/Abilities/MediaQueryTest.php +++ b/tests/Abilities/MediaQueryTest.php @@ -318,6 +318,12 @@ public function test_registered_once_with_annotations_and_provenance(): void { self::assertSame( 'Media Query', $ability->get_label(), 'The ability should use the PR\'s label.' ); self::assertSame( 'media', $ability->get_category(), 'The ability should use the package-authored media category.' ); self::assertTrue( (bool) ( $meta['show_in_rest'] ?? false ), 'The ability should be exposed in REST.' ); + self::assertSame( + array( 'public' => true ), + $meta['mcp'] ?? null, + 'The mcp meta opts the ability in for MCP-tool exposure; the high-level public meta is unchanged.' + ); + self::assertFalse( $ability->get_meta_item( 'public', false ), 'The high-level public meta is unchanged.' ); self::assertSame( array( 'readonly' => true, diff --git a/tests/Abilities/MediaUpdateTest.php b/tests/Abilities/MediaUpdateTest.php index 87a3667..4b70249 100644 --- a/tests/Abilities/MediaUpdateTest.php +++ b/tests/Abilities/MediaUpdateTest.php @@ -250,6 +250,12 @@ public function test_registers_the_update_ability(): void { self::assertSame( 'Media Update', $ability->get_label(), 'The ability should use the PR\'s label.' ); self::assertSame( 'media', $ability->get_category(), 'The ability should use the package-authored media category.' ); self::assertTrue( (bool) ( $meta['show_in_rest'] ?? false ), 'The ability should be exposed in REST.' ); + self::assertSame( + array( 'public' => true ), + $meta['mcp'] ?? null, + 'The mcp meta opts the ability in for MCP-tool exposure; the high-level public meta is unchanged.' + ); + self::assertFalse( $ability->get_meta_item( 'public', false ), 'The high-level public meta is unchanged.' ); $annotations = $meta['annotations'] ?? array(); self::assertFalse( (bool) ( $annotations['readonly'] ?? true ), 'Updating writes the item.' ); diff --git a/tests/Abilities/MediaUploadTest.php b/tests/Abilities/MediaUploadTest.php index 253c5f4..ffbc2d9 100644 --- a/tests/Abilities/MediaUploadTest.php +++ b/tests/Abilities/MediaUploadTest.php @@ -292,6 +292,12 @@ public function test_registers_the_upload_ability(): void { self::assertSame( 'Media Upload', $ability->get_label(), 'The ability should use the PR\'s label.' ); self::assertSame( 'media', $ability->get_category(), 'The ability should use the package-authored media category.' ); self::assertTrue( (bool) ( $meta['show_in_rest'] ?? false ), 'The ability should be exposed in REST.' ); + self::assertSame( + array( 'public' => true ), + $meta['mcp'] ?? null, + 'The mcp meta opts the ability in for MCP-tool exposure; the high-level public meta is unchanged.' + ); + self::assertFalse( $ability->get_meta_item( 'public', false ), 'The high-level public meta is unchanged.' ); $annotations = $meta['annotations'] ?? array(); self::assertFalse( (bool) ( $annotations['readonly'] ?? true ), 'Uploading writes the library.' ); diff --git a/tests/Abilities/SettingsGetTest.php b/tests/Abilities/SettingsGetTest.php index b0cdde7..0156e04 100644 --- a/tests/Abilities/SettingsGetTest.php +++ b/tests/Abilities/SettingsGetTest.php @@ -213,6 +213,12 @@ public function test_registered_once_with_core_annotations(): void { $meta['annotations'] ?? null ); self::assertTrue( (bool) ( $meta['public'] ?? false ) ); + self::assertSame( + true, + $meta['show_in_rest'] ?? null, + 'The ability stays exposed over the REST abilities routes on every supported WordPress: core seeds show_in_rest from `public` on 7.1+ only, so the package copy ships the flag explicitly (the `// Package:` meta accommodation).' + ); + self::assertSame( true, $ability->get_meta_item( 'public', null ), 'The high-level public meta stays true.' ); if ( empty( $meta['source'] ) ) { // Trunk env: core provides the name; the package copy must have skipped. diff --git a/tests/Abilities/TermWritesTest.php b/tests/Abilities/TermWritesTest.php index b8a3922..024e4e9 100644 --- a/tests/Abilities/TermWritesTest.php +++ b/tests/Abilities/TermWritesTest.php @@ -403,6 +403,8 @@ public function test_registered_once_with_the_pr_contract(): void { foreach ( array( $create, $update, $delete ) as $ability ) { self::assertSame( 'content', $ability->get_category(), 'The write abilities share the query\'s content category.' ); self::assertTrue( $ability->get_meta_item( 'show_in_rest', false ), 'The write abilities should be exposed in REST.' ); + self::assertSame( array( 'public' => true ), $ability->get_meta_item( 'mcp', array() ), 'The mcp meta opts the write abilities in for MCP-tool exposure.' ); + self::assertFalse( $ability->get_meta_item( 'public', false ), 'The write abilities\' high-level public meta is unchanged.' ); } $create_annotations = $create->get_meta_item( 'annotations', array() ); diff --git a/tests/Abilities/TermsQueryTest.php b/tests/Abilities/TermsQueryTest.php index b913298..b4ce16e 100644 --- a/tests/Abilities/TermsQueryTest.php +++ b/tests/Abilities/TermsQueryTest.php @@ -372,6 +372,12 @@ public function test_registered_once_with_annotations_and_provenance(): void { self::assertSame( 'Terms Query', $ability->get_label(), 'The ability should use the PR\'s label.' ); self::assertSame( 'content', $ability->get_category(), 'The ability should use the content category.' ); self::assertTrue( (bool) ( $meta['show_in_rest'] ?? false ), 'The ability should be exposed in REST.' ); + self::assertSame( + array( 'public' => true ), + $meta['mcp'] ?? null, + 'The mcp meta opts the ability in for MCP-tool exposure; the high-level public meta is unchanged.' + ); + self::assertFalse( $ability->get_meta_item( 'public', false ), 'The high-level public meta is unchanged.' ); self::assertSame( array( 'readonly' => true, diff --git a/tests/Abilities/UsersQueryTest.php b/tests/Abilities/UsersQueryTest.php index d25f88d..2c49e4d 100644 --- a/tests/Abilities/UsersQueryTest.php +++ b/tests/Abilities/UsersQueryTest.php @@ -261,6 +261,12 @@ public function test_registered_once_with_core_annotations(): void { $meta['annotations'] ?? null ); self::assertTrue( (bool) ( $meta['public'] ?? false ) ); + self::assertSame( + true, + $meta['show_in_rest'] ?? null, + 'The ability stays exposed over the REST abilities routes on every supported WordPress: core seeds show_in_rest from `public` on 7.1+ only, so the package copy ships the flag explicitly (the `// Package:` meta accommodation).' + ); + self::assertSame( true, $ability->get_meta_item( 'public', null ), 'The high-level public meta stays true.' ); if ( empty( $meta['source'] ) ) { // Trunk env: core provides the name; the package copy must have skipped. diff --git a/tools/coreify.php b/tools/coreify.php index e7dfbc2..a7bf8c2 100644 --- a/tools/coreify.php +++ b/tools/coreify.php @@ -74,6 +74,42 @@ function fail( string $msg ): void { 'execute_callback', 'permission_callback', 'required_capabilities', 'meta', ); +/** + * The one intentional, hand-marked meta divergence the package's tier-core + * backports carry vs core trunk: an explicit `'show_in_rest' => true` in + * ability_meta(). + * + * Core's registration relies on `meta.public` seeding `show_in_rest`, a + * WordPress 7.1+ behavior; the backports serve WordPress 6.9+, and core's REST + * abilities routes (list + run) hard-require the flag. Core's own trio classes + * omit it (fine on 7.2+); the AI plugin's synced copies set it explicitly with + * a `// Plugin:` marker, and the package does the same once with `// Package:`. + * + * This map teaches the round-trip to tolerate exactly this marked meta line and + * nothing more, symmetrically: + * + * - forward() drops the mapped keys (plus their marker comments) when + * unwrapping ability_meta() into core-shaped registration args, so the + * regenerated core text stays semantically identical to the fixture. + * - reverse() re-injects them (key + marker comment lines, boolean true) when + * emitting the package shape's ability_meta(), so a regenerated backport + * keeps the flag and the shipped files keep passing the check gate. + * + * Global across PROFILES is deliberate: every tier-core backport has the same + * 6.9+ floor and the same need. reverse() injects only when the fixture's meta + * lacks the key, so a fixture that already carries it is carried through + * verbatim, never duplicated; forward() requires the exact marker lines below + * immediately before the dropped key, so the shipped files and this const + * change together — the marker is the audit trail, not documentation. + */ +const PKG_META_ACCOMMODATIONS = array( + 'show_in_rest' => array( + '// Package: set explicitly so the ability is exposed over REST on all', + "// supported WordPress versions; core's registration relies on the", + '// `public` flag seeding `show_in_rest`, which WordPress 7.1+ only does.', + ), +); + /** * Optional authoring methods: Abstract_Core_Ability defaults them, and an * ability overrides them only when core's register() entry point computes @@ -994,6 +1030,13 @@ function transform_forward( string $pkgText, string $absText ): string { } $vals[ $entry ] = return_expr( $tokens, $rec ); } + + // The package's marked meta accommodations (PKG_META_ACCOMMODATIONS) leave the + // registration core-shaped: drop exactly the mapped keys (with their marker + // comments) from the meta entry. + if ( isset( $vals['meta'] ) ) { + $vals['meta'] = pkg_meta_drop_accommodations( $vals['meta'] ); + } $nameLit = "'" . $abname . "'"; if ( isset( $vals['required_capabilities'] ) && expr_is_empty_array( $vals['required_capabilities'] ) ) { @@ -1140,6 +1183,223 @@ function transform_forward( string $pkgText, string $absText ): string { return $header . $body . "}\n"; } +/* --------------------------------------------------------------------- + * Package meta accommodations (the one marked divergence, PKG_META_ACCOMMODATIONS) + * ------------------------------------------------------------------- */ + +/** + * Scan a registration-shaped array( ... ) expression: the outer group bounds + * and each top-level element as [first meaningful token, last meaningful + * token] with its separator comma index (null when there is none, i.e. the + * final element before the array close). + * + * The scan is comment-tolerant on purpose: the drop side must reach elements + * preceded by marker comment lines. Callers enforce their own strictness. + * + * @param array $tokens Token list of a '}|null + * Null when the expression is not an array( ... ) with `=>` elements. + */ +function pkg_meta_scan_top_level( array $tokens ): ?array { + $n = count( $tokens ); + $open = null; + for ( $i = 0; $i < $n; $i++ ) { + if ( T_ARRAY !== $tokens[ $i ][0] ) { + continue; + } + $paren = next_meaningful_token( $tokens, $i + 1 ); + if ( null !== $paren && 40 === $tokens[ $paren ][0] ) { + $open = $paren; + break; + } + } + if ( null === $open ) { + return null; + } + $close = match_group( $tokens, $open ); + $elements = array(); + $i = $open + 1; + while ( $i < $close ) { + $first = next_meaningful_token( $tokens, $i, $close - 1 ); + if ( null === $first ) { + break; + } + $arrow = next_meaningful_token( $tokens, $first + 1, $close - 1 ); + if ( null === $arrow || T_DOUBLE_ARROW !== $tokens[ $arrow ][0] ) { + return null; + } + $depth = 0; + $comma = null; + for ( $j = $arrow + 1; $j < $close; $j++ ) { + if ( is_skipped( $tokens[ $j ] ) ) { + continue; + } + if ( 0 === $depth && 44 === $tokens[ $j ][0] ) { + $comma = $j; + break; + } + $depth += struct_delta( $tokens[ $j ] ); + } + $end = prev_meaningful_token( $tokens, $arrow + 1, ( null === $comma ? $close : $comma ) - 1 ); + if ( null === $end ) { + return null; + } + $elements[] = array( 'first' => $first, 'end' => $end, 'comma' => $comma ); + $i = ( null === $comma ? $close : $comma ) + 1; + } + return array( 'open' => $open, 'close' => $close, 'elements' => $elements ); +} + +/** + * The reverse side of the accommodation: inject the {@see PKG_META_ACCOMMODATIONS} + * keys (boolean true, with their marker comment lines) into an emitted + * ability_meta() return statement — `return array( ... );` — so regenerated + * package files keep the accommodation the shipped files hand-author (the mB + * side of the gate). + * + * Keys already present in the meta are carried through untouched (never + * duplicated), so injection is a no-op for any future fixture that already + * ships the flag itself. Existing elements are re-emitted with `=>` arrows + * padded to the widest key among existing and injected elements (WPCS + * alignment); values keep their original text. + * + * @param string $metaReturn The ability_meta() return statement text. + */ +function pkg_meta_inject_accommodations( string $metaReturn ): string { + $tokens = tokenize_source( ' $markers ) { + if ( ! in_array( $key, $present, true ) ) { + $pending[] = $key; + } + } + if ( array() === $pending ) { + return $metaReturn; // already carries every accommodation + } + + /* element line indentation, from the line break ahead of the first element */ + $first0 = $scan['elements'][0]['first']; + $wstok = $first0 > 0 ? $tokens[ $first0 - 1 ] : null; + if ( null === $wstok || T_WHITESPACE !== $wstok[0] || false === strpos( $wstok[1], "\n" ) ) { + fail( 'pkg-meta inject: cannot derive the meta element indentation' ); + } + $indent = substr( $wstok[1], strrpos( $wstok[1], "\n" ) + 1 ); + + $maxlen = 0; + foreach ( $scan['elements'] as $e ) { + $maxlen = max( $maxlen, strlen( $tokens[ $e['first'] ][1] ) ); + } + foreach ( $pending as $key ) { + $maxlen = max( $maxlen, strlen( $key ) + 2 ); + } + + $lines = array(); + foreach ( $scan['elements'] as $e ) { + $arrow = next_meaningful_token( $tokens, $e['first'] + 1, $e['end'] ); + $value = ltrim( span_text( $tokens, $arrow + 1, $e['end'] ) ); + $pad = str_repeat( ' ', $maxlen - strlen( $tokens[ $e['first'] ][1] ) + 1 ); + $lines[] = $indent . $tokens[ $e['first'] ][1] . $pad . '=> ' . $value . ','; + } + foreach ( $pending as $key ) { + foreach ( PKG_META_ACCOMMODATIONS[ $key ] as $marker ) { + $lines[] = $indent . $marker; + } + $pad = str_repeat( ' ', $maxlen - ( strlen( $key ) + 2 ) + 1 ); + $lines[] = $indent . "'" . $key . "'" . $pad . '=> true,'; + } + + $last = $scan['elements'][ count( $scan['elements'] ) - 1 ]; + $tail = span_text( $tokens, ( $last['comma'] ?? $last['end'] ) + 1, count( $tokens ) - 1 ); + return span_text( $tokens, 1, $scan['open'] ) . "\n" . implode( "\n", $lines ) . $tail; +} + +/** + * The forward side of the accommodation: drop the {@see PKG_META_ACCOMMODATIONS} + * keys (plus their exact marker comment lines) from a lifted ability_meta() + * return expression, so the reconstructed core-shaped registration stays + * semantically identical to the fixture (the mA side of the gate). No wildcard + * dropping: only exactly the mapped keys are removed, and nothing else. + * + * Strict by design: a mapped key found in the meta MUST carry the const's + * marker comment lines immediately before it. An unmarked or reworded + * accommodation fails loudly instead of degrading silently, so the shipped + * files and the const change together. + * + * @param string $metaExpr The ability_meta() return expression text. + */ +function pkg_meta_drop_accommodations( string $metaExpr ): string { + $tokens = tokenize_source( ' $scan['open']; $j-- ) { + $id = $tokens[ $j ][0]; + if ( T_WHITESPACE === $id ) { + continue; + } + if ( T_COMMENT !== $id && T_DOC_COMMENT !== $id ) { + break; + } + $collected[] = array( $j, trim( $tokens[ $j ][1] ) ); + } + $got = array_reverse( array_column( $collected, 1 ) ); + if ( $got !== PKG_META_ACCOMMODATIONS[ $key ] ) { + fail( "pkg-meta drop: ability_meta() carries the accommodated key '$key' without PKG_META_ACCOMMODATIONS's exact marker comment lines (expected: " . implode( ' ', PKG_META_ACCOMMODATIONS[ $key ] ) . ' ; got: ' . ( array() === $got ? '(none)' : implode( ' ', $got ) ) . '); update the const and the source marker together.' ); + } + $dropStart = $collected[ count( $collected ) - 1 ][0]; + if ( T_WHITESPACE === $tokens[ $dropStart - 1 ][0] && false !== strpos( $tokens[ $dropStart - 1 ][1], "\n" ) ) { + $dropStart--; // swallow the line break separating the dropped block from the element above + } + $drops[] = array( 'start' => $dropStart, 'end' => $e['comma'] ?? $e['end'] ); + } + if ( array() === $drops ) { + return $metaExpr; + } + $out = ''; + for ( $j = 1, $n = count( $tokens ); $j < $n; $j++ ) { // token 0 is the '= $d['start'] && $j <= $d['end'] ) { + $drop = true; + break; + } + } + if ( ! $drop ) { + $out .= $tokens[ $j ][1]; + } + } + return $out; +} + /* --------------------------------------------------------------------- * REVERSE transform * ------------------------------------------------------------------- */ @@ -1379,6 +1639,11 @@ function transform_reverse( string $coreText ): string { } $authors['ability_meta'] = $mkret( 'meta' ); + // The package's marked meta accommodations (PKG_META_ACCOMMODATIONS) keep regenerated + // backports identical to the shipped files: re-inject the mapped keys (with their + // marker comment lines) when the fixture's meta lacks them. + $authors['ability_meta'] = pkg_meta_inject_accommodations( $authors['ability_meta'] ); + $rc = 'return array();'; if ( isset( $entries['required_capabilities'] ) ) { $rc = 'return ' . reindent( $entries['required_capabilities'], -2 ) . ';';