pi-ai-vault 0.11.0 → 0.12.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-ai-vault",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "pi extension for ai-vault: one machine-wide encrypted secret store, live Bitwarden and Ente Auth. Works in pi and in omp (the omp fork reads the same manifest).",
5
5
  "repository": {
6
6
  "type": "git",
@@ -25,7 +25,7 @@
25
25
  "security"
26
26
  ],
27
27
  "dependencies": {
28
- "@xiaolikt/ai-vault": "0.11.0"
28
+ "@xiaolikt/ai-vault": "0.12.0"
29
29
  },
30
30
  "peerDependencies": {
31
31
  "@earendil-works/pi-coding-agent": "^1.5.0",
package/src/bitwarden.ts CHANGED
@@ -56,7 +56,7 @@ import { bitwardenItems, filterItems } from "./import-map.js";
56
56
  import type { Draft } from "./types.js";
57
57
  import { onSessionLeave, sessionLabel, withTier } from "./host.js";
58
58
  import { confirmWindow, hasDisplay, promptSecret } from "./prompt.js";
59
- import { getSettingsSafe, settingsCli } from "./settings.js";
59
+ import { getSettingsSafe, settingsCli, settingsToken } from "./settings.js";
60
60
  import { tr } from "./i18n.js";
61
61
  import { scrubDeep, scrubText } from "./scrub.js";
62
62
 
@@ -603,7 +603,8 @@ async function ensureUnlocked(st: BwState, ctx: ExtensionContext | undefined, si
603
603
  if (policy.unlock !== "session") {
604
604
  // Keep the key where the CLI can hand it back; the file's expiry comes
605
605
  // from the bitwarden.hours policy.
606
- await settingsCli().run(["bw-session", "put"], { stdin: key }).catch(() => undefined);
606
+ const token = settingsToken();
607
+ await settingsCli().run(["bw-session", "put", ...(token ? ["--token", token] : [])], { stdin: key }).catch(() => undefined);
607
608
  }
608
609
  await auditEvent("unlock", "ok", st.sessionLabel);
609
610
  // Refresh in the background: a full sync can take seconds on large vaults.
package/src/index.ts CHANGED
@@ -23,13 +23,13 @@ import {
23
23
  type BitwardenMeta,
24
24
  type BwState,
25
25
  } from "./bitwarden.js";
26
- import { CliError, VaultCli } from "./cli.js";
26
+ import { CliError } from "./cli.js";
27
27
  import { ITEM_TYPES, isSecretField, parseItemType } from "./import-map.js";
28
28
  import type { Draft, ItemType } from "./types.js";
29
29
  import { registerEnte } from "./ente.js";
30
30
  import { isOmpHost, onSessionLeave, sessionLabel, subagentId, withTier } from "./host.js";
31
31
  import { confirmWindow, forDialog, promptSecret, promptText, type PromptHost } from "./prompt.js";
32
- import { forgetSettings, getSettingsSafe, type Settings } from "./settings.js";
32
+ import { forgetSettings, getSettingsSafe, setSettingsToken, settingsCli, settingsToken, type Settings } from "./settings.js";
33
33
 
34
34
  type Ctx = ExtensionToolContext | ExtensionCommandContext;
35
35
 
@@ -100,7 +100,7 @@ interface Scope {
100
100
  unlocking?: Promise<boolean>;
101
101
  }
102
102
 
103
- const cli = new VaultCli();
103
+ const cli = settingsCli();
104
104
 
105
105
  const CLI_MISSING =
106
106
  tr("ai-vault CLI not found. Install it once for a machine-wide store: cargo install --path <repo>/crates/ai-vault (or point AI_VAULT_BIN at a compiled binary)", "ai-vault CLI 没找到。装一次就有全机共享的库:cargo install --path <repo>/crates/ai-vault(或用 AI_VAULT_BIN 指向编译好的二进制)");
@@ -148,6 +148,8 @@ async function lockNow(scope: Scope): Promise<void> {
148
148
  scope.token = undefined;
149
149
  scope.epoch++;
150
150
  scope.items = 0;
151
+ setSettingsToken(undefined);
152
+ forgetSettings();
151
153
  if (!token || !cli.available) return;
152
154
  try {
153
155
  await cli.run(["lock", "--token", token]);
@@ -174,6 +176,7 @@ async function ensureUnlocked(scope: Scope, ctx: Ctx, signal?: AbortSignal): Pro
174
176
  scope.items = status.items;
175
177
  scope.mode = status.mode;
176
178
  scope.auto = false;
179
+ setSettingsToken(scope.token);
177
180
  return true;
178
181
  } catch (error) {
179
182
  if (!(error instanceof CliError) || !error.auth) {
@@ -194,6 +197,7 @@ async function ensureUnlocked(scope: Scope, ctx: Ctx, signal?: AbortSignal): Pro
194
197
  scope.items = status.items;
195
198
  scope.mode = status.mode;
196
199
  scope.auto = status.auto;
200
+ setSettingsToken(undefined);
197
201
  return true;
198
202
  } catch (error) {
199
203
  if (error instanceof CliError && /not initialized/i.test(error.message)) {
@@ -237,6 +241,7 @@ async function ensureUnlocked(scope: Scope, ctx: Ctx, signal?: AbortSignal): Pro
237
241
  // per-binding token would bring back a confirm window on every read.
238
242
  scope.token = result.mode === "never" ? undefined : result.token;
239
243
  scope.items = result.items;
244
+ setSettingsToken(scope.token);
240
245
  return true;
241
246
  } catch (error) {
242
247
  if (error instanceof CliError && error.auth) {
@@ -693,6 +698,7 @@ const HELP = [
693
698
  tr("/vault init create the machine-wide encrypted store (a dialog sets the master password)", "/vault init 创建全机共享的本地加密库(弹出窗口设置主密码)"),
694
699
  tr("/vault unlock | lock unlock / lock (clears this session's grants)", "/vault unlock | lock 解锁 / 锁定(清空本会话的授权)"),
695
700
  tr("/vault mode [never|time|session] show/switch the unlock policy (switching needs the master password)", "/vault mode [never|time|session] 查看/切换解锁模式(切换要主密码)"),
701
+ tr("/vault confirm [off|on] a window per read while unlocked (off asks the master password)", "/vault confirm [off|on] 解锁后每次读取是否弹确认窗(关要主密码)"),
696
702
  tr("/vault settings [key value] show/change settings (vault.unlock, bitwarden.*, ente.* …)", "/vault settings [键 值] 查看/修改设置(vault.unlock、bitwarden.*、ente.* …)"),
697
703
  tr("/vault ente-key save Ente's ENTE_CLI_VAULT_KEY (no keychain, no login)", "/vault ente-key 保存 ente 的 ENTE_CLI_VAULT_KEY(免钥匙串/免登录)"),
698
704
  tr("/vault status state and grants left", "/vault status 状态与授权剩余时间"),
@@ -861,22 +867,61 @@ function commandHandler(scope: Scope, bw: BwState) {
861
867
  }
862
868
  return;
863
869
  }
870
+ case "confirm": {
871
+ // /vault confirm show
872
+ // /vault confirm off|on turn the per-read window off (asks the master password) / on
873
+ const [arg] = rest;
874
+ if (!arg) {
875
+ try {
876
+ const current = await cli.json<Settings>(["settings", ...tokenArgs(scope)]);
877
+ note(ctx, `vault.confirm: ${current.vault.confirm}${current.vault.confirm ? tr(" (a window per read while unlocked)", " (解锁后每次读取弹确认窗)") : tr(" (the unlock is the approval; reads are audited)", " (解锁即授权;读取仍记审计)")}`);
878
+ } catch (error) {
879
+ note(ctx, `ai-vault: ${describe(error)}`, "error");
880
+ }
881
+ return;
882
+ }
883
+ const value = arg.toLowerCase();
884
+ if (!["off", "on", "false", "true"].includes(value)) {
885
+ note(ctx, tr("usage: /vault confirm [off|on]", "用法: /vault confirm [off|on]"), "warning");
886
+ return;
887
+ }
888
+ const turnOff = value === "off" || value === "false";
889
+ try {
890
+ const run = await cli.withPassword(["settings", "vault.confirm", turnOff ? "false" : "true", ...tokenArgs(scope)], () =>
891
+ promptSecret(dialogs(ctx), tr("ai-vault · master password", "ai-vault · 主密码"), tr("turn off the per-read confirmation", "关闭每次读取的确认窗")),
892
+ );
893
+ forgetSettings();
894
+ const updated = JSON.parse(run.stdout) as Settings;
895
+ note(
896
+ ctx,
897
+ updated.vault.confirm
898
+ ? tr("vault.confirm is on: every read opens a confirm window.", "vault.confirm 已开:每次读取弹确认窗。")
899
+ : tr(
900
+ "vault.confirm is off: while a session is unlocked, reads return without a window. Each read is still written to the audit log. Turning it back on: /vault confirm on",
901
+ "vault.confirm 已关:会话解锁期间读取不再弹窗。每次读取仍写审计。重新打开:/vault confirm on",
902
+ ),
903
+ );
904
+ } catch (error) {
905
+ note(ctx, `ai-vault: ${error instanceof CliError && error.denied ? tr("cancelled", "已取消") : describe(error)}`, "warning");
906
+ }
907
+ return;
908
+ }
864
909
  case "settings": {
865
910
  const [key, ...valueParts] = rest;
866
911
  const value = valueParts.join(" ").trim();
867
912
  try {
868
913
  if (!key) {
869
- note(ctx, settingsText(await cli.json<Settings>(["settings"])));
914
+ note(ctx, settingsText(await cli.json<Settings>(["settings", ...tokenArgs(scope)])));
870
915
  return;
871
916
  }
872
917
  if (!value) {
873
- const one = await cli.json<{ key: string; value: string }>(["settings", key]);
918
+ const one = await cli.json<{ key: string; value: string }>(["settings", key, ...tokenArgs(scope)]);
874
919
  note(ctx, `${one.key}: ${one.value}`);
875
920
  return;
876
921
  }
877
922
  // `vault.unlock` opens its own password window (or answers
878
923
  // needs-ui); the other keys do not prompt at all.
879
- const run = await cli.withPassword(["settings", key, value], () =>
924
+ const run = await cli.withPassword(["settings", key, value, ...tokenArgs(scope)], () =>
880
925
  promptSecret(dialogs(ctx), tr("ai-vault · master password", "ai-vault · 主密码"), tr("changing the unlock policy needs verification", "改变解锁模式需要验证")),
881
926
  );
882
927
  forgetSettings();
package/src/settings.ts CHANGED
@@ -11,7 +11,7 @@ export type UnlockModeName = "never" | "time" | "session";
11
11
  export type ImportPolicy = "ask" | "always" | "never";
12
12
 
13
13
  export interface Settings {
14
- vault: { unlock: UnlockModeName; hours: number; auto: boolean };
14
+ vault: { unlock: UnlockModeName; hours: number; confirm: boolean; auto: boolean };
15
15
  bitwarden: {
16
16
  unlock: UnlockModeName;
17
17
  hours: number;
@@ -24,17 +24,42 @@ export interface Settings {
24
24
  }
25
25
 
26
26
  export const DEFAULT_SETTINGS: Settings = {
27
- vault: { unlock: "session", hours: 8, auto: false },
27
+ vault: { unlock: "session", hours: 8, confirm: true, auto: false },
28
28
  bitwarden: { unlock: "session", hours: 8, import: "ask", persisted: false, expiresAt: null },
29
29
  ente: { unlock: "session", hours: 8, persisted: false, expiresAt: null },
30
30
  };
31
31
 
32
- const cli = new VaultCli();
33
- let cache: { at: number; value: Settings } | undefined;
32
+ /**
33
+ * One CLI client for the whole extension. It carries the one `HostSecret`
34
+ * every call must be signed under: a second instance would have its own
35
+ * (empty) secret, and a call made through it would fail the session file's
36
+ * HMAC and get it deleted — locking the binding out. Keep this the same
37
+ * object the tools use.
38
+ */
39
+ let cli: VaultCli | undefined;
34
40
 
35
- /** The shared CLI client, for the pieces that read and write the settings. */
36
41
  export function settingsCli(): VaultCli {
37
- return cli;
42
+ return (cli ??= new VaultCli());
43
+ }
44
+
45
+ let cache: { at: number; value: Settings } | undefined;
46
+
47
+ /**
48
+ * The session token the settings are read under. The settings live inside the
49
+ * store's ciphertext, so reading them is a privileged call like any other;
50
+ * the binding that unlocked sets this before asking.
51
+ */
52
+ let sessionToken: string | undefined;
53
+
54
+ export function setSettingsToken(token: string | undefined): void {
55
+ if (token !== sessionToken) {
56
+ sessionToken = token;
57
+ cache = undefined;
58
+ }
59
+ }
60
+
61
+ export function settingsToken(): string | undefined {
62
+ return sessionToken;
38
63
  }
39
64
 
40
65
  /**
@@ -43,7 +68,7 @@ export function settingsCli(): VaultCli {
43
68
  */
44
69
  export async function getSettings(signal?: AbortSignal, options: { fresh?: boolean } = {}): Promise<Settings> {
45
70
  if (!options.fresh && cache && Date.now() - cache.at < 30_000) return cache.value;
46
- const value = await cli.json<Settings>(["settings"], { signal });
71
+ const value = await settingsCli().json<Settings>(["settings", ...(sessionToken ? ["--token", sessionToken] : [])], { signal });
47
72
  cache = { at: Date.now(), value };
48
73
  return value;
49
74
  }
@@ -60,3 +85,8 @@ export async function getSettingsSafe(signal?: AbortSignal): Promise<Settings> {
60
85
  export function forgetSettings(): void {
61
86
  cache = undefined;
62
87
  }
88
+
89
+ /** Tests change the store's settings from outside the process; let them drop the cache. */
90
+ process.on("ai-vault:settings-changed", () => {
91
+ cache = undefined;
92
+ });