@unson/brainbase-mcp 0.51.2 → 0.51.3
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.
|
@@ -178,6 +178,12 @@ export interface TurnStateLockOptions {
|
|
|
178
178
|
declare function withTurnStateLock<T>(env: NodeJS.ProcessEnv, key: string, fn: () => T | Promise<T>, options?: TurnStateLockOptions): Promise<T>;
|
|
179
179
|
/** Namespaces session and turn IDs per registration, matching Claude Code isolation. */
|
|
180
180
|
export declare function antigravityNamespacedId(id: string, config: Pick<AntigravityHookConfig, 'registration'>): string;
|
|
181
|
+
/**
|
|
182
|
+
* Formats a Brainbase judgment context as an Antigravity `injectSteps` entry.
|
|
183
|
+
* Because `systemMessage` persists in Antigravity's conversation trajectory across turns,
|
|
184
|
+
* the header binds it to the current `turn_id` and explicitly supersedes earlier turns.
|
|
185
|
+
*/
|
|
186
|
+
declare function buildSystemMessageStep(turnId: string, additionalContext?: string, userMarkdown?: string): Record<string, unknown> | null;
|
|
181
187
|
/** Handles one Antigravity lifecycle hook event. Never throws. */
|
|
182
188
|
export declare function handleAntigravityJudgmentHook(payload: AntigravityHookPayload, options?: AntigravityHookOptions): Promise<AntigravityHookOutput>;
|
|
183
189
|
/** Builds the POSIX-quoted command registered in Antigravity `hooks.json`. */
|
|
@@ -203,6 +209,7 @@ export declare function registerAntigravityJudgmentHooks(hooksJson: Record<strin
|
|
|
203
209
|
}): Record<string, unknown>;
|
|
204
210
|
/** @internal Exposed strictly for deterministic concurrency and TOCTOU regression tests. */
|
|
205
211
|
export declare const __antigravityHookTestInternals: {
|
|
212
|
+
buildSystemMessageStep: typeof buildSystemMessageStep;
|
|
206
213
|
withTurnStateLock: typeof withTurnStateLock;
|
|
207
214
|
readTranscriptTailEntries: typeof readTranscriptTailEntries;
|
|
208
215
|
resolveOpenatHelperBinary: typeof resolveOpenatHelperBinary;
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
// 5. Base URLs with path prefixes (e.g. `https://bb.unson.jp/runtime-mcp`) preserve their
|
|
19
19
|
// path when resolving `host/judgment/hook`.
|
|
20
20
|
// 6. Strict fail-open outputs with short HTTP timeouts:
|
|
21
|
-
// - PreInvocation: `{}` (or `{ injectSteps: [{ systemMessage: { systemMessage
|
|
21
|
+
// - PreInvocation: `{}` (or `{ injectSteps: [{ systemMessage: { systemMessage } }], terminationBehavior: '' }`)
|
|
22
22
|
// - PreToolUse: `{ decision: 'allow' }` (or `{ decision: 'deny', reason }`)
|
|
23
23
|
// - PostToolUse: ALWAYS `{}` (any follow-up context is queued with `turn_id` for next PreInvocation)
|
|
24
24
|
// - Stop: ALWAYS `{ decision: '' }` (never `"continue"`)
|
|
@@ -26,7 +26,7 @@ import { spawn, spawnSync } from 'node:child_process';
|
|
|
26
26
|
import { createHash, randomUUID } from 'node:crypto';
|
|
27
27
|
import { accessSync, closeSync, constants as fsConstants, fstatSync, lstatSync, mkdirSync, openSync, readFileSync, realpathSync, renameSync, statSync, unlinkSync, writeFileSync, } from 'node:fs';
|
|
28
28
|
import { homedir } from 'node:os';
|
|
29
|
-
import { delimiter, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
29
|
+
import { basename, delimiter, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
30
30
|
import { fileURLToPath } from 'node:url';
|
|
31
31
|
import { BrainbaseLoginError, getAccessTokenOfLogin } from './brainbase-login.js';
|
|
32
32
|
import { PRE_TOOL_USE_TOOLS, REGISTRATION_ID, bearerTransportSafe, canonicalFilePath, canonicalRepositoryIdentifier, digestSummary, forwardedStopPayload, hostCanMapRepository, resolveRepositoryScope, shellQuote, singleQuotedWords, } from './claude-code-judgment-hook.js';
|
|
@@ -679,6 +679,121 @@ function resolveAuthorizedTranscriptTarget(transcriptPath, options) {
|
|
|
679
679
|
function resolveAuthorizedTranscriptPath(transcriptPath, options) {
|
|
680
680
|
return resolveAuthorizedTranscriptTarget(transcriptPath, options)?.canonicalPath ?? null;
|
|
681
681
|
}
|
|
682
|
+
function isEnoentError(error) {
|
|
683
|
+
return !!error && typeof error === 'object' && error.code === 'ENOENT';
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Resolves an authorized transcript location ONLY when the transcript file itself
|
|
687
|
+
* does not exist yet (`ENOENT`), e.g. on the first tool call before Antigravity
|
|
688
|
+
* flushes `logs/transcript.jsonl` while `steps/<n>/output.txt` already exists.
|
|
689
|
+
*
|
|
690
|
+
* Strictly enforces the same canonical root, conversation structure, and
|
|
691
|
+
* no-symlink ancestor invariants; fails closed if the file exists (and was
|
|
692
|
+
* already rejected by `resolveAuthorizedTranscriptTarget`), contains `.`/`..`,
|
|
693
|
+
* or traverses any symlink inside the authorized root.
|
|
694
|
+
*/
|
|
695
|
+
function resolveAuthorizedMissingTranscriptTarget(transcriptPath, options) {
|
|
696
|
+
if (typeof transcriptPath !== 'string' || !transcriptPath.trim() || transcriptPath.includes('\0')) {
|
|
697
|
+
return null;
|
|
698
|
+
}
|
|
699
|
+
const conversationId = options?.conversationId?.trim() ?? '';
|
|
700
|
+
if (!isSafeConversationIdSegment(conversationId))
|
|
701
|
+
return null;
|
|
702
|
+
const rawPath = transcriptPath.trim();
|
|
703
|
+
if (!isAbsolute(rawPath))
|
|
704
|
+
return null;
|
|
705
|
+
const rawSegments = rawPath.split(/[/\\]/u);
|
|
706
|
+
if (rawSegments.includes('..') || rawSegments.includes('.')) {
|
|
707
|
+
return null;
|
|
708
|
+
}
|
|
709
|
+
try {
|
|
710
|
+
lstatSync(rawPath);
|
|
711
|
+
return null;
|
|
712
|
+
}
|
|
713
|
+
catch (error) {
|
|
714
|
+
if (!isEnoentError(error))
|
|
715
|
+
return null;
|
|
716
|
+
}
|
|
717
|
+
const fileName = basename(rawPath);
|
|
718
|
+
if (!ALLOWED_TRANSCRIPT_FILENAMES.has(fileName) || !isSafePathSegment(fileName)) {
|
|
719
|
+
return null;
|
|
720
|
+
}
|
|
721
|
+
let candidateCanonical = null;
|
|
722
|
+
const parentDir = dirname(rawPath);
|
|
723
|
+
try {
|
|
724
|
+
const parentStat = lstatSync(parentDir);
|
|
725
|
+
if (parentStat.isSymbolicLink() || !parentStat.isDirectory())
|
|
726
|
+
return null;
|
|
727
|
+
candidateCanonical = join(realpathSync(parentDir), fileName);
|
|
728
|
+
}
|
|
729
|
+
catch (error) {
|
|
730
|
+
if (!isEnoentError(error))
|
|
731
|
+
return null;
|
|
732
|
+
const parentName = basename(parentDir);
|
|
733
|
+
if (parentName !== 'logs' || !isSafePathSegment(parentName))
|
|
734
|
+
return null;
|
|
735
|
+
const grandParentDir = dirname(parentDir);
|
|
736
|
+
try {
|
|
737
|
+
const grandStat = lstatSync(grandParentDir);
|
|
738
|
+
if (grandStat.isSymbolicLink() || !grandStat.isDirectory())
|
|
739
|
+
return null;
|
|
740
|
+
candidateCanonical = join(realpathSync(grandParentDir), parentName, fileName);
|
|
741
|
+
}
|
|
742
|
+
catch {
|
|
743
|
+
return null;
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
if (!candidateCanonical)
|
|
747
|
+
return null;
|
|
748
|
+
const roots = canonicalTranscriptRoots(options);
|
|
749
|
+
if (roots.length === 0)
|
|
750
|
+
return null;
|
|
751
|
+
const matchedRoot = roots.find((root) => (root !== sep
|
|
752
|
+
&& root !== '/'
|
|
753
|
+
&& matchesConversationTranscriptStructure(root, candidateCanonical, conversationId)));
|
|
754
|
+
if (!matchedRoot)
|
|
755
|
+
return null;
|
|
756
|
+
try {
|
|
757
|
+
const rootStat = lstatSync(matchedRoot);
|
|
758
|
+
if (rootStat.isSymbolicLink() || !rootStat.isDirectory())
|
|
759
|
+
return null;
|
|
760
|
+
if (realpathSync(matchedRoot) !== matchedRoot)
|
|
761
|
+
return null;
|
|
762
|
+
}
|
|
763
|
+
catch {
|
|
764
|
+
return null;
|
|
765
|
+
}
|
|
766
|
+
const segments = relative(matchedRoot, candidateCanonical).split(sep);
|
|
767
|
+
if (segments.length === 0 || !segments.every(isSafePathSegment))
|
|
768
|
+
return null;
|
|
769
|
+
let current = matchedRoot;
|
|
770
|
+
let sawMissingDir = false;
|
|
771
|
+
for (let i = 0; i < segments.length - 1; i += 1) {
|
|
772
|
+
current = join(current, segments[i]);
|
|
773
|
+
if (sawMissingDir)
|
|
774
|
+
return null;
|
|
775
|
+
try {
|
|
776
|
+
const dirStat = lstatSync(current);
|
|
777
|
+
if (dirStat.isSymbolicLink() || !dirStat.isDirectory())
|
|
778
|
+
return null;
|
|
779
|
+
}
|
|
780
|
+
catch (error) {
|
|
781
|
+
if (!isEnoentError(error) || i !== segments.length - 2 || segments[i] !== 'logs') {
|
|
782
|
+
return null;
|
|
783
|
+
}
|
|
784
|
+
sawMissingDir = true;
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
try {
|
|
788
|
+
lstatSync(candidateCanonical);
|
|
789
|
+
return null;
|
|
790
|
+
}
|
|
791
|
+
catch (error) {
|
|
792
|
+
if (!isEnoentError(error))
|
|
793
|
+
return null;
|
|
794
|
+
}
|
|
795
|
+
return { canonicalRoot: matchedRoot, canonicalPath: candidateCanonical, segments };
|
|
796
|
+
}
|
|
682
797
|
function buildPythonOpenatReader(faults, mode = 'tail') {
|
|
683
798
|
const forceOpenat2Errno = faults?.forceOpenat2Errno;
|
|
684
799
|
const renameAfterRead = faults?.renameAfterTailRead;
|
|
@@ -1222,82 +1337,120 @@ function resolveSiblingFullTranscriptTarget(baseTarget, options) {
|
|
|
1222
1337
|
}
|
|
1223
1338
|
function resolveAuthorizedStepOutputTarget(baseTarget, conversationId, candidatePath) {
|
|
1224
1339
|
if (!candidatePath || !isAbsolute(candidatePath) || candidatePath.includes('\0')) {
|
|
1225
|
-
return
|
|
1340
|
+
return { kind: 'rejected' };
|
|
1226
1341
|
}
|
|
1227
1342
|
const sysGenSegments = systemGeneratedPrefixSegments(baseTarget, conversationId);
|
|
1228
1343
|
if (!sysGenSegments)
|
|
1229
|
-
return
|
|
1344
|
+
return { kind: 'rejected' };
|
|
1230
1345
|
const stepsDirRaw = join(baseTarget.canonicalRoot, ...sysGenSegments, 'steps');
|
|
1231
1346
|
try {
|
|
1232
|
-
|
|
1347
|
+
let stepsDirStat;
|
|
1348
|
+
try {
|
|
1349
|
+
stepsDirStat = lstatSync(stepsDirRaw);
|
|
1350
|
+
}
|
|
1351
|
+
catch (error) {
|
|
1352
|
+
return isEnoentError(error) ? { kind: 'missing' } : { kind: 'rejected' };
|
|
1353
|
+
}
|
|
1233
1354
|
if (stepsDirStat.isSymbolicLink() || !stepsDirStat.isDirectory())
|
|
1234
|
-
return
|
|
1355
|
+
return { kind: 'rejected' };
|
|
1235
1356
|
const stepsDir = realpathSync(stepsDirRaw);
|
|
1236
1357
|
if (stepsDir !== stepsDirRaw || !isStrictDescendantPath(baseTarget.canonicalRoot, stepsDir)) {
|
|
1237
|
-
return
|
|
1358
|
+
return { kind: 'rejected' };
|
|
1359
|
+
}
|
|
1360
|
+
let candidateParentStat;
|
|
1361
|
+
try {
|
|
1362
|
+
candidateParentStat = lstatSync(dirname(candidatePath));
|
|
1363
|
+
}
|
|
1364
|
+
catch (error) {
|
|
1365
|
+
return isEnoentError(error) ? { kind: 'missing' } : { kind: 'rejected' };
|
|
1238
1366
|
}
|
|
1239
|
-
const candidateParentStat = lstatSync(dirname(candidatePath));
|
|
1240
1367
|
if (candidateParentStat.isSymbolicLink() || !candidateParentStat.isDirectory())
|
|
1241
|
-
return
|
|
1242
|
-
|
|
1368
|
+
return { kind: 'rejected' };
|
|
1369
|
+
let candidateStat;
|
|
1370
|
+
try {
|
|
1371
|
+
candidateStat = lstatSync(candidatePath);
|
|
1372
|
+
}
|
|
1373
|
+
catch (error) {
|
|
1374
|
+
return isEnoentError(error) ? { kind: 'missing' } : { kind: 'rejected' };
|
|
1375
|
+
}
|
|
1243
1376
|
if (candidateStat.isSymbolicLink() || !candidateStat.isFile())
|
|
1244
|
-
return
|
|
1377
|
+
return { kind: 'rejected' };
|
|
1245
1378
|
const canonicalFile = realpathSync(candidatePath);
|
|
1246
1379
|
if (!isStrictDescendantPath(stepsDir, canonicalFile))
|
|
1247
|
-
return
|
|
1380
|
+
return { kind: 'rejected' };
|
|
1248
1381
|
const relFromSteps = relative(stepsDir, canonicalFile).split(sep);
|
|
1249
1382
|
if (relFromSteps.length !== 2 || !/^\d+$/u.test(relFromSteps[0]) || relFromSteps[1] !== 'output.txt') {
|
|
1250
|
-
return
|
|
1383
|
+
return { kind: 'rejected' };
|
|
1251
1384
|
}
|
|
1252
1385
|
if (!verifyNoSymlinkPathUnderRoot(baseTarget.canonicalRoot, canonicalFile).ok) {
|
|
1253
|
-
return
|
|
1386
|
+
return { kind: 'rejected' };
|
|
1254
1387
|
}
|
|
1255
1388
|
return {
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1389
|
+
kind: 'ok',
|
|
1390
|
+
target: {
|
|
1391
|
+
canonicalRoot: baseTarget.canonicalRoot,
|
|
1392
|
+
canonicalPath: canonicalFile,
|
|
1393
|
+
segments: [...sysGenSegments, 'steps', relFromSteps[0], 'output.txt'],
|
|
1394
|
+
},
|
|
1259
1395
|
};
|
|
1260
1396
|
}
|
|
1261
1397
|
catch {
|
|
1262
|
-
return
|
|
1398
|
+
return { kind: 'rejected' };
|
|
1263
1399
|
}
|
|
1264
1400
|
}
|
|
1401
|
+
function isToolResultEntry(entry) {
|
|
1402
|
+
if (entry.type === 'USER_INPUT' || entry.type === 'PLANNER_RESPONSE')
|
|
1403
|
+
return false;
|
|
1404
|
+
return typeof entry.content === 'string' && entry.content.trim().length > 0;
|
|
1405
|
+
}
|
|
1406
|
+
function hasStepIndexInTranscript(entries, stepIdx) {
|
|
1407
|
+
return entries.some((entry) => entry.step_index === stepIdx);
|
|
1408
|
+
}
|
|
1409
|
+
function hasConflictingToolUseIdAtStep(entries, stepIdx, toolUseId) {
|
|
1410
|
+
return entries.some((entry) => {
|
|
1411
|
+
if (entry.step_index !== stepIdx)
|
|
1412
|
+
return false;
|
|
1413
|
+
const entryId = entryToolUseId(entry);
|
|
1414
|
+
return entryId !== null && entryId !== toolUseId;
|
|
1415
|
+
});
|
|
1416
|
+
}
|
|
1265
1417
|
function findToolEntryInTranscript(entries, stepCandidates, toolUseId) {
|
|
1266
1418
|
const normalizedToolUseId = typeof toolUseId === 'string' && toolUseId.trim() ? toolUseId.trim() : null;
|
|
1267
|
-
|
|
1268
|
-
|
|
1419
|
+
// 1. Prefer exact tool_use_id / tool_call_id match on step candidates first.
|
|
1420
|
+
if (normalizedToolUseId !== null) {
|
|
1421
|
+
for (const targetStep of stepCandidates) {
|
|
1269
1422
|
for (let index = entries.length - 1; index >= 0; index -= 1) {
|
|
1270
1423
|
const entry = entries[index];
|
|
1271
|
-
if (entry.step_index !== targetStep)
|
|
1272
|
-
continue;
|
|
1273
|
-
if (entry.type === 'USER_INPUT' || entry.type === 'PLANNER_RESPONSE')
|
|
1274
|
-
continue;
|
|
1275
|
-
if (typeof entry.content !== 'string' || !entry.content.trim())
|
|
1424
|
+
if (entry.step_index !== targetStep || !isToolResultEntry(entry))
|
|
1276
1425
|
continue;
|
|
1277
1426
|
if (entryToolUseId(entry) === normalizedToolUseId)
|
|
1278
1427
|
return entry;
|
|
1279
1428
|
}
|
|
1280
1429
|
}
|
|
1430
|
+
// 2. Fall back to exact tool_use_id / tool_call_id match anywhere in the transcript tail
|
|
1431
|
+
// (handles step_index shifts between transcript.jsonl and transcript_full.jsonl).
|
|
1281
1432
|
for (let index = entries.length - 1; index >= 0; index -= 1) {
|
|
1282
1433
|
const entry = entries[index];
|
|
1283
|
-
if (entry
|
|
1434
|
+
if (!isToolResultEntry(entry))
|
|
1284
1435
|
continue;
|
|
1285
|
-
if (entry
|
|
1286
|
-
|
|
1287
|
-
if (typeof entry.content !== 'string' || !entry.content.trim())
|
|
1288
|
-
continue;
|
|
1289
|
-
return entry;
|
|
1436
|
+
if (entryToolUseId(entry) === normalizedToolUseId)
|
|
1437
|
+
return entry;
|
|
1290
1438
|
}
|
|
1291
1439
|
}
|
|
1292
|
-
|
|
1440
|
+
// 3. Untagged step_index match is strictly restricted to the exact primary step (`stepCandidates[0]`).
|
|
1441
|
+
// Never match a neighboring step (`stepIdx + 1`) without an explicit tool_use_id match, and
|
|
1442
|
+
// never match an entry whose explicit tool_use_id disagrees with the caller's toolUseId.
|
|
1443
|
+
const primaryStep = stepCandidates[0];
|
|
1444
|
+
if (primaryStep !== undefined) {
|
|
1293
1445
|
for (let index = entries.length - 1; index >= 0; index -= 1) {
|
|
1294
1446
|
const entry = entries[index];
|
|
1295
|
-
if (entry.
|
|
1447
|
+
if (entry.step_index !== primaryStep || !isToolResultEntry(entry))
|
|
1296
1448
|
continue;
|
|
1297
|
-
|
|
1449
|
+
const entryId = entryToolUseId(entry);
|
|
1450
|
+
if (normalizedToolUseId !== null && entryId !== null && entryId !== normalizedToolUseId) {
|
|
1298
1451
|
continue;
|
|
1299
|
-
|
|
1300
|
-
|
|
1452
|
+
}
|
|
1453
|
+
return entry;
|
|
1301
1454
|
}
|
|
1302
1455
|
}
|
|
1303
1456
|
return null;
|
|
@@ -1333,10 +1486,10 @@ function readStepOutputFallback(baseTarget, conversationId, referenceTexts, cand
|
|
|
1333
1486
|
const filePath = parseLocalFileUrlPath(explicitUrl);
|
|
1334
1487
|
if (!filePath)
|
|
1335
1488
|
return null;
|
|
1336
|
-
const
|
|
1337
|
-
if (
|
|
1489
|
+
const resolution = resolveAuthorizedStepOutputTarget(baseTarget, conversationId, filePath);
|
|
1490
|
+
if (resolution.kind !== 'ok')
|
|
1338
1491
|
return null;
|
|
1339
|
-
return readTarget(
|
|
1492
|
+
return readTarget(resolution.target);
|
|
1340
1493
|
}
|
|
1341
1494
|
const sysGenSegments = systemGeneratedPrefixSegments(baseTarget, conversationId);
|
|
1342
1495
|
if (!sysGenSegments)
|
|
@@ -1348,55 +1501,73 @@ function readStepOutputFallback(baseTarget, conversationId, referenceTexts, cand
|
|
|
1348
1501
|
continue;
|
|
1349
1502
|
seenSteps.add(stepNum);
|
|
1350
1503
|
const candidatePath = join(stepsDir, String(stepNum), 'output.txt');
|
|
1351
|
-
const
|
|
1352
|
-
if (
|
|
1504
|
+
const resolution = resolveAuthorizedStepOutputTarget(baseTarget, conversationId, candidatePath);
|
|
1505
|
+
if (resolution.kind === 'missing')
|
|
1353
1506
|
continue;
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1507
|
+
if (resolution.kind === 'rejected')
|
|
1508
|
+
return null;
|
|
1509
|
+
// Candidate file exists and passed path checks; fail closed immediately if reading fails
|
|
1510
|
+
// (oversized >512 KiB, empty, truncation banner, non-Linux, or openat2 rejection) rather
|
|
1511
|
+
// than falling through to another step's output.
|
|
1512
|
+
return readTarget(resolution.target);
|
|
1357
1513
|
}
|
|
1358
1514
|
return null;
|
|
1359
1515
|
}
|
|
1360
1516
|
export function extractToolResultFromTranscript(transcriptPath, stepIdx, options) {
|
|
1361
|
-
const hasStepIdx = typeof stepIdx === 'number' && Number.isSafeInteger(stepIdx);
|
|
1517
|
+
const hasStepIdx = typeof stepIdx === 'number' && Number.isSafeInteger(stepIdx) && stepIdx >= 0;
|
|
1362
1518
|
const hintToolUseId = typeof options?.toolUseId === 'string' && options.toolUseId.trim()
|
|
1363
1519
|
? options.toolUseId.trim()
|
|
1364
1520
|
: undefined;
|
|
1365
1521
|
if (!hasStepIdx && !hintToolUseId)
|
|
1366
1522
|
return null;
|
|
1367
|
-
|
|
1368
|
-
|
|
1523
|
+
if (!options)
|
|
1524
|
+
return null;
|
|
1525
|
+
const existingBaseTarget = resolveAuthorizedTranscriptTarget(transcriptPath, options);
|
|
1526
|
+
const baseTarget = existingBaseTarget ?? resolveAuthorizedMissingTranscriptTarget(transcriptPath, options);
|
|
1527
|
+
if (!baseTarget)
|
|
1369
1528
|
return null;
|
|
1370
1529
|
const conversationId = options.conversationId.trim();
|
|
1371
|
-
const baseEntries = readTranscriptEntriesFromTarget(baseTarget, options, TRANSCRIPT_TAIL_MAX_BYTES);
|
|
1372
|
-
// Prefer exact step_index match, then immediate neighbor (+1) if wrapper shifted index.
|
|
1373
1530
|
const stepCandidates = hasStepIdx ? [stepIdx, stepIdx + 1] : [];
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1531
|
+
let baseEntries = [];
|
|
1532
|
+
let matchedEntry = null;
|
|
1533
|
+
let baseStripped = null;
|
|
1534
|
+
if (existingBaseTarget !== null) {
|
|
1535
|
+
baseEntries = readTranscriptEntriesFromTarget(existingBaseTarget, options, TRANSCRIPT_TAIL_MAX_BYTES);
|
|
1536
|
+
matchedEntry = findToolEntryInTranscript(baseEntries, stepCandidates, hintToolUseId);
|
|
1537
|
+
if (matchedEntry && typeof matchedEntry.content === 'string') {
|
|
1538
|
+
const stripped = stripToolTimestampHeader(matchedEntry.content);
|
|
1539
|
+
if (stripped) {
|
|
1540
|
+
if (!isTruncatedToolResult(matchedEntry, stripped)) {
|
|
1541
|
+
return Buffer.byteLength(stripped, 'utf8') <= TRANSCRIPT_TOOL_RESULT_MAX_BYTES
|
|
1542
|
+
? stripped
|
|
1543
|
+
: null;
|
|
1544
|
+
}
|
|
1545
|
+
baseStripped = stripped;
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1384
1548
|
}
|
|
1385
|
-
const matchedStep = typeof matchedEntry.step_index === 'number' && Number.isSafeInteger(matchedEntry.step_index)
|
|
1549
|
+
const matchedStep = matchedEntry && typeof matchedEntry.step_index === 'number' && Number.isSafeInteger(matchedEntry.step_index)
|
|
1386
1550
|
? matchedEntry.step_index
|
|
1387
1551
|
: null;
|
|
1388
1552
|
const effectiveStepCandidates = [
|
|
1389
1553
|
...(matchedStep !== null ? [matchedStep] : []),
|
|
1390
1554
|
...stepCandidates.filter((step) => step !== matchedStep),
|
|
1391
1555
|
];
|
|
1392
|
-
const effectiveToolUseId = entryToolUseId(matchedEntry) ?? hintToolUseId;
|
|
1393
|
-
// 1. Prefer sibling `transcript_full.jsonl` in the exact same authorized logs directory
|
|
1556
|
+
const effectiveToolUseId = (matchedEntry ? entryToolUseId(matchedEntry) : null) ?? hintToolUseId;
|
|
1557
|
+
// 1. Prefer sibling `transcript_full.jsonl` in the exact same authorized logs directory
|
|
1558
|
+
// (handles both truncated `transcript.jsonl` entries and steps not yet flushed to `transcript.jsonl`).
|
|
1559
|
+
let fullEntries = [];
|
|
1560
|
+
let fullEntry = null;
|
|
1394
1561
|
let fullStripped = null;
|
|
1562
|
+
let fullMatchedStep = null;
|
|
1395
1563
|
const fullTarget = resolveSiblingFullTranscriptTarget(baseTarget, options);
|
|
1396
1564
|
if (fullTarget) {
|
|
1397
|
-
|
|
1398
|
-
|
|
1565
|
+
fullEntries = readTranscriptEntriesFromTarget(fullTarget, options, TRANSCRIPT_FULL_TAIL_MAX_BYTES);
|
|
1566
|
+
fullEntry = findToolEntryInTranscript(fullEntries, effectiveStepCandidates, effectiveToolUseId);
|
|
1399
1567
|
if (fullEntry && typeof fullEntry.content === 'string') {
|
|
1568
|
+
if (typeof fullEntry.step_index === 'number' && Number.isSafeInteger(fullEntry.step_index)) {
|
|
1569
|
+
fullMatchedStep = fullEntry.step_index;
|
|
1570
|
+
}
|
|
1400
1571
|
const candidateStripped = stripToolTimestampHeader(fullEntry.content);
|
|
1401
1572
|
if (candidateStripped) {
|
|
1402
1573
|
if (!isTruncatedToolResult(fullEntry, candidateStripped)) {
|
|
@@ -1410,8 +1581,39 @@ export function extractToolResultFromTranscript(transcriptPath, stepIdx, options
|
|
|
1410
1581
|
}
|
|
1411
1582
|
// 2. Fallback to authorized `<conversation>/.system_generated/steps/<n>/output.txt`
|
|
1412
1583
|
// (strictly confined by realpath + no-symlink + Linux openat2 RESOLVE_BENEATH).
|
|
1413
|
-
const
|
|
1414
|
-
|
|
1584
|
+
const hasMatchedTranscriptEntry = matchedEntry !== null || fullEntry !== null;
|
|
1585
|
+
let stepFallbackCandidates;
|
|
1586
|
+
if (hasMatchedTranscriptEntry) {
|
|
1587
|
+
if (fullEntry === null && matchedStep !== null && effectiveToolUseId
|
|
1588
|
+
&& hasConflictingToolUseIdAtStep(fullEntries, matchedStep, effectiveToolUseId)) {
|
|
1589
|
+
return null;
|
|
1590
|
+
}
|
|
1591
|
+
stepFallbackCandidates = [
|
|
1592
|
+
...(fullMatchedStep !== null ? [fullMatchedStep] : []),
|
|
1593
|
+
...effectiveStepCandidates.filter((step) => step !== fullMatchedStep),
|
|
1594
|
+
];
|
|
1595
|
+
}
|
|
1596
|
+
else {
|
|
1597
|
+
// Unflushed direct recovery: Antigravity writes `steps/<stepIdx>/output.txt` before flushing
|
|
1598
|
+
// `transcript.jsonl`. Strictly limit candidates to the exact `[stepIdx]` (never `stepIdx + 1`)
|
|
1599
|
+
// and fail closed if either transcript already has an entry at `stepIdx` (e.g. PLANNER_RESPONSE,
|
|
1600
|
+
// USER_INPUT, non-tool entry, or conflicting tool_use_id).
|
|
1601
|
+
if (!hasStepIdx)
|
|
1602
|
+
return null;
|
|
1603
|
+
if (hasStepIndexInTranscript(baseEntries, stepIdx) || hasStepIndexInTranscript(fullEntries, stepIdx)) {
|
|
1604
|
+
return null;
|
|
1605
|
+
}
|
|
1606
|
+
stepFallbackCandidates = [stepIdx];
|
|
1607
|
+
}
|
|
1608
|
+
const referenceTexts = [];
|
|
1609
|
+
if (fullStripped !== null)
|
|
1610
|
+
referenceTexts.push(fullStripped);
|
|
1611
|
+
if (baseStripped !== null)
|
|
1612
|
+
referenceTexts.push(baseStripped);
|
|
1613
|
+
if (referenceTexts.length === 0 && stepFallbackCandidates.length === 0) {
|
|
1614
|
+
return null;
|
|
1615
|
+
}
|
|
1616
|
+
return readStepOutputFallback(baseTarget, conversationId, referenceTexts, stepFallbackCandidates, options);
|
|
1415
1617
|
}
|
|
1416
1618
|
/**
|
|
1417
1619
|
* Extracts a tool result from the authorized transcript / step output and restores
|
|
@@ -1782,12 +1984,18 @@ function buildSystemMessageStep(turnId, additionalContext, userMarkdown) {
|
|
|
1782
1984
|
`[Brainbase Judgment Turn: ${turnId}]`,
|
|
1783
1985
|
`※この判断契約・先頭監査行は当ターン(${turnId})のみ有効です。過去のターンに注入された旧ターンの判断契約・先頭監査行は上書きされ無効です。`,
|
|
1784
1986
|
].join('\n');
|
|
1785
|
-
const
|
|
1786
|
-
|
|
1987
|
+
const bodyParts = [];
|
|
1988
|
+
if (normalizedMarkdown) {
|
|
1989
|
+
bodyParts.push(normalizedMarkdown);
|
|
1990
|
+
}
|
|
1991
|
+
if (normalizedContext && normalizedContext !== normalizedMarkdown) {
|
|
1992
|
+
bodyParts.push(normalizedContext);
|
|
1993
|
+
}
|
|
1994
|
+
const modelBody = bodyParts.join('\n\n');
|
|
1995
|
+
const modelText = `${guardHeader}\n\n${modelBody}`;
|
|
1787
1996
|
return {
|
|
1788
1997
|
systemMessage: {
|
|
1789
1998
|
systemMessage: modelText,
|
|
1790
|
-
...(normalizedMarkdown ? { metadata: { user_markdown: normalizedMarkdown } } : {}),
|
|
1791
1999
|
},
|
|
1792
2000
|
};
|
|
1793
2001
|
}
|
|
@@ -2462,6 +2670,7 @@ export function registerAntigravityJudgmentHooks(hooksJson, options) {
|
|
|
2462
2670
|
}
|
|
2463
2671
|
/** @internal Exposed strictly for deterministic concurrency and TOCTOU regression tests. */
|
|
2464
2672
|
export const __antigravityHookTestInternals = {
|
|
2673
|
+
buildSystemMessageStep,
|
|
2465
2674
|
withTurnStateLock,
|
|
2466
2675
|
readTranscriptTailEntries,
|
|
2467
2676
|
resolveOpenatHelperBinary,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unson/brainbase-mcp",
|
|
3
|
-
"version": "0.51.
|
|
3
|
+
"version": "0.51.3",
|
|
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",
|
|
@@ -669,5 +669,5 @@
|
|
|
669
669
|
"url": "https://github.com/Unson-LLC/brainbase/issues"
|
|
670
670
|
},
|
|
671
671
|
"homepage": "https://github.com/Unson-LLC/brainbase#readme",
|
|
672
|
-
"gitHead": "
|
|
672
|
+
"gitHead": "862b25ea71345948f91b70f6b70ba048de576e1a"
|
|
673
673
|
}
|