@unson/brainbase-mcp 0.51.8 → 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.
@@ -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
- const code = error?.code;
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 new Error('judgment_hook_login_unavailable');
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: JSON.stringify(sent),
2483
+ body,
2441
2484
  redirect: 'error',
2442
- signal: AbortSignal.timeout(timeoutForEvent(context.config, antigravityEvent)),
2485
+ signal,
2443
2486
  });
2487
+ text = await response.text();
2444
2488
  }
2445
2489
  catch (error) {
2446
- throw new Error(error?.name === 'TimeoutError'
2447
- ? 'judgment_hook_timeout' : 'judgment_hook_unreachable');
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 upstream = typeof envelope?.error === 'string' && /^[a-z0-9_]{1,80}$/u.test(envelope.error)
2459
- ? `_${envelope.error}` : '';
2460
- throw new Error(`judgment_hook_http_${response.status}${upstream}`);
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
- // Fail-open: keep turn_active=true with start_status='failed' so subsequent invocations
2805
- // in the same turn do not retry or leak orphan events.
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 Object.assign(new Error('login_origin_mismatch'), { code: 'login_origin_mismatch' });
3107
+ throw new BrainbaseLoginError('login_origin_mismatch');
3047
3108
  }
3048
3109
  return { token: login.token, loginId: login.loginId };
3049
3110
  }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unson/brainbase-mcp",
3
- "version": "0.51.8",
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": "db93501fc9226053fdb20082921e6c062c2d4b56"
676
+ "gitHead": "d86936d501ce4b634538a82b4af0f9ab7ae0a3dd"
677
677
  }