LibreChat/.github/workflows/promote-main.yml
Marco Beretta c00c776e0e
📦 feat: Scaffold the @librechat/chat Package (#16964)
* feat: Scaffold the @librechat/chat package

Adds packages/chat with core, react and components entries, a tsdown build,
a node-environment jest config, turbo and root build wiring, import boundary
rules, design-lint scope, and core types for the chat transport, stream,
host and status. No code moves yet.

* test: Cover the @librechat/chat boundaries as scenarios

* fix: Run the chat suite in test:turbo and build it for the mock lane

test:turbo now selects @librechat/chat, the mock e2e job builds the package the
chat-package scenarios load, and the synthetic repository in the suppression
scenarios carries the packages/chat/src root lint:design:record names.

* chore: Build the chat package in frontend:ci too

* chore: Run the chat suite in test:all

* fix: Hold core TSX to the chat boundary and scan the package for cycles

* fix: Apply the chat import boundary to JavaScript sources

* fix: Keep nested core files off the components entry

* fix: Close the remaining chat package gates and fence aborts by generation

Blocks imports through the @librechat/frontend workspace name, adds the chat
sources to the i18n scan, the CSS color gate and the client Tailwind content,
and lets ChatAbortRequest carry the generation it means to stop.

* fix: Keep JSX out of the chat core and carry abort and reconnect outcomes

Rejects JSX under the core, copies the chat manifest before the Docker
install, keys the client app build caches on the chat sources, fences
reconnects by generation, and types the abort result the transport returns.

* test: Give the CSS gate's scratch tree the chat root it now scans

* fix: Close the remaining @librechat/chat gates and wiring

Rejects import() and require() across the package boundaries, holds chat
components to the literal-copy rule, compiles TSX in the package's jest
transform, builds the package in Frontend Unit Tests, requires its test job for
main promotion, adds it to the unused-dependency scan, and lists it wherever a
sibling workspace is listed: Dockerfile.multi, the docker smoke paths, the
remaining node_modules caches, the reinstall helper, the bun build chain and
AGENTS.md.

* fix: Tighten the chat boundaries and carry steering and Assistants aborts

Template-literal import() and require() and self-imports through the package
name are rejected, user-facing JSX attributes need localized text, the cycle
scan resolves self-imports, Dockerfile.multi builds the package for the client
stage, and the transport carries steer, cancelSteer, armSteer and the
Assistants abort key.

* feat: Match the chat transport and host contracts to the client's

The transport now reports settled and replaced starts, the stream's protocol
version and resumed flag, reconnect replay, generation status, paused-run
resume with approvals or answers, abort and steer protocol versions, and the
request error shape. The host config and auth carry what the client's
ChatSettings now supplies: rename-while-running, model specs, retention mode
and the user.

* fix: Keep lazy React-entry loads out of the core and type the Assistants stop

Relative import() and require() of the /react entry from core are rejected,
and an abortKey stop resolves to the terminal conversation and run messages the
Assistants route returns.

* fix: Fence start controls by protocol and close extension-bearing entry loads

Settled and replaced start results carry the protocol version, core loads of
the /react or /components entry with a file extension are rejected, and
test:packages:chat builds data-provider first.
2026-10-10 21:00:10 +02:00

275 lines
14 KiB
YAML

name: Promote Dev to Main
run-name: Promote dev ${{ inputs.dev_sha }} to main
on:
workflow_dispatch:
inputs:
dev_sha:
description: 'Approved current dev commit, full lowercase 40-character SHA'
type: string
required: true
expected_main_sha:
description: 'Current main commit reviewed as the promotion baseline, full SHA'
type: string
required: true
permissions: {}
concurrency:
group: main-promotion
cancel-in-progress: false
jobs:
promote:
if: >-
github.repository == 'LibreChat-AI/LibreChat' &&
github.event_name == 'workflow_dispatch' &&
github.ref == 'refs/heads/main'
runs-on: ubuntu-24.04
timeout-minutes: 10
environment: main-promotion
permissions:
contents: read
actions: read
env:
GH_HOST: github.com
GH_PROMPT_DISABLED: '1'
DEV_SHA: ${{ inputs.dev_sha }}
EXPECTED_MAIN_SHA: ${{ inputs.expected_main_sha }}
WORKFLOW_REF: ${{ github.workflow_ref }}
WORKFLOW_SHA: ${{ github.workflow_sha }}
steps:
# Inline policy comes from the workflow on main. No candidate code, checkout,
# artifacts, caches, dependency installs, or repository-local actions run here.
- name: Verify protected approval, commit ancestry, and full dev CI
env:
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
cat > "$RUNNER_TEMP/main-promotion.py" <<'PY'
import base64
import json
import os
import re
import subprocess
import sys
from pathlib import Path
REPO = 'LibreChat-AI/LibreChat'
REMOTE = 'https://github.com/LibreChat-AI/LibreChat.git'
WORKFLOW = REPO + '/.github/workflows/promote-main.yml@refs/heads/main'
SHA = re.compile(r'[0-9a-f]{40}')
LOGIN = re.compile(r'[A-Za-z0-9][A-Za-z0-9-]{0,38}')
CI = {
'backend-review.yml': (59857785, {
'Build packages', 'Circular dependency checks', 'TypeScript type checks',
'Tests: data-provider', 'Tests: data-schemas',
*[f'Tests: api (shard {n}/3)' for n in range(1, 4)],
*[f'Tests: @librechat/api (shard {n}/4)' for n in range(1, 5)],
}),
'frontend-review.yml': (59789527, {
'Build packages', 'Client build recovery regression',
'TypeScript type checks (client)', 'Vite build verification',
'Tests: @librechat/client', 'Tests: @librechat/chat',
*[f'Tests: Ubuntu (shard {n}/2)' for n in range(1, 3)],
}),
}
class Refused(Exception):
pass
def require(condition, message):
if not condition:
raise Refused(message)
def api(path):
result = subprocess.run(
['gh', 'api', '--hostname', 'github.com', '--method', 'GET',
'-H', 'Accept: application/vnd.github+json',
'-H', 'X-GitHub-Api-Version: 2022-11-28', f'repos/{REPO}/{path}'],
capture_output=True, text=True, check=False, timeout=30,
)
require(result.returncode == 0, 'GitHub policy read failed; refusing promotion')
try:
return json.loads(result.stdout)
except ValueError:
raise Refused('Invalid GitHub response; refusing promotion') from None
def complete_list(value, key):
items = value.get(key)
require(isinstance(items, list) and value.get('total_count') == len(items)
and len(items) < 100, 'Incomplete or oversized policy response')
return items
def ref(branch):
value = api(f'git/ref/heads/{branch}')
obj = value.get('object', {})
require(value.get('ref') == f'refs/heads/{branch}' and obj.get('type') == 'commit'
and SHA.fullmatch(obj.get('sha', '')), 'Invalid branch response')
return obj['sha']
def context():
dev = os.environ.get('DEV_SHA', '')
main = os.environ.get('EXPECTED_MAIN_SHA', '')
require(SHA.fullmatch(dev) and SHA.fullmatch(main), 'Supply two full lowercase commit SHAs')
require(os.environ.get('GITHUB_REPOSITORY') == REPO
and os.environ.get('GITHUB_EVENT_NAME') == 'workflow_dispatch'
and os.environ.get('GITHUB_REF') == 'refs/heads/main'
and os.environ.get('GITHUB_SERVER_URL') == 'https://github.com'
and os.environ.get('GITHUB_API_URL') == 'https://api.github.com'
and os.environ.get('WORKFLOW_REF') == WORKFLOW
and os.environ.get('WORKFLOW_SHA') == main
and os.environ.get('GITHUB_SHA') == main,
'Dispatch the trusted workflow from the current main branch')
actors = {os.environ.get('GITHUB_ACTOR', ''),
os.environ.get('GITHUB_TRIGGERING_ACTOR', '')}
for actor in actors:
require(LOGIN.fullmatch(actor), 'Invalid workflow actor')
permission = api(f'collaborators/{actor}/permission')
require(permission.get('role_name') in ('admin', 'maintain')
and permission.get('permission') in ('admin', 'write'),
'Both the initiator and rerun actor must be maintainers')
return dev, main
def approval():
value = api('environments/main-promotion')
require(value.get('can_admins_bypass') is False, 'Disable administrator approval bypass')
require(value.get('deployment_branch_policy') == {
'protected_branches': False, 'custom_branch_policies': True,
}, 'Use a main-only environment branch policy')
rules = value.get('protection_rules', [])
require(any(rule.get('type') == 'required_reviewers'
and rule.get('prevent_self_review') is True
and bool(rule.get('reviewers')) for rule in rules),
'Configure required reviewers and prevent self-review')
policies = complete_list(
api('environments/main-promotion/deployment-branch-policies?per_page=100'),
'branch_policies',
)
require(len(policies) == 1 and policies[0].get('name') == 'main'
and policies[0].get('type') == 'branch',
'Only the main branch may access promotion credentials')
def full_ci(dev):
for filename, (workflow_id, required) in CI.items():
workflow = api(f'actions/workflows/{filename}')
require(workflow.get('id') == workflow_id
and workflow.get('path') == f'.github/workflows/{filename}'
and workflow.get('state') == 'active', 'Unexpected CI workflow identity')
runs = complete_list(api(
f'actions/workflows/{workflow_id}/runs?head_sha={dev}&per_page=100'
), 'workflow_runs')
eligible = [run for run in runs
if run.get('event') in ('push', 'workflow_dispatch')
and run.get('head_branch') == 'dev'
and run.get('head_sha') == dev
and run.get('repository', {}).get('full_name') == REPO]
require(bool(eligible), f'Missing full dev CI: {filename}')
run = max(eligible, key=lambda item: item['id'])
require(run.get('workflow_id') == workflow_id
and run.get('path') == f'.github/workflows/{filename}'
and run.get('status') == 'completed' and run.get('conclusion') == 'success',
f'Latest full dev CI is not successful: {filename}')
run_id, attempt = run['id'], run['run_attempt']
require(type(run_id) is int and type(attempt) is int and run_id > 0 and attempt > 0,
'Invalid CI run identity')
jobs = complete_list(api(
f'actions/runs/{run_id}/attempts/{attempt}/jobs?per_page=100'
), 'jobs')
names = [job.get('name') for job in jobs]
require(len(names) == len(set(names)) and required.issubset(names),
f'Missing or duplicate full-suite jobs: {filename}')
require(all(job.get('status') == 'completed' and job.get('head_sha') == dev
and (job.get('conclusion') == 'success'
or (job.get('name') == 'Codegraph select'
and job.get('conclusion') == 'skipped')) for job in jobs),
f'Failed, skipped, or incomplete full-suite job: {filename}')
latest = api(f'actions/runs/{run_id}')
require(all(latest.get(key) == run.get(key) for key in (
'id', 'run_attempt', 'head_sha', 'head_branch', 'event',
'workflow_id', 'path', 'status', 'conclusion',
)), 'CI changed during verification; wait and dispatch again')
def git(args, token):
# No token in arguments, remote config, files, or output. Ignore global
# git config and hooks; only a disposable bare object database is used.
header = base64.b64encode(f'x-access-token:{token}'.encode()).decode()
print(f'::add-mask::{header}', flush=True)
env = dict(os.environ, GIT_CONFIG_NOSYSTEM='1', GIT_CONFIG_GLOBAL='/dev/null',
GIT_TERMINAL_PROMPT='0', GIT_CONFIG_COUNT='1',
GIT_CONFIG_KEY_0='http.https://github.com/.extraheader',
GIT_CONFIG_VALUE_0=f'AUTHORIZATION: basic {header}')
result = subprocess.run(
['git', '-c', 'core.hooksPath=/dev/null', *args],
env=env, capture_output=True, text=True, check=False, timeout=90,
)
require(result.returncode == 0, 'Git operation refused; do not force or bypass protection')
return result.stdout.strip()
def verify():
dev, main = context()
approval()
require(ref('dev') == dev and ref('main') == main, 'Branch tips changed; dispatch again')
full_ci(dev)
directory = Path(os.environ['RUNNER_TEMP']) / 'main-promotion.git'
token = os.environ['GH_TOKEN']
git(['init', '--bare', str(directory)], token)
prefix = ['--git-dir', str(directory)]
git([*prefix, 'fetch', '--no-tags', REMOTE,
'+refs/heads/main:refs/heads/main', '+refs/heads/dev:refs/heads/dev'], token)
require(git([*prefix, 'rev-parse', 'refs/heads/main'], token) == main
and git([*prefix, 'rev-parse', 'refs/heads/dev'], token) == dev,
'Branch tips changed during fetch; dispatch again')
git([*prefix, 'merge-base', '--is-ancestor', main, dev], token)
# The App deliberately has no Workflows permission. Updating the
# release control plane requires a separately reviewed manual FF.
changed = git([*prefix, 'diff', '--name-only', main, dev, '--',
'.github/workflows', '.github/scripts', '.github/CODEOWNERS'], token)
require(not changed, 'Workflow/control-plane changes require a maintainer-reviewed manual fast-forward')
require(ref('dev') == dev and ref('main') == main, 'Branch tips changed after CI verification')
return dev, main, prefix
def run():
dev, main, prefix = verify()
if sys.argv[1] == 'promote':
require(bool(os.environ.get('PROMOTION_TOKEN')), 'No promotion credential')
# No force flag. A concurrent non-ancestor main update is rejected
# by Git receive-pack; dev can never change the pinned target SHA.
if dev != main:
git([*prefix, 'push', '--porcelain', REMOTE, f'{dev}:refs/heads/main'],
os.environ['PROMOTION_TOKEN'])
require(ref('main') == dev, 'Post-push verification failed; inspect main before retrying')
outcome = 'Promoted' if dev != main else 'Already current'
else:
require(sys.argv[1] == 'verify', 'Invalid operation')
outcome = 'Verified; main has not been changed'
with open(os.environ['GITHUB_STEP_SUMMARY'], 'a') as summary:
summary.write(f'### {outcome}\n\nMain baseline: `{main}`\n\nApproved dev: `{dev}`\n')
if __name__ == '__main__':
try:
run()
except Refused as error:
print(f'::error::Promotion refused: {error}', file=sys.stderr)
sys.exit(1)
except (KeyError, TypeError, ValueError, subprocess.TimeoutExpired):
print('::error::Invalid or unavailable policy data; refusing promotion.', file=sys.stderr)
sys.exit(1)
PY
python3 -I "$RUNNER_TEMP/main-promotion.py" verify
- name: Mint a repository-scoped short-lived promotion token
id: app
uses: actions/create-github-app-token@fee1f7d63c2ff003460e3d139729b119787bc349 # v2
with:
app-id: ${{ vars.MAIN_PROMOTION_APP_ID }}
private-key: ${{ secrets.MAIN_PROMOTION_APP_PRIVATE_KEY }}
owner: LibreChat-AI
repositories: LibreChat
permission-contents: write
- name: Revalidate and fast-forward only main
env:
GH_TOKEN: ${{ github.token }}
PROMOTION_TOKEN: ${{ steps.app.outputs.token }}
run: python3 -I "$RUNNER_TEMP/main-promotion.py" promote