@tpsdev-ai/flair 0.58.0 → 0.62.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -6
- package/SECURITY.md +36 -0
- package/config.yaml +15 -1
- package/dist/bridges/builtins/mem0.js +63 -28
- package/dist/bridges/runtime/import-runner.js +12 -0
- package/dist/bridges/runtime/load-plugin.js +42 -13
- package/dist/bridges/runtime/yaml-loader.js +4 -2
- package/dist/build-info.json +3 -3
- package/dist/cli.js +1030 -343
- package/dist/commands/agent.js +384 -45
- package/dist/commands/attention.js +4 -3
- package/dist/commands/backup.js +202 -34
- package/dist/commands/bridge.js +172 -119
- package/dist/commands/deploy.js +37 -4
- package/dist/commands/doctor.js +601 -163
- package/dist/commands/export.js +54 -19
- package/dist/commands/federation.js +141 -21
- package/dist/commands/hook.js +155 -2
- package/dist/commands/idp.js +14 -1
- package/dist/commands/import.js +7 -3
- package/dist/commands/init.js +462 -193
- package/dist/commands/integrity.js +388 -0
- package/dist/commands/keys.js +247 -58
- package/dist/commands/mcp.js +146 -53
- package/dist/commands/memory.js +171 -21
- package/dist/commands/migrate-harness-memory.js +3 -2
- package/dist/commands/orgevent.js +46 -37
- package/dist/commands/presence.js +4 -3
- package/dist/commands/principal.js +426 -48
- package/dist/commands/quality.js +10 -1
- package/dist/commands/reembed.js +141 -17
- package/dist/commands/relationship.js +2 -1
- package/dist/commands/rem.js +56 -18
- package/dist/commands/restore.js +101 -61
- package/dist/commands/search.js +6 -4
- package/dist/commands/service.js +151 -38
- package/dist/commands/soul.js +4 -3
- package/dist/commands/status.js +43 -5
- package/dist/commands/test.js +3 -2
- package/dist/commands/upgrade.js +24 -1
- package/dist/commands/workspace.js +6 -3
- package/dist/component-env.js +31 -41
- package/dist/deploy.js +77 -1
- package/dist/doctor-client.js +529 -92
- package/dist/engine-version-contract.js +1 -0
- package/dist/engine-version.js +23 -3
- package/dist/federation-verify.js +4 -3
- package/dist/hook-install.js +670 -6
- package/dist/keystore.js +72 -0
- package/dist/lib/action-recall-runtime.js +328 -0
- package/dist/lib/agent-home-fields.js +132 -0
- package/dist/lib/agent-home.js +427 -0
- package/dist/lib/agent-id-rule.js +46 -0
- package/dist/lib/agent-roster.js +33 -0
- package/dist/lib/auth-resolve.js +49 -16
- package/dist/lib/capture-runtime.js +124 -0
- package/dist/lib/credential-status-rule.js +78 -0
- package/dist/lib/daemon-liveness.js +81 -8
- package/dist/lib/doctor-run.js +140 -35
- package/dist/lib/durability-copy.js +10 -0
- package/dist/lib/embedding-space-doctor.js +75 -0
- package/dist/lib/flair-agent-role.js +120 -0
- package/dist/lib/hub-url.js +12 -0
- package/dist/lib/init-admin-pass.js +88 -12
- package/dist/lib/init-listener-environ.js +1 -2
- package/dist/lib/init-occupied-listener.js +15 -111
- package/dist/lib/init-spawn-attribution.js +73 -0
- package/dist/lib/init-tcp-probe.js +33 -0
- package/dist/lib/instance-identity-row.js +20 -0
- package/dist/lib/launchd-management.js +27 -2
- package/dist/lib/listener-table.js +268 -0
- package/dist/lib/local-delete-instance.js +27 -0
- package/dist/lib/local-instance-pair.js +256 -0
- package/dist/lib/mcp-enable.js +1775 -338
- package/dist/lib/mcp-oauth-env-core.js +133 -0
- package/dist/lib/mcp-oauth-env.js +125 -0
- package/dist/lib/mcp-spec.js +17 -1
- package/dist/lib/memory-attributes.js +2 -0
- package/dist/lib/memory-id-policy.js +46 -0
- package/dist/lib/memory-integrity.js +309 -0
- package/dist/lib/memory-purge-response.js +20 -0
- package/dist/lib/ops-table-count.js +25 -0
- package/dist/lib/owned-pins.js +67 -10
- package/dist/lib/peer-status.js +27 -0
- package/dist/lib/principal-status.js +29 -0
- package/dist/lib/process-start-time.js +103 -7
- package/dist/lib/proto-safe-record.js +42 -0
- package/dist/lib/record-id-path.js +25 -0
- package/dist/lib/reembed-server-support.js +66 -0
- package/dist/lib/restore-verify.js +104 -0
- package/dist/lib/scheduler-platform.js +10 -6
- package/dist/lib/secret-keys.js +25 -0
- package/dist/lib/secrets-push.js +50 -22
- package/dist/lib/skill-seed-pending.js +24 -0
- package/dist/lib/skill-seed.js +365 -0
- package/dist/lib/stop-start-recovery.js +54 -0
- package/dist/lib/using-flair-skill.js +87 -0
- package/dist/mcp-client-assertion.js +112 -4
- package/dist/memory-id-checkpoint.js +326 -0
- package/dist/memory-node-compare.js +207 -0
- package/dist/packages/flair-mcp/src/secret-redaction.js +113 -0
- package/dist/rem/restore.js +6 -5
- package/dist/rem/runner.js +103 -44
- package/dist/resources/Agent.js +348 -8
- package/dist/resources/AgentKeyRegister.js +214 -0
- package/dist/resources/AgentKeyRegistration.js +28 -0
- package/dist/resources/AgentSeed.js +19 -4
- package/dist/resources/AutoPromoteCandidates.js +7 -1
- package/dist/resources/Credential.js +177 -2
- package/dist/resources/Federation.js +752 -132
- package/dist/resources/FederationRetry.js +140 -0
- package/dist/resources/InstructionVersion.js +220 -0
- package/dist/resources/Integration.js +300 -25
- package/dist/resources/Memory.js +2113 -604
- package/dist/resources/MemoryArchive.js +75 -42
- package/dist/resources/MemoryBootstrap.js +227 -64
- package/dist/resources/MemoryCandidate.js +21 -1
- package/dist/resources/MemoryDedupStats.js +30 -1
- package/dist/resources/MemoryFeed.js +327 -45
- package/dist/resources/MemoryGrant.js +21 -1
- package/dist/resources/MemoryMaintenance.js +129 -25
- package/dist/resources/MemoryPurge.js +232 -0
- package/dist/resources/MemoryReflect.js +7 -2
- package/dist/resources/MemoryReindex.js +24 -8
- package/dist/resources/Message.js +5 -3
- package/dist/resources/OAuth.js +186 -9
- package/dist/resources/OrgEvent.js +126 -3
- package/dist/resources/OrgSkillAssignment.js +209 -0
- package/dist/resources/Presence.js +144 -49
- package/dist/resources/PromoteMemoryCandidate.js +18 -7
- package/dist/resources/Relationship.js +23 -0
- package/dist/resources/SemanticSearch.js +63 -32
- package/dist/resources/Soul.js +189 -76
- package/dist/resources/TeamDirectory.js +35 -0
- package/dist/resources/WorkspaceState.js +21 -1
- package/dist/resources/XAA.js +212 -65
- package/dist/resources/agent-auth.js +40 -15
- package/dist/resources/agent-status-guard.js +63 -0
- package/dist/resources/auth-middleware.js +402 -162
- package/dist/resources/ed25519-auth.js +1 -1
- package/dist/resources/embedding-space-guard.js +366 -72
- package/dist/resources/engine-version-stamp-boot.js +53 -0
- package/dist/resources/federation-classify.js +94 -0
- package/dist/resources/federation-crypto.js +5 -5
- package/dist/resources/federation-retry-lib.js +135 -0
- package/dist/resources/flair-agent-role-boot.js +26 -0
- package/dist/resources/grant-visibility.js +135 -0
- package/dist/resources/harper-routing.js +83 -0
- package/dist/resources/health.js +130 -20
- package/dist/resources/host-source-citation.js +49 -0
- package/dist/resources/host-source-visibility.js +7 -6
- package/dist/resources/instance-create-lock.js +17 -0
- package/dist/resources/instance-identity.js +23 -0
- package/dist/resources/instruction-version-record.js +343 -0
- package/dist/resources/key-lock.js +31 -0
- package/dist/resources/mcp-handler.js +36 -8
- package/dist/resources/mcp-oauth-env-guard.js +36 -0
- package/dist/resources/mcp-oauth.js +26 -12
- package/dist/resources/mcp-tool-arguments.js +4 -6
- package/dist/resources/mcp-tools.js +150 -23
- package/dist/resources/memory-bootstrap-lib.js +5 -15
- package/dist/resources/memory-declared-attributes.js +3 -1
- package/dist/resources/memory-deletion-history.js +55 -0
- package/dist/resources/memory-durability.js +54 -0
- package/dist/resources/memory-embed-hash.js +88 -0
- package/dist/resources/memory-embedding-value.js +8 -0
- package/dist/resources/memory-host-source.js +27 -5
- package/dist/resources/memory-id-guard.js +75 -0
- package/dist/resources/memory-redaction.js +236 -0
- package/dist/resources/memory-supplied-vector.js +72 -0
- package/dist/resources/memory-write-lock.js +310 -0
- package/dist/resources/memory-write-response.js +139 -0
- package/dist/resources/migrations/data-dir.js +74 -16
- package/dist/resources/migrations/dir-safety.js +1 -1
- package/dist/resources/migrations/embedding-stamp.js +193 -79
- package/dist/resources/migrations/export.js +1 -0
- package/dist/resources/migrations/graph-heal.js +18 -25
- package/dist/resources/migrations/lock.js +7 -1
- package/dist/resources/migrations/snapshot.js +1 -0
- package/dist/resources/migrations/source-fields.js +5 -5
- package/dist/resources/migrations/synthetic-test-migration.js +14 -11
- package/dist/resources/migrations/visibility-backfill.js +29 -29
- package/dist/resources/multi-worker-guard.js +121 -0
- package/dist/resources/oauth-discovery.js +1 -0
- package/dist/resources/oauth-wellknown.js +8 -4
- package/dist/resources/object-body-guard.js +33 -0
- package/dist/resources/org-event-created-at.js +133 -0
- package/dist/resources/originator-instance.js +39 -7
- package/dist/resources/owner-delete-recheck.js +78 -0
- package/dist/resources/owner-field-guard.js +39 -5
- package/dist/resources/owner-scoped-write-policy.js +212 -0
- package/dist/resources/owner-scoped-writes.js +58 -0
- package/dist/resources/presence-receipt.js +236 -0
- package/dist/resources/promotion-stamp.js +47 -12
- package/dist/resources/record-owner-guard.js +74 -26
- package/dist/resources/record-types.js +17 -1
- package/dist/resources/relay-ops.js +78 -6
- package/dist/resources/replay-store.js +145 -21
- package/dist/resources/request-target.js +136 -0
- package/dist/resources/request-transaction.js +35 -0
- package/dist/resources/search-readiness.js +4 -1
- package/dist/resources/seed-ids.js +9 -0
- package/dist/resources/seed-reservation.js +117 -0
- package/dist/resources/semantic-retrieval-core.js +20 -9
- package/dist/resources/skill-manifest.js +190 -0
- package/dist/resources/skill-provenance.js +15 -10
- package/dist/resources/skill-subject.js +53 -0
- package/dist/resources/skill-version-write.js +432 -0
- package/dist/resources/skill-write-fault.js +17 -0
- package/dist/resources/skill-write-policy.js +67 -0
- package/dist/resources/skill-write.js +67 -7
- package/dist/resources/table-helpers.js +10 -7
- package/dist/resources/team-directory.js +248 -0
- package/dist/resources/tool-descriptors/index.js +69 -8
- package/dist/resources/txn-pause-point.js +118 -0
- package/dist/resources/usage-recording.js +171 -63
- package/dist/resources/write-back.js +218 -0
- package/dist/src/engine-version-contract.js +1 -0
- package/dist/src/keystore.js +72 -0
- package/dist/src/lib/agent-home-fields.js +132 -0
- package/dist/src/lib/agent-id-rule.js +46 -0
- package/dist/src/lib/agent-key-register.js +80 -0
- package/dist/src/lib/atomic-write.js +256 -0
- package/dist/src/lib/credential-status-rule.js +78 -0
- package/dist/src/lib/expired-by-agent.js +37 -0
- package/dist/src/lib/flair-agent-role.js +120 -0
- package/dist/src/lib/idp-mapping-rule.js +81 -0
- package/dist/src/lib/instance-identity-row.js +20 -0
- package/dist/src/lib/mcp-oauth-env-core.js +133 -0
- package/dist/src/lib/memory-attributes.js +2 -0
- package/dist/src/lib/memory-id-policy.js +46 -0
- package/dist/src/lib/principal-status.js +29 -0
- package/dist/src/lib/proto-safe-record.js +42 -0
- package/dist/src/lib/reembed-server-support.js +66 -0
- package/dist/src/lib/scheduler-platform.js +10 -6
- package/dist/src/lib/systemd-agent-id.js +38 -0
- package/docs/access-control.md +768 -0
- package/docs/api-reference.md +52 -13
- package/docs/auth.md +10 -12
- package/docs/bridges.md +11 -0
- package/docs/claude-code.md +34 -4
- package/docs/deepseek-harness.md +1 -1
- package/docs/deploying-on-fabric.md +70 -0
- package/docs/deployment.md +2 -0
- package/docs/embedding-in-a-harper-app.md +1 -1
- package/docs/federation.md +33 -2
- package/docs/hosted-on-fabric.md +43 -36
- package/docs/integrations.md +6 -6
- package/docs/mcp-clients.md +10 -5
- package/docs/n8n.md +17 -11
- package/docs/notes/mcp-agent-auth-consumer.md +12 -4
- package/docs/notes/mcp-oauth-model2.md +34 -5
- package/docs/quickstart-fabric.md +2 -2
- package/docs/quickstart.md +9 -1
- package/docs/releasing.md +34 -28
- package/docs/rem.md +8 -0
- package/docs/secrets-and-keys.md +19 -2
- package/docs/standalone-local.md +1 -0
- package/docs/supply-chain-policy.md +13 -3
- package/docs/upgrade.md +32 -6
- package/package.json +12 -6
- package/schemas/agent-key-registration.graphql +22 -0
- package/schemas/agent.graphql +3 -0
- package/schemas/federation.graphql +38 -1
- package/schemas/memory.graphql +115 -5
- package/schemas/oauth.graphql +27 -4
- package/schemas/schema.graphql +2 -0
- package/templates/launchd/start-flair-with-admin-pass.sh +89 -16
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* request-target.ts — the raw request target the auth middleware decides on.
|
|
3
|
+
*
|
|
4
|
+
* Before any other request processing, the middleware answers a target that is not in
|
|
5
|
+
* canonical form with a named 400 (`non_canonical_request_target`). A raw
|
|
6
|
+
* target is canonical unless its path (the part before the first `?`) has:
|
|
7
|
+
*
|
|
8
|
+
* - a `.` or `..` segment, raw or percent-encoded as a whole segment
|
|
9
|
+
* (`%2e`, `%2E%2e`, `.%2e`, …);
|
|
10
|
+
* - repeated slashes (`//`) anywhere in the path;
|
|
11
|
+
* - a raw backslash.
|
|
12
|
+
*
|
|
13
|
+
* Everything else is canonical, including `%2F` and `%5C` inside a segment, a
|
|
14
|
+
* dot inside a segment (`a.b`), and any query string.
|
|
15
|
+
*
|
|
16
|
+
* PURE: string logic only, imports nothing.
|
|
17
|
+
*/
|
|
18
|
+
/** The named error a non-canonical request target is refused with. */
|
|
19
|
+
export const NON_CANONICAL_REQUEST_TARGET_ERROR = "non_canonical_request_target";
|
|
20
|
+
/** A whole segment that is `.` or `..`, each dot raw or percent-encoded. */
|
|
21
|
+
const DOT_SEGMENT = /^(?:\.|%2e){1,2}$/i;
|
|
22
|
+
/**
|
|
23
|
+
* The path part of a raw request target: everything before the first `?`.
|
|
24
|
+
*/
|
|
25
|
+
export function requestPath(rawTarget) {
|
|
26
|
+
const q = rawTarget.indexOf("?");
|
|
27
|
+
return q === -1 ? rawTarget : rawTarget.slice(0, q);
|
|
28
|
+
}
|
|
29
|
+
/** True when a raw request target is in canonical form (see the module header). */
|
|
30
|
+
export function isCanonicalRequestTarget(rawTarget) {
|
|
31
|
+
const path = requestPath(rawTarget);
|
|
32
|
+
if (path.includes("\\"))
|
|
33
|
+
return false;
|
|
34
|
+
if (path.includes("//"))
|
|
35
|
+
return false;
|
|
36
|
+
return !path.split("/").some((segment) => DOT_SEGMENT.test(segment));
|
|
37
|
+
}
|
|
38
|
+
/** The named 400 for a non-canonical request target. HEAD gets no body. */
|
|
39
|
+
export function nonCanonicalRequestTargetRefusal(method) {
|
|
40
|
+
const body = JSON.stringify({
|
|
41
|
+
error: NON_CANONICAL_REQUEST_TARGET_ERROR,
|
|
42
|
+
message: "the request target must be in canonical form: no '.' or '..' path segment, no repeated slashes (`//`), and no backslash",
|
|
43
|
+
});
|
|
44
|
+
return new Response(method === "HEAD" ? null : body, {
|
|
45
|
+
status: 400,
|
|
46
|
+
headers: { "content-type": "application/json" },
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The raw path's segments, split on `/` FIRST and still percent-encoded.
|
|
51
|
+
* Empty segments are dropped.
|
|
52
|
+
*/
|
|
53
|
+
export function rawPathSegments(path) {
|
|
54
|
+
return path.split("/").filter(Boolean);
|
|
55
|
+
}
|
|
56
|
+
/** The last raw segment of a path (still percent-encoded). */
|
|
57
|
+
export function lastRawPathSegment(path) {
|
|
58
|
+
return path.slice(path.lastIndexOf("/") + 1);
|
|
59
|
+
}
|
|
60
|
+
/** Decode one raw path segment; null when it is not valid percent-encoding. */
|
|
61
|
+
export function decodePathSegment(segment) {
|
|
62
|
+
try {
|
|
63
|
+
return decodeURIComponent(segment);
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* For a plain one-level resource path, return the first segment and the
|
|
71
|
+
* still-encoded remainder after it, removing one leading slash.
|
|
72
|
+
*/
|
|
73
|
+
export function routedResourceAndRemainder(path) {
|
|
74
|
+
const p = path.startsWith("/") ? path.slice(1) : path;
|
|
75
|
+
const slash = p.indexOf("/");
|
|
76
|
+
if (slash === -1)
|
|
77
|
+
return { resource: p, remainder: "" };
|
|
78
|
+
return { resource: p.slice(0, slash), remainder: p.slice(slash + 1) };
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Return the resource segment and fully decoded remainder (the initial
|
|
82
|
+
* RequestTarget id for a plain one-level resource path). `parsePath` can change
|
|
83
|
+
* the final row id. Null for an empty, trailing-slash or malformed remainder.
|
|
84
|
+
*/
|
|
85
|
+
export function routedRecordId(path) {
|
|
86
|
+
const { resource, remainder } = routedResourceAndRemainder(path);
|
|
87
|
+
if (remainder === "" || remainder.endsWith("/"))
|
|
88
|
+
return null;
|
|
89
|
+
let id;
|
|
90
|
+
try {
|
|
91
|
+
id = decodeURIComponent(remainder);
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
if (id === "")
|
|
97
|
+
return null;
|
|
98
|
+
return { resource, id };
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Return the prefix before the first dot when its suffix is in
|
|
102
|
+
* `declaredAttributes`; otherwise return the id unchanged.
|
|
103
|
+
*/
|
|
104
|
+
export function routedIdAfterPropertyRule(id, declaredAttributes) {
|
|
105
|
+
const dot = id.indexOf(".");
|
|
106
|
+
if (dot > -1 && declaredAttributes.has(id.slice(dot + 1)))
|
|
107
|
+
return id.slice(0, dot);
|
|
108
|
+
return id;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Return the first nonempty segment if `isKnown` accepts it, otherwise its
|
|
112
|
+
* prefix before the first dot if accepted, otherwise the segment unchanged.
|
|
113
|
+
*/
|
|
114
|
+
export function routedFirstResourceName(path, isKnown) {
|
|
115
|
+
const seg = rawPathSegments(path)[0] ?? "";
|
|
116
|
+
if (isKnown(seg))
|
|
117
|
+
return seg;
|
|
118
|
+
const dot = seg.indexOf(".");
|
|
119
|
+
if (dot > -1 && isKnown(seg.slice(0, dot)))
|
|
120
|
+
return seg.slice(0, dot);
|
|
121
|
+
return seg;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* For a path with one leading slash, return it with the first segment replaced
|
|
125
|
+
* by `routedFirstResourceName`; unchanged when that name does not change.
|
|
126
|
+
*/
|
|
127
|
+
export function routedResourcePath(path, known) {
|
|
128
|
+
const segs = rawPathSegments(path);
|
|
129
|
+
const first = segs[0];
|
|
130
|
+
if (first === undefined)
|
|
131
|
+
return path;
|
|
132
|
+
const name = routedFirstResourceName(path, (n) => known.has(n));
|
|
133
|
+
if (name === first)
|
|
134
|
+
return path;
|
|
135
|
+
return `/${name}${path.slice(1 + first.length)}`;
|
|
136
|
+
}
|
|
@@ -76,3 +76,38 @@ export async function withOwnedTransaction(ctx, fn) {
|
|
|
76
76
|
ctx.transaction = saved;
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
|
+
/** Marks a context whose OPEN write transaction belongs to an in-process
|
|
80
|
+
* caller that composes a Memory write into it. A Symbol, so no request body
|
|
81
|
+
* or header can set it. */
|
|
82
|
+
const COMPOSED_WRITE = Symbol("flair.composedMemoryWrite");
|
|
83
|
+
/**
|
|
84
|
+
* Run `fn` in a shared write transaction ({@link withSharedWriteTransaction})
|
|
85
|
+
* and mark `shared` for the duration, so `Memory.put()` JOINS this transaction
|
|
86
|
+
* instead of owning one (flair#2478 slice 1, part 1). The caller owns the
|
|
87
|
+
* commit: its own writes in `fn` commit or abort with the Memory row. Manual
|
|
88
|
+
* promotion uses it, so its Memory write and verdict stamp stay one
|
|
89
|
+
* transaction (flair#2354). The per-id write lock orders the put's read and
|
|
90
|
+
* decision against other owner PUT/PATCH writers of that id, but this
|
|
91
|
+
* transaction commits after the lock is released, so its commit is not
|
|
92
|
+
* serialized with theirs; writers that take no lock are not serialized at all.
|
|
93
|
+
*/
|
|
94
|
+
export async function withComposedWriteTransaction(ctx, fn) {
|
|
95
|
+
return withSharedWriteTransaction(ctx, async (shared) => {
|
|
96
|
+
const had = Object.prototype.hasOwnProperty.call(shared, COMPOSED_WRITE);
|
|
97
|
+
const prior = shared[COMPOSED_WRITE];
|
|
98
|
+
shared[COMPOSED_WRITE] = true;
|
|
99
|
+
try {
|
|
100
|
+
return await fn(shared);
|
|
101
|
+
}
|
|
102
|
+
finally {
|
|
103
|
+
if (had)
|
|
104
|
+
shared[COMPOSED_WRITE] = prior;
|
|
105
|
+
else
|
|
106
|
+
delete shared[COMPOSED_WRITE];
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
/** Is `ctx` inside {@link withComposedWriteTransaction} with its transaction still open? */
|
|
111
|
+
export function composesCallerWrite(ctx) {
|
|
112
|
+
return !!ctx && ctx[COMPOSED_WRITE] === true && isJoinableTransaction(ctx);
|
|
113
|
+
}
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
* search) is what clears `empty`.
|
|
29
29
|
*/
|
|
30
30
|
import { bm25SearchLagReason } from "./bm25-status.js";
|
|
31
|
+
import { MEMORY_REEMBED_PATCH_CAPABILITY } from "../src/lib/reembed-server-support.js";
|
|
31
32
|
/**
|
|
32
33
|
* How a ready result was verified. Constant strings — no interpolation
|
|
33
34
|
* (flair#1411). Public /Health still omits this field when searchReady is
|
|
@@ -117,8 +118,10 @@ export function buildPublicHealthBody(readiness, identity) {
|
|
|
117
118
|
version: identity.version,
|
|
118
119
|
buildCommit: identity.buildCommit,
|
|
119
120
|
searchReady: readiness.searchReady,
|
|
121
|
+
// flair#2337: this build advertises the re-embed PATCH token.
|
|
122
|
+
// The shared constant aligns this build's CLI and server literals.
|
|
123
|
+
capabilities: [MEMORY_REEMBED_PATCH_CAPABILITY],
|
|
120
124
|
};
|
|
121
|
-
// Public shape is unchanged: searchReadyReason is still present iff !searchReady.
|
|
122
125
|
// Ready-path verification constants stay on the decision object (flair#1411).
|
|
123
126
|
if (!readiness.searchReady && readiness.searchReadyReason) {
|
|
124
127
|
body.searchReadyReason = readiness.searchReadyReason;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* seed-ids.ts — the `using-flair` seed's fixed skill row id (flair#2141 S2).
|
|
3
|
+
*
|
|
4
|
+
* Pure, with no runtime import, so a unit test can read it without a Harper
|
|
5
|
+
* instance. resources/seed-reservation.ts reserves it; src/lib/skill-seed.ts
|
|
6
|
+
* writes it (its `SEED_SKILL_ID` must equal this value, and
|
|
7
|
+
* test/unit/skill-seed.test.ts pins that).
|
|
8
|
+
*/
|
|
9
|
+
export const SEED_SKILL_ROW_ID = "skill:using-flair";
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* seed-reservation.ts — reserve the `using-flair` seed's fixed ids for the
|
|
3
|
+
* operator source (flair#2141 S2).
|
|
4
|
+
*
|
|
5
|
+
* Normal `flair init` seeds one skill row at a FIXED Memory id and one org
|
|
6
|
+
* assignment at a FIXED OrgSkillAssignment id that points at it. Re-initializing
|
|
7
|
+
* an already-installed default local instance with `flair init --skip-start`
|
|
8
|
+
* defers the seed until a later `flair start` that has the admin credential.
|
|
9
|
+
* Bootstrap may list the
|
|
10
|
+
* assignment for an active agent when it wins same-name conflict resolution,
|
|
11
|
+
* the agent has not opted out, and the entry fits its budget. The row behind
|
|
12
|
+
* that id must be the operator's. The rule:
|
|
13
|
+
*
|
|
14
|
+
* A create, PUT, PATCH or DELETE of the reserved Memory id needs the operator
|
|
15
|
+
* source: an authenticated Basic administrator, or a deliberate internal call
|
|
16
|
+
* (`soulWriteSource`, the same predicate that gates every OrgSkillAssignment
|
|
17
|
+
* write). Every other caller is refused, an agent key whose id equals the
|
|
18
|
+
* administrator's username and an admin agent key included.
|
|
19
|
+
*
|
|
20
|
+
* The decision reads the auth verdict and the request's credential class. It
|
|
21
|
+
* never compares the stored owner with the caller's id: an agent can hold the
|
|
22
|
+
* id the operator's rows are attributed to.
|
|
23
|
+
*
|
|
24
|
+
* The assignment id needs no entry here: every OrgSkillAssignment write
|
|
25
|
+
* already requires the operator source (resources/OrgSkillAssignment.ts), and
|
|
26
|
+
* test/unit/org-skill-assignment-writer-coverage.test.ts pins its writers.
|
|
27
|
+
*
|
|
28
|
+
* Where it applies: Memory post/put/patch/delete, the supersede target, and
|
|
29
|
+
* FeedMemories check caller-chosen ids before writing; federation skips a
|
|
30
|
+
* reserved id (`seed_id_not_federated`). Server bookkeeping is also allowed:
|
|
31
|
+
* lastReflected and embedding backfill update their selected fields. Usage
|
|
32
|
+
* recording intends to change only usageCount, but re-PUTs the stored row and
|
|
33
|
+
* can receive an agent-supplied id. The coverage test classifies raw Memory
|
|
34
|
+
* write sites detected by its supported source patterns; it does not prove
|
|
35
|
+
* that every possible write path is classified.
|
|
36
|
+
*/
|
|
37
|
+
import { resolveAgentAuth } from "./agent-auth.js";
|
|
38
|
+
import { FORBIDDEN, UNAUTH } from "./record-type-kit.js";
|
|
39
|
+
import { soulWriteSource } from "./soul-write-policy.js";
|
|
40
|
+
import { SEED_SKILL_ROW_ID } from "./seed-ids.js";
|
|
41
|
+
export { SEED_SKILL_ROW_ID };
|
|
42
|
+
/** Table → ids reserved for the operator source, subject to documented bookkeeping writes. */
|
|
43
|
+
export const RESERVED_SEED_IDS = Object.freeze({
|
|
44
|
+
Memory: Object.freeze([SEED_SKILL_ROW_ID]),
|
|
45
|
+
});
|
|
46
|
+
/** Whether `id` is a reserved seed id on `table`. */
|
|
47
|
+
export function isReservedSeedId(table, id) {
|
|
48
|
+
if (typeof id !== "string" && typeof id !== "number")
|
|
49
|
+
return false;
|
|
50
|
+
return (RESERVED_SEED_IDS[table] ?? []).includes(String(id));
|
|
51
|
+
}
|
|
52
|
+
/** The ids a resource write names: the URL-bound id and any body id (one record or an array). */
|
|
53
|
+
export function writeTargetIds(resource, content) {
|
|
54
|
+
const ids = [];
|
|
55
|
+
try {
|
|
56
|
+
const getId = resource?.getId;
|
|
57
|
+
if (typeof getId === "function")
|
|
58
|
+
ids.push(getId.call(resource));
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
/* no bound id */
|
|
62
|
+
}
|
|
63
|
+
const records = Array.isArray(content) ? content : [content];
|
|
64
|
+
for (const record of records) {
|
|
65
|
+
if (record && typeof record === "object" && "id" in record)
|
|
66
|
+
ids.push(record.id);
|
|
67
|
+
}
|
|
68
|
+
return ids;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The decision: null when no id is reserved or the caller has operator
|
|
72
|
+
* authority; otherwise 401 (anonymous) or 403.
|
|
73
|
+
*/
|
|
74
|
+
export function reservedSeedWriteDenial(table, ids, context, auth) {
|
|
75
|
+
const reserved = ids.find((id) => isReservedSeedId(table, id));
|
|
76
|
+
if (reserved === undefined)
|
|
77
|
+
return null;
|
|
78
|
+
if (auth.kind === "anonymous")
|
|
79
|
+
return UNAUTH();
|
|
80
|
+
if (soulWriteSource(context, auth))
|
|
81
|
+
return null;
|
|
82
|
+
return FORBIDDEN(`seed_id_reserved: ${table} ${JSON.stringify(String(reserved))} requires the operator source ` +
|
|
83
|
+
"(an authenticated Basic administrator or deliberate internal call)");
|
|
84
|
+
}
|
|
85
|
+
/** `reservedSeedWriteDenial`, resolving the auth verdict only when an id is reserved. */
|
|
86
|
+
export async function refuseReservedSeedWrite(table, ids, context) {
|
|
87
|
+
if (!ids.some((id) => isReservedSeedId(table, id)))
|
|
88
|
+
return null;
|
|
89
|
+
return reservedSeedWriteDenial(table, ids, context, await resolveAgentAuth(context));
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The LINEAGE form of the reservation (flair#2139 S2). A skill update SUPERSEDES
|
|
93
|
+
* into a fresh physical Memory row that carries the stable logical
|
|
94
|
+
* `skillSubjectId` forward, so reserving the seed's physical id alone would let
|
|
95
|
+
* another caller enter the seed's lineage through a successor row whose own id
|
|
96
|
+
* is not the reserved one. This denies a write whose skill SUBJECT is reserved
|
|
97
|
+
* unless the caller has the operator source, exactly as the physical-id check
|
|
98
|
+
* does. `subjectIds` are the resolved logical subject ids the write targets.
|
|
99
|
+
*/
|
|
100
|
+
export function reservedSeedSubjectDenial(table, subjectIds, context, auth) {
|
|
101
|
+
const reserved = subjectIds.find((id) => isReservedSeedId(table, id));
|
|
102
|
+
if (reserved === undefined)
|
|
103
|
+
return null;
|
|
104
|
+
if (auth.kind === "anonymous")
|
|
105
|
+
return UNAUTH();
|
|
106
|
+
if (soulWriteSource(context, auth))
|
|
107
|
+
return null;
|
|
108
|
+
return FORBIDDEN(`seed_subject_reserved: the ${table} ${JSON.stringify(String(reserved))} lineage requires the operator source ` +
|
|
109
|
+
"(an authenticated Basic administrator or deliberate internal call)");
|
|
110
|
+
}
|
|
111
|
+
export function reservedSeedFeedWriteDenial(table, ids) {
|
|
112
|
+
const reserved = ids.find((id) => id != null && isReservedSeedId(table, String(id)));
|
|
113
|
+
if (reserved === undefined)
|
|
114
|
+
return null;
|
|
115
|
+
return FORBIDDEN(`seed_id_reserved: ${table} ${JSON.stringify(String(reserved))} is written by the flair init seed; ` +
|
|
116
|
+
"the feed ingest does not write it");
|
|
117
|
+
}
|
|
@@ -63,6 +63,7 @@ import { buildBM25, fuseRrfNormalized, SEM_LIMIT } from "./bm25.js";
|
|
|
63
63
|
import { isAllowedBm25Candidate } from "./bm25-filter.js";
|
|
64
64
|
import { indexedBm25Ids } from "./bm25-index-service.js";
|
|
65
65
|
import { byRecencyThenId } from "./sort-comparators.js";
|
|
66
|
+
import { isTrustedVectorStamp, trustedVectorCondition } from "./embedding-space-guard.js";
|
|
66
67
|
/**
|
|
67
68
|
* The only way a row enters the ranked pool. `_rank` is a required
|
|
68
69
|
* argument — not an object property that `...any` can swallow — so a
|
|
@@ -131,8 +132,14 @@ export async function retrieveCandidates(params) {
|
|
|
131
132
|
select: hnswSelect,
|
|
132
133
|
limit,
|
|
133
134
|
};
|
|
134
|
-
|
|
135
|
-
|
|
135
|
+
// flair#2573 slice 1: defence in depth on the vector leg. The guard's
|
|
136
|
+
// verdict is the load-bearing exclusion — a non-uniform verdict makes the
|
|
137
|
+
// caller drop the query embedding. This condition is added to the HNSW
|
|
138
|
+
// query alone — never to `conditions`, which the lexical leg shares (a row
|
|
139
|
+
// with an untrusted stamp remains eligible for lexical matching). A query
|
|
140
|
+
// condition cannot keep a vector out of Harper's HNSW traversal; it only
|
|
141
|
+
// excludes the row from the returned set.
|
|
142
|
+
semQuery.conditions = [...conditions, trustedVectorCondition()];
|
|
136
143
|
const semResults = withDetachedTxn(ctx, () => databases.flair.Memory.search(semQuery));
|
|
137
144
|
for await (const record of semResults) {
|
|
138
145
|
if (record.expiresAt && Date.parse(record.expiresAt) < Date.now())
|
|
@@ -269,7 +276,8 @@ export async function retrieveCandidates(params) {
|
|
|
269
276
|
if (key in full)
|
|
270
277
|
projected[key] = full[key];
|
|
271
278
|
allowedById.set(id, projected);
|
|
272
|
-
|
|
279
|
+
// A trusted stamp permits cosine scoring; an absent vector scores zero.
|
|
280
|
+
if (qEmb && isTrustedVectorStamp(full.embeddingModel)) {
|
|
273
281
|
const storedEmbedding = Array.isArray(full.embedding) ? full.embedding : [];
|
|
274
282
|
semSimById.set(id, cosineSimilarity(qEmb, storedEmbedding));
|
|
275
283
|
}
|
|
@@ -328,9 +336,12 @@ export async function retrieveCandidates(params) {
|
|
|
328
336
|
let semSim = semSimById.get(id);
|
|
329
337
|
if (semSim === undefined && qEmb) {
|
|
330
338
|
const full = await withDetachedTxn(ctx, () => databases.flair.Memory.get(id));
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
339
|
+
// A trusted stamp permits cosine scoring; an absent vector scores zero.
|
|
340
|
+
if (isTrustedVectorStamp(full?.embeddingModel)) {
|
|
341
|
+
const storedEmbedding = Array.isArray(full?.embedding) ? full.embedding : [];
|
|
342
|
+
semSim = cosineSimilarity(qEmb, storedEmbedding);
|
|
343
|
+
semSimById.set(id, semSim);
|
|
344
|
+
}
|
|
334
345
|
}
|
|
335
346
|
let keywordHit = false;
|
|
336
347
|
if (q && String(record.content || "").toLowerCase().includes(String(q).toLowerCase())) {
|
|
@@ -463,10 +474,10 @@ export async function retrieveCandidates(params) {
|
|
|
463
474
|
sort: { attribute: "embedding", target: qEmb, distance: "cosine" },
|
|
464
475
|
select: hnswSelect,
|
|
465
476
|
limit,
|
|
477
|
+
// flair#2573 slice 1: defence in depth on the vector leg (see the hybrid
|
|
478
|
+
// sem leg above); the guard's verdict is the load-bearing exclusion.
|
|
479
|
+
conditions: [...conditions, trustedVectorCondition()],
|
|
466
480
|
};
|
|
467
|
-
if (conditions.length > 0) {
|
|
468
|
-
query.conditions = conditions;
|
|
469
|
-
}
|
|
470
481
|
const memoryResults = withDetachedTxn(ctx, () => databases.flair.Memory.search(query));
|
|
471
482
|
for await (const record of memoryResults) {
|
|
472
483
|
if (record.expiresAt && Date.parse(record.expiresAt) < Date.now())
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* skill-manifest.ts — the structured skills manifest in every successful
|
|
3
|
+
* bootstrap response (flair#2141 S1b, S1). No Harper imports.
|
|
4
|
+
*
|
|
5
|
+
* `resolveSkillManifest` turns an agent's `skill-assignment` Soul rows and the
|
|
6
|
+
* instance's org-scope assignments (OrgSkillAssignment) into two lists:
|
|
7
|
+
* - `skills`: the winners only — `{ name, skillId, scope, priority, source }`,
|
|
8
|
+
* where `skillId` is the skill-tagged Memory row to fetch with `skill_get`.
|
|
9
|
+
* - `diagnostics`: assignments refused (a non-durable source, an
|
|
10
|
+
* equal-priority tie or a non-boolean `optOut`) or superseded, and winners
|
|
11
|
+
* whose skill row does not resolve (unresolved or ambiguous).
|
|
12
|
+
* Candidates are the agent's own assignments (scope "own") plus the org
|
|
13
|
+
* assignments (scope "org") whose names the agent is not opted out of. The
|
|
14
|
+
* priority/tie rules (`resolveActiveSkills`, skill-provenance.ts) run on the
|
|
15
|
+
* candidates; then each winner's skill row is resolved: an own name to the
|
|
16
|
+
* agent's own skill row, an org assignment to its `skillRef`.
|
|
17
|
+
*
|
|
18
|
+
* The manifest strings (`name`, `source`) come from Soul `skill-assignment`
|
|
19
|
+
* rows, and an org entry's `name` from its OrgSkillAssignment row; they reach
|
|
20
|
+
* the agent's context without SkillScan. That is safe only because no agent
|
|
21
|
+
* key can write those rows; only operator credentials and Flair's own internal
|
|
22
|
+
* paths can (resources/soul-write-policy.ts, resources/OrgSkillAssignment.ts).
|
|
23
|
+
* Widening who may write them changes this render path.
|
|
24
|
+
*/
|
|
25
|
+
import { formatBase, normalizePriority, parseSkillMetadata, resolveActiveSkills, skillSourceOf, } from "./skill-provenance.js";
|
|
26
|
+
import { isSkillWrite } from "./skill-write.js";
|
|
27
|
+
/** The Memory fields the bootstrap skill-row query selects. */
|
|
28
|
+
export const SKILL_ROW_SELECT = [
|
|
29
|
+
"id", "agentId", "visibility", "tags", "metadata", "archived", "validTo", "expiresAt", "skillSubjectId",
|
|
30
|
+
];
|
|
31
|
+
const NO_ORG = { assignments: [], rows: [], instanceId: null };
|
|
32
|
+
/**
|
|
33
|
+
* Whether a target receives org skills, from its Agent record: the record
|
|
34
|
+
* exists, `kind` is "agent" (absent counts as "agent") and `status` is
|
|
35
|
+
* "active" (absent counts as "active").
|
|
36
|
+
*/
|
|
37
|
+
export function receivesOrgSkills(agent) {
|
|
38
|
+
if (!agent || typeof agent !== "object")
|
|
39
|
+
return false;
|
|
40
|
+
const { kind, status } = agent;
|
|
41
|
+
return (kind ?? "agent") === "agent" && (status ?? "active") === "active";
|
|
42
|
+
}
|
|
43
|
+
/** A skill row's name: `metadata.name` (skill_store folds it there). */
|
|
44
|
+
export function skillNameOf(row) {
|
|
45
|
+
const name = parseSkillMetadata(row.metadata).name;
|
|
46
|
+
return typeof name === "string" && name.length > 0 ? name : undefined;
|
|
47
|
+
}
|
|
48
|
+
function isPast(value, now) {
|
|
49
|
+
return typeof value === "string" && value.length > 0 && Date.parse(value) < now;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The rows passed to resolveSkillRef: skill-tagged, readable by the target agent
|
|
53
|
+
* (`isReadable`, the read-scope predicate `skill_get` applies), not archived,
|
|
54
|
+
* and not closed (`validTo`) or expired (`expiresAt`).
|
|
55
|
+
*/
|
|
56
|
+
export function resolvableSkillRows(rows, isReadable, now = Date.now()) {
|
|
57
|
+
return rows.filter((row) => typeof row.id === "string"
|
|
58
|
+
&& isSkillWrite(row)
|
|
59
|
+
&& isReadable(row)
|
|
60
|
+
&& row.archived !== true
|
|
61
|
+
&& !isPast(row.expiresAt, now)
|
|
62
|
+
&& !isPast(row.validTo, now));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Name-only assignments resolve against the agent's own rows. The operator's
|
|
66
|
+
* shipped skill is assigned by explicit `skillRef`; it is not an implicit
|
|
67
|
+
* fallback for an agent's name-only assignment.
|
|
68
|
+
*/
|
|
69
|
+
export function seedSkillRows(_rows) {
|
|
70
|
+
return [];
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Resolve a name-only assignment to one skill row: the target agent's own row
|
|
74
|
+
* with that name. Explicit org `skillRef` assignments use resolveOrgRef.
|
|
75
|
+
* More than one row at a step is ambiguous; no row is unresolved.
|
|
76
|
+
*/
|
|
77
|
+
export function resolveSkillRef(name, rows, agentId) {
|
|
78
|
+
const named = rows.filter((row) => skillNameOf(row) === name);
|
|
79
|
+
const decide = (list, whose) => {
|
|
80
|
+
if (list.length === 1)
|
|
81
|
+
return { kind: "resolved", skillId: list[0].id };
|
|
82
|
+
if (list.length === 0)
|
|
83
|
+
return null;
|
|
84
|
+
return {
|
|
85
|
+
kind: "ambiguous",
|
|
86
|
+
reason: `${list.length} ${whose} skill rows have this name`,
|
|
87
|
+
candidates: list.map((row) => row.id).sort(),
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
return decide(named.filter((row) => row.agentId === agentId), "of the agent's own")
|
|
91
|
+
?? decide(seedSkillRows(named), "seed")
|
|
92
|
+
?? { kind: "unresolved", reason: "the agent has no live skill row with this name" };
|
|
93
|
+
}
|
|
94
|
+
function resolveOrgRef(skillRef, rows, refSubjects) {
|
|
95
|
+
if (typeof skillRef !== "string" || skillRef.length === 0) {
|
|
96
|
+
return { kind: "unresolved", reason: "the skillRef is not a live skill row this agent can read" };
|
|
97
|
+
}
|
|
98
|
+
const byId = rows.filter((row) => row.id === skillRef);
|
|
99
|
+
const matches = byId.length > 0 ? byId : rows.filter((row) => row.skillSubjectId === (refSubjects?.get(skillRef) ?? skillRef));
|
|
100
|
+
if (matches.length === 1)
|
|
101
|
+
return { kind: "resolved", skillId: matches[0].id };
|
|
102
|
+
if (matches.length === 0)
|
|
103
|
+
return { kind: "unresolved", reason: "the skillRef is not a live skill row this agent can read" };
|
|
104
|
+
return {
|
|
105
|
+
kind: "ambiguous",
|
|
106
|
+
reason: `${matches.length} live skill rows resolve this skillRef`,
|
|
107
|
+
candidates: matches.map((row) => row.id).sort(),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/** The "## Active Skills" prose line for a manifest entry. */
|
|
111
|
+
export function skillLine(entry) {
|
|
112
|
+
return formatBase(entry.name, entry.priority, entry.source ?? undefined);
|
|
113
|
+
}
|
|
114
|
+
function compareDiagnostics(a, b) {
|
|
115
|
+
return a.name.localeCompare(b.name)
|
|
116
|
+
|| a.decision.localeCompare(b.decision)
|
|
117
|
+
|| (a.source ?? "").localeCompare(b.source ?? "")
|
|
118
|
+
|| a.priority.localeCompare(b.priority)
|
|
119
|
+
|| a.reason.localeCompare(b.reason)
|
|
120
|
+
|| a.scope.localeCompare(b.scope);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* The manifest for one agent: `assignments` are its `skill-assignment` Soul
|
|
124
|
+
* rows, `rows` the skill rows its own names may resolve to (see
|
|
125
|
+
* resolvableSkillRows), `org` the org assignments it receives. A Soul row with
|
|
126
|
+
* `metadata.optOut: true` is an opt-out, never a candidate: it removes the org
|
|
127
|
+
* assignments with that name before the priority rules, when `org.instanceId`
|
|
128
|
+
* is a non-empty string and the row's `originatorInstanceId` equals it.
|
|
129
|
+
* `skills` is sorted by name; `diagnostics` by name, decision, source,
|
|
130
|
+
* priority, reason and scope.
|
|
131
|
+
*/
|
|
132
|
+
export function resolveSkillManifest(assignments, rows, agentId, org = NO_ORG) {
|
|
133
|
+
const skills = [];
|
|
134
|
+
const diagnostics = [];
|
|
135
|
+
const candidates = [];
|
|
136
|
+
const optedOut = new Set();
|
|
137
|
+
for (const assignment of assignments) {
|
|
138
|
+
const optOut = parseSkillMetadata(assignment.metadata).optOut;
|
|
139
|
+
if (optOut === undefined || optOut === false) {
|
|
140
|
+
candidates.push({ input: assignment, scope: "own" });
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
if (typeof assignment.value !== "string" || assignment.value.length === 0)
|
|
144
|
+
continue;
|
|
145
|
+
if (optOut !== true) {
|
|
146
|
+
diagnostics.push({
|
|
147
|
+
name: assignment.value, scope: "own",
|
|
148
|
+
priority: normalizePriority(assignment.priority),
|
|
149
|
+
source: skillSourceOf(assignment) ?? null,
|
|
150
|
+
decision: "refused", reason: "metadata.optOut is not a boolean",
|
|
151
|
+
});
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (typeof org.instanceId === "string" && org.instanceId !== "" && assignment.originatorInstanceId === org.instanceId) {
|
|
155
|
+
optedOut.add(assignment.value);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
for (const row of org.assignments) {
|
|
159
|
+
if (typeof row.skillName !== "string" || optedOut.has(row.skillName))
|
|
160
|
+
continue;
|
|
161
|
+
candidates.push({ input: { value: row.skillName, priority: row.priority }, scope: "org", skillRef: row.skillRef });
|
|
162
|
+
}
|
|
163
|
+
const byInput = new Map(candidates.map((candidate) => [candidate.input, candidate]));
|
|
164
|
+
for (const outcome of resolveActiveSkills(candidates.map((candidate) => candidate.input)).outcomes) {
|
|
165
|
+
const candidate = byInput.get(outcome.input);
|
|
166
|
+
const scope = candidate.scope;
|
|
167
|
+
const source = outcome.source ?? null;
|
|
168
|
+
if (!outcome.loaded) {
|
|
169
|
+
diagnostics.push({
|
|
170
|
+
name: outcome.name, scope, priority: outcome.priority, source,
|
|
171
|
+
decision: outcome.decision, reason: outcome.reason,
|
|
172
|
+
});
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const ref = scope === "org"
|
|
176
|
+
? resolveOrgRef(candidate.skillRef, org.rows, org.refSubjects)
|
|
177
|
+
: resolveSkillRef(outcome.name, rows, agentId);
|
|
178
|
+
if (ref.kind === "resolved") {
|
|
179
|
+
skills.push({ name: outcome.name, skillId: ref.skillId, scope, priority: outcome.priority, source });
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
diagnostics.push({
|
|
183
|
+
name: outcome.name, scope, priority: outcome.priority, source, decision: ref.kind, reason: ref.reason,
|
|
184
|
+
...(ref.kind === "ambiguous" ? { candidates: ref.candidates } : {}),
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
skills.sort((a, b) => a.name.localeCompare(b.name));
|
|
188
|
+
diagnostics.sort(compareDiagnostics);
|
|
189
|
+
return { skills, diagnostics };
|
|
190
|
+
}
|
|
@@ -158,6 +158,11 @@ export function refuseSkillAssignmentWrite(content, existing) {
|
|
|
158
158
|
if (assignmentKey(content, existing) !== SKILL_ASSIGNMENT_KEY)
|
|
159
159
|
return null;
|
|
160
160
|
const metadata = content?.metadata !== undefined ? content.metadata : existing?.metadata;
|
|
161
|
+
// flair#2141 S1: `metadata.optOut`, when present, is a boolean.
|
|
162
|
+
const parsed = parseSkillMetadata(metadata);
|
|
163
|
+
if (Object.hasOwn(parsed, "optOut") && typeof parsed.optOut !== "boolean") {
|
|
164
|
+
return new Response(JSON.stringify({ error: "skill_opt_out_not_boolean", message: "metadata.optOut must be true or false" }), { status: 400, headers: { "content-type": "application/json" } });
|
|
165
|
+
}
|
|
161
166
|
const result = registerSkillAssignment({
|
|
162
167
|
key: SKILL_ASSIGNMENT_KEY,
|
|
163
168
|
value: content?.value ?? existing?.value,
|
|
@@ -172,7 +177,7 @@ export function refuseSkillAssignmentWrite(content, existing) {
|
|
|
172
177
|
message: result.message,
|
|
173
178
|
}), { status: 400, headers: { "content-type": "application/json" } });
|
|
174
179
|
}
|
|
175
|
-
function normalizePriority(p) {
|
|
180
|
+
export function normalizePriority(p) {
|
|
176
181
|
if (typeof p === "string" && Object.hasOwn(PRIORITY_RANK, p))
|
|
177
182
|
return p;
|
|
178
183
|
return "standard";
|
|
@@ -180,7 +185,7 @@ function normalizePriority(p) {
|
|
|
180
185
|
function priorityRank(p) {
|
|
181
186
|
return PRIORITY_RANK[p] ?? PRIORITY_RANK.standard;
|
|
182
187
|
}
|
|
183
|
-
function formatBase(name, priority, source) {
|
|
188
|
+
export function formatBase(name, priority, source) {
|
|
184
189
|
const src = source ? `, source: ${source}` : "";
|
|
185
190
|
return `- ${name} (${priority} priority${src})`;
|
|
186
191
|
}
|
|
@@ -196,7 +201,7 @@ function compareOutcomes(a, b) {
|
|
|
196
201
|
return byPri;
|
|
197
202
|
return a.reason.localeCompare(b.reason);
|
|
198
203
|
}
|
|
199
|
-
function outcomeOf(name, priority, source, decision, reason, loaded) {
|
|
204
|
+
function outcomeOf(input, name, priority, source, decision, reason, loaded) {
|
|
200
205
|
let line = formatBase(name, priority, source);
|
|
201
206
|
if (decision === "superseded") {
|
|
202
207
|
line += ` [SKILL_CONFLICT superseded: ${reason}]`;
|
|
@@ -206,7 +211,7 @@ function outcomeOf(name, priority, source, decision, reason, loaded) {
|
|
|
206
211
|
? ` [refused: ${reason}]`
|
|
207
212
|
: ` [SKILL_CONFLICT refused: ${reason}]`;
|
|
208
213
|
}
|
|
209
|
-
return { name, priority, source, decision, reason, loaded, line };
|
|
214
|
+
return { name, priority, source, decision, reason, loaded, line, input };
|
|
210
215
|
}
|
|
211
216
|
/**
|
|
212
217
|
* Stated, deterministic Active Skills outcome.
|
|
@@ -231,10 +236,10 @@ export function resolveActiveSkills(assignments) {
|
|
|
231
236
|
const priority = normalizePriority(raw.priority);
|
|
232
237
|
const source = skillSourceOf(raw);
|
|
233
238
|
if (source && !isDurableSkillSource(source)) {
|
|
234
|
-
outcomes.push(outcomeOf(name, priority, source, "refused", `non-durable source ${source}`, false));
|
|
239
|
+
outcomes.push(outcomeOf(raw, name, priority, source, "refused", `non-durable source ${source}`, false));
|
|
235
240
|
continue;
|
|
236
241
|
}
|
|
237
|
-
prepared.push({ name, priority, source });
|
|
242
|
+
prepared.push({ input: raw, name, priority, source });
|
|
238
243
|
}
|
|
239
244
|
const byName = new Map();
|
|
240
245
|
for (const row of prepared) {
|
|
@@ -247,25 +252,25 @@ export function resolveActiveSkills(assignments) {
|
|
|
247
252
|
for (const group of byName.values()) {
|
|
248
253
|
if (group.length === 1) {
|
|
249
254
|
const only = group[0];
|
|
250
|
-
outcomes.push(outcomeOf(only.name, only.priority, only.source, "loaded", "sole", true));
|
|
255
|
+
outcomes.push(outcomeOf(only.input, only.name, only.priority, only.source, "loaded", "sole", true));
|
|
251
256
|
continue;
|
|
252
257
|
}
|
|
253
258
|
const best = Math.min(...group.map((g) => priorityRank(g.priority)));
|
|
254
259
|
const top = group.filter((g) => priorityRank(g.priority) === best);
|
|
255
260
|
if (top.length === 1) {
|
|
256
261
|
const winner = top[0];
|
|
257
|
-
outcomes.push(outcomeOf(winner.name, winner.priority, winner.source, "loaded", `unique_priority ${winner.priority}`, true));
|
|
262
|
+
outcomes.push(outcomeOf(winner.input, winner.name, winner.priority, winner.source, "loaded", `unique_priority ${winner.priority}`, true));
|
|
258
263
|
for (const loser of group) {
|
|
259
264
|
if (loser === winner)
|
|
260
265
|
continue;
|
|
261
|
-
outcomes.push(outcomeOf(loser.name, loser.priority, loser.source, "superseded", `${winner.priority} priority is stated precedence over ${loser.priority}`, false));
|
|
266
|
+
outcomes.push(outcomeOf(loser.input, loser.name, loser.priority, loser.source, "superseded", `${winner.priority} priority is stated precedence over ${loser.priority}`, false));
|
|
262
267
|
}
|
|
263
268
|
continue;
|
|
264
269
|
}
|
|
265
270
|
const sources = [...new Set(top.map((t) => t.source ?? "(none)"))].sort((a, b) => a.localeCompare(b));
|
|
266
271
|
const reason = `equal-priority tie at ${top[0].priority}; load refused (sources: ${sources.join(", ")})`;
|
|
267
272
|
for (const member of group) {
|
|
268
|
-
outcomes.push(outcomeOf(member.name, member.priority, member.source, "refused", reason, false));
|
|
273
|
+
outcomes.push(outcomeOf(member.input, member.name, member.priority, member.source, "refused", reason, false));
|
|
269
274
|
}
|
|
270
275
|
}
|
|
271
276
|
outcomes.sort(compareOutcomes);
|