diff --git a/.github/fork/daily-sync.md b/.github/fork/daily-sync.md new file mode 100644 index 0000000000000..31288c341c450 --- /dev/null +++ b/.github/fork/daily-sync.md @@ -0,0 +1,96 @@ +# Daily fork sync + +A T3 scheduled task follows these steps once a day. Its prompt names the sync +worktree: a detached worktree of the fork, with an `upstream` remote for +`pingdotgg/t3code`, used only for this sync. Work only there, and never touch +other checkouts or worktrees. + +Hard rules: + +- Never push to `upstream`, and never open or comment on anything in + `pingdotgg/t3code`. PRs go to the fork: `gh pr create --repo tebiggs/t3code --base main`. +- Never force-push, never merge a PR, and never push to `main` except through + `sync.sh`. +- Follow the host's own instructions for installs, typechecks and tests, and + run only the targeted ones you need. CI on the PR runs the rest. +- After opening a PR, register it with `link_pull_request`. + +## 1. Merge upstream + +From the sync worktree: + +```sh +git fetch --quiet origin main && git checkout --quiet --detach origin/main +.github/fork/sync.sh +``` + +Note the `previous=` and `latest=` lines it prints. + +- **Exit 0:** main is current. Go to step 2. +- **Exit 4:** a sync-conflict PR is still open. Report it and go to step 2. +- **Exit 3:** the merge conflicts. Resolve it: + - Take upstream's side unless the conflict touches a feature listed in + `FORK.md`. Then keep the fork behavior working on top of upstream's new code. + - Don't make unrelated changes. + - Commit as `chore(fork): merge upstream ` with the + `Co-Authored-By: Claude ` trailer. + - Push the commit to `sync-conflict/` and open a PR with the same title. + - Start the body with "Merge this with **Create a merge commit**, not squash, so + upstream history stays intact." Then list each conflicted file and how you + resolved it. +- **Any other exit:** report the output. Then run `git merge --abort` if a merge + is in progress, and `git checkout --quiet --detach origin/main`. + +## 2. Check the last release build + +```sh +gh run list --repo tebiggs/t3code --workflow fork-release.yml --branch main \ + --event push --status completed --limit 1 \ + --json databaseId,attempt,conclusion,headSha,url +``` + +If that run failed, read `gh run view --repo tebiggs/t3code --log-failed`. + +- If the failure looks like runner or network trouble, rerun the whole run with + `gh run rerun --repo tebiggs/t3code`, but only when `attempt` is 1 and + `headSha` is still `origin/main`. A newer build replaces an older one. + Don't use `--failed`: build artifacts expire after a day, so the retried jobs + couldn't download what the passing jobs built. If the token lacks permission + to rerun, report the failure instead. +- If the code broke, fix it on a `fix/fork-` branch from `origin/main` + and open a PR. Skip this if an open PR already fixes it. + +## 3. Look for upstream features that replace fork features + +Skip this step when `previous` equals `latest`. Otherwise list what upstream +merged in that range: + +```sh +git log --first-parent --format='%s' .. +``` + +Compare those titles with the features in `FORK.md`. Read an upstream PR's diff +when its title alone doesn't tell you. A title ending in `(#N)` links to +`https://github.com/pingdotgg/t3code/pull/N`. + +When upstream now covers what a fork feature does for the user, even if it +works differently, open a PR on `dedup/`. The PR should: + +- remove the fork version +- keep anything upstream doesn't cover +- update `FORK.md` +- link the upstream PRs + +Skip the PR if one is already open for that feature. If the overlap is only +partial, don't open a PR; mention it in the report instead. + +## 4. Report + +Write a few sentences: + +- what merged, or that main was already current +- every PR you opened, with its link +- any failed build and what you did about it +- any partial overlaps + +Leave out steps that found nothing. diff --git a/.github/fork/sync.sh b/.github/fork/sync.sh new file mode 100755 index 0000000000000..e7c4092e8db22 --- /dev/null +++ b/.github/fork/sync.sh @@ -0,0 +1,85 @@ +#!/usr/bin/env bash +# Merges upstream's newest nightly tag into the fork's main and pushes it. +# .github/fork/daily-sync.md drives it. Needs an `upstream` remote for +# pingdotgg/t3code. +# +# Exit codes: +# 0 main already has the newest tag, or the clean merge was pushed +# 3 the merge conflicts; it is left in progress for resolution +# 4 a sync-conflict PR is still open, so nothing was merged +# 1 anything else +# +# Prints `previous=` and `latest=` so the caller can review the +# upstream range. + +set -Eeuo pipefail +# The quiet git commands would otherwise fail without saying which one did. +trap 'echo "sync: failed: $BASH_COMMAND" >&2' ERR + +FORK_REPO=tebiggs/t3code +TAG_FILE=.github/fork/upstream-tag + +main() { + cd "$(git rev-parse --show-toplevel)" + if [[ -n "$(git status --porcelain)" ]]; then + echo "sync: the worktree has local changes; resolve or discard them first" >&2 + exit 1 + fi + + # A later merge on top of an unmerged resolution would conflict the same way. + local open_pr + open_pr="$(gh pr list --repo "$FORK_REPO" --state open --limit 1000 --json number,headRefName \ + --jq '[.[] | select(.headRefName | startswith("sync-conflict/")) | .number] | first // empty')" + if [[ -n "$open_pr" ]]; then + echo "sync: waiting on sync-conflict PR #$open_pr" + exit 4 + fi + + git fetch --quiet origin main + local latest current + latest="$(git ls-remote --tags --refs upstream 'refs/tags/v*-nightly.*' | + sed 's#.*refs/tags/##' | sort -V | tail -n 1)" + current="$(git show "origin/main:$TAG_FILE" | tr -d '[:space:]')" + if [[ -z "$latest" || -z "$current" ]]; then + echo "sync: could not read the upstream nightly tags or $TAG_FILE" >&2 + exit 1 + fi + echo "previous=$current" + echo "latest=$latest" + if [[ "$latest" == "$current" ]]; then + echo "sync: main already has $latest" + exit 0 + fi + + git fetch --quiet --no-tags upstream \ + "refs/tags/$latest:refs/tags/$latest" "refs/tags/$current:refs/tags/$current" + git checkout --quiet --detach origin/main + + # --no-ff even when main has no fork commits, so the tag file lands in a merge commit. + if ! git merge --quiet --no-ff --no-commit "$latest"; then + # Exit 3 promises a merge stopped on conflicts. Any other refusal, such as + # unrelated histories, leaves nothing to resolve. + if [[ -z "$(git ls-files --unmerged)" ]]; then + echo "sync: merging $latest failed without conflicts" >&2 + git merge --abort 2>/dev/null || true + exit 1 + fi + printf '%s\n' "$latest" > "$TAG_FILE" + git add "$TAG_FILE" + echo "sync: merging $latest conflicts in:" + git diff --name-only --diff-filter=U + exit 3 + fi + + printf '%s\n' "$latest" > "$TAG_FILE" + git add "$TAG_FILE" + git commit --quiet \ + -m "chore(fork): merge upstream $latest" \ + -m "Co-Authored-By: Claude " + # Not forced: if main moved since the fetch, this fails and the next run retries. + git push --quiet origin HEAD:refs/heads/main + echo "sync: pushed $(git rev-parse --short=8 HEAD) to main" +} + +# Wrapped in main so bash parses the whole file before the checkout above can replace it. +main "$@" diff --git a/.github/fork/upstream-tag b/.github/fork/upstream-tag new file mode 100644 index 0000000000000..a8a50c17a0bee --- /dev/null +++ b/.github/fork/upstream-tag @@ -0,0 +1 @@ +v0.1.1-nightly.20261020.3438 diff --git a/.github/workflows/fork-release.yml b/.github/workflows/fork-release.yml new file mode 100644 index 0000000000000..b384fb4f42827 --- /dev/null +++ b/.github/workflows/fork-release.yml @@ -0,0 +1,434 @@ +name: Fork release + +# Fork-only (tebiggs/t3code). Builds what the fork ships: the Windows x64 +# desktop app, the Linux x64 server archive and the Android APK. A push to +# main publishes them as a nightly-channel prerelease, which the fork's +# updaters read. Pull requests run the same checks and builds without +# publishing. Upstream merges reach main through .github/fork/sync.sh, run by +# the daily sync task. The workflow token cannot move main itself, because +# GitHub refuses that token any ref update that changes workflow files, and +# upstream merges often do. + +on: + push: + branches: + - main + pull_request: + +permissions: + contents: read + +concurrency: + group: fork-release-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + prepare: + name: Prepare + if: github.repository == 'tebiggs/t3code' + runs-on: ubuntu-24.04 + timeout-minutes: 5 + outputs: + version: ${{ steps.meta.outputs.version }} + ref: ${{ steps.meta.outputs.ref }} + upstream_tag: ${{ steps.meta.outputs.upstream_tag }} + clerk_publishable_key: ${{ steps.meta.outputs.clerk_publishable_key }} + clerk_jwt_template: ${{ steps.meta.outputs.clerk_jwt_template }} + clerk_cli_oauth_client_id: ${{ steps.meta.outputs.clerk_cli_oauth_client_id }} + relay_url: ${{ steps.meta.outputs.relay_url }} + steps: + - name: Checkout + uses: actions/checkout@v6 + with: + ref: ${{ github.event.pull_request.head.sha || github.sha }} + sparse-checkout: | + .github/fork + .env.example + sparse-checkout-cone-mode: false + + # Fork versions keep upstream's nightly shape (cliRelease.ts and + # updateChannels.ts parse it) and sort by build date, then by this + # workflow's run number. + - id: meta + name: Resolve version and public config + shell: bash + env: + REF: ${{ github.event.pull_request.head.sha || github.sha }} + run: | + set -euo pipefail + upstream_tag="$(tr -d '[:space:]' < .github/fork/upstream-tag)" + if [[ ! "$upstream_tag" =~ ^v([0-9]+\.[0-9]+\.[0-9]+)-nightly\.[0-9]{8}\.[0-9]+$ ]]; then + echo "::error::.github/fork/upstream-tag must hold an upstream nightly tag, got '$upstream_tag'." + exit 1 + fi + version="${BASH_REMATCH[1]}-nightly.$(date -u +%Y%m%d).${GITHUB_RUN_NUMBER}" + { + echo "version=$version" + echo "ref=$REF" + echo "upstream_tag=$upstream_tag" + } >> "$GITHUB_OUTPUT" + # .env.example carries the public T3 Connect identifiers that official builds use. + for pair in \ + clerk_publishable_key=T3CODE_CLERK_PUBLISHABLE_KEY \ + clerk_jwt_template=T3CODE_CLERK_JWT_TEMPLATE \ + clerk_cli_oauth_client_id=T3CODE_CLERK_CLI_OAUTH_CLIENT_ID \ + relay_url=T3CODE_RELAY_URL; do + value="$(sed -n "s/^${pair#*=}=//p" .env.example)" + if [[ -z "$value" ]]; then + echo "::error::.env.example has no ${pair#*=}." + exit 1 + fi + echo "${pair%%=*}=$value" >> "$GITHUB_OUTPUT" + done + echo "Building $version from $REF (upstream $upstream_tag)." + + checks: + name: Typecheck and fork tests + needs: prepare + runs-on: ubuntu-24.04 + timeout-minutes: 60 + steps: + - name: Checkout + uses: actions/checkout@v6 + with: + ref: ${{ needs.prepare.outputs.ref }} + sparse-checkout: | + /* + !/.repos/ + sparse-checkout-cone-mode: false + + - name: Setup dependencies + uses: ./.github/actions/setup-ci-deps + + - name: Ensure Electron runtime is installed + run: vp run --filter @t3tools/desktop ensure:electron + + - uses: ./.github/actions/setup-apt-mirrors + + - name: Install browser secret helper build libraries + run: sudo apt-get update && sudo apt-get install -y libsecret-1-dev pkg-config + + # Upstream's own CI already passed on the nightly being merged. What can + # break here is fork code against upstream's newest APIs. + - name: Typecheck + run: vp run typecheck + + # Runs the test files the fork changes and the tests beside source files + # it changes, through each package's own test script. + - name: Test what the fork changes + shell: bash + env: + UPSTREAM_TAG: ${{ needs.prepare.outputs.upstream_tag }} + run: | + set -euo pipefail + git fetch --quiet --no-tags --depth=1 https://github.com/pingdotgg/t3code.git "refs/tags/$UPSTREAM_TAG" + declare -A tests=() + while read -r file; do + [[ "$file" =~ ^((apps|packages)/[^/]+|scripts)/(.+)$ ]] || continue + dir="${BASH_REMATCH[1]}" + rel="${BASH_REMATCH[3]}" + if [[ "$rel" =~ \.test\.tsx?$ ]]; then + candidates=("$rel") + else + base="${rel%.*}" + candidates=("$base.test.ts" "$base.test.tsx" "$base.browser.test.ts" "$base.browser.test.tsx") + fi + for candidate in "${candidates[@]}"; do + if [[ -f "$dir/$candidate" ]]; then + tests[$dir]+=" $candidate" + fi + done + done < <(git diff --name-only FETCH_HEAD HEAD) + + if [[ ${#tests[@]} -eq 0 ]]; then + echo "The fork changes no tested files." + exit 0 + fi + for dir in "${!tests[@]}"; do + package="$(jq -r .name "$dir/package.json")" + files="$(tr ' ' '\n' <<< "${tests[$dir]}" | sort -u | xargs)" + echo "::group::$package: $files" + case "$package" in + @t3tools/web) node apps/web/node_modules/playwright/cli.js install --with-deps --only-shell chromium ;; + t3) node apps/server/node_modules/playwright-core/cli.js install --with-deps --only-shell chromium ;; + esac + # shellcheck disable=SC2086 # one argument per test file + vp run --filter "$package" test $files + echo "::endgroup::" + done + + bundle: + name: Build JS bundle + needs: prepare + runs-on: ubuntu-24.04 + timeout-minutes: 30 + env: + T3CODE_CLERK_PUBLISHABLE_KEY: ${{ needs.prepare.outputs.clerk_publishable_key }} + T3CODE_CLERK_JWT_TEMPLATE: ${{ needs.prepare.outputs.clerk_jwt_template }} + T3CODE_CLERK_CLI_OAUTH_CLIENT_ID: ${{ needs.prepare.outputs.clerk_cli_oauth_client_id }} + T3CODE_RELAY_URL: ${{ needs.prepare.outputs.relay_url }} + steps: + - name: Checkout + uses: actions/checkout@v6 + with: + ref: ${{ needs.prepare.outputs.ref }} + sparse-checkout: | + /* + !/.repos/ + sparse-checkout-cone-mode: false + + - name: Setup Vite+ + uses: voidzero-dev/setup-vp@v1 + with: + node-version-file: package.json + cache: true + run-install: false + + - name: Install bundle dependencies + run: vp install --filter=t3... --filter=@t3tools/web... --filter=@t3tools/desktop... --filter=@t3tools/scripts... + + - name: Align package versions to release version + run: node scripts/update-release-package-versions.ts "${{ needs.prepare.outputs.version }}" + + - uses: ./.github/actions/setup-apt-mirrors + + - name: Install browser secret helper build libraries + run: sudo apt-get update && sudo apt-get install -y libsecret-1-dev pkg-config + + - name: Build JS bundle + env: + T3CODE_TELEMETRY_CHANNEL: dev + run: vp run build:desktop + + - name: Upload JS bundle + uses: actions/upload-artifact@v7 + with: + name: js-bundle + path: | + apps/server/dist + apps/desktop/dist-electron + if-no-files-found: error + retention-days: 1 + + # Upstream's per-platform build, on standard runners and without signing + # secrets. The Linux job produces the server archive that fork servers install; + # the Windows job waits on it for the bundled WSL runtime. + linux: + name: Linux x64 + needs: [prepare, bundle] + permissions: + actions: read + contents: read + uses: ./.github/workflows/release-desktop.yml + with: + version: ${{ needs.prepare.outputs.version }} + ref: ${{ needs.prepare.outputs.ref }} + release_channel: nightly + relay_client_tracing: false + clerk_publishable_key: ${{ needs.prepare.outputs.clerk_publishable_key }} + clerk_jwt_template: ${{ needs.prepare.outputs.clerk_jwt_template }} + clerk_cli_oauth_client_id: ${{ needs.prepare.outputs.clerk_cli_oauth_client_id }} + relay_url: ${{ needs.prepare.outputs.relay_url }} + label: Linux x64 + runner: ubuntu-24.04 + platform: linux + target: AppImage + arch: x64 + rust_target: x86_64-unknown-linux-gnu + resource_key: linux-x64 + cli_archive: true + + windows: + name: Windows x64 + needs: [prepare, bundle] + permissions: + actions: read + contents: read + uses: ./.github/workflows/release-desktop.yml + with: + version: ${{ needs.prepare.outputs.version }} + ref: ${{ needs.prepare.outputs.ref }} + release_channel: nightly + relay_client_tracing: false + clerk_publishable_key: ${{ needs.prepare.outputs.clerk_publishable_key }} + clerk_jwt_template: ${{ needs.prepare.outputs.clerk_jwt_template }} + clerk_cli_oauth_client_id: ${{ needs.prepare.outputs.clerk_cli_oauth_client_id }} + relay_url: ${{ needs.prepare.outputs.relay_url }} + label: Windows x64 + # Built on Linux like upstream's arm64 target: on GitHub's Windows image, + # Git Bash's GNU tar shadows the system bsdtar and cannot unpack C:\ paths. + # The Windows runner only verifies the natives and smoke-tests the CLI. + runner: ubuntu-24.04 + verify_runner: windows-2025 + platform: win + target: nsis + arch: x64 + rust_target: x86_64-pc-windows-msvc + resource_key: win32-x64 + cli_archive: true + + android: + name: Android APK + needs: prepare + runs-on: ubuntu-24.04 + timeout-minutes: 90 + env: + APP_VARIANT: preview + EXPO_NO_GIT_STATUS: "1" + steps: + - name: Checkout + uses: actions/checkout@v6 + with: + ref: ${{ needs.prepare.outputs.ref }} + sparse-checkout: | + /* + !/.repos/ + sparse-checkout-cone-mode: false + + - name: Setup dependencies + uses: ./.github/actions/setup-ci-deps + + - name: Setup Java + uses: actions/setup-java@v5 + with: + distribution: temurin + java-version: "17" + + # The APK's JavaScript ships inside it; OTA updates would swap in + # upstream's preview bundles. + - name: Configure the build + run: | + cp .env.example .env + echo "T3CODE_MOBILE_UPDATES_ENABLED=0" >> .env + + - name: Generate the native project + working-directory: apps/mobile + run: ./node_modules/.bin/expo prebuild --platform android --clean --no-install + + # Release builds sign with the React Native template's debug keystore, + # the same key as the APK already installed from earlier fork builds. + - name: Build release APK + working-directory: apps/mobile/android + run: ./gradlew :app:assembleRelease -PreactNativeArchitectures=arm64-v8a -x lintVitalAnalyzeRelease --no-daemon + + - name: Collect APK + env: + VERSION: ${{ needs.prepare.outputs.version }} + run: | + mkdir -p release-android + cp apps/mobile/android/app/build/outputs/apk/release/app-release.apk \ + "release-android/t3code-android-${VERSION}-arm64-v8a.apk" + + - name: Upload APK + uses: actions/upload-artifact@v7 + with: + name: android-apk + path: release-android/*.apk + if-no-files-found: error + retention-days: 1 + + # Pull requests stop after assembling the assets, so a renamed build output + # fails there rather than on main. + publish: + name: Release assets + needs: [prepare, checks, linux, windows, android] + runs-on: ubuntu-24.04 + timeout-minutes: 20 + permissions: + contents: write + env: + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + VERSION: ${{ needs.prepare.outputs.version }} + REF: ${{ needs.prepare.outputs.ref }} + UPSTREAM_TAG: ${{ needs.prepare.outputs.upstream_tag }} + steps: + - name: Download Windows desktop + uses: actions/download-artifact@v8 + with: + name: desktop-win-x64 + path: release-assets + + - name: Download CLI archives + uses: actions/download-artifact@v8 + with: + pattern: cli-* + merge-multiple: true + path: release-assets + + - name: Download APK + uses: actions/download-artifact@v8 + with: + name: android-apk + path: release-assets + + # electron-updater reads nightly.yml; the build job suffixed it with the arch. + - name: Prepare release assets + shell: bash + run: | + set -euo pipefail + cd release-assets + rm -f builder-debug.yml + mv nightly-win-x64.yml nightly.yml + sha256sum t3-*.tar.gz t3-*.zip > SHA256SUMS + ls -l + + - name: Publish release + if: github.event_name == 'push' + shell: bash + run: | + set -euo pipefail + notes="Fork build of [upstream ${UPSTREAM_TAG}](https://github.com/pingdotgg/t3code/releases/tag/${UPSTREAM_TAG}) plus the features in [FORK.md](https://github.com/${GH_REPO}/blob/${REF}/FORK.md), from ${REF}." + gh release create "v${VERSION}" release-assets/* \ + --target "$REF" \ + --prerelease \ + --title "T3 Code v${VERSION} (fork)" \ + --notes "$notes" + + # Keeps the newest few so a client mid-update still finds its files. + - name: Prune old releases + if: github.event_name == 'push' + shell: bash + run: | + set -euo pipefail + gh release list --limit 100 --json tagName,isPrerelease,createdAt \ + --jq '[.[] | select(.isPrerelease and (.tagName | test("-nightly\\.")))] | sort_by(.createdAt) | reverse | .[5:] | .[].tagName' | + while read -r tag; do + echo "Deleting $tag" + gh release delete "$tag" --cleanup-tag --yes + done + + # Upstream's workflows wait forever for runners this fork doesn't have. This + # re-disables any that an upstream merge adds. release-desktop.yml stays + # enabled because this workflow calls it. + disable_upstream_workflows: + name: Disable upstream workflows + if: github.repository == 'tebiggs/t3code' && github.event_name == 'push' + runs-on: ubuntu-24.04 + timeout-minutes: 5 + permissions: + actions: write + env: + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + steps: + - name: Disable upstream workflows and cancel their runs + shell: bash + run: | + set -euo pipefail + gh api "repos/$GH_REPO/actions/workflows" --paginate \ + --jq '.workflows[] | select(.state == "active") | [.id, .path] | @tsv' | + while IFS=$'\t' read -r id path; do + case "$path" in + .github/workflows/fork-release.yml | .github/workflows/release-desktop.yml) continue ;; + .github/workflows/*) ;; + *) continue ;; + esac + echo "Disabling $path" + gh api -X PUT "repos/$GH_REPO/actions/workflows/$id/disable" --silent + gh run list --workflow "$id" --limit 100 --json databaseId,status \ + --jq '.[] | select(.status != "completed") | .databaseId' | + while read -r run; do + gh run cancel "$run" || true + done + done diff --git a/FORK.md b/FORK.md new file mode 100644 index 0000000000000..a0c98ae7ac3f8 --- /dev/null +++ b/FORK.md @@ -0,0 +1,25 @@ +# Fork notes + +This is a fork of T3 Code. `main` is upstream's newest nightly plus the +features listed below. + +- **Releases.** Every push to `main` builds the Windows x64 desktop app, the + Linux x64 server and the Android APK. The build runs in + `.github/workflows/fork-release.yml`. When typecheck and the tests + covering the fork's changes pass, it publishes everything as a prerelease here. + The fork's apps update from these releases. Pull requests get the same + checks and builds but publish nothing. +- **Upstream sync.** A T3 scheduled task follows `.github/fork/daily-sync.md` + once a day: + - It merges upstream's newest nightly tag into `main`. + - It opens a `sync-conflict/*` PR when the merge conflicts. + - It opens a `dedup/*` PR when upstream ships something that replaces a + fork feature. +- **Merge sync-conflict PRs with a merge commit, never squash.** Squashing + drops upstream's history, and every later sync would then conflict. + +## Fork features + +Give each feature one line: its PR, what it does, and its main paths. The daily +sync compares upstream changes against this list. Add the line in the PR that +adds the feature, and remove it in the PR that drops the feature.