@yagni-app/code 0.2.1 → 0.3.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.
Files changed (98) hide show
  1. package/dist/cli.d.ts +30 -0
  2. package/dist/cli.js +147 -3
  3. package/dist/connectClaudeCode.d.ts +77 -0
  4. package/dist/connectClaudeCode.js +228 -0
  5. package/dist/connectCodex.d.ts +75 -0
  6. package/dist/connectCodex.js +201 -0
  7. package/dist/doctor.d.ts +1 -1
  8. package/dist/doctor.js +1 -1
  9. package/dist/extension/advisor.d.ts +4 -4
  10. package/dist/extension/advisor.js +6 -7
  11. package/dist/extension/approvedPrefixes.d.ts +103 -0
  12. package/dist/extension/approvedPrefixes.js +282 -0
  13. package/dist/extension/askAdvisorTool.d.ts +20 -5
  14. package/dist/extension/askAdvisorTool.js +126 -20
  15. package/dist/extension/askYagniTool.d.ts +23 -0
  16. package/dist/extension/askYagniTool.js +91 -2
  17. package/dist/extension/branding.d.ts +34 -3
  18. package/dist/extension/branding.js +114 -13
  19. package/dist/extension/chipEditor.d.ts +30 -9
  20. package/dist/extension/chipEditor.js +173 -59
  21. package/dist/extension/claudeRules.d.ts +0 -2
  22. package/dist/extension/claudeRules.js +0 -8
  23. package/dist/extension/cmux/dispatcher.d.ts +25 -0
  24. package/dist/extension/cmux/dispatcher.js +266 -0
  25. package/dist/extension/cmux/hooks.d.ts +12 -0
  26. package/dist/extension/cmux/hooks.js +192 -0
  27. package/dist/extension/cmux/index.d.ts +3 -0
  28. package/dist/extension/cmux/index.js +155 -0
  29. package/dist/extension/cmux/naming.d.ts +5 -0
  30. package/dist/extension/cmux/naming.js +23 -0
  31. package/dist/extension/cmux/state.d.ts +33 -0
  32. package/dist/extension/cmux/state.js +142 -0
  33. package/dist/extension/config.d.ts +44 -1
  34. package/dist/extension/config.js +37 -4
  35. package/dist/extension/costHud.d.ts +16 -22
  36. package/dist/extension/costHud.js +8 -47
  37. package/dist/extension/crashReport.js +1 -3
  38. package/dist/extension/execPolicy.d.ts +135 -0
  39. package/dist/extension/execPolicy.js +936 -0
  40. package/dist/extension/flywheel.d.ts +44 -0
  41. package/dist/extension/flywheel.js +53 -0
  42. package/dist/extension/footer.d.ts +118 -0
  43. package/dist/extension/footer.js +308 -0
  44. package/dist/extension/guardian.d.ts +139 -0
  45. package/dist/extension/guardian.js +237 -0
  46. package/dist/extension/index.d.ts +35 -7
  47. package/dist/extension/index.js +338 -33
  48. package/dist/extension/mineBeat.d.ts +95 -0
  49. package/dist/extension/mineBeat.js +193 -0
  50. package/dist/extension/permission.d.ts +124 -10
  51. package/dist/extension/permission.js +603 -52
  52. package/dist/extension/pipeline/childRegistry.d.ts +41 -0
  53. package/dist/extension/pipeline/childRegistry.js +118 -0
  54. package/dist/extension/pipeline/finish.js +5 -1
  55. package/dist/extension/pipeline/goCommand.d.ts +1 -1
  56. package/dist/extension/pipeline/goCommand.js +40 -9
  57. package/dist/extension/pipeline/goStatusCommands.d.ts +10 -0
  58. package/dist/extension/pipeline/goStatusCommands.js +61 -1
  59. package/dist/extension/pipeline/personas.js +26 -1
  60. package/dist/extension/pipeline/resilience.d.ts +2 -1
  61. package/dist/extension/pipeline/resilience.js +21 -2
  62. package/dist/extension/pipeline/runRegistry.d.ts +23 -1
  63. package/dist/extension/pipeline/runRegistry.js +57 -1
  64. package/dist/extension/pipeline/runner.js +4 -0
  65. package/dist/extension/pipeline/verify.d.ts +4 -0
  66. package/dist/extension/pipeline/verify.js +48 -26
  67. package/dist/extension/recordDecisionTool.d.ts +8 -0
  68. package/dist/extension/recordDecisionTool.js +24 -0
  69. package/dist/extension/redact.d.ts +20 -0
  70. package/dist/extension/redact.js +64 -0
  71. package/dist/extension/rerouteNotice.d.ts +3 -4
  72. package/dist/extension/rerouteNotice.js +20 -11
  73. package/dist/extension/subagentRender.d.ts +129 -0
  74. package/dist/extension/subagentRender.js +441 -0
  75. package/dist/extension/subagents.d.ts +11 -8
  76. package/dist/extension/subagents.js +162 -37
  77. package/dist/extension/ticketTools.d.ts +37 -0
  78. package/dist/extension/ticketTools.js +117 -0
  79. package/dist/extension/todos.d.ts +28 -1
  80. package/dist/extension/todos.js +76 -1
  81. package/dist/extension/tokenProvider.js +46 -5
  82. package/dist/extension/ultra.d.ts +27 -0
  83. package/dist/extension/ultra.js +76 -0
  84. package/dist/launch.d.ts +7 -0
  85. package/dist/launch.js +24 -12
  86. package/dist/login.d.ts +4 -2
  87. package/dist/login.js +19 -4
  88. package/dist/padding.d.ts +22 -0
  89. package/dist/padding.js +25 -0
  90. package/dist/promptEnrichment.d.ts +40 -0
  91. package/dist/promptEnrichment.js +85 -0
  92. package/dist/signalForward.d.ts +60 -0
  93. package/dist/signalForward.js +130 -0
  94. package/dist/token.d.ts +25 -0
  95. package/dist/token.js +45 -0
  96. package/package.json +6 -5
  97. package/dist/extension/boostCommand.d.ts +0 -144
  98. package/dist/extension/boostCommand.js +0 -263
