@unson/brainbase-mcp 0.51.7 → 0.51.9
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/dist/antigravity-judgment-hook.js +77 -16
- package/dist/run-receipt.d.ts +3 -1
- package/dist/run-receipt.js +45 -4
- package/package.json +2 -2
|
@@ -28,6 +28,7 @@ import { accessSync, closeSync, constants as fsConstants, fstatSync, lstatSync,
|
|
|
28
28
|
import { homedir } from 'node:os';
|
|
29
29
|
import { basename, delimiter, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
30
30
|
import { fileURLToPath } from 'node:url';
|
|
31
|
+
import { types } from 'node:util';
|
|
31
32
|
import { BrainbaseLoginError, getAccessTokenOfLogin } from './brainbase-login.js';
|
|
32
33
|
import { PRE_TOOL_USE_TOOLS, REGISTRATION_ID, bearerTransportSafe, canonicalFilePath, canonicalRepositoryIdentifier, digestSummary, forwardedStopPayload, hostCanMapRepository, resolveRepositoryScope, shellQuote, singleQuotedWords, } from './claude-code-judgment-hook.js';
|
|
33
34
|
export const ANTIGRAVITY_HOOK_CONFIG_SCHEMA = 'brainbase-antigravity-judgment-hook-v1';
|
|
@@ -50,6 +51,10 @@ const DEFAULT_STALE_TURN_MS = 30 * 60 * 1000;
|
|
|
50
51
|
const MAX_CONFIG_TIMEOUT_MS = 60_000;
|
|
51
52
|
const TURN_STATE_LOCK_TIMEOUT_MS = 2_000;
|
|
52
53
|
const FLOCK_CONFLICT_EXIT_CODE = 75;
|
|
54
|
+
const START_ERRORS = [
|
|
55
|
+
'timeout', 'http_5xx', 'login_missing', 'login_expired', 'login_origin_mismatch',
|
|
56
|
+
'login_unavailable', 'transport', 'unknown',
|
|
57
|
+
];
|
|
53
58
|
function sha256(text) {
|
|
54
59
|
return createHash('sha256').update(text).digest('hex');
|
|
55
60
|
}
|
|
@@ -2343,6 +2348,9 @@ function readState(env, key) {
|
|
|
2343
2348
|
lastClosedUserStepIndex = maxStepIndex(lastClosedUserStepIndex, currentTurnUserStepIndex);
|
|
2344
2349
|
currentTurnUserStepIndex = null;
|
|
2345
2350
|
}
|
|
2351
|
+
const lastStartError = parsed.start_status === 'failed'
|
|
2352
|
+
? START_ERRORS.find((code) => code === parsed.last_start_error)
|
|
2353
|
+
: undefined;
|
|
2346
2354
|
return {
|
|
2347
2355
|
schema_version: TURN_SCHEMA,
|
|
2348
2356
|
turn_id: parsed.turn_id,
|
|
@@ -2355,6 +2363,7 @@ function readState(env, key) {
|
|
|
2355
2363
|
last_prompt_digest: typeof parsed.last_prompt_digest === 'string' ? parsed.last_prompt_digest : null,
|
|
2356
2364
|
start_status: parsed.start_status === 'started' || parsed.start_status === 'out_of_scope'
|
|
2357
2365
|
? parsed.start_status : 'failed',
|
|
2366
|
+
...(lastStartError && { last_start_error: lastStartError }),
|
|
2358
2367
|
in_git: parsed.in_git === true,
|
|
2359
2368
|
repository: typeof parsed.repository === 'string' ? parsed.repository : null,
|
|
2360
2369
|
checkout: typeof parsed.checkout === 'string' ? parsed.checkout : null,
|
|
@@ -2399,6 +2408,39 @@ function timeoutForEvent(config, antigravityEvent) {
|
|
|
2399
2408
|
function turnLogin(state) {
|
|
2400
2409
|
return { expected: typeof state.login === 'string' && state.login ? state.login : '\u0000unrecorded' };
|
|
2401
2410
|
}
|
|
2411
|
+
// Only errors created at the forwarding boundaries carry a diagnostic. Looking
|
|
2412
|
+
// up their identity avoids reading properties from an arbitrary thrown value.
|
|
2413
|
+
const forwardErrors = new WeakMap();
|
|
2414
|
+
function forwardError(code) {
|
|
2415
|
+
const error = new Error(code);
|
|
2416
|
+
forwardErrors.set(error, code);
|
|
2417
|
+
return error;
|
|
2418
|
+
}
|
|
2419
|
+
function loginStartError(error) {
|
|
2420
|
+
if (!types.isNativeError(error) || !(error instanceof BrainbaseLoginError))
|
|
2421
|
+
return 'unknown';
|
|
2422
|
+
switch (Object.getOwnPropertyDescriptor(error, 'code')?.value) {
|
|
2423
|
+
case 'login_missing': return 'login_missing';
|
|
2424
|
+
case 'login_expired': return 'login_expired';
|
|
2425
|
+
case 'login_origin_mismatch': return 'login_origin_mismatch';
|
|
2426
|
+
case 'login_busy':
|
|
2427
|
+
case 'login_changed':
|
|
2428
|
+
case 'login_credentials_invalid':
|
|
2429
|
+
case 'login_credentials_path_invalid':
|
|
2430
|
+
case 'login_credentials_path_symlink':
|
|
2431
|
+
case 'login_credentials_unreadable':
|
|
2432
|
+
case 'login_credentials_unrecognized':
|
|
2433
|
+
case 'login_deadline':
|
|
2434
|
+
case 'login_lock_lost':
|
|
2435
|
+
case 'login_refresh_unavailable':
|
|
2436
|
+
case 'login_refresh_unreachable':
|
|
2437
|
+
case 'login_storage_unsupported':
|
|
2438
|
+
case 'login_token_expiry_missing':
|
|
2439
|
+
return 'login_unavailable';
|
|
2440
|
+
default:
|
|
2441
|
+
return 'unknown';
|
|
2442
|
+
}
|
|
2443
|
+
}
|
|
2402
2444
|
async function forward(payload, context, repository, login, antigravityEvent) {
|
|
2403
2445
|
if (!hostCanMapRepository(repository, context.config))
|
|
2404
2446
|
throw new Error('judgment_repository_unmappable');
|
|
@@ -2412,12 +2454,10 @@ async function forward(payload, context, repository, login, antigravityEvent) {
|
|
|
2412
2454
|
loginId = identity.loginId;
|
|
2413
2455
|
}
|
|
2414
2456
|
catch (error) {
|
|
2415
|
-
|
|
2416
|
-
throw new Error(typeof code === 'string' && /^login_[a-z_]{1,60}$/u.test(code)
|
|
2417
|
-
? `judgment_hook_${code}` : 'judgment_hook_login_unavailable');
|
|
2457
|
+
throw forwardError(loginStartError(error));
|
|
2418
2458
|
}
|
|
2419
2459
|
if (!token || loginId === null)
|
|
2420
|
-
throw
|
|
2460
|
+
throw forwardError('login_unavailable');
|
|
2421
2461
|
if (login.expected !== undefined && login.expected !== loginId)
|
|
2422
2462
|
throw new Error('judgment_turn_login_changed');
|
|
2423
2463
|
login.used = loginId;
|
|
@@ -2426,7 +2466,10 @@ async function forward(payload, context, repository, login, antigravityEvent) {
|
|
|
2426
2466
|
session_id: antigravityNamespacedId(String(payload.session_id), context.config),
|
|
2427
2467
|
turn_id: antigravityNamespacedId(String(payload.turn_id), context.config),
|
|
2428
2468
|
};
|
|
2469
|
+
const body = JSON.stringify(sent);
|
|
2470
|
+
const signal = AbortSignal.timeout(timeoutForEvent(context.config, antigravityEvent));
|
|
2429
2471
|
let response;
|
|
2472
|
+
let text;
|
|
2430
2473
|
try {
|
|
2431
2474
|
response = await context.fetch(endpoint, {
|
|
2432
2475
|
method: 'POST',
|
|
@@ -2437,16 +2480,19 @@ async function forward(payload, context, repository, login, antigravityEvent) {
|
|
|
2437
2480
|
...(context.config.project_code ? { 'x-brainbase-project-code': context.config.project_code } : {}),
|
|
2438
2481
|
...(repository ? { 'x-brainbase-repository': repository } : {}),
|
|
2439
2482
|
},
|
|
2440
|
-
body
|
|
2483
|
+
body,
|
|
2441
2484
|
redirect: 'error',
|
|
2442
|
-
signal
|
|
2485
|
+
signal,
|
|
2443
2486
|
});
|
|
2487
|
+
text = await response.text();
|
|
2444
2488
|
}
|
|
2445
2489
|
catch (error) {
|
|
2446
|
-
|
|
2447
|
-
|
|
2490
|
+
if (signal.aborted && error === signal.reason)
|
|
2491
|
+
throw forwardError('timeout');
|
|
2492
|
+
if (types.isNativeError(error) && error instanceof TypeError)
|
|
2493
|
+
throw forwardError('transport');
|
|
2494
|
+
throw forwardError('unknown');
|
|
2448
2495
|
}
|
|
2449
|
-
const text = await response.text();
|
|
2450
2496
|
let envelope;
|
|
2451
2497
|
try {
|
|
2452
2498
|
envelope = JSON.parse(text);
|
|
@@ -2455,9 +2501,9 @@ async function forward(payload, context, repository, login, antigravityEvent) {
|
|
|
2455
2501
|
envelope = null;
|
|
2456
2502
|
}
|
|
2457
2503
|
if (!response.ok) {
|
|
2458
|
-
const
|
|
2459
|
-
|
|
2460
|
-
|
|
2504
|
+
const status = response.status;
|
|
2505
|
+
throw forwardError(Number.isInteger(status) && status >= 500 && status <= 599
|
|
2506
|
+
? 'http_5xx' : 'unknown');
|
|
2461
2507
|
}
|
|
2462
2508
|
const output = envelope?.output;
|
|
2463
2509
|
if (!envelope || envelope.schema_version !== '1' || envelope.accepted !== true
|
|
@@ -2781,6 +2827,7 @@ async function handlePreInvocation(payload, conversationId, context) {
|
|
|
2781
2827
|
writeState(context.env, key, {
|
|
2782
2828
|
...latest,
|
|
2783
2829
|
start_status: 'started',
|
|
2830
|
+
last_start_error: undefined,
|
|
2784
2831
|
login: loginId,
|
|
2785
2832
|
effective_project_code: effectiveProjectCode,
|
|
2786
2833
|
auth_fingerprint: authFingerprint,
|
|
@@ -2800,9 +2847,23 @@ async function handlePreInvocation(payload, conversationId, context) {
|
|
|
2800
2847
|
const step = buildSystemMessageStep(openedState.turn_id, additionalContext, systemMessage);
|
|
2801
2848
|
return step ? preInvocationOutputFromSteps([step]) : {};
|
|
2802
2849
|
}
|
|
2803
|
-
catch {
|
|
2804
|
-
//
|
|
2805
|
-
//
|
|
2850
|
+
catch (error) {
|
|
2851
|
+
// Keep the failed turn active so duplicate invocations do not retry.
|
|
2852
|
+
// Record the diagnostic only if the lock is immediately available.
|
|
2853
|
+
try {
|
|
2854
|
+
await withTurnStateLock(context.env, key, () => {
|
|
2855
|
+
const latest = readState(context.env, key);
|
|
2856
|
+
if (latest?.turn_active && latest.start_status === 'failed'
|
|
2857
|
+
&& latest.turn_id === openedState.turn_id
|
|
2858
|
+
&& latest.turn_generation === openedState.turn_generation) {
|
|
2859
|
+
writeState(context.env, key, {
|
|
2860
|
+
...latest,
|
|
2861
|
+
last_start_error: forwardErrors.get(error) ?? 'unknown',
|
|
2862
|
+
});
|
|
2863
|
+
}
|
|
2864
|
+
}, { ...context.turnLock, timeoutMs: 0 });
|
|
2865
|
+
}
|
|
2866
|
+
catch { }
|
|
2806
2867
|
return {};
|
|
2807
2868
|
}
|
|
2808
2869
|
}
|
|
@@ -3043,7 +3104,7 @@ export async function handleAntigravityJudgmentHook(payload, options = {}) {
|
|
|
3043
3104
|
accessToken: options.accessToken ?? (async () => {
|
|
3044
3105
|
const login = await getAccessTokenOfLogin({ fetch: options.fetch, ...options.credentials });
|
|
3045
3106
|
if (login.serverUrl !== config.login_server) {
|
|
3046
|
-
throw
|
|
3107
|
+
throw new BrainbaseLoginError('login_origin_mismatch');
|
|
3047
3108
|
}
|
|
3048
3109
|
return { token: login.token, loginId: login.loginId };
|
|
3049
3110
|
}),
|
package/dist/run-receipt.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
declare const SOURCE_TYPE_VALUES: readonly ["mana", "codex_automations", "github_actions", "salestailor", "openryoko", "openclaw", "orca"];
|
|
2
|
+
export type RunReceiptSourceType = typeof SOURCE_TYPE_VALUES[number];
|
|
2
3
|
export type RunReceiptStatus = 'success' | 'failed' | 'blocked' | 'waiting_human' | 'cancelled';
|
|
3
4
|
export type RunReceiptEvidenceState = 'confirmed' | 'unconfirmed' | 'no_data';
|
|
4
5
|
export type RunReceiptObservationKind = 'source_run' | 'connector_observation';
|
|
@@ -38,3 +39,4 @@ export declare function createRunReceiptIdentity({ projectId, sourceType, extern
|
|
|
38
39
|
sourceWorkflowId: string;
|
|
39
40
|
}): RunReceiptIdentity;
|
|
40
41
|
export declare function normalizeRunReceipt(payload: unknown): NormalizedRunReceipt;
|
|
42
|
+
export {};
|
package/dist/run-receipt.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// @ts-nocheck
|
|
2
2
|
import { createHash } from 'node:crypto';
|
|
3
3
|
const CONTRACT_VERSION = 'run_receipt.v1';
|
|
4
|
-
const
|
|
4
|
+
const SOURCE_TYPE_VALUES = ['mana', 'codex_automations', 'github_actions', 'salestailor', 'openryoko', 'openclaw', 'orca'];
|
|
5
|
+
const SOURCE_TYPES = new Set(SOURCE_TYPE_VALUES);
|
|
5
6
|
const RUN_STATUSES = new Set(['success', 'failed', 'blocked', 'waiting_human', 'cancelled']);
|
|
6
7
|
const EVIDENCE_STATES = new Set(['confirmed', 'unconfirmed', 'no_data']);
|
|
7
8
|
const OBSERVATION_KINDS = new Set(['source_run', 'connector_observation']);
|
|
@@ -50,6 +51,8 @@ const RUN_KEYS = new Set([
|
|
|
50
51
|
'evidence_refs',
|
|
51
52
|
'judgment_trace'
|
|
52
53
|
]);
|
|
54
|
+
const ORCA_RUN_KEYS = new Set([...RUN_KEYS, 'cost_basis', 'observed_at', 'stall_reason']);
|
|
55
|
+
const ORCA_COST_METRICS = new Set(['cost_usd', 'cost_usd_24h']);
|
|
53
56
|
const DELIVERY_KEYS = new Set(['idempotency_key', 'attempt', 'sent_at']);
|
|
54
57
|
const EVIDENCE_KEYS = new Set(['kind', 'ref', 'label']);
|
|
55
58
|
const TRACE_KEYS = new Set([
|
|
@@ -210,7 +213,7 @@ function hasIndependentEvidence(leftRefs, rightRefs) {
|
|
|
210
213
|
const left = new Set(leftRefs.map((ref) => `${ref.kind}:${ref.ref}`));
|
|
211
214
|
return rightRefs.some((ref) => !left.has(`${ref.kind}:${ref.ref}`));
|
|
212
215
|
}
|
|
213
|
-
function validateMetrics(value) {
|
|
216
|
+
function validateMetrics(value, isOrca) {
|
|
214
217
|
if (value === undefined || value === null)
|
|
215
218
|
return undefined;
|
|
216
219
|
const metrics = requireObject(value, 'run.metrics');
|
|
@@ -221,6 +224,15 @@ function validateMetrics(value) {
|
|
|
221
224
|
fail('forbidden_metric_name', `run.metrics.${key} is content-like`, { path: `run.metrics.${key}` });
|
|
222
225
|
}
|
|
223
226
|
const metric = metrics[key];
|
|
227
|
+
if (isOrca && metric !== null) {
|
|
228
|
+
const path = `run.metrics.${key}`;
|
|
229
|
+
if (ORCA_COST_METRICS.has(key) && (!Number.isFinite(metric) || metric < 0)) {
|
|
230
|
+
fail('invalid_orca_cost', `${path} must be a non-negative finite number or null`, { path });
|
|
231
|
+
}
|
|
232
|
+
if (key === 'staleness_threshold_sec' && (!Number.isInteger(metric) || metric <= 0)) {
|
|
233
|
+
fail('invalid_orca_staleness_threshold', `${path} must be a positive integer or null`, { path });
|
|
234
|
+
}
|
|
235
|
+
}
|
|
224
236
|
if (metric !== null && typeof metric !== 'boolean' && !(typeof metric === 'number' && Number.isFinite(metric))) {
|
|
225
237
|
fail('invalid_metric_value', `run.metrics.${key} must be a finite number, boolean, or null`, {
|
|
226
238
|
path: `run.metrics.${key}`
|
|
@@ -230,6 +242,33 @@ function validateMetrics(value) {
|
|
|
230
242
|
});
|
|
231
243
|
return normalized;
|
|
232
244
|
}
|
|
245
|
+
function validateOrcaRunFields(run) {
|
|
246
|
+
const { cost_basis, observed_at, stall_reason } = run;
|
|
247
|
+
if (cost_basis !== undefined && cost_basis !== 'run_estimated') {
|
|
248
|
+
fail('unsupported_orca_cost_basis', 'run.cost_basis must be run_estimated', { path: 'run.cost_basis' });
|
|
249
|
+
}
|
|
250
|
+
if (observed_at !== undefined) {
|
|
251
|
+
const path = 'run.observed_at';
|
|
252
|
+
if (typeof observed_at !== 'string' || observed_at.length > 64
|
|
253
|
+
|| observed_at !== observed_at.trim() || !RFC3339_PATTERN.test(observed_at)
|
|
254
|
+
|| !Number.isFinite(Date.parse(observed_at)) || Number(observed_at.slice(11, 13)) >= 24
|
|
255
|
+
// Date.parse can roll impossible days forward. Check the local calendar date in UTC.
|
|
256
|
+
|| new Date(`${observed_at.slice(0, 10)}T00:00:00Z`).toISOString().slice(0, 10) !== observed_at.slice(0, 10)) {
|
|
257
|
+
fail('invalid_timestamp', `${path} must be a valid RFC 3339 timestamp`, { path });
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if (stall_reason !== undefined) {
|
|
261
|
+
requireString(stall_reason, 'run.stall_reason', 300);
|
|
262
|
+
if (/[\u0080-\u009f\u2028\u2029]/.test(stall_reason)) {
|
|
263
|
+
fail('invalid_string', 'run.stall_reason must be single-line and control-character free', { path: 'run.stall_reason' });
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return {
|
|
267
|
+
...(cost_basis !== undefined ? { cost_basis } : {}),
|
|
268
|
+
...(observed_at !== undefined ? { observed_at } : {}),
|
|
269
|
+
...(stall_reason !== undefined ? { stall_reason } : {})
|
|
270
|
+
};
|
|
271
|
+
}
|
|
233
272
|
function validateEvidenceRefs(value, path = 'run.evidence_refs', maxLength = null) {
|
|
234
273
|
if (value === undefined || value === null)
|
|
235
274
|
return [];
|
|
@@ -526,7 +565,8 @@ export function normalizeRunReceipt(payload) {
|
|
|
526
565
|
const runValue = requireObject(envelope.run, 'run');
|
|
527
566
|
const deliveryValue = requireObject(envelope.delivery, 'delivery');
|
|
528
567
|
rejectUnknownKeys(sourceValue, SOURCE_KEYS, 'source');
|
|
529
|
-
|
|
568
|
+
const isOrca = sourceValue.type === 'orca';
|
|
569
|
+
rejectUnknownKeys(runValue, isOrca ? ORCA_RUN_KEYS : RUN_KEYS, 'run');
|
|
530
570
|
rejectUnknownKeys(deliveryValue, DELIVERY_KEYS, 'delivery');
|
|
531
571
|
const source = {
|
|
532
572
|
type: requireEnum(sourceValue.type, 'source.type', SOURCE_TYPES, 'unsupported_source_type'),
|
|
@@ -544,7 +584,7 @@ export function normalizeRunReceipt(payload) {
|
|
|
544
584
|
fail('invalid_timestamp_order', 'run.finished_at may not precede run.started_at');
|
|
545
585
|
}
|
|
546
586
|
const evidenceRefs = validateEvidenceRefs(runValue.evidence_refs);
|
|
547
|
-
const metrics = validateMetrics(runValue.metrics);
|
|
587
|
+
const metrics = validateMetrics(runValue.metrics, isOrca);
|
|
548
588
|
const judgmentTrace = validateJudgmentTrace(runValue.judgment_trace);
|
|
549
589
|
const actionRequired = optionalEnum(runValue.action_required, 'run.action_required', ACTIONS, 'unsupported_action_required');
|
|
550
590
|
const run = {
|
|
@@ -568,6 +608,7 @@ export function normalizeRunReceipt(payload) {
|
|
|
568
608
|
: {}),
|
|
569
609
|
...(actionRequired ? { action_required: actionRequired } : {}),
|
|
570
610
|
...(metrics !== undefined ? { metrics } : {}),
|
|
611
|
+
...(isOrca ? validateOrcaRunFields(runValue) : {}),
|
|
571
612
|
...(judgmentTrace !== undefined ? { judgment_trace: judgmentTrace } : {}),
|
|
572
613
|
evidence_refs: evidenceRefs
|
|
573
614
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unson/brainbase-mcp",
|
|
3
|
-
"version": "0.51.
|
|
3
|
+
"version": "0.51.9",
|
|
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",
|
|
@@ -673,5 +673,5 @@
|
|
|
673
673
|
"url": "https://github.com/Unson-LLC/brainbase/issues"
|
|
674
674
|
},
|
|
675
675
|
"homepage": "https://github.com/Unson-LLC/brainbase#readme",
|
|
676
|
-
"gitHead": "
|
|
676
|
+
"gitHead": "d86936d501ce4b634538a82b4af0f9ab7ae0a3dd"
|
|
677
677
|
}
|