0
Fork 0
mirror of https://github.com/mastra-ai/mastra.git synced 2026-11-17 08:44:46 +00:00

test(core): cover a child scope added to an existing Knowledge scope's declaration (#29925)

## Description

Adds a test for the condition that sends a held Knowledge scope back to
the store when its declaration lists a child scope that doesn't exist
yet.

Since #29775, a write run skips the store call for a held scope that
already exists, whose parent hasn't changed, whose declaration has no
template pages, and whose declared child scopes all exist
(`#materialize` in `packages/core/src/knowledge/scope-runtime.ts`).
Existing tests in `write-start-scopes.test.ts` fail when the parent or
template-page condition is replaced with `true`. Replacing the
child-scope condition with `true` leaves all 9 passing, so nothing in
that file would catch a write run that stops creating a child scope
added to an existing scope's declaration.

The new test materializes `team:t1` with no children and checks that
`team:t1/internal` doesn't exist. It then runs a write start with
`children: [{ name: 'internal' }]` and checks that `team:t1/internal`
now exists under `team:t1`.

## Testing

- `pnpm --filter @mastra/core exec vitest run
src/knowledge/__tests__/write-start-scopes.test.ts`: 10 passed (9
existing, 1 new).
- With the child-scope condition replaced by `true`, the new test fails
(`expected undefined to be defined`) and the other 9 pass.
- `oxlint` and `prettier --check` are clean on the file.

### Postgres check of #29775

I also ran #29775 against Postgres 16 (`pgvector/pgvector:0.8.4-pg16`).
That script imports the scope runtime from core source, so it isn't part
of this PR.

- Compared with the per-scope loop from before #29775, each in its own
schema, the batched start produced the same scope trees (parent,
declaration, name, description, template pages), warnings and errors.
The start sequences covered a conflicting held parent, an unplaced scope
getting linked once its parent is held, ambiguous parents,
template-child principals, `belongsTo` cycles, and 2,301 held scopes. A
template page deleted outside Knowledge was recreated the same way by
both.
- On the batched start only: a child scope added to an existing scope's
declaration was created, and a `belongsTo` added to an existing scope's
declaration set its parent.
- 8 concurrent cold batched starts over the same 201 scopes all
succeeded. All 201 scopes existed afterwards, every project was under
the org, and no address appeared on more than one scope row.
- `materializeScope` calls for 2,301 held scopes: a first write went
from 4,601 (the old loop materialized the shared parent again for each
project) to 2,301 plus one batched lookup, and a repeat write from 2,301
to 0. In one local run on Docker for macOS, the repeat write took 11 ms
against 7.0 s before.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## ELI5

This PR adds a test for a team that already exists when its declaration
gains a child scope. The test checks that the next materialization
creates the new child.

## Changes

- Added a test in
`packages/core/src/knowledge/__tests__/write-start-scopes.test.ts`.
- The test first materializes `team:t1` without children and confirms
`team:t1/internal` is absent.
- It then materializes `team:t1` with `children: [{ name: 'internal' }]`
and checks that the child scope exists.

## Checks

- Test, lint, and formatting results: not available.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Co-authored-by: Jamie Lee Nichols <10267569+CodingButter@users.noreply.github.com>
Co-authored-by: mastracode <284800079+mastra-platform[bot]@users.noreply.github.com>
This commit is contained in:
Jamie Nichols 2026-11-14 07:34:14 -08:00 • committed by GitHub
commit 5b7f130224
No known key found for this signature in database
GPG key ID: B5690EEEBB952194