package/dist/token.js ADDED
@@ -0,0 +1,45 @@
1
+ /**
2
+ * `yagni token` — print the active environment's API token to stdout.
3
+ *
4
+ * This is the credential feed for `yagni connect claude-code`: Claude Code's
5
+ * `apiKeyHelper` runs this command (re-running on 401 and on a TTL), so the
6
+ * contract is strict — stdout carries the TOKEN AND NOTHING ELSE (Claude Code
7
+ * v2.1.227+ rejects helpers that print banners), and every notice goes to
8
+ * stderr. Riding the same refresh-at-launch client as the launcher means a
9
+ * token that enters the 7-day rotation window is rotated and persisted here
10
+ * too, so a Claude Code session keeps working without the user ever running
11
+ * `yagni` itself.
12
+ */
13
+ import { classifyTokenExpiry } from "./launch.js";
14
+ import { maybeRefreshAtLaunch } from "./refresh.js";
15
+ import { credentialsFromProfile, persistProfileTokenRotation, readActiveProfile, } from "./profiles.js";
16
+ export async function tokenCommand(deps = {}) {
17
+ const readProfile = deps.readProfile ?? readActiveProfile;
18
+ const refresh = deps.refresh ?? maybeRefreshAtLaunch;
19
+ const persist = deps.persist ?? persistProfileTokenRotation;
20
+ const stdout = deps.stdout ?? ((t) => process.stdout.write(t));
21
+ const stderr = deps.stderr ?? ((t) => process.stderr.write(t));
22
+ const profile = await readProfile();
23
+ let creds = credentialsFromProfile(profile);
24
+ if (!creds?.token) {
25
+ stderr(`Not logged in to environment "${profile.name}" (${profile.baseUrl}). Run \`yagni login\` first.\n`);
26
+ return 1;
27
+ }
28
+ const outcome = await refresh(creds, {
29
+ persist: (c) => persist(profile.name, c),
30
+ ...(deps.now ? { now: deps.now } : {}),
31
+ });
32
+ for (const warning of outcome.warnings)
33
+ stderr(`${warning}\n`);
34
+ creds = outcome.creds;
35
+ // An expired token would send the consumer into a silent 401 loop — fail
36
+ // loudly instead so Claude Code surfaces a helper error the user can act on.
37
+ const nowMs = deps.now ? deps.now() : Date.now();
38
+ if (classifyTokenExpiry(creds.expiresAt, nowMs).kind === "expired") {
39
+ stderr("Your YAGNI Code session has expired. Run `yagni login` to re-authenticate.\n");
40
+ return 1;
41
+ }
42
+ stdout(`${creds.token}\n`);
43
+ return 0;
44
+ }
45
+ //# sourceMappingURL=token.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yagni-app/code",
3
- "version": "0.2.1",
3
+ "version": "0.3.1",
4
4
  "description": "YAGNI Code: a terminal coding agent that already knows your company. One YAGNI login routes the model and grounds the agent in your team's context.",
