@cybedefend/vibedefend 1.8.0 → 1.8.2
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 +4 -2
- package/dist/auth/auth-store.js +37 -24
- package/dist/auth/auth-store.js.map +1 -1
- package/dist/auth/auth.js +30 -7
- package/dist/auth/auth.js.map +1 -1
- package/dist/auth/refresh-lock.js +139 -0
- package/dist/auth/refresh-lock.js.map +1 -0
- package/dist/clients/antigravity.js +16 -1
- package/dist/clients/antigravity.js.map +1 -1
- package/dist/clients/windsurf.js +20 -8
- package/dist/clients/windsurf.js.map +1 -1
- package/dist/diagnostics.js +0 -1
- package/dist/diagnostics.js.map +1 -1
- package/dist/hook-runner.js +554 -485
- package/dist/hooks/runtime/fetch-rules.js +1 -3
- package/dist/hooks/runtime/fetch-rules.js.map +1 -1
- package/dist/hooks/runtime/resolve.js +25 -243
- package/dist/hooks/runtime/resolve.js.map +1 -1
- package/dist/hooks/runtime/session-start.js +17 -5
- package/dist/hooks/runtime/session-start.js.map +1 -1
- package/dist/hooks/runtime/sign-in-notice.js +44 -0
- package/dist/hooks/runtime/sign-in-notice.js.map +1 -0
- package/dist/hooks/runtime/sniff.js +20 -1
- package/dist/hooks/runtime/sniff.js.map +1 -1
- package/dist/hooks/runtime/user-prompt-submit.js +3 -5
- package/dist/hooks/runtime/user-prompt-submit.js.map +1 -1
- package/dist/install.js +19 -9
- package/dist/install.js.map +1 -1
- package/dist/login.js +2 -3
- package/dist/login.js.map +1 -1
- package/dist/logout.js +2 -6
- package/dist/logout.js.map +1 -1
- package/package.json +2 -2
package/dist/hook-runner.js
CHANGED
|
@@ -5059,6 +5059,21 @@ function detectClient(input2) {
|
|
|
5059
5059
|
function extractWindsurfShape(input2) {
|
|
5060
5060
|
const action = input2.agent_action_name;
|
|
5061
5061
|
const toolInfo = input2.tool_info ?? {};
|
|
5062
|
+
if (action === "pre_run_command" || action === "post_run_command") {
|
|
5063
|
+
const command = typeof toolInfo.command_line === "string" ? toolInfo.command_line : "";
|
|
5064
|
+
const cwd = typeof toolInfo.cwd === "string" && toolInfo.cwd.length > 0 ? toolInfo.cwd : process.cwd();
|
|
5065
|
+
const filePaths = extractShellWriteTargets(command).map(
|
|
5066
|
+
(t) => resolveWriteTarget(cwd, t)
|
|
5067
|
+
);
|
|
5068
|
+
return {
|
|
5069
|
+
client: "windsurf",
|
|
5070
|
+
toolName: "Bash",
|
|
5071
|
+
filePath: filePaths[0] ?? "",
|
|
5072
|
+
filePaths,
|
|
5073
|
+
newContent: command,
|
|
5074
|
+
raw: input2
|
|
5075
|
+
};
|
|
5076
|
+
}
|
|
5062
5077
|
const filePath = typeof toolInfo.file_path === "string" ? toolInfo.file_path : "";
|
|
5063
5078
|
const newContent = joinStringField(toolInfo.edits, "new_string");
|
|
5064
5079
|
let toolName = action;
|
|
@@ -5141,9 +5156,7 @@ function extractToolInputShape(input2, client) {
|
|
|
5141
5156
|
}
|
|
5142
5157
|
|
|
5143
5158
|
// src/hooks/runtime/resolve.ts
|
|
5144
|
-
import {
|
|
5145
|
-
import { createHash } from "node:crypto";
|
|
5146
|
-
import { existsSync as existsSync6, readFileSync as readFileSync6 } from "node:fs";
|
|
5159
|
+
import { existsSync as existsSync6, readFileSync as readFileSync7 } from "node:fs";
|
|
5147
5160
|
|
|
5148
5161
|
// src/auth/auth-store.ts
|
|
5149
5162
|
import { execFileSync } from "node:child_process";
|
|
@@ -5177,11 +5190,13 @@ function storeAuth(mcpName, auth) {
|
|
|
5177
5190
|
storeFileFallback(mcpName, auth);
|
|
5178
5191
|
}
|
|
5179
5192
|
function loadAuth(mcpName) {
|
|
5180
|
-
|
|
5181
|
-
if (process.platform
|
|
5182
|
-
|
|
5183
|
-
|
|
5184
|
-
|
|
5193
|
+
const fromFile = parseBundle(readFileFallback(mcpName));
|
|
5194
|
+
if (process.platform !== "darwin") return fromFile;
|
|
5195
|
+
const fromKeychain = parseBundle(readMacOSKeychain(mcpName));
|
|
5196
|
+
if (!fromKeychain) return fromFile;
|
|
5197
|
+
return fromFile && isSameSignIn(fromFile, fromKeychain) && fromFile.expiresAt > fromKeychain.expiresAt ? fromFile : fromKeychain;
|
|
5198
|
+
}
|
|
5199
|
+
function parseBundle(blob) {
|
|
5185
5200
|
if (!blob) return null;
|
|
5186
5201
|
try {
|
|
5187
5202
|
const parsed = JSON.parse(blob);
|
|
@@ -5193,6 +5208,9 @@ function loadAuth(mcpName) {
|
|
|
5193
5208
|
return null;
|
|
5194
5209
|
}
|
|
5195
5210
|
}
|
|
5211
|
+
function isSameSignIn(a, b) {
|
|
5212
|
+
return a.logtoEndpoint === b.logtoEndpoint && a.cliAppId === b.cliAppId && a.logtoResource === b.logtoResource;
|
|
5213
|
+
}
|
|
5196
5214
|
function clearAuth(mcpName) {
|
|
5197
5215
|
if (process.platform === "darwin") {
|
|
5198
5216
|
try {
|
|
@@ -5213,24 +5231,11 @@ function clearAuth(mcpName) {
|
|
|
5213
5231
|
removeFileFallback(mcpName);
|
|
5214
5232
|
}
|
|
5215
5233
|
function storeMacOSKeychain(mcpName, blob) {
|
|
5216
|
-
try {
|
|
5217
|
-
execFileSync(
|
|
5218
|
-
"security",
|
|
5219
|
-
[
|
|
5220
|
-
"delete-generic-password",
|
|
5221
|
-
"-s",
|
|
5222
|
-
KEYCHAIN_SERVICE,
|
|
5223
|
-
"-a",
|
|
5224
|
-
accountFor(mcpName)
|
|
5225
|
-
],
|
|
5226
|
-
{ stdio: "ignore" }
|
|
5227
|
-
);
|
|
5228
|
-
} catch {
|
|
5229
|
-
}
|
|
5230
5234
|
execFileSync(
|
|
5231
5235
|
"security",
|
|
5232
5236
|
[
|
|
5233
5237
|
"add-generic-password",
|
|
5238
|
+
"-U",
|
|
5234
5239
|
"-s",
|
|
5235
5240
|
KEYCHAIN_SERVICE,
|
|
5236
5241
|
"-a",
|
|
@@ -5238,7 +5243,10 @@ function storeMacOSKeychain(mcpName, blob) {
|
|
|
5238
5243
|
"-w",
|
|
5239
5244
|
blob
|
|
5240
5245
|
],
|
|
5241
|
-
|
|
5246
|
+
// A keychain whose dialog nobody answers hangs every client. Logto has
|
|
5247
|
+
// already consumed the previous refresh token: past this, the renewed
|
|
5248
|
+
// bundle goes to the file rather than wait with the hook.
|
|
5249
|
+
{ stdio: "ignore", timeout: 3e3 }
|
|
5242
5250
|
);
|
|
5243
5251
|
}
|
|
5244
5252
|
function readMacOSKeychain(mcpName) {
|
|
@@ -5333,6 +5341,110 @@ function removeFileFallback(mcpName, replacedBy) {
|
|
|
5333
5341
|
}
|
|
5334
5342
|
}
|
|
5335
5343
|
|
|
5344
|
+
// src/auth/refresh-lock.ts
|
|
5345
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
5346
|
+
import {
|
|
5347
|
+
closeSync,
|
|
5348
|
+
mkdirSync as mkdirSync2,
|
|
5349
|
+
openSync,
|
|
5350
|
+
readFileSync as readFileSync2,
|
|
5351
|
+
statSync,
|
|
5352
|
+
unlinkSync,
|
|
5353
|
+
writeSync
|
|
5354
|
+
} from "node:fs";
|
|
5355
|
+
import { homedir as homedir3 } from "node:os";
|
|
5356
|
+
import { dirname, join as join2 } from "node:path";
|
|
5357
|
+
var WAIT_MS = 5e3;
|
|
5358
|
+
var POLL_MS = 50;
|
|
5359
|
+
var STALE_AFTER_MS = 3e4;
|
|
5360
|
+
var RefreshLockBusyError = class extends Error {
|
|
5361
|
+
constructor() {
|
|
5362
|
+
super("Another process is refreshing the token.");
|
|
5363
|
+
this.name = "RefreshLockBusyError";
|
|
5364
|
+
}
|
|
5365
|
+
};
|
|
5366
|
+
function refreshLockPath(mcpName) {
|
|
5367
|
+
const digest = createHash("sha256").update(mcpName).digest("hex").slice(0, 16);
|
|
5368
|
+
return join2(homedir3(), ".cybedefend", `refresh-${digest}.lock`);
|
|
5369
|
+
}
|
|
5370
|
+
async function withRefreshLock(mcpName, body, options = {}) {
|
|
5371
|
+
const path = refreshLockPath(mcpName);
|
|
5372
|
+
const deadline = Date.now() + (options.waitMs ?? WAIT_MS);
|
|
5373
|
+
for (; ; ) {
|
|
5374
|
+
const attempt = tryAcquire(path);
|
|
5375
|
+
if (attempt === "unavailable") return body();
|
|
5376
|
+
if (attempt !== "held") {
|
|
5377
|
+
try {
|
|
5378
|
+
return await body();
|
|
5379
|
+
} finally {
|
|
5380
|
+
release(path, attempt);
|
|
5381
|
+
}
|
|
5382
|
+
}
|
|
5383
|
+
if (isAbandoned(path)) {
|
|
5384
|
+
removeQuietly(path);
|
|
5385
|
+
continue;
|
|
5386
|
+
}
|
|
5387
|
+
if (Date.now() >= deadline) throw new RefreshLockBusyError();
|
|
5388
|
+
await new Promise((resolve2) => setTimeout(resolve2, options.pollMs ?? POLL_MS));
|
|
5389
|
+
}
|
|
5390
|
+
}
|
|
5391
|
+
function tryAcquire(path) {
|
|
5392
|
+
let handle;
|
|
5393
|
+
try {
|
|
5394
|
+
mkdirSync2(dirname(path), { recursive: true });
|
|
5395
|
+
} catch {
|
|
5396
|
+
return "unavailable";
|
|
5397
|
+
}
|
|
5398
|
+
try {
|
|
5399
|
+
handle = openSync(path, "wx", 384);
|
|
5400
|
+
} catch (e) {
|
|
5401
|
+
return e.code === "EEXIST" ? "held" : "unavailable";
|
|
5402
|
+
}
|
|
5403
|
+
const token = randomUUID();
|
|
5404
|
+
try {
|
|
5405
|
+
writeSync(handle, JSON.stringify({ pid: process.pid, token }));
|
|
5406
|
+
} catch {
|
|
5407
|
+
} finally {
|
|
5408
|
+
closeSync(handle);
|
|
5409
|
+
}
|
|
5410
|
+
return token;
|
|
5411
|
+
}
|
|
5412
|
+
function isAbandoned(path) {
|
|
5413
|
+
let age;
|
|
5414
|
+
try {
|
|
5415
|
+
age = Date.now() - statSync(path).mtimeMs;
|
|
5416
|
+
} catch {
|
|
5417
|
+
return false;
|
|
5418
|
+
}
|
|
5419
|
+
if (age >= STALE_AFTER_MS) return true;
|
|
5420
|
+
const pid = holder(path)?.pid;
|
|
5421
|
+
return typeof pid === "number" && !isAlive(pid);
|
|
5422
|
+
}
|
|
5423
|
+
function holder(path) {
|
|
5424
|
+
try {
|
|
5425
|
+
return JSON.parse(readFileSync2(path, "utf8"));
|
|
5426
|
+
} catch {
|
|
5427
|
+
return null;
|
|
5428
|
+
}
|
|
5429
|
+
}
|
|
5430
|
+
function isAlive(pid) {
|
|
5431
|
+
try {
|
|
5432
|
+
process.kill(pid, 0);
|
|
5433
|
+
return true;
|
|
5434
|
+
} catch (e) {
|
|
5435
|
+
return e.code !== "ESRCH";
|
|
5436
|
+
}
|
|
5437
|
+
}
|
|
5438
|
+
function release(path, token) {
|
|
5439
|
+
if (holder(path)?.token === token) removeQuietly(path);
|
|
5440
|
+
}
|
|
5441
|
+
function removeQuietly(path) {
|
|
5442
|
+
try {
|
|
5443
|
+
unlinkSync(path);
|
|
5444
|
+
} catch {
|
|
5445
|
+
}
|
|
5446
|
+
}
|
|
5447
|
+
|
|
5336
5448
|
// src/auth/token-exchange.ts
|
|
5337
5449
|
var RefreshTokenInvalidError = class extends Error {
|
|
5338
5450
|
constructor(msg) {
|
|
@@ -5391,12 +5503,28 @@ async function getValidAccessToken(mcpName) {
|
|
|
5391
5503
|
);
|
|
5392
5504
|
}
|
|
5393
5505
|
if (!inFlightRefresh) {
|
|
5394
|
-
inFlightRefresh =
|
|
5506
|
+
inFlightRefresh = refreshUnderLock(mcpName).finally(() => {
|
|
5395
5507
|
inFlightRefresh = null;
|
|
5396
5508
|
});
|
|
5397
5509
|
}
|
|
5398
5510
|
return await inFlightRefresh;
|
|
5399
5511
|
}
|
|
5512
|
+
function hasUsableStoredAuth(mcpName, now = Date.now()) {
|
|
5513
|
+
const stored = loadAuth(mcpName);
|
|
5514
|
+
if (!stored) return false;
|
|
5515
|
+
if (stored.refreshToken) return true;
|
|
5516
|
+
return typeof stored.expiresAt === "number" && stored.expiresAt > now;
|
|
5517
|
+
}
|
|
5518
|
+
function refreshUnderLock(mcpName) {
|
|
5519
|
+
return withRefreshLock(mcpName, async () => {
|
|
5520
|
+
const current = loadAuth(mcpName);
|
|
5521
|
+
if (!current) {
|
|
5522
|
+
throw new LoginRequiredError("No stored credentials. Run `vibedefend login`.");
|
|
5523
|
+
}
|
|
5524
|
+
if (Date.now() < current.expiresAt) return current.accessToken;
|
|
5525
|
+
return doRefresh(mcpName, current);
|
|
5526
|
+
});
|
|
5527
|
+
}
|
|
5400
5528
|
async function doRefresh(mcpName, stored) {
|
|
5401
5529
|
if (!stored.refreshToken) {
|
|
5402
5530
|
throw new LoginRequiredError("No refresh_token available; re-login required.");
|
|
@@ -5421,6 +5549,11 @@ async function doRefresh(mcpName, stored) {
|
|
|
5421
5549
|
return updated.accessToken;
|
|
5422
5550
|
} catch (e) {
|
|
5423
5551
|
if (e instanceof RefreshTokenInvalidError) {
|
|
5552
|
+
const latest = loadAuth(mcpName);
|
|
5553
|
+
if (latest && latest.refreshToken !== stored.refreshToken) {
|
|
5554
|
+
if (Date.now() < latest.expiresAt) return latest.accessToken;
|
|
5555
|
+
throw e;
|
|
5556
|
+
}
|
|
5424
5557
|
clearAuth(mcpName);
|
|
5425
5558
|
throw new LoginRequiredError(
|
|
5426
5559
|
"Your session has expired. Run `vibedefend login` to re-authenticate."
|
|
@@ -5535,13 +5668,13 @@ async function resolveProjectByRepository(apiBase, token, q, deps = {}) {
|
|
|
5535
5668
|
}
|
|
5536
5669
|
|
|
5537
5670
|
// src/hooks/runtime/repo-link-cache.ts
|
|
5538
|
-
import { existsSync as existsSync3, mkdirSync as
|
|
5539
|
-
import { dirname as
|
|
5671
|
+
import { existsSync as existsSync3, mkdirSync as mkdirSync4, readFileSync as readFileSync4, writeFileSync as writeFileSync3 } from "node:fs";
|
|
5672
|
+
import { dirname as dirname3 } from "node:path";
|
|
5540
5673
|
|
|
5541
5674
|
// src/utils.ts
|
|
5542
|
-
import { existsSync as existsSync2, mkdirSync as
|
|
5543
|
-
import { dirname, join as
|
|
5544
|
-
import { homedir as
|
|
5675
|
+
import { existsSync as existsSync2, mkdirSync as mkdirSync3, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "node:fs";
|
|
5676
|
+
import { dirname as dirname2, join as join3 } from "node:path";
|
|
5677
|
+
import { homedir as homedir4, platform } from "node:os";
|
|
5545
5678
|
|
|
5546
5679
|
// node_modules/.pnpm/kleur@4.1.5/node_modules/kleur/index.mjs
|
|
5547
5680
|
var FORCE_COLOR;
|
|
@@ -5644,11 +5777,11 @@ function init(open2, close) {
|
|
|
5644
5777
|
|
|
5645
5778
|
// src/utils.ts
|
|
5646
5779
|
function home(...parts) {
|
|
5647
|
-
return
|
|
5780
|
+
return join3(homedir4(), ...parts);
|
|
5648
5781
|
}
|
|
5649
5782
|
function ensureDirFor(filePath) {
|
|
5650
|
-
const dir =
|
|
5651
|
-
if (!existsSync2(dir))
|
|
5783
|
+
const dir = dirname2(filePath);
|
|
5784
|
+
if (!existsSync2(dir)) mkdirSync3(dir, { recursive: true });
|
|
5652
5785
|
}
|
|
5653
5786
|
|
|
5654
5787
|
// src/hooks/runtime/repo-link-cache.ts
|
|
@@ -5664,7 +5797,7 @@ function readRepoLinkCache(deps = {}) {
|
|
|
5664
5797
|
const p = pathOf(deps);
|
|
5665
5798
|
if (!existsSync3(p)) return {};
|
|
5666
5799
|
try {
|
|
5667
|
-
const raw =
|
|
5800
|
+
const raw = readFileSync4(p, "utf8");
|
|
5668
5801
|
if (!raw.trim()) return {};
|
|
5669
5802
|
const parsed = JSON.parse(raw);
|
|
5670
5803
|
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
@@ -5710,25 +5843,25 @@ function writeCachedRepoLink(key, entry, deps = {}) {
|
|
|
5710
5843
|
resolvedAt: now()
|
|
5711
5844
|
};
|
|
5712
5845
|
const p = pathOf(deps);
|
|
5713
|
-
const dir =
|
|
5714
|
-
if (!existsSync3(dir))
|
|
5846
|
+
const dir = dirname3(p);
|
|
5847
|
+
if (!existsSync3(dir)) mkdirSync4(dir, { recursive: true });
|
|
5715
5848
|
writeFileSync3(p, JSON.stringify(map2, null, 2) + "\n");
|
|
5716
5849
|
} catch {
|
|
5717
5850
|
}
|
|
5718
5851
|
}
|
|
5719
5852
|
|
|
5720
5853
|
// src/hooks/runtime/config.ts
|
|
5721
|
-
import { existsSync as existsSync4, readFileSync as
|
|
5722
|
-
import { homedir as
|
|
5723
|
-
import { join as
|
|
5854
|
+
import { existsSync as existsSync4, readFileSync as readFileSync5 } from "node:fs";
|
|
5855
|
+
import { homedir as homedir5 } from "node:os";
|
|
5856
|
+
import { join as join4 } from "node:path";
|
|
5724
5857
|
function runtimeConfigPath() {
|
|
5725
|
-
return
|
|
5858
|
+
return join4(homedir5(), ".cybedefend", "runtime-config.json");
|
|
5726
5859
|
}
|
|
5727
5860
|
function loadRuntimeConfig(path) {
|
|
5728
5861
|
const p = path ?? runtimeConfigPath();
|
|
5729
5862
|
if (!existsSync4(p)) return null;
|
|
5730
5863
|
try {
|
|
5731
|
-
const raw =
|
|
5864
|
+
const raw = readFileSync5(p, "utf8");
|
|
5732
5865
|
return JSON.parse(raw);
|
|
5733
5866
|
} catch {
|
|
5734
5867
|
return null;
|
|
@@ -5750,9 +5883,9 @@ async function readStdinJson() {
|
|
|
5750
5883
|
}
|
|
5751
5884
|
|
|
5752
5885
|
// src/hooks/runtime/trusted-api-base.ts
|
|
5753
|
-
import { existsSync as existsSync5, readFileSync as
|
|
5754
|
-
import { homedir as
|
|
5755
|
-
import { join as
|
|
5886
|
+
import { existsSync as existsSync5, readFileSync as readFileSync6 } from "node:fs";
|
|
5887
|
+
import { homedir as homedir6 } from "node:os";
|
|
5888
|
+
import { join as join5 } from "node:path";
|
|
5756
5889
|
|
|
5757
5890
|
// src/config.ts
|
|
5758
5891
|
var REGIONS = [
|
|
@@ -5780,7 +5913,7 @@ var REGIONS = [
|
|
|
5780
5913
|
|
|
5781
5914
|
// src/hooks/runtime/trusted-api-base.ts
|
|
5782
5915
|
function userRegionsPath() {
|
|
5783
|
-
return
|
|
5916
|
+
return join5(homedir6(), ".cybedefend", "regions.json");
|
|
5784
5917
|
}
|
|
5785
5918
|
function normalizeOrigin(candidate) {
|
|
5786
5919
|
if (typeof candidate !== "string") return null;
|
|
@@ -5799,7 +5932,7 @@ function normalizeOrigin(candidate) {
|
|
|
5799
5932
|
function readUserRegionApiBases(path) {
|
|
5800
5933
|
try {
|
|
5801
5934
|
if (!existsSync5(path)) return [];
|
|
5802
|
-
const parsed = JSON.parse(
|
|
5935
|
+
const parsed = JSON.parse(readFileSync6(path, "utf8"));
|
|
5803
5936
|
if (!Array.isArray(parsed)) return [];
|
|
5804
5937
|
return parsed.map(
|
|
5805
5938
|
(entry) => entry && typeof entry === "object" ? entry.apiBase : null
|
|
@@ -5868,7 +6001,7 @@ function resolveProjectId(cwd = process.cwd(), deps = {}) {
|
|
|
5868
6001
|
const configPath = `${cwd}/.cybedefend/config.json`;
|
|
5869
6002
|
if (existsSync6(configPath)) {
|
|
5870
6003
|
try {
|
|
5871
|
-
const raw =
|
|
6004
|
+
const raw = readFileSync7(configPath, "utf8");
|
|
5872
6005
|
const cfg = JSON.parse(raw);
|
|
5873
6006
|
return {
|
|
5874
6007
|
projectId: cfg.projectId ?? null,
|
|
@@ -5883,119 +6016,9 @@ function resolveToken(mcpName, opts = {}) {
|
|
|
5883
6016
|
const envToken = process.env.CYBEDEFEND_TOKEN;
|
|
5884
6017
|
if (envToken && envToken.length > 0) return envToken;
|
|
5885
6018
|
const stored = (opts.loadStoredAuth ?? loadAuth)(mcpName);
|
|
5886
|
-
|
|
5887
|
-
return stored.accessToken;
|
|
5888
|
-
}
|
|
5889
|
-
const platform2 = opts.platform ?? process.platform;
|
|
5890
|
-
const readKeychain = opts.readKeychain ?? readMacOSKeychain2;
|
|
5891
|
-
if (platform2 === "darwin") {
|
|
5892
|
-
const now = (opts.now ?? Date.now)();
|
|
5893
|
-
return resolveTokenMacOS(mcpName, opts.mcpUrl, readKeychain, now) ?? resolveTokenCodexMacOS(mcpName, opts.mcpUrl, readKeychain, now);
|
|
5894
|
-
}
|
|
5895
|
-
if (platform2 === "linux") return resolveTokenLinux(mcpName);
|
|
5896
|
-
return null;
|
|
5897
|
-
}
|
|
5898
|
-
function codexCredentialAccount(mcpName, mcpUrl) {
|
|
5899
|
-
const digest = createHash("sha256").update(JSON.stringify({ type: "http", url: mcpUrl, headers: {} })).digest("hex");
|
|
5900
|
-
return `${mcpName}|${digest.slice(0, 16)}`;
|
|
5901
|
-
}
|
|
5902
|
-
function isExpired(expiresAt, now) {
|
|
5903
|
-
if (typeof expiresAt !== "number" || !Number.isFinite(expiresAt) || expiresAt <= 0) {
|
|
5904
|
-
return false;
|
|
5905
|
-
}
|
|
5906
|
-
const expiresAtMs = expiresAt < 1e11 ? expiresAt * 1e3 : expiresAt;
|
|
5907
|
-
return now >= expiresAtMs;
|
|
5908
|
-
}
|
|
5909
|
-
function readMacOSKeychain2(service, account) {
|
|
5910
|
-
try {
|
|
5911
|
-
const blob = execFileSync2(
|
|
5912
|
-
"security",
|
|
5913
|
-
[
|
|
5914
|
-
"find-generic-password",
|
|
5915
|
-
"-s",
|
|
5916
|
-
service,
|
|
5917
|
-
...account === void 0 ? [] : ["-a", account],
|
|
5918
|
-
"-w"
|
|
5919
|
-
],
|
|
5920
|
-
{
|
|
5921
|
-
encoding: "utf8",
|
|
5922
|
-
stdio: ["ignore", "pipe", "ignore"],
|
|
5923
|
-
timeout: 2e3
|
|
5924
|
-
}
|
|
5925
|
-
).trim();
|
|
5926
|
-
return blob.length > 0 ? blob : null;
|
|
5927
|
-
} catch {
|
|
5928
|
-
return null;
|
|
5929
|
-
}
|
|
5930
|
-
}
|
|
5931
|
-
function resolveTokenMacOS(mcpName, mcpUrl, readKeychain, now) {
|
|
5932
|
-
const blob = readKeychain("Claude Code-credentials");
|
|
5933
|
-
if (!blob) return null;
|
|
5934
|
-
try {
|
|
5935
|
-
const data = JSON.parse(blob);
|
|
5936
|
-
const oauth = data.mcpOAuth ?? {};
|
|
5937
|
-
for (const entry of Object.values(oauth)) {
|
|
5938
|
-
if (entry?.serverName !== mcpName || !entry.accessToken) continue;
|
|
5939
|
-
if (mcpUrl && typeof entry.serverUrl === "string" && entry.serverUrl !== mcpUrl) {
|
|
5940
|
-
continue;
|
|
5941
|
-
}
|
|
5942
|
-
if (isExpired(entry.expiresAt, now)) {
|
|
5943
|
-
continue;
|
|
5944
|
-
}
|
|
5945
|
-
return entry.accessToken;
|
|
5946
|
-
}
|
|
5947
|
-
} catch {
|
|
5948
|
-
return blob || null;
|
|
5949
|
-
}
|
|
5950
|
-
return null;
|
|
5951
|
-
}
|
|
5952
|
-
function resolveTokenCodexMacOS(mcpName, mcpUrl, readKeychain, now) {
|
|
5953
|
-
const accounts = mcpUrl ? [codexCredentialAccount(mcpName, mcpUrl), mcpName] : [mcpName];
|
|
5954
|
-
for (const account of accounts) {
|
|
5955
|
-
const blob = readKeychain("Codex MCP Credentials", account);
|
|
5956
|
-
if (!blob) continue;
|
|
5957
|
-
const token = parseCodexCredential(blob, now);
|
|
5958
|
-
if (token) return token;
|
|
5959
|
-
}
|
|
5960
|
-
return null;
|
|
5961
|
-
}
|
|
5962
|
-
function parseCodexCredential(blob, now) {
|
|
5963
|
-
try {
|
|
5964
|
-
const data = JSON.parse(blob);
|
|
5965
|
-
if (isExpired(data.expires_at ?? data.expiresAt, now)) return null;
|
|
5966
|
-
return data.token_response?.access_token ?? data.access_token ?? data.accessToken ?? data.token ?? null;
|
|
5967
|
-
} catch {
|
|
5968
|
-
return blob || null;
|
|
5969
|
-
}
|
|
5970
|
-
}
|
|
5971
|
-
function resolveTokenLinux(mcpName) {
|
|
5972
|
-
try {
|
|
5973
|
-
const token = execFileSync2(
|
|
5974
|
-
"secret-tool",
|
|
5975
|
-
["lookup", "service", "Claude-Code", "account", mcpName],
|
|
5976
|
-
{
|
|
5977
|
-
encoding: "utf8",
|
|
5978
|
-
stdio: ["ignore", "pipe", "ignore"],
|
|
5979
|
-
timeout: 2e3
|
|
5980
|
-
}
|
|
5981
|
-
).trim();
|
|
5982
|
-
return token.length > 0 ? token : null;
|
|
5983
|
-
} catch {
|
|
5984
|
-
return null;
|
|
5985
|
-
}
|
|
5986
|
-
}
|
|
5987
|
-
function isExpiredJwt(token) {
|
|
5988
|
-
const payload = token.split(".")[1];
|
|
5989
|
-
if (!payload) return false;
|
|
5990
|
-
try {
|
|
5991
|
-
const json2 = Buffer.from(payload, "base64url").toString("utf8");
|
|
5992
|
-
const exp = JSON.parse(json2).exp;
|
|
5993
|
-
return typeof exp === "number" && exp * 1e3 <= Date.now();
|
|
5994
|
-
} catch {
|
|
5995
|
-
return false;
|
|
5996
|
-
}
|
|
6019
|
+
return stored && (opts.now ?? Date.now)() < stored.expiresAt ? stored.accessToken : null;
|
|
5997
6020
|
}
|
|
5998
|
-
async function resolveTokenWithRefresh(mcpName, deps = {}
|
|
6021
|
+
async function resolveTokenWithRefresh(mcpName, deps = {}) {
|
|
5999
6022
|
const getValidAccessTokenFn = deps.getValidAccessToken ?? getValidAccessToken;
|
|
6000
6023
|
const resolveTokenFn = deps.resolveTokenFn ?? resolveToken;
|
|
6001
6024
|
const envToken = process.env.CYBEDEFEND_TOKEN;
|
|
@@ -6004,19 +6027,9 @@ async function resolveTokenWithRefresh(mcpName, deps = {}, opts = {}) {
|
|
|
6004
6027
|
const token = await getValidAccessTokenFn(mcpName);
|
|
6005
6028
|
if (token) return token;
|
|
6006
6029
|
} catch (e) {
|
|
6007
|
-
if (e instanceof LoginRequiredError)
|
|
6008
|
-
return resolveTokenFn(mcpName, opts);
|
|
6009
|
-
}
|
|
6030
|
+
if (e instanceof LoginRequiredError) return null;
|
|
6010
6031
|
}
|
|
6011
|
-
|
|
6012
|
-
if (legacy && isExpiredJwt(legacy)) {
|
|
6013
|
-
process.stderr.write(
|
|
6014
|
-
`[vibedefend] the stored credential for "${mcpName}" has expired and cannot be refreshed (no refresh token in the legacy store). Run \`vibedefend login\`.
|
|
6015
|
-
`
|
|
6016
|
-
);
|
|
6017
|
-
return null;
|
|
6018
|
-
}
|
|
6019
|
-
return legacy;
|
|
6032
|
+
return resolveTokenFn(mcpName);
|
|
6020
6033
|
}
|
|
6021
6034
|
var RESOLVE_API_TIMEOUT_MS = 4e3;
|
|
6022
6035
|
function resolveActivationMode(config2) {
|
|
@@ -6085,9 +6098,9 @@ async function resolveProjectIdWithApi(cwd = process.cwd(), deps = {}) {
|
|
|
6085
6098
|
}
|
|
6086
6099
|
|
|
6087
6100
|
// src/hooks/runtime/usage-block.ts
|
|
6088
|
-
import { existsSync as existsSync7, mkdirSync as
|
|
6089
|
-
import { homedir as
|
|
6090
|
-
import { dirname as
|
|
6101
|
+
import { existsSync as existsSync7, mkdirSync as mkdirSync5, readFileSync as readFileSync8, writeFileSync as writeFileSync4 } from "node:fs";
|
|
6102
|
+
import { homedir as homedir7 } from "node:os";
|
|
6103
|
+
import { dirname as dirname4, join as join6 } from "node:path";
|
|
6091
6104
|
var BLOCKED_CODE = "VIBEDEFEND_BLOCKED";
|
|
6092
6105
|
var USAGE_WARNING_HEADER = "x-vibedefend-usage-warning";
|
|
6093
6106
|
var UNITS = ["injection", "judgment", "diff_scan"];
|
|
@@ -6184,8 +6197,8 @@ function blockKey(block) {
|
|
|
6184
6197
|
}
|
|
6185
6198
|
var NOTICES_CAP = 300;
|
|
6186
6199
|
function defaultUsageNoticesFile() {
|
|
6187
|
-
const dir = process.env.CYBEDEFEND_BASELINE_DIR ??
|
|
6188
|
-
return
|
|
6200
|
+
const dir = process.env.CYBEDEFEND_BASELINE_DIR ?? join6(homedir7(), ".cybedefend");
|
|
6201
|
+
return join6(dir, "usage-notices-sessions.json");
|
|
6189
6202
|
}
|
|
6190
6203
|
function makeUsageNotifier(opts = {}) {
|
|
6191
6204
|
const file2 = opts.file ?? defaultUsageNoticesFile();
|
|
@@ -6198,7 +6211,7 @@ function makeUsageNotifier(opts = {}) {
|
|
|
6198
6211
|
function load2() {
|
|
6199
6212
|
try {
|
|
6200
6213
|
if (!existsSync7(file2)) return [];
|
|
6201
|
-
const data = JSON.parse(
|
|
6214
|
+
const data = JSON.parse(readFileSync8(file2, "utf8"));
|
|
6202
6215
|
return Array.isArray(data.seen) ? data.seen.filter((s) => typeof s === "string") : [];
|
|
6203
6216
|
} catch {
|
|
6204
6217
|
return [];
|
|
@@ -6210,7 +6223,7 @@ function makeUsageNotifier(opts = {}) {
|
|
|
6210
6223
|
const seen = load2();
|
|
6211
6224
|
if (seen.includes(marker)) return null;
|
|
6212
6225
|
try {
|
|
6213
|
-
|
|
6226
|
+
mkdirSync5(dirname4(file2), { recursive: true });
|
|
6214
6227
|
writeFileSync4(
|
|
6215
6228
|
file2,
|
|
6216
6229
|
JSON.stringify({ seen: [...seen, marker].slice(-NOTICES_CAP) }),
|
|
@@ -6344,13 +6357,13 @@ async function fetchProposalsCount(opts) {
|
|
|
6344
6357
|
}
|
|
6345
6358
|
|
|
6346
6359
|
// src/hooks/runtime/session-activation.ts
|
|
6347
|
-
import { existsSync as existsSync8, mkdirSync as
|
|
6348
|
-
import { homedir as
|
|
6349
|
-
import { dirname as
|
|
6360
|
+
import { existsSync as existsSync8, mkdirSync as mkdirSync6, readFileSync as readFileSync9, writeFileSync as writeFileSync5 } from "node:fs";
|
|
6361
|
+
import { homedir as homedir8 } from "node:os";
|
|
6362
|
+
import { dirname as dirname5, isAbsolute as isAbsolute2, join as join7, resolve as resolvePath2 } from "node:path";
|
|
6350
6363
|
import { fileURLToPath } from "node:url";
|
|
6351
6364
|
|
|
6352
6365
|
// src/hooks/runtime/emit.ts
|
|
6353
|
-
import { writeSync } from "node:fs";
|
|
6366
|
+
import { writeSync as writeSync2 } from "node:fs";
|
|
6354
6367
|
var ENVELOPE_CLIENTS = {
|
|
6355
6368
|
SessionStart: /* @__PURE__ */ new Set(["codex"]),
|
|
6356
6369
|
PreToolUse: /* @__PURE__ */ new Set(["claude-code", "codex", "vscode"]),
|
|
@@ -6376,7 +6389,7 @@ function writeStdoutSync(text) {
|
|
|
6376
6389
|
while (offset < buf.length) {
|
|
6377
6390
|
let written = 0;
|
|
6378
6391
|
try {
|
|
6379
|
-
written =
|
|
6392
|
+
written = writeSync2(1, buf, offset, buf.length - offset);
|
|
6380
6393
|
} catch (e) {
|
|
6381
6394
|
if (e?.code !== "EAGAIN") return;
|
|
6382
6395
|
}
|
|
@@ -6514,14 +6527,14 @@ function isSettledReason(reason) {
|
|
|
6514
6527
|
}
|
|
6515
6528
|
var SESSIONS_CAP = 200;
|
|
6516
6529
|
function defaultStoreFile() {
|
|
6517
|
-
const dir = process.env.CYBEDEFEND_BASELINE_DIR ??
|
|
6518
|
-
return
|
|
6530
|
+
const dir = process.env.CYBEDEFEND_BASELINE_DIR ?? join7(homedir8(), ".cybedefend");
|
|
6531
|
+
return join7(dir, "inactive-sessions.json");
|
|
6519
6532
|
}
|
|
6520
6533
|
function defaultInactiveSessionStore(file2 = defaultStoreFile(), now = Date.now) {
|
|
6521
6534
|
function load2() {
|
|
6522
6535
|
if (!existsSync8(file2)) return {};
|
|
6523
6536
|
try {
|
|
6524
|
-
const parsed = JSON.parse(
|
|
6537
|
+
const parsed = JSON.parse(readFileSync9(file2, "utf8"));
|
|
6525
6538
|
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
|
|
6526
6539
|
} catch {
|
|
6527
6540
|
return {};
|
|
@@ -6529,7 +6542,7 @@ function defaultInactiveSessionStore(file2 = defaultStoreFile(), now = Date.now)
|
|
|
6529
6542
|
}
|
|
6530
6543
|
function save(map2) {
|
|
6531
6544
|
try {
|
|
6532
|
-
|
|
6545
|
+
mkdirSync6(dirname5(file2), { recursive: true });
|
|
6533
6546
|
writeFileSync5(file2, JSON.stringify(map2) + "\n", "utf8");
|
|
6534
6547
|
} catch {
|
|
6535
6548
|
}
|
|
@@ -6646,13 +6659,13 @@ function composeInactiveNotice(reason, repository, client) {
|
|
|
6646
6659
|
}
|
|
6647
6660
|
|
|
6648
6661
|
// src/hooks/runtime/work-dir.ts
|
|
6649
|
-
import { existsSync as existsSync9, realpathSync, statSync } from "node:fs";
|
|
6650
|
-
import { dirname as
|
|
6662
|
+
import { existsSync as existsSync9, realpathSync, statSync as statSync2 } from "node:fs";
|
|
6663
|
+
import { dirname as dirname6, isAbsolute as isAbsolute3, join as join8, resolve } from "node:path";
|
|
6651
6664
|
function findRepoRoot(dir) {
|
|
6652
6665
|
let current = resolve(dir);
|
|
6653
6666
|
for (; ; ) {
|
|
6654
|
-
if (existsSync9(
|
|
6655
|
-
const parent =
|
|
6667
|
+
if (existsSync9(join8(current, ".git"))) return current;
|
|
6668
|
+
const parent = dirname6(current);
|
|
6656
6669
|
if (parent === current) return null;
|
|
6657
6670
|
current = parent;
|
|
6658
6671
|
}
|
|
@@ -6661,10 +6674,10 @@ function nearestExistingDir(path) {
|
|
|
6661
6674
|
let current = resolve(path);
|
|
6662
6675
|
for (; ; ) {
|
|
6663
6676
|
try {
|
|
6664
|
-
if (
|
|
6677
|
+
if (statSync2(current).isDirectory()) return current;
|
|
6665
6678
|
} catch {
|
|
6666
6679
|
}
|
|
6667
|
-
const parent =
|
|
6680
|
+
const parent = dirname6(current);
|
|
6668
6681
|
if (parent === current) return current;
|
|
6669
6682
|
current = parent;
|
|
6670
6683
|
}
|
|
@@ -6679,7 +6692,7 @@ function realPath(p) {
|
|
|
6679
6692
|
function projectDirForFile(file2, workDir) {
|
|
6680
6693
|
if (file2.length === 0 || file2.includes("$")) return workDir;
|
|
6681
6694
|
const absolute = isAbsolute3(file2) ? file2 : resolve(workDir, file2);
|
|
6682
|
-
const fileRoot = findRepoRoot(nearestExistingDir(
|
|
6695
|
+
const fileRoot = findRepoRoot(nearestExistingDir(dirname6(absolute)));
|
|
6683
6696
|
if (fileRoot === null) return workDir;
|
|
6684
6697
|
const workRoot = findRepoRoot(nearestExistingDir(workDir));
|
|
6685
6698
|
if (workRoot !== null && realPath(workRoot) === realPath(fileRoot)) {
|
|
@@ -6691,23 +6704,23 @@ function projectDirForFile(file2, workDir) {
|
|
|
6691
6704
|
// src/hooks/runtime/antigravity-session.ts
|
|
6692
6705
|
import {
|
|
6693
6706
|
appendFileSync,
|
|
6694
|
-
closeSync,
|
|
6707
|
+
closeSync as closeSync2,
|
|
6695
6708
|
constants,
|
|
6696
6709
|
lstatSync,
|
|
6697
|
-
mkdirSync as
|
|
6698
|
-
openSync,
|
|
6710
|
+
mkdirSync as mkdirSync7,
|
|
6711
|
+
openSync as openSync2,
|
|
6699
6712
|
readdirSync,
|
|
6700
|
-
readFileSync as
|
|
6713
|
+
readFileSync as readFileSync10,
|
|
6701
6714
|
renameSync,
|
|
6702
6715
|
rmSync,
|
|
6703
|
-
unlinkSync,
|
|
6716
|
+
unlinkSync as unlinkSync2,
|
|
6704
6717
|
utimesSync,
|
|
6705
6718
|
writeFileSync as writeFileSync6,
|
|
6706
|
-
writeSync as
|
|
6719
|
+
writeSync as writeSync3
|
|
6707
6720
|
} from "node:fs";
|
|
6708
6721
|
import { createHash as createHash2, randomBytes } from "node:crypto";
|
|
6709
|
-
import { homedir as
|
|
6710
|
-
import { join as
|
|
6722
|
+
import { homedir as homedir9 } from "node:os";
|
|
6723
|
+
import { join as join9 } from "node:path";
|
|
6711
6724
|
var MAX_STASHED_CONTEXTS = 20;
|
|
6712
6725
|
var DEFAULT_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1e3;
|
|
6713
6726
|
var STASH_NAME_RE = /^\d+-[0-9a-f]{8}\.md$/;
|
|
@@ -6719,10 +6732,10 @@ function conversationKey(raw) {
|
|
|
6719
6732
|
return createHash2("sha256").update(id).digest("hex").slice(0, 32);
|
|
6720
6733
|
}
|
|
6721
6734
|
function antigravityStateDir() {
|
|
6722
|
-
return
|
|
6735
|
+
return join9(homedir9(), ".cybedefend", "antigravity");
|
|
6723
6736
|
}
|
|
6724
6737
|
function keyDir(key, dir) {
|
|
6725
|
-
return
|
|
6738
|
+
return join9(dir, key);
|
|
6726
6739
|
}
|
|
6727
6740
|
function isFinalStashName(name) {
|
|
6728
6741
|
return STASH_NAME_RE.test(name);
|
|
@@ -6744,15 +6757,15 @@ function isUnsafeFilePath(path) {
|
|
|
6744
6757
|
var NOFOLLOW_SUPPORTED = typeof constants.O_NOFOLLOW === "number";
|
|
6745
6758
|
function appendLedgerLine(path, line) {
|
|
6746
6759
|
if (NOFOLLOW_SUPPORTED) {
|
|
6747
|
-
const fd =
|
|
6760
|
+
const fd = openSync2(
|
|
6748
6761
|
path,
|
|
6749
6762
|
constants.O_WRONLY | constants.O_APPEND | constants.O_CREAT | constants.O_NOFOLLOW,
|
|
6750
6763
|
FILE_MODE
|
|
6751
6764
|
);
|
|
6752
6765
|
try {
|
|
6753
|
-
|
|
6766
|
+
writeSync3(fd, line, null, "utf8");
|
|
6754
6767
|
} finally {
|
|
6755
|
-
|
|
6768
|
+
closeSync2(fd);
|
|
6756
6769
|
}
|
|
6757
6770
|
return;
|
|
6758
6771
|
}
|
|
@@ -6761,15 +6774,15 @@ function appendLedgerLine(path, line) {
|
|
|
6761
6774
|
}
|
|
6762
6775
|
function writeMarkerFile(path, content) {
|
|
6763
6776
|
if (NOFOLLOW_SUPPORTED) {
|
|
6764
|
-
const fd =
|
|
6777
|
+
const fd = openSync2(
|
|
6765
6778
|
path,
|
|
6766
6779
|
constants.O_WRONLY | constants.O_CREAT | constants.O_TRUNC | constants.O_NOFOLLOW,
|
|
6767
6780
|
FILE_MODE
|
|
6768
6781
|
);
|
|
6769
6782
|
try {
|
|
6770
|
-
|
|
6783
|
+
writeSync3(fd, content, null, "utf8");
|
|
6771
6784
|
} finally {
|
|
6772
|
-
|
|
6785
|
+
closeSync2(fd);
|
|
6773
6786
|
}
|
|
6774
6787
|
return;
|
|
6775
6788
|
}
|
|
@@ -6778,7 +6791,7 @@ function writeMarkerFile(path, content) {
|
|
|
6778
6791
|
}
|
|
6779
6792
|
function ensureRealDir(path) {
|
|
6780
6793
|
if (isUnsafePath(path)) return false;
|
|
6781
|
-
|
|
6794
|
+
mkdirSync7(path, { recursive: true, mode: DIR_MODE });
|
|
6782
6795
|
return true;
|
|
6783
6796
|
}
|
|
6784
6797
|
function ensureLiveKeyDir(key, dir, now) {
|
|
@@ -6797,12 +6810,12 @@ function stashContext(key, body, dir = antigravityStateDir()) {
|
|
|
6797
6810
|
const now = Date.now();
|
|
6798
6811
|
const kd = ensureLiveKeyDir(key, dir, now);
|
|
6799
6812
|
if (!kd) return;
|
|
6800
|
-
const contextDir =
|
|
6813
|
+
const contextDir = join9(kd, "context");
|
|
6801
6814
|
if (!ensureRealDir(contextDir)) return;
|
|
6802
6815
|
const suffix = randomBytes(4).toString("hex");
|
|
6803
|
-
tmpPath =
|
|
6816
|
+
tmpPath = join9(contextDir, `.${now}-${suffix}.tmp`);
|
|
6804
6817
|
writeFileSync6(tmpPath, body, { mode: FILE_MODE, flag: "wx" });
|
|
6805
|
-
const finalPath =
|
|
6818
|
+
const finalPath = join9(contextDir, `${now}-${suffix}.md`);
|
|
6806
6819
|
renameSync(tmpPath, finalPath);
|
|
6807
6820
|
tmpPath = void 0;
|
|
6808
6821
|
const kept = readdirSync(contextDir).filter(isFinalStashName).sort();
|
|
@@ -6810,7 +6823,7 @@ function stashContext(key, body, dir = antigravityStateDir()) {
|
|
|
6810
6823
|
if (excess > 0) {
|
|
6811
6824
|
for (const stale of kept.slice(0, excess)) {
|
|
6812
6825
|
try {
|
|
6813
|
-
|
|
6826
|
+
unlinkSync2(join9(contextDir, stale));
|
|
6814
6827
|
} catch {
|
|
6815
6828
|
}
|
|
6816
6829
|
}
|
|
@@ -6818,7 +6831,7 @@ function stashContext(key, body, dir = antigravityStateDir()) {
|
|
|
6818
6831
|
} catch {
|
|
6819
6832
|
if (tmpPath) {
|
|
6820
6833
|
try {
|
|
6821
|
-
|
|
6834
|
+
unlinkSync2(tmpPath);
|
|
6822
6835
|
} catch {
|
|
6823
6836
|
}
|
|
6824
6837
|
}
|
|
@@ -6828,15 +6841,15 @@ function drainContext(key, dir = antigravityStateDir()) {
|
|
|
6828
6841
|
try {
|
|
6829
6842
|
const kd = keyDir(key, dir);
|
|
6830
6843
|
if (isUnsafePath(kd)) return [];
|
|
6831
|
-
const contextDir =
|
|
6844
|
+
const contextDir = join9(kd, "context");
|
|
6832
6845
|
if (isUnsafePath(contextDir)) return [];
|
|
6833
6846
|
const entries = readdirSync(contextDir).filter(isFinalStashName).sort();
|
|
6834
6847
|
const seen = /* @__PURE__ */ new Set();
|
|
6835
6848
|
const bodies = [];
|
|
6836
6849
|
for (const name of entries) {
|
|
6837
|
-
const filePath =
|
|
6850
|
+
const filePath = join9(contextDir, name);
|
|
6838
6851
|
try {
|
|
6839
|
-
const body =
|
|
6852
|
+
const body = readFileSync10(filePath, "utf8");
|
|
6840
6853
|
if (!seen.has(body)) {
|
|
6841
6854
|
seen.add(body);
|
|
6842
6855
|
bodies.push(body);
|
|
@@ -6844,7 +6857,7 @@ function drainContext(key, dir = antigravityStateDir()) {
|
|
|
6844
6857
|
} catch {
|
|
6845
6858
|
}
|
|
6846
6859
|
try {
|
|
6847
|
-
|
|
6860
|
+
unlinkSync2(filePath);
|
|
6848
6861
|
} catch {
|
|
6849
6862
|
}
|
|
6850
6863
|
}
|
|
@@ -6858,7 +6871,7 @@ function recordEdit(key, file2, dir = antigravityStateDir()) {
|
|
|
6858
6871
|
const now = Date.now();
|
|
6859
6872
|
const kd = ensureLiveKeyDir(key, dir, now);
|
|
6860
6873
|
if (!kd) return;
|
|
6861
|
-
appendLedgerLine(
|
|
6874
|
+
appendLedgerLine(join9(kd, "edits.jsonl"), JSON.stringify({ f: file2 }) + "\n");
|
|
6862
6875
|
} catch {
|
|
6863
6876
|
}
|
|
6864
6877
|
}
|
|
@@ -6866,9 +6879,9 @@ function readEdits(key, dir = antigravityStateDir()) {
|
|
|
6866
6879
|
try {
|
|
6867
6880
|
const kd = keyDir(key, dir);
|
|
6868
6881
|
if (isUnsafePath(kd)) return { count: 0, files: [] };
|
|
6869
|
-
const editsPath =
|
|
6882
|
+
const editsPath = join9(kd, "edits.jsonl");
|
|
6870
6883
|
if (isUnsafeFilePath(editsPath)) return { count: 0, files: [] };
|
|
6871
|
-
const content =
|
|
6884
|
+
const content = readFileSync10(editsPath, "utf8");
|
|
6872
6885
|
let count = 0;
|
|
6873
6886
|
const files = [];
|
|
6874
6887
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -6898,7 +6911,7 @@ function markReviewed(key, dir = antigravityStateDir()) {
|
|
|
6898
6911
|
const now = Date.now();
|
|
6899
6912
|
const kd = ensureLiveKeyDir(key, dir, now);
|
|
6900
6913
|
if (!kd) return;
|
|
6901
|
-
writeMarkerFile(
|
|
6914
|
+
writeMarkerFile(join9(kd, "reviewed"), String(now));
|
|
6902
6915
|
} catch {
|
|
6903
6916
|
}
|
|
6904
6917
|
}
|
|
@@ -6906,7 +6919,7 @@ function isReviewed(key, dir = antigravityStateDir()) {
|
|
|
6906
6919
|
try {
|
|
6907
6920
|
const kd = keyDir(key, dir);
|
|
6908
6921
|
if (isUnsafePath(kd)) return false;
|
|
6909
|
-
return lstatSync(
|
|
6922
|
+
return lstatSync(join9(kd, "reviewed")).isFile();
|
|
6910
6923
|
} catch {
|
|
6911
6924
|
return false;
|
|
6912
6925
|
}
|
|
@@ -6915,7 +6928,7 @@ function pruneStale(dir = antigravityStateDir(), now = Date.now(), maxAgeMs = DE
|
|
|
6915
6928
|
try {
|
|
6916
6929
|
const entries = readdirSync(dir);
|
|
6917
6930
|
for (const name of entries) {
|
|
6918
|
-
const entryPath =
|
|
6931
|
+
const entryPath = join9(dir, name);
|
|
6919
6932
|
try {
|
|
6920
6933
|
const { mtimeMs } = lstatSync(entryPath);
|
|
6921
6934
|
if (now - mtimeMs > maxAgeMs) {
|
|
@@ -6980,9 +6993,7 @@ async function fetchRulesHook(deps = {}) {
|
|
|
6980
6993
|
const { projectId, apiBase: configApiBase } = resolution;
|
|
6981
6994
|
if (!projectId) return;
|
|
6982
6995
|
const apiBase = process.env.CYBEDEFEND_API_BASE ?? configApiBase ?? config2.region.apiBase;
|
|
6983
|
-
const token = resolveTok(config2.region.mcpName
|
|
6984
|
-
mcpUrl: config2.region.mcpUrl
|
|
6985
|
-
});
|
|
6996
|
+
const token = resolveTok(config2.region.mcpName);
|
|
6986
6997
|
if (!token) return;
|
|
6987
6998
|
const intent = buildIntent(event);
|
|
6988
6999
|
const fileLabel = files.join(", ");
|
|
@@ -7089,6 +7100,15 @@ function composeSecurityBody(markdown, total, filePath) {
|
|
|
7089
7100
|
].join("\n");
|
|
7090
7101
|
}
|
|
7091
7102
|
|
|
7103
|
+
// src/hooks/runtime/sign-in-notice.ts
|
|
7104
|
+
import { homedir as homedir11 } from "node:os";
|
|
7105
|
+
import { join as join11 } from "node:path";
|
|
7106
|
+
|
|
7107
|
+
// src/hooks/runtime/user-prompt-submit.ts
|
|
7108
|
+
import { existsSync as existsSync10, mkdirSync as mkdirSync8, readFileSync as readFileSync11, writeFileSync as writeFileSync7 } from "node:fs";
|
|
7109
|
+
import { homedir as homedir10 } from "node:os";
|
|
7110
|
+
import { join as join10 } from "node:path";
|
|
7111
|
+
|
|
7092
7112
|
// src/hooks/runtime/proposal-mode.ts
|
|
7093
7113
|
var TRUTHY = /* @__PURE__ */ new Set(["1", "true", "yes", "on"]);
|
|
7094
7114
|
var FALSY = /* @__PURE__ */ new Set(["0", "false", "no", "off"]);
|
|
@@ -7113,11 +7133,144 @@ var DIRECT_PROPOSE_DIRECTIVES = {
|
|
|
7113
7133
|
"non-blocking": `**Non-blocking mode is ON** (autonomous agent \u2014 nobody is in the chat to answer). Do NOT ask the user (no AskUserQuestion). If \u2014 and ONLY if \u2014 one durable, reusable gap stands out, call ${REPORT_MISSING_CALL} directly for that single rule. It lands as a pending proposal that a human accepts or rejects in the CybeDefend dashboard. Otherwise propose nothing.`
|
|
7114
7134
|
};
|
|
7115
7135
|
|
|
7136
|
+
// src/hooks/runtime/user-prompt-submit.ts
|
|
7137
|
+
var SESSIONS_CAP2 = 100;
|
|
7138
|
+
function defaultSessionsFile() {
|
|
7139
|
+
const dir = process.env.CYBEDEFEND_BASELINE_DIR ?? join10(homedir10(), ".cybedefend");
|
|
7140
|
+
return join10(dir, "user-prompt-seen-sessions.json");
|
|
7141
|
+
}
|
|
7142
|
+
function defaultSessionTracker(file2 = defaultSessionsFile()) {
|
|
7143
|
+
function load2() {
|
|
7144
|
+
if (!existsSync10(file2)) return [];
|
|
7145
|
+
try {
|
|
7146
|
+
const raw = readFileSync11(file2, "utf8");
|
|
7147
|
+
const data = JSON.parse(raw);
|
|
7148
|
+
if (!Array.isArray(data.sessions)) return [];
|
|
7149
|
+
return data.sessions.filter((s) => typeof s === "string");
|
|
7150
|
+
} catch {
|
|
7151
|
+
return [];
|
|
7152
|
+
}
|
|
7153
|
+
}
|
|
7154
|
+
return {
|
|
7155
|
+
hasSeen(sessionId) {
|
|
7156
|
+
return load2().includes(sessionId);
|
|
7157
|
+
},
|
|
7158
|
+
async markSeen(sessionId) {
|
|
7159
|
+
const existing = load2();
|
|
7160
|
+
if (existing.includes(sessionId)) return;
|
|
7161
|
+
const next = [...existing, sessionId].slice(-SESSIONS_CAP2);
|
|
7162
|
+
try {
|
|
7163
|
+
mkdirSync8(join10(file2, ".."), { recursive: true });
|
|
7164
|
+
writeFileSync7(file2, JSON.stringify({ sessions: next }), "utf8");
|
|
7165
|
+
} catch {
|
|
7166
|
+
}
|
|
7167
|
+
}
|
|
7168
|
+
};
|
|
7169
|
+
}
|
|
7170
|
+
async function userPromptSubmitHook(deps = {}) {
|
|
7171
|
+
const readStdin = deps.readStdin ?? readStdinJson;
|
|
7172
|
+
const loadConfig = deps.loadConfig ?? loadRuntimeConfig;
|
|
7173
|
+
const resolveProject = deps.resolveProject ?? resolveProjectIdWithApi;
|
|
7174
|
+
const resolveTok = deps.resolveTokenFn ?? resolveToken;
|
|
7175
|
+
const fetchProposals = deps.fetchProposalsFn ?? fetchProposalsCount;
|
|
7176
|
+
const emitFn = deps.emitFn ?? emit;
|
|
7177
|
+
const sessionTracker = deps.sessionTracker ?? defaultSessionTracker();
|
|
7178
|
+
const stdin = await readStdin();
|
|
7179
|
+
const event = sniff(stdin);
|
|
7180
|
+
const sessionId = typeof stdin.session_id === "string" && stdin.session_id.length > 0 ? stdin.session_id : null;
|
|
7181
|
+
if (sessionId !== null && sessionTracker.hasSeen(sessionId)) {
|
|
7182
|
+
return;
|
|
7183
|
+
}
|
|
7184
|
+
const config2 = loadConfig();
|
|
7185
|
+
if (!config2) return;
|
|
7186
|
+
const folder = sessionFolderOf(stdin, event.client);
|
|
7187
|
+
if (isSessionInactive(stdin, { store: deps.sessionStore, cwd: folder })) return;
|
|
7188
|
+
const { projectId, apiBase: configApiBase } = await resolveProject(folder);
|
|
7189
|
+
if (!projectId) return;
|
|
7190
|
+
const apiBase = process.env.CYBEDEFEND_API_BASE ?? configApiBase ?? config2.region.apiBase;
|
|
7191
|
+
const token = resolveTok(config2.region.mcpName);
|
|
7192
|
+
let total = null;
|
|
7193
|
+
if (token) {
|
|
7194
|
+
const result = await fetchProposals({ apiBase, projectId, token });
|
|
7195
|
+
if (result) total = result.total;
|
|
7196
|
+
}
|
|
7197
|
+
const lines = [
|
|
7198
|
+
"## \u26A0\uFE0F CybeDefend doctrine \u2014 execute BEFORE answering this prompt",
|
|
7199
|
+
"",
|
|
7200
|
+
`**Project**: \`${projectId}\``,
|
|
7201
|
+
"",
|
|
7202
|
+
"### Step 0 \u2014 Proposal inbox"
|
|
7203
|
+
];
|
|
7204
|
+
const nonBlocking = isNonBlockingMode(config2);
|
|
7205
|
+
if (nonBlocking) {
|
|
7206
|
+
lines.push(
|
|
7207
|
+
`- CybeDefend: ${total === null ? "proposal inbox not checked" : `**${total}** pending proposals`} \u2014 no action required.`,
|
|
7208
|
+
`- ${NON_BLOCKING_INBOX_DIRECTIVE}`
|
|
7209
|
+
);
|
|
7210
|
+
} else if (total === null) {
|
|
7211
|
+
lines.push(
|
|
7212
|
+
"- CybeDefend: could not fetch proposal-inbox status (token / API unreachable).",
|
|
7213
|
+
'- Call `cybe_rules_list_proposals(projectId, status="proposed")` yourself to verify, then handle each surfaced proposal via AskUserQuestion (Accept / Reject / Skip / Tell me more) before continuing.'
|
|
7214
|
+
);
|
|
7215
|
+
} else if (total > 0) {
|
|
7216
|
+
lines.push(
|
|
7217
|
+
`- CybeDefend: **${total}** pending proposals.`,
|
|
7218
|
+
'- Call `cybe_rules_list_proposals(projectId, status="proposed")` and surface each via AskUserQuestion (Accept / Reject / Skip / Tell me more) BEFORE responding to this prompt.'
|
|
7219
|
+
);
|
|
7220
|
+
} else {
|
|
7221
|
+
lines.push(
|
|
7222
|
+
"- CybeDefend: **0** pending proposals.",
|
|
7223
|
+
'- You MUST still acknowledge step 0 in your reply ("No pending business-rule proposals."). Silently skipping this step is a doctrine violation observed in earlier sessions.'
|
|
7224
|
+
);
|
|
7225
|
+
}
|
|
7226
|
+
lines.push(
|
|
7227
|
+
"",
|
|
7228
|
+
"### Step 1 \u2014 Rules fetch (before any code edit)",
|
|
7229
|
+
"- Even if you plan to brainstorm / explore / ask clarifying questions first, call BOTH:",
|
|
7230
|
+
' - `cybe_rules_fetch(projectId, files=[...], intent="<1-2 sentence narrative>")` for business rules',
|
|
7231
|
+
' - `cybe_secrules_fetch(projectId, files=[...], intent="...", languages=[...])` for security rules',
|
|
7232
|
+
'- Use a rich `intent` \u2014 the retrieval is 60% graph + 40% vector, so a generic placeholder like "Editing file" returns nothing useful.',
|
|
7233
|
+
"- The PreToolUse hook fires automatically as a backstop on Edit/Write/MultiEdit, but your explicit call with a narrative intent gets better matches.",
|
|
7234
|
+
"",
|
|
7235
|
+
"### Order",
|
|
7236
|
+
nonBlocking ? "1. Step 0 needs no action (non-blocking mode)." : "1. Satisfy step 0 (acknowledge / handle the inbox).",
|
|
7237
|
+
"2. Satisfy step 1 (fetch rules with a narrative intent) once you know which files you will touch.",
|
|
7238
|
+
"3. THEN proceed with whatever skill / brainstorm / implementation flow fits the task.",
|
|
7239
|
+
""
|
|
7240
|
+
);
|
|
7241
|
+
emitFn(lines.join("\n"), event.client);
|
|
7242
|
+
if (sessionId !== null) {
|
|
7243
|
+
try {
|
|
7244
|
+
await sessionTracker.markSeen(sessionId);
|
|
7245
|
+
} catch {
|
|
7246
|
+
}
|
|
7247
|
+
}
|
|
7248
|
+
}
|
|
7249
|
+
|
|
7250
|
+
// src/hooks/runtime/sign-in-notice.ts
|
|
7251
|
+
var SIGN_IN_NOTICE = "\u{1F6E1}\uFE0F CybeDefend found no sign-in: Action Guards may not be enforced, and no rules reach your agent. Run `vibedefend login`.";
|
|
7252
|
+
function noticeSessionsFile() {
|
|
7253
|
+
const dir = process.env.CYBEDEFEND_BASELINE_DIR ?? join11(homedir11(), ".cybedefend");
|
|
7254
|
+
return join11(dir, "sign-in-notice-sessions.json");
|
|
7255
|
+
}
|
|
7256
|
+
async function claimSignInNotice(mcpName, sessionId, deps = {}) {
|
|
7257
|
+
if (!sessionId) return false;
|
|
7258
|
+
try {
|
|
7259
|
+
const sessions = defaultSessionTracker(noticeSessionsFile());
|
|
7260
|
+
if (sessions.hasSeen(sessionId)) return false;
|
|
7261
|
+
if ((deps.hasUsableSignIn ?? hasUsableStoredAuth)(mcpName)) return false;
|
|
7262
|
+
await sessions.markSeen(sessionId);
|
|
7263
|
+
return true;
|
|
7264
|
+
} catch {
|
|
7265
|
+
return false;
|
|
7266
|
+
}
|
|
7267
|
+
}
|
|
7268
|
+
|
|
7116
7269
|
// src/hooks/runtime/self-update-check.ts
|
|
7117
7270
|
import { spawn } from "node:child_process";
|
|
7118
|
-
import { existsSync as
|
|
7119
|
-
import { homedir as
|
|
7120
|
-
import { join as
|
|
7271
|
+
import { existsSync as existsSync12, readFileSync as readFileSync12, rmSync as rmSync2, writeFileSync as writeFileSync8 } from "node:fs";
|
|
7272
|
+
import { homedir as homedir12 } from "node:os";
|
|
7273
|
+
import { join as join12 } from "node:path";
|
|
7121
7274
|
|
|
7122
7275
|
// src/hooks/runtime/semver.ts
|
|
7123
7276
|
function parseVersion(input2) {
|
|
@@ -7145,7 +7298,7 @@ function isNewer(latest, current) {
|
|
|
7145
7298
|
|
|
7146
7299
|
// src/self-update.ts
|
|
7147
7300
|
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
7148
|
-
import { existsSync as
|
|
7301
|
+
import { existsSync as existsSync11, realpathSync as realpathSync2 } from "node:fs";
|
|
7149
7302
|
var GLOBAL_MODES = /* @__PURE__ */ new Set([
|
|
7150
7303
|
"npm-global",
|
|
7151
7304
|
"pnpm-global",
|
|
@@ -7173,7 +7326,7 @@ function detectInstallMode(scriptPath) {
|
|
|
7173
7326
|
}
|
|
7174
7327
|
function resolveScriptPath() {
|
|
7175
7328
|
const fromArgv = process.argv[1];
|
|
7176
|
-
if (fromArgv &&
|
|
7329
|
+
if (fromArgv && existsSync11(fromArgv)) {
|
|
7177
7330
|
try {
|
|
7178
7331
|
return realpathSync2(fromArgv);
|
|
7179
7332
|
} catch {
|
|
@@ -7195,9 +7348,9 @@ function isValidRegistryVersion(value) {
|
|
|
7195
7348
|
}
|
|
7196
7349
|
async function selfUpdateCheck(deps) {
|
|
7197
7350
|
const now = deps.now ?? (() => Date.now());
|
|
7198
|
-
const dir = deps.cybeDir ??
|
|
7199
|
-
const throttlePath =
|
|
7200
|
-
const logPath =
|
|
7351
|
+
const dir = deps.cybeDir ?? join12(process.env.CYBEDEFEND_HOME ?? homedir12(), ".cybedefend");
|
|
7352
|
+
const throttlePath = join12(dir, "update-check.json");
|
|
7353
|
+
const logPath = join12(dir, "last-update.log");
|
|
7201
7354
|
const installMode = deps.installMode ?? (deps.runnerSource ? detectInstallMode(deps.runnerSource) : detectInstallMode(resolveScriptPath()));
|
|
7202
7355
|
const priorFailure = takeUpdateLogNotice(logPath);
|
|
7203
7356
|
const throttle = readThrottle(throttlePath);
|
|
@@ -7230,8 +7383,8 @@ function applyOrNotify(deps, installMode, latest, logPath) {
|
|
|
7230
7383
|
}
|
|
7231
7384
|
function readThrottle(path) {
|
|
7232
7385
|
try {
|
|
7233
|
-
if (!
|
|
7234
|
-
const data = JSON.parse(
|
|
7386
|
+
if (!existsSync12(path)) return null;
|
|
7387
|
+
const data = JSON.parse(readFileSync12(path, "utf8"));
|
|
7235
7388
|
if (typeof data.lastCheckedAt !== "number") return null;
|
|
7236
7389
|
return { lastCheckedAt: data.lastCheckedAt };
|
|
7237
7390
|
} catch {
|
|
@@ -7241,16 +7394,16 @@ function readThrottle(path) {
|
|
|
7241
7394
|
function writeThrottle(path, throttle) {
|
|
7242
7395
|
try {
|
|
7243
7396
|
ensureDirFor(path);
|
|
7244
|
-
|
|
7397
|
+
writeFileSync8(path, JSON.stringify(throttle) + "\n");
|
|
7245
7398
|
} catch {
|
|
7246
7399
|
}
|
|
7247
7400
|
}
|
|
7248
7401
|
function takeUpdateLogNotice(logPath) {
|
|
7249
7402
|
try {
|
|
7250
|
-
if (!
|
|
7403
|
+
if (!existsSync12(logPath)) return null;
|
|
7251
7404
|
let parsed = {};
|
|
7252
7405
|
try {
|
|
7253
|
-
parsed = JSON.parse(
|
|
7406
|
+
parsed = JSON.parse(readFileSync12(logPath, "utf8"));
|
|
7254
7407
|
} catch {
|
|
7255
7408
|
parsed = {};
|
|
7256
7409
|
}
|
|
@@ -7325,18 +7478,18 @@ function pmCommand(mode, spec) {
|
|
|
7325
7478
|
|
|
7326
7479
|
// src/hooks/runtime/hook-refresh-trigger.ts
|
|
7327
7480
|
import { spawn as spawn2 } from "node:child_process";
|
|
7328
|
-
import { existsSync as
|
|
7329
|
-
import { homedir as
|
|
7330
|
-
import { dirname as
|
|
7481
|
+
import { existsSync as existsSync13, readFileSync as readFileSync13, writeFileSync as writeFileSync9 } from "node:fs";
|
|
7482
|
+
import { homedir as homedir13 } from "node:os";
|
|
7483
|
+
import { dirname as dirname7, join as join13 } from "node:path";
|
|
7331
7484
|
function maybeRefreshStaleWiring(deps) {
|
|
7332
7485
|
try {
|
|
7333
7486
|
const { config: config2, runnerVersion } = deps;
|
|
7334
7487
|
if (!config2.runnerSource || config2.installedVersion === runnerVersion) {
|
|
7335
7488
|
return false;
|
|
7336
7489
|
}
|
|
7337
|
-
const cliEntry =
|
|
7338
|
-
if (!(deps.exists ??
|
|
7339
|
-
const markerPath = deps.markerPath ??
|
|
7490
|
+
const cliEntry = join13(dirname7(dirname7(config2.runnerSource)), "bin", "vibedefend.js");
|
|
7491
|
+
if (!(deps.exists ?? existsSync13)(cliEntry)) return false;
|
|
7492
|
+
const markerPath = deps.markerPath ?? join13(process.env.CYBEDEFEND_HOME ?? homedir13(), ".cybedefend", "hook-refresh.json");
|
|
7340
7493
|
if (readMarkerVersion(markerPath) === runnerVersion) return false;
|
|
7341
7494
|
(deps.spawnRefresh ?? defaultSpawnRefresh)(cliEntry);
|
|
7342
7495
|
writeMarker(markerPath, runnerVersion);
|
|
@@ -7347,8 +7500,8 @@ function maybeRefreshStaleWiring(deps) {
|
|
|
7347
7500
|
}
|
|
7348
7501
|
function readMarkerVersion(path) {
|
|
7349
7502
|
try {
|
|
7350
|
-
if (!
|
|
7351
|
-
const data = JSON.parse(
|
|
7503
|
+
if (!existsSync13(path)) return null;
|
|
7504
|
+
const data = JSON.parse(readFileSync13(path, "utf8"));
|
|
7352
7505
|
return typeof data.version === "string" ? data.version : null;
|
|
7353
7506
|
} catch {
|
|
7354
7507
|
return null;
|
|
@@ -7357,7 +7510,7 @@ function readMarkerVersion(path) {
|
|
|
7357
7510
|
function writeMarker(path, version2) {
|
|
7358
7511
|
try {
|
|
7359
7512
|
ensureDirFor(path);
|
|
7360
|
-
|
|
7513
|
+
writeFileSync9(path, JSON.stringify({ version: version2, spawnedAt: Date.now() }) + "\n");
|
|
7361
7514
|
} catch {
|
|
7362
7515
|
}
|
|
7363
7516
|
}
|
|
@@ -7371,18 +7524,18 @@ function defaultSpawnRefresh(cliEntry) {
|
|
|
7371
7524
|
}
|
|
7372
7525
|
|
|
7373
7526
|
// src/version.ts
|
|
7374
|
-
import { readFileSync as
|
|
7527
|
+
import { readFileSync as readFileSync14 } from "node:fs";
|
|
7375
7528
|
import { fileURLToPath as fileURLToPath3 } from "node:url";
|
|
7376
|
-
import { dirname as
|
|
7529
|
+
import { dirname as dirname8, join as join14 } from "node:path";
|
|
7377
7530
|
var PKG_NAME = "@cybedefend/vibedefend";
|
|
7378
7531
|
function defaultPkgPath() {
|
|
7379
|
-
return
|
|
7532
|
+
return join14(dirname8(fileURLToPath3(import.meta.url)), "..", "package.json");
|
|
7380
7533
|
}
|
|
7381
7534
|
function readPackageMeta(opts = {}) {
|
|
7382
7535
|
if (typeof opts.bakedVersion === "string" && opts.bakedVersion.length > 0) {
|
|
7383
7536
|
return { name: PKG_NAME, version: opts.bakedVersion };
|
|
7384
7537
|
}
|
|
7385
|
-
const readFile = opts.readFile ?? ((p) =>
|
|
7538
|
+
const readFile = opts.readFile ?? ((p) => readFileSync14(p, "utf8"));
|
|
7386
7539
|
const pkgPath = opts.pkgPath ?? defaultPkgPath();
|
|
7387
7540
|
try {
|
|
7388
7541
|
return JSON.parse(readFile(pkgPath));
|
|
@@ -7391,7 +7544,7 @@ function readPackageMeta(opts = {}) {
|
|
|
7391
7544
|
}
|
|
7392
7545
|
}
|
|
7393
7546
|
var pkg = readPackageMeta({
|
|
7394
|
-
bakedVersion: true ? "1.8.
|
|
7547
|
+
bakedVersion: true ? "1.8.2" : void 0
|
|
7395
7548
|
});
|
|
7396
7549
|
|
|
7397
7550
|
// src/hooks/runtime/session-start.ts
|
|
@@ -7447,9 +7600,7 @@ async function sessionStartHook(deps = {}) {
|
|
|
7447
7600
|
);
|
|
7448
7601
|
return;
|
|
7449
7602
|
}
|
|
7450
|
-
const token = resolveTok(config2.region.mcpName
|
|
7451
|
-
mcpUrl: config2.region.mcpUrl
|
|
7452
|
-
});
|
|
7603
|
+
const token = resolveTok(config2.region.mcpName);
|
|
7453
7604
|
let total = null;
|
|
7454
7605
|
if (token) {
|
|
7455
7606
|
const result = await fetchProposals({ apiBase, projectId, token });
|
|
@@ -7529,27 +7680,41 @@ async function sessionStartHook(deps = {}) {
|
|
|
7529
7680
|
if (notice) lines.push("", notice);
|
|
7530
7681
|
} catch {
|
|
7531
7682
|
}
|
|
7532
|
-
|
|
7683
|
+
const body = lines.join("\n") + "\n";
|
|
7684
|
+
const claimNotice = deps.claimSignInNoticeFn ?? claimSignInNotice;
|
|
7685
|
+
if (!token && await claimNotice(config2.region.mcpName, sessionId)) {
|
|
7686
|
+
emitNoticeFn(
|
|
7687
|
+
{
|
|
7688
|
+
userMessage: SIGN_IN_NOTICE,
|
|
7689
|
+
modelContext: body + (SYSTEM_MESSAGE_CLIENTS.has(event.client) ? "\n- **No sign-in**: the user has been told to run `vibedefend login`; do not repeat it.\n" : `
|
|
7690
|
+
- **No sign-in**: Tell the user once, in one short line: ${SIGN_IN_NOTICE}
|
|
7691
|
+
`)
|
|
7692
|
+
},
|
|
7693
|
+
event.client
|
|
7694
|
+
);
|
|
7695
|
+
return;
|
|
7696
|
+
}
|
|
7697
|
+
emitFn(body, event.client);
|
|
7533
7698
|
}
|
|
7534
7699
|
|
|
7535
7700
|
// src/hooks/runtime/session-review.ts
|
|
7536
|
-
import { existsSync as
|
|
7701
|
+
import { existsSync as existsSync15, readFileSync as readFileSync16 } from "node:fs";
|
|
7537
7702
|
|
|
7538
7703
|
// src/hooks/runtime/review-sessions.ts
|
|
7539
|
-
import { existsSync as
|
|
7540
|
-
import { homedir as
|
|
7541
|
-
import { dirname as
|
|
7542
|
-
var
|
|
7704
|
+
import { existsSync as existsSync14, mkdirSync as mkdirSync9, readFileSync as readFileSync15, writeFileSync as writeFileSync10 } from "node:fs";
|
|
7705
|
+
import { homedir as homedir14 } from "node:os";
|
|
7706
|
+
import { dirname as dirname9, join as join15 } from "node:path";
|
|
7707
|
+
var SESSIONS_CAP3 = 200;
|
|
7543
7708
|
function defaultStoreFile2() {
|
|
7544
|
-
const dir = process.env.CYBEDEFEND_BASELINE_DIR ??
|
|
7545
|
-
return
|
|
7709
|
+
const dir = process.env.CYBEDEFEND_BASELINE_DIR ?? join15(homedir14(), ".cybedefend");
|
|
7710
|
+
return join15(dir, "review-sessions.json");
|
|
7546
7711
|
}
|
|
7547
7712
|
var isCount = (n) => typeof n === "number" && Number.isInteger(n) && n >= 0;
|
|
7548
7713
|
function defaultReviewSessionStore(file2 = defaultStoreFile2(), now = Date.now) {
|
|
7549
7714
|
function load2() {
|
|
7550
|
-
if (!
|
|
7715
|
+
if (!existsSync14(file2)) return {};
|
|
7551
7716
|
try {
|
|
7552
|
-
const parsed = JSON.parse(
|
|
7717
|
+
const parsed = JSON.parse(readFileSync15(file2, "utf8"));
|
|
7553
7718
|
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
|
|
7554
7719
|
} catch {
|
|
7555
7720
|
return {};
|
|
@@ -7557,8 +7722,8 @@ function defaultReviewSessionStore(file2 = defaultStoreFile2(), now = Date.now)
|
|
|
7557
7722
|
}
|
|
7558
7723
|
function save(map2) {
|
|
7559
7724
|
try {
|
|
7560
|
-
|
|
7561
|
-
|
|
7725
|
+
mkdirSync9(dirname9(file2), { recursive: true });
|
|
7726
|
+
writeFileSync10(file2, JSON.stringify(map2) + "\n", "utf8");
|
|
7562
7727
|
} catch {
|
|
7563
7728
|
}
|
|
7564
7729
|
}
|
|
@@ -7573,9 +7738,9 @@ function defaultReviewSessionStore(file2 = defaultStoreFile2(), now = Date.now)
|
|
|
7573
7738
|
const map2 = load2();
|
|
7574
7739
|
map2[sessionId] = { ...record2, at: now() };
|
|
7575
7740
|
const entries = Object.entries(map2);
|
|
7576
|
-
if (entries.length >
|
|
7741
|
+
if (entries.length > SESSIONS_CAP3) {
|
|
7577
7742
|
entries.sort(([, a], [, b]) => (a.at ?? 0) - (b.at ?? 0));
|
|
7578
|
-
save(Object.fromEntries(entries.slice(-
|
|
7743
|
+
save(Object.fromEntries(entries.slice(-SESSIONS_CAP3)));
|
|
7579
7744
|
return;
|
|
7580
7745
|
}
|
|
7581
7746
|
save(map2);
|
|
@@ -7768,9 +7933,9 @@ function loadTranscript(stdin, readFile) {
|
|
|
7768
7933
|
return out;
|
|
7769
7934
|
}
|
|
7770
7935
|
function defaultReadTranscript(path) {
|
|
7771
|
-
if (!
|
|
7936
|
+
if (!existsSync15(path)) return null;
|
|
7772
7937
|
try {
|
|
7773
|
-
return
|
|
7938
|
+
return readFileSync16(path, "utf8");
|
|
7774
7939
|
} catch {
|
|
7775
7940
|
return null;
|
|
7776
7941
|
}
|
|
@@ -8037,125 +8202,6 @@ async function preCompactHook(deps = {}) {
|
|
|
8037
8202
|
emitFn(body, event.client);
|
|
8038
8203
|
}
|
|
8039
8204
|
|
|
8040
|
-
// src/hooks/runtime/user-prompt-submit.ts
|
|
8041
|
-
import { existsSync as existsSync15, mkdirSync as mkdirSync8, readFileSync as readFileSync15, writeFileSync as writeFileSync10 } from "node:fs";
|
|
8042
|
-
import { homedir as homedir12 } from "node:os";
|
|
8043
|
-
import { join as join13 } from "node:path";
|
|
8044
|
-
var SESSIONS_CAP3 = 100;
|
|
8045
|
-
function defaultSessionsFile() {
|
|
8046
|
-
const dir = process.env.CYBEDEFEND_BASELINE_DIR ?? join13(homedir12(), ".cybedefend");
|
|
8047
|
-
return join13(dir, "user-prompt-seen-sessions.json");
|
|
8048
|
-
}
|
|
8049
|
-
function defaultSessionTracker(file2 = defaultSessionsFile()) {
|
|
8050
|
-
function load2() {
|
|
8051
|
-
if (!existsSync15(file2)) return [];
|
|
8052
|
-
try {
|
|
8053
|
-
const raw = readFileSync15(file2, "utf8");
|
|
8054
|
-
const data = JSON.parse(raw);
|
|
8055
|
-
if (!Array.isArray(data.sessions)) return [];
|
|
8056
|
-
return data.sessions.filter((s) => typeof s === "string");
|
|
8057
|
-
} catch {
|
|
8058
|
-
return [];
|
|
8059
|
-
}
|
|
8060
|
-
}
|
|
8061
|
-
return {
|
|
8062
|
-
hasSeen(sessionId) {
|
|
8063
|
-
return load2().includes(sessionId);
|
|
8064
|
-
},
|
|
8065
|
-
async markSeen(sessionId) {
|
|
8066
|
-
const existing = load2();
|
|
8067
|
-
if (existing.includes(sessionId)) return;
|
|
8068
|
-
const next = [...existing, sessionId].slice(-SESSIONS_CAP3);
|
|
8069
|
-
try {
|
|
8070
|
-
mkdirSync8(join13(file2, ".."), { recursive: true });
|
|
8071
|
-
writeFileSync10(file2, JSON.stringify({ sessions: next }), "utf8");
|
|
8072
|
-
} catch {
|
|
8073
|
-
}
|
|
8074
|
-
}
|
|
8075
|
-
};
|
|
8076
|
-
}
|
|
8077
|
-
async function userPromptSubmitHook(deps = {}) {
|
|
8078
|
-
const readStdin = deps.readStdin ?? readStdinJson;
|
|
8079
|
-
const loadConfig = deps.loadConfig ?? loadRuntimeConfig;
|
|
8080
|
-
const resolveProject = deps.resolveProject ?? resolveProjectIdWithApi;
|
|
8081
|
-
const resolveTok = deps.resolveTokenFn ?? resolveToken;
|
|
8082
|
-
const fetchProposals = deps.fetchProposalsFn ?? fetchProposalsCount;
|
|
8083
|
-
const emitFn = deps.emitFn ?? emit;
|
|
8084
|
-
const sessionTracker = deps.sessionTracker ?? defaultSessionTracker();
|
|
8085
|
-
const stdin = await readStdin();
|
|
8086
|
-
const event = sniff(stdin);
|
|
8087
|
-
const sessionId = typeof stdin.session_id === "string" && stdin.session_id.length > 0 ? stdin.session_id : null;
|
|
8088
|
-
if (sessionId !== null && sessionTracker.hasSeen(sessionId)) {
|
|
8089
|
-
return;
|
|
8090
|
-
}
|
|
8091
|
-
const config2 = loadConfig();
|
|
8092
|
-
if (!config2) return;
|
|
8093
|
-
const folder = sessionFolderOf(stdin, event.client);
|
|
8094
|
-
if (isSessionInactive(stdin, { store: deps.sessionStore, cwd: folder })) return;
|
|
8095
|
-
const { projectId, apiBase: configApiBase } = await resolveProject(folder);
|
|
8096
|
-
if (!projectId) return;
|
|
8097
|
-
const apiBase = process.env.CYBEDEFEND_API_BASE ?? configApiBase ?? config2.region.apiBase;
|
|
8098
|
-
const token = resolveTok(config2.region.mcpName, {
|
|
8099
|
-
mcpUrl: config2.region.mcpUrl
|
|
8100
|
-
});
|
|
8101
|
-
let total = null;
|
|
8102
|
-
if (token) {
|
|
8103
|
-
const result = await fetchProposals({ apiBase, projectId, token });
|
|
8104
|
-
if (result) total = result.total;
|
|
8105
|
-
}
|
|
8106
|
-
const lines = [
|
|
8107
|
-
"## \u26A0\uFE0F CybeDefend doctrine \u2014 execute BEFORE answering this prompt",
|
|
8108
|
-
"",
|
|
8109
|
-
`**Project**: \`${projectId}\``,
|
|
8110
|
-
"",
|
|
8111
|
-
"### Step 0 \u2014 Proposal inbox"
|
|
8112
|
-
];
|
|
8113
|
-
const nonBlocking = isNonBlockingMode(config2);
|
|
8114
|
-
if (nonBlocking) {
|
|
8115
|
-
lines.push(
|
|
8116
|
-
`- CybeDefend: ${total === null ? "proposal inbox not checked" : `**${total}** pending proposals`} \u2014 no action required.`,
|
|
8117
|
-
`- ${NON_BLOCKING_INBOX_DIRECTIVE}`
|
|
8118
|
-
);
|
|
8119
|
-
} else if (total === null) {
|
|
8120
|
-
lines.push(
|
|
8121
|
-
"- CybeDefend: could not fetch proposal-inbox status (token / API unreachable).",
|
|
8122
|
-
'- Call `cybe_rules_list_proposals(projectId, status="proposed")` yourself to verify, then handle each surfaced proposal via AskUserQuestion (Accept / Reject / Skip / Tell me more) before continuing.'
|
|
8123
|
-
);
|
|
8124
|
-
} else if (total > 0) {
|
|
8125
|
-
lines.push(
|
|
8126
|
-
`- CybeDefend: **${total}** pending proposals.`,
|
|
8127
|
-
'- Call `cybe_rules_list_proposals(projectId, status="proposed")` and surface each via AskUserQuestion (Accept / Reject / Skip / Tell me more) BEFORE responding to this prompt.'
|
|
8128
|
-
);
|
|
8129
|
-
} else {
|
|
8130
|
-
lines.push(
|
|
8131
|
-
"- CybeDefend: **0** pending proposals.",
|
|
8132
|
-
'- You MUST still acknowledge step 0 in your reply ("No pending business-rule proposals."). Silently skipping this step is a doctrine violation observed in earlier sessions.'
|
|
8133
|
-
);
|
|
8134
|
-
}
|
|
8135
|
-
lines.push(
|
|
8136
|
-
"",
|
|
8137
|
-
"### Step 1 \u2014 Rules fetch (before any code edit)",
|
|
8138
|
-
"- Even if you plan to brainstorm / explore / ask clarifying questions first, call BOTH:",
|
|
8139
|
-
' - `cybe_rules_fetch(projectId, files=[...], intent="<1-2 sentence narrative>")` for business rules',
|
|
8140
|
-
' - `cybe_secrules_fetch(projectId, files=[...], intent="...", languages=[...])` for security rules',
|
|
8141
|
-
'- Use a rich `intent` \u2014 the retrieval is 60% graph + 40% vector, so a generic placeholder like "Editing file" returns nothing useful.',
|
|
8142
|
-
"- The PreToolUse hook fires automatically as a backstop on Edit/Write/MultiEdit, but your explicit call with a narrative intent gets better matches.",
|
|
8143
|
-
"",
|
|
8144
|
-
"### Order",
|
|
8145
|
-
nonBlocking ? "1. Step 0 needs no action (non-blocking mode)." : "1. Satisfy step 0 (acknowledge / handle the inbox).",
|
|
8146
|
-
"2. Satisfy step 1 (fetch rules with a narrative intent) once you know which files you will touch.",
|
|
8147
|
-
"3. THEN proceed with whatever skill / brainstorm / implementation flow fits the task.",
|
|
8148
|
-
""
|
|
8149
|
-
);
|
|
8150
|
-
emitFn(lines.join("\n"), event.client);
|
|
8151
|
-
if (sessionId !== null) {
|
|
8152
|
-
try {
|
|
8153
|
-
await sessionTracker.markSeen(sessionId);
|
|
8154
|
-
} catch {
|
|
8155
|
-
}
|
|
8156
|
-
}
|
|
8157
|
-
}
|
|
8158
|
-
|
|
8159
8205
|
// src/hooks/runtime/pre-invocation.ts
|
|
8160
8206
|
var STASH_SEPARATOR = "\n\n---\n\n";
|
|
8161
8207
|
async function preInvocationHook(deps = {}) {
|
|
@@ -11630,15 +11676,15 @@ function resolveCandidatePaths(rawPath, opts) {
|
|
|
11630
11676
|
// src/hooks/runtime/session-writes.ts
|
|
11631
11677
|
import {
|
|
11632
11678
|
existsSync as existsSync17,
|
|
11633
|
-
mkdirSync as
|
|
11634
|
-
readFileSync as
|
|
11679
|
+
mkdirSync as mkdirSync10,
|
|
11680
|
+
readFileSync as readFileSync18,
|
|
11635
11681
|
renameSync as renameSync2,
|
|
11636
11682
|
rmdirSync,
|
|
11637
|
-
statSync as
|
|
11683
|
+
statSync as statSync4,
|
|
11638
11684
|
writeFileSync as writeFileSync11
|
|
11639
11685
|
} from "node:fs";
|
|
11640
|
-
import { homedir as
|
|
11641
|
-
import { join as
|
|
11686
|
+
import { homedir as homedir15 } from "node:os";
|
|
11687
|
+
import { join as join16, posix } from "node:path";
|
|
11642
11688
|
var DEFAULT_MAX_PATHS = 500;
|
|
11643
11689
|
var DEFAULT_SESSION_WRITES_TTL_MS = 4 * 60 * 60 * 1e3;
|
|
11644
11690
|
var SESSION_WRITES_FILE = "guards-session-writes.json";
|
|
@@ -11652,11 +11698,11 @@ function withLock(lockDir, fn) {
|
|
|
11652
11698
|
let held = false;
|
|
11653
11699
|
while (!held) {
|
|
11654
11700
|
try {
|
|
11655
|
-
|
|
11701
|
+
mkdirSync10(lockDir);
|
|
11656
11702
|
held = true;
|
|
11657
11703
|
} catch {
|
|
11658
11704
|
try {
|
|
11659
|
-
if (Date.now() -
|
|
11705
|
+
if (Date.now() - statSync4(lockDir).mtimeMs > LOCK_STALE_MS) {
|
|
11660
11706
|
rmdirSync(lockDir);
|
|
11661
11707
|
continue;
|
|
11662
11708
|
}
|
|
@@ -11679,7 +11725,7 @@ function withLock(lockDir, fn) {
|
|
|
11679
11725
|
}
|
|
11680
11726
|
}
|
|
11681
11727
|
function defaultDir() {
|
|
11682
|
-
return process.env.CYBEDEFEND_BASELINE_DIR ??
|
|
11728
|
+
return process.env.CYBEDEFEND_BASELINE_DIR ?? join16(homedir15(), ".cybedefend");
|
|
11683
11729
|
}
|
|
11684
11730
|
function normalizePath(p) {
|
|
11685
11731
|
if (typeof p !== "string") return "";
|
|
@@ -11714,14 +11760,14 @@ function coerce(raw) {
|
|
|
11714
11760
|
}
|
|
11715
11761
|
function makeSessionWritesStore(opts = {}) {
|
|
11716
11762
|
const dir = opts.dir ?? defaultDir();
|
|
11717
|
-
const file2 =
|
|
11763
|
+
const file2 = join16(dir, SESSION_WRITES_FILE);
|
|
11718
11764
|
const now = opts.now ?? (() => Date.now());
|
|
11719
11765
|
const maxPaths = typeof opts.maxPaths === "number" && opts.maxPaths > 0 ? Math.floor(opts.maxPaths) : DEFAULT_MAX_PATHS;
|
|
11720
11766
|
const ttlMs = typeof opts.ttlMs === "number" && opts.ttlMs > 0 ? opts.ttlMs : DEFAULT_SESSION_WRITES_TTL_MS;
|
|
11721
11767
|
function readAll() {
|
|
11722
11768
|
if (!existsSync17(file2)) return {};
|
|
11723
11769
|
try {
|
|
11724
|
-
return coerce(JSON.parse(
|
|
11770
|
+
return coerce(JSON.parse(readFileSync18(file2, "utf8")));
|
|
11725
11771
|
} catch {
|
|
11726
11772
|
return {};
|
|
11727
11773
|
}
|
|
@@ -11736,7 +11782,7 @@ function makeSessionWritesStore(opts = {}) {
|
|
|
11736
11782
|
}
|
|
11737
11783
|
function writeAll(all) {
|
|
11738
11784
|
try {
|
|
11739
|
-
if (!existsSync17(dir))
|
|
11785
|
+
if (!existsSync17(dir)) mkdirSync10(dir, { recursive: true });
|
|
11740
11786
|
const tmp = `${file2}.${process.pid}.tmp`;
|
|
11741
11787
|
writeFileSync11(tmp, JSON.stringify(all), "utf8");
|
|
11742
11788
|
renameSync2(tmp, file2);
|
|
@@ -11749,7 +11795,7 @@ function makeSessionWritesStore(opts = {}) {
|
|
|
11749
11795
|
const usable = paths.filter(isUsable);
|
|
11750
11796
|
if (usable.length === 0) return;
|
|
11751
11797
|
try {
|
|
11752
|
-
if (!existsSync17(dir))
|
|
11798
|
+
if (!existsSync17(dir)) mkdirSync10(dir, { recursive: true });
|
|
11753
11799
|
withLock(`${file2}.lock`, () => {
|
|
11754
11800
|
const t = now();
|
|
11755
11801
|
const all = pruneExpired(readAll(), t);
|
|
@@ -12065,10 +12111,10 @@ function nearMiss(req, rule) {
|
|
|
12065
12111
|
import { isAbsolute as isAbsolute6, resolve as resolvePath4 } from "node:path";
|
|
12066
12112
|
|
|
12067
12113
|
// src/hooks/runtime/guard-rules-cache.ts
|
|
12068
|
-
import { readFileSync as
|
|
12069
|
-
import { dirname as
|
|
12070
|
-
import { homedir as
|
|
12071
|
-
import { join as
|
|
12114
|
+
import { readFileSync as readFileSync19, writeFileSync as writeFileSync12, existsSync as existsSync18, mkdirSync as mkdirSync11 } from "node:fs";
|
|
12115
|
+
import { dirname as dirname10 } from "node:path";
|
|
12116
|
+
import { homedir as homedir16 } from "node:os";
|
|
12117
|
+
import { join as join17 } from "node:path";
|
|
12072
12118
|
|
|
12073
12119
|
// node_modules/.pnpm/zod@4.6.7/node_modules/zod/v4/classic/external.js
|
|
12074
12120
|
var external_exports = {};
|
|
@@ -31905,8 +31951,8 @@ function tokenFingerprint(token) {
|
|
|
31905
31951
|
}
|
|
31906
31952
|
|
|
31907
31953
|
// src/hooks/runtime/guard-rules-cache.ts
|
|
31908
|
-
var DEFAULT_CACHE_FILE =
|
|
31909
|
-
|
|
31954
|
+
var DEFAULT_CACHE_FILE = join17(
|
|
31955
|
+
homedir16(),
|
|
31910
31956
|
".cybedefend",
|
|
31911
31957
|
"guards-cache.json"
|
|
31912
31958
|
);
|
|
@@ -31981,7 +32027,7 @@ function reportDroppedRules(dropped) {
|
|
|
31981
32027
|
}
|
|
31982
32028
|
function makeGuardRulesCache(opts) {
|
|
31983
32029
|
const cacheFile = opts.cacheFile ?? DEFAULT_CACHE_FILE;
|
|
31984
|
-
const failuresFile = opts.failuresFile ??
|
|
32030
|
+
const failuresFile = opts.failuresFile ?? join17(dirname10(cacheFile), "guards-cache-failures.json");
|
|
31985
32031
|
const staleSec = opts.staleSec ?? DEFAULT_STALE_SEC;
|
|
31986
32032
|
const fetchFn = opts.fetchFn;
|
|
31987
32033
|
const clock = opts.now ?? Date.now;
|
|
@@ -32086,7 +32132,7 @@ function makeGuardRulesCache(opts) {
|
|
|
32086
32132
|
function readCache(projectId) {
|
|
32087
32133
|
if (!existsSync18(cacheFile)) return null;
|
|
32088
32134
|
try {
|
|
32089
|
-
const raw =
|
|
32135
|
+
const raw = readFileSync19(cacheFile, "utf8");
|
|
32090
32136
|
const data = JSON.parse(raw);
|
|
32091
32137
|
if (data.projectId !== projectId) return null;
|
|
32092
32138
|
return data;
|
|
@@ -32097,7 +32143,7 @@ function makeGuardRulesCache(opts) {
|
|
|
32097
32143
|
function readFailures() {
|
|
32098
32144
|
try {
|
|
32099
32145
|
if (!existsSync18(failuresFile)) return {};
|
|
32100
|
-
const parsed = JSON.parse(
|
|
32146
|
+
const parsed = JSON.parse(readFileSync19(failuresFile, "utf8"));
|
|
32101
32147
|
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
32102
32148
|
return {};
|
|
32103
32149
|
}
|
|
@@ -32137,9 +32183,9 @@ function makeGuardRulesCache(opts) {
|
|
|
32137
32183
|
return { refreshIfStale };
|
|
32138
32184
|
}
|
|
32139
32185
|
function ensureDirFor2(filePath) {
|
|
32140
|
-
const dir =
|
|
32186
|
+
const dir = dirname10(filePath);
|
|
32141
32187
|
if (!existsSync18(dir)) {
|
|
32142
|
-
|
|
32188
|
+
mkdirSync11(dir, { recursive: true });
|
|
32143
32189
|
}
|
|
32144
32190
|
}
|
|
32145
32191
|
var DEFAULT_TIMEOUT_MS2 = 8e3;
|
|
@@ -32279,11 +32325,11 @@ function detectPromptInjection(text) {
|
|
|
32279
32325
|
}
|
|
32280
32326
|
|
|
32281
32327
|
// src/hooks/runtime/judge-cache.ts
|
|
32282
|
-
import { readFileSync as
|
|
32283
|
-
import { dirname as
|
|
32284
|
-
import { homedir as
|
|
32285
|
-
var DEFAULT_JUDGE_CACHE_FILE =
|
|
32286
|
-
|
|
32328
|
+
import { readFileSync as readFileSync20, writeFileSync as writeFileSync13, existsSync as existsSync19, mkdirSync as mkdirSync12 } from "node:fs";
|
|
32329
|
+
import { dirname as dirname11, join as join18 } from "node:path";
|
|
32330
|
+
import { homedir as homedir17 } from "node:os";
|
|
32331
|
+
var DEFAULT_JUDGE_CACHE_FILE = join18(
|
|
32332
|
+
homedir17(),
|
|
32287
32333
|
".cybedefend",
|
|
32288
32334
|
"guards-judge-cache.json"
|
|
32289
32335
|
);
|
|
@@ -32296,7 +32342,7 @@ function makeJudgeCache(opts = {}) {
|
|
|
32296
32342
|
function readAll() {
|
|
32297
32343
|
if (!existsSync19(cacheFile)) return {};
|
|
32298
32344
|
try {
|
|
32299
|
-
return JSON.parse(
|
|
32345
|
+
return JSON.parse(readFileSync20(cacheFile, "utf8"));
|
|
32300
32346
|
} catch {
|
|
32301
32347
|
return {};
|
|
32302
32348
|
}
|
|
@@ -32327,36 +32373,36 @@ function makeJudgeCache(opts = {}) {
|
|
|
32327
32373
|
return { get, set: set2 };
|
|
32328
32374
|
}
|
|
32329
32375
|
function ensureDirFor3(filePath) {
|
|
32330
|
-
const dir =
|
|
32376
|
+
const dir = dirname11(filePath);
|
|
32331
32377
|
if (!existsSync19(dir)) {
|
|
32332
|
-
|
|
32378
|
+
mkdirSync12(dir, { recursive: true });
|
|
32333
32379
|
}
|
|
32334
32380
|
}
|
|
32335
32381
|
|
|
32336
32382
|
// src/hooks/runtime/guard-check.ts
|
|
32337
32383
|
import { spawn as spawn3 } from "node:child_process";
|
|
32338
|
-
import { homedir as
|
|
32339
|
-
import { join as
|
|
32384
|
+
import { homedir as homedir20 } from "node:os";
|
|
32385
|
+
import { join as join21 } from "node:path";
|
|
32340
32386
|
|
|
32341
32387
|
// src/hooks/runtime/guard-violations-buffer.ts
|
|
32342
32388
|
import {
|
|
32343
32389
|
appendFileSync as appendFileSync2,
|
|
32344
|
-
closeSync as
|
|
32390
|
+
closeSync as closeSync4,
|
|
32345
32391
|
existsSync as existsSync20,
|
|
32346
|
-
openSync as
|
|
32347
|
-
readFileSync as
|
|
32392
|
+
openSync as openSync4,
|
|
32393
|
+
readFileSync as readFileSync21,
|
|
32348
32394
|
writeFileSync as writeFileSync14,
|
|
32349
|
-
writeSync as
|
|
32350
|
-
mkdirSync as
|
|
32395
|
+
writeSync as writeSync4,
|
|
32396
|
+
mkdirSync as mkdirSync13,
|
|
32351
32397
|
rmSync as rmSync3,
|
|
32352
|
-
statSync as
|
|
32398
|
+
statSync as statSync5,
|
|
32353
32399
|
utimesSync as utimesSync2
|
|
32354
32400
|
} from "node:fs";
|
|
32355
|
-
import { dirname as
|
|
32356
|
-
import { homedir as
|
|
32357
|
-
import { join as
|
|
32358
|
-
var DEFAULT_BUFFER_FILE =
|
|
32359
|
-
|
|
32401
|
+
import { dirname as dirname12 } from "node:path";
|
|
32402
|
+
import { homedir as homedir18 } from "node:os";
|
|
32403
|
+
import { join as join19 } from "node:path";
|
|
32404
|
+
var DEFAULT_BUFFER_FILE = join19(
|
|
32405
|
+
homedir18(),
|
|
32360
32406
|
".cybedefend",
|
|
32361
32407
|
"guards-violations-buffer.jsonl"
|
|
32362
32408
|
);
|
|
@@ -32367,8 +32413,8 @@ var MAX_RECORD_AGE_MS = 7 * 24 * 60 * 60 * 1e3;
|
|
|
32367
32413
|
var FLUSH_LOCK_STALE_MS = 6e4;
|
|
32368
32414
|
function makeViolationsBuffer(opts = {}) {
|
|
32369
32415
|
const bufferFile = opts.bufferFile ?? DEFAULT_BUFFER_FILE;
|
|
32370
|
-
const holdsFile = opts.holdsFile ??
|
|
32371
|
-
const lockFile = opts.lockFile ??
|
|
32416
|
+
const holdsFile = opts.holdsFile ?? join19(dirname12(bufferFile), "guards-violations-holds.json");
|
|
32417
|
+
const lockFile = opts.lockFile ?? join19(dirname12(bufferFile), "guards-violations-flush.lock");
|
|
32372
32418
|
const now = opts.now ?? Date.now;
|
|
32373
32419
|
function bufferViolation2(ctx, v) {
|
|
32374
32420
|
const record2 = {
|
|
@@ -32482,7 +32528,7 @@ function makeViolationsBuffer(opts = {}) {
|
|
|
32482
32528
|
}
|
|
32483
32529
|
function readBuffer() {
|
|
32484
32530
|
try {
|
|
32485
|
-
const raw =
|
|
32531
|
+
const raw = readFileSync21(bufferFile);
|
|
32486
32532
|
return { content: raw.toString("utf8"), bytes: raw.length };
|
|
32487
32533
|
} catch {
|
|
32488
32534
|
return null;
|
|
@@ -32490,7 +32536,7 @@ function makeViolationsBuffer(opts = {}) {
|
|
|
32490
32536
|
}
|
|
32491
32537
|
function appendedSince(bytes) {
|
|
32492
32538
|
try {
|
|
32493
|
-
const raw =
|
|
32539
|
+
const raw = readFileSync21(bufferFile);
|
|
32494
32540
|
return raw.length > bytes ? raw.subarray(bytes).toString("utf8") : "";
|
|
32495
32541
|
} catch {
|
|
32496
32542
|
return "";
|
|
@@ -32504,11 +32550,11 @@ function makeViolationsBuffer(opts = {}) {
|
|
|
32504
32550
|
}
|
|
32505
32551
|
for (let attempt = 0; attempt < 2; attempt++) {
|
|
32506
32552
|
try {
|
|
32507
|
-
const fd =
|
|
32553
|
+
const fd = openSync4(lockFile, "wx");
|
|
32508
32554
|
try {
|
|
32509
|
-
|
|
32555
|
+
writeSync4(fd, String(process.pid));
|
|
32510
32556
|
} finally {
|
|
32511
|
-
|
|
32557
|
+
closeSync4(fd);
|
|
32512
32558
|
}
|
|
32513
32559
|
return true;
|
|
32514
32560
|
} catch (e) {
|
|
@@ -32525,7 +32571,7 @@ function makeViolationsBuffer(opts = {}) {
|
|
|
32525
32571
|
}
|
|
32526
32572
|
function lockIsStale() {
|
|
32527
32573
|
try {
|
|
32528
|
-
return Date.now() -
|
|
32574
|
+
return Date.now() - statSync5(lockFile).mtimeMs > FLUSH_LOCK_STALE_MS;
|
|
32529
32575
|
} catch {
|
|
32530
32576
|
return true;
|
|
32531
32577
|
}
|
|
@@ -32552,7 +32598,7 @@ function makeViolationsBuffer(opts = {}) {
|
|
|
32552
32598
|
function readHolds() {
|
|
32553
32599
|
try {
|
|
32554
32600
|
if (!existsSync20(holdsFile)) return {};
|
|
32555
|
-
const parsed = JSON.parse(
|
|
32601
|
+
const parsed = JSON.parse(readFileSync21(holdsFile, "utf8"));
|
|
32556
32602
|
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
32557
32603
|
return {};
|
|
32558
32604
|
}
|
|
@@ -32612,9 +32658,9 @@ function capRecords(records, now) {
|
|
|
32612
32658
|
return dated.slice(-MAX_BUFFERED_RECORDS).map(({ record: record2 }) => record2);
|
|
32613
32659
|
}
|
|
32614
32660
|
function ensureDirFor4(filePath) {
|
|
32615
|
-
const dir =
|
|
32661
|
+
const dir = dirname12(filePath);
|
|
32616
32662
|
if (!existsSync20(dir)) {
|
|
32617
|
-
|
|
32663
|
+
mkdirSync13(dir, { recursive: true });
|
|
32618
32664
|
}
|
|
32619
32665
|
}
|
|
32620
32666
|
var _defaultBuffer = makeViolationsBuffer();
|
|
@@ -32623,9 +32669,9 @@ var flushViolations = _defaultBuffer.flushViolations.bind(_defaultBuffer);
|
|
|
32623
32669
|
var liftViolationHolds = _defaultBuffer.liftHolds.bind(_defaultBuffer);
|
|
32624
32670
|
|
|
32625
32671
|
// src/hooks/runtime/unmapped-tools.ts
|
|
32626
|
-
import { existsSync as existsSync21, mkdirSync as
|
|
32627
|
-
import { dirname as
|
|
32628
|
-
import { homedir as
|
|
32672
|
+
import { existsSync as existsSync21, mkdirSync as mkdirSync14, readFileSync as readFileSync22, writeFileSync as writeFileSync15 } from "node:fs";
|
|
32673
|
+
import { dirname as dirname13, join as join20 } from "node:path";
|
|
32674
|
+
import { homedir as homedir19 } from "node:os";
|
|
32629
32675
|
var TARGETLESS_TOOL_RE = /^(?:ToolSearch|TodoRead|TodoWrite|Task|Agent|AskUserQuestion|Skill|SlashCommand|EnterPlanMode|ExitPlanMode|EnterWorktree|ExitWorktree|BashOutput|KillShell|KillBash|Monitor|ListMcpResourcesTool|ReadMcpResourceTool|ReadMcpResourceDirTool|ScheduleWakeup|PushNotification|CronCreate|CronDelete|CronList|SendMessage|ListAgents|TaskOutput|TaskStop|ReportFindings|Workflow)$/;
|
|
32630
32676
|
var OWN_MCP_TOOL_RE = /^mcp__cybedefend(?:-[A-Za-z0-9_-]+)?__/;
|
|
32631
32677
|
function isTargetlessTool(tool) {
|
|
@@ -32637,13 +32683,13 @@ function strictUnmappedPolicy() {
|
|
|
32637
32683
|
}
|
|
32638
32684
|
var MAX_TOOLS = 200;
|
|
32639
32685
|
function defaultUnmappedToolsFile() {
|
|
32640
|
-
const dir = process.env.CYBEDEFEND_BASELINE_DIR ??
|
|
32641
|
-
return
|
|
32686
|
+
const dir = process.env.CYBEDEFEND_BASELINE_DIR ?? join20(homedir19(), ".cybedefend");
|
|
32687
|
+
return join20(dir, "guards-unmapped-tools.json");
|
|
32642
32688
|
}
|
|
32643
32689
|
function load(file2) {
|
|
32644
32690
|
try {
|
|
32645
32691
|
if (!existsSync21(file2)) return { tools: {} };
|
|
32646
|
-
const parsed = JSON.parse(
|
|
32692
|
+
const parsed = JSON.parse(readFileSync22(file2, "utf8"));
|
|
32647
32693
|
if (!parsed || typeof parsed !== "object" || typeof parsed.tools !== "object" || !parsed.tools) {
|
|
32648
32694
|
return { tools: {} };
|
|
32649
32695
|
}
|
|
@@ -32667,7 +32713,7 @@ function makeUnmappedToolTelemetry(file2 = defaultUnmappedToolsFile()) {
|
|
|
32667
32713
|
if (names.length > MAX_TOOLS) {
|
|
32668
32714
|
names.sort((a, b) => data.tools[a].lastSeen.localeCompare(data.tools[b].lastSeen)).slice(0, names.length - MAX_TOOLS).forEach((n) => delete data.tools[n]);
|
|
32669
32715
|
}
|
|
32670
|
-
|
|
32716
|
+
mkdirSync14(dirname13(file2), { recursive: true });
|
|
32671
32717
|
writeFileSync15(file2, JSON.stringify(data, null, 2) + "\n", "utf8");
|
|
32672
32718
|
} catch {
|
|
32673
32719
|
}
|
|
@@ -32733,11 +32779,11 @@ function appendDebugLog(entry) {
|
|
|
32733
32779
|
try {
|
|
32734
32780
|
const homeDir = process.env.HOME ?? process.env.USERPROFILE ?? "";
|
|
32735
32781
|
if (!homeDir) return;
|
|
32736
|
-
const { mkdirSync:
|
|
32737
|
-
const { join:
|
|
32738
|
-
const dir =
|
|
32739
|
-
|
|
32740
|
-
appendFileSync3(
|
|
32782
|
+
const { mkdirSync: mkdirSync15, appendFileSync: appendFileSync3 } = __require("node:fs");
|
|
32783
|
+
const { join: join22 } = __require("node:path");
|
|
32784
|
+
const dir = join22(homeDir, ".cybedefend");
|
|
32785
|
+
mkdirSync15(dir, { recursive: true });
|
|
32786
|
+
appendFileSync3(join22(dir, "hook-debug.log"), entry + "\n", "utf8");
|
|
32741
32787
|
} catch {
|
|
32742
32788
|
}
|
|
32743
32789
|
}
|
|
@@ -32817,6 +32863,18 @@ function commandField(params) {
|
|
|
32817
32863
|
}
|
|
32818
32864
|
return null;
|
|
32819
32865
|
}
|
|
32866
|
+
function windsurfToolCall(envelope) {
|
|
32867
|
+
const ev = sniffCaller(envelope);
|
|
32868
|
+
switch (ev.toolName) {
|
|
32869
|
+
case "Bash":
|
|
32870
|
+
return { rawTool: ev.toolName, params: { command: ev.newContent } };
|
|
32871
|
+
case "Read":
|
|
32872
|
+
case "Write":
|
|
32873
|
+
return { rawTool: ev.toolName, params: { file_path: ev.filePath } };
|
|
32874
|
+
default:
|
|
32875
|
+
return { rawTool: ev.toolName, params: {} };
|
|
32876
|
+
}
|
|
32877
|
+
}
|
|
32820
32878
|
function fileMapping(agent, tool, params, homeDir, cwd, rawTool, extraFilePaths) {
|
|
32821
32879
|
const filePath = typeof params.file_path === "string" ? params.file_path : typeof params.path === "string" ? params.path : null;
|
|
32822
32880
|
if (!filePath) return { kind: "unmapped", tool: rawTool ?? UNNAMED_TOOL };
|
|
@@ -32842,8 +32900,9 @@ function fileMapping(agent, tool, params, homeDir, cwd, rawTool, extraFilePaths)
|
|
|
32842
32900
|
}
|
|
32843
32901
|
function mapEnvelope(envelope, agent = sniffCaller(envelope).client) {
|
|
32844
32902
|
const antigravity = agent === "antigravity" ? readAntigravityCall(envelope) : null;
|
|
32845
|
-
const
|
|
32846
|
-
const
|
|
32903
|
+
const windsurf = agent === "windsurf" ? windsurfToolCall(envelope) : null;
|
|
32904
|
+
const rawTool = antigravity ? antigravity.tool : windsurf ? windsurf.rawTool : typeof envelope.tool_name === "string" ? envelope.tool_name : typeof envelope.tool === "string" ? envelope.tool : void 0;
|
|
32905
|
+
const params = antigravity ? antigravity.params : windsurf ? windsurf.params : envelope.tool_input ?? envelope.params ?? envelope.input ?? {};
|
|
32847
32906
|
const loggedKeys = antigravity ? antigravity.argKeys : Object.keys(params);
|
|
32848
32907
|
debugLog({
|
|
32849
32908
|
event: "pre_map",
|
|
@@ -33283,7 +33342,7 @@ async function runCatchJudgeStep(args) {
|
|
|
33283
33342
|
message
|
|
33284
33343
|
};
|
|
33285
33344
|
}
|
|
33286
|
-
function emitUnverifiedBanner(reason, client) {
|
|
33345
|
+
function emitUnverifiedBanner(reason, client, userNotice) {
|
|
33287
33346
|
const banner = [
|
|
33288
33347
|
"\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588",
|
|
33289
33348
|
"\u2588 VibeDefend Action Guards \u2014 UNVERIFIED \u2588",
|
|
@@ -33301,16 +33360,24 @@ function emitUnverifiedBanner(reason, client) {
|
|
|
33301
33360
|
].join("\n") + "\n";
|
|
33302
33361
|
process.stderr.write(banner);
|
|
33303
33362
|
const isCodex = typeof process.env.CODEX_SANDBOX !== "undefined" || typeof process.env.CODEX_AGENT !== "undefined";
|
|
33304
|
-
|
|
33305
|
-
|
|
33306
|
-
|
|
33363
|
+
const withContext = isCodex && client !== "antigravity";
|
|
33364
|
+
if (!withContext && !userNotice) return;
|
|
33365
|
+
try {
|
|
33366
|
+
const ctx = JSON.stringify({
|
|
33367
|
+
...userNotice ? { systemMessage: userNotice } : {},
|
|
33368
|
+
...withContext ? {
|
|
33307
33369
|
additionalContext: `VibeDefend Action Guards UNVERIFIED \u2014 ${reason} Tool call allowed WITHOUT enforcement. Run \`vibedefend login\` to restore protection.`
|
|
33308
|
-
}
|
|
33309
|
-
|
|
33310
|
-
|
|
33311
|
-
|
|
33370
|
+
} : {}
|
|
33371
|
+
});
|
|
33372
|
+
process.stdout.write(ctx + "\n");
|
|
33373
|
+
} catch {
|
|
33312
33374
|
}
|
|
33313
33375
|
}
|
|
33376
|
+
async function claimUserNotice(mcpName, envelope, client) {
|
|
33377
|
+
if (!SYSTEM_MESSAGE_CLIENTS.has(client)) return void 0;
|
|
33378
|
+
return await claimSignInNotice(mcpName, sessionIdOf3(envelope, client)) ? SIGN_IN_NOTICE : void 0;
|
|
33379
|
+
}
|
|
33380
|
+
var agentMessageWritten = false;
|
|
33314
33381
|
function sessionIdOf3(envelope, client) {
|
|
33315
33382
|
if (typeof envelope.session_id === "string") return envelope.session_id;
|
|
33316
33383
|
if (typeof envelope.sessionId === "string") return envelope.sessionId;
|
|
@@ -33591,23 +33658,24 @@ function emitAgentMessage(verdict, message, client) {
|
|
|
33591
33658
|
// Codex
|
|
33592
33659
|
additionalContext: fullMessage
|
|
33593
33660
|
};
|
|
33661
|
+
agentMessageWritten = true;
|
|
33594
33662
|
process.stdout.write(JSON.stringify(payload) + "\n");
|
|
33595
33663
|
} catch {
|
|
33596
33664
|
}
|
|
33597
33665
|
}
|
|
33598
33666
|
function makeProductionJudgeDeps() {
|
|
33599
33667
|
const cache = makeJudgeCache();
|
|
33600
|
-
const warnDir = process.env.CYBEDEFEND_BASELINE_DIR ??
|
|
33668
|
+
const warnDir = process.env.CYBEDEFEND_BASELINE_DIR ?? join21(homedir20(), ".cybedefend");
|
|
33601
33669
|
const scriptCache = makeJudgeCache({
|
|
33602
|
-
cacheFile:
|
|
33670
|
+
cacheFile: join21(warnDir, "guards-judge-script-cache.json"),
|
|
33603
33671
|
ttlMs: SCRIPT_VERDICT_TTL_MS,
|
|
33604
33672
|
maxEntries: SCRIPT_VERDICT_MAX_ENTRIES
|
|
33605
33673
|
});
|
|
33606
33674
|
const warnTracker = defaultSessionTracker(
|
|
33607
|
-
|
|
33675
|
+
join21(warnDir, "guards-judge-warned-sessions.json")
|
|
33608
33676
|
);
|
|
33609
33677
|
const blockedTracker = defaultSessionTracker(
|
|
33610
|
-
|
|
33678
|
+
join21(warnDir, "guards-judge-blocked-sessions.json")
|
|
33611
33679
|
);
|
|
33612
33680
|
return {
|
|
33613
33681
|
usage: {
|
|
@@ -33635,8 +33703,8 @@ function makeProductionJudgeDeps() {
|
|
|
33635
33703
|
}
|
|
33636
33704
|
function makeProductionUnmappedDeps() {
|
|
33637
33705
|
const telemetry = makeUnmappedToolTelemetry();
|
|
33638
|
-
const dir = process.env.CYBEDEFEND_BASELINE_DIR ??
|
|
33639
|
-
const tracker = defaultSessionTracker(
|
|
33706
|
+
const dir = process.env.CYBEDEFEND_BASELINE_DIR ?? join21(homedir20(), ".cybedefend");
|
|
33707
|
+
const tracker = defaultSessionTracker(join21(dir, "guards-unmapped-notified.json"));
|
|
33640
33708
|
return {
|
|
33641
33709
|
record: telemetry.record,
|
|
33642
33710
|
noticeOnce: (sessionId, tool) => {
|
|
@@ -33664,6 +33732,7 @@ function makeSessionWritesPort(sessionId) {
|
|
|
33664
33732
|
}
|
|
33665
33733
|
async function runPreToolUseGuard() {
|
|
33666
33734
|
try {
|
|
33735
|
+
agentMessageWritten = false;
|
|
33667
33736
|
const envelope = await readStdinJson();
|
|
33668
33737
|
const config2 = loadRuntimeConfig();
|
|
33669
33738
|
if (!config2) return 0;
|
|
@@ -33675,11 +33744,7 @@ async function runPreToolUseGuard() {
|
|
|
33675
33744
|
);
|
|
33676
33745
|
if (!projectId) return 0;
|
|
33677
33746
|
const apiBaseResolved = process.env.CYBEDEFEND_API_BASE ?? configApiBase ?? config2.region.apiBase;
|
|
33678
|
-
const token = await resolveTokenWithRefresh(
|
|
33679
|
-
config2.region.mcpName,
|
|
33680
|
-
{},
|
|
33681
|
-
{ mcpUrl: config2.region.mcpUrl }
|
|
33682
|
-
);
|
|
33747
|
+
const token = await resolveTokenWithRefresh(config2.region.mcpName);
|
|
33683
33748
|
const cache = makeGuardRulesCache({
|
|
33684
33749
|
fetchFn: (ctx) => fetchEffectiveRulesFromGateway(ctx)
|
|
33685
33750
|
});
|
|
@@ -33689,9 +33754,11 @@ async function runPreToolUseGuard() {
|
|
|
33689
33754
|
apiBaseResolved
|
|
33690
33755
|
});
|
|
33691
33756
|
if (!token && rulesOutcome.kind === "unavailable") {
|
|
33757
|
+
const client = sniffCaller(envelope).client;
|
|
33692
33758
|
emitUnverifiedBanner(
|
|
33693
33759
|
"Not authenticated \u2014 run `vibedefend login` to restore Action Guards.",
|
|
33694
|
-
|
|
33760
|
+
client,
|
|
33761
|
+
await claimUserNotice(config2.region.mcpName, envelope, client)
|
|
33695
33762
|
);
|
|
33696
33763
|
return 0;
|
|
33697
33764
|
}
|
|
@@ -33735,6 +33802,10 @@ async function runPreToolUseGuard() {
|
|
|
33735
33802
|
usageNotifier: makeUsageNotifier({ write: () => void 0 })
|
|
33736
33803
|
});
|
|
33737
33804
|
if (token) scheduleViolationsFlush({ buffer, token, apiBaseResolved });
|
|
33805
|
+
if (!token && exitCode === 0 && !agentMessageWritten) {
|
|
33806
|
+
const notice = await claimUserNotice(config2.region.mcpName, envelope, sniffCaller(envelope).client);
|
|
33807
|
+
if (notice) process.stdout.write(JSON.stringify({ systemMessage: notice }) + "\n");
|
|
33808
|
+
}
|
|
33738
33809
|
return exitCode;
|
|
33739
33810
|
} catch {
|
|
33740
33811
|
return 0;
|
|
@@ -33766,9 +33837,7 @@ async function runViolationsFlush(deps = {}) {
|
|
|
33766
33837
|
try {
|
|
33767
33838
|
const config2 = (deps.loadConfig ?? loadRuntimeConfig)();
|
|
33768
33839
|
if (!config2) return;
|
|
33769
|
-
const token = (deps.resolveTokenFn ?? resolveToken)(config2.region.mcpName
|
|
33770
|
-
mcpUrl: config2.region.mcpUrl
|
|
33771
|
-
});
|
|
33840
|
+
const token = (deps.resolveTokenFn ?? resolveToken)(config2.region.mcpName);
|
|
33772
33841
|
if (!token) return;
|
|
33773
33842
|
const apiBaseResolved = process.env.CYBEDEFEND_API_BASE ?? config2.region.apiBase;
|
|
33774
33843
|
await (deps.buffer ?? makeViolationsBuffer()).flushViolations(
|