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

knowledgepixels / nanopub-registry / 24142185107

08 Apr 2026 02:56PM UTC coverage: 32.357% (-0.5%) from 32.824%
24142185107

push

github

web-flow
Merge pull request #98 from knowledgepixels/fix/revert-seqnum-batch-allocation

Revert seqNum batch allocation, use atomic counter

265 of 916 branches covered (28.93%)

Branch coverage included in aggregate %.

785 of 2329 relevant lines covered (33.71%)

5.66 hits per line

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

49.0
src/main/java/com/knowledgepixels/registry/AgentFilter.java
1
package com.knowledgepixels.registry;
2

3
import com.mongodb.client.ClientSession;
4
import org.bson.Document;
5
import org.slf4j.Logger;
6
import org.slf4j.LoggerFactory;
7

8
import java.util.Collections;
9
import java.util.HashMap;
10
import java.util.Map;
11

12
import static com.knowledgepixels.registry.RegistryDB.collection;
13

14
/**
15
 * Controls which pubkeys are allowed to publish nanopubs and what their quotas are.
16
 * Configured via REGISTRY_COVERAGE_AGENTS env var.
17
 *
18
 * Format: whitespace-separated entries, each being either:
19
 * - "viaSetting" — include all agents approved by the trust network (with computed quotas)
20
 * - "pubkeyHash:quota" — include a specific pubkey with an explicit quota
21
 *
22
 * Example: viaSetting abc123...def456:5000 789xyz...abc012:10000
23
 *
24
 * When unset, defaults to viaSetting (all trusted agents, no restrictions).
25
 */
26
public final class AgentFilter {
27

28
    private static final Logger logger = LoggerFactory.getLogger(AgentFilter.class);
9✔
29

30
    private static volatile boolean viaSetting = true;
6✔
31
    private static volatile boolean enforceQuota = false;
6✔
32
    private static volatile Map<String, Integer> explicitPubkeys = Collections.emptyMap();
9✔
33

34
    private AgentFilter() {}
35

36
    /**
37
     * Initializes the agent filter from the REGISTRY_COVERAGE_AGENTS env var.
38
     */
39
    public static void init() {
40
        String config = Utils.getEnv("REGISTRY_COVERAGE_AGENTS", "viaSetting");
12✔
41
        boolean via = false;
6✔
42
        Map<String, Integer> pubkeys = new HashMap<>();
12✔
43

44
        for (String entry : config.trim().split("\\s+")) {
57✔
45
            if (entry.isEmpty()) continue;
9!
46
            if ("viaSetting".equals(entry)) {
12✔
47
                via = true;
9✔
48
            } else if (entry.contains(":")) {
12✔
49
                String[] parts = entry.split(":", 2);
15✔
50
                String pubkeyHash = parts[0];
12✔
51
                int quota;
52
                try {
53
                    quota = Integer.parseInt(parts[1]);
15✔
54
                } catch (NumberFormatException e) {
3✔
55
                    throw new IllegalArgumentException(
18✔
56
                            "Invalid quota in REGISTRY_COVERAGE_AGENTS: " + entry);
57
                }
3✔
58
                pubkeys.put(pubkeyHash, quota);
18✔
59
            } else {
3✔
60
                throw new IllegalArgumentException(
18✔
61
                        "Invalid entry in REGISTRY_COVERAGE_AGENTS: " + entry
62
                                + " (expected 'viaSetting' or 'pubkeyHash:quota')");
63
            }
64
        }
65

66
        viaSetting = via;
6✔
67
        enforceQuota = "true".equals(System.getenv("REGISTRY_ENFORCE_QUOTA"));
15✔
68
        explicitPubkeys = Collections.unmodifiableMap(pubkeys);
9✔
69

70
        if (via && pubkeys.isEmpty()) {
15✔
71
            logger.info("Agent filter: viaSetting (all trusted agents)");
12✔
72
        } else if (via) {
6✔
73
            logger.info("Agent filter: viaSetting + {} explicit pubkeys", pubkeys.size());
21✔
74
        } else {
75
            logger.info("Agent filter: {} explicit pubkeys only", pubkeys.size());
18✔
76
        }
77

78
        if (via && "false".equals(System.getenv("REGISTRY_ENABLE_TRUST_CALCULATION"))) {
21!
79
            logger.warn("viaSetting is enabled but trust calculation is disabled — " +
×
80
                    "no agents will be discovered via the trust network; only explicit pubkeys will work");
81
        }
82
    }
3✔
83

84
    /**
85
     * Returns true if the trust network (viaSetting) is used for agent approval.
86
     */
87
    public static boolean usesViaSetting() {
88
        return viaSetting;
6✔
89
    }
90

91
    /**
92
     * Returns the explicitly configured pubkeys with their quotas.
93
     */
94
    public static Map<String, Integer> getExplicitPubkeys() {
95
        return explicitPubkeys;
6✔
96
    }
97

98
    /**
99
     * Returns the quota for a given pubkey, checking explicit config first,
100
     * then the accounts collection for trust-computed quotas.
101
     * Returns -1 if the pubkey is not allowed.
102
     */
103
    public static int getQuota(ClientSession session, String pubkeyHash) {
104
        // Explicit pubkeys take precedence
105
        if (explicitPubkeys.containsKey(pubkeyHash)) {
×
106
            return explicitPubkeys.get(pubkeyHash);
×
107
        }
108

109
        // Check trust network if enabled
110
        if (viaSetting) {
×
111
            Document account = collection(Collection.ACCOUNTS.toString()).find(session,
×
112
                    new Document("pubkey", pubkeyHash).append("status", "loaded")).first();
×
113
            if (account != null && account.get("quota") != null) {
×
114
                return account.getInteger("quota");
×
115
            }
116
            // Also accept toLoad accounts (approved but not yet fully loaded)
117
            account = collection(Collection.ACCOUNTS.toString()).find(session,
×
118
                    new Document("pubkey", pubkeyHash).append("status", "toLoad")).first();
×
119
            if (account != null && account.get("quota") != null) {
×
120
                return account.getInteger("quota");
×
121
            }
122
        }
123

124
        return -1; // not allowed
×
125
    }
126

127
    /**
128
     * Returns true if the given pubkey is allowed to publish (has a quota >= 0).
129
     * Always returns true if quota enforcement is disabled.
130
     */
131
    public static boolean isAllowed(ClientSession session, String pubkeyHash) {
132
        if (!enforceQuota) return true;
×
133
        return getQuota(session, pubkeyHash) >= 0;
×
134
    }
135

136
    /**
137
     * Returns true if the given pubkey has exceeded its quota.
138
     * Always returns false if quota enforcement is disabled.
139
     */
140
    public static boolean isOverQuota(ClientSession session, String pubkeyHash) {
141
        if (!enforceQuota) return false;
×
142
        int quota = getQuota(session, pubkeyHash);
×
143
        if (quota < 0) return true; // not allowed at all
×
144

145
        // Count nanopubs for this pubkey via the "$" list position
146
        Document listDoc = collection("lists").find(session,
×
147
                new Document("pubkey", pubkeyHash).append("type", "$")).first();
×
148
        long currentCount = (listDoc != null && listDoc.get("maxPosition") != null)
×
149
                ? listDoc.getLong("maxPosition") + 1 : 0;
×
150

151
        return currentCount >= quota;
×
152
    }
153
}
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