Skip to content

fix(licensing): try EVOLUTION_OPERATOR_EMAIL when the global API key is not a license - #2814

Merged
daniloleonecarneiro merged 3 commits into
developfrom
fix/license-operator-email-fallback
Dec 2, 2026
Merged

daniloleonecarneiro merged 3 commits into
developfrom
fix/license-operator-email-fallback

Conversation

@daniloleonecarneiro

@daniloleonecarneiro daniloleonecarneiro commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

📋 Description

EVOLUTION_OPERATOR_EMAIL auto-activation never ran. initializeRuntime only tried it when there was no stored license and no global API key, but AUTHENTICATION_API_KEY always has a value (it falls back to a default in env.config.ts). When the global key is not a licensing key, the boot printed the registration banner and stopped there.

Now, when the global key is rejected by the licensing server, the boot tries the operator e-mail before falling back to the browser registration flow. Both "no key" paths share autoRegisterOrWait.

🧪 Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)

🧪 Testing

Local licensing server + API bundle built with LICENSE_ENDPOINT_* pointing to it, clean RuntimeConfig, AUTHENTICATION_API_KEY that is not a license and EVOLUTION_OPERATOR_EMAIL set:

API licensing server result
develop current main inactive; only /v1/activate (401) is called, /v1/register/auto never
develop with self-hosted /register/auto inactive; /v1/register/auto never called
this branch current main /v1/register/auto called, 401 (route still admin-only there), falls back to the banner
this branch with self-hosted /register/auto active, License activated automatically via EVOLUTION_OPERATOR_EMAIL, business routes 200
  • tsc --noEmit, eslint (pre-commit) and npm run build + lint:check (pre-push)

✅ Checklist

  • I have performed a self-review of my code
  • My changes generate no new warnings

📝 Additional Notes

On the licensing server this path only succeeds once the self-hosted /v1/register/auto change is deployed there; until then the behaviour is the same as today (banner + browser flow).

Summary by Sourcery

Allow operator-email auto-activation to run when the configured global API key is not accepted as a license.

Bug Fixes:

  • Try EVOLUTION_OPERATOR_EMAIL automatic license activation when the global API key is rejected, before falling back to browser registration.

Chores:

  • Update the environment example file.

…is not a license

AUTHENTICATION_API_KEY always has a value (it falls back to a default), so
the boot never reached the branch that tries the operator e-mail. When the
global key is rejected by the licensing server, try the e-mail activation
before falling back to the browser registration flow.
@sourcery-ai

sourcery-ai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Implementation

Add a shared fallback that tries operator-email activation when no stored license applies, including after the global API key receives a 401 or 403; preserve the existing behavior for other errors.

  • src/licensing/runtime.ts L182-192, L199-215 (+3 small edits)
  • src/licensing/transport.ts L41-46

Configuration

Update the example environment configuration for the licensing change.

  • env.example L69-72

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/licensing/runtime.ts" line_range="182" />
<code_context>
-      printRegistrationBanner();
-      rc.setActive(false);
       logger.debug(`Global API key not accepted by licensing server: ${readErrorMessage(err)}`);
+      await autoRegisterOrWait(rc);
     }
   } else {
</code_context>
<issue_to_address>
**Instance activates under the wrong license**

When the configured global key is valid but activation or saving its license data fails, and auto-registration succeeds with a key for a different account, `initializeRuntime` treats errors from `activateInstance` or `saveRuntimeData` as a rejected key and calls `autoRegisterOrWait`, which can replace the valid global key with the email-issued key; the instance then runs under the wrong account’s license.

Only fall back to auto-registration when the licensing server explicitly rejects the configured key, and handle activation or persistence errors separately.
</issue_to_address>

### Comment 2
<location path="src/licensing/runtime.ts" line_range="182" />
<code_context>
-      printRegistrationBanner();
-      rc.setActive(false);
       logger.debug(`Global API key not accepted by licensing server: ${readErrorMessage(err)}`);
+      await autoRegisterOrWait(rc);
     }
   } else {
</code_context>
<issue_to_address>
**Licensing outage delays startup**

When the licensing server is unresponsive, a global API key is configured, and `EVOLUTION_OPERATOR_EMAIL` is set, `initializeRuntime` waits for the failed global-key activation request and then waits for auto-registration before returning; if both requests time out, startup can take about 20 seconds before `server.listen` runs, exceeding a deployment’s startup deadline and preventing the service from becoming ready.

Use a shared startup deadline or avoid making the second licensing request block server startup when the activation request has already timed out.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 2 findings to address first, and when a global API key is rejected, the runtime now tries to activate a license using the operator email and may save that license and enable the instance. Reverting stops future attempts but does not necessarily remove a license already activated; that state is bounded and should be repairable through the licensing flow.

Blocking findings: src/licensing/runtime.ts:182, src/licensing/runtime.ts:182


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/licensing/runtime.ts Resolved
Comment thread src/licensing/runtime.ts Resolved

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 4 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/licensing/runtime.ts" line_range="182" />
<code_context>
-      printRegistrationBanner();
-      rc.setActive(false);
       logger.debug(`Global API key not accepted by licensing server: ${readErrorMessage(err)}`);
+      await autoRegisterOrWait(rc);
     }
   } else {
</code_context>
<issue_to_address>
**Valid license key gets replaced**

When the configured global key is valid, activation or saving it fails for a non-rejection reason, and email auto-registration succeeds, `initializeRuntime` treats errors from `activateInstance` or `saveRuntimeData` as a rejected key and calls `autoRegisterOrWait`, which saves the email-issued key in place of the configured key and can leave the instance using a different license or customer.

Only run email auto-registration after confirming the configured key was rejected, and do not replace it after transient activation or persistence failures.
</issue_to_address>

### Comment 2
<location path="src/licensing/runtime.ts" line_range="182" />
<code_context>
-      printRegistrationBanner();
-      rc.setActive(false);
       logger.debug(`Global API key not accepted by licensing server: ${readErrorMessage(err)}`);
+      await autoRegisterOrWait(rc);
     }
   } else {
</code_context>
<issue_to_address>
**New license keeps old customer ID**

When a stale `customer_id` row exists and `/v1/register/auto` omits or returns a nonpositive customer ID, `tryAutoRegisterFromEnv` saves the new API key, but `saveRuntimeData` only updates `customer_id` when the response value is positive; the persisted license is therefore paired with the previous customer ID on the next load.

Clear the stored customer ID when saving a license whose customer ID is absent or nonpositive.
</issue_to_address>

### Comment 3
<location path="src/licensing/runtime.ts" line_range="182" />
<code_context>
-      printRegistrationBanner();
-      rc.setActive(false);
       logger.debug(`Global API key not accepted by licensing server: ${readErrorMessage(err)}`);
+      await autoRegisterOrWait(rc);
     }
   } else {
</code_context>
<issue_to_address>
**Startup waits on a second request**

When a global API key is configured and the licensing server times out or is unreachable, `initializeRuntime` awaits `autoRegisterOrWait` after the activation request times out, causing a second request to consume another timeout before `server.listen` runs and leaving the API unavailable during that delay.

Do not start the email auto-registration request after an activation timeout or an unreachable licensing server.
</issue_to_address>

### Comment 4
<location path="src/licensing/runtime.ts" line_range="207-208" />
<code_context>
  * tryAutoRegisterFromEnv attempts a silent license activation using only the
- * operator email from EVOLUTION_OPERATOR_EMAIL. The customer must have completed
- * at least one manual registration in the past.
+ * operator email from EVOLUTION_OPERATOR_EMAIL. Whether the email alone is enough
+ * to get a key is decided by the licensing server.
  *
  * Returns true on success. Returns false on any failure — the caller is expected
</code_context>
<issue_to_address>
**Setup guide implies prior registration**

When an operator configures `EVOLUTION_OPERATOR_EMAIL` using the checked-in `env.example` guidance, `env.example` tells operators to use an email from a first manual registration and says an unregistered email falls back to manual activation. That conflicts with the updated description that the server decides whether email alone is sufficient, so operators without a prior registration can wrongly conclude this automatic path is unavailable.

Update `env.example` to explain that the server determines whether the email can auto-activate, without requiring a prior manual registration.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 3 findings to address first, and when a configured global API key is rejected, the runtime now tries email-based activation and can save a license and enable the gated runtime if the licensing server accepts it. Reverting stops future attempts, but it does not necessarily revoke a license already issued or undo access already granted.

Blocking findings: src/licensing/runtime.ts:182, src/licensing/runtime.ts:182, src/licensing/runtime.ts:182


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/licensing/runtime.ts Resolved
Comment thread src/licensing/runtime.ts Resolved
Comment thread src/licensing/runtime.ts Resolved
Comment thread src/licensing/runtime.ts Resolved
…lobal key is refused

A licensing outage or a failed save while activating a valid global key
also reached the e-mail activation, which could replace the operator's key
with an e-mail issued one and doubled the startup wait on timeouts. Only a
401/403 from the licensing server now leads to the e-mail activation; other
errors keep the previous behaviour.
@daniloleonecarneiro
daniloleonecarneiro merged commit cad5129 into develop Dec 2, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant