• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

knowledgepixels / nanopub-query / 27816850935

19 Jun 2026 09:15AM UTC coverage: 61.075% (+0.1%) from 60.944%
27816850935

push

github

web-flow
Merge pull request #123 from knowledgepixels/feat/ref-scope-preset-assignments

Ref-scope preset-assignment listing (#122)

531 of 962 branches covered (55.2%)

Branch coverage included in aggregate %.

1548 of 2442 relevant lines covered (63.39%)

9.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

19.09
src/main/java/com/knowledgepixels/query/AuthorityResolver.java
1
package com.knowledgepixels.query;
2

3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.Optional;
6
import java.util.Set;
7

8
import org.eclipse.rdf4j.common.transaction.IsolationLevels;
9
import org.eclipse.rdf4j.model.IRI;
10
import org.eclipse.rdf4j.model.Statement;
11
import org.eclipse.rdf4j.model.Value;
12
import org.eclipse.rdf4j.model.ValueFactory;
13
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
14
import org.eclipse.rdf4j.model.vocabulary.FOAF;
15
import org.eclipse.rdf4j.model.vocabulary.RDF;
16
import org.eclipse.rdf4j.query.BindingSet;
17
import org.eclipse.rdf4j.query.QueryLanguage;
18
import org.eclipse.rdf4j.query.TupleQueryResult;
19
import org.eclipse.rdf4j.repository.RepositoryConnection;
20
import org.eclipse.rdf4j.repository.RepositoryResult;
21
import org.nanopub.vocabulary.NPA;
22
import org.nanopub.vocabulary.NPX;
23
import org.slf4j.Logger;
24
import org.slf4j.LoggerFactory;
25

26
import com.knowledgepixels.query.vocabulary.GEN;
27
import com.knowledgepixels.query.vocabulary.NPAT;
28
import com.knowledgepixels.query.vocabulary.SpacesVocab;
29

30
/**
31
 * Drives the space-state materialization pipeline. Three entry points scheduled
32
 * by {@code MainVerticle}:
33
 * <ul>
34
 *   <li>{@link #tick()} — detects trust-state flips (full build) and otherwise
35
 *       advances the current space-state graph by an {@link #runIncrementalCycle
36
 *       incremental cycle} bounded by {@code (processedUpTo, currentLoadCounter]}.</li>
37
 *   <li>{@link #periodicRebuildTick()} — checks the {@code npa:needsFullRebuild}
38
 *       flag set by structural invalidations and re-runs the full build into a
39
 *       fresh graph, atomically flips the pointer, drops the old graph.</li>
40
 *   <li>{@link #cleanOrphans()} — startup cleanup of {@code npass:*} graphs the
41
 *       pointer isn't referencing.</li>
42
 * </ul>
43
 *
44
 * <p>Incremental cycle order: invalidation DELETEs (admin RI / RoleAssignment /
45
 * non-admin RI) → mirror-step delta is implicit (rebuilt only on full build) →
46
 * per-tier INSERTs (admin → alias → attachment → maintainer → member → observer) →
47
 * late-arrival sweep (re-run downstream tiers without the load-number filter
48
 * iff this cycle added any structural rows). Sets {@code npa:needsFullRebuild}
49
 * when an admin RI / RoleAssignment / RoleDeclaration was invalidated; periodic
50
 * worker turns the flag into a from-scratch rebuild.
51
 *
52
 * <p>See {@code doc/design-space-repositories.md} — this implements the "Full
53
 * build", "Incremental cycle", and "Periodic full rebuild" procedures.
54
 */
55
public final class AuthorityResolver {
56

57
    private static final Logger logger = LoggerFactory.getLogger(AuthorityResolver.class);
9✔
58

59
    private static final ValueFactory vf = SimpleValueFactory.getInstance();
6✔
60

61
    private static final String SPACES_REPO = "spaces";
62
    private static final String TRUST_REPO = "trust";
63

64
    /** NPA constants pulled in locally (trust-side). */
65
    private static final IRI NPA_HAS_CURRENT_TRUST_STATE =
9✔
66
            vf.createIRI(NPA.NAMESPACE, "hasCurrentTrustState");
6✔
67
    private static final IRI NPA_ACCOUNT_STATE = vf.createIRI(NPA.NAMESPACE, "AccountState");
15✔
68
    private static final IRI NPA_AGENT = vf.createIRI(NPA.NAMESPACE, "agent");
15✔
69
    private static final IRI NPA_PUBKEY = vf.createIRI(NPA.NAMESPACE, "pubkey");
15✔
70
    private static final IRI NPA_TRUST_STATUS = vf.createIRI(NPA.NAMESPACE, "trustStatus");
15✔
71
    private static final IRI NPA_LOADED = vf.createIRI(NPA.NAMESPACE, "loaded");
15✔
72
    private static final IRI NPA_TO_LOAD = vf.createIRI(NPA.NAMESPACE, "toLoad");
15✔
73

74
    /**
75
     * Trust-approved set: rows with one of these {@code npa:trustStatus} values
76
     * are mirrored into the space-state graph. Per
77
     * {@code doc/design-trust-state-repos.md}, these are the two "authority-
78
     * approving" statuses; {@code npa:contested}, {@code npa:skipped}, and the
79
     * transient statuses are distinct values of the same predicate and are
80
     * excluded automatically by this positive-list filter.
81
     */
82
    private static final Set<IRI> APPROVED_SET = Set.of(NPA_LOADED, NPA_TO_LOAD);
15✔
83

84
    private static AuthorityResolver instance;
85

86
    /** Returns the singleton. */
87
    public static synchronized AuthorityResolver get() {
88
        if (instance == null) {
6✔
89
            instance = new AuthorityResolver();
12✔
90
        }
91
        return instance;
6✔
92
    }
93

94
    private AuthorityResolver() {
6✔
95
    }
3✔
96

97
    // ---------------- Operational metrics snapshot ----------------
98
    //
99
    // Updated at the end of each runFullBuild / runIncrementalCycle, read by
100
    // MetricsCollector via the get*() accessors below. volatile is enough —
101
    // writers serialise via the synchronized methods, and readers (Prometheus
102
    // scrapes) only need most-recent visibility, not transactional consistency
103
    // across the snapshot. Defaults to zero values so a scrape that races a
104
    // boot before the first cycle returns 0, not NaN.
105

106
    private volatile TierSubjectTotals lastSubjectTotals = new TierSubjectTotals(0L, 0L, 0L);
24✔
107
    private volatile long lastInsertedTriplesTotal;
108
    private volatile long lastFullBuildDurationMs;
109
    private volatile long lastIncrementalCycleDurationMs;
110
    private volatile long lastProcessedUpToLag;
111

112
    public TierSubjectTotals getLastSubjectTotals() { return lastSubjectTotals; }
9✔
113
    public long getLastInsertedTriplesTotal() { return lastInsertedTriplesTotal; }
9✔
114
    public long getLastFullBuildDurationMs() { return lastFullBuildDurationMs; }
9✔
115
    public long getLastIncrementalCycleDurationMs() { return lastIncrementalCycleDurationMs; }
9✔
116
    public long getLastProcessedUpToLag() { return lastProcessedUpToLag; }
9✔
117

118
    // ---------------- Public entry points ----------------
119

120
    /**
121
     * Poll entry point. Behaviour:
122
     * <ul>
123
     *   <li>If no current space-state graph or the trust state has flipped → full build.</li>
124
     *   <li>Otherwise → {@link #runIncrementalCycle incremental cycle} on the load-number
125
     *       delta {@code (processedUpTo, currentLoadCounter]}. No-op if {@code
126
     *       processedUpTo == currentLoadCounter}.</li>
127
     * </ul>
128
     * Safe to call repeatedly on a schedule. Gated by {@link FeatureFlags#spacesEnabled()}.
129
     */
130
    public void tick() {
131
        if (!FeatureFlags.spacesEnabled()) return;
6!
132
        String trustStateHash = TrustStateRegistry.get().getCurrentHash().orElse(null);
18✔
133
        if (trustStateHash == null) {
6!
134
            logger.debug("AuthorityResolver.tick: no current trust state yet — skipping");
9✔
135
            return;
3✔
136
        }
137
        IRI currentGraph = getCurrentSpaceStateGraph();
×
138
        String currentGraphName = (currentGraph == null) ? null
×
139
                : currentGraph.stringValue().substring(SpacesVocab.NPASS_NAMESPACE.length());
×
140
        if (currentGraphName == null || !currentGraphName.startsWith(trustStateHash + "_")) {
×
141
            logger.info("AuthorityResolver.tick: trust-state flip detected (now {}); running full build",
×
142
                    abbrev(trustStateHash));
×
143
            runFullBuild(trustStateHash);
×
144
            return;
×
145
        }
146
        runIncrementalCycle(currentGraph);
×
147
    }
×
148

149
    /**
150
     * Periodic worker. If {@code npa:needsFullRebuild} was raised by an
151
     * incremental cycle's structural DELETE, runs a from-scratch rebuild into
152
     * a fresh space-state graph (using the current trust-state hash and load
153
     * counter) and clears the flag. No-op when the flag is not set. Safe to
154
     * call concurrently with {@link #tick()} when both are scheduled on the
155
     * same single-threaded executor.
156
     */
157
    public void periodicRebuildTick() {
158
        if (!FeatureFlags.spacesEnabled()) return;
×
159
        if (!readNeedsFullRebuild()) return;
×
160
        String trustStateHash = TrustStateRegistry.get().getCurrentHash().orElse(null);
×
161
        if (trustStateHash == null) {
×
162
            logger.debug("AuthorityResolver.periodicRebuildTick: no current trust state — deferring");
×
163
            return;
×
164
        }
165
        logger.info("AuthorityResolver.periodicRebuildTick: needsFullRebuild flag set; rebuilding");
×
166
        runFullBuild(trustStateHash);
×
167
        clearNeedsFullRebuild();
×
168
    }
×
169

170
    /**
171
     * Startup cleanup: drop any {@code npass:*} graph that the
172
     * {@code npa:hasCurrentSpaceState} pointer isn't pointing at. Orphans come
173
     * from crashes mid-build. Safe to call at any time; idempotent.
174
     */
175
    public void cleanOrphans() {
176
        if (!FeatureFlags.spacesEnabled()) return;
×
177
        IRI current = getCurrentSpaceStateGraph();
×
178
        try (RepositoryConnection conn = TripleStore.get().getRepoConnection(SPACES_REPO)) {
×
179
            int dropped = 0;
×
180
            try (RepositoryResult<org.eclipse.rdf4j.model.Resource> ctxs = conn.getContextIDs()) {
×
181
                List<IRI> toDrop = new ArrayList<>();
×
182
                while (ctxs.hasNext()) {
×
183
                    org.eclipse.rdf4j.model.Resource ctx = ctxs.next();
×
184
                    if (!(ctx instanceof IRI iri)) continue;
×
185
                    if (!iri.stringValue().startsWith(SpacesVocab.NPASS_NAMESPACE)) continue;
×
186
                    if (iri.equals(current)) continue;
×
187
                    toDrop.add(iri);
×
188
                }
×
189
                for (IRI iri : toDrop) {
×
190
                    conn.begin(IsolationLevels.SERIALIZABLE);
×
191
                    conn.clear(iri);
×
192
                    conn.commit();
×
193
                    dropped++;
×
194
                    logger.info("AuthorityResolver.cleanOrphans: dropped orphan graph {}", iri);
×
195
                }
×
196
            }
197
            if (dropped == 0) {
×
198
                logger.debug("AuthorityResolver.cleanOrphans: no orphan space-state graphs");
×
199
            }
200
        } catch (Exception ex) {
×
201
            logger.info("AuthorityResolver.cleanOrphans: failed: {}", ex.toString());
×
202
        }
×
203
    }
×
204

205
    // ---------------- Full build ----------------
206

207
    /**
208
     * Mutex-protected full build of the space-state graph for the given trust
209
     * state. Captures {@code M = currentLoadCounter}, mirrors trust-approved
210
     * rows, (PR 2b: runs per-tier UPDATE loops from scratch), stamps
211
     * {@code processedUpTo = M}, flips the pointer, drops the previous graph.
212
     */
213
    synchronized void runFullBuild(String trustStateHash) {
214
        long startNanos = System.nanoTime();
×
215
        long loadCounter = getCurrentLoadCounter();
×
216
        IRI newGraph = SpacesVocab.forSpaceState(trustStateHash, loadCounter);
×
217
        IRI oldGraph = getCurrentSpaceStateGraph();
×
218
        if (newGraph.equals(oldGraph)) {
×
219
            logger.debug("AuthorityResolver.runFullBuild: already current at {}", newGraph);
×
220
            return;
×
221
        }
222

223
        // 1. Mirror trust-approved rows into the new graph.
224
        int mirrored = mirrorTrustState(trustStateHash, newGraph);
×
225

226
        // 2. Per-tier UPDATE loops (from scratch: lastProcessed = -1 so the
227
        //    delta filter FILTER(?ln > ?lastProcessed) includes everything).
228
        TierInsertedTriples counts = runAllTierLoops(newGraph, -1);
×
229

230
        // 3. Stamp processedUpTo inside the new graph.
231
        writeProcessedUpTo(newGraph, loadCounter);
×
232

233
        // 4. Flip the current-space-state pointer.
234
        flipPointer(newGraph);
×
235

236
        // 5. Drop the old graph if one existed.
237
        if (oldGraph != null) {
×
238
            dropGraph(oldGraph);
×
239
        }
240

241
        TierSubjectTotals totals = computeTierSubjectTotals(newGraph);
×
242
        long durationMs = (System.nanoTime() - startNanos) / 1_000_000L;
×
243
        lastSubjectTotals = totals;
×
244
        lastInsertedTriplesTotal = (long) counts.admin + counts.alias + counts.presetAttachment
×
245
                + counts.presetAssignmentRef
246
                + counts.attachment + counts.maintainer + counts.member + counts.observer
247
                + counts.subSpace + counts.subSpacePrefix + counts.maintainedResource;
248
        lastFullBuildDurationMs = durationMs;
×
249
        lastProcessedUpToLag = 0L;
×
250
        logger.info("AuthorityResolver: full build complete — graph={} mirrored={} rows loadCounter={} "
×
251
                        + "subjects: adminRIs={} attachmentRAs={} nonAdminRIs={} "
252
                        + "(inserted-triples: admin={} alias={} preset-attachment={} preset-assignment-ref={} attachment={} maintainer={} member={} observer={} "
253
                        + "subspace={} subspace-prefix={} maintained-resource={}) durationMs={}",
254
                newGraph, mirrored, loadCounter,
×
255
                totals.adminRIs(), totals.attachmentRAs(), totals.nonAdminRIs(),
×
256
                counts.admin, counts.alias, counts.presetAttachment, counts.presetAssignmentRef, counts.attachment, counts.maintainer, counts.member, counts.observer,
×
257
                counts.subSpace, counts.subSpacePrefix, counts.maintainedResource,
×
258
                durationMs);
×
259
    }
×
260

261
    // ---------------- Incremental cycle ----------------
262

263
    /**
264
     * Single delta cycle on the current space-state graph. Bounded by
265
     * {@code (processedUpTo, currentLoadCounter]}; no-op if the range is empty.
266
     *
267
     * <p>Order:
268
     * <ol>
269
     *   <li>Apply invalidation DELETEs (admin RI, RoleAssignment, non-admin RI)
270
     *       and the RoleDeclaration ASK. Any DELETE on a structural kind sets
271
     *       {@code npa:needsFullRebuild} to bound the staleness from sticky
272
     *       downstream entries; the periodic worker turns that into a from-scratch
273
     *       rebuild on its next pass.</li>
274
     *   <li>Run per-tier INSERTs in the same order as the full build.</li>
275
     *   <li>Late-arrival sweep: if any structural row was added, re-run downstream
276
     *       tier INSERTs with {@code lastProcessed = -1} to catch candidates whose
277
     *       enabling event landed in this same cycle. Dedup filters protect
278
     *       against double-insert.</li>
279
     *   <li>Bump {@code processedUpTo} to {@code currentLoadCounter}.</li>
280
     * </ol>
281
     */
282
    synchronized void runIncrementalCycle(IRI graph) {
283
        long startNanos = System.nanoTime();
×
284
        long currentLoadCounter = getCurrentLoadCounter();
×
285
        long lastProcessed = readProcessedUpTo(graph);
×
286
        if (lastProcessed < 0) {
×
287
            logger.warn("AuthorityResolver.runIncrementalCycle: missing processedUpTo on {}; skipping",
×
288
                    graph);
289
            return;
×
290
        }
291
        lastProcessedUpToLag = currentLoadCounter - lastProcessed;
×
292
        if (currentLoadCounter <= lastProcessed) {
×
293
            logger.debug("AuthorityResolver.runIncrementalCycle: caught up at load {} on {}",
×
294
                    currentLoadCounter, graph);
×
295
            return;
×
296
        }
297

298
        boolean structuralInvalidation = applyInvalidations(graph, lastProcessed);
×
299
        TierInsertedTriples counts = runAllTierLoops(graph, lastProcessed);
×
300
        boolean structuralAdds = (counts.admin > 0)
×
301
                || (counts.alias > 0)
302
                || (counts.presetAttachment > 0)
303
                || (counts.attachment > 0)
304
                || (counts.subSpace > 0)
305
                || newRoleDeclarationsArrived(lastProcessed)
×
306
                || newPresetAssignmentsArrived(lastProcessed);
×
307
        if (structuralAdds) {
×
308
            // Late-arrival sweep: leaf tiers (attachment/maintainer/member/observer)
309
            // can promote candidates whose enabling event arrived in this same cycle.
310
            // Sub-space admit is also re-run here for Mode-B late-arrival (a new
311
            // partner declaration can validate an older primary that the regular
312
            // pass's load-number filter excluded). The URL-prefix fallback also
313
            // re-runs so newly-orphaned children pick up derived edges. Skip the
314
            // admin tier — its only enabling event is the admin grant itself,
315
            // already handled by the regular pass.
316
            TierInsertedTriples lateCounts = runDownstreamWithoutLoadFilter(graph);
×
317
            counts.alias              += lateCounts.alias;
×
318
            counts.presetAttachment   += lateCounts.presetAttachment;
×
319
            counts.presetAssignmentRef += lateCounts.presetAssignmentRef;
×
320
            counts.attachment         += lateCounts.attachment;
×
321
            counts.maintainer         += lateCounts.maintainer;
×
322
            counts.member             += lateCounts.member;
×
323
            counts.observer           += lateCounts.observer;
×
324
            counts.subSpace           += lateCounts.subSpace;
×
325
            counts.subSpacePrefix     += lateCounts.subSpacePrefix;
×
326
            counts.maintainedResource += lateCounts.maintainedResource;
×
327
        }
328

329
        writeProcessedUpTo(graph, currentLoadCounter);
×
330

331
        TierSubjectTotals totals = computeTierSubjectTotals(graph);
×
332
        long durationMs = (System.nanoTime() - startNanos) / 1_000_000L;
×
333
        lastSubjectTotals = totals;
×
334
        lastInsertedTriplesTotal = (long) counts.admin + counts.alias + counts.presetAttachment
×
335
                + counts.presetAssignmentRef
336
                + counts.attachment + counts.maintainer + counts.member + counts.observer
337
                + counts.subSpace + counts.subSpacePrefix + counts.maintainedResource;
338
        lastIncrementalCycleDurationMs = durationMs;
×
339
        logger.info("AuthorityResolver: incremental cycle complete — graph={} delta=({}, {}] "
×
340
                        + "subjects: adminRIs={} attachmentRAs={} nonAdminRIs={} "
341
                        + "(inserted-triples: admin={} alias={} preset-attachment={} preset-assignment-ref={} attachment={} maintainer={} member={} observer={} "
342
                        + "subspace={} subspace-prefix={} maintained-resource={}) "
343
                        + "structuralInvalidation={} structuralAdds={} durationMs={}",
344
                graph, lastProcessed, currentLoadCounter,
×
345
                totals.adminRIs(), totals.attachmentRAs(), totals.nonAdminRIs(),
×
346
                counts.admin, counts.alias, counts.presetAttachment, counts.presetAssignmentRef, counts.attachment, counts.maintainer, counts.member, counts.observer,
×
347
                counts.subSpace, counts.subSpacePrefix, counts.maintainedResource,
×
348
                structuralInvalidation, structuralAdds, durationMs);
×
349
    }
×
350

351
    /**
352
     * Runs the four invalidation-DELETE / ASK steps. Sets {@code npa:needsFullRebuild}
353
     * when admin-RI, RoleAssignment, or RoleDeclaration invalidations matched (the
354
     * three structural kinds). Leaf-tier RI deletes don't set the flag.
355
     *
356
     * @return true iff at least one structural kind was invalidated
357
     */
358
    boolean applyInvalidations(IRI graph, long lastProcessed) {
359
        boolean structural = false;
×
360
        if (wouldInvalidate(graph, lastProcessed, /*adminPinned=*/ true,
×
361
                            adminInvalidationCheckWhere(graph, lastProcessed))) {
×
362
            executeUpdate(adminInvalidationDelete(graph, lastProcessed));
×
363
            structural = true;
×
364
        }
365
        if (wouldInvalidate(graph, lastProcessed, /*adminPinned=*/ false,
×
366
                            roleAssignmentInvalidationCheckWhere(graph, lastProcessed))) {
×
367
            executeUpdate(roleAssignmentInvalidationDelete(graph, lastProcessed));
×
368
            structural = true;
×
369
        }
370
        // Role-declaration invalidation is deliberately NOT acted on (see
371
        // nonAdminTierUpdate): a role assignment is governed by the admin-validated
372
        // attachment, not by the declaration author's later supersession/retraction, so
373
        // an invalidated RD neither deletes rows nor triggers a rebuild.
374
        // Sub-space declarations are structural — invalidating one (Mode A) or one
375
        // of two co-declarations (Mode B) changes the validated parent/child
376
        // topology. The DELETE removes the per-declaration row; the convenience
377
        // direct triples are left sticky and cleaned on the next periodic rebuild.
378
        if (wouldInvalidate(graph, lastProcessed, /*adminPinned=*/ false,
×
379
                            subSpaceInvalidationCheckWhere(graph, lastProcessed))) {
×
380
            executeUpdate(subSpaceInvalidationDelete(graph, lastProcessed));
×
381
            structural = true;
×
382
        }
383
        // Space-alias declarations are structural — invalidating one removes an
384
        // owl:sameAs edge that feeds the admin-authority closure (issue #113). The
385
        // DELETE removes the per-declaration row; the convenience npa:sameAsSpace edge
386
        // is left sticky and cleaned on the next periodic rebuild (same policy as
387
        // sub-space declarations).
388
        if (wouldInvalidate(graph, lastProcessed, /*adminPinned=*/ false,
×
389
                            aliasInvalidationCheckWhere(graph, lastProcessed))) {
×
390
            executeUpdate(aliasInvalidationDelete(graph, lastProcessed));
×
391
            structural = true;
×
392
        }
393
        // Preset-derived RoleAssignment removal (issue #302). NOT npx:invalidates: a newer
394
        // admin-authored same-(preset,resource) assignment supersedes by dct:created (a
395
        // gen:DeactivatedPresetAssignment, or any newer assignment that is no longer active).
396
        // Structural — sticky downstream non-admin RIs derived through a removed attachment
397
        // are bounded by the periodic full rebuild. The DELETE is scoped by
398
        // npa:derivedFromPreset so directly-published gen:hasRole attachments are never
399
        // touched; the §4.3 re-INSERT re-materializes only currently-active pairs in the same
400
        // cycle. See doc/design-preset-role-materialization.md §4.4.
401
        if (wouldInvalidate(graph, lastProcessed, /*adminPinned=*/ false,
×
402
                            presetDeactivationCheckWhere(graph, lastProcessed))) {
×
403
            executeUpdate(presetDeactivationDelete(graph, lastProcessed));
×
404
            structural = true;
×
405
        }
406
        // Leaf-tier RI deletes — no flag.
407
        executeUpdate(leafTierInvalidationDelete(graph, lastProcessed));
×
408
        // Ref-scoped preset-assignment listing stamps whose assignment nanopub was
409
        // hard-retracted (issue #122) — no flag (display leaf, nothing downstream).
410
        executeUpdate(presetAssignmentRefInvalidationDelete(graph, lastProcessed));
×
411
        // Maintained-resource declaration deletes — no flag (leaf relation, no
412
        // downstream caches to bound).
413
        executeUpdate(maintainedResourceInvalidationDelete(graph, lastProcessed));
×
414
        if (structural) setNeedsFullRebuild();
×
415
        return structural;
×
416
    }
417

418
    /**
419
     * Runs the four leaf tiers (attachment/maintainer/member/observer) with
420
     * {@code lastProcessed = -1} so the load-number filter on the candidate
421
     * side admits everything. Dedup filters in the tier templates prevent
422
     * double-insert. Used by the late-arrival sweep.
423
     */
424
    TierInsertedTriples runDownstreamWithoutLoadFilter(IRI graph) {
425
        TierInsertedTriples c = new TierInsertedTriples();
×
426
        // Alias late-arrival: catches alias declarations whose canonical admin grant
427
        // became valid only in this same cycle (the load-number filter on the
428
        // declaration's nanopub would otherwise exclude it). Runs first so the
429
        // attachment / role tiers below see this cycle's fresh npa:sameAsSpace edges.
430
        c.alias = runTierLabeled("alias(late)", graph, aliasAdmitUpdate(graph, -1));
×
431
        // Sub-space late-arrival: catches Mode-B candidates whose primary
432
        // declaration is older than lastProcessed but whose partner just landed.
433
        c.subSpace = runTierLabeled("subspace(late)", graph,
×
434
                subSpaceAdmitUpdate(graph, -1));
×
435
        // Maintained-resource late-arrival: catches declarations that landed
436
        // before the publisher's admin grant became valid in this state.
437
        c.maintainedResource = runTierLabeled("maintained-resource(late)", graph,
×
438
                maintainedResourceAdmitUpdate(graph, -1));
×
439
        // URL-prefix fallback: re-run after the late-arrival sub-space admit so
440
        // any newly-validated children get their fallback edges suppressed (for
441
        // future inserts) and any newly-orphaned children pick up fallback edges.
442
        c.subSpacePrefix = runTierLabeled("subspace-prefix(late)", graph,
×
443
                subSpacePrefixFallbackUpdate(graph));
×
444
        // Preset-attachment late-arrival: catches assignments whose preset declaration or
445
        // admin grant only became valid in this same cycle. Runs before attachment(late)
446
        // so the non-admin late tiers below see this cycle's fresh preset-derived RAs.
447
        c.presetAttachment = runTierLabeled("preset-attachment(late)", graph,
×
448
                presetAttachmentValidationUpdate(graph, -1));
×
449
        // Ref-scoped preset-assignment late stamp: catches assignments whose authorizing
450
        // admin grant only became valid this cycle (the load filter would skip the older
451
        // assignment nanopub). Mirrors the preset-attachment late sweep above.
452
        c.presetAssignmentRef = runTierLabeled("preset-assignment-ref(late)", graph,
×
453
                presetAssignmentRefStampUpdate(graph, -1));
×
454
        c.attachment = runTierLabeled("attachment(late)", graph,
×
455
                attachmentValidationUpdate(graph, -1));
×
456
        c.maintainer = runTierLabeled("maintainer(late)", graph,
×
457
                nonAdminTierUpdate(graph, -1, GEN.MAINTAINER_ROLE, PUBLISHER_IS_ADMIN));
×
458
        c.member = runTierLabeled("member(admin-pub,late)", graph,
×
459
                nonAdminTierUpdate(graph, -1, GEN.MEMBER_ROLE, PUBLISHER_IS_ADMIN));
×
460
        c.member += runTierLabeled("member(maint-pub,late)", graph,
×
461
                nonAdminTierUpdate(graph, -1,
×
462
                        GEN.MEMBER_ROLE, publisherIsTieredRole(GEN.MAINTAINER_ROLE)));
×
463
        c.observer = runTierLabeled("observer(admin-pub,late)", graph,
×
464
                nonAdminTierUpdate(graph, -1, GEN.OBSERVER_ROLE, PUBLISHER_IS_ADMIN));
×
465
        c.observer += runTierLabeled("observer(maint-pub,late)", graph,
×
466
                nonAdminTierUpdate(graph, -1,
×
467
                        GEN.OBSERVER_ROLE, publisherIsTieredRole(GEN.MAINTAINER_ROLE)));
×
468
        c.observer += runTierLabeled("observer(member-pub,late)", graph,
×
469
                nonAdminTierUpdate(graph, -1,
×
470
                        GEN.OBSERVER_ROLE, publisherIsTieredRole(GEN.MEMBER_ROLE)));
×
471
        c.observer += runTierLabeled("observer(self,late)", graph,
×
472
                nonAdminTierUpdate(graph, -1, GEN.OBSERVER_ROLE, PUBLISHER_IS_SELF));
×
473
        return c;
×
474
    }
475

476
    /**
477
     * Cheap ASK: did any new {@code npa:RoleDeclaration} extraction land in the
478
     * load-number delta {@code (lastProcessed, ∞)}? Used by the late-arrival
479
     * trigger so an RD that arrives in the same cycle as a matching candidate
480
     * still gets validated.
481
     */
482
    boolean newRoleDeclarationsArrived(long lastProcessed) {
483
        String ask = String.format("""
×
484
                PREFIX npa: <%1$s>
485
                ASK {
486
                  GRAPH <%2$s> {
487
                    ?rd a npa:RoleDeclaration ;
488
                        npa:viaNanopub ?np .
489
                  }
490
                  GRAPH <%3$s> {
491
                    ?np npa:hasLoadNumber ?ln .
492
                    FILTER (?ln > %4$d)
493
                  }
494
                }
495
                """, NPA.NAMESPACE, SpacesVocab.SPACES_GRAPH, NPA.GRAPH, lastProcessed);
×
496
        return runAsk(ask);
×
497
    }
498

499
    /**
500
     * Cheap ASK: did any new {@code npa:PresetAssignment} or {@code npa:PresetDeclaration}
501
     * extraction land in the load-number delta {@code (lastProcessed, ∞)}? Drives the
502
     * late-arrival re-run so a preset assignment that arrives in the same cycle as its
503
     * declaration (or admin grant) still materializes, and so an arriving newer assignment
504
     * triggers the deactivation/latest-wins re-evaluation.
505
     */
506
    boolean newPresetAssignmentsArrived(long lastProcessed) {
507
        String ask = String.format("""
×
508
                PREFIX npa: <%1$s>
509
                ASK {
510
                  GRAPH <%2$s> {
511
                    ?x a ?t ;
512
                       npa:viaNanopub ?np .
513
                    FILTER (?t = npa:PresetAssignment || ?t = npa:PresetDeclaration)
514
                  }
515
                  GRAPH <%3$s> {
516
                    ?np npa:hasLoadNumber ?ln .
517
                    FILTER (?ln > %4$d)
518
                  }
519
                }
520
                """, NPA.NAMESPACE, SpacesVocab.SPACES_GRAPH, NPA.GRAPH, lastProcessed);
×
521
        return runAsk(ask);
×
522
    }
523

524
    // ---------------- Tier UPDATE loops ----------------
525

526
    /**
527
     * Per-tier inserted-triple tallies for one build or cycle. Counts the sum
528
     * of {@code (graphSize_after - graphSize_before)} across all iterations of
529
     * each tier's fixed-point INSERT loop — i.e. inserted *triples*, not
530
     * distinct subjects (a single RoleInstantiation insert writes 4–5 triples).
531
     *
532
     * <p>Used internally by the {@link #runIncrementalCycle structuralAdds}
533
     * boolean check (we only care whether any tier inserted at all).
534
     * Not what the log lines report: see {@link TierSubjectTotals} +
535
     * {@link #computeTierSubjectTotals} for the distinct-subject totals
536
     * surfaced to operators.
537
     */
538
    static final class TierInsertedTriples {
×
539
        int admin;
540
        int alias;
541
        int presetAttachment;
542
        int presetAssignmentRef;
543
        int attachment;
544
        int maintainer;
545
        int member;
546
        int observer;
547
        int subSpace;
548
        int subSpacePrefix;
549
        int maintainedResource;
550
    }
551

552
    /**
553
     * Snapshot of distinct-subject totals in a space-state graph at a moment
554
     * in time. Independent of which tier-loop added each subject.
555
     */
556
    record TierSubjectTotals(long adminRIs, long attachmentRAs, long nonAdminRIs) {}
36✔
557

558
    /**
559
     * Runs the five tier loops in order: admin → {@code gen:hasRole} attachment
560
     * validation → maintainer → member → observer. Each loop iterates a SPARQL
561
     * INSERT to fixed point (no new triples added). Returns per-tier counts.
562
     *
563
     * @param graph         target space-state graph
564
     * @param lastProcessed load-number horizon; use {@code -1} for full build
565
     */
566
    TierInsertedTriples runAllTierLoops(IRI graph, long lastProcessed) {
567
        TierInsertedTriples c = new TierInsertedTriples();
×
568
        c.admin = runTierLabeled("admin", graph, adminTierUpdate(graph, lastProcessed));
×
569
        // Alias admit runs after the admin closure has settled (both the authority
570
        // gate and the anti-hijack check read the admin set) and before attachment /
571
        // role tiers (their alias-aware admin lookups consume the npa:sameAsSpace edge
572
        // this pass emits). See issue #113.
573
        c.alias = runTierLabeled("alias", graph, aliasAdmitUpdate(graph, lastProcessed));
×
574
        // Sub-space admit runs after admin closure has settled (Mode A + Mode B both
575
        // need the admin set). Independent of role tiers — order between subspace
576
        // and attachment / maintainer / member / observer doesn't matter.
577
        c.subSpace = runTierLabeled("subspace", graph, subSpaceAdmitUpdate(graph, lastProcessed));
×
578
        // Maintained-resource admit also depends only on the admin closure. Single
579
        // Mode A: publisher must be admin of the maintaining space. No co-declaration
580
        // partner, no URL-prefix fallback.
581
        c.maintainedResource = runTierLabeled("maintained-resource", graph,
×
582
                maintainedResourceAdmitUpdate(graph, lastProcessed));
×
583
        // URL-prefix sub-space fallback runs after the explicit-declaration admit
584
        // pass commits so the per-child suppression check sees this cycle's fresh
585
        // validations. No load filter — depends on which Spaces exist, not on
586
        // delta-arrivals; the dedup FILTER NOT EXISTS prevents re-insertion.
587
        c.subSpacePrefix = runTierLabeled("subspace-prefix", graph,
×
588
                subSpacePrefixFallbackUpdate(graph));
×
589
        // Preset-attachment runs immediately before the regular attachment tier so the
590
        // gen:RoleAssignment rows it materializes (from active, admin-authored preset
591
        // assignments) are picked up by the downstream non-admin tiers in the same pass,
592
        // exactly like directly-published attachments. See
593
        // doc/design-preset-role-materialization.md.
594
        c.presetAttachment = runTierLabeled("preset-attachment", graph,
×
595
                presetAttachmentValidationUpdate(graph, lastProcessed));
×
596
        // Ref-scoped preset-assignment listing stamp (issue #122). Display-only leaf —
597
        // independent of the role tiers and of structuralAdds; order doesn't matter.
598
        c.presetAssignmentRef = runTierLabeled("preset-assignment-ref", graph,
×
599
                presetAssignmentRefStampUpdate(graph, lastProcessed));
×
600
        c.attachment = runTierLabeled("attachment", graph,
×
601
                attachmentValidationUpdate(graph, lastProcessed));
×
602
        c.maintainer = runTierLabeled("maintainer", graph, nonAdminTierUpdate(graph, lastProcessed,
×
603
                GEN.MAINTAINER_ROLE, PUBLISHER_IS_ADMIN));
604
        // Member tier: admin OR maintainer publisher — split into two simpler updates
605
        // so the query planner doesn't struggle with the UNION.
606
        c.member = runTierLabeled("member(admin-pub)", graph, nonAdminTierUpdate(graph, lastProcessed,
×
607
                GEN.MEMBER_ROLE, PUBLISHER_IS_ADMIN));
608
        c.member += runTierLabeled("member(maint-pub)", graph, nonAdminTierUpdate(graph, lastProcessed,
×
609
                GEN.MEMBER_ROLE, publisherIsTieredRole(GEN.MAINTAINER_ROLE)));
×
610
        // Observer tier: self-evidence OR a downward grant from any higher tier.
611
        // ObserverRole is the default tier when a role definition omits an
612
        // explicit subclass (see "Role types" in design-space-repositories.md), so
613
        // most "X assigned Y this role" nanopubs land here. Restricting the tier
614
        // to PUBLISHER_IS_SELF would silently drop those grants. The four
615
        // sub-loops mirror the trust-state's downward-only chain: admin grants
616
        // anything; maintainers and members grant observer; everyone may
617
        // self-attest.
618
        c.observer = runTierLabeled("observer(admin-pub)", graph, nonAdminTierUpdate(graph, lastProcessed,
×
619
                GEN.OBSERVER_ROLE, PUBLISHER_IS_ADMIN));
620
        c.observer += runTierLabeled("observer(maint-pub)", graph, nonAdminTierUpdate(graph, lastProcessed,
×
621
                GEN.OBSERVER_ROLE, publisherIsTieredRole(GEN.MAINTAINER_ROLE)));
×
622
        c.observer += runTierLabeled("observer(member-pub)", graph, nonAdminTierUpdate(graph, lastProcessed,
×
623
                GEN.OBSERVER_ROLE, publisherIsTieredRole(GEN.MEMBER_ROLE)));
×
624
        c.observer += runTierLabeled("observer(self)", graph, nonAdminTierUpdate(graph, lastProcessed,
×
625
                GEN.OBSERVER_ROLE, PUBLISHER_IS_SELF));
626
        return c;
×
627
    }
628

629
    /**
630
     * Builds a publisher constraint requiring the publisher to be a validated holder
631
     * of the given tier's role (maintainer or member) in the target space.
632
     * Owns its own AccountState resolution so ?publisher is bound through the
633
     * targeted (pkh → agent) lookup rather than enumerated.
634
     */
635
    private static String publisherIsTieredRole(IRI tierClass) {
636
        // Re-keyed on the assignment's ref (alias → canonical already resolved by the
637
        // attachment tier). Relies on materialized non-admin RIs carrying their role
638
        // property (npa:regularProperty / npa:inverseProperty) — supplied by the
639
        // enrichment in nonAdminTierUpdate; without it this constraint matched nothing.
640
        return """
×
641
                ?acct a npa:AccountState ;
642
                      npa:pubkey ?pkh ;
643
                      npa:agent  ?publisher .
644
                ?tierRI a gen:RoleInstantiation ;
645
                        npa:forSpaceRef ?spaceRef ;
646
                        npa:forAgent ?publisher .
647
                ?rdT a npa:RoleDeclaration ;
648
                     npa:hasRoleType <%1$s> .
649
                { ?tierRI npa:regularProperty ?predT . ?rdT gen:hasRegularProperty ?predT . }
650
                UNION
651
                { ?tierRI npa:inverseProperty ?predT . ?rdT gen:hasInverseProperty ?predT . }
652
                """.formatted(tierClass);
×
653
    }
654

655
    /** Wraps {@link #runTierLoop} with tier-name context for logs/exceptions. */
656
    private int runTierLabeled(String tier, IRI graph, String sparqlUpdate) {
657
        try {
658
            return runTierLoop(graph, sparqlUpdate);
×
659
        } catch (RuntimeException ex) {
×
660
            logger.error("AuthorityResolver: tier={} failed with SPARQL UPDATE:\n{}\n", tier, sparqlUpdate, ex);
×
661
            throw ex;
×
662
        }
663
    }
664

665
    /**
666
     * Runs a single tier's INSERT to fixed point. Counts rows by probing
667
     * graph size before/after each INSERT; stops when the size doesn't change.
668
     *
669
     * @return total number of triples inserted by this tier across all iterations
670
     */
671
    int runTierLoop(IRI graph, String sparqlUpdate) {
672
        int total = 0;
×
673
        long before = graphSize(graph);
×
674
        while (true) {
675
            // Note: no explicit transaction wrapping here. In tests we observed that
676
            // HTTPRepository's RDF4J-transaction protocol silently no-op'd cross-graph
677
            // SPARQL UPDATEs with UNION sub-patterns inside conn.begin()/commit(),
678
            // while the same UPDATE POSTed directly to /statements applied correctly.
679
            // A bare prepareUpdate().execute() takes the direct /statements path and
680
            // runs the UPDATE atomically per SPARQL 1.1 semantics — which is all we
681
            // need; there's nothing else to commit atomically alongside the UPDATE.
682
            try (RepositoryConnection conn = TripleStore.get().getRepoConnection(SPACES_REPO)) {
×
683
                conn.prepareUpdate(QueryLanguage.SPARQL, sparqlUpdate).execute();
×
684
            }
685
            long after = graphSize(graph);
×
686
            long added = after - before;
×
687
            if (added <= 0) break;
×
688
            total += added;
×
689
            before = after;
×
690
        }
×
691
        return total;
×
692
    }
693

694
    private long graphSize(IRI graph) {
695
        try (RepositoryConnection conn = TripleStore.get().getRepoConnection(SPACES_REPO)) {
×
696
            return conn.size(graph);
×
697
        }
698
    }
699

700
    /**
701
     * Distinct-subject totals in the given space-state graph, broken down by
702
     * RoleInstantiation kind (admin-pinned vs not) and RoleAssignment.
703
     * Three SELECT-COUNT queries — cheap, called once per build/cycle for
704
     * the user-facing log line. Returns zeros on failure (logged) so a flaky
705
     * count read can't wedge the cycle.
706
     */
707
    TierSubjectTotals computeTierSubjectTotals(IRI graph) {
708
        long adminRIs       = countDistinctSubjects(graph, """
×
709
                ?ri a gen:RoleInstantiation ; npa:inverseProperty gen:hasAdmin .
710
                """, "ri");
711
        long attachmentRAs  = countDistinctSubjects(graph, """
×
712
                ?ra a gen:RoleAssignment .
713
                """, "ra");
714
        long nonAdminRIs    = countDistinctSubjects(graph, """
×
715
                ?ri a gen:RoleInstantiation .
716
                FILTER NOT EXISTS { ?ri npa:inverseProperty gen:hasAdmin }
717
                """, "ri");
718
        return new TierSubjectTotals(adminRIs, attachmentRAs, nonAdminRIs);
×
719
    }
720

721
    private long countDistinctSubjects(IRI graph, String wherePattern, String varName) {
722
        String query = String.format("""
×
723
                PREFIX npa: <%1$s>
724
                PREFIX gen: <%2$s>
725
                SELECT (COUNT(DISTINCT ?%3$s) AS ?n) WHERE {
726
                  GRAPH <%4$s> {
727
                    %5$s
728
                  }
729
                }
730
                """, NPA.NAMESPACE, GEN.NAMESPACE, varName, graph, wherePattern);
731
        try (RepositoryConnection conn = TripleStore.get().getRepoConnection(SPACES_REPO);
×
732
             TupleQueryResult r = conn.prepareTupleQuery(QueryLanguage.SPARQL, query).evaluate()) {
×
733
            if (!r.hasNext()) return 0;
×
734
            return Long.parseLong(r.next().getBinding("n").getValue().stringValue());
×
735
        } catch (Exception ex) {
×
736
            logger.warn("AuthorityResolver: countDistinctSubjects on {} failed: {}",
×
737
                    graph, ex.toString());
×
738
            return 0;
×
739
        }
740
    }
741

742
    // ---------------- SPARQL templates ----------------
743

744
    /**
745
     * Reusable invalidation filter on a bound nanopub-IRI variable. Pass the bare
746
     * variable name (no leading {@code ?}); e.g. {@code invalidationFilter("np")}
747
     * produces an outer-scoped {@code FILTER NOT EXISTS { GRAPH npa:graph
748
     * { ?_inv_np npx:invalidates ?np . } }}.
749
     *
750
     * <p>Joins on the raw {@code npx:invalidates} triple in {@code npa:graph},
751
     * which {@link com.knowledgepixels.query.NanopubLoader} writes into the
752
     * spaces repo from two complementary directions, making the filter symmetric
753
     * in load order:
754
     * <ul>
755
     *   <li>At the invalidator's own load: the loader's space-repo trigger fires
756
     *       whenever the nanopub has either its own space-relevant extractions
757
     *       OR an {@code npx:invalidates}/{@code npx:retracts}/{@code npx:supersedes}
758
     *       triple, so a pure-retraction nanopub still lands its raw triple plus
759
     *       {@code npa:hasLoadNumber} stamp in {@code npa:graph}.</li>
760
     *   <li>At the invalidated target's load (when the invalidator landed
761
     *       earlier): {@code NanopubLoader.getInvalidatingStatements} reads the
762
     *       triple back from the meta repo and mirrors it into the target's own
763
     *       write to the spaces repo.</li>
764
     * </ul>
765
     *
766
     * <p>The earlier shape joined on a structured {@code npa:Invalidation} entry
767
     * in {@code npa:spacesGraph} that was only emitted on the invalidator's side
768
     * AND only when the invalidated target's meta had already loaded, leaving a
769
     * window where a superseding nanopub loaded before its target produced no
770
     * entry and the stale row was never filtered out (see also the matching
771
     * change in the tier-specific {@code *InvalidationCheckWhere}/{@code
772
     * *InvalidationDelete} templates below).
773
     *
774
     * <p>Important: this filter must be placed OUTSIDE the surrounding
775
     * {@code GRAPH npa:spacesGraph { ... }} block, not nested inside it. When
776
     * nested, RDF4J's planner couples the FILTER NOT EXISTS evaluation into the
777
     * join order (per-row scan multiplied by the candidate set), which we
778
     * measured turning a 39ms query into a 60s+ timeout on the live observer-tier
779
     * data. Outside the GRAPH block, the planner defers the filter until
780
     * {@code ?np}/{@code ?rdNp} are bound and does a targeted index lookup.
781
     *
782
     * <p>Variable names must match {@code [A-Za-z0-9_]+} per SPARQL grammar —
783
     * embedding a {@code ?} inside {@code ?_inv_?np} would yield a parse error.
784
     */
785
    private static String invalidationFilter(String bareVarName) {
786
        return "FILTER NOT EXISTS { GRAPH <" + NPA.GRAPH + "> {"
30✔
787
                + " ?_inv_" + bareVarName
788
                + " <" + NPX.INVALIDATES + "> ?" + bareVarName + " . "
789
                + samePublisherClause("_inv_" + bareVarName, bareVarName)
6✔
790
                + " } }";
791
    }
792

793
    /**
794
     * SPARQL triple pair (placed inside a {@code GRAPH npa:graph { ... }} block)
795
     * requiring the invalidating nanopub and its target to share a signing public
796
     * key — the self-retraction authority gate for issue #112. Without it, the
797
     * materializer honors {@code npx:invalidates}/{@code retracts}/{@code supersedes}
798
     * from <em>any</em> validly-signed nanopub, so any agent can erase another
799
     * space's materialized state (griefing/DoS of the view — fail-closed, no
800
     * privilege escalation, but real). Additions are already admin-gated; this is
801
     * the symmetric gate on removals.
802
     *
803
     * <p>Both {@code npa:hasValidSignatureForPublicKeyHash} triples live in
804
     * {@code npa:graph} of the spaces repo: the target via its own space-load, the
805
     * invalidator via the symmetric retractor propagation in
806
     * {@link com.knowledgepixels.query.NanopubLoader} (forward {@code
807
     * loadInvalidateStatements} + reverse {@code loadInvalidatorIntoSpacesRepo}),
808
     * so the join is populated regardless of load order.
809
     *
810
     * <p>"Same pubkey" is intentionally stricter than "same agent": a retraction
811
     * signed by a different key the author owns (key rotation) is not honored, and
812
     * cross-admin supersession is out of scope here (would need an admin-authority
813
     * arm). The pubkey-bridge variable is suffixed with {@code targetVar} so two
814
     * filters in one query (e.g. on {@code ?np} and {@code ?rdNp}) don't collide.
815
     *
816
     * @param invVar    invalidator nanopub variable name (no leading {@code ?})
817
     * @param targetVar invalidated-target nanopub variable name (no leading {@code ?})
818
     */
819
    private static String samePublisherClause(String invVar, String targetVar) {
820
        String pk = "?_invpk_" + targetVar;
9✔
821
        return "?" + invVar + " <" + NPA.HAS_VALID_SIGNATURE_FOR_PUBLIC_KEY_HASH + "> " + pk + " . "
30✔
822
                + "?" + targetVar + " <" + NPA.HAS_VALID_SIGNATURE_FOR_PUBLIC_KEY_HASH + "> " + pk + " .";
823
    }
824

825
    /**
826
     * Admin tier: seed from {@code npadef:...hasRootAdmin} (trusted by construction)
827
     * plus closed-over admin grants; insert any {@code gen:RoleInstantiation} with
828
     * {@code npa:inverseProperty gen:hasAdmin} whose publisher (resolved via mirrored
829
     * trust-approved AccountState) is already in the admin set.
830
     *
831
     * <p>The seed is gated by {@link #spaceRefAliveFilter} (not the per-nanopub
832
     * {@code invalidationFilter("defNp")}): the {@code hasRootAdmin} seed is anchored
833
     * to the root NPID, which is the immutable space-ref identity, so superseding the
834
     * root <em>nanopub</em> with a continuation revision must not strip the seed —
835
     * only retracting every definition of the ref removes it. See issue #110.
836
     */
837
    static String adminTierUpdate(IRI graph, long lastProcessed) {
838
        // Order tuned for RDF4J's evaluator:
839
        //   1. Anchor on the small (seed UNION closed-over) set to bind ?publisher
840
        //      and ?space cheaply.
841
        //   2. Resolve ?pkh from the mirrored AccountState row (?publisher bound).
842
        //   3. Probe instantiations using the now-bound (?space, ?pkh) — targeted
843
        //      lookup, not a full RoleInstantiation scan.
844
        //   4. Load-number filter on bound ?np.
845
        //   5. Dedup at the end.
846
        // Authority is keyed on the space *ref* (npa:forSpaceRef), not the bare Space
847
        // IRI: two refs that share an IRI but have different roots are independent
848
        // domains (see doc/design-spaceref-isolation.md). The instantiation evidence in
849
        // the extraction graph is IRI-keyed (a gen:hasAdmin nanopub names the bare IRI),
850
        // so we project it per-ref by joining each instantiation naming ?space to the
851
        // admin rows of every ref of ?space whose admin set contains the publisher. The
852
        // inserted subject is minted per (?ri, ?spaceRef) so one instantiation validating
853
        // into N refs yields N distinct rows. TRANSITIONAL-DUAL-EMIT (Phase 4: remove):
854
        // forSpace is still emitted alongside forSpaceRef so the not-yet-migrated
855
        // downstream tiers / pre-ref read queries keep functioning on a mixed-version
856
        // fleet; it is dropped once everything keys on forSpaceRef.
857
        return """
69✔
858
                PREFIX npa:  <%1$s>
859
                PREFIX gen:  <%2$s>
860
                INSERT { GRAPH <%3$s> {
861
                  ?sri a gen:RoleInstantiation ;
862
                       npa:forSpaceRef ?spaceRef ;
863
                       npa:forSpace ?space ;
864
                       npa:inverseProperty gen:hasAdmin ;
865
                       npa:forAgent ?agent ;
866
                       npa:viaNanopub ?np .
867
                } }
868
                WHERE {
869
                  # 1. Anchor: who is already an admin of which space ref?
870
                  {
871
                    # Seed branch: root-admin of a space ref that is still alive
872
                    # (has at least one non-invalidated definition). NOT filtered on
873
                    # ?def's own invalidation — superseding the root nanopub with a
874
                    # continuation revision must keep the seed; only a fully-retracted
875
                    # ref drops it (issue #110).
876
                    GRAPH <%4$s> {
877
                      ?def a npa:SpaceDefinition ;
878
                           npa:forSpaceRef  ?spaceRef ;
879
                           npa:hasRootAdmin ?publisher .
880
                      ?spaceRef npa:spaceIri ?space .
881
                    }
882
                    %7$s
883
                  }
884
                  UNION
885
                  {
886
                    # Closed-over branch: an existing admin of this ref. Recurse on the
887
                    # ref, then resolve its bare IRI to probe the IRI-keyed instantiation.
888
                    GRAPH <%3$s> {
889
                      ?prev a gen:RoleInstantiation ;
890
                            npa:forSpaceRef     ?spaceRef ;
891
                            npa:inverseProperty gen:hasAdmin ;
892
                            npa:forAgent        ?publisher .
893
                    }
894
                    GRAPH <%4$s> {
895
                      ?spaceRef npa:spaceIri ?space .
896
                    }
897
                  }
898
                  # 2. Mirror: resolve ?publisher → ?pkh via the trust-approved row.
899
                  GRAPH <%3$s> {
900
                    ?acct a npa:AccountState ;
901
                          npa:agent  ?publisher ;
902
                          npa:pubkey ?pkh .
903
                  }
904
                  # 3. Targeted instantiation lookup by space + pubkey (IRI-keyed).
905
                  GRAPH <%4$s> {
906
                    ?ri a gen:RoleInstantiation ;
907
                        npa:forSpace        ?space ;
908
                        npa:inverseProperty gen:hasAdmin ;
909
                        npa:forAgent        ?agent ;
910
                        npa:pubkeyHash      ?pkh ;
911
                        npa:viaNanopub      ?np .
912
                  }
913
                  # 3a. Mint the per-ref state subject: (?ri, ?spaceRef) → ?sri.
914
                  BIND(IRI(CONCAT(STR(?ri), "__", ENCODE_FOR_URI(STR(?spaceRef)))) AS ?sri)
915
                  %6$s
916
                  # 4. Load-number filter on bound ?np.
917
                  GRAPH <%8$s> {
918
                    ?np npa:hasLoadNumber ?ln .
919
                    FILTER (?ln > %5$d)
920
                  }
921
                  # 5. Dedup last — keyed on (ref, agent).
922
                  FILTER NOT EXISTS { GRAPH <%3$s> {
923
                    ?existing a gen:RoleInstantiation ;
924
                              npa:forSpaceRef ?spaceRef ;
925
                              npa:forAgent ?agent ;
926
                              npa:inverseProperty gen:hasAdmin .
927
                  } }
928
                }
929
                """.formatted(
3✔
930
                NPA.NAMESPACE,
931
                GEN.NAMESPACE,
932
                graph,
933
                SpacesVocab.SPACES_GRAPH,
934
                lastProcessed,
15✔
935
                invalidationFilter("np"),
12✔
936
                spaceRefAliveFilter(),
18✔
937
                NPA.GRAPH);
938
    }
939

940
    /**
941
     * Seed-survival filter for the admin tier (issue #110). The {@code hasRootAdmin}
942
     * seed is anchored to the root NPID, which is the immutable space-ref identity, so
943
     * it must survive supersession of the root <em>nanopub</em> by a continuation
944
     * revision (a later definition re-roots to the same ref via
945
     * {@code gen:hasRootDefinition} and so carries no {@code hasRootAdmin} of its own).
946
     * The previous {@code invalidationFilter("defNp")} dropped the seed the moment the
947
     * root revision was superseded, leaving the whole admin closure — and everything
948
     * cascading from it — unmaterialized for any space whose definition had ever been
949
     * updated.
950
     *
951
     * <p>Expressed positively: the seed survives iff the space ref still has at least
952
     * one non-invalidated {@link SpacesVocab#SPACE_DEFINITION}. A fully-retracted ref
953
     * (every definition invalidated) has no live definition, so the {@code FILTER
954
     * EXISTS} fails and the seed correctly disappears. Anchored on the already-bound
955
     * {@code ?spaceRef}, so it's a targeted lookup over that ref's (few) definitions.
956
     */
957
    private static String spaceRefAliveFilter() {
958
        return """
33✔
959
                FILTER EXISTS {
960
                  GRAPH <%1$s> {
961
                    ?liveDef a npa:SpaceDefinition ;
962
                             npa:forSpaceRef ?spaceRef ;
963
                             npa:viaNanopub  ?liveNp .
964
                  }
965
                  %2$s
966
                }
967
                """.formatted(SpacesVocab.SPACES_GRAPH, invalidationFilter("liveNp"));
9✔
968
    }
969

970
    /**
971
     * {@code gen:hasRole} attachment validation: an attachment is validated iff its
972
     * publisher is already a validated admin of the target space. Adds
973
     * {@code gen:RoleAssignment} rows to the space-state graph.
974
     */
975
    static String attachmentValidationUpdate(IRI graph, long lastProcessed) {
976
        // Ref-keyed (see doc/design-spaceref-isolation.md). The attachment names a bare
977
        // Space IRI; it is validated per-ref for every ref of that IRI whose admin set
978
        // contains the publisher (direct), or — when the named IRI is an owl:sameAs alias
979
        // — for the canonical ref it maps to (issue #113). ?targetRef is the ref the
980
        // RoleAssignment attaches to; the inserted subject is minted per (?ra, ?targetRef)
981
        // so one attachment validating into N refs yields N distinct rows.
982
        // TRANSITIONAL-DUAL-EMIT (Phase 4: remove): forSpace (the attached IRI, possibly an
983
        // alias) is kept so the non-admin tier can probe the IRI-keyed instantiations
984
        // naming it, and so pre-ref read queries keep functioning on a mixed-version fleet.
985
        return """
69✔
986
                PREFIX npa:  <%1$s>
987
                PREFIX gen:  <%2$s>
988
                INSERT { GRAPH <%3$s> {
989
                  ?ra2 a gen:RoleAssignment ;
990
                       npa:forSpaceRef ?targetRef ;
991
                       npa:forSpace ?space ;
992
                       gen:hasRole  ?role ;
993
                       npa:viaNanopub ?np .
994
                } }
995
                WHERE {
996
                  GRAPH <%4$s> {
997
                    ?ra a gen:RoleAssignment ;
998
                        npa:forSpace ?space ;
999
                        gen:hasRole  ?role ;
1000
                        npa:pubkeyHash ?pkh ;
1001
                        npa:viaNanopub ?np .
1002
                  }
1003
                  GRAPH <%7$s> {
1004
                    ?np npa:hasLoadNumber ?ln .
1005
                    FILTER (?ln > %5$d)
1006
                  }
1007
                  GRAPH <%3$s> {
1008
                    ?acct a npa:AccountState ;
1009
                          npa:agent  ?publisher ;
1010
                          npa:pubkey ?pkh .
1011
                  }
1012
                  # Per-ref admin gate. ?targetRef = a ref of ?space the publisher admins
1013
                  # (direct), or the canonical ref ?space is an owl:sameAs alias of.
1014
                  {
1015
                    GRAPH <%4$s> { ?targetRef npa:spaceIri ?space . }
1016
                    GRAPH <%3$s> {
1017
                      ?adminRI a gen:RoleInstantiation ;
1018
                               npa:forSpaceRef ?targetRef ;
1019
                               npa:inverseProperty gen:hasAdmin ;
1020
                               npa:forAgent ?publisher .
1021
                    }
1022
                  }
1023
                  UNION
1024
                  {
1025
                    GRAPH <%3$s> {
1026
                      ?space npa:sameAsSpace ?targetRef .
1027
                      ?adminRI a gen:RoleInstantiation ;
1028
                               npa:forSpaceRef ?targetRef ;
1029
                               npa:inverseProperty gen:hasAdmin ;
1030
                               npa:forAgent ?publisher .
1031
                    }
1032
                  }
1033
                  BIND(IRI(CONCAT(STR(?ra), "__", ENCODE_FOR_URI(STR(?targetRef)))) AS ?ra2)
1034
                  %6$s
1035
                  FILTER NOT EXISTS { GRAPH <%3$s> {
1036
                    ?existing a gen:RoleAssignment ;
1037
                              npa:forSpaceRef ?targetRef ;
1038
                              gen:hasRole  ?role .
1039
                  } }
1040
                }
1041
                """.formatted(
3✔
1042
                NPA.NAMESPACE,
1043
                GEN.NAMESPACE,
1044
                graph,
1045
                SpacesVocab.SPACES_GRAPH,
1046
                lastProcessed,
15✔
1047
                invalidationFilter("np"),
18✔
1048
                NPA.GRAPH);
1049
    }
1050

1051
    /**
1052
     * Preset-bundled role materialization (Nanodash issue #302). For each active,
1053
     * admin-authored {@code gen:PresetAssignment} targeting a {@code gen:Space}, inserts
1054
     * one {@code gen:RoleAssignment} per role the preset bundles — exactly as if
1055
     * {@code <space> gen:hasRole <role>} had been published by the assignment's publisher.
1056
     * The materialized rows carry {@code npa:derivedFromPreset} (the assignment nanopub)
1057
     * so the deactivation delete and read-side marking can scope to them without touching
1058
     * directly-published attachments. See {@code doc/design-preset-role-materialization.md}.
1059
     *
1060
     * <p>Activation is resolved by an <b>authorization-scoped latest-wins</b> over the
1061
     * {@code (preset, resource)} pair, NOT {@code npx:invalidates} (§3): the candidate set
1062
     * for the {@code MAX(dct:created)} comparison is restricted to assignments whose
1063
     * publisher is also a validated admin of the target ref, so an unauthorized key's newer
1064
     * assignment cannot shadow an admin's activation (the #113-class anti-hijack rule).
1065
     */
1066
    static String presetAttachmentValidationUpdate(IRI graph, long lastProcessed) {
1067
        // Ref-keyed like attachmentValidationUpdate: the assignment names a bare resource
1068
        // IRI; it is validated per-ref for every Space ref of that IRI whose admin set
1069
        // contains the publisher. The inserted subject is minted per (assignment, ref, role)
1070
        // — one assignment fans out to N roles and N refs. Non-Space targets resolve no
1071
        // ?targetRef and so insert nothing (correct no-op; maintained-resource / individual
1072
        // targets are future work, see design doc §2). TRANSITIONAL-DUAL-EMIT (Phase 4:
1073
        // remove): forSpace kept alongside forSpaceRef so the non-admin tiers can probe the
1074
        // IRI-keyed instantiations and pre-ref read queries keep functioning.
1075
        return """
69✔
1076
                PREFIX npa:  <%1$s>
1077
                PREFIX gen:  <%2$s>
1078
                INSERT { GRAPH <%3$s> {
1079
                  ?ra2 a gen:RoleAssignment ;
1080
                       npa:forSpaceRef ?targetRef ;
1081
                       npa:forSpace    ?resource ;
1082
                       gen:hasRole     ?role ;
1083
                       npa:viaNanopub  ?assignNp ;
1084
                       npa:derivedFromPreset ?assignNp .
1085
                } }
1086
                WHERE {
1087
                  # 1. Anchor: active preset assignments in the extraction graph.
1088
                  GRAPH <%4$s> {
1089
                    ?pa a npa:PresetAssignment ;
1090
                        npa:ofPreset    ?preset ;
1091
                        npa:forResource ?resource ;
1092
                        npa:isActivated true ;
1093
                        npa:pubkeyHash  ?pkh ;
1094
                        npa:viaNanopub  ?assignNp ;
1095
                        <http://purl.org/dc/terms/created> ?created .
1096
                  }
1097
                  # 2. Load-number filter on the assignment nanopub.
1098
                  GRAPH <%7$s> {
1099
                    ?assignNp npa:hasLoadNumber ?ln .
1100
                    FILTER (?ln > %5$d)
1101
                  }
1102
                  # 3. Resolve publisher pkh -> agent via the mirrored trust-approved row.
1103
                  GRAPH <%3$s> {
1104
                    ?acct a npa:AccountState ;
1105
                          npa:agent  ?publisher ;
1106
                          npa:pubkey ?pkh .
1107
                  }
1108
                  # 4. Target must be a Space ref the publisher admins — direct, or the
1109
                  #    canonical ref ?resource is an owl:sameAs alias of (issue #113 parity
1110
                  #    with attachmentValidationUpdate, so a preset assigned against an alias
1111
                  #    IRI still materializes against the canonical ref).
1112
                  {
1113
                    GRAPH <%4$s> { ?targetRef npa:spaceIri ?resource . }
1114
                    GRAPH <%3$s> {
1115
                      ?adminRI a gen:RoleInstantiation ;
1116
                               npa:forSpaceRef ?targetRef ;
1117
                               npa:inverseProperty gen:hasAdmin ;
1118
                               npa:forAgent ?publisher .
1119
                    }
1120
                  }
1121
                  UNION
1122
                  {
1123
                    GRAPH <%3$s> {
1124
                      ?resource npa:sameAsSpace ?targetRef .
1125
                      ?adminRI a gen:RoleInstantiation ;
1126
                               npa:forSpaceRef ?targetRef ;
1127
                               npa:inverseProperty gen:hasAdmin ;
1128
                               npa:forAgent ?publisher .
1129
                    }
1130
                  }
1131
                  # 5. Resolve the assignment's referenced preset IRI (node or kind) to its
1132
                  #    canonical kind, mirroring how Nanodash views key on dct:isVersionOf
1133
                  #    (ViewDisplay.getViewKindIri). Every declaration carries npa:ofPreset for
1134
                  #    both its node IRI and kind, so either reference maps to the same ?kind.
1135
                  GRAPH <%4$s> {
1136
                    ?pdMap a npa:PresetDeclaration ;
1137
                           npa:ofPreset   ?preset ;
1138
                           npa:presetKind ?kind .
1139
                  }
1140
                  # 5a. Roles come from the LATEST live declaration of that kind, restricted to
1141
                  #     Space-targeted presets — so a superseded preset version's roles never leak
1142
                  #     (the per-view-kind latest-wins, ported to materialization).
1143
                  GRAPH <%4$s> {
1144
                    ?pd a npa:PresetDeclaration ;
1145
                        npa:presetKind           ?kind ;
1146
                        npa:presetRole           ?role ;
1147
                        npa:appliesToInstancesOf gen:Space ;
1148
                        npa:viaNanopub           ?pdNp ;
1149
                        <http://purl.org/dc/terms/created> ?pdCreated .
1150
                  }
1151
                  # 5b. Latest-declaration-per-kind: reject if a newer LIVE declaration of the
1152
                  #     same kind exists (tiebreak on subject IRI for equal timestamps).
1153
                  FILTER NOT EXISTS {
1154
                    GRAPH <%4$s> {
1155
                      ?pdNewer a npa:PresetDeclaration ;
1156
                               npa:presetKind ?kind ;
1157
                               npa:viaNanopub ?pdNpNewer ;
1158
                               <http://purl.org/dc/terms/created> ?pdCreatedNewer .
1159
                      FILTER (?pdCreatedNewer > ?pdCreated
1160
                              || (?pdCreatedNewer = ?pdCreated && STR(?pdNewer) > STR(?pd)))
1161
                    }
1162
                    %8$s
1163
                  }
1164
                  # 5c. The chosen declaration must itself be live (not superseded/retracted).
1165
                  %9$s
1166
                  # 6. Mint the per (assignment, ref, role) subject.
1167
                  BIND(IRI(CONCAT(STR(?pa), "__", ENCODE_FOR_URI(STR(?targetRef)),
1168
                                  "__", ENCODE_FOR_URI(STR(?role)))) AS ?ra2)
1169
                  # 7. Authorization-scoped latest-wins (anti-hijack, design doc §3): reject
1170
                  #    if a newer same-(preset,resource) assignment exists whose publisher is
1171
                  #    ALSO a validated admin of ?targetRef. Filtering the shadowing candidate
1172
                  #    to admin-authored rows BEFORE taking the latest is what stops an
1173
                  #    unauthorized key from suppressing an admin's activation. Placed after
1174
                  #    the main vars are bound so the planner defers it (RDF4J quirk).
1175
                  FILTER NOT EXISTS {
1176
                    GRAPH <%4$s> {
1177
                      ?paNewer a npa:PresetAssignment ;
1178
                               npa:ofPreset    ?preset ;
1179
                               npa:forResource ?resource ;
1180
                               npa:pubkeyHash  ?pkhNewer ;
1181
                               <http://purl.org/dc/terms/created> ?createdNewer .
1182
                      FILTER (?createdNewer > ?created
1183
                              || (?createdNewer = ?created && STR(?paNewer) > STR(?pa)))
1184
                    }
1185
                    GRAPH <%3$s> {
1186
                      ?acctNewer a npa:AccountState ;
1187
                                 npa:agent  ?publisherNewer ;
1188
                                 npa:pubkey ?pkhNewer .
1189
                      ?adminRINewer a gen:RoleInstantiation ;
1190
                                    npa:forSpaceRef ?targetRef ;
1191
                                    npa:inverseProperty gen:hasAdmin ;
1192
                                    npa:forAgent ?publisherNewer .
1193
                    }
1194
                  }
1195
                  # 8. Defensive: drop if the assignment nanopub itself was hard-retracted.
1196
                  %6$s
1197
                  # 9. Dedup last — keyed on (ref, role).
1198
                  FILTER NOT EXISTS { GRAPH <%3$s> {
1199
                    ?existing a gen:RoleAssignment ;
1200
                              npa:forSpaceRef ?targetRef ;
1201
                              gen:hasRole ?role .
1202
                  } }
1203
                }
1204
                """.formatted(
3✔
1205
                NPA.NAMESPACE,
1206
                GEN.NAMESPACE,
1207
                graph,
1208
                SpacesVocab.SPACES_GRAPH,
1209
                lastProcessed,
15✔
1210
                invalidationFilter("assignNp"),
27✔
1211
                NPA.GRAPH,
1212
                invalidationFilter("pdNpNewer"),
15✔
1213
                invalidationFilter("pdNp"));
6✔
1214
    }
1215

1216
    /**
1217
     * Stamps a ref-scoped, admin-validated mirror of each {@code npa:PresetAssignment}
1218
     * into the state graph (issue #122). The publisher-agnostic extraction row
1219
     * ({@link SpacesExtractor#extractPresetAssignment}) is keyed only by
1220
     * {@code npa:forResource}, so a consumer listing a space's preset assignments by IRI
1221
     * sees the union across <em>all</em> refs claiming that IRI. This stamp adds
1222
     * {@code npa:forSpaceRef ?targetRef} so the "Assigned presets" listing is no longer
1223
     * merged across refs of the same IRI — the one remaining About-tab listing that still
1224
     * merged across refs (every other ref-scoped listing already has a {@code forSpaceRef}
1225
     * companion).
1226
     *
1227
     * <p>Faithful per-assignment mirror — deliberately <em>not</em> role-gated and
1228
     * <em>not</em> latest-wins-resolved, unlike {@link #presetAttachmentValidationUpdate}:
1229
     * <ul>
1230
     *   <li>No {@code npa:PresetDeclaration}/role join, so a preset that bundles only
1231
     *       <em>views</em> (no roles) is still listed.</li>
1232
     *   <li>Emits active <em>and</em> deactivated rows (carries {@code npa:isActivated})
1233
     *       so the listing can show state; a deactivation is just a newer admin-authored
1234
     *       row, so no {@code dct:created}-driven removal is needed here (contrast §4.4).</li>
1235
     *   <li>Latest-wins is deferred to the consumer query, which ranges only over these
1236
     *       admin-authored rows — so it is authorization-scoped for free (design §3): a
1237
     *       non-admin of the ref can never get a row stamped, so it cannot enter the
1238
     *       latest-wins race.</li>
1239
     * </ul>
1240
     *
1241
     * <p>Display-only leaf: nothing downstream derives from these rows (contrast the
1242
     * preset-derived {@code gen:RoleAssignment}), so the caller must <em>not</em> feed this
1243
     * tier's count into {@code structuralAdds}. The {@code npa:forSpaceRef} predicate also
1244
     * distinguishes a stamped row from the IRI-keyed extraction row (which never carries it),
1245
     * so {@link #presetAssignmentRefInvalidationDelete} can target exactly these rows.
1246
     * Reuses steps 1–4 of {@link #presetAttachmentValidationUpdate}; see
1247
     * doc/design-preset-role-materialization.md §3 and issue #122.
1248
     */
1249
    static String presetAssignmentRefStampUpdate(IRI graph, long lastProcessed) {
1250
        return """
69✔
1251
                PREFIX npa:  <%1$s>
1252
                PREFIX gen:  <%2$s>
1253
                INSERT { GRAPH <%3$s> {
1254
                  ?paRef a npa:PresetAssignment ;
1255
                         npa:ofPreset    ?preset ;
1256
                         npa:forResource ?resource ;
1257
                         npa:forSpaceRef ?targetRef ;
1258
                         npa:isActivated ?activated ;
1259
                         npa:viaNanopub  ?assignNp ;
1260
                         <http://purl.org/dc/terms/created> ?created .
1261
                } }
1262
                WHERE {
1263
                  # 1. Anchor: every assignment row (active or not) in the extraction graph.
1264
                  GRAPH <%4$s> {
1265
                    ?pa a npa:PresetAssignment ;
1266
                        npa:ofPreset    ?preset ;
1267
                        npa:forResource ?resource ;
1268
                        npa:isActivated ?activated ;
1269
                        npa:pubkeyHash  ?pkh ;
1270
                        npa:viaNanopub  ?assignNp ;
1271
                        <http://purl.org/dc/terms/created> ?created .
1272
                  }
1273
                  # 2. Load-number filter on the assignment nanopub (delta window).
1274
                  GRAPH <%6$s> {
1275
                    ?assignNp npa:hasLoadNumber ?ln .
1276
                    FILTER (?ln > %5$d)
1277
                  }
1278
                  # 3. Resolve publisher pkh -> agent via the mirrored trust-approved row.
1279
                  GRAPH <%3$s> {
1280
                    ?acct a npa:AccountState ;
1281
                          npa:agent  ?publisher ;
1282
                          npa:pubkey ?pkh .
1283
                  }
1284
                  # 4. Target must be a Space ref the publisher admins. ?targetRef = that ref;
1285
                  #    fan-out to N refs the publisher admins (per-ref isolation, consistent
1286
                  #    with the role materializer and design-spaceref-isolation.md). Direct,
1287
                  #    or the canonical ref ?resource is an owl:sameAs alias of (issue #113),
1288
                  #    so an assignment naming an alias is still listed under the canonical ref.
1289
                  {
1290
                    GRAPH <%4$s> { ?targetRef npa:spaceIri ?resource . }
1291
                    GRAPH <%3$s> {
1292
                      ?adminRI a gen:RoleInstantiation ;
1293
                               npa:forSpaceRef ?targetRef ;
1294
                               npa:inverseProperty gen:hasAdmin ;
1295
                               npa:forAgent ?publisher .
1296
                    }
1297
                  }
1298
                  UNION
1299
                  {
1300
                    GRAPH <%3$s> {
1301
                      ?resource npa:sameAsSpace ?targetRef .
1302
                      ?adminRI a gen:RoleInstantiation ;
1303
                               npa:forSpaceRef ?targetRef ;
1304
                               npa:inverseProperty gen:hasAdmin ;
1305
                               npa:forAgent ?publisher .
1306
                    }
1307
                  }
1308
                  # 5. Defensive: drop if the assignment nanopub itself was hard-retracted.
1309
                  %7$s
1310
                  # 6. Mint per (assignment, ref); dedup on the bound subject. No latest-wins
1311
                  #    here — a deactivation is just a newer admin-authored row, and the
1312
                  #    consumer resolves latest dct:created per (preset,resource) over these
1313
                  #    admin-authored rows (so the resolution is authorization-scoped).
1314
                  BIND(IRI(CONCAT(STR(?pa), "__", ENCODE_FOR_URI(STR(?targetRef)))) AS ?paRef)
1315
                  FILTER NOT EXISTS { GRAPH <%3$s> { ?paRef a npa:PresetAssignment . } }
1316
                }
1317
                """.formatted(
3✔
1318
                NPA.NAMESPACE,
1319
                GEN.NAMESPACE,
1320
                graph,
1321
                SpacesVocab.SPACES_GRAPH,
1322
                lastProcessed,
27✔
1323
                NPA.GRAPH,
1324
                invalidationFilter("assignNp"));
6✔
1325
    }
1326

1327
    /**
1328
     * Non-admin tier publisher constraints (inserted as a SPARQL sub-pattern).
1329
     * Each constraint owns the AccountState (pkh → agent) lookup so the join
1330
     * variable is bound through a targeted pattern. The observer-self variant
1331
     * binds {@code npa:agent ?agent} directly — no separate {@code ?publisher}
1332
     * variable, no post-join equality filter — which lets the planner anchor
1333
     * the AccountState lookup on the already-bound {@code ?agent} instead of
1334
     * enumerating all approved publishers and filtering at the end.
1335
     */
1336
    static final String PUBLISHER_IS_ADMIN = """
1337
            ?acct a npa:AccountState ;
1338
                  npa:pubkey ?pkh ;
1339
                  npa:agent  ?publisher .
1340
            # Admin of the assignment's ref. The ref already resolves alias →
1341
            # canonical (the attachment tier bound ?spaceRef through the owl:sameAs
1342
            # alias edge for aliased IRIs, issue #113), so no alias arm is needed here.
1343
            ?adminRI a gen:RoleInstantiation ;
1344
                     npa:forSpaceRef ?spaceRef ;
1345
                     npa:inverseProperty gen:hasAdmin ;
1346
                     npa:forAgent ?publisher .
1347
            """;
1348

1349
    /** Observer self-evidence: the assignee's own pubkey signed the instantiation. */
1350
    static final String PUBLISHER_IS_SELF = """
1351
            ?acct a npa:AccountState ;
1352
                  npa:pubkey ?pkh ;
1353
                  npa:agent  ?agent .
1354
            """;
1355

1356
    /**
1357
     * Maintainer / Member / Observer tier INSERT. Same shape: find an instantiation
1358
     * whose predicate matches a RoleDeclaration of the given tier attached to the
1359
     * target space, and whose publisher passes the tier-specific constraint.
1360
     */
1361
    static String nonAdminTierUpdate(IRI graph, long lastProcessed,
1362
                                     IRI tierClass, String publisherConstraint) {
1363
        // Order tuned for RDF4J's evaluator (which executes BGPs roughly in order).
1364
        // The crucial choice is the *anchor*: instantiation-first plans send the
1365
        // planner exploring the full ~thousands of candidate RIs and only filter
1366
        // by tier at the very end. Attachment-first anchors on the small set of
1367
        // gen:RoleAssignment rows already validated in this space-state graph
1368
        // (~hundreds, often zero) and walks outward by bound (?role, ?space).
1369
        //
1370
        //   1. Anchor on RoleAssignments in this space-state graph (small).
1371
        //   1a. Resolve the IRIs that denote the assignment's ref — its canonical
1372
        //      IRI plus any validated owl:sameAs aliases — so an instantiation that
1373
        //      names an alias of the space still matches (issue #113). Bound here so
1374
        //      the instantiation lookup below stays anchored by ?instSpace.
1375
        //   2. Match the tier-pinned RoleDeclaration by ?role.
1376
        //   3. Pair role-decl direction to instantiation direction in one UNION
1377
        //      so only (reg, reg)/(inv, inv) combos are explored.
1378
        //   4. Targeted instantiation lookup — (?instSpace, ?pred) are bound.
1379
        //   5. Publisher constraint (incl. AccountState resolution).
1380
        //   6. Load-number filter on bound ?np.
1381
        //   7. Dedup at the end.
1382
        return """
69✔
1383
                PREFIX npa:  <%1$s>
1384
                PREFIX gen:  <%2$s>
1385
                INSERT { GRAPH <%3$s> {
1386
                  ?ri2 a gen:RoleInstantiation ;
1387
                       npa:forSpaceRef ?spaceRef ;
1388
                       # TRANSITIONAL-DUAL-EMIT (Phase 4: remove): forSpace alongside
1389
                       # forSpaceRef so pre-ref read queries (e.g. get-space-members) keep
1390
                       # functioning on a mixed-version fleet; downstream tiers key on the ref.
1391
                       npa:forSpace ?space ;
1392
                       npa:forAgent ?agent ;
1393
                       ?dirPred ?pred ;
1394
                       npa:viaNanopub ?np .
1395
                } }
1396
                WHERE {
1397
                  # 1. Anchor: validated attachments in this space-state graph (ref-keyed).
1398
                  GRAPH <%3$s> {
1399
                    ?ra a gen:RoleAssignment ;
1400
                        gen:hasRole     ?role ;
1401
                        npa:forSpaceRef ?spaceRef ;
1402
                        npa:forSpace    ?space .
1403
                  }
1404
                  # 1a. The IRIs that denote this ref: its canonical IRI, plus any validated
1405
                  #     owl:sameAs aliases of it (issue #113) — so an instantiation naming an
1406
                  #     alias of the space still materializes here. Bound BEFORE the
1407
                  #     instantiation BGP so that lookup stays anchored by ?instSpace (planner
1408
                  #     note above); ?spaceRef is already bound, so each arm is a targeted
1409
                  #     lookup yielding a tiny IRI set. The alias arm only follows admin-
1410
                  #     validated npa:sameAsSpace edges, so it grants no authority the admin
1411
                  #     tier would not (anti-hijack is enforced upstream, not relaxed here).
1412
                  {
1413
                    GRAPH <%4$s> { ?spaceRef npa:spaceIri ?instSpace . }
1414
                  }
1415
                  UNION
1416
                  {
1417
                    GRAPH <%3$s> { ?instSpace npa:sameAsSpace ?spaceRef . }
1418
                  }
1419
                  # 2. Tier-pinned RoleDeclaration (?role bound from the attachment). Its
1420
                  #    nanopub's invalidation is intentionally NOT consulted (see step 7), so
1421
                  #    no ?rdNp binding is needed.
1422
                  GRAPH <%4$s> {
1423
                    ?rd a npa:RoleDeclaration ;
1424
                        npa:hasRoleType <%7$s> ;
1425
                        npa:role        ?role .
1426
                    # 3. Pair direction so only matching combos are explored. ?dirPred
1427
                    #    carries the matched direction so the materialized row records the
1428
                    #    role property (read by get-space-members and publisherIsTieredRole).
1429
                    {
1430
                      ?rd gen:hasRegularProperty ?pred .
1431
                      ?ri npa:regularProperty    ?pred .
1432
                      BIND(npa:regularProperty AS ?dirPred)
1433
                    }
1434
                    UNION
1435
                    {
1436
                      ?rd gen:hasInverseProperty ?pred .
1437
                      ?ri npa:inverseProperty    ?pred .
1438
                      BIND(npa:inverseProperty AS ?dirPred)
1439
                    }
1440
                    # 4. Targeted instantiation lookup — (?instSpace, ?pred) bound. The
1441
                    #    instantiation names its space by IRI; ?instSpace was resolved to this
1442
                    #    ref above (canonical or owl:sameAs alias), so an alias-named
1443
                    #    instantiation joins the same ?spaceRef as a canonical one. The
1444
                    #    materialized row still carries npa:forSpace ?space (the attachment's
1445
                    #    IRI) for the transitional dual-emit, so pre-ref reads see the member
1446
                    #    under the space's primary IRI.
1447
                    ?ri a gen:RoleInstantiation ;
1448
                        npa:forSpace   ?instSpace ;
1449
                        npa:forAgent   ?agent ;
1450
                        npa:pubkeyHash ?pkh ;
1451
                        npa:viaNanopub ?np .
1452
                  }
1453
                  # 5. Publisher constraint (incl. AccountState resolution).
1454
                  GRAPH <%3$s> {
1455
                    %8$s
1456
                  }
1457
                  # 5a. Mint the per-ref state subject: (?ri, ?spaceRef) → ?ri2.
1458
                  BIND(IRI(CONCAT(STR(?ri), "__", ENCODE_FOR_URI(STR(?spaceRef)))) AS ?ri2)
1459
                  # 6. Load-number filter on bound ?np.
1460
                  GRAPH <%9$s> {
1461
                    ?np npa:hasLoadNumber ?ln .
1462
                    FILTER (?ln > %5$d)
1463
                  }
1464
                  # 7. Instantiation invalidation filter — outside the GRAPH block so the
1465
                  #    planner defers it until ?np is bound. Role-DECLARATION invalidation is
1466
                  #    deliberately NOT consulted: the tier already anchors on the admin-
1467
                  #    validated attachment (?ra), which is removed when an admin retracts it,
1468
                  #    so admin control is fully enforced there. Letting the declaration's
1469
                  #    author (usually not the space admin) supersede/retract their declaration
1470
                  #    strip a space's members is the same cross-author-strip anti-pattern as
1471
                  #    issue #112. Role IRIs are version-pinned, so the attached definition is
1472
                  #    immutable regardless of the declaration nanopub's later lifecycle.
1473
                  %6$s
1474
                  # 8. Dedup last — keyed on (ref, agent, nanopub).
1475
                  FILTER NOT EXISTS { GRAPH <%3$s> {
1476
                    ?existing a gen:RoleInstantiation ;
1477
                              npa:forSpaceRef ?spaceRef ;
1478
                              npa:forAgent ?agent ;
1479
                              npa:viaNanopub ?np .
1480
                  } }
1481
                }
1482
                """.formatted(
3✔
1483
                NPA.NAMESPACE,
1484
                GEN.NAMESPACE,
1485
                graph,
1486
                SpacesVocab.SPACES_GRAPH,
1487
                lastProcessed,
15✔
1488
                invalidationFilter("np"),
42✔
1489
                tierClass,
1490
                publisherConstraint,
1491
                NPA.GRAPH);
1492
    }
1493

1494
    /**
1495
     * Sub-space admit pass. Copies validated {@code npa:SubSpaceDeclaration}
1496
     * extraction rows into the space-state graph (preserving the {@code npasub:}
1497
     * subject) and emits convenience {@code <child> npa:isSubSpaceOf <parent>} and
1498
     * {@code <parent> npa:hasSubSpace <child>} direct triples. Two satisfaction
1499
     * modes joined by UNION:
1500
     * <ul>
1501
     *   <li>Mode A — the declaration's publisher is a validated admin of both the
1502
     *       child and the parent space.</li>
1503
     *   <li>Mode B — a different non-invalidated declaration for the same
1504
     *       {@code (child, parent)} pair exists, and the two publishers between
1505
     *       them cover both admin sides (i.e. one of them is admin of the child,
1506
     *       one of them is admin of the parent — possibly the same one twice if
1507
     *       both happen to be admin of both).</li>
1508
     * </ul>
1509
     *
1510
     * <p>Mode-B late-arrival: when only the partner declaration is new in this
1511
     * cycle (the primary is older than {@code lastProcessed}), the load-number
1512
     * filter on {@code ?np} excludes the candidate. The late-arrival sweep
1513
     * ({@link #runDownstreamWithoutLoadFilter}) re-runs this pass without the
1514
     * load filter and catches it.
1515
     */
1516
    static String subSpaceAdmitUpdate(IRI graph, long lastProcessed) {
1517
        return """
69✔
1518
                PREFIX npa: <%1$s>
1519
                PREFIX gen: <%2$s>
1520
                INSERT { GRAPH <%3$s> {
1521
                  ?d a npa:SubSpaceDeclaration ;
1522
                     npa:childSpace  ?child ;
1523
                     npa:parentSpace ?parent ;
1524
                     npa:viaNanopub  ?np .
1525
                  ?childRef  npa:isSubSpaceOf ?parentRef .
1526
                  ?parentRef npa:hasSubSpace  ?childRef  .
1527
                  # TRANSITIONAL-DUAL-EMIT (Phase 1.5; remove in Phase 4): IRI-valued
1528
                  # sub-space edge alongside the ref-to-ref one, so pre-ref published
1529
                  # queries that key on the bare Space IRI keep binding on a mixed-version
1530
                  # fleet. See doc/report-2026-06-12-mixed-fleet-spaceref-breakage.md.
1531
                  ?child  npa:isSubSpaceOf ?parent .
1532
                  ?parent npa:hasSubSpace  ?child  .
1533
                } }
1534
                WHERE {
1535
                  # 1. Anchor: candidate declarations from the extraction graph.
1536
                  GRAPH <%4$s> {
1537
                    ?d a npa:SubSpaceDeclaration ;
1538
                       npa:childSpace  ?child ;
1539
                       npa:parentSpace ?parent ;
1540
                       npa:pubkeyHash  ?pkh ;
1541
                       npa:viaNanopub  ?np .
1542
                  }
1543
                  # 2. Mirror: resolve ?pkh → ?publisher via the trust-approved row.
1544
                  GRAPH <%3$s> {
1545
                    ?acct a npa:AccountState ;
1546
                          npa:pubkey ?pkh ;
1547
                          npa:agent  ?publisher .
1548
                  }
1549
                  # 3. Authority gate, ref-keyed. The edge is emitted ref-to-ref between
1550
                  #    the child ref and parent ref the authorizing admin governs; the
1551
                  #    admin rows' dual-emitted npa:forSpace binds the refs to the child /
1552
                  #    parent IRIs (cross-product when an IRI has several governed refs).
1553
                  {
1554
                    # Mode A — publisher is admin of BOTH a child ref and a parent ref.
1555
                    GRAPH <%3$s> {
1556
                      ?riC a gen:RoleInstantiation ;
1557
                           npa:inverseProperty gen:hasAdmin ;
1558
                           npa:forSpace ?child ;
1559
                           npa:forSpaceRef ?childRef ;
1560
                           npa:forAgent ?publisher .
1561
                      ?riP a gen:RoleInstantiation ;
1562
                           npa:inverseProperty gen:hasAdmin ;
1563
                           npa:forSpace ?parent ;
1564
                           npa:forSpaceRef ?parentRef ;
1565
                           npa:forAgent ?publisher .
1566
                    }
1567
                  }
1568
                  UNION
1569
                  {
1570
                    # Mode B — co-declaration whose publisher covers the side this
1571
                    # one's publisher doesn't. Between {publisher, publisher2},
1572
                    # both admin sides must be covered.
1573
                    GRAPH <%4$s> {
1574
                      ?d2 a npa:SubSpaceDeclaration ;
1575
                          npa:childSpace  ?child ;
1576
                          npa:parentSpace ?parent ;
1577
                          npa:pubkeyHash  ?pkh2 ;
1578
                          npa:viaNanopub  ?np2 .
1579
                      FILTER (?np2 != ?np)
1580
                    }
1581
                    %8$s
1582
                    GRAPH <%3$s> {
1583
                      ?acct2 a npa:AccountState ;
1584
                             npa:pubkey ?pkh2 ;
1585
                             npa:agent  ?publisher2 .
1586
                      ?riA a gen:RoleInstantiation ;
1587
                           npa:inverseProperty gen:hasAdmin ;
1588
                           npa:forSpace ?child ;
1589
                           npa:forSpaceRef ?childRef .
1590
                      { ?riA npa:forAgent ?publisher } UNION { ?riA npa:forAgent ?publisher2 }
1591
                      ?riB a gen:RoleInstantiation ;
1592
                           npa:inverseProperty gen:hasAdmin ;
1593
                           npa:forSpace ?parent ;
1594
                           npa:forSpaceRef ?parentRef .
1595
                      { ?riB npa:forAgent ?publisher } UNION { ?riB npa:forAgent ?publisher2 }
1596
                    }
1597
                  }
1598
                  # 4. Invalidation filter on the primary declaration's nanopub.
1599
                  %6$s
1600
                  # 5. Load-number filter on bound ?np.
1601
                  GRAPH <%7$s> {
1602
                    ?np npa:hasLoadNumber ?ln .
1603
                    FILTER (?ln > %5$d)
1604
                  }
1605
                  # 6. Dedup last — on the emitted ref-to-ref edge.
1606
                  FILTER NOT EXISTS { GRAPH <%3$s> {
1607
                    ?childRef npa:isSubSpaceOf ?parentRef .
1608
                  } }
1609
                }
1610
                """.formatted(
3✔
1611
                NPA.NAMESPACE,
1612
                GEN.NAMESPACE,
1613
                graph,
1614
                SpacesVocab.SPACES_GRAPH,
1615
                lastProcessed,
15✔
1616
                invalidationFilter("np"),
27✔
1617
                NPA.GRAPH,
1618
                invalidationFilter("np2"));
6✔
1619
    }
1620

1621
    /**
1622
     * Maintained-resource admit pass. Copies validated
1623
     * {@code npa:MaintainedResourceDeclaration} extraction rows into the space-state
1624
     * graph (preserving the {@code npamrd:} subject) and emits convenience
1625
     * {@code <r> npa:isMaintainedBy <s>} and {@code <s> npa:hasMaintainedResource <r>}
1626
     * direct triples. Single satisfaction mode:
1627
     * <ul>
1628
     *   <li>Mode A — the declaration's publisher is a validated admin of the
1629
     *       maintaining space.</li>
1630
     * </ul>
1631
     *
1632
     * <p>No Mode B because only one space is involved; the two-sides-must-be-covered
1633
     * concern that drives sub-space Mode B doesn't apply. Late-arrival is still
1634
     * possible (declaration lands before the publisher's admin grant becomes valid):
1635
     * the load-number filter on {@code ?np} excludes the candidate, and the
1636
     * late-arrival sweep ({@link #runDownstreamWithoutLoadFilter}) re-runs this pass
1637
     * without the load filter and catches it.
1638
     */
1639
    static String maintainedResourceAdmitUpdate(IRI graph, long lastProcessed) {
1640
        return """
69✔
1641
                PREFIX npa: <%1$s>
1642
                PREFIX gen: <%2$s>
1643
                INSERT { GRAPH <%3$s> {
1644
                  ?d a npa:MaintainedResourceDeclaration ;
1645
                     npa:resourceIri     ?r ;
1646
                     npa:maintainerSpace ?s ;
1647
                     npa:viaNanopub      ?np .
1648
                  ?r npa:isMaintainedBy        ?sRef .
1649
                  ?sRef npa:hasMaintainedResource ?r .
1650
                  # TRANSITIONAL-DUAL-EMIT (Phase 1.5; remove in Phase 4): IRI-valued
1651
                  # maintained-resource edge alongside the resource→ref one, so pre-ref
1652
                  # published queries (e.g. get-view-displays' maintained hop) keep binding
1653
                  # on a mixed-version fleet. This is the edge whose absence broke 1.15.0 —
1654
                  # see doc/report-2026-06-12-mixed-fleet-spaceref-breakage.md.
1655
                  ?r npa:isMaintainedBy        ?s .
1656
                  ?s npa:hasMaintainedResource ?r .
1657
                } }
1658
                WHERE {
1659
                  # 1. Anchor: candidate declarations from the extraction graph.
1660
                  GRAPH <%4$s> {
1661
                    ?d a npa:MaintainedResourceDeclaration ;
1662
                       npa:resourceIri     ?r ;
1663
                       npa:maintainerSpace ?s ;
1664
                       npa:pubkeyHash      ?pkh ;
1665
                       npa:viaNanopub      ?np .
1666
                  }
1667
                  # 2. Mirror: resolve ?pkh → ?publisher via the trust-approved row.
1668
                  GRAPH <%3$s> {
1669
                    ?acct a npa:AccountState ;
1670
                          npa:pubkey ?pkh ;
1671
                          npa:agent  ?publisher .
1672
                    # 3. Authority gate (Mode A only): publisher is admin of a ref of the
1673
                    #    maintaining space. ?sRef = that ref (resource → ref edge).
1674
                    ?riA a gen:RoleInstantiation ;
1675
                         npa:inverseProperty gen:hasAdmin ;
1676
                         npa:forSpace ?s ;
1677
                         npa:forSpaceRef ?sRef ;
1678
                         npa:forAgent ?publisher .
1679
                  }
1680
                  # 4. Invalidation filter on the declaration's nanopub.
1681
                  %6$s
1682
                  # 5. Load-number filter on bound ?np.
1683
                  GRAPH <%7$s> {
1684
                    ?np npa:hasLoadNumber ?ln .
1685
                    FILTER (?ln > %5$d)
1686
                  }
1687
                  # 6. Dedup last — on the emitted resource → ref edge.
1688
                  FILTER NOT EXISTS { GRAPH <%3$s> {
1689
                    ?r npa:isMaintainedBy ?sRef .
1690
                  } }
1691
                }
1692
                """.formatted(
3✔
1693
                NPA.NAMESPACE,
1694
                GEN.NAMESPACE,
1695
                graph,
1696
                SpacesVocab.SPACES_GRAPH,
1697
                lastProcessed,
15✔
1698
                invalidationFilter("np"),
18✔
1699
                NPA.GRAPH);
1700
    }
1701

1702
    /**
1703
     * Space-alias admit pass (issue #113). Copies validated
1704
     * {@code npa:SpaceAliasDeclaration} extraction rows into the space-state graph
1705
     * (preserving the {@code npaalias:} subject) and emits the directional
1706
     * {@code <alias> npa:sameAsSpace <canonical>} edge consumed by the alias-aware
1707
     * admin-authority lookups in {@link #attachmentValidationUpdate},
1708
     * {@link #PUBLISHER_IS_ADMIN}, and {@link #publisherIsTieredRole}.
1709
     *
1710
     * <p>Two gates, both read against the (already-settled) admin closure in the
1711
     * space-state graph:
1712
     * <ul>
1713
     *   <li><b>Authority</b> — the declaration's publisher (resolved via the mirrored
1714
     *       trust-approved {@code AccountState}) is a validated admin of the
1715
     *       <em>canonical</em> space. The alias is declared inside the canonical
1716
     *       space's own {@code gen:Space} nanopub, so this is the same evidence rule
1717
     *       as a {@code gen:hasRole} attachment.</li>
1718
     *   <li><b>Anti-hijack</b> — the alias must not be an independently-governed live
1719
     *       space: it must have no admin who is not also an admin of the canonical
1720
     *       space ({@code admins(alias) ⊆ admins(canonical)}). The common rename case
1721
     *       (the alias's own definition was superseded, so it has no live admin
1722
     *       closure) passes trivially; an attacker publishing
1723
     *       {@code <evil> owl:sameAs <activeSpace>} is rejected because the active
1724
     *       space has admins not in evil's set.</li>
1725
     * </ul>
1726
     *
1727
     * <p>Late-arrival: when the canonical admin grant only becomes valid in the same
1728
     * cycle as the declaration, the load-number filter on {@code ?np} excludes the
1729
     * candidate; the late-arrival sweep ({@link #runDownstreamWithoutLoadFilter})
1730
     * re-runs this pass without the load filter and catches it.
1731
     */
1732
    static String aliasAdmitUpdate(IRI graph, long lastProcessed) {
1733
        // Ref-keyed (see doc/design-spaceref-isolation.md). The declaration names bare
1734
        // canonical/alias IRIs. It is admitted per canonical *ref* whose admin set
1735
        // contains the publisher; the emitted edge is ref-valued on the canonical side
1736
        // (<alias> npa:sameAsSpace <canonicalRef>), which is what the alias-aware admin
1737
        // lookups in the attachment tier consume. Anti-hijack compares the alias IRI's
1738
        // admins against that specific canonical ref's admins — strictly tighter than the
1739
        // old bare-IRI form.
1740
        return """
69✔
1741
                PREFIX npa: <%1$s>
1742
                PREFIX gen: <%2$s>
1743
                INSERT { GRAPH <%3$s> {
1744
                  ?d a npa:SpaceAliasDeclaration ;
1745
                     npa:canonicalSpace ?canonical ;
1746
                     npa:aliasSpace     ?alias ;
1747
                     npa:viaNanopub     ?np .
1748
                  ?alias npa:sameAsSpace ?canonRef .
1749
                  # TRANSITIONAL-DUAL-EMIT (Phase 1.5; remove in Phase 4): IRI-valued
1750
                  # alias edge alongside the ref-valued one, so pre-ref published queries
1751
                  # that resolve owl:sameAs by bare canonical IRI keep binding on a
1752
                  # mixed-version fleet. Internal alias-aware lookups (attachment tier)
1753
                  # join through npa:forSpaceRef, which is ref-valued, so this IRI-valued
1754
                  # object never satisfies them — it is inert internally, read-only for
1755
                  # legacy consumers. See doc/report-2026-06-12-mixed-fleet-spaceref-breakage.md.
1756
                  ?alias npa:sameAsSpace ?canonical .
1757
                } }
1758
                WHERE {
1759
                  # 1. Anchor: candidate alias declarations from the extraction graph.
1760
                  GRAPH <%4$s> {
1761
                    ?d a npa:SpaceAliasDeclaration ;
1762
                       npa:canonicalSpace ?canonical ;
1763
                       npa:aliasSpace     ?alias ;
1764
                       npa:pubkeyHash     ?pkh ;
1765
                       npa:viaNanopub     ?np .
1766
                  }
1767
                  # 2. Authority gate per canonical ref: ?canonRef is a ref of ?canonical
1768
                  #    whose admin set contains the declaration's publisher.
1769
                  GRAPH <%4$s> { ?canonRef npa:spaceIri ?canonical . }
1770
                  GRAPH <%3$s> {
1771
                    ?acct a npa:AccountState ;
1772
                          npa:pubkey ?pkh ;
1773
                          npa:agent  ?publisher .
1774
                    ?adminRI a gen:RoleInstantiation ;
1775
                             npa:inverseProperty gen:hasAdmin ;
1776
                             npa:forSpaceRef ?canonRef ;
1777
                             npa:forAgent ?publisher .
1778
                  }
1779
                  # 3. Anti-hijack: the alias IRI must have no admin who is not also an
1780
                  #    admin of this canonical ref (admins(alias) ⊆ admins(canonRef)).
1781
                  FILTER NOT EXISTS {
1782
                    GRAPH <%3$s> {
1783
                      ?aliasAdmin a gen:RoleInstantiation ;
1784
                                  npa:inverseProperty gen:hasAdmin ;
1785
                                  npa:forSpace ?alias ;
1786
                                  npa:forAgent ?otherAgent .
1787
                    }
1788
                    FILTER NOT EXISTS {
1789
                      GRAPH <%3$s> {
1790
                        ?canonAdmin a gen:RoleInstantiation ;
1791
                                    npa:inverseProperty gen:hasAdmin ;
1792
                                    npa:forSpaceRef ?canonRef ;
1793
                                    npa:forAgent ?otherAgent .
1794
                      }
1795
                    }
1796
                  }
1797
                  # 4. Invalidation filter on the declaration's nanopub.
1798
                  %6$s
1799
                  # 5. Load-number filter on bound ?np.
1800
                  GRAPH <%7$s> {
1801
                    ?np npa:hasLoadNumber ?ln .
1802
                    FILTER (?ln > %5$d)
1803
                  }
1804
                  # 6. Dedup last — on the emitted (alias, canonical ref) edge.
1805
                  FILTER NOT EXISTS { GRAPH <%3$s> {
1806
                    ?alias npa:sameAsSpace ?canonRef .
1807
                  } }
1808
                }
1809
                """.formatted(
3✔
1810
                NPA.NAMESPACE,
1811
                GEN.NAMESPACE,
1812
                graph,
1813
                SpacesVocab.SPACES_GRAPH,
1814
                lastProcessed,
15✔
1815
                invalidationFilter("np"),
18✔
1816
                NPA.GRAPH);
1817
    }
1818

1819
    /**
1820
     * URL-prefix sub-space fallback admit pass. For every pair of {@code SpaceRef}
1821
     * aggregates where the child's {@code npa:hasIdPrefix} matches the parent's
1822
     * {@code npa:spaceIri}, emits convenience {@code <child> npa:isSubSpaceOf <parent>}
1823
     * and {@code <parent> npa:hasSubSpace <child>} direct triples plus a reified
1824
     * {@code npa:DerivedSubSpaceLink} tag carrying {@code npa:derivationKind
1825
     * npa:byUrlPrefix} so consumers can hide derived edges.
1826
     *
1827
     * <p>Per-child suppression: any validated {@code npa:SubSpaceDeclaration} on the
1828
     * child in {@code npass:<…>} suppresses every fallback edge for that child.
1829
     * Suppression checks the validated set (not raw extraction-graph declarations)
1830
     * so an unapproved or in-flight Mode B declaration doesn't silently hide both
1831
     * the URL-prefix fallback and the (still-invalid) explicit relation.
1832
     *
1833
     * <p>Run order: must run after {@link #subSpaceAdmitUpdate} commits in the
1834
     * same cycle so the suppression check sees this cycle's freshly-validated
1835
     * declarations.
1836
     *
1837
     * <p>No load-number filter: the fallback depends on which Spaces exist (parent
1838
     * + child {@code SpaceRef}s), not on which were just added. Always full-scan;
1839
     * the dedup {@code FILTER NOT EXISTS} on the tag IRI prevents re-insertion.
1840
     *
1841
     * <p>No invalidation handling: derived edges have no source nanopub. Two
1842
     * staleness modes: (a) child later gets first validated declaration → old
1843
     * derived edges stay sticky until the next periodic rebuild (same policy as
1844
     * admin-RI invalidation); (b) child loses last validated declaration → the
1845
     * regular fallback pass on the next cycle re-engages, adds derived edges
1846
     * incrementally, no rebuild needed.
1847
     */
1848
    static String subSpacePrefixFallbackUpdate(IRI graph) {
1849
        return """
48✔
1850
                PREFIX npa: <%1$s>
1851
                INSERT { GRAPH <%2$s> {
1852
                  ?childRef  npa:isSubSpaceOf ?parentRef .
1853
                  ?parentRef npa:hasSubSpace  ?childRef  .
1854
                  # TRANSITIONAL-DUAL-EMIT (Phase 1.5; remove in Phase 4): IRI-valued
1855
                  # derived sub-space edge alongside the ref-to-ref one, mirroring the
1856
                  # explicit sub-space pass, so pre-ref published queries keep binding on a
1857
                  # mixed-version fleet. See doc/report-2026-06-12-mixed-fleet-spaceref-breakage.md.
1858
                  ?child  npa:isSubSpaceOf ?parent .
1859
                  ?parent npa:hasSubSpace  ?child  .
1860
                  ?tagIri a npa:DerivedSubSpaceLink ;
1861
                          npa:childSpace     ?child ;
1862
                          npa:parentSpace    ?parent ;
1863
                          npa:derivationKind npa:byUrlPrefix .
1864
                } }
1865
                WHERE {
1866
                  # 1. Anchor: child SpaceRef → its path-prefixes (extracted at load
1867
                  #    time from the Space IRI; see SpacesExtractor.enumerateIdPrefixes).
1868
                  GRAPH <%3$s> {
1869
                    ?childRef  npa:spaceIri    ?child ;
1870
                               npa:hasIdPrefix ?parent .
1871
                    # 2. Parent SpaceRef must exist for the same IRI as the prefix.
1872
                    ?parentRef npa:spaceIri    ?parent .
1873
                  }
1874
                  # 3. Suppress fallback for any child that has a validated declaration
1875
                  #    in this state graph. Per-child IRI, all-or-nothing.
1876
                  FILTER NOT EXISTS {
1877
                    GRAPH <%2$s> {
1878
                      ?d a npa:SubSpaceDeclaration ;
1879
                         npa:childSpace ?child .
1880
                    }
1881
                  }
1882
                  # 4. Mint a deterministic tag IRI per (child ref, parent ref) — the edge
1883
                  #    is emitted ref-to-ref, so the tag and dedup are per ref-pair.
1884
                  BIND(IRI(CONCAT("http://purl.org/nanopub/admin/derivedlink/",
1885
                                  MD5(CONCAT(STR(?childRef), "|", STR(?parentRef))))) AS ?tagIri)
1886
                  # 5. Dedup: don't re-insert if this tag is already present.
1887
                  FILTER NOT EXISTS {
1888
                    GRAPH <%2$s> {
1889
                      ?tagIri a npa:DerivedSubSpaceLink .
1890
                    }
1891
                  }
1892
                }
1893
                """.formatted(
3✔
1894
                NPA.NAMESPACE,
1895
                graph,
1896
                SpacesVocab.SPACES_GRAPH);
1897
    }
1898

1899
    // ---------------- Invalidation templates (incremental cycle) ----------------
1900

1901
    /**
1902
     * WHERE clause shared by the admin-RI invalidation ASK precheck and the
1903
     * matching DELETE. Identifies admin-tier {@code gen:RoleInstantiation} rows
1904
     * in the space-state graph whose {@code npa:viaNanopub} is the target of an
1905
     * {@code npx:invalidates} triple in {@code npa:graph} whose subject nanopub
1906
     * has a load number in {@code (lastProcessed, ∞)}.
1907
     */
1908
    static String adminInvalidationCheckWhere(IRI graph, long lastProcessed) {
1909
        return String.format("""
60✔
1910
                  GRAPH <%1$s> {
1911
                    ?ri a gen:RoleInstantiation ;
1912
                        npa:inverseProperty gen:hasAdmin ;
1913
                        npa:viaNanopub ?np .
1914
                  }
1915
                  GRAPH <%2$s> {
1916
                    ?invNp <%3$s> ?np ;
1917
                           npa:hasLoadNumber ?ln .
1918
                    FILTER (?ln > %4$d)
1919
                    %5$s
1920
                  }
1921
                """, graph, NPA.GRAPH, NPX.INVALIDATES, lastProcessed,
18✔
1922
                samePublisherClause("invNp", "np"));
6✔
1923
    }
1924

1925
    /** DELETE template for admin-tier RoleInstantiations whose source nanopub was invalidated. */
1926
    static String adminInvalidationDelete(IRI graph, long lastProcessed) {
1927
        return String.format("""
63✔
1928
                PREFIX npa: <%1$s>
1929
                PREFIX gen: <%2$s>
1930
                DELETE { GRAPH <%3$s> {
1931
                  ?ri ?p ?o .
1932
                } }
1933
                WHERE {
1934
                  GRAPH <%3$s> { ?ri ?p ?o . }
1935
                %4$s
1936
                }
1937
                """, NPA.NAMESPACE, GEN.NAMESPACE, graph,
1938
                adminInvalidationCheckWhere(graph, lastProcessed));
6✔
1939
    }
1940

1941
    /** WHERE clause for RoleAssignment invalidation. */
1942
    static String roleAssignmentInvalidationCheckWhere(IRI graph, long lastProcessed) {
1943
        return String.format("""
60✔
1944
                  GRAPH <%1$s> {
1945
                    ?ra a gen:RoleAssignment ;
1946
                        npa:viaNanopub ?np .
1947
                  }
1948
                  GRAPH <%2$s> {
1949
                    ?invNp <%3$s> ?np ;
1950
                           npa:hasLoadNumber ?ln .
1951
                    FILTER (?ln > %4$d)
1952
                    %5$s
1953
                  }
1954
                """, graph, NPA.GRAPH, NPX.INVALIDATES, lastProcessed,
18✔
1955
                samePublisherClause("invNp", "np"));
6✔
1956
    }
1957

1958
    /** DELETE template for RoleAssignments whose source nanopub was invalidated. */
1959
    static String roleAssignmentInvalidationDelete(IRI graph, long lastProcessed) {
1960
        return String.format("""
63✔
1961
                PREFIX npa: <%1$s>
1962
                PREFIX gen: <%2$s>
1963
                DELETE { GRAPH <%3$s> {
1964
                  ?ra ?p ?o .
1965
                } }
1966
                WHERE {
1967
                  GRAPH <%3$s> { ?ra ?p ?o . }
1968
                %4$s
1969
                }
1970
                """, NPA.NAMESPACE, GEN.NAMESPACE, graph,
1971
                roleAssignmentInvalidationCheckWhere(graph, lastProcessed));
6✔
1972
    }
1973

1974
    /**
1975
     * DELETE template for non-admin (leaf-tier) RoleInstantiations whose source
1976
     * nanopub was invalidated. Identified as {@code gen:RoleInstantiation} rows
1977
     * lacking the admin-pinning {@code npa:inverseProperty gen:hasAdmin} triple.
1978
     * No flag is set; leaf-tier removals are recoverable on the next cycle.
1979
     */
1980
    static String leafTierInvalidationDelete(IRI graph, long lastProcessed) {
1981
        return String.format("""
84✔
1982
                PREFIX npa: <%1$s>
1983
                PREFIX gen: <%2$s>
1984
                DELETE { GRAPH <%3$s> {
1985
                  ?ri ?p ?o .
1986
                } }
1987
                WHERE {
1988
                  GRAPH <%3$s> {
1989
                    ?ri a gen:RoleInstantiation ;
1990
                        npa:viaNanopub ?np .
1991
                    FILTER NOT EXISTS { ?ri npa:inverseProperty gen:hasAdmin }
1992
                    ?ri ?p ?o .
1993
                  }
1994
                  GRAPH <%4$s> {
1995
                    ?invNp <%5$s> ?np ;
1996
                           npa:hasLoadNumber ?ln .
1997
                    FILTER (?ln > %6$d)
1998
                    %7$s
1999
                  }
2000
                }
2001
                """, NPA.NAMESPACE, GEN.NAMESPACE, graph,
2002
                NPA.GRAPH, NPX.INVALIDATES, lastProcessed,
18✔
2003
                samePublisherClause("invNp", "np"));
6✔
2004
    }
2005

2006
    /**
2007
     * WHERE clause shared by the sub-space invalidation ASK precheck and the
2008
     * matching DELETE. Identifies validated {@code npa:SubSpaceDeclaration} rows
2009
     * in the space-state graph whose {@code npa:viaNanopub} is the target of an
2010
     * {@code npx:invalidates} triple in {@code npa:graph} whose subject nanopub
2011
     * has a load number in {@code (lastProcessed, ∞)}.
2012
     */
2013
    static String subSpaceInvalidationCheckWhere(IRI graph, long lastProcessed) {
2014
        return String.format("""
60✔
2015
                  GRAPH <%1$s> {
2016
                    ?d a npa:SubSpaceDeclaration ;
2017
                       npa:viaNanopub ?np .
2018
                  }
2019
                  GRAPH <%2$s> {
2020
                    ?invNp <%3$s> ?np ;
2021
                           npa:hasLoadNumber ?ln .
2022
                    FILTER (?ln > %4$d)
2023
                    %5$s
2024
                  }
2025
                """, graph, NPA.GRAPH, NPX.INVALIDATES, lastProcessed,
18✔
2026
                samePublisherClause("invNp", "np"));
6✔
2027
    }
2028

2029
    /**
2030
     * DELETE template for validated {@code npa:SubSpaceDeclaration} rows whose
2031
     * source nanopub was invalidated. Removes the per-declaration row by subject;
2032
     * the convenience direct triples ({@code <child> npa:isSubSpaceOf <parent>}
2033
     * and inverse) are left sticky and cleaned by the next periodic full rebuild
2034
     * (same staleness policy as admin-RI invalidation — see {@code
2035
     * doc/design-space-repositories.md} on the structural-rebuild flag).
2036
     */
2037
    static String subSpaceInvalidationDelete(IRI graph, long lastProcessed) {
2038
        return String.format("""
63✔
2039
                PREFIX npa: <%1$s>
2040
                PREFIX gen: <%2$s>
2041
                DELETE { GRAPH <%3$s> {
2042
                  ?d ?p ?o .
2043
                } }
2044
                WHERE {
2045
                  GRAPH <%3$s> { ?d ?p ?o . }
2046
                %4$s
2047
                }
2048
                """, NPA.NAMESPACE, GEN.NAMESPACE, graph,
2049
                subSpaceInvalidationCheckWhere(graph, lastProcessed));
6✔
2050
    }
2051

2052
    /**
2053
     * DELETE template for validated {@code npa:MaintainedResourceDeclaration} rows
2054
     * whose source nanopub was invalidated. Removes the per-declaration row by
2055
     * subject; the convenience direct triples ({@code <r> npa:isMaintainedBy <s>}
2056
     * and inverse) are left sticky and cleaned by the next periodic full rebuild
2057
     * (same staleness policy as sub-space declaration invalidation, but without
2058
     * the structural-rebuild flag — maintained-resource is a leaf relation, no
2059
     * downstream consumers depend on its closure).
2060
     */
2061
    static String maintainedResourceInvalidationDelete(IRI graph, long lastProcessed) {
2062
        return String.format("""
84✔
2063
                PREFIX npa: <%1$s>
2064
                PREFIX gen: <%2$s>
2065
                DELETE { GRAPH <%3$s> {
2066
                  ?d ?p ?o .
2067
                } }
2068
                WHERE {
2069
                  GRAPH <%3$s> {
2070
                    ?d a npa:MaintainedResourceDeclaration ;
2071
                       npa:viaNanopub ?np .
2072
                    ?d ?p ?o .
2073
                  }
2074
                  GRAPH <%4$s> {
2075
                    ?invNp <%5$s> ?np ;
2076
                           npa:hasLoadNumber ?ln .
2077
                    FILTER (?ln > %6$d)
2078
                    %7$s
2079
                  }
2080
                }
2081
                """, NPA.NAMESPACE, GEN.NAMESPACE, graph,
2082
                NPA.GRAPH, NPX.INVALIDATES, lastProcessed,
18✔
2083
                samePublisherClause("invNp", "np"));
6✔
2084
    }
2085

2086
    /**
2087
     * WHERE clause shared by the alias invalidation ASK precheck and the matching
2088
     * DELETE. Identifies validated {@code npa:SpaceAliasDeclaration} rows in the
2089
     * space-state graph whose {@code npa:viaNanopub} is the target of an
2090
     * {@code npx:invalidates} triple in {@code npa:graph} whose subject nanopub has a
2091
     * load number in {@code (lastProcessed, ∞)}.
2092
     */
2093
    static String aliasInvalidationCheckWhere(IRI graph, long lastProcessed) {
2094
        return String.format("""
60✔
2095
                  GRAPH <%1$s> {
2096
                    ?d a npa:SpaceAliasDeclaration ;
2097
                       npa:viaNanopub ?np .
2098
                  }
2099
                  GRAPH <%2$s> {
2100
                    ?invNp <%3$s> ?np ;
2101
                           npa:hasLoadNumber ?ln .
2102
                    FILTER (?ln > %4$d)
2103
                    %5$s
2104
                  }
2105
                """, graph, NPA.GRAPH, NPX.INVALIDATES, lastProcessed,
18✔
2106
                samePublisherClause("invNp", "np"));
6✔
2107
    }
2108

2109
    /**
2110
     * DELETE template for validated {@code npa:SpaceAliasDeclaration} rows whose
2111
     * source nanopub was invalidated. Removes the per-declaration row by subject; the
2112
     * convenience {@code <alias> npa:sameAsSpace <canonical>} edge is left sticky and
2113
     * cleaned by the next periodic full rebuild (same staleness policy as sub-space
2114
     * declaration invalidation — the alias feeds the authority closure, so this kind
2115
     * is structural and flips {@code npa:needsFullRebuild}).
2116
     */
2117
    static String aliasInvalidationDelete(IRI graph, long lastProcessed) {
2118
        return String.format("""
63✔
2119
                PREFIX npa: <%1$s>
2120
                PREFIX gen: <%2$s>
2121
                DELETE { GRAPH <%3$s> {
2122
                  ?d ?p ?o .
2123
                } }
2124
                WHERE {
2125
                  GRAPH <%3$s> { ?d ?p ?o . }
2126
                %4$s
2127
                }
2128
                """, NPA.NAMESPACE, GEN.NAMESPACE, graph,
2129
                aliasInvalidationCheckWhere(graph, lastProcessed));
6✔
2130
    }
2131

2132
    /**
2133
     * WHERE clause shared by the preset-deactivation ASK precheck and the matching DELETE
2134
     * (Nanodash issue #302). Binds {@code ?ra} = a materialized preset-derived
2135
     * {@code gen:RoleAssignment} ({@code npa:derivedFromPreset}) for which a <em>newer,
2136
     * admin-authored</em> same-{@code (preset, resource)} assignment exists by
2137
     * {@code dct:created} (load number in {@code (lastProcessed, ∞)}). This is NOT an
2138
     * {@code npx:invalidates} check — preset activation is latest-wins by timestamp.
2139
     *
2140
     * <p>Authorization-scoped (anti-hijack, design doc §3/§4.4): the newer assignment's
2141
     * publisher must itself be a validated admin of the row's {@code npa:forSpaceRef}, so an
2142
     * unauthorized key's newer assignment can neither delete nor shadow an admin's
2143
     * materialized role. {@code dct:created} is written as a full IRI (not a {@code dct:}
2144
     * prefix) because {@link #wouldInvalidate}'s ASK wrapper only declares {@code npa:} /
2145
     * {@code gen:}.
2146
     */
2147
    static String presetDeactivationCheckWhere(IRI graph, long lastProcessed) {
2148
        return String.format("""
60✔
2149
                  GRAPH <%1$s> {
2150
                    ?ra a gen:RoleAssignment ;
2151
                        npa:derivedFromPreset ?assignNp ;
2152
                        npa:forSpaceRef ?targetRef .
2153
                  }
2154
                  GRAPH <%2$s> {
2155
                    ?pa a npa:PresetAssignment ;
2156
                        npa:viaNanopub  ?assignNp ;
2157
                        npa:ofPreset    ?preset ;
2158
                        npa:forResource ?resource ;
2159
                        <http://purl.org/dc/terms/created> ?created .
2160
                    ?paNewer a npa:PresetAssignment ;
2161
                             npa:ofPreset    ?preset ;
2162
                             npa:forResource ?resource ;
2163
                             npa:pubkeyHash  ?pkhNewer ;
2164
                             npa:viaNanopub  ?assignNpNewer ;
2165
                             <http://purl.org/dc/terms/created> ?createdNewer .
2166
                    FILTER (?createdNewer > ?created
2167
                            || (?createdNewer = ?created && STR(?paNewer) > STR(?pa)))
2168
                  }
2169
                  GRAPH <%3$s> {
2170
                    ?assignNpNewer npa:hasLoadNumber ?lnNewer .
2171
                    FILTER (?lnNewer > %4$d)
2172
                  }
2173
                  GRAPH <%1$s> {
2174
                    ?acctNewer a npa:AccountState ;
2175
                               npa:agent  ?publisherNewer ;
2176
                               npa:pubkey ?pkhNewer .
2177
                    ?adminRINewer a gen:RoleInstantiation ;
2178
                                  npa:forSpaceRef ?targetRef ;
2179
                                  npa:inverseProperty gen:hasAdmin ;
2180
                                  npa:forAgent ?publisherNewer .
2181
                  }
2182
                """, graph, SpacesVocab.SPACES_GRAPH, NPA.GRAPH, lastProcessed);
6✔
2183
    }
2184

2185
    /**
2186
     * DELETE template for preset-derived {@code gen:RoleAssignment} rows superseded by a
2187
     * newer admin-authored same-pair assignment (issue #302). Removes the whole row by
2188
     * subject; scoped via {@code npa:derivedFromPreset} so directly-published attachments
2189
     * are never touched. The {@link #presetAttachmentValidationUpdate} re-INSERT in the
2190
     * same cycle re-materializes the pair iff the newest assignment is still active.
2191
     */
2192
    static String presetDeactivationDelete(IRI graph, long lastProcessed) {
2193
        return String.format("""
63✔
2194
                PREFIX npa: <%1$s>
2195
                PREFIX gen: <%2$s>
2196
                DELETE { GRAPH <%3$s> {
2197
                  ?ra ?p ?o .
2198
                } }
2199
                WHERE {
2200
                  GRAPH <%3$s> { ?ra ?p ?o . }
2201
                %4$s
2202
                }
2203
                """, NPA.NAMESPACE, GEN.NAMESPACE, graph,
2204
                presetDeactivationCheckWhere(graph, lastProcessed));
6✔
2205
    }
2206

2207
    /**
2208
     * DELETE template for ref-scoped preset-assignment stamps ({@link
2209
     * #presetAssignmentRefStampUpdate}) whose underlying assignment nanopub was
2210
     * hard-retracted (issue #122). Removes the whole row by subject; scoped to
2211
     * state-graph {@code npa:PresetAssignment} rows that carry {@code npa:forSpaceRef}
2212
     * (the IRI-keyed extraction rows never do), so it can never touch them.
2213
     *
2214
     * <p>Leaf delete — no structural flag: nothing downstream derives from a listing
2215
     * stamp, so a stale row only mis-displays a retracted assignment until this cycle's
2216
     * delete runs. Admin-grant revocation is bounded by the periodic full rebuild (same
2217
     * sticky-convenience policy as the alias / sub-space declaration edges). A
2218
     * <em>deactivation</em> needs no delete here: it is represented as a newer
2219
     * admin-authored stamp with {@code npa:isActivated false}, resolved by the consumer's
2220
     * latest-wins.
2221
     */
2222
    static String presetAssignmentRefInvalidationDelete(IRI graph, long lastProcessed) {
2223
        return String.format("""
84✔
2224
                PREFIX npa: <%1$s>
2225
                PREFIX gen: <%2$s>
2226
                DELETE { GRAPH <%3$s> {
2227
                  ?paRef ?p ?o .
2228
                } }
2229
                WHERE {
2230
                  GRAPH <%3$s> {
2231
                    ?paRef a npa:PresetAssignment ;
2232
                           npa:forSpaceRef ?targetRef ;
2233
                           npa:viaNanopub  ?assignNp .
2234
                    ?paRef ?p ?o .
2235
                  }
2236
                  GRAPH <%4$s> {
2237
                    ?invNp <%5$s> ?assignNp ;
2238
                           npa:hasLoadNumber ?ln .
2239
                    FILTER (?ln > %6$d)
2240
                    %7$s
2241
                  }
2242
                }
2243
                """, NPA.NAMESPACE, GEN.NAMESPACE, graph,
2244
                NPA.GRAPH, NPX.INVALIDATES, lastProcessed,
18✔
2245
                samePublisherClause("invNp", "assignNp"));
6✔
2246
    }
2247

2248
    /** Wraps an ASK by joining the shared prefixes. */
2249
    private boolean wouldInvalidate(IRI graph, long lastProcessed,
2250
                                    boolean adminPinned, String whereClause) {
2251
        // adminPinned is informational only — kept to make call sites read clearly;
2252
        // the WHERE clause already encodes the kind via its own type predicates.
2253
        String ask = String.format("""
×
2254
                PREFIX npa: <%1$s>
2255
                PREFIX gen: <%2$s>
2256
                ASK { %3$s }
2257
                """, NPA.NAMESPACE, GEN.NAMESPACE, whereClause);
2258
        return runAsk(ask);
×
2259
    }
2260

2261
    private boolean runAsk(String sparql) {
2262
        try (RepositoryConnection conn = TripleStore.get().getRepoConnection(SPACES_REPO)) {
×
2263
            return conn.prepareBooleanQuery(QueryLanguage.SPARQL, sparql).evaluate();
×
2264
        }
2265
    }
2266

2267
    private void executeUpdate(String sparqlUpdate) {
2268
        try (RepositoryConnection conn = TripleStore.get().getRepoConnection(SPACES_REPO)) {
×
2269
            conn.prepareUpdate(QueryLanguage.SPARQL, sparqlUpdate).execute();
×
2270
        }
2271
    }
×
2272

2273
    // ---------------- Mirror step ----------------
2274

2275
    /**
2276
     * Copies trust-approved {@code npa:AccountState} rows from {@code npat:<T>}
2277
     * in the {@code trust} repo into {@code newGraph} in the {@code spaces} repo,
2278
     * inside one spaces-side serializable transaction.
2279
     *
2280
     * @return number of rows mirrored (useful for metrics / logging)
2281
     */
2282
    int mirrorTrustState(String trustStateHash, IRI newGraph) {
2283
        IRI trustStateIri = NPAT.forHash(trustStateHash);
×
2284
        int count = 0;
×
2285
        try (RepositoryConnection trustConn = TripleStore.get().getRepoConnection(TRUST_REPO);
×
2286
             RepositoryConnection spacesConn = TripleStore.get().getRepoConnection(SPACES_REPO)) {
×
2287
            trustConn.begin(IsolationLevels.READ_COMMITTED);
×
2288
            spacesConn.begin(IsolationLevels.SERIALIZABLE);
×
2289
            // Walk rdf:type triples in the trust state's graph; for each AccountState,
2290
            // check status and copy the approved ones verbatim (minus status-specific
2291
            // detail triples, which we don't need for validation).
2292
            try (RepositoryResult<Statement> typeRows = trustConn.getStatements(
×
2293
                    null, RDF.TYPE, NPA_ACCOUNT_STATE, trustStateIri)) {
2294
                while (typeRows.hasNext()) {
×
2295
                    Statement st = typeRows.next();
×
2296
                    if (!(st.getSubject() instanceof IRI accountStateIri)) continue;
×
2297
                    Value status = trustConn.getStatements(accountStateIri, NPA_TRUST_STATUS, null, trustStateIri)
×
2298
                            .stream().findFirst().map(Statement::getObject).orElse(null);
×
2299
                    if (!(status instanceof IRI statusIri) || !APPROVED_SET.contains(statusIri)) continue;
×
2300
                    Value agent = trustConn.getStatements(accountStateIri, NPA_AGENT, null, trustStateIri)
×
2301
                            .stream().findFirst().map(Statement::getObject).orElse(null);
×
2302
                    Value pubkey = trustConn.getStatements(accountStateIri, NPA_PUBKEY, null, trustStateIri)
×
2303
                            .stream().findFirst().map(Statement::getObject).orElse(null);
×
2304
                    if (agent == null || pubkey == null) {
×
2305
                        logger.warn("AuthorityResolver.mirror: account {} missing agent or pubkey; skipping",
×
2306
                                accountStateIri);
2307
                        continue;
×
2308
                    }
2309
                    spacesConn.add(accountStateIri, RDF.TYPE, NPA_ACCOUNT_STATE, newGraph);
×
2310
                    spacesConn.add(accountStateIri, NPA_AGENT, agent, newGraph);
×
2311
                    spacesConn.add(accountStateIri, NPA_PUBKEY, pubkey, newGraph);
×
2312
                    spacesConn.add(accountStateIri, NPA_TRUST_STATUS, statusIri, newGraph);
×
2313
                    count++;
×
2314
                }
×
2315
            }
2316
            // Mirror canonical foaf:name triples for approved agents. The trust
2317
            // loader emits one per agent (across approved keys, MAX(ratio) wins).
2318
            // Copying them into the space-state graph means consumers reading
2319
            // ?agent foaf:name ?n inside the state graph hit local data, with no
2320
            // cross-repo SERVICE.
2321
            try (RepositoryResult<Statement> nameRows = trustConn.getStatements(
×
2322
                    null, FOAF.NAME, null, trustStateIri)) {
2323
                while (nameRows.hasNext()) {
×
2324
                    Statement st = nameRows.next();
×
2325
                    spacesConn.add(st.getSubject(), st.getPredicate(), st.getObject(), newGraph);
×
2326
                }
×
2327
            }
2328
            spacesConn.commit();
×
2329
            trustConn.commit();
×
2330
        }
2331
        return count;
×
2332
    }
2333

2334
    // ---------------- Pointer + counter helpers ----------------
2335

2336
    /**
2337
     * Reads the current {@code npa:hasCurrentSpaceState} pointer from the
2338
     * {@code npa:graph} admin graph of the {@code spaces} repo. Returns
2339
     * {@code null} if no pointer exists yet.
2340
     */
2341
    IRI getCurrentSpaceStateGraph() {
2342
        try (RepositoryConnection conn = TripleStore.get().getRepoConnection(SPACES_REPO)) {
×
2343
            Value v = Utils.getObjectForPattern(conn, NPA.GRAPH, NPA.THIS_REPO,
×
2344
                    SpacesVocab.HAS_CURRENT_SPACE_STATE);
2345
            return (v instanceof IRI iri) ? iri : null;
×
2346
        } catch (Exception ex) {
×
2347
            logger.warn("AuthorityResolver: failed to read hasCurrentSpaceState pointer: {}", ex.toString());
×
2348
            return null;
×
2349
        }
2350
    }
2351

2352
    long getCurrentLoadCounter() {
2353
        try (RepositoryConnection conn = TripleStore.get().getRepoConnection(SPACES_REPO)) {
×
2354
            Value v = Utils.getObjectForPattern(conn, NPA.GRAPH, NPA.THIS_REPO,
×
2355
                    SpacesVocab.CURRENT_LOAD_COUNTER);
2356
            if (v == null) return 0;
×
2357
            try {
2358
                return Long.parseLong(v.stringValue());
×
2359
            } catch (NumberFormatException ex) {
×
2360
                logger.warn("AuthorityResolver: non-numeric currentLoadCounter: {}", v);
×
2361
                return 0;
×
2362
            }
2363
        } catch (Exception ex) {
×
2364
            logger.warn("AuthorityResolver: failed to read currentLoadCounter: {}", ex.toString());
×
2365
            return 0;
×
2366
        }
2367
    }
2368

2369
    /**
2370
     * Atomic pointer flip: a single SPARQL {@code DELETE … INSERT … WHERE}
2371
     * replaces the old pointer with the new one in one statement, so readers
2372
     * never see a zero-pointer window.
2373
     */
2374
    void flipPointer(IRI newGraph) {
2375
        String update = String.format("""
×
2376
                DELETE { GRAPH <%s> { <%s> <%s> ?old } }
2377
                INSERT { GRAPH <%s> { <%s> <%s> <%s> } }
2378
                WHERE  { OPTIONAL { GRAPH <%s> { <%s> <%s> ?old } } }
2379
                """,
2380
                NPA.GRAPH, NPA.THIS_REPO, SpacesVocab.HAS_CURRENT_SPACE_STATE,
2381
                NPA.GRAPH, NPA.THIS_REPO, SpacesVocab.HAS_CURRENT_SPACE_STATE, newGraph,
2382
                NPA.GRAPH, NPA.THIS_REPO, SpacesVocab.HAS_CURRENT_SPACE_STATE);
2383
        try (RepositoryConnection conn = TripleStore.get().getRepoConnection(SPACES_REPO)) {
×
2384
            conn.begin(IsolationLevels.SERIALIZABLE);
×
2385
            conn.prepareUpdate(QueryLanguage.SPARQL, update).execute();
×
2386
            conn.commit();
×
2387
        }
2388
    }
×
2389

2390
    void writeProcessedUpTo(IRI graph, long loadCounter) {
2391
        String update = String.format("""
×
2392
                DELETE { GRAPH <%s> { <%s> <%s> ?old } }
2393
                INSERT { GRAPH <%s> { <%s> <%s> "%d"^^<http://www.w3.org/2001/XMLSchema#long> } }
2394
                WHERE  { OPTIONAL { GRAPH <%s> { <%s> <%s> ?old } } }
2395
                """,
2396
                graph, graph, SpacesVocab.PROCESSED_UP_TO,
2397
                graph, graph, SpacesVocab.PROCESSED_UP_TO, loadCounter,
×
2398
                graph, graph, SpacesVocab.PROCESSED_UP_TO);
2399
        try (RepositoryConnection conn = TripleStore.get().getRepoConnection(SPACES_REPO)) {
×
2400
            conn.begin(IsolationLevels.SERIALIZABLE);
×
2401
            conn.prepareUpdate(QueryLanguage.SPARQL, update).execute();
×
2402
            conn.commit();
×
2403
        }
2404
    }
×
2405

2406
    /**
2407
     * Reads {@code processedUpTo} from the given space-state graph.
2408
     * Returns {@code -1} if absent (graph not fully built yet).
2409
     */
2410
    long readProcessedUpTo(IRI graph) {
2411
        try (RepositoryConnection conn = TripleStore.get().getRepoConnection(SPACES_REPO)) {
×
2412
            String query = String.format(
×
2413
                    "SELECT ?n WHERE { GRAPH <%s> { <%s> <%s> ?n } }",
2414
                    graph, graph, SpacesVocab.PROCESSED_UP_TO);
2415
            try (TupleQueryResult r = conn.prepareTupleQuery(QueryLanguage.SPARQL, query).evaluate()) {
×
2416
                if (!r.hasNext()) return -1;
×
2417
                BindingSet b = r.next();
×
2418
                return Long.parseLong(b.getBinding("n").getValue().stringValue());
×
2419
            }
×
2420
        } catch (Exception ex) {
×
2421
            logger.warn("AuthorityResolver: failed to read processedUpTo for {}: {}", graph, ex.toString());
×
2422
            return -1;
×
2423
        }
2424
    }
2425

2426
    /**
2427
     * Reads the {@code npa:needsFullRebuild} flag (boolean literal) from
2428
     * {@code npa:graph} in the {@code spaces} repo. Defaults to {@code false}
2429
     * when the triple is absent.
2430
     */
2431
    boolean readNeedsFullRebuild() {
2432
        try (RepositoryConnection conn = TripleStore.get().getRepoConnection(SPACES_REPO)) {
×
2433
            Value v = Utils.getObjectForPattern(conn, NPA.GRAPH, NPA.THIS_REPO,
×
2434
                    SpacesVocab.NEEDS_FULL_REBUILD);
2435
            return v != null && Boolean.parseBoolean(v.stringValue());
×
2436
        } catch (Exception ex) {
×
2437
            logger.warn("AuthorityResolver: failed to read needsFullRebuild: {}", ex.toString());
×
2438
            return false;
×
2439
        }
2440
    }
2441

2442
    void setNeedsFullRebuild() {
2443
        writeNeedsFullRebuild(true);
×
2444
    }
×
2445

2446
    void clearNeedsFullRebuild() {
2447
        writeNeedsFullRebuild(false);
×
2448
    }
×
2449

2450
    private void writeNeedsFullRebuild(boolean value) {
2451
        String update = String.format("""
×
2452
                DELETE { GRAPH <%s> { <%s> <%s> ?old } }
2453
                INSERT { GRAPH <%s> { <%s> <%s> "%s"^^<http://www.w3.org/2001/XMLSchema#boolean> } }
2454
                WHERE  { OPTIONAL { GRAPH <%s> { <%s> <%s> ?old } } }
2455
                """,
2456
                NPA.GRAPH, NPA.THIS_REPO, SpacesVocab.NEEDS_FULL_REBUILD,
2457
                NPA.GRAPH, NPA.THIS_REPO, SpacesVocab.NEEDS_FULL_REBUILD, value,
×
2458
                NPA.GRAPH, NPA.THIS_REPO, SpacesVocab.NEEDS_FULL_REBUILD);
2459
        try (RepositoryConnection conn = TripleStore.get().getRepoConnection(SPACES_REPO)) {
×
2460
            conn.begin(IsolationLevels.SERIALIZABLE);
×
2461
            conn.prepareUpdate(QueryLanguage.SPARQL, update).execute();
×
2462
            conn.commit();
×
2463
        }
2464
    }
×
2465

2466
    void dropGraph(IRI graph) {
2467
        try (RepositoryConnection conn = TripleStore.get().getRepoConnection(SPACES_REPO)) {
×
2468
            conn.begin(IsolationLevels.SERIALIZABLE);
×
2469
            conn.clear(graph);
×
2470
            conn.commit();
×
2471
            logger.info("AuthorityResolver: dropped old space-state graph {}", graph);
×
2472
        }
2473
    }
×
2474

2475
    // ---------------- Trust-repo pointer lookup (used by TrustStateRegistry's bootstrap) ----------------
2476

2477
    /**
2478
     * Queries the {@code trust} repo directly for the current trust-state hash.
2479
     * Prefer {@link TrustStateRegistry#getCurrentHash()} in normal operation —
2480
     * this helper exists for tests and diagnostics.
2481
     *
2482
     * @return the current trust-state hash, or empty if none is set
2483
     */
2484
    Optional<String> readTrustRepoCurrentHash() {
2485
        try (RepositoryConnection conn = TripleStore.get().getRepoConnection(TRUST_REPO)) {
×
2486
            Value v = Utils.getObjectForPattern(conn, NPA.GRAPH, NPA.THIS_REPO,
×
2487
                    NPA_HAS_CURRENT_TRUST_STATE);
2488
            if (!(v instanceof IRI iri)) return Optional.empty();
×
2489
            String s = iri.stringValue();
×
2490
            if (!s.startsWith(NPAT.NAMESPACE)) return Optional.empty();
×
2491
            return Optional.of(s.substring(NPAT.NAMESPACE.length()));
×
2492
        } catch (Exception ex) {
×
2493
            logger.warn("AuthorityResolver: failed to read trust-repo current pointer: {}", ex.toString());
×
2494
            return Optional.empty();
×
2495
        }
2496
    }
2497

2498
    private static String abbrev(String hash) {
2499
        return hash.length() > 12 ? hash.substring(0, 12) + "…" : hash;
×
2500
    }
2501

2502
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc