Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog.d/409.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
section: authoring
---

- **Extractions can report whether their subject is present** (#409). `extraction/v0` has a new optional `presence` field, `present` or `absent`, that says whether what a projection describes exists at the revision, such as a workflow the revision predates. Omitted means not reported, which hosts treat as unknown, never present. The engine rejects an `absent` extraction that has entities or relationships, or that names nothing in `missingInputs`, and it keeps `presence` as written, so extractions that omit it keep their digests. Custom extractors check the files that make up their subject with `request.subjectExists()` and `request.subjectMatches()` in Node, `request.subject_exists()` and `request.subject_matches()` in Python, or `Context::subject_exists` and `Context::subject_matches` in the Rust extractor SDK. `result()` then writes `presence`: `absent` when none of them exists, and `present` otherwise. `engine.presence(before, after)` (and `presenceChange` in `@githubnext/projections-schemas`) reports whether a change added, removed or kept a subject. In the Rust SDK, `Context::finish` now returns a `Result`. Built-in extractors will write `presence` in a later release, which changes their digests once, and `extraction/v1` will require it. See [presence](docs/contracts/extractions.md#presence).
15 changes: 13 additions & 2 deletions docs/agents/create-projection.md
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,8 @@ in the examined revision, `request.checkout` where its dependencies are,
files in the revision, project-relative), and
`result(entities=..., relationships=...)` writes the extraction, items as lists
with an `id` or as dicts keyed by ID, sorted, `graph/v0` unless
`schema="..."`, with optional `metadata`, `volatile` and `missing_inputs`:
`schema="..."`, with optional `metadata`, `volatile`, `presence` and
`missing_inputs`:

```python
from projections import request, result
Expand All @@ -495,7 +496,17 @@ import {request, result} from '@githubnext/projections-sdk/extractor'
result({entities: {'service:payments': {kind: 'service', label: 'Payments'}}})
```

In other languages, write the JSON above.
When what the projection describes may not exist at a revision (a workflow,
a manifest the revision predates), check it before reading it:
`request.subject_exists("flags.yaml")` in Python,
`request.subjectExists('flags.json')` in JavaScript, or `subject_matches` /
`subjectMatches` for a glob. If none of the anchors you checked exists, return
no items. `result()` then writes `presence: "absent"` with the path in
`missingInputs`, and hosts show the subject as added or removed rather than
empty. Don't fail, and don't return a bare empty extraction.

In other languages, write the JSON above, with `"presence": "absent"` and the
missing path in `missingInputs` when the subject doesn't exist.

