@r-33/opencode 0.5.0 → 0.5.1

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.
@@ -4001,25 +4001,57 @@ var init_detect_worktree_root2 = __esm({
4001
4001
  }
4002
4002
  });
4003
4003
 
4004
+ // src/attribution/global-attribution-file/global-attribution-file.ts
4005
+ import * as path8 from "node:path";
4006
+ var globalAttributionFile;
4007
+ var init_global_attribution_file = __esm({
4008
+ "src/attribution/global-attribution-file/global-attribution-file.ts"() {
4009
+ "use strict";
4010
+ init_cli();
4011
+ globalAttributionFile = () => path8.join(resolveV1SecretStoreDirectory(), "route33.attribution.json");
4012
+ }
4013
+ });
4014
+
4015
+ // src/attribution/global-attribution-file/index.ts
4016
+ var init_global_attribution_file2 = __esm({
4017
+ "src/attribution/global-attribution-file/index.ts"() {
4018
+ "use strict";
4019
+ init_global_attribution_file();
4020
+ }
4021
+ });
4022
+
4004
4023
  // src/attribution/resolve-attribution-file/resolve-attribution-file.ts
4005
4024
  import * as fs7 from "node:fs";
4006
- import * as path8 from "node:path";
4007
- var resolveAttributionFile;
4025
+ import * as path9 from "node:path";
4026
+ var STATE_FILE, resolveAttributionFile;
4008
4027
  var init_resolve_attribution_file = __esm({
4009
4028
  "src/attribution/resolve-attribution-file/resolve-attribution-file.ts"() {
4010
4029
  "use strict";
4011
4030
  init_detect_worktree_root2();
4031
+ init_global_attribution_file2();
4032
+ STATE_FILE = "route33.attribution.json";
4012
4033
  resolveAttributionFile = (options) => {
4034
+ if (options.scope === "global") return globalAttributionFile();
4013
4035
  const worktreeRoot = detectWorktreeRoot(options.directory);
4014
4036
  if (worktreeRoot !== void 0) {
4015
- return path8.join(worktreeRoot, ".opencode", "route33.attribution.json");
4037
+ return path9.join(worktreeRoot, ".opencode", STATE_FILE);
4038
+ }
4039
+ switch (options.outsideWorktree) {
4040
+ case "use-directory":
4041
+ return path9.join(
4042
+ fs7.realpathSync(options.directory),
4043
+ ".opencode",
4044
+ STATE_FILE
4045
+ );
4046
+ case "use-global":
4047
+ return globalAttributionFile();
4048
+ case "return-undefined":
4049
+ return void 0;
4050
+ default: {
4051
+ const unexpected = options.outsideWorktree;
4052
+ throw new TypeError(`Unexpected policy: ${String(unexpected)}`);
4053
+ }
4016
4054
  }
4017
- const fallbackRoots = {
4018
- "return-undefined": void 0,
4019
- "use-directory": fs7.realpathSync(options.directory)
4020
- };
4021
- const fallbackRoot = fallbackRoots[options.outsideWorktree];
4022
- return fallbackRoot === void 0 ? void 0 : path8.join(fallbackRoot, ".opencode", "route33.attribution.json");
4023
4055
  };
4024
4056
  }
4025
4057
  });
@@ -4058,20 +4090,55 @@ var init_clear_attribution2 = __esm({
4058
4090
  }
4059
4091
  });
4060
4092
 
4093
+ // src/attribution/is-global-attribution/is-global-attribution.ts
4094
+ var isGlobalAttribution;
4095
+ var init_is_global_attribution = __esm({
4096
+ "src/attribution/is-global-attribution/is-global-attribution.ts"() {
4097
+ "use strict";
4098
+ init_detect_worktree_root2();
4099
+ isGlobalAttribution = (options) => options.scope === "global" || options.outsideWorktree === "use-global" && detectWorktreeRoot(options.directory) === void 0;
4100
+ }
4101
+ });
4102
+
4103
+ // src/attribution/is-global-attribution/index.ts
4104
+ var init_is_global_attribution2 = __esm({
4105
+ "src/attribution/is-global-attribution/index.ts"() {
4106
+ "use strict";
4107
+ init_is_global_attribution();
4108
+ }
4109
+ });
4110
+
4111
+ // src/attribution/is-global-choice/is-global-choice.ts
4112
+ var isGlobalChoice;
4113
+ var init_is_global_choice = __esm({
4114
+ "src/attribution/is-global-choice/is-global-choice.ts"() {
4115
+ "use strict";
4116
+ isGlobalChoice = (attribution) => attribution.kind === "personal" && attribution.boundKeyId === void 0;
4117
+ }
4118
+ });
4119
+
4120
+ // src/attribution/is-global-choice/index.ts
4121
+ var init_is_global_choice2 = __esm({
4122
+ "src/attribution/is-global-choice/index.ts"() {
4123
+ "use strict";
4124
+ init_is_global_choice();
4125
+ }
4126
+ });
4127
+
4061
4128
  // src/attribution/read-legacy-key/read-legacy-key.ts
4062
4129
  import * as fs9 from "node:fs";
4063
- import * as path9 from "node:path";
4130
+ import * as path10 from "node:path";
4064
4131
  var LEGACY_KEY_RELATIVE_PATH, readLegacyKey;
4065
4132
  var init_read_legacy_key = __esm({
4066
4133
  "src/attribution/read-legacy-key/read-legacy-key.ts"() {
4067
4134
  "use strict";
4068
- LEGACY_KEY_RELATIVE_PATH = path9.join(".opencode", "route33.key");
4135
+ LEGACY_KEY_RELATIVE_PATH = path10.join(".opencode", "route33.key");
4069
4136
  readLegacyKey = (worktree) => {
4070
4137
  if (worktree.trim() === "") return void 0;
4071
4138
  let raw;
4072
4139
  try {
4073
4140
  raw = fs9.readFileSync(
4074
- path9.join(worktree, LEGACY_KEY_RELATIVE_PATH),
4141
+ path10.join(worktree, LEGACY_KEY_RELATIVE_PATH),
4075
4142
  "utf8"
4076
4143
  );
4077
4144
  } catch {
@@ -4182,6 +4249,8 @@ var readAttribution;
4182
4249
  var init_read_attribution = __esm({
4183
4250
  "src/attribution/read-attribution/read-attribution.ts"() {
4184
4251
  "use strict";
4252
+ init_is_global_attribution2();
4253
+ init_is_global_choice2();
4185
4254
  init_parse_attribution2();
4186
4255
  init_resolve_attribution_file2();
4187
4256
  readAttribution = (options) => {
@@ -4191,7 +4260,8 @@ var init_read_attribution = __esm({
4191
4260
  }
4192
4261
  try {
4193
4262
  const parsed = JSON.parse(fs10.readFileSync(file, "utf8"));
4194
- return parseAttribution(parsed);
4263
+ const attribution = parseAttribution(parsed);
4264
+ return isGlobalAttribution(options) && !isGlobalChoice(attribution) ? void 0 : attribution;
4195
4265
  } catch {
4196
4266
  return void 0;
4197
4267
  }
@@ -4209,15 +4279,15 @@ var init_read_attribution2 = __esm({
4209
4279
 
4210
4280
  // src/attribution/remove-legacy-key/remove-legacy-key.ts
4211
4281
  import * as fs11 from "node:fs";
4212
- import * as path10 from "node:path";
4282
+ import * as path11 from "node:path";
4213
4283
  var LEGACY_KEY_RELATIVE_PATH2, removeLegacyKey;
4214
4284
  var init_remove_legacy_key = __esm({
4215
4285
  "src/attribution/remove-legacy-key/remove-legacy-key.ts"() {
4216
4286
  "use strict";
4217
- LEGACY_KEY_RELATIVE_PATH2 = path10.join(".opencode", "route33.key");
4287
+ LEGACY_KEY_RELATIVE_PATH2 = path11.join(".opencode", "route33.key");
4218
4288
  removeLegacyKey = (worktree) => {
4219
4289
  try {
4220
- fs11.rmSync(path10.join(worktree, LEGACY_KEY_RELATIVE_PATH2), { force: true });
4290
+ fs11.rmSync(path11.join(worktree, LEGACY_KEY_RELATIVE_PATH2), { force: true });
4221
4291
  return true;
4222
4292
  } catch {
4223
4293
  return false;
@@ -4237,18 +4307,23 @@ var init_remove_legacy_key2 = __esm({
4237
4307
  // src/attribution/write-attribution/write-attribution.ts
4238
4308
  import { randomUUID } from "node:crypto";
4239
4309
  import * as fs12 from "node:fs";
4240
- import * as path11 from "node:path";
4310
+ import * as path12 from "node:path";
4241
4311
  var writeAttribution;
4242
4312
  var init_write_attribution = __esm({
4243
4313
  "src/attribution/write-attribution/write-attribution.ts"() {
4244
4314
  "use strict";
4315
+ init_is_global_attribution2();
4316
+ init_is_global_choice2();
4245
4317
  init_resolve_attribution_file2();
4246
4318
  writeAttribution = (options, attribution) => {
4247
4319
  const file = resolveAttributionFile(options);
4248
4320
  if (file === void 0) {
4249
4321
  return false;
4250
4322
  }
4251
- fs12.mkdirSync(path11.dirname(file), { recursive: true });
4323
+ if (isGlobalAttribution(options) && !isGlobalChoice(attribution)) {
4324
+ return false;
4325
+ }
4326
+ fs12.mkdirSync(path12.dirname(file), { recursive: true });
4252
4327
  const temporaryFile = `${file}.${String(process.pid)}.${randomUUID()}.tmp`;
4253
4328
  try {
4254
4329
  fs12.writeFileSync(temporaryFile, `${JSON.stringify(attribution)}
@@ -4277,6 +4352,9 @@ var init_attribution = __esm({
4277
4352
  "use strict";
4278
4353
  init_clear_attribution2();
4279
4354
  init_detect_worktree_root2();
4355
+ init_global_attribution_file2();
4356
+ init_is_global_attribution2();
4357
+ init_is_global_choice2();
4280
4358
  init_read_legacy_key2();
4281
4359
  init_parse_attribution2();
4282
4360
  init_read_attribution2();
@@ -4396,7 +4474,7 @@ var init_lease_state2 = __esm({
4396
4474
  // src/key-lease/file-secret-store/file-secret-store.ts
4397
4475
  import { randomUUID as randomUUID2 } from "node:crypto";
4398
4476
  import * as fs13 from "node:fs/promises";
4399
- import * as path12 from "node:path";
4477
+ import * as path13 from "node:path";
4400
4478
  var FileSecretStore;
4401
4479
  var init_file_secret_store = __esm({
4402
4480
  "src/key-lease/file-secret-store/file-secret-store.ts"() {
@@ -4430,7 +4508,7 @@ var init_file_secret_store = __esm({
4430
4508
  throw new TypeError("File secret store scope is not portable");
4431
4509
  }
4432
4510
  this.#directory = options.directory;
4433
- this.#file = path12.join(options.directory, `lease-${options.scopeId}.json`);
4511
+ this.#file = path13.join(options.directory, `lease-${options.scopeId}.json`);
4434
4512
  }
4435
4513
  /** Reads and parses the scoped gateway credential.
4436
4514
  * @returns The persisted credential, when present.
@@ -4483,7 +4561,7 @@ var init_file_secret_store = __esm({
4483
4561
  async stage(key, lease) {
4484
4562
  await fs13.mkdir(this.#directory, { mode: 448, recursive: true });
4485
4563
  await fs13.chmod(this.#directory, 448);
4486
- const temporary = path12.join(this.#directory, `.lease-${randomUUID2()}.tmp`);
4564
+ const temporary = path13.join(this.#directory, `.lease-${randomUUID2()}.tmp`);
4487
4565
  try {
4488
4566
  const record = lease === void 0 ? key.value : { key: key.value, lease };
4489
4567
  await fs13.writeFile(temporary, `${JSON.stringify(record)}
@@ -5731,6 +5809,7 @@ var init_workspace_key_binding = __esm({
5731
5809
  * @returns Secret-free options; local options do not need destructive consent.
5732
5810
  */
5733
5811
  async candidates(choice) {
5812
+ if (detectWorktreeRoot(this.deps.state.directory) === void 0) return [];
5734
5813
  const listing = this.#listings.get(choice.orgId) ?? this.listKeys(choice.orgId);
5735
5814
  this.#listings.set(choice.orgId, listing);
5736
5815
  const rows = await listing;
@@ -5778,6 +5857,8 @@ var init_workspace_key_binding = __esm({
5778
5857
  * @returns The attribution to commit after saving the secret.
5779
5858
  */
5780
5859
  async bind(choice, id, confirmed) {
5860
+ if (detectWorktreeRoot(this.deps.state.directory) === void 0)
5861
+ throw new GatewayCredentialError("Key binding requires a worktree.");
5781
5862
  const row = await this.deps.client.get(this.deps.token, id, choice.orgId);
5782
5863
  if (row.id !== id || !this.matches(row, choice))
5783
5864
  throw new GatewayCredentialError(
@@ -5992,6 +6073,22 @@ var init_attribution_prompt2 = __esm({
5992
6073
  }
5993
6074
  });
5994
6075
 
6076
+ // src/ux/attribution-plan/attribution-plan.ts
6077
+ var init_attribution_plan = __esm({
6078
+ "src/ux/attribution-plan/attribution-plan.ts"() {
6079
+ "use strict";
6080
+ init_attribution_prompt2();
6081
+ }
6082
+ });
6083
+
6084
+ // src/ux/attribution-plan/index.ts
6085
+ var init_attribution_plan2 = __esm({
6086
+ "src/ux/attribution-plan/index.ts"() {
6087
+ "use strict";
6088
+ init_attribution_plan();
6089
+ }
6090
+ });
6091
+
5995
6092
  // src/ux/attribution-prompt-error/attribution-prompt-error.ts
5996
6093
  var MESSAGES, AttributionPromptError;
5997
6094
  var init_attribution_prompt_error = __esm({
@@ -6109,6 +6206,7 @@ var init_callback_page_copy2 = __esm({
6109
6206
  var init_ux = __esm({
6110
6207
  "src/ux/index.ts"() {
6111
6208
  "use strict";
6209
+ init_attribution_plan2();
6112
6210
  init_attribution_prompt2();
6113
6211
  init_attribution_prompt_error2();
6114
6212
  init_prompt_flow2();
@@ -6554,10 +6652,11 @@ var init_route33_runtime = __esm({
6554
6652
  }
6555
6653
  /**
6556
6654
  * Reads the stored key for a saved attribution without any login.
6655
+ * @param state - Whose attribution to read; this folder's by default.
6557
6656
  * @returns The reusable key and lease when the stored lease is still valid.
6558
6657
  */
6559
- async storedReusableKey() {
6560
- const attribution = readAttribution(this.#options.state);
6658
+ async storedReusableKey(state = this.#options.state) {
6659
+ const attribution = readAttribution(state);
6561
6660
  if (attribution === void 0) {
6562
6661
  return void 0;
6563
6662
  }
@@ -6606,7 +6705,7 @@ var init_route33_runtime = __esm({
6606
6705
  async resolveWorkspaceCredential(directory) {
6607
6706
  const attribution = readAttribution({
6608
6707
  directory,
6609
- outsideWorktree: "return-undefined"
6708
+ outsideWorktree: this.#options.state.outsideWorktree
6610
6709
  });
6611
6710
  if (attribution === void 0) {
6612
6711
  const installation = this.#installation({ kind: "personal" });
@@ -6724,10 +6823,11 @@ var init_route33_runtime = __esm({
6724
6823
  * @param extras - The wizard's key-creation answers.
6725
6824
  * @param directory - Optional worktree override for project leases.
6726
6825
  * @param saveAttribution - Wizard save that must succeed before replacing a live key.
6826
+ * @param state - Where that save lands; the directory's own state by default.
6727
6827
  * @returns The fresh key material with its lease metadata.
6728
6828
  */
6729
- async mintWorkspaceKey(attribution, extras, directory = this.#options.state.directory, saveAttribution) {
6730
- const file = resolveAttributionFile({ ...this.#options.state, directory });
6829
+ async mintWorkspaceKey(attribution, extras, directory = this.#options.state.directory, saveAttribution, state = { ...this.#options.state, directory }) {
6830
+ const file = resolveAttributionFile(state);
6731
6831
  if (file === void 0)
6732
6832
  throw new TypeError("Workspace attribution requires a worktree");
6733
6833
  const previous = statSync2(file, { bigint: true, throwIfNoEntry: false });
@@ -6770,7 +6870,7 @@ var init_route33_runtime = __esm({
6770
6870
  async workspaceKeyAllowance(directory) {
6771
6871
  const attribution = readAttribution({
6772
6872
  directory,
6773
- outsideWorktree: "return-undefined"
6873
+ outsideWorktree: this.#options.state.outsideWorktree
6774
6874
  });
6775
6875
  if (attribution === void 0) {
6776
6876
  return void 0;
@@ -7016,6 +7116,7 @@ var init_create_route33_plugin = __esm({
7016
7116
  ...options.config
7017
7117
  };
7018
7118
  const runtimeOptions = options.runtime;
7119
+ const outsideWorktree = options.outsideWorktree ?? "return-undefined";
7019
7120
  const installation = runtimeOptions === void 0 ? void 0 : runtimeOptions.installation ?? createLeaseInstallationFactory({
7020
7121
  listScopes: () => FileSecretStore.scopes(
7021
7122
  resolveV1SecretStoreDirectory({
@@ -7034,10 +7135,7 @@ var init_create_route33_plugin = __esm({
7034
7135
  ...runtimeOptions,
7035
7136
  auth: { ...runtimeOptions.auth, clientId },
7036
7137
  installation,
7037
- state: {
7038
- directory: input.worktree,
7039
- outsideWorktree: "return-undefined"
7040
- }
7138
+ state: { directory: input.worktree, outsideWorktree }
7041
7139
  });
7042
7140
  if (runtime !== void 0) {
7043
7141
  options.bindRuntime?.(runtime);
@@ -7075,7 +7173,7 @@ var init_create_route33_plugin = __esm({
7075
7173
  return false;
7076
7174
  }
7077
7175
  },
7078
- outsideWorktree: "return-undefined"
7176
+ outsideWorktree
7079
7177
  });
7080
7178
  }
7081
7179
  return Promise.resolve({
@@ -8427,7 +8525,7 @@ var v2SetupDirectory = (context) => {
8427
8525
  if (location === void 0) return void 0;
8428
8526
  const attributed = readAttribution({
8429
8527
  directory: location.directory,
8430
- outsideWorktree: "return-undefined"
8528
+ outsideWorktree: "use-global"
8431
8529
  }) !== void 0;
8432
8530
  return attributed ? void 0 : location.directory;
8433
8531
  };
@@ -8504,12 +8602,56 @@ var createV1ConnectWizard = (composeOptions = productionOptions) => (api) => {
8504
8602
 
8505
8603
  // src/tui-entry/v2-tui/v2-tui.ts
8506
8604
  init_attribution();
8507
- init_ux();
8508
8605
 
8509
8606
  // src/tui-entry/show-r33-menu/show-r33-menu.ts
8510
8607
  init_attribution();
8511
8608
  init_cli();
8512
8609
 
8610
+ // src/tui-entry/show-r33-menu/menu-options/menu-options.ts
8611
+ var GLOBAL_KEY_SUBJECT = "the key for folders outside Git";
8612
+ var r33MenuOptions = (outsideGit) => [
8613
+ ...outsideGit ? [{ title: `Attribute \u2014 ${GLOBAL_KEY_SUBJECT}`, value: "attribute" }] : [
8614
+ {
8615
+ title: "Attribute \u2014 pick this folder's budget again",
8616
+ value: "attribute"
8617
+ },
8618
+ { title: `Global key \u2014 ${GLOBAL_KEY_SUBJECT}`, value: "global" }
8619
+ ],
8620
+ {
8621
+ title: "Inspect session \u2014 attribution and key metadata",
8622
+ value: "inspect"
8623
+ },
8624
+ { title: "Rotate key now", value: "rotate" }
8625
+ ];
8626
+
8627
+ // src/tui-entry/show-r33-menu/run-action/run-action.ts
8628
+ var runR33MenuAction = async (context, action, deps) => {
8629
+ try {
8630
+ const handlers = deps.handlers();
8631
+ const local = () => executeR33Command({
8632
+ budgetAction: "Attribute",
8633
+ handlers: handlers && {
8634
+ ...handlers,
8635
+ "inspect-session": async () => {
8636
+ const inspection = await handlers["inspect-session"]();
8637
+ return inspection.connected || deps.signedIn === void 0 ? inspection : { ...inspection, connected: await deps.signedIn() };
8638
+ }
8639
+ },
8640
+ promptText: `/r33 ${action}`
8641
+ });
8642
+ const reply = action === "rotate" && deps.rotate !== void 0 ? await deps.rotate() : await local();
8643
+ context.ui.toast.show({ message: reply, variant: "info" });
8644
+ } catch (error) {
8645
+ context.ui.toast.show({
8646
+ message: error instanceof Error ? error.message : String(error),
8647
+ variant: "error"
8648
+ });
8649
+ }
8650
+ };
8651
+
8652
+ // src/tui-entry/show-r33-menu/run-budget-entry/run-budget-entry.ts
8653
+ init_attribution();
8654
+
8513
8655
  // src/tui-entry/v2-autoconnect/v2-autoconnect.ts
8514
8656
  init_attribution();
8515
8657
 
@@ -8520,6 +8662,7 @@ var V2_CONNECT_COPY = {
8520
8662
  busyTitle: "Route 33",
8521
8663
  dismissed: "Route 33 is not set up for this folder; open /r33 and choose Attribute.",
8522
8664
  expired: "Route 33 setup expired; open /r33 and choose Attribute.",
8665
+ globalKeyNotice: "This picks the personal key for every folder outside Git on this machine. This repository keeps its own budget.",
8523
8666
  manualFallback: "Guided Route 33 setup is unavailable; run /connect and choose Route 33.",
8524
8667
  manualPath: "Open /r33 and choose Attribute.",
8525
8668
  noBudgetsTitle: "Route 33 \u2014 no budgets available",
@@ -8619,7 +8762,6 @@ var showCompletion = (dialog, options, selection, extras) => {
8619
8762
  // src/v2-entry/connect-copy/connect-copy.ts
8620
8763
  var CONNECT_COPY = {
8621
8764
  failFastClientId: "This Route 33 build has no OAuth client ID; reinstall it from the opencode-dist ref.",
8622
- failFastWorktree: "To assign a Route 33 budget, open a Git repository.",
8623
8765
  selectionInvalid: "That Route 33 option is no longer available; open /r33 and choose Attribute.",
8624
8766
  signedInNextStep: "Signed in to Route 33; open /r33 and choose Attribute to assign a budget.",
8625
8767
  budgetSelected: "Budget selected but not assigned; open /r33 and choose Attribute to finish setup."
@@ -8684,9 +8826,29 @@ var answerPass = async (deps) => {
8684
8826
  return "refused";
8685
8827
  };
8686
8828
 
8687
- // src/v2-entry/connect-form/connect-form.ts
8688
- var ATTRIBUTION_FIELD_KEY2 = "attribution";
8829
+ // src/v2-entry/global-key-answer/global-key-answer.ts
8830
+ var ATTRIBUTION_SCOPE_FIELD_KEY = "attribution_scope";
8831
+ var GLOBAL_KEY_ANSWER = { [ATTRIBUTION_SCOPE_FIELD_KEY]: "global" };
8832
+ var isGlobalKeyAnswer = (answer) => answer[ATTRIBUTION_SCOPE_FIELD_KEY] === "global";
8833
+
8834
+ // src/v2-entry/connect-form/budget-question-copy/budget-question-copy.ts
8835
+ var budgetQuestionCopy = (plan) => plan.notice === void 0 ? {
8836
+ attribution: {
8837
+ title: "Which budget covers Route 33 usage in this folder?"
8838
+ },
8839
+ organisation: "Which Route 33 organisation covers this folder?"
8840
+ } : {
8841
+ attribution: {
8842
+ description: plan.notice,
8843
+ title: "Which personal key covers folders outside Git?"
8844
+ },
8845
+ organisation: "Which Route 33 organisation covers folders outside Git?"
8846
+ };
8847
+
8848
+ // src/v2-entry/connect-form/budget-fields/budget-fields.ts
8689
8849
  var ORGANISATION_FIELD_KEY = "organisation";
8850
+
8851
+ // src/v2-entry/connect-form/connect-form.ts
8690
8852
  var BUDGET_CAP_FIELD_KEY = "budget_cap";
8691
8853
  var BUDGET_CAP_MAX_FIELD_KEY = "budget_cap_max";
8692
8854
  var PREPARED_BUDGET_FIELD_KEY = "prepared_budget";
@@ -8908,7 +9070,7 @@ var prepareKeyFields = async (deps) => {
8908
9070
  (field) => field.key === PREPARED_BUDGET_FIELD_KEY
8909
9071
  )?.placeholder;
8910
9072
  const fields = registered.filter(
8911
- (field) => field.key !== ATTRIBUTION_FIELD_KEY2 && field.key !== ORGANISATION_FIELD_KEY && field.key !== PREPARED_BUDGET_FIELD_KEY
9073
+ (field) => field.key !== ATTRIBUTION_FIELD_KEY && field.key !== ATTRIBUTION_SCOPE_FIELD_KEY && field.key !== ORGANISATION_FIELD_KEY && field.key !== PREPARED_BUDGET_FIELD_KEY
8912
9074
  );
8913
9075
  if (fields.length === 0 || (preparedFor ?? deps.selection) !== deps.selection) {
8914
9076
  deps.toast({ message: V2_CONNECT_COPY.setupIncomplete, variant: "error" });
@@ -8923,7 +9085,7 @@ var runAnswerStep = async (deps) => {
8923
9085
  busy: { message, title: V2_CONNECT_COPY.busyTitle },
8924
9086
  connect: deps.connect,
8925
9087
  dialog: deps.dialog,
8926
- extras,
9088
+ extras: { ...deps.answers, ...extras },
8927
9089
  form: { key: deps.attributionFieldKey },
8928
9090
  locationInput: deps.locationInput,
8929
9091
  ...onBudgetSelected === void 0 ? {} : { onBudgetSelected },
@@ -9040,6 +9202,7 @@ var runBudgetRound = async (deps) => {
9040
9202
  }
9041
9203
  current = selection;
9042
9204
  const result = await runAnswerStep({
9205
+ answers: deps.answers,
9043
9206
  attributionFieldKey: "attribution",
9044
9207
  budgetOptions: scoped,
9045
9208
  draft,
@@ -9074,32 +9237,65 @@ var runBudgetRound = async (deps) => {
9074
9237
  }
9075
9238
  };
9076
9239
 
9240
+ // src/tui-entry/v2-budget-picker/budget-view/budget-view.ts
9241
+ init_ux();
9242
+ var budgetView = (fields, global) => {
9243
+ const field = (key) => fields.find((entry) => entry.key === key);
9244
+ const attribution = field(ATTRIBUTION_FIELD_KEY);
9245
+ if (attribution === void 0) return void 0;
9246
+ const orgs = field(ORGANISATION_FIELD_KEY);
9247
+ const folder = {
9248
+ answers: {},
9249
+ budgetOptions: attribution.options ?? [],
9250
+ budgetTitle: attribution.title,
9251
+ notice: attribution.description,
9252
+ orgTitle: orgs?.title,
9253
+ orgs: orgs?.options ?? []
9254
+ };
9255
+ if (!global) return folder;
9256
+ if (field(ATTRIBUTION_SCOPE_FIELD_KEY) === void 0) return void 0;
9257
+ const notice = V2_CONNECT_COPY.globalKeyNotice;
9258
+ const copy = budgetQuestionCopy({ notice });
9259
+ return {
9260
+ ...folder,
9261
+ answers: GLOBAL_KEY_ANSWER,
9262
+ budgetOptions: folder.budgetOptions.filter(
9263
+ ({ value }) => isPersonalSelection(value)
9264
+ ),
9265
+ budgetTitle: copy.attribution.title,
9266
+ notice,
9267
+ orgTitle: copy.organisation
9268
+ };
9269
+ };
9270
+
9077
9271
  // src/tui-entry/v2-budget-picker/v2-budget-picker.ts
9078
9272
  var v2BudgetPicker = async (deps) => {
9079
9273
  if (!deps.unattributed()) {
9080
9274
  deps.toast({ message: V2_CONNECT_COPY.success, variant: "success" });
9081
9275
  return;
9082
9276
  }
9083
- const attributionField = deps.formFields.find(
9084
- (field) => field.key === ATTRIBUTION_FIELD_KEY
9085
- );
9086
- if (attributionField === void 0) {
9277
+ const view = budgetView(deps.formFields, deps.global === true);
9278
+ if (view === void 0) {
9087
9279
  deps.toast({ message: V2_CONNECT_COPY.manualFallback, variant: "error" });
9088
9280
  return;
9089
9281
  }
9090
- const orgField = deps.formFields.find(
9091
- (field) => field.key === ORGANISATION_FIELD_KEY
9092
- );
9282
+ if (view.notice !== void 0) {
9283
+ await deps.dialog.alert({
9284
+ message: view.notice,
9285
+ title: V2_CONNECT_COPY.busyTitle
9286
+ });
9287
+ }
9093
9288
  for (; ; ) {
9094
9289
  const round = await runBudgetRound({
9095
- budgetOptions: attributionField.options ?? [],
9096
- budgetTitle: attributionField.title,
9290
+ answers: view.answers,
9291
+ budgetOptions: view.budgetOptions,
9292
+ budgetTitle: view.budgetTitle,
9097
9293
  connect: deps.connect,
9098
9294
  context: deps.context,
9099
9295
  dialog: deps.dialog,
9100
9296
  locationInput: deps.locationInput,
9101
- orgTitle: orgField?.title,
9102
- orgs: orgField?.options ?? [],
9297
+ orgTitle: view.orgTitle,
9298
+ orgs: view.orgs,
9103
9299
  pollTerminal: deps.pollTerminal,
9104
9300
  toast: deps.toast,
9105
9301
  unattributed: deps.unattributed
@@ -9253,6 +9449,7 @@ var v2GuidedConnect = async (deps) => {
9253
9449
  context,
9254
9450
  dialog,
9255
9451
  formFields,
9452
+ global: deps.global,
9256
9453
  locationInput,
9257
9454
  pollTerminal,
9258
9455
  toast,
@@ -9295,7 +9492,7 @@ var v2Autoconnect = async (context, options = {}) => {
9295
9492
  if (directory === void 0) return;
9296
9493
  const unattributed = () => readAttribution({
9297
9494
  directory,
9298
- outsideWorktree: "return-undefined"
9495
+ outsideWorktree: "use-global"
9299
9496
  }) === void 0;
9300
9497
  const dialog = context.ui.dialog;
9301
9498
  if (dialog !== void 0) {
@@ -9321,63 +9518,34 @@ var v2Autoconnect = async (context, options = {}) => {
9321
9518
  });
9322
9519
  };
9323
9520
 
9324
- // src/tui-entry/show-r33-menu/run-action/run-action.ts
9325
- var runR33MenuAction = async (context, action, deps) => {
9326
- try {
9327
- const handlers = deps.handlers();
9328
- const local = () => executeR33Command({
9329
- budgetAction: "Attribute",
9330
- handlers: handlers && {
9331
- ...handlers,
9332
- "inspect-session": async () => {
9333
- const inspection = await handlers["inspect-session"]();
9334
- return inspection.connected || deps.signedIn === void 0 ? inspection : { ...inspection, connected: await deps.signedIn() };
9335
- }
9336
- },
9337
- promptText: `/r33 ${action}`
9338
- });
9339
- const reply = action === "rotate" && deps.rotate !== void 0 ? await deps.rotate() : await local();
9340
- context.ui.toast.show({ message: reply, variant: "info" });
9341
- } catch (error) {
9342
- context.ui.toast.show({
9343
- message: error instanceof Error ? error.message : String(error),
9344
- variant: "error"
9345
- });
9521
+ // src/tui-entry/show-r33-menu/run-budget-entry/run-budget-entry.ts
9522
+ var runBudgetEntry = async (context, entry, state) => {
9523
+ if (entry === "global") {
9524
+ await v2GuidedConnect({ context, global: true, unattributed: () => true });
9525
+ return;
9346
9526
  }
9527
+ if (readAttribution(state) === void 0) {
9528
+ await v2Autoconnect(context, { directory: state.directory });
9529
+ return;
9530
+ }
9531
+ await v2GuidedConnect({ context, unattributed: () => true });
9347
9532
  };
9348
9533
 
9349
9534
  // src/tui-entry/show-r33-menu/show-r33-menu.ts
9350
- var menuOptions2 = [
9351
- {
9352
- title: "Attribute \u2014 pick this folder's budget again",
9353
- value: "attribute"
9354
- },
9355
- {
9356
- title: "Inspect session \u2014 attribution and key metadata",
9357
- value: "inspect"
9358
- },
9359
- {
9360
- title: "Rotate key now",
9361
- value: "rotate"
9362
- }
9363
- ];
9364
9535
  var showR33Menu = async (context, deps) => {
9536
+ const location = context.location ?? context.data?.location.default();
9537
+ const state = location && {
9538
+ directory: location.directory,
9539
+ outsideWorktree: "use-global"
9540
+ };
9365
9541
  const selection = await context.ui.dialog.select({
9366
9542
  title: "Route 33 \u2014 choose an action",
9367
- options: menuOptions2
9543
+ options: r33MenuOptions(state !== void 0 && isGlobalAttribution(state))
9368
9544
  });
9369
9545
  if (selection === void 0) return;
9370
- if (selection === "attribute") {
9371
- const location = context.location ?? context.data?.location.default();
9372
- if (location === void 0 || readEnvDirectApiKey() !== void 0) return;
9373
- if (readAttribution({
9374
- directory: location.directory,
9375
- outsideWorktree: "return-undefined"
9376
- }) === void 0) {
9377
- await v2Autoconnect(context, { directory: location.directory });
9378
- } else {
9379
- await v2GuidedConnect({ context, unattributed: () => true });
9380
- }
9546
+ if (selection === "attribute" || selection === "global") {
9547
+ if (state === void 0 || readEnvDirectApiKey() !== void 0) return;
9548
+ await runBudgetEntry(context, selection, state);
9381
9549
  return;
9382
9550
  }
9383
9551
  await runR33MenuAction(context, selection, deps);
@@ -9446,7 +9614,6 @@ var v2BootMatrix = async ({
9446
9614
  probe,
9447
9615
  assumedAlive = false,
9448
9616
  attributed,
9449
- globalFallback,
9450
9617
  wizard
9451
9618
  }) => {
9452
9619
  if (!assumedAlive && await probe() === "dead") {
@@ -9455,9 +9622,6 @@ var v2BootMatrix = async ({
9455
9622
  if (attributed()) {
9456
9623
  return { kind: "silent" };
9457
9624
  }
9458
- if (globalFallback !== void 0 && await globalFallback()) {
9459
- return { kind: "silent" };
9460
- }
9461
9625
  await wizard();
9462
9626
  return { kind: "wizard-settled" };
9463
9627
  };
@@ -9626,6 +9790,35 @@ var v2SetupLock = (deps) => {
9626
9790
  return { start: settle };
9627
9791
  };
9628
9792
 
9793
+ // src/tui-entry/v2-single-account/v2-single-account.ts
9794
+ init_predicates();
9795
+ var registerV2SingleAccount = (deps) => {
9796
+ const list = deps.client?.credential?.list;
9797
+ const remove = deps.client?.credential?.remove;
9798
+ if (list === void 0 || remove === void 0) return;
9799
+ const prune = async () => {
9800
+ const accounts = (await list()).filter(
9801
+ ({ integrationID }) => integrationID === "route33"
9802
+ );
9803
+ const kept = accounts.find(({ active }) => active) ?? accounts.at(-1);
9804
+ for (const { id } of accounts) {
9805
+ if (id !== kept?.id) await remove({ credentialID: id });
9806
+ }
9807
+ };
9808
+ let queue = Promise.resolve();
9809
+ const schedule = () => {
9810
+ queue = queue.then(prune).catch(() => void 0);
9811
+ };
9812
+ schedule();
9813
+ deps.data?.listen?.(({ details }) => {
9814
+ const event = details;
9815
+ const data = isRecord(event) ? event["data"] : void 0;
9816
+ if (details.type === "credential.switched" && isRecord(data) && data["integrationID"] === "route33" && typeof data["credentialID"] === "string") {
9817
+ schedule();
9818
+ }
9819
+ });
9820
+ };
9821
+
9629
9822
  // src/tui-entry/v2-tui-runtime/v2-tui-runtime.ts
9630
9823
  init_cli();
9631
9824
  init_host_storage_secret_store2();
@@ -9737,7 +9930,7 @@ var composeV2TuiRuntime = (context) => {
9737
9930
  }),
9738
9931
  state: {
9739
9932
  directory: location?.directory ?? "",
9740
- outsideWorktree: "return-undefined"
9933
+ outsideWorktree: "use-global"
9741
9934
  }
9742
9935
  });
9743
9936
  };
@@ -9798,7 +9991,7 @@ var v2AutoSelectModel = async (context) => {
9798
9991
 
9799
9992
  // src/tui-entry/v2-tui/with-attribution-picker/with-attribution-picker.ts
9800
9993
  var withAttributionPicker = async (context, run) => {
9801
- const attempt = { complete: false };
9994
+ const attempt = { complete: false, global: false };
9802
9995
  let notice;
9803
9996
  const location = context.location ?? context.data?.location.default();
9804
9997
  const client = {
@@ -9807,10 +10000,17 @@ var withAttributionPicker = async (context, run) => {
9807
10000
  const integration = v2IntegrationSurface(context);
9808
10001
  const status = integration?.oauth?.status;
9809
10002
  if (!status) return integration;
10003
+ const connect = integration.oauth.connect;
9810
10004
  return {
9811
10005
  ...integration,
9812
10006
  oauth: {
9813
10007
  ...integration.oauth,
10008
+ ...connect && {
10009
+ connect: (input) => {
10010
+ attempt.global ||= isGlobalKeyAnswer(input.answer ?? {});
10011
+ return connect(input);
10012
+ }
10013
+ },
9814
10014
  status: async (input) => {
9815
10015
  const result = await status(input);
9816
10016
  attempt.complete = result.data.status === "complete";
@@ -9836,7 +10036,7 @@ var withAttributionPicker = async (context, run) => {
9836
10036
  }
9837
10037
  }
9838
10038
  });
9839
- if (!attempt.complete) return;
10039
+ if (!attempt.complete || attempt.global) return;
9840
10040
  await notice;
9841
10041
  await v2AutoSelectModel({ ...context, location }).catch(() => {
9842
10042
  context.ui.toast.show({
@@ -9890,10 +10090,9 @@ var createV2Tui = () => ({
9890
10090
  const location = context.location ?? context.data?.location.default();
9891
10091
  const directory = location?.directory ?? "";
9892
10092
  const wizard = v2WizardControl();
9893
- let globalPersonalKey = false;
9894
10093
  const attributed = () => directory === "" ? false : readAttribution({
9895
10094
  directory,
9896
- outsideWorktree: "return-undefined"
10095
+ outsideWorktree: "use-global"
9897
10096
  }) !== void 0;
9898
10097
  const storedRoute33Token = () => {
9899
10098
  const box = context.storage?.store("route33_auth", {
@@ -9920,26 +10119,11 @@ var createV2Tui = () => ({
9920
10119
  readStoredToken: storedRoute33Token
9921
10120
  });
9922
10121
  };
9923
- const globalFallback = async () => {
9924
- if (detectWorktreeRoot(directory) !== void 0) return false;
9925
- const form = await v2AttributionForm(context).catch(() => void 0);
9926
- for (const option of form?.options ?? []) {
9927
- if (isPersonalSelection(option.value) && option.label.includes(PERSONAL_KEY_ACTIVE_MARKER)) {
9928
- return true;
9929
- }
9930
- }
9931
- return false;
9932
- };
9933
10122
  const automatic = (host) => v2Autoconnect(host, { superseded: wizard.superseded });
9934
10123
  const boot = async (options) => {
9935
10124
  await v2BootMatrix({
9936
10125
  assumedAlive: options?.assumedAlive === true,
9937
10126
  attributed,
9938
- globalFallback: async () => {
9939
- const silent = await globalFallback();
9940
- globalPersonalKey = silent;
9941
- return silent;
9942
- },
9943
10127
  probe: sessionProbe,
9944
10128
  wizard: () => withAttributionPicker({ ...context, location }, automatic)
9945
10129
  });
@@ -9954,7 +10138,7 @@ var createV2Tui = () => ({
9954
10138
  }
9955
10139
  },
9956
10140
  mode: context.keymap?.mode,
9957
- pending: () => v2SetupDirectory({ location: { directory } }) !== void 0 && !globalPersonalKey,
10141
+ pending: () => v2SetupDirectory({ location: { directory } }) !== void 0,
9958
10142
  run: boot
9959
10143
  });
9960
10144
  void lock.start().catch(() => void 0);
@@ -9984,6 +10168,7 @@ var createV2Tui = () => ({
9984
10168
  location,
9985
10169
  runtime: tuiRuntime
9986
10170
  });
10171
+ registerV2SingleAccount({ client: context.client, data: context.data });
9987
10172
  }
9988
10173
  });
9989
10174