$kirby->pages() incomplete not containing all pages

I am creating a page at runtime within a plugin via a certain route. In the Template of the plugin I want to access other pages, but not all pages exist. Furthermore it is dependent on the created page draft status wich ones are available.

To replicate use this minimal setup:
Create a plugin with the following index.php

<?php

Kirby::plugin('kirbyluke/testplugin', [
    'routes' => [
		[
			'pattern' => 'mytestroute',
			'action' => function () {
				$myExistingPage = page('testpage');

				// delete the page if it exists
				if ($myExistingPage) {
					$myExistingPage->delete();
				} else {
					$myExistingPage = site()->index()->filterBy('template', 'reference')->first();
					if ($myExistingPage) {
						$myExistingPage->delete();
					}
				}

				// Remove any drafts with the same slug
				$drafts = site()->drafts()->filterBy('slug', 'testpage');
				foreach ($drafts as $draft) {
					$draft->delete();
				}

				// recreate page
				$mytestplugin = site()->createChild([
					'title'    => 'testpage',
					'slug'     => 'testpage',
					'template' => 'testtemplate',
					'isDraft'  => false,
				]);

				return $mytestplugin->render();
			}
		]
	],
    'templates' => [
        'testtemplate' => __DIR__ . '/templates/testtemplate.php',
    ],
]);

Then create the Template at the plugins sub directory templates/testtemplate.php

<h1>Testtemplate</h1>

<?php
var_dump($site->pages());

With this minimal setup you should now create listed and unlisted pages via the kirby backend. After that visit the plugin route /mytestroute.

My pages are the following:

I get the following output for the pages dump:

object(Kirby\Cms\Pages)#542 (3) { [0]=> string(4) "team" [1]=> string(4) "home" [2]=> string(8) "testpage" }

Notice how all of the unlisted pages (without indices in the content directory) are absent from this object but the testpage itself is present.

If we then go ahed and edit in our plugin the page creation and set 'isDraft' => true, I am getting all pages in the dump output like so:

object(Kirby\Cms\Pages)#483 (8) { [0]=> string(4) "team" [1]=> string(4) "home" [2]=> int(403) [3]=> string(3) "bla" [4]=> string(5) "error" [5]=> string(9) "locations" [6]=> string(5) "menus" [7]=> string(8) "settings" }

Furthermore I went to do the same test on another page created via the kirby backend with the same testtemplate and this one shows the complete pages object no matter if it is public, unlisted or draft.

Why is that behavior and why does setting the newly created page to a draft solve the other pages not beeing recognized and why only on pages created that way during runtime and not on kirby-backend-created pages?
I hope you can recreate my problem and someone has some insights regarding this behavior. Thanks.

I can reproduce the issue and it looks like a bug. I’ve created an issue for this case. Thanks for the report.