teapot-coding-agent 0.35.29 → 0.35.31

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.
@@ -57,8 +57,19 @@ export class ServerAuth {
57
57
  }
58
58
  /** load persisted state (called once, before the server is connected) */
59
59
  async hydrate(data) {
60
- if (data)
61
- this.store = { ...data };
60
+ if (!data)
61
+ return;
62
+ this.store = { ...data };
63
+ /**
64
+ * Restore the PKCE verifier alongside the rest.
65
+ *
66
+ * `codeVerifier()` reads the in-memory field and throws when it is empty, so a rebuilt instance
67
+ * would refuse the exchange even though the verifier is on disk. MEASURED cause of Notion's
68
+ * `invalid_grant`: `saveCodeVerifier` never persisted, and even once it does, nothing read the
69
+ * value back into the field that the exchange actually uses.
70
+ */
71
+ if (data.codeVerifier)
72
+ this.verifier = data.codeVerifier;
62
73
  }
63
74
  /** the persisted payload, for the store */
64
75
  snapshot() {
@@ -78,8 +89,44 @@ export class ServerAuth {
78
89
  clientInformation() {
79
90
  if (!this.store.clientId)
80
91
  return undefined;
81
- if (this.store.clientInformation)
82
- return this.store.clientInformation;
92
+ /**
93
+ * A registration made with a DIFFERENT redirect URI cannot be reused.
94
+ *
95
+ * The callback listens on an ephemeral port (`server.listen(0)`), so `redirectUrl` carries a
96
+ * new port on every attempt. A registration is bound to the `redirect_uris` it was created
97
+ * with, and the authorize request must match — so a stored registration from another port would
98
+ * have to be re-made.
99
+ *
100
+ * MEASURED from this machine's real store: `clientInformation.redirect_uris` was
101
+ * `["http://127.0.0.1/callback"]` — NO PORT, because it was registered from the placeholder
102
+ * provider in `authFor()` — while every flow runs on `http://127.0.0.1:<port>/callback`. The
103
+ * browser completed the authorization, `codeVerifier` was persisted, and no `access_token` was
104
+ * ever written: the exchange never got past the URI check.
105
+ *
106
+ * Returning `undefined` makes the SDK register afresh against the CURRENT redirect URI, which
107
+ * is the only value that can match. The client id/secret are kept, so anything the server bound
108
+ * to them survives; only the redirect list is re-established.
109
+ */
110
+ const stored = this.store.clientInformation;
111
+ if (stored) {
112
+ const uris = Array.isArray(stored.redirect_uris)
113
+ ? stored.redirect_uris
114
+ : [];
115
+ /**
116
+ * Only a RECORDED mismatch forces a re-registration.
117
+ *
118
+ * An empty list means the server never told us which URI it bound — some registration
119
+ * responses omit `redirect_uris` entirely, and a record written by an older teapot has none.
120
+ * Treating "unknown" as "wrong" would re-register on every single flow and lose whatever the
121
+ * server bound to the client id, so absence keeps the registration.
122
+ */
123
+ if (uris.length === 0)
124
+ return stored;
125
+ if (uris.includes(this.redirectUrl))
126
+ return stored;
127
+ // a recorded URI that cannot match this run: re-register rather than send one it never saw
128
+ return undefined;
129
+ }
83
130
  return {
84
131
  client_id: this.store.clientId,
85
132
  ...(this.store.clientSecret ? { client_secret: this.store.clientSecret } : {}),
@@ -126,11 +173,34 @@ export class ServerAuth {
126
173
  async saveCodeVerifier(verifier) {
127
174
  this.verifier = verifier;
128
175
  this.store.codeVerifier = verifier;
176
+ /**
177
+ * PERSIST it, or the exchange cannot complete after the instance is rebuilt.
178
+ *
179
+ * The verifier is the secret half of PKCE: the authorization request carries its SHA-256
180
+ * challenge and the token request must present the verifier itself. Keeping it only in memory
181
+ * means any path that constructs a fresh `ServerAuth` — a restart while the browser tab is open,
182
+ * a reconnect, a second `authFor()` — exchanges with no verifier and the authorization server
183
+ * answers `invalid_grant`.
184
+ *
185
+ * MEASURED against the real store: `clientId`/`clientSecret`/`clientInformation` were written
186
+ * but `codeVerifier` was ABSENT, because this method was the one mutation that never persisted.
187
+ * Notion's `/token` returns `invalid_grant` for exactly this shape.
188
+ */
189
+ await this.persist(this.server, this.store);
129
190
  }
130
191
  codeVerifier() {
131
- if (!this.verifier)
192
+ /**
193
+ * Fall back to the STORE, not just the in-memory field.
194
+ *
195
+ * The field is populated by `saveCodeVerifier` (same instance) or by `hydrate` (a rebuilt one).
196
+ * Reading the store as a last resort means an instance that was hydrated before this field was
197
+ * restored still completes the exchange instead of throwing — the failure mode is a silent
198
+ * `invalid_grant` from the server, so being generous here costs nothing.
199
+ */
200
+ const v = this.verifier ?? this.store.codeVerifier;
201
+ if (!v)
132
202
  throw new Error("no PKCE verifier — the authorization flow did not start here");
133
- return this.verifier;
203
+ return v;
134
204
  }
135
205
  // ---- teapot-facing ----
136
206
  /** the url the browser must open, once `redirectToAuthorization` has been called */
@@ -90,9 +90,16 @@ export class McpRegistry {
90
90
  /**
91
91
  * The provider for one server, created on demand.
92
92
  *
93
- * The redirect URL is a placeholder until a flow starts — the SDK reads it when it
94
- * builds the authorization request, and by then `startOAuth` has set the real loopback
95
- * port. Keeping one long-lived placeholder would pin a port that is not listening.
93
+ * The redirect URL here is used for CLIENT REGISTRATION, not just for the authorization request:
94
+ * `clientMetadata.redirect_uris` is what gets registered on the first connect. A portless
95
+ * placeholder therefore registers a redirect URI the real flow can never match — MEASURED, that
96
+ * is exactly what happened (stored `redirect_uris: ["http://127.0.0.1/callback"]`), and it is why
97
+ * a completed Notion authorization left no access token.
98
+ *
99
+ * `ServerAuth.clientInformation()` now refuses a registration whose URI differs from the current
100
+ * one, so a stale record is re-made instead of reused. This value still cannot carry the real
101
+ * port — none is known until `startOAuth` opens one — but it is now harmless, and keeping a
102
+ * long-lived provider bound to a port that is not listening would be worse.
96
103
  */
97
104
  async authFor(name) {
98
105
  if (!this.store)
@@ -904,7 +904,19 @@ export function buildApp(master) {
904
904
  ]));
905
905
  return c.json({
906
906
  configPath: master.configPath,
907
- needsSetup: !master.configFileExists,
907
+ /**
908
+ * "First run" now means NO AI PROVIDER IS REGISTERED, not "no config file".
909
+ *
910
+ * The wizard that used to gate on the config file is gone. A config file can exist with no
911
+ * usable provider (the operator edited it by hand, or every provider was removed), and in
912
+ * that state the wizard did not appear and the app simply could not do anything. The
913
+ * question the UI actually needs answered is "can this thing talk to a model yet?".
914
+ *
915
+ * A provider counts as registered when it has an entry at all — a key may legitimately be
916
+ * absent for a local endpoint (ollama, a proxy that authenticates by network), so requiring
917
+ * one would nag about a working setup.
918
+ */
919
+ needsSetup: Object.keys(master.config.providers ?? {}).length === 0,
908
920
  providers: mask(master.config.providers),
909
921
  defaultProvider: master.config.defaultProvider,
910
922
  progressIntervalMs: master.config.progressIntervalMs,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "teapot-coding-agent",
3
- "version": "0.35.29",
3
+ "version": "0.35.31",
4
4
  "description": "A lightweight, always-on multi-agent harness for AI coding agents",
5
5
  "type": "module",
6
6
  "license": "AGPL-3.0-or-later",