Skip to content

fix(engine): wire engineToken and normalize apiUrl in headless store context (#544) - #668

Merged
Mihir-Rabari merged 1 commit into
Mihir-Rabari:devfrom
atharvashukla098-tech:feat/issue-544-headless-store-context
Oct 19, 2026
Merged

Mihir-Rabari merged 1 commit into
Mihir-Rabari:devfrom
atharvashukla098-tech:feat/issue-544-headless-store-context

Conversation

@atharvashukla098-tech

@atharvashukla098-tech atharvashukla098-tech commented Oct 19, 2026 •

Copy link
Copy Markdown
Collaborator

User description

Resolves #544

Summary of Changes

  • Headless Store Context Authentication: Wired \engineToken: params.engineToken\ into \createContextStore(...)\ inside \pieceHelper.executeTool\ (\packages/server/engine/src/lib/helper/piece-helper.ts), ensuring store-touching pieces executed via MCP \�p_run_action\ authenticate properly against /v1/store-entries.
  • URL Normalization:
    • Normalized \�piUrl\ in \�uildUrl\ (\packages/server/engine/src/lib/piece-context/store.ts) with a trailing slash so that internal API URLs without a trailing slash (e.g. \http://localhost:3100/api\) do not produce malformed URL endpoints or throw \TypeError: Invalid URL.
    • Normalized \�piUrl\ in \engineFileApi.upload\ and \download\ (\packages/server/engine/src/lib/api/engine-file-api.ts) for consistent URL construction across headless file uploads.
  • Unit Tests:
    • Added unit test asserting \store.get(...)\ carries \Authorization: Bearer \ and constructs valid store-entry URLs from slash-less \internalApiUrl.
    • Added unit test asserting \ iles.write(...)\ correctly normalizes slash-less \internalApiUrl.
    • All 209 engine unit tests pass.

CodeAnt-AI Description

Fix headless action access to the store and files

What Changed

  • Headless actions now authenticate store requests with the engine token, preventing unauthorized failures.
  • Store and file requests now reach the correct endpoints when the API URL has no trailing slash.

Impact

✅ Fewer unauthorized store requests
✅ Successful file transfers with slash-less API URLs
✅ Fewer malformed API requests

💡 Usage Guide

Checking Your Pull Request

Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.

Talking to CodeAnt AI

Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:

@codeant-ai ask: Your question here

This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.

Example

@codeant-ai ask: Can you suggest a safer alternative to storing this secret?

Preserve Org Learnings with CodeAnt

You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:

@codeant-ai: Your feedback here

This helps CodeAnt AI learn and adapt to your team's coding style and standards.

Example

@codeant-ai: Do not flag unused imports.

Retrigger review

Ask CodeAnt AI to review the PR again, by typing:

@codeant-ai: review

Check Your Repository Health

To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.

…context (Mihir-Rabari#544)

In headless action execution (used by MCP ap_run_action),
createContextStore dropped engineToken, leading to 401s when accessing
store entries. Additionally, normalize apiUrl in buildUrl and
engineFileApi so endpoints without a trailing slash do not produce
malformed URLs or crash with TypeError: Invalid URL.
@codeant-ai

codeant-ai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

🤖 CodeAnt AI — Review Status

Status Commit Started (UTC) Finished (UTC)
✅ Reviewed your PR e542b66 Oct 19, 2026 · 10:43 10:46

@codeant-ai

codeant-ai Bot commented Oct 19, 2026

Copy link
Copy Markdown

Thanks for using CodeAnt! 🎉

We're free for open-source projects. if you're enjoying it, help us grow by sharing.

Share on X ·
Reddit ·
LinkedIn

@codeant-ai codeant-ai Bot added the size:M This PR changes 30-99 lines, ignoring generated files label Oct 19, 2026
@codeant-ai

codeant-ai Bot commented Oct 19, 2026

Copy link
Copy Markdown

CodeAnt PR Risk: Low Risk

  • The PR appears safe to merge; it normalizes API URLs and wires the engine token into the headless store context.
  • Tests cover store authorization and file uploads when the internal API URL lacks a trailing slash.

Assessed commit: e542b66cdd94

@Mihir-Rabari Mihir-Rabari left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Review TL;DR — ✅ APPROVE

Reviewed head: e542b66

First review of this PR — no prior feedback to carry over.

✅ Strengths

  • Wires engineToken: params.engineToken into the headless createContextStore call (piece-helper.ts:237) — exactly the #544 root cause: the store client builds Authorization: Bearer ${engineToken} (store.ts:45/79/113) and the headless path never passed one. The old flowVersionId: 'headless' arg is gone, matching the current { apiUrl, prefix, flowId, engineToken } signature (store.ts:7) — verified live at this head.
  • Trailing-slash normalization in buildUrl (store.ts:151-152) and both engineFileApi upload/download URLs (engine-file-api.ts:21-22, 76-77) — kills the TypeError: Invalid URL class from slash-less internalApiUrl values without disturbing callers that already pass a trailing slash.
  • Audits the sibling factory like the issue asked: the files path gets the same normalization, so store + files both survive http://localhost:3100/api-style config.

Tests (mandate check)

  • Two real-behavior regression tests (piece-helper.test.ts:77-135): one asserts store.get carries Authorization: Bearer secret-engine-token AND constructs http://localhost:3100/api/v1/store-entries?key=flow_headless%2Fdemo-key from a slash-less internalApiUrl; the other asserts files.write builds a normalized /api/v1/files/... URL from a slash-less base. Both drive the actual fetch mock through pieceHelper.executeTool — exactly the "explicit internalApiUrl assertion" regression test #544 requested. Green in Checks (unit); full matrix green at this head.

⚠️ Should fix (non-blocking follow-up)

  • buildUrl still throws TypeError if internalApiUrl arrives genuinely undefined from the MCP dispatch path (the .endsWith read throws before new URL). A small guard with a descriptive error message would make that misconfiguration debuggable — worth a follow-up if the dispatch side ever regresses.

💡 Nits

  • The endsWith('/') ternary now appears 3× across store.ts/engine-file-api.ts — a tiny ensureTrailingSlash() util would DRY it.

Prior feedback status: N/A — first review; no prior Mihir/codeant threads.

Acceptance criteria (#544)

  • engineToken wired into headless store context → ✅
  • No more malformed URLs for slash-less internalApiUrl (store + files) → ✅
  • Sibling factories audited (files) → ✅
  • Regression test with explicit internalApiUrl assertion → ✅

Clean fix, well-scoped, tested. Merging — thanks! 🎉

— Hermes Agent, reviewing on behalf of @Mihir-Rabari

@Mihir-Rabari
Mihir-Rabari merged commit 6c11420 into Mihir-Rabari:dev Oct 19, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants