@unson/brainbase-mcp 0.50.28 → 0.50.30

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.
Files changed (39) hide show
  1. package/contracts/brainbase-public-contract.v1.json +1 -0
  2. package/dist/antigravity-judgment-hook.d.ts +191 -0
  3. package/dist/antigravity-judgment-hook.js +2042 -0
  4. package/dist/claude-code-judgment-hook.d.ts +23 -0
  5. package/dist/claude-code-judgment-hook.js +8 -5
  6. package/dist/cli.js +273 -6
  7. package/dist/graph-retrieval.d.ts +4 -0
  8. package/dist/graph-retrieval.js +4 -0
  9. package/dist/judgment-event-adapter.d.ts +37 -2
  10. package/dist/judgment-event-adapter.js +88 -3
  11. package/dist/judgment-event-outbox.d.ts +9 -1
  12. package/dist/judgment-event-outbox.js +45 -8
  13. package/dist/judgment-execution-outcome.d.ts +2 -1
  14. package/dist/judgment-execution-outcome.js +2 -1
  15. package/dist/judgment-frame.d.ts +26 -4
  16. package/dist/judgment-frame.js +190 -15
  17. package/dist/judgment-host.d.ts +30 -0
  18. package/dist/judgment-host.js +170 -22
  19. package/dist/judgment-model-outcome.d.ts +29 -1
  20. package/dist/judgment-model-outcome.js +69 -1
  21. package/dist/judgment-offline-binary-audit.d.ts +360 -0
  22. package/dist/judgment-offline-binary-audit.js +555 -0
  23. package/dist/judgment-project-binding.d.ts +1 -1
  24. package/dist/judgment-project-binding.js +2 -1
  25. package/dist/judgment-reasoning-audit.d.ts +115 -0
  26. package/dist/judgment-reasoning-audit.js +623 -0
  27. package/dist/judgment-reasoning-gate-prompt.d.ts +42 -0
  28. package/dist/judgment-reasoning-gate-prompt.js +83 -0
  29. package/dist/judgment-runtime-resolution.d.ts +2 -2
  30. package/dist/judgment-runtime-resolution.js +2 -2
  31. package/dist/judgment-runtime-resolution.public.d.ts +2 -2
  32. package/dist/judgment-session-world-model.d.ts +163 -0
  33. package/dist/judgment-session-world-model.js +408 -0
  34. package/dist/mcp-tool-schema-portability.d.ts +54 -0
  35. package/dist/mcp-tool-schema-portability.js +1013 -0
  36. package/dist/organization-graph.js +9 -1
  37. package/dist/remote-judgment-hook-http.d.ts +36 -3
  38. package/dist/remote-judgment-hook-http.js +94 -15
  39. package/package.json +14 -2
@@ -121,6 +121,7 @@ export function assertPortableGraphBundle(bundle) {
121
121
  validatePortableGraph(bundle);
122
122
  }
123
123
  function parseGraphRetrievalResponse(value) {
124
+ const validOptionalCount = (candidate) => (candidate === undefined || (typeof candidate === 'number' && Number.isInteger(candidate) && candidate >= 0));
124
125
  if (!isRecord(value)
125
126
  || (value.graphVersion !== 1 && value.graphVersion !== 2)
126
127
  || (value.schemaVersion !== 1 && value.schemaVersion !== 2)
@@ -135,6 +136,8 @@ function parseGraphRetrievalResponse(value) {
135
136
  || !['semantic', 'lexical', 'seed'].includes(String(value.semantic.method))
136
137
  || !Array.isArray(value.candidates)
137
138
  || !Array.isArray(value.results)
139
+ || !validOptionalCount(value.totalScopedCount)
140
+ || !validOptionalCount(value.returnedCount)
138
141
  || !Array.isArray(value.observedRelations)
139
142
  || !Array.isArray(value.observedRelationCatalog)
140
143
  || !isRecord(value.traversal)
@@ -154,7 +157,12 @@ function parseGraphRetrievalResponse(value) {
154
157
  // This route searches only the token owner's own snapshot (the owner-private
155
158
  // area), never the organization's shared Graph. A service from before the
156
159
  // owner_private label answered organization_graph for the same area.
157
- return { ...value, authority: 'owner_private' };
160
+ // Older services that omit totalScopedCount/returnedCount leave them
161
+ // undefined rather than fabricating a denominator from returned results.
162
+ return {
163
+ ...value,
164
+ authority: 'owner_private',
165
+ };
158
166
  }
159
167
  function parseImportResult(value) {
160
168
  if (!isRecord(value)
@@ -1,4 +1,6 @@
1
1
  import type { JudgmentJournalTurnInput } from './judgment-journal-turn-publisher.js';
2
+ import type { ReasoningGateMode } from './judgment-reasoning-gate-prompt.js';
3
+ import type { SessionWorldModelUpdaterV2 } from './judgment-session-world-model.js';
2
4
  /** Minimal identity verified by the consuming host's authentication boundary. */
3
5
  export interface RemoteJudgmentHookPrincipal {
4
6
  personId: string;
@@ -12,12 +14,15 @@ export interface RemoteJudgmentHookDispatchResult {
12
14
  output: Record<string, unknown>;
13
15
  receiptId?: string;
14
16
  routeResolutionSha256?: string;
17
+ /** Optional non-digest reasoning gate telemetry surfaced by the Host dispatcher. */
18
+ reasoningGateApplied?: boolean;
19
+ reasoningGateMode?: ReasoningGateMode;
15
20
  }
16
21
  /**
17
- * `remote`: a remote runtime. `claude-code`: Claude Code on
18
- * the owner's loopback bridge or authenticated with a person's hosted JWT.
22
+ * `remote`: a remote runtime. `claude-code` / `antigravity`: interactive CLI
23
+ * hosts on the owner's loopback bridge or authenticated with a person's hosted JWT.
19
24
  */
20
- export type RemoteJudgmentHookHost = 'remote' | 'claude-code';
25
+ export type RemoteJudgmentHookHost = 'remote' | 'claude-code' | 'antigravity';
21
26
  /**
22
27
  * Authentication context verified by the Host before it reaches the hook
23
28
  * dispatcher. A shared bearer intentionally has no trusted principal or
@@ -39,6 +44,8 @@ export interface RemoteJudgmentHookDispatchOptions {
39
44
  trustedAuthKind?: RemoteJudgmentHookAuthKind;
40
45
  /** Raw encoded Company Authority response verified by the canonical API. */
41
46
  companyAuthorityResponse?: string;
47
+ /** Optional PR-B ephemeral session mini world model v2 updater forwarded to the Host. */
48
+ sessionWorldModelUpdater?: SessionWorldModelUpdaterV2;
42
49
  }
43
50
  export type RemoteJudgmentHookDispatch = (payload: Record<string, unknown>, projectCode: string | undefined, options: RemoteJudgmentHookDispatchOptions) => Promise<RemoteJudgmentHookDispatchResult>;
44
51
  export interface RemoteJudgmentHookRequest {
@@ -62,10 +69,36 @@ export interface RemoteJudgmentHookRequest {
62
69
  localClaudeCodeAllowed?: boolean;
63
70
  /** True only for a person authenticated by the hosted MCP's JWT boundary. */
64
71
  personHostedClaudeCodeAllowed?: boolean;
72
+ /**
73
+ * The x-brainbase-repository header of a person-hosted Claude Code turn.
74
+ * Caller-declared, so it is only a lookup key for `resolveProjectCode`;
75
+ * the resolved project must still be inside the verified principal scope.
76
+ */
77
+ repository?: string;
78
+ /**
79
+ * Host-owned repository-to-project mapping, consulted only when a
80
+ * person-hosted Claude Code turn sent no project code. Return undefined
81
+ * when the host has no mapping for the repository.
82
+ */
83
+ resolveProjectCode?: (input: {
84
+ repository: string;
85
+ principal: RemoteJudgmentHookPrincipal;
86
+ }) => Promise<string | undefined> | string | undefined;
65
87
  isAuthorized: (authorization: string | undefined, expectedToken: string) => boolean;
66
88
  dispatch: RemoteJudgmentHookDispatch;
67
89
  /** Optional HTTP publisher for person-authenticated hosted Claude Code Stop events. */
68
90
  journalTurnPublisher?: (input: JudgmentJournalTurnInput) => Promise<void>;
91
+ /** Optional PR-B ephemeral session mini world model v2 updater forwarded to dispatch. */
92
+ sessionWorldModelUpdater?: SessionWorldModelUpdaterV2;
93
+ /** Optional non-digest reasoning gate telemetry observer (fail-open). */
94
+ onReasoningGateTelemetry?: (details: {
95
+ eventName: string;
96
+ sessionId: string;
97
+ turnId: string;
98
+ projectCode?: string;
99
+ reasoning_gate_applied: boolean;
100
+ reasoning_gate_mode: ReasoningGateMode;
101
+ }) => void;
69
102
  onDispatchError?: (details: {
70
103
  eventName: string;
71
104
  reason: string;
@@ -29,6 +29,22 @@ function isInternalJudgmentControlTool(payload) {
29
29
  'mcp__brainbase__brainbase_judgment_audit_read',
30
30
  'mcp__brainbase__brainbase_judgment_repair_read'].includes(toolName);
31
31
  }
32
+ function invokeFailOpenObserver(observer, details) {
33
+ if (!observer)
34
+ return;
35
+ try {
36
+ const result = observer(details);
37
+ if (result !== null && (typeof result === 'object' || typeof result === 'function')
38
+ && typeof result.then === 'function') {
39
+ Promise.resolve(result).catch(() => {
40
+ /* Fail-open observer cannot affect the hook response or emit unhandled rejections. */
41
+ });
42
+ }
43
+ }
44
+ catch {
45
+ /* Fail-open observer cannot affect the hook response. */
46
+ }
47
+ }
32
48
  function safeCauseReasonCode(error) {
33
49
  let current = typeof error === 'object' && error && 'cause' in error ? error.cause : undefined;
34
50
  const visited = new Set();
@@ -40,6 +56,27 @@ function safeCauseReasonCode(error) {
40
56
  }
41
57
  return undefined;
42
58
  }
59
+ const REPOSITORY_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._/-]{0,199}$/;
60
+ async function resolveHostedProjectCode(request, principal) {
61
+ const repository = request.repository?.trim();
62
+ if (repository && REPOSITORY_PATTERN.test(repository) && request.resolveProjectCode) {
63
+ let mapped;
64
+ try {
65
+ mapped = await request.resolveProjectCode({ repository, principal });
66
+ }
67
+ catch {
68
+ // A failed lookup is not "no mapping": the repository may belong to a
69
+ // project outside the person's scope. Record nothing; the caller retries.
70
+ return { kind: 'lookup_failed' };
71
+ }
72
+ if (typeof mapped === 'string' && mapped)
73
+ return { kind: 'resolved', projectCode: mapped };
74
+ }
75
+ const projects = [...new Set(principal.projectCodes)];
76
+ return projects.length === 1
77
+ ? { kind: 'resolved', projectCode: projects[0] }
78
+ : { kind: 'unresolved' };
79
+ }
43
80
  export async function handleRemoteJudgmentHookRequest(request) {
44
81
  if (request.method !== 'POST' || request.url !== REMOTE_JUDGMENT_HOOK_PATH)
45
82
  return null;
@@ -70,28 +107,51 @@ export async function handleRemoteJudgmentHookRequest(request) {
70
107
  }
71
108
  const personHostedClaudeCode = request.personHostedClaudeCodeAllowed === true
72
109
  && request.trustedAuthKind === 'brainbase-jwt';
73
- const host = request.declaredHost === undefined || request.declaredHost === ''
74
- ? 'remote' : 'claude-code';
75
- if (host === 'claude-code'
76
- && (request.declaredHost !== 'claude-code' || (request.localClaudeCodeAllowed !== true && !personHostedClaudeCode))) {
110
+ let host;
111
+ if (request.declaredHost === undefined || request.declaredHost === '') {
112
+ host = 'remote';
113
+ }
114
+ else if (request.declaredHost === 'claude-code' || request.declaredHost === 'antigravity') {
115
+ host = request.declaredHost;
116
+ }
117
+ else {
118
+ return { status: 400, body: { error: 'judgment_hook_host_unsupported' } };
119
+ }
120
+ const isInteractiveHost = host === 'claude-code' || host === 'antigravity';
121
+ if (isInteractiveHost && (request.localClaudeCodeAllowed !== true && !personHostedClaudeCode)) {
77
122
  return { status: 400, body: { error: 'judgment_hook_host_unsupported' } };
78
123
  }
79
124
  if (host === 'remote' && !SUPPORTED_HOOK_EVENTS.has(eventName)) {
80
125
  return { status: 400, body: { error: 'unsupported_hook_event' } };
81
126
  }
82
- // A local Claude Code turn may leave the project to the Host, which derives
127
+ // A local interactive turn may leave the project to the Host, which derives
83
128
  // it from the turn's cwd exactly as for Codex.
84
- const requiresProjectScope = host === 'remote' || (host === 'claude-code' && personHostedClaudeCode);
85
- if (host === 'claude-code' && personHostedClaudeCode
129
+ const requiresProjectScope = host === 'remote' || (isInteractiveHost && personHostedClaudeCode);
130
+ if (isInteractiveHost && personHostedClaudeCode
86
131
  && !request.trustedPrincipal?.tenantId?.trim()) {
87
132
  return { status: 403, body: { error: 'authenticated_history_principal_unavailable' } };
88
133
  }
89
- if ((requiresProjectScope || request.projectCode !== undefined)
90
- && (!request.projectCode || !PROJECT_CODE_PATTERN.test(request.projectCode))) {
134
+ let projectCode = request.projectCode;
135
+ if (isInteractiveHost && personHostedClaudeCode && request.trustedPrincipal
136
+ && (projectCode === undefined || projectCode === '')) {
137
+ // A person's Claude Code need not name the project. The Host decides it:
138
+ // its own repository mapping first, then the person's only project. When
139
+ // neither decides, nothing is recorded and the caller is told why.
140
+ const resolution = await resolveHostedProjectCode(request, request.trustedPrincipal);
141
+ if (resolution.kind === 'lookup_failed') {
142
+ return { status: 503, body: { error: 'judgment_project_lookup_failed' } };
143
+ }
144
+ if (resolution.kind === 'unresolved') {
145
+ return { status: 400, body: { error: 'judgment_project_unresolved' } };
146
+ }
147
+ projectCode = resolution.projectCode;
148
+ }
149
+ if ((requiresProjectScope || projectCode !== undefined)
150
+ && (!projectCode || !PROJECT_CODE_PATTERN.test(projectCode))) {
91
151
  return { status: 400, body: { error: 'invalid_project_code' } };
92
152
  }
93
153
  if (requiresProjectScope && request.trustedPrincipal
94
- && (!request.projectCode || !request.trustedPrincipal.projectCodes.includes(request.projectCode))) {
154
+ && (!projectCode || !request.trustedPrincipal.projectCodes.includes(projectCode))) {
95
155
  return { status: 403, body: { error: 'project_not_authorized' } };
96
156
  }
97
157
  if (request.trustedPrincipal && !request.trustedPrincipal.tenantId?.trim()) {
@@ -112,13 +172,32 @@ export async function handleRemoteJudgmentHookRequest(request) {
112
172
  ...(request.companyAuthorityResponse
113
173
  ? { companyAuthorityResponse: request.companyAuthorityResponse }
114
174
  : {}),
175
+ ...(request.sessionWorldModelUpdater
176
+ ? { sessionWorldModelUpdater: request.sessionWorldModelUpdater }
177
+ : {}),
115
178
  }
116
- : { host };
117
- const dispatchResult = await request.dispatch(hookPayload, request.projectCode, dispatchOptions);
179
+ : {
180
+ host,
181
+ ...(request.sessionWorldModelUpdater
182
+ ? { sessionWorldModelUpdater: request.sessionWorldModelUpdater }
183
+ : {}),
184
+ };
185
+ const dispatchResult = await request.dispatch(hookPayload, projectCode, dispatchOptions);
118
186
  const { output } = dispatchResult;
119
187
  if (!output || typeof output !== 'object' || Array.isArray(output)) {
120
188
  return { status: 503, body: { error: 'judgment_hook_output_invalid' } };
121
189
  }
190
+ if (typeof dispatchResult.reasoningGateApplied === 'boolean'
191
+ && dispatchResult.reasoningGateMode !== undefined) {
192
+ invokeFailOpenObserver(request.onReasoningGateTelemetry, {
193
+ eventName,
194
+ sessionId,
195
+ turnId,
196
+ ...(projectCode ? { projectCode } : {}),
197
+ reasoning_gate_applied: dispatchResult.reasoningGateApplied,
198
+ reasoning_gate_mode: dispatchResult.reasoningGateMode,
199
+ });
200
+ }
122
201
  // The remote runtime's stream validator needs an audit line per tool. A
123
202
  // local Claude Code turn has no such validator; the Host decides alone.
124
203
  if (host === 'remote' && (eventName === 'PostToolUse' || eventName === 'PostToolUseFailure')
@@ -132,7 +211,7 @@ export async function handleRemoteJudgmentHookRequest(request) {
132
211
  || !/^[a-f0-9]{64}$/.test(dispatchResult.routeResolutionSha256))) {
133
212
  return { status: 503, body: { error: 'judgment_hook_route_receipt_missing' } };
134
213
  }
135
- if (eventName === 'Stop' && host === 'claude-code' && personHostedClaudeCode
214
+ if (eventName === 'Stop' && isInteractiveHost && personHostedClaudeCode
136
215
  && request.localClaudeCodeAllowed !== true) {
137
216
  const report = (reason) => {
138
217
  try {
@@ -149,7 +228,7 @@ export async function handleRemoteJudgmentHookRequest(request) {
149
228
  try {
150
229
  await Promise.race([
151
230
  Promise.resolve().then(() => request.journalTurnPublisher({
152
- sessionId, turnId, projectCode: request.projectCode, trustedToken: request.trustedToken,
231
+ sessionId, turnId, projectCode: projectCode, trustedToken: request.trustedToken,
153
232
  trustedPrincipal: { personId: request.trustedPrincipal.personId, tenantId: request.trustedPrincipal.tenantId },
154
233
  })),
155
234
  new Promise((_resolve, reject) => {
@@ -194,7 +273,7 @@ export async function handleRemoteJudgmentHookRequest(request) {
194
273
  ? error.code
195
274
  : undefined;
196
275
  const causeReasonCode = safeCauseReasonCode(error);
197
- request.onDispatchError?.({
276
+ invokeFailOpenObserver(request.onDispatchError, {
198
277
  eventName,
199
278
  reason,
200
279
  errorName: error instanceof Error ? error.name : 'UnknownError',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unson/brainbase-mcp",
3
- "version": "0.50.28",
3
+ "version": "0.50.30",
4
4
  "description": "Local-first Brainbase judgment OS that lets supported AI agents reuse a user's approved goals, priorities, decisions, and judgment criteria.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -39,6 +39,10 @@
39
39
  "types": "./dist/judgment-frame.d.ts",
40
40
  "import": "./dist/judgment-frame.js"
41
41
  },
42
+ "./judgment-reasoning-audit": {
43
+ "types": "./dist/judgment-reasoning-audit.d.ts",
44
+ "import": "./dist/judgment-reasoning-audit.js"
45
+ },
42
46
  "./judgment-history": {
43
47
  "types": "./dist/judgment-history.d.ts",
44
48
  "import": "./dist/judgment-history.js"
@@ -179,6 +183,10 @@
179
183
  "types": "./dist/claude-code-judgment-hook.d.ts",
180
184
  "import": "./dist/claude-code-judgment-hook.js"
181
185
  },
186
+ "./antigravity-judgment-hook": {
187
+ "types": "./dist/antigravity-judgment-hook.d.ts",
188
+ "import": "./dist/antigravity-judgment-hook.js"
189
+ },
182
190
  "./company-os-evaluation": {
183
191
  "types": "./dist/company-os-evaluation.d.ts",
184
192
  "import": "./dist/company-os-evaluation.js"
@@ -375,6 +383,10 @@
375
383
  "types": "./dist/mcp-runtime.d.ts",
376
384
  "import": "./dist/mcp-runtime.js"
377
385
  },
386
+ "./mcp-tool-schema-portability": {
387
+ "types": "./dist/mcp-tool-schema-portability.d.ts",
388
+ "import": "./dist/mcp-tool-schema-portability.js"
389
+ },
378
390
  "./resource-reservations": {
379
391
  "types": "./dist/resource-reservations.d.ts",
380
392
  "import": "./dist/resource-reservations.js"
@@ -669,5 +681,5 @@
669
681
  "url": "https://github.com/Unson-LLC/brainbase/issues"
670
682
  },
671
683
  "homepage": "https://github.com/Unson-LLC/brainbase#readme",
672
- "gitHead": "ca61b8110f03308e184c04ab06b6b839b828a0df"
684
+ "gitHead": "8d8facf77d44ddbae6d74b62d70037e21026aaf8"
673
685
  }