- `graph/v0`: entities and directed relationships between them; the general choice. Built-in extractors present it with `architecture`, `dependencies`, `file-packing`, `gallery`, `graph`, `text-diff`.
- Fields and strictness: no typed `data`, so every `data` field is open JSON (https://github.com/githubnext/projections/blob/main/docs/reference/schemas/graph.md).
Expand Down
20 changes: 17 additions & 3 deletions docs/contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ explaining why. Hosted views never load artifact-supplied renderers.
## One implementation, native and browser

Both the native and browser Engines implement the same request/response contract
(engine ABI version 0), exposing eight operations: `parse-json`, `decode`,
`normalize`, `canonicalize`, `validate-comparison`, `vary`, `pair`, and
`review`.
(engine ABI version 0), exposing nine operations: `parse-json`, `decode`,
`normalize`, `canonicalize`, `validate-comparison`, `vary`, `pair`, `review`,
and `presence`.

- `review` (`{comparison}`, reading only each change's `status`,
`correspondence`, and `varied` fields on an engine-validated comparison)
Expand Down Expand Up @@ -289,6 +289,20 @@ Both the native and browser Engines implement the same request/response contract
Wasm. Code outside the engine never compares digests or definitions on its
own; the `no-restricted-syntax` rules in `tools/scripts/engine-contracts.mjs`
flag any attempt to do so.
- `presence` (`{before, after}`, reading only `presence`, `missingInputs`, and
whether `entities` or `relationships` has an item) says whether what two
extractions describe exists on each side and what the change did to it:
`{before, after, change, inferred}`. Each side is `present`, `absent`, or
`unknown`. `change` is `added`, `removed`, `absent` (on both sides),
`present`, or `unknown`. A side that omits `presence` is inferred `present`
when it has items and `absent` when it has none and names missing inputs, and
is otherwise unknown; `inferred` is true when either side was inferred (see
[presence](contracts/extractions.md#presence)). The CLI calls
`projections_engine::presence_change`, JavaScript hosts call
`engine.presence(before, after)`, and `presenceChange` in
`@githubnext/projections-schemas` mirrors it for callers that can't wait on
the engine; the shared conformance corpus holds all of them to the same
answers.

- `vary` (`{before, after}`) compares two extractions of the same revision—such
as a rerun—item by item by ID without matching. It returns
Expand Down
91 changes: 81 additions & 10 deletions docs/contracts/extractions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

An extraction is the snapshot an extractor produces for a single revision. Every
extraction includes `protocolVersion: 0`, a domain `schema`, `entities`, and
`relationships`, along with optional `volatile`, `missingInputs`, and `metadata`
fields. The seven domain schemas are `graph/v0`, `dag/v0`, `treemap/v0`,
`metrics/v0`, `api/v0`, `database/v0`, and `architecture/v0`. Extractor IDs are
just names—they are not an engine-owned catalog or extension boundary.
`relationships`, along with optional `volatile`, `missingInputs`, `presence`,
and `metadata` fields. The seven domain schemas are `graph/v0`, `dag/v0`,
`treemap/v0`, `metrics/v0`, `api/v0`, `database/v0`, and `architecture/v0`.
Extractor IDs are just names—they are not an engine-owned catalog or extension
boundary.

`missingInputs` records any declared inputs (repository-relative files, folders,
or globs) that do not exist at the examined revision—for instance, an older
Expand All @@ -16,6 +17,9 @@ most 1024 characters with no NULs, up to 256 entries (`duplicate-missing-input`,
field if the list is empty. Runners surface missing inputs as comparison
notices, and they are included in the extraction's canonical bytes.

`presence` says whether what the projection describes, its subject, exists at
the revision; see [Presence](#presence).

An extraction can also declare `vocabularies`—typed conventions layered on top
of its domain schema, such as `dependency-injection/v0`, `infrastructure/v0`,
and `runtime-policy/v0` over `architecture/v0`, or `scenarios/v0` over
Expand Down Expand Up @@ -104,6 +108,73 @@ without normalizing Unicode text. Extractions are bounded to 10,000 entities,
and unknown fields are rejected outright rather than silently dropped—any custom
extension belongs in explicit `data` or `metadata` objects.

## Presence

An extraction's subject is what its projection describes: a workflow file, a
package manifest, a set of schema files. `presence` reports whether it exists at
the examined revision:

- `present`: it exists, though it may be empty. Zero items means "present and
empty".
- `absent`: it doesn't. For example, the revision predates the workflow. An
absent extraction has no entities or relationships, and its `missingInputs`
names what is missing, so a host can say what isn't there.
- Omitted: not reported. That means unknown, never present.

```json
{
"protocolVersion": 0,
"schema": "dag/v0",
"presence": "absent",
"entities": [],
"relationships": [],
"missingInputs": [".github/workflows/release.yml"],
"metadata": {"workflow": ".github/workflows/release.yml"}
}
```

The engine rejects any other value (`invalid-presence`), an absent extraction
with items (`absent-with-items`), and one whose `missingInputs` is empty or
omitted (`absent-without-missing-input`). It keeps `presence` as written, either
value, and includes it in the canonical bytes. An extraction that omits it keeps
the bytes and digest it had before the field existed. When reruns report
different presence, the extractor isn't deterministic: `vary` reports
`/presence` as a difference beyond tolerance.

Presence is separate from the other states an extraction can be in. A missing
auxiliary input, one whose absence leaves the subject in place, is recorded in
`missingInputs` with the subject still `present`. A subject that exists but
can't be read fails the extraction, as before, rather than reporting it absent.
An extractor that describes the whole repository or project always has its
subject: include globs that match nothing select nothing, so the extraction is
present and empty.

Extractors set `presence` through the SDK helpers: `subject_exists` and
`subject_matches` in Rust, `request.subjectExists()` and
`request.subjectMatches()` in Node, `request.subject_exists()` and
`request.subject_matches()` in Python. Each records an anchor of the subject.
The extraction is `absent` when none of the anchors checked exists, and
`present` otherwise. Each missing anchor is also added to `missingInputs` (see
[extractors](extractors.md#an-absent-subject)).

A pair of extractions has one presence state, which the engine derives
(`engine.presence(before, after)`, the `presence` operation) and
`presenceChange` in `@githubnext/projections-schemas` mirrors without the
engine. The shared conformance corpus holds both to the same answers. Each side
is `present` or `absent` as reported. A side that omits `presence` is `unknown`
unless it can be inferred: `present` when it has items, which an absent
extraction can't have, or `absent` when it has none and names missing inputs.
That second rule is the legacy heuristic, and it misreads a present, empty
subject that lacks only an auxiliary input. The pair's `change` is `added`
(absent, then present), `removed` (present, then absent), `absent` (absent on
both sides), `present` (present on both sides), or `unknown` when either side
is. `inferred` is true when either side's presence was inferred. Comparisons
stay item-level: a subject that's added still shows every item as added.

Built-in extractors will write `presence` on every extraction, so their digests
change once when they start. In `extraction/v1`, `presence` will be required,
and the inference will go away.

## Volatile data

Extractions behave much like test results: determinism is the goal, but some
Expand Down Expand Up @@ -235,9 +306,9 @@ Canonical bytes are produced by sorting `entities` and `relationships` by UTF-16
code-unit ID order and serializing with RFC 8785 JSON canonicalization. Omitted
item `data` becomes `{}`. Evidence, previous IDs, and extraction metadata are
excluded, while kind, label, data, confidence, relationship endpoints, IDs,
normalized `volatile` declarations, and `missingInputs` are included. Because
volatile values are still ordinary data, two runs that differ within tolerance
produce different digests. Negative zero canonicalizes to zero, and object keys
and numeric formatting follow RFC 8785 rather than locale sorting or host
insertion order. The extraction digest is the lowercase SHA-256 of these UTF-8
bytes.
normalized `volatile` declarations, `missingInputs`, and `presence` (when
written) are included. Because volatile values are still ordinary data, two runs
that differ within tolerance produce different digests. Negative zero
canonicalizes to zero, and object keys and numeric formatting follow RFC 8785
rather than locale sorting or host insertion order. The extraction digest is the
lowercase SHA-256 of these UTF-8 bytes.
21 changes: 21 additions & 0 deletions docs/contracts/extractors.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,27 @@ cancellation, missing tools, invalid output, or partial extraction, fail
explicitly with a nonzero exit status and a clear message on stderr—never
recover by emitting a valid-looking empty extraction.

## An absent subject

If what the projection describes doesn't exist at the revision, emit an absent
extraction instead of failing or returning a bare empty one. Examples: a
workflow the revision predates, or a manifest it removed. An absent extraction
has `presence: "absent"`, no items, and the missing path in `missingInputs` (see
[presence](extractions.md#presence)). A pull request that adds the subject then
compares an absent base with a present head and shows it as added.

The helpers do this for you. Check each anchor of the subject, meaning a path or
glob that names it, with `subject_exists`/`subject_matches` in the Rust SDK,
`request.subjectExists()`/`request.subjectMatches()` in Node, or
`request.subject_exists()`/`request.subject_matches()` in Python. Each one
records a missing anchor in `missingInputs`. At the end, the extraction is
`absent` when no anchor checked exists and `present` otherwise. An extractor
that finds none of its subject but still returns items fails with a message
naming the anchors. Check auxiliary inputs, whose absence leaves the subject in
place, with Rust's `input_exists` instead; they never affect `presence`. An
extractor that checks no anchor reports no `presence` unless it sets it itself,
and hosts read an unreported presence as unknown.

## Built-in extractors

Built-in extractors use the exact same interface as custom ones. Each runs as
Expand Down
Loading