5
5
  "license": "SEE LICENSE IN LICENSE.md",
6
6
  "author": "YAGNI, Inc. <jack@yagni.app> (https://yagni.app)",
@@ -34,9 +34,10 @@
34
34
  "main": "./dist/cli.js",
35
35
  "types": "./dist/cli.d.ts",
36
36
  "dependencies": {
37
- "@earendil-works/pi-coding-agent": "0.83.0",
38
- "@earendil-works/pi-tui": "0.83.0",
39
- "typebox": "^1.1.38"
37
+ "@earendil-works/pi-coding-agent": "0.84.1",
38
+ "@earendil-works/pi-tui": "0.84.1",
39
+ "smol-toml": "^1.8.0",
40
+ "typebox": "^1.3.11"
40
41
  },
41
- "yagniSourceSha": "33b62c8a01064f7681085e441aa00b02693b13c7"
42
+ "yagniSourceSha": "27482c0e8f72e0f171de23bd39e774d9661c0bc6"
42
43
  }
@@ -1,144 +0,0 @@
1
- /**
2
- * `/boost` — the sanctioned session-scoped escalation to Peak (spec §7).
3
- *
4
- * The session default is the `balanced` tier. `/boost` flips the
5
- * DRIVER session's live model to `peak` until `/boost off` or the process
6
- * exits; `peak` stays directly pickable through pi's own model picker too,
7
- * this just adds a one-word lever plus an attribution flag.
8
- *
9
- * Two halves, same split as `advisor.ts` / `permission.ts`:
10
- *
11
- * - `boostOn` / `boostOff` are PURE. They own every transition and the exact
12
- * notice copy; no pi, no I/O, no env. That is what needs exhaustive tests.
13
- * - `registerBoostCommand` is the wiring: it applies the pure result's
14
- * `targetModelId` through pi's real model-switch API, toggles
15
- * `process.env.YAGNI_BOOST`, and paints the notice + a status-bar chip.
16
- *
17
- * The model-switch API (verified against pi 0.83.0's typings, not guessed):
18
- * `ctx.modelRegistry.find(provider, modelId)` resolves a tier id to a
19
- * `Model`, and `pi.setModel(model)` (the top-level `ExtensionAPI` method, NOT
20
- * `ctx.setModel` — `ExtensionCommandContext` does not expose a setter) applies
21
- * it to the live session, resolving `false` when no API key is configured.
22
- * The `yagni` provider's catalog entries key `id` on the tier id itself (see
23
- * `provider.ts`), so `peak` is the pi model id for the peak tier.
24
- *
25
- * State lives in the registration closure — module/session-scoped, mirroring
26
- * `costHud.ts`'s accumulator — because a session's process exit is the only
27
- * "end" there is; nothing needs to persist across it. `registerBoostCommand`
28
- * returns a small `{ isBoosted() }` handle onto that same closure so other
29
- * registrations (currently just `/cost`, see below) can read the live flag
30
- * without a second source of truth.
31
- *
32
- * Picker-drift guard (spec review item 3): pi's OWN model picker (Ctrl+P,
33
- * `/model`) can change the live model independently of `/boost` in either
34
- * direction, so `state.active` can go stale relative to `ctx.model.id`. Both
35
- * halves read the live model and reconcile rather than trusting `state.active`
36
- * blindly:
37
- *
38
- * - `boostOn` while already active but the live model has drifted OFF peak
39
- * (the user cycled models mid-boost without `/boost off`): re-applies
40
- * peak instead of a silent "Already boosted." that would leave the
41
- * driver quietly running a cheaper tier under an attribution header that
42
- * claims otherwise. The remembered `priorModelId` is left untouched — the
43
- * tier to restore is still whatever was active before the ORIGINAL boost,
44
- * not the tier the user happened to drift to.
45
- * - `boostOff` while active but the live model is no longer peak (same
46
- * drift, encountered from the other command): the user already left
47
- * boost manually, so forcing a switch back to the remembered prior tier
48
- * would clobber a choice they just made on purpose. Instead this clears
49
- * `/boost`'s own bookkeeping (state, env, chip) without touching the
50
- * model, and names where they actually landed.
51
- *
52
- * KNOWN asymmetry, documented rather than worked around: `YAGNI_BOOST=1`
53
- * while boosted makes every CHILD process spawned during the boost (a /go
54
- * run, a subagent, an advisor consult) inherit it, and those children's
55
- * completions carry `x-yagni-boost` because their provider is registered
56
- * fresh per child. The DRIVER's own completions do not gain that header
57
- * retroactively — `buildYagniProvider`'s headers are baked into the
58
- * `registerProvider` call once, at session start, from the env snapshot at
59
- * that moment (see `provider.ts` / `attributionHeaders`). Re-registering the
60
- * provider mid-session to pick up a new header is explicitly NOT the fix
61
- * here: it would race the in-flight request the switch itself triggers and
62
- * has no test coverage as a live-swap path.
63
- *
64
- * This is exactly why `/cost` cannot rely on server rows alone: the server's
65
- * per-tier "Boosted (tier) spend" subtotal (`formatServerCostLines`, Task 7)
66
- * only ever sees rows carrying `x-yagni-boost` — i.e. `/go` children, never
67
- * the driver's own turns. A session that only chats while boosted would
68
- * otherwise show no boost line at all. `registerCostCommand`'s `isBoosted`
69
- * dep (threaded from THIS module's return handle, the same way
70
- * `advisorSubtotal` is threaded from `advisor.ts`) closes that gap: `/cost`
71
- * appends a fixed "Boost is on. Driver turns bill at the peak tier." line
72
- * whenever the live toggle is on, on BOTH the server-authoritative and the
73
- * local-fallback branch, independent of what server rows happen to show.
74
- */
75
- import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
76
- /** Session-scoped boost state, held in the registration closure. */
77
- export interface BoostState {
78
- active: boolean;
79
- /** The tier to restore on `/boost off`. Set only while `active`. */
80
- priorModelId?: string;
81
- }
82
- /** The pure outcome of a `/boost` transition. */
83
- export interface BoostResult {
84
- state: BoostState;
85
- /** Exact notice text to surface to the user. */
86
- notice: string;
87
- /** The tier id to switch the session to, or undefined for a pure no-op. */
88
- targetModelId?: string;
89
- }
90
- /**
91
- * Turn boost on. PURE.
92
- *
93
- * - Already active AND the live model is still `peak`: genuine no-op,
94
- * "Already boosted." (no switch — nothing about the prior tier changes).
95
- * - Already active but the live model has drifted off `peak` (picker-drift
96
- * guard 3b, see module docblock): re-applies `peak`, keeping the ORIGINAL
97
- * `priorModelId` rather than adopting the drifted-to tier.
98
- * - Not active, current model IS already `peak`: activates anyway (so
99
- * attribution and `/boost off` behave correctly) with a distinct notice,
100
- * remembering `peak` itself as the tier to restore.
101
- * - Not active, otherwise: remembers the current tier, targets `peak`.
102
- */
103
- export declare function boostOn(state: BoostState, currentModelId: string | undefined): BoostResult;
104
- /**
105
- * Turn boost off. PURE.
106
- *
107
- * - Not active: no-op, "Boost is not on."
108
- * - Active but the live model is no longer `peak` (picker-drift guard 3a,
109
- * see module docblock): the user already left boost manually. Clears
110
- * `/boost`'s own bookkeeping WITHOUT a switch — forcing one back to the
111
- * remembered prior tier would clobber a choice just made on purpose — and
112
- * names the model they actually landed on.
113
- * - Active and still on `peak`: restores the remembered prior tier (falling
114
- * back to the session default when none was recorded, which should not
115
- * normally happen). When the prior tier is itself `peak` (the
116
- * already-on-Peak activation path), the resulting switch is a no-op in
117
- * effect — still applied, harmlessly.
118
- */
119
- export declare function boostOff(state: BoostState, currentModelId: string | undefined): BoostResult;
120
- /** Injectable seams for `registerBoostCommand`. */
121
- export interface RegisterBoostCommandDeps {
122
- /** Environment `YAGNI_BOOST` is toggled on. Defaults to `process.env`. */
123
- env?: NodeJS.ProcessEnv;
124
- }
125
- /** What `registerBoostCommand` hands back so other registrations (`/cost`) can read the live flag. */
126
- export interface BoostCommandHandle {
127
- /** Whether the session is currently boosted, read fresh off the same closure `/boost` mutates. */
128
- isBoosted(): boolean;
129
- }
130
- /**
131
- * Register the `/boost` command.
132
- *
133
- * `/boost` (no args) turns boost on; `/boost off` turns it off; any other
134
- * argument shows a usage notice and changes nothing. Every transition that
135
- * requires a model switch runs it through pi's real API and is guarded end to
136
- * end: state and `YAGNI_BOOST` are only ever committed together, AFTER the
137
- * switch succeeds (or is deliberately skipped — the picker-drift guards), so
138
- * a half-applied boost (env set but model unchanged, or vice versa) can never
139
- * happen. A failed ON reports "Boost failed."; a failed OFF reports a
140
- * DISTINCT message, because the failure mode is materially worse — the
141
- * driver is left still running (and billing) on Peak, not merely unchanged.
142
- */
143
- export declare function registerBoostCommand(pi: ExtensionAPI, deps?: RegisterBoostCommandDeps): BoostCommandHandle;
144
- //# sourceMappingURL=boostCommand.d.ts.map
@@ -1,263 +0,0 @@
1
- /**
2
- * `/boost` — the sanctioned session-scoped escalation to Peak (spec §7).
3
- *
4
- * The session default is the `balanced` tier. `/boost` flips the
5
- * DRIVER session's live model to `peak` until `/boost off` or the process
6
- * exits; `peak` stays directly pickable through pi's own model picker too,
7
- * this just adds a one-word lever plus an attribution flag.
8
- *
9
- * Two halves, same split as `advisor.ts` / `permission.ts`:
10
- *
11
- * - `boostOn` / `boostOff` are PURE. They own every transition and the exact
12
- * notice copy; no pi, no I/O, no env. That is what needs exhaustive tests.
13
- * - `registerBoostCommand` is the wiring: it applies the pure result's
14
- * `targetModelId` through pi's real model-switch API, toggles
15
- * `process.env.YAGNI_BOOST`, and paints the notice + a status-bar chip.
16
- *
17
- * The model-switch API (verified against pi 0.83.0's typings, not guessed):
18
- * `ctx.modelRegistry.find(provider, modelId)` resolves a tier id to a
19
- * `Model`, and `pi.setModel(model)` (the top-level `ExtensionAPI` method, NOT
20
- * `ctx.setModel` — `ExtensionCommandContext` does not expose a setter) applies
21
- * it to the live session, resolving `false` when no API key is configured.
22
- * The `yagni` provider's catalog entries key `id` on the tier id itself (see
23
- * `provider.ts`), so `peak` is the pi model id for the peak tier.
24
- *
25
- * State lives in the registration closure — module/session-scoped, mirroring
26
- * `costHud.ts`'s accumulator — because a session's process exit is the only
27
- * "end" there is; nothing needs to persist across it. `registerBoostCommand`
28
- * returns a small `{ isBoosted() }` handle onto that same closure so other
29
- * registrations (currently just `/cost`, see below) can read the live flag
30
- * without a second source of truth.
31
- *
32
- * Picker-drift guard (spec review item 3): pi's OWN model picker (Ctrl+P,
33
- * `/model`) can change the live model independently of `/boost` in either
34
- * direction, so `state.active` can go stale relative to `ctx.model.id`. Both
35
- * halves read the live model and reconcile rather than trusting `state.active`
36
- * blindly:
37
- *
38
- * - `boostOn` while already active but the live model has drifted OFF peak
39
- * (the user cycled models mid-boost without `/boost off`): re-applies
40
- * peak instead of a silent "Already boosted." that would leave the
41
- * driver quietly running a cheaper tier under an attribution header that
42
- * claims otherwise. The remembered `priorModelId` is left untouched — the
43
- * tier to restore is still whatever was active before the ORIGINAL boost,
44
- * not the tier the user happened to drift to.
45
- * - `boostOff` while active but the live model is no longer peak (same
46
- * drift, encountered from the other command): the user already left
47
- * boost manually, so forcing a switch back to the remembered prior tier
48
- * would clobber a choice they just made on purpose. Instead this clears
49
- * `/boost`'s own bookkeeping (state, env, chip) without touching the
50
- * model, and names where they actually landed.
51
- *
52
- * KNOWN asymmetry, documented rather than worked around: `YAGNI_BOOST=1`
53
- * while boosted makes every CHILD process spawned during the boost (a /go
54
- * run, a subagent, an advisor consult) inherit it, and those children's
55
- * completions carry `x-yagni-boost` because their provider is registered
56
- * fresh per child. The DRIVER's own completions do not gain that header
57
- * retroactively — `buildYagniProvider`'s headers are baked into the
58
- * `registerProvider` call once, at session start, from the env snapshot at
59
- * that moment (see `provider.ts` / `attributionHeaders`). Re-registering the
60
- * provider mid-session to pick up a new header is explicitly NOT the fix
61
- * here: it would race the in-flight request the switch itself triggers and
62
- * has no test coverage as a live-swap path.
63
- *
64
- * This is exactly why `/cost` cannot rely on server rows alone: the server's
65
- * per-tier "Boosted (tier) spend" subtotal (`formatServerCostLines`, Task 7)
66
- * only ever sees rows carrying `x-yagni-boost` — i.e. `/go` children, never
67
- * the driver's own turns. A session that only chats while boosted would
68
- * otherwise show no boost line at all. `registerCostCommand`'s `isBoosted`
69
- * dep (threaded from THIS module's return handle, the same way
70
- * `advisorSubtotal` is threaded from `advisor.ts`) closes that gap: `/cost`
71
- * appends a fixed "Boost is on. Driver turns bill at the peak tier." line
72
- * whenever the live toggle is on, on BOTH the server-authoritative and the
73
- * local-fallback branch, independent of what server rows happen to show.
74
- */
75
- /** The pi provider name the YAGNI catalog registers under (see `provider.ts`). */
76
- const YAGNI_PROVIDER = "yagni";
77
- /** The tier `/boost` escalates to. */
78
- const BOOST_TIER = "peak";
79
- /** The session default tier, restored when no prior tier is known. */
80
- const DEFAULT_TIER = "balanced";
81
- /** Tier ids are lowercase; every known tier's display name is just Title Case of the id. */
82
- function displayTierName(id) {
83
- return id.length === 0 ? id : id.charAt(0).toUpperCase() + id.slice(1);
84
- }
85
- /**
86
- * Turn boost on. PURE.
87
- *
88
- * - Already active AND the live model is still `peak`: genuine no-op,
89
- * "Already boosted." (no switch — nothing about the prior tier changes).
90
- * - Already active but the live model has drifted off `peak` (picker-drift
91
- * guard 3b, see module docblock): re-applies `peak`, keeping the ORIGINAL
92
- * `priorModelId` rather than adopting the drifted-to tier.
93
- * - Not active, current model IS already `peak`: activates anyway (so
94
- * attribution and `/boost off` behave correctly) with a distinct notice,
95
- * remembering `peak` itself as the tier to restore.
96
- * - Not active, otherwise: remembers the current tier, targets `peak`.
97
- */
98
- export function boostOn(state, currentModelId) {
99
- if (state.active) {
100
- if (currentModelId === BOOST_TIER) {
101
- return { state, notice: "Already boosted." };
102
- }
103
- return {
104
- state,
105
- notice: `Re-boosted to Peak. /boost off to return to ${displayTierName(state.priorModelId ?? DEFAULT_TIER)}.`,
106
- targetModelId: BOOST_TIER,
107
- };
108
- }
109
- if (currentModelId === BOOST_TIER) {
110
- return {
111
- state: { active: true, priorModelId: BOOST_TIER },
112
- notice: "Already on Peak. /boost off returns to Peak.",
113
- targetModelId: BOOST_TIER,
114
- };
115
- }
116
- return {
117
- state: { active: true, priorModelId: currentModelId },
118
- notice: `Boosted to Peak. /boost off to return to ${displayTierName(currentModelId ?? DEFAULT_TIER)}.`,
119
- targetModelId: BOOST_TIER,
120
- };
121
- }
122
- /**
123
- * Turn boost off. PURE.
124
- *
125
- * - Not active: no-op, "Boost is not on."
126
- * - Active but the live model is no longer `peak` (picker-drift guard 3a,
127
- * see module docblock): the user already left boost manually. Clears
128
- * `/boost`'s own bookkeeping WITHOUT a switch — forcing one back to the
129
- * remembered prior tier would clobber a choice just made on purpose — and
130
- * names the model they actually landed on.
131
- * - Active and still on `peak`: restores the remembered prior tier (falling
132
- * back to the session default when none was recorded, which should not
133
- * normally happen). When the prior tier is itself `peak` (the
134
- * already-on-Peak activation path), the resulting switch is a no-op in
135
- * effect — still applied, harmlessly.
136
- */
137
- export function boostOff(state, currentModelId) {
138
- if (!state.active) {
139
- return { state, notice: "Boost is not on." };
140
- }
141
- if (currentModelId !== BOOST_TIER) {
142
- return {
143
- state: { active: false, priorModelId: undefined },
144
- notice: `Boost off. Leaving the model on ${displayTierName(currentModelId ?? DEFAULT_TIER)}.`,
145
- };
146
- }
147
- const priorId = state.priorModelId ?? DEFAULT_TIER;
148
- return {
149
- state: { active: false, priorModelId: undefined },
150
- notice: `Back to ${displayTierName(priorId)}.`,
151
- targetModelId: priorId,
152
- };
153
- }
154
- /**
155
- * Resolve `tierId` to a live `Model` and apply it via `pi.setModel`.
156
- *
157
- * On a registry lookup miss, `allowFallback` (true only on the OFF path —
158
- * MINOR 5) retries once against the session default tier rather than
159
- * stranding the user on Peak over one missing catalog entry. Returns the
160
- * tier id that was ACTUALLY applied, so the caller can tell whether the
161
- * fallback fired and adjust the notice to name it honestly.
162
- *
163
- * @throws when neither the requested tier nor (if allowed) the fallback
164
- * resolves, or when `pi.setModel` itself throws or resolves `false`.
165
- */
166
- async function applyTier(pi, ctx, tierId, allowFallback) {
167
- let model = ctx.modelRegistry?.find(YAGNI_PROVIDER, tierId);
168
- let applied = tierId;
169
- if (!model && allowFallback && tierId !== DEFAULT_TIER) {
170
- model = ctx.modelRegistry?.find(YAGNI_PROVIDER, DEFAULT_TIER);
171
- applied = DEFAULT_TIER;
172
- }
173
- if (!model) {
174
- throw new Error(`Unknown YAGNI tier "${tierId}".`);
175
- }
176
- const ok = await pi.setModel(model);
177
- if (!ok) {
178
- throw new Error("setModel reported no API key configured.");
179
- }
180
- return applied;
181
- }
182
- /**
183
- * Register the `/boost` command.
184
- *
185
- * `/boost` (no args) turns boost on; `/boost off` turns it off; any other
186
- * argument shows a usage notice and changes nothing. Every transition that
187
- * requires a model switch runs it through pi's real API and is guarded end to
188
- * end: state and `YAGNI_BOOST` are only ever committed together, AFTER the
189
- * switch succeeds (or is deliberately skipped — the picker-drift guards), so
190
- * a half-applied boost (env set but model unchanged, or vice versa) can never
191
- * happen. A failed ON reports "Boost failed."; a failed OFF reports a
192
- * DISTINCT message, because the failure mode is materially worse — the
193
- * driver is left still running (and billing) on Peak, not merely unchanged.
194
- */
195
- export function registerBoostCommand(pi, deps = {}) {
196
- const env = deps.env ?? process.env;
197
- let state = { active: false };
198
- const paintStatus = (ctx, active) => {
199
- try {
200
- if (ctx.hasUI)
201
- ctx.ui.setStatus?.("yagni-boost", active ? "⚡ Peak" : undefined);
202
- }
203
- catch {
204
- // The chip is chrome; never let it break /boost.
205
- }
206
- };
207
- pi.registerCommand("boost", {
208
- description: "Escalate this session's model to Peak until /boost off or the session ends. /boost off returns to the prior tier.",
209
- handler: async (args, ctx) => {
210
- const notify = (message, type) => {
211
- if (ctx.hasUI)
212
- ctx.ui.notify(message, type);
213
- };
214
- // MINOR 4: case-insensitive "off" match, mirroring /mode's arg handling.
215
- const arg = args.trim().toLowerCase();
216
- if (arg !== "" && arg !== "off") {
217
- notify("Usage: /boost (turn on) or /boost off.", "warning");
218
- return;
219
- }
220
- const isOff = arg === "off";
221
- const wasActive = state.active;
222
- const result = isOff ? boostOff(state, ctx.model?.id) : boostOn(state, ctx.model?.id);
223
- try {
224
- let notice = result.notice;
225
- if (result.targetModelId) {
226
- const applied = await applyTier(pi, ctx, result.targetModelId, isOff);
227
- if (applied !== result.targetModelId) {
228
- // MINOR 5: the remembered/target tier no longer resolves in the
229
- // catalog; we landed on the session default instead of
230
- // stranding the user on Peak. Name the fallback so the notice
231
- // stays honest about what actually happened.
232
- notice = `Back to ${displayTierName(applied)} (could not restore ${displayTierName(result.targetModelId)}).`;
233
- }
234
- }
235
- state = result.state;
236
- // MINOR 6: only touch the env on a real active/inactive flip. Both
237
- // pure no-op paths ("Already boosted.", "Boost is not on.") return
238
- // `state` unchanged, so this never fires for them — an inherited
239
- // YAGNI_BOOST from outside this session is left exactly as found.
240
- if (state.active !== wasActive) {
241
- if (state.active) {
242
- env.YAGNI_BOOST = "1";
243
- }
244
- else {
245
- delete env.YAGNI_BOOST;
246
- }
247
- }
248
- notify(notice, "info");
249
- paintStatus(ctx, state.active);
250
- }
251
- catch {
252
- // IMPORTANT 1: a failed OFF is materially worse than a failed ON —
253
- // the driver is STILL on Peak, still billing at Peak rates, which a
254
- // generic "Boost failed." does not convey. Neither `state` nor
255
- // `YAGNI_BOOST` was touched above (the throw lands before both), so
256
- // the chip is also left exactly as it was: still lit.
257
- notify(isOff ? "Could not leave boost. Still on Peak; try /boost off again." : "Boost failed.", "error");
258
- }
259
- },
260
- });
261
- return { isBoosted: () => state.active };
262
- }
263
- //# sourceMappingURL=boostCommand.js.map