Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bd8657af44 | ||
|
|
f34cfbb7c4 | ||
|
|
0c66565854 | ||
|
|
e775bc6228 | ||
|
|
03ac1f0cd9 | ||
|
|
7778a1cea7 | ||
|
|
dad0889766 | ||
|
|
b9cc8d6ef1 | ||
|
|
0e0040832c | ||
|
|
cd8b9fc8b3 | ||
|
|
fa6073e2e1 | ||
|
|
4eb1b57933 | ||
|
|
4414f922fe | ||
|
|
6e42231cb1 | ||
|
|
bdf5aed665 | ||
|
|
0b62e2eb11 | ||
|
|
f9727d0853 | ||
|
|
d8e0ba3ea0 |
No files matched your search
@@ -0,0 +1,72 @@
|
||||
---
|
||||
description: Cursor Cloud environment setup and run notes for T3 Code (Cursor-specific; not for other editors/CI)
|
||||
alwaysApply: true
|
||||
---
|
||||
|
||||
# Cursor Cloud specific instructions
|
||||
|
||||
These notes apply only to Cursor Cloud agents (kept out of the shared `AGENTS.md` since T3 Code is
|
||||
developed in many places). They cover the pre-provisioned VM environment and non-obvious run caveats.
|
||||
|
||||
## Toolchain
|
||||
|
||||
Pre-installed and wired into login shells (`~/.bashrc`/`~/.profile`): Node 24 (via nvm), the global
|
||||
`vp` (Vite+) CLI, and its bundled pnpm. `vp` is the package manager + task runner for this repo
|
||||
(`vp i`, `vp check`, `vp run typecheck`, `vp test`, `vp run --filter <pkg> <script>`).
|
||||
|
||||
- Node gotcha: the base image also ships an older Node on `/exec-daemon` that wins on `PATH` in
|
||||
non-login shells. Run commands in a login shell (`bash -lc '...'`) or prepend
|
||||
`$HOME/.nvm/versions/node/v24.13.1/bin` so Node 24 (`engines.node: ^24.13.1`) is used.
|
||||
- `vp check` also runs the formatter (not just lint); markdown edits must be formatter-clean. Run
|
||||
`vp check --fix` before committing.
|
||||
|
||||
## Running the web app
|
||||
|
||||
- `npm run dev` (from repo root) starts contracts (watch), web (Vite, port 5733), and the server
|
||||
(`node --watch`, port 13773) together, auto-wiring `VITE_HTTP_URL`/`VITE_WS_URL`. Use
|
||||
`npm run dev:server` / `npm run dev:web` for one side. If base ports are taken it auto-offsets to
|
||||
the next free pair, so read the actual ports from stdout. Avoid production `build`/`start` in dev.
|
||||
- Auth/pairing: the server is unauthenticated by default and prints a pairing URL to stdout on startup
|
||||
(e.g. `http://localhost:5733/pair#token=XXXX`). Open that URL in the browser to pair before the web
|
||||
UI can talk to the server. Server state (SQLite, auth, projects) lives under `~/.t3`
|
||||
(`T3CODE_HOME`); multiple `npm run dev` instances bind different ports but share that same DB.
|
||||
- Agent providers are external CLIs (`codex`, `claude`, `cursor-agent`, `opencode`) probed on `PATH`.
|
||||
Without one installed the UI loads but no agent can run. Claude Code
|
||||
(`npm i -g @anthropic-ai/claude-code`) works headlessly using `ANTHROPIC_API_KEY` from the
|
||||
environment (the server forwards `process.env` to the child; no interactive `claude auth login`
|
||||
needed). Provider probing runs at startup and refreshes roughly every 5 minutes — restart the dev
|
||||
server to pick up a newly installed provider CLI immediately.
|
||||
|
||||
## Android native builds (apps/mobile)
|
||||
|
||||
The Android toolchain is pre-installed and wired into login shells: OpenJDK 17
|
||||
(`JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64`; the base image's default `java` is 21, so builds must
|
||||
use `JAVA_HOME`), and the Android SDK at `$HOME/Android/Sdk` (`ANDROID_HOME`/`ANDROID_SDK_ROOT`) with
|
||||
platform-tools, `platforms;android-36`, `build-tools;36.0.0`, `ndk;27.1.12297006`, and `cmake;3.22.1`.
|
||||
Gradle auto-downloads any additional pinned SDK packages on demand.
|
||||
|
||||
- Generate the native project first: from `apps/mobile`,
|
||||
`EXPO_NO_GIT_STATUS=1 APP_VARIANT=development npx expo prebuild --clean --platform android`
|
||||
(the `android/` folder is generated, not committed). RN 0.85 defaults: compileSdk/targetSdk 36,
|
||||
minSdk 24, NDK `27.1.12297006` (see `react-native/gradle/libs.versions.toml`).
|
||||
- Build with Gradle from `apps/mobile/android`, e.g. `./gradlew :app:assembleDebug`. First build is
|
||||
slow (Kotlin + C++/NDK codegen). Limit ABIs to speed things up, e.g.
|
||||
`-PreactNativeArchitectures=arm64-v8a`. Verified working end-to-end on a native module:
|
||||
`./gradlew :react-native-worklets:assembleDebug` compiles Kotlin + C++ and emits an AAR + `.so`s.
|
||||
- No emulator: this VM has no `/dev/kvm` (nested virtualization), so a hardware-accelerated Android
|
||||
emulator will not run. Use a physical device (`adb`) or EAS cloud builds (`vp run eas:android:*`) to
|
||||
actually run the app. The full IDE GUI is unnecessary here — the SDK/NDK/Gradle toolchain that
|
||||
Android Studio bundles is what is installed.
|
||||
- Known blocker for the full-app `:app:assembleDebug` (pre-existing, app-level — not the environment):
|
||||
`:react-native-screens:compileDebugKotlin` fails because `patches/[email protected]`
|
||||
adds new codegen props (`subtitle`, `largeSubtitle`, `navigationItemStyle`,
|
||||
`headerCenterBarButtonItems`, `headerToolbarItems`) to the JS spec + iOS native code but includes no
|
||||
Android Kotlin setters, so the generated `ScreenStackHeaderConfigManagerInterface` is unimplemented.
|
||||
The mobile README documents only iOS for local builds and routes Android through EAS. Fixing local
|
||||
Android app builds requires an app/patch change (add the Android Kotlin setters), which is out of
|
||||
scope for environment setup.
|
||||
|
||||
## Known flaky tests (unrelated to setup)
|
||||
|
||||
- `apps/server/src/git/GitManager.test.ts` (cross-repo PR metadata, can time out at 12s).
|
||||
- `apps/server/src/provider/Layers/ProviderRegistry.test.ts` (codex binaryPath re-probe ordering).
|
||||
@@ -57,7 +57,10 @@ jobs:
|
||||
with:
|
||||
eas-version: latest
|
||||
token: ${{ secrets.EXPO_TOKEN }}
|
||||
packager: pnpm
|
||||
# npm, not pnpm: this only installs eas-cli into the action's own
|
||||
# tool dir, and pnpm 11 hard-fails that install on dtrace-provider's
|
||||
# ignored build script (no allowBuilds config outside the repo).
|
||||
packager: npm
|
||||
|
||||
- name: Pull preview environment variables
|
||||
if: steps.expo-token.outputs.present == 'true'
|
||||
|
||||
@@ -80,7 +80,10 @@ jobs:
|
||||
with:
|
||||
eas-version: latest
|
||||
token: ${{ secrets.EXPO_TOKEN }}
|
||||
packager: pnpm
|
||||
# npm, not pnpm: this only installs eas-cli into the action's own
|
||||
# tool dir, and pnpm 11 hard-fails that install on dtrace-provider's
|
||||
# ignored build script (no allowBuilds config outside the repo).
|
||||
packager: npm
|
||||
|
||||
- name: Pull production environment variables
|
||||
if: steps.expo-token.outputs.present == 'true'
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
"@effect/vitest": "catalog:",
|
||||
"@types/node": "catalog:",
|
||||
"cross-env": "^10.1.0",
|
||||
"electron-builder": "26.8.1",
|
||||
"electron-builder": "26.15.6",
|
||||
"tailwindcss": "^4.0.0",
|
||||
"vite-plus": "catalog:"
|
||||
},
|
||||
|
||||
@@ -15,6 +15,7 @@ const VARIANT_CONFIG: Record<
|
||||
readonly appName: string;
|
||||
readonly scheme: string;
|
||||
readonly iosIcon: string;
|
||||
readonly splashIcon: string;
|
||||
readonly iosBundleIdentifier: string;
|
||||
readonly androidPackage: string;
|
||||
readonly relyingParty?: string;
|
||||
@@ -24,6 +25,7 @@ const VARIANT_CONFIG: Record<
|
||||
appName: "T3 Code Dev",
|
||||
scheme: "t3code-dev",
|
||||
iosIcon: "./assets/icon-composer-dev.icon",
|
||||
splashIcon: "./assets/splash-icon-dev.png",
|
||||
iosBundleIdentifier: "com.t3tools.t3code.dev",
|
||||
androidPackage: "com.t3tools.t3code.dev",
|
||||
relyingParty: "clerk.t3.codes",
|
||||
@@ -32,6 +34,7 @@ const VARIANT_CONFIG: Record<
|
||||
appName: "T3 Code Preview",
|
||||
scheme: "t3code-preview",
|
||||
iosIcon: "./assets/icon-composer-prod.icon",
|
||||
splashIcon: "./assets/splash-icon-prod.png",
|
||||
iosBundleIdentifier: "com.t3tools.t3code.preview",
|
||||
androidPackage: "com.t3tools.t3code.preview",
|
||||
relyingParty: "clerk.t3.codes",
|
||||
@@ -40,6 +43,7 @@ const VARIANT_CONFIG: Record<
|
||||
appName: "T3 Code",
|
||||
scheme: "t3code",
|
||||
iosIcon: "./assets/icon-composer-prod.icon",
|
||||
splashIcon: "./assets/splash-icon-prod.png",
|
||||
iosBundleIdentifier: "com.t3tools.t3code",
|
||||
androidPackage: "com.t3tools.t3code",
|
||||
relyingParty: "clerk.t3.codes",
|
||||
@@ -131,12 +135,12 @@ const config: ExpoConfig = {
|
||||
[
|
||||
"expo-splash-screen",
|
||||
{
|
||||
image: "./assets/splash-icon.png",
|
||||
image: variant.splashIcon,
|
||||
resizeMode: "contain",
|
||||
backgroundColor: "#ffffff",
|
||||
imageWidth: 220,
|
||||
dark: {
|
||||
image: "./assets/splash-icon.png",
|
||||
image: variant.splashIcon,
|
||||
backgroundColor: "#0a0a0a",
|
||||
},
|
||||
},
|
||||
@@ -155,12 +159,21 @@ const config: ExpoConfig = {
|
||||
},
|
||||
],
|
||||
"./plugins/withIosCocoaPodsUuidCache.cjs",
|
||||
// Must be listed BEFORE expo-widgets: same-type mods run last-registered-
|
||||
// first, so registering earlier makes this plugin's mods run AFTER
|
||||
// expo-widgets' — its dangerous mod wipes ios/ExpoWidgetsTarget/ (which
|
||||
// would delete the asset catalog) and its xcodeproj mod creates the widget
|
||||
// target (which must exist before the compile phase can be attached).
|
||||
"./plugins/withWidgetLogoAsset.cjs",
|
||||
[
|
||||
"expo-widgets",
|
||||
{
|
||||
bundleIdentifier: `${variant.iosBundleIdentifier}.widgets`,
|
||||
groupIdentifier: `group.${variant.iosBundleIdentifier}`,
|
||||
enablePushNotifications: true,
|
||||
// Agent activity can update many times an hour; without the
|
||||
// frequent-updates entitlement iOS throttles the update budget sooner.
|
||||
frequentUpdates: true,
|
||||
widgets: [
|
||||
{
|
||||
name: "AgentActivity",
|
||||
@@ -183,6 +196,15 @@ const config: ExpoConfig = {
|
||||
publishableKey: repoEnv.EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY ?? null,
|
||||
jwtTemplate: repoEnv.EXPO_PUBLIC_CLERK_JWT_TEMPLATE ?? null,
|
||||
},
|
||||
// Native Google sign-in credentials. @clerk/expo reads these from `extra`
|
||||
// under their exact env-var names (not nested), and its config plugin reads
|
||||
// the iOS URL scheme at prebuild to register it in Info.plist.
|
||||
// Unset values must be omitted (not null): the public manifest serializes
|
||||
// null to {}, which is truthy and would defeat Clerk's fallback checks.
|
||||
EXPO_PUBLIC_CLERK_GOOGLE_WEB_CLIENT_ID: repoEnv.EXPO_PUBLIC_CLERK_GOOGLE_WEB_CLIENT_ID,
|
||||
EXPO_PUBLIC_CLERK_GOOGLE_IOS_CLIENT_ID: repoEnv.EXPO_PUBLIC_CLERK_GOOGLE_IOS_CLIENT_ID,
|
||||
EXPO_PUBLIC_CLERK_GOOGLE_ANDROID_CLIENT_ID: repoEnv.EXPO_PUBLIC_CLERK_GOOGLE_ANDROID_CLIENT_ID,
|
||||
EXPO_PUBLIC_CLERK_GOOGLE_IOS_URL_SCHEME: repoEnv.EXPO_PUBLIC_CLERK_GOOGLE_IOS_URL_SCHEME,
|
||||
observability: {
|
||||
tracesUrl: repoEnv.EXPO_PUBLIC_OTLP_TRACES_URL ?? "https://api.axiom.co/v1/traces",
|
||||
tracesDataset: repoEnv.EXPO_PUBLIC_OTLP_TRACES_DATASET ?? null,
|
||||
|
||||
File renamed without changes.
|
Before Width: | Height: | Size: 2.0 MiB After Width: | Height: | Size: 2.0 MiB |
Binary file not shown.
|
After Width: | Height: | Size: 302 KiB |
@@ -0,0 +1,3 @@
|
||||
<svg width="111" height="74" viewBox="7 29 111 74" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<path d="M33.4509 93V47.56H15.5309V37H64.3309V47.56H46.4109V93H33.4509ZM86.7253 93.96C82.832 93.96 78.9653 93.4533 75.1253 92.44C71.2853 91.3733 68.032 89.88 65.3653 87.96L70.4053 78.04C72.5386 79.5867 75.0186 80.8133 77.8453 81.72C80.672 82.6267 83.5253 83.08 86.4053 83.08C89.6586 83.08 92.2186 82.44 94.0853 81.16C95.952 79.88 96.8853 78.12 96.8853 75.88C96.8853 73.7467 96.0586 72.0667 94.4053 70.84C92.752 69.6133 90.0853 69 86.4053 69H80.4853V60.44L96.0853 42.76L97.5253 47.4H68.1653V37H107.365V45.4L91.8453 63.08L85.2853 59.32H89.0453C95.9253 59.32 101.125 60.8667 104.645 63.96C108.165 67.0533 109.925 71.0267 109.925 75.88C109.925 79.0267 109.099 81.9867 107.445 84.76C105.792 87.48 103.259 89.6933 99.8453 91.4C96.432 93.1067 92.0586 93.96 86.7253 93.96Z" fill="black"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 887 B |
@@ -0,0 +1,81 @@
|
||||
"use strict";
|
||||
|
||||
// Bundle the widget asset catalog into an app-extension (widget) target.
|
||||
//
|
||||
// expo-widgets generates the widget target without a Resources build phase, and
|
||||
// hand-adding a PBXResourcesBuildPhase does NOT get picked up — xcodebuild's
|
||||
// planner never schedules `actool` for it (verified: even a full re-plan skips
|
||||
// it). So instead we add a shell-script phase that runs `actool` directly and
|
||||
// drops the compiled Assets.car into the extension bundle. Marked
|
||||
// alwaysOutOfDate so the build system always runs it.
|
||||
//
|
||||
// Idempotent across re-runs. Returns true when it added the phase, false when
|
||||
// it was already present. Throws when the target does not exist — that means
|
||||
// this ran before expo-widgets created the target (plugin ordering bug) and
|
||||
// silently skipping would ship a widget without its assets.
|
||||
|
||||
const PHASE_NAME = "Compile Widget Assets";
|
||||
|
||||
// Compiles ExpoWidgetsTarget/Assets.xcassets into the extension's resources dir.
|
||||
// Uses only Xcode-provided build settings so it works for device + simulator.
|
||||
const ACTOOL_SCRIPT = [
|
||||
"set -e",
|
||||
'CATALOG="${SRCROOT}/ExpoWidgetsTarget/Assets.xcassets"',
|
||||
'if [ ! -d "$CATALOG" ]; then',
|
||||
' echo "error: widget asset catalog not found at $CATALOG (expo-widgets wiped it? check plugin ordering in app.config.ts)"',
|
||||
" exit 1",
|
||||
"fi",
|
||||
'DEST="${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}"',
|
||||
'mkdir -p "$DEST"',
|
||||
'xcrun actool "$CATALOG" --compile "$DEST" --platform "${PLATFORM_NAME}" --minimum-deployment-target "${IPHONEOS_DEPLOYMENT_TARGET:-16.0}" --output-format human-readable-text',
|
||||
].join("\n");
|
||||
|
||||
function stripComments(map) {
|
||||
const out = {};
|
||||
for (const key of Object.keys(map || {})) {
|
||||
if (key.endsWith("_comment")) continue;
|
||||
out[key] = map[key];
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function findByName(map, name) {
|
||||
for (const [uuid, value] of Object.entries(stripComments(map))) {
|
||||
if (value && value.name === name) return { uuid, value };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('xcode').XcodeProject} proj
|
||||
* @param {{ targetName: string }} opts
|
||||
*/
|
||||
function addWidgetAssetCatalog(proj, opts) {
|
||||
const objects = proj.hash.project.objects;
|
||||
const target = findByName(objects.PBXNativeTarget, opts.targetName);
|
||||
if (!target) {
|
||||
throw new Error(
|
||||
`addWidgetAssetCatalog: target "${opts.targetName}" not found — ` +
|
||||
"withWidgetLogoAsset must be registered before expo-widgets so its " +
|
||||
"xcodeproj mod runs after the widget target is created.",
|
||||
);
|
||||
}
|
||||
|
||||
const phases = target.value.buildPhases || [];
|
||||
const existing = objects.PBXShellScriptBuildPhase || {};
|
||||
const already = Object.entries(stripComments(existing)).some(
|
||||
([uuid, value]) =>
|
||||
value && value.name === `"${PHASE_NAME}"` && phases.some((p) => p.value === uuid),
|
||||
);
|
||||
if (already) return false;
|
||||
|
||||
const { uuid } = proj.addBuildPhase([], "PBXShellScriptBuildPhase", PHASE_NAME, target.uuid, {
|
||||
shellPath: "/bin/sh",
|
||||
shellScript: ACTOOL_SCRIPT,
|
||||
});
|
||||
// Always run: input-analysis is exactly what skipped the Resources phase.
|
||||
objects.PBXShellScriptBuildPhase[uuid].alwaysOutOfDate = 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
module.exports = { addWidgetAssetCatalog };
|
||||
@@ -0,0 +1,68 @@
|
||||
"use strict";
|
||||
|
||||
// Ships the branded T3 mark to the Live Activity / widget extension.
|
||||
//
|
||||
// expo-widgets generates ExpoWidgetsTarget without a Resources build phase and
|
||||
// has no asset support, so this plugin (a) writes an SVG template image set into
|
||||
// the generated widget asset catalog and (b) wires that catalog into the widget
|
||||
// target with an actool build phase.
|
||||
//
|
||||
// ORDERING: must be listed BEFORE "expo-widgets" in the plugins array. Expo
|
||||
// chains same-type mods so the last-registered runs FIRST; registering this
|
||||
// plugin earlier makes its mods run AFTER expo-widgets' mods. That matters
|
||||
// twice: expo-widgets' dangerous mod rmSync's ios/ExpoWidgetsTarget/ (deleting
|
||||
// any catalog written before it), and its xcodeproj mod is what creates the
|
||||
// widget target. Listed after expo-widgets, both steps silently no-op on a
|
||||
// fresh prebuild — which is how prod build 8 shipped without the logo.
|
||||
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const { withDangerousMod, withXcodeProject } = require("expo/config-plugins");
|
||||
const { addWidgetAssetCatalog } = require("./lib/addWidgetAssetCatalog.cjs");
|
||||
|
||||
const TARGET_NAME = "ExpoWidgetsTarget";
|
||||
const CATALOG_NAME = "Assets.xcassets";
|
||||
const IMAGE_SET = "T3Mark.imageset";
|
||||
const SVG_NAME = "T3Mark.svg";
|
||||
|
||||
const CATALOG_CONTENTS = JSON.stringify({ info: { author: "expo", version: 1 } }, null, 2) + "\n";
|
||||
const IMAGE_SET_CONTENTS =
|
||||
JSON.stringify(
|
||||
{
|
||||
images: [{ idiom: "universal", filename: SVG_NAME }],
|
||||
info: { author: "expo", version: 1 },
|
||||
properties: {
|
||||
"preserves-vector-representation": true,
|
||||
"template-rendering-intent": "template",
|
||||
},
|
||||
},
|
||||
null,
|
||||
2,
|
||||
) + "\n";
|
||||
|
||||
function withAssetFiles(config) {
|
||||
return withDangerousMod(config, [
|
||||
"ios",
|
||||
(cfg) => {
|
||||
const source = path.join(cfg.modRequest.projectRoot, "assets", "widget", SVG_NAME);
|
||||
const catalogDir = path.join(cfg.modRequest.platformProjectRoot, TARGET_NAME, CATALOG_NAME);
|
||||
const imageSetDir = path.join(catalogDir, IMAGE_SET);
|
||||
fs.mkdirSync(imageSetDir, { recursive: true });
|
||||
fs.writeFileSync(path.join(catalogDir, "Contents.json"), CATALOG_CONTENTS);
|
||||
fs.writeFileSync(path.join(imageSetDir, "Contents.json"), IMAGE_SET_CONTENTS);
|
||||
fs.copyFileSync(source, path.join(imageSetDir, SVG_NAME));
|
||||
return cfg;
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
function withAssetWiring(config) {
|
||||
return withXcodeProject(config, (cfg) => {
|
||||
addWidgetAssetCatalog(cfg.modResults, { targetName: TARGET_NAME });
|
||||
return cfg;
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = function withWidgetLogoAsset(config) {
|
||||
return withAssetWiring(withAssetFiles(config));
|
||||
};
|
||||
@@ -0,0 +1,30 @@
|
||||
"use strict";
|
||||
|
||||
// One-off: apply the widget asset-catalog wiring to the already-generated
|
||||
// ios/ project so the current build compiles ExpoWidgetsTarget/Assets.xcassets
|
||||
// without a full `expo prebuild`. The durable equivalent lives in
|
||||
// plugins/withWidgetLogoAsset.cjs and runs on prebuild.
|
||||
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
|
||||
const xcodePath = require.resolve("xcode", {
|
||||
paths: [
|
||||
require.resolve("@expo/config-plugins", { paths: [require.resolve("expo/package.json")] }),
|
||||
],
|
||||
});
|
||||
const xcode = require(xcodePath);
|
||||
const { addWidgetAssetCatalog } = require("../plugins/lib/addWidgetAssetCatalog.cjs");
|
||||
|
||||
const pbxprojPath = path.join(__dirname, "..", "ios", "T3CodeDev.xcodeproj", "project.pbxproj");
|
||||
const proj = xcode.project(pbxprojPath);
|
||||
proj.parseSync();
|
||||
|
||||
const added = addWidgetAssetCatalog(proj, { targetName: "ExpoWidgetsTarget" });
|
||||
|
||||
if (added) {
|
||||
fs.writeFileSync(pbxprojPath, proj.writeSync());
|
||||
console.log("Added widget asset-compile phase to ExpoWidgetsTarget.");
|
||||
} else {
|
||||
console.log("No change: phase already present.");
|
||||
}
|
||||
@@ -17,6 +17,8 @@ import { AppText as Text } from "./components/AppText";
|
||||
import { ArchivedThreadsRouteScreen } from "./features/archive/ArchivedThreadsRouteScreen";
|
||||
import { useAgentNotificationNavigation } from "./features/agent-awareness/notificationNavigation";
|
||||
import { ClerkSettingsSheetDetentProvider } from "./features/cloud/ClerkSettingsSheetDetent";
|
||||
import { ConnectOnboardingRouteScreen } from "./features/cloud/ConnectOnboardingRouteScreen";
|
||||
import { useConnectOnboardingNavigation } from "./features/cloud/connectOnboardingNavigation";
|
||||
import { ThreadFilesTreeScreen, ThreadFileScreen } from "./features/files/ThreadFilesRouteScreen";
|
||||
import { AdaptiveWorkspaceLayout } from "./features/layout/AdaptiveWorkspaceLayout";
|
||||
import { HardwareKeyboardCommandProvider } from "./features/keyboard/HardwareKeyboardCommandProvider";
|
||||
@@ -220,6 +222,7 @@ const NewTaskSheetStack = createNativeStackNavigator({
|
||||
// influence the adaptive workspace layout: opening Settings over Home should
|
||||
// not flip the sidebar in or change the active thread.
|
||||
const WORKSPACE_OVERLAY_ROUTES = new Set([
|
||||
"ConnectOnboarding",
|
||||
"Connections",
|
||||
"ConnectionsNew",
|
||||
"GitBranches",
|
||||
@@ -251,6 +254,8 @@ function RootStackLayout(props: {
|
||||
}) {
|
||||
useAgentNotificationNavigation();
|
||||
useThreadOutboxDrain();
|
||||
// Presents the T3 Connect onboarding sheet after an in-session sign-in.
|
||||
useConnectOnboardingNavigation();
|
||||
// Full pathname (sheets included) for keyboard-command scoping; the
|
||||
// workspace layout only reacts to the underlying non-overlay route.
|
||||
const path = getPathFromState(props.state, navigationPathConfig);
|
||||
@@ -412,6 +417,20 @@ export const RootStack = createNativeStackNavigator({
|
||||
sheetGrabberVisible: true,
|
||||
},
|
||||
}),
|
||||
ConnectOnboarding: createNativeStackScreen({
|
||||
screen: ConnectOnboardingRouteScreen,
|
||||
linking: "connect-onboarding",
|
||||
options: {
|
||||
// Root screenOptions hide headers; formSheets that want the native
|
||||
// title bar opt back in with the sheet header preset.
|
||||
...SHEET_SOLID_HEADER_OPTIONS,
|
||||
title: "Set up T3 Connect",
|
||||
gestureEnabled: true,
|
||||
presentation: "formSheet",
|
||||
sheetAllowedDetents: [0.6, 0.95],
|
||||
sheetGrabberVisible: true,
|
||||
},
|
||||
}),
|
||||
Connections: createNativeStackScreen({
|
||||
screen: ConnectionsRouteScreen,
|
||||
linking: "connections",
|
||||
|
||||
@@ -92,7 +92,7 @@ const capabilitiesLayer = Layer.succeedContext(
|
||||
if (session === null) {
|
||||
return yield* new ConnectionBlockedError({
|
||||
reason: "authentication",
|
||||
detail: "Sign in to T3 Cloud to connect this environment.",
|
||||
detail: "Sign in to T3 Connect to connect this environment.",
|
||||
});
|
||||
}
|
||||
const token = yield* session.readClerkToken().pipe(
|
||||
@@ -107,7 +107,7 @@ const capabilitiesLayer = Layer.succeedContext(
|
||||
if (token === null) {
|
||||
return yield* new ConnectionBlockedError({
|
||||
reason: "authentication",
|
||||
detail: "The T3 Cloud session is unavailable.",
|
||||
detail: "The T3 Connect session is unavailable.",
|
||||
});
|
||||
}
|
||||
return token;
|
||||
|
||||
@@ -2,11 +2,20 @@ import type { RelayDeviceRegistrationRequest } from "@t3tools/contracts/relay";
|
||||
|
||||
import type { Preferences } from "../../lib/storage";
|
||||
|
||||
// Development builds are Xcode-signed and receive sandbox APNs tokens;
|
||||
// preview and production builds are distribution-signed and use production
|
||||
// APNs. The relay routes each device's pushes accordingly.
|
||||
export function resolveApsEnvironment(appVariant: unknown): "sandbox" | "production" {
|
||||
return appVariant === "development" ? "sandbox" : "production";
|
||||
}
|
||||
|
||||
export function makeRelayDeviceRegistrationRequest(input: {
|
||||
readonly deviceId: string;
|
||||
readonly label: string;
|
||||
readonly iosMajorVersion: number;
|
||||
readonly appVersion?: string;
|
||||
readonly bundleId?: string;
|
||||
readonly apsEnvironment?: "sandbox" | "production";
|
||||
readonly pushToken?: string;
|
||||
readonly pushToStartToken?: string;
|
||||
readonly notificationsEnabled: boolean;
|
||||
@@ -19,6 +28,8 @@ export function makeRelayDeviceRegistrationRequest(input: {
|
||||
platform: "ios",
|
||||
iosMajorVersion: input.iosMajorVersion,
|
||||
appVersion: input.appVersion,
|
||||
...(input.bundleId ? { bundleId: input.bundleId } : {}),
|
||||
...(input.apsEnvironment ? { apsEnvironment: input.apsEnvironment } : {}),
|
||||
...(input.pushToken ? { pushToken: input.pushToken } : {}),
|
||||
...(input.pushToStartToken ? { pushToStartToken: input.pushToStartToken } : {}),
|
||||
preferences: {
|
||||
|
||||
@@ -16,15 +16,23 @@ import { verifyDpopProof } from "@t3tools/shared/dpop";
|
||||
import type { SavedRemoteConnection } from "../../lib/connection";
|
||||
import { cryptoLayer } from "../cloud/dpop";
|
||||
import { managedRelayClientLayer } from "../cloud/managedRelayLayer";
|
||||
import { makeRelayDeviceRegistrationRequest } from "./registrationPayload";
|
||||
import {
|
||||
clearAgentAwarenessRegistrationRecord,
|
||||
loadAgentAwarenessRegistrationRecord,
|
||||
loadOrCreateAgentAwarenessDeviceId,
|
||||
saveAgentAwarenessRegistrationRecord,
|
||||
} from "../../lib/storage";
|
||||
import { makeRelayDeviceRegistrationRequest, resolveApsEnvironment } from "./registrationPayload";
|
||||
import {
|
||||
AgentAwarenessOperationError,
|
||||
__resetAgentAwarenessRemoteRegistrationForTest,
|
||||
getAgentAwarenessRegistrationStatus,
|
||||
refreshActiveLiveActivityRemoteRegistration,
|
||||
refreshAgentAwarenessRegistration,
|
||||
normalizeAgentAwarenessRelayBaseUrl,
|
||||
registerAgentAwarenessConnection,
|
||||
registerLiveActivityPushToken,
|
||||
releaseAgentAwarenessRelayTokenProvider,
|
||||
setAgentAwarenessRelayTokenProvider,
|
||||
shouldRegisterAgentAwarenessDeviceForProvider,
|
||||
unregisterAgentAwarenessConnection,
|
||||
@@ -41,6 +49,16 @@ const backgroundRuntime = vi.hoisted(() => ({
|
||||
readonly resolve: (exit: Exit.Exit<unknown, unknown>) => void;
|
||||
}>,
|
||||
}));
|
||||
const appStateMock = vi.hoisted(() => ({
|
||||
listeners: [] as Array<(state: string) => void>,
|
||||
}));
|
||||
const registrationRecordStore = vi.hoisted(() => ({
|
||||
current: null as {
|
||||
readonly identity: string;
|
||||
readonly signature: string;
|
||||
readonly pushToStartToken?: string;
|
||||
} | null,
|
||||
}));
|
||||
|
||||
vi.mock("expo-constants", () => ({
|
||||
default: {
|
||||
@@ -100,6 +118,19 @@ vi.mock("react-native", () => ({
|
||||
OS: "ios",
|
||||
Version: "18.0",
|
||||
},
|
||||
AppState: {
|
||||
addEventListener: (_event: string, listener: (state: string) => void) => {
|
||||
appStateMock.listeners.push(listener);
|
||||
return {
|
||||
remove: () => {
|
||||
const index = appStateMock.listeners.indexOf(listener);
|
||||
if (index >= 0) {
|
||||
appStateMock.listeners.splice(index, 1);
|
||||
}
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("../../lib/runtime", () => ({
|
||||
@@ -114,7 +145,18 @@ vi.mock("../../lib/runtime", () => ({
|
||||
vi.mock("../../lib/storage", () => ({
|
||||
loadAgentAwarenessDeviceId: vi.fn(() => Promise.resolve("device-1")),
|
||||
loadOrCreateAgentAwarenessDeviceId: vi.fn(() => Promise.resolve("device-1")),
|
||||
loadPreferences: vi.fn(() => Promise.resolve({})),
|
||||
loadPreferences: vi.fn(() => Promise.resolve({ liveActivitiesEnabled: false })),
|
||||
loadAgentAwarenessRegistrationRecord: vi.fn(() =>
|
||||
Promise.resolve(registrationRecordStore.current),
|
||||
),
|
||||
saveAgentAwarenessRegistrationRecord: vi.fn((record: { identity: string; signature: string }) => {
|
||||
registrationRecordStore.current = record;
|
||||
return Promise.resolve();
|
||||
}),
|
||||
clearAgentAwarenessRegistrationRecord: vi.fn(() => {
|
||||
registrationRecordStore.current = null;
|
||||
return Promise.resolve();
|
||||
}),
|
||||
}));
|
||||
|
||||
function proofIat(proof: string): number {
|
||||
@@ -176,6 +218,12 @@ describe("makeRelayDeviceRegistrationRequest", () => {
|
||||
backgroundRuntime.pending.length = 0;
|
||||
Constants.expoConfig!.extra = {};
|
||||
__resetAgentAwarenessRemoteRegistrationForTest();
|
||||
appStateMock.listeners.length = 0;
|
||||
registrationRecordStore.current = null;
|
||||
vi.mocked(saveAgentAwarenessRegistrationRecord).mockClear();
|
||||
vi.mocked(loadAgentAwarenessRegistrationRecord).mockClear();
|
||||
vi.mocked(clearAgentAwarenessRegistrationRecord).mockClear();
|
||||
vi.mocked(loadOrCreateAgentAwarenessDeviceId).mockResolvedValue("device-1");
|
||||
widgetMocks.getInstances.mockReset();
|
||||
widgetMocks.getInstances.mockReturnValue([]);
|
||||
});
|
||||
@@ -213,6 +261,31 @@ describe("makeRelayDeviceRegistrationRequest", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("registers the app's APNs routing so the relay targets the right bundle", () => {
|
||||
expect(
|
||||
makeRelayDeviceRegistrationRequest({
|
||||
deviceId: "device-1",
|
||||
label: "Julius's iPhone",
|
||||
iosMajorVersion: 18,
|
||||
appVersion: "1.0.0",
|
||||
bundleId: "com.t3tools.t3code.preview",
|
||||
apsEnvironment: resolveApsEnvironment("preview"),
|
||||
notificationsEnabled: true,
|
||||
preferences: {},
|
||||
}),
|
||||
).toMatchObject({
|
||||
bundleId: "com.t3tools.t3code.preview",
|
||||
apsEnvironment: "production",
|
||||
});
|
||||
});
|
||||
|
||||
it("routes development builds to the APNs sandbox", () => {
|
||||
expect(resolveApsEnvironment("development")).toBe("sandbox");
|
||||
expect(resolveApsEnvironment("preview")).toBe("production");
|
||||
expect(resolveApsEnvironment("production")).toBe("production");
|
||||
expect(resolveApsEnvironment(undefined)).toBe("production");
|
||||
});
|
||||
|
||||
it("marks notification delivery disabled when APNs permission is unavailable", () => {
|
||||
expect(
|
||||
makeRelayDeviceRegistrationRequest({
|
||||
@@ -329,6 +402,53 @@ describe("makeRelayDeviceRegistrationRequest", () => {
|
||||
},
|
||||
);
|
||||
|
||||
it.effect(
|
||||
"re-registers active Live Activity tokens when the app returns to the foreground",
|
||||
() => {
|
||||
const activity = {
|
||||
getPushToken: vi.fn(() => Promise.resolve("activity-token")),
|
||||
addPushTokenListener: vi.fn(),
|
||||
};
|
||||
widgetMocks.getInstances.mockReturnValue([activity] as never);
|
||||
setAgentAwarenessRelayTokenProvider(() => Promise.resolve("clerk-token-user-a"));
|
||||
|
||||
return Effect.gen(function* () {
|
||||
yield* runBackgroundOperations();
|
||||
activity.getPushToken.mockClear();
|
||||
|
||||
expect(appStateMock.listeners).toHaveLength(1);
|
||||
for (const listener of appStateMock.listeners) {
|
||||
listener("background");
|
||||
}
|
||||
yield* runBackgroundOperations();
|
||||
expect(activity.getPushToken).not.toHaveBeenCalled();
|
||||
|
||||
for (const listener of appStateMock.listeners) {
|
||||
listener("active");
|
||||
}
|
||||
yield* runBackgroundOperations();
|
||||
expect(activity.getPushToken).toHaveBeenCalled();
|
||||
}).pipe(Effect.provide(relayTestLayer));
|
||||
},
|
||||
);
|
||||
|
||||
it("ends local Live Activities and stops foreground reconciliation on cloud sign-out", () => {
|
||||
const end = vi.fn(() => Promise.resolve());
|
||||
const activity = {
|
||||
getPushToken: vi.fn(() => Promise.resolve("activity-token")),
|
||||
addPushTokenListener: vi.fn(),
|
||||
end,
|
||||
};
|
||||
widgetMocks.getInstances.mockReturnValue([activity] as never);
|
||||
setAgentAwarenessRelayTokenProvider(() => Promise.resolve("clerk-token-user-a"));
|
||||
expect(appStateMock.listeners).toHaveLength(1);
|
||||
|
||||
setAgentAwarenessRelayTokenProvider(null);
|
||||
|
||||
expect(end).toHaveBeenCalledWith("immediate");
|
||||
expect(appStateMock.listeners).toHaveLength(0);
|
||||
});
|
||||
|
||||
it.effect("refreshes APNs registration for connected environments after settings changes", () => {
|
||||
registerAgentAwarenessConnection(savedConnection());
|
||||
return Effect.gen(function* () {
|
||||
@@ -395,6 +515,174 @@ describe("makeRelayDeviceRegistrationRequest", () => {
|
||||
nowEpochSeconds: proofIat(dpop),
|
||||
}),
|
||||
).toMatchObject({ ok: true });
|
||||
expect(getAgentAwarenessRegistrationStatus()).toBe("registered");
|
||||
}).pipe(Effect.provide(relayTestLayer));
|
||||
});
|
||||
|
||||
it.effect("marks registration failed when device registration cannot complete", () => {
|
||||
Constants.expoConfig!.extra = {
|
||||
relay: {
|
||||
url: "https://relay.example.test/",
|
||||
},
|
||||
};
|
||||
vi.mocked(loadOrCreateAgentAwarenessDeviceId).mockRejectedValueOnce(
|
||||
new Error("registration failed"),
|
||||
);
|
||||
setAgentAwarenessRelayTokenProvider(() => Promise.resolve("clerk-token-user-a"));
|
||||
|
||||
return Effect.gen(function* () {
|
||||
// Drive the registration directly so the assertion does not depend on the
|
||||
// background queue draining; refreshAgentAwarenessRegistration swallows the
|
||||
// error but must record the failed status so the settings toggles cannot
|
||||
// read as enabled.
|
||||
yield* refreshAgentAwarenessRegistration();
|
||||
expect(getAgentAwarenessRegistrationStatus()).toBe("failed");
|
||||
}).pipe(Effect.provide(relayTestLayer));
|
||||
});
|
||||
|
||||
it("clears registration status on cloud sign-out", () => {
|
||||
setAgentAwarenessRelayTokenProvider(() => Promise.resolve("clerk-token-user-a"));
|
||||
setAgentAwarenessRelayTokenProvider(null);
|
||||
expect(getAgentAwarenessRegistrationStatus()).toBe("unknown");
|
||||
expect(clearAgentAwarenessRegistrationRecord).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("releases the provider without ending activities or clearing the registration", () => {
|
||||
const end = vi.fn(() => Promise.resolve());
|
||||
const activity = {
|
||||
getPushToken: vi.fn(() => Promise.resolve("activity-token")),
|
||||
addPushTokenListener: vi.fn(),
|
||||
end,
|
||||
};
|
||||
widgetMocks.getInstances.mockReturnValue([activity] as never);
|
||||
registrationRecordStore.current = { identity: "", signature: "sig" };
|
||||
setAgentAwarenessRelayTokenProvider(() => Promise.resolve("clerk-token-user-a"));
|
||||
expect(appStateMock.listeners).toHaveLength(1);
|
||||
|
||||
releaseAgentAwarenessRelayTokenProvider();
|
||||
|
||||
expect(appStateMock.listeners).toHaveLength(0);
|
||||
expect(end).not.toHaveBeenCalled();
|
||||
expect(clearAgentAwarenessRegistrationRecord).not.toHaveBeenCalled();
|
||||
expect(registrationRecordStore.current).not.toBeNull();
|
||||
});
|
||||
|
||||
it.effect("resets a pending status to unknown when relay config is missing", () => {
|
||||
// No relay url configured: registration can neither run nor ever succeed,
|
||||
// so the status must not stick at "pending".
|
||||
setAgentAwarenessRelayTokenProvider(() => Promise.resolve("clerk-token-user-a"));
|
||||
|
||||
return Effect.gen(function* () {
|
||||
yield* runBackgroundOperations();
|
||||
expect(getAgentAwarenessRegistrationStatus()).toBe("unknown");
|
||||
}).pipe(Effect.provide(relayTestLayer));
|
||||
});
|
||||
|
||||
it.effect("keeps a registered status when a later refresh fails", () => {
|
||||
Constants.expoConfig!.extra = {
|
||||
relay: {
|
||||
url: "https://relay.example.test/",
|
||||
},
|
||||
};
|
||||
setAgentAwarenessRelayTokenProvider(() => Promise.resolve("clerk-token-user-a"));
|
||||
|
||||
return Effect.gen(function* () {
|
||||
yield* runBackgroundOperations();
|
||||
expect(getAgentAwarenessRegistrationStatus()).toBe("registered");
|
||||
|
||||
// The relay still holds the accepted registration; a transient refresh
|
||||
// failure must not flip the settings toggles off.
|
||||
vi.mocked(loadOrCreateAgentAwarenessDeviceId).mockRejectedValueOnce(
|
||||
new Error("transient failure"),
|
||||
);
|
||||
yield* refreshAgentAwarenessRegistration();
|
||||
expect(getAgentAwarenessRegistrationStatus()).toBe("registered");
|
||||
}).pipe(Effect.provide(relayTestLayer));
|
||||
});
|
||||
|
||||
it.effect("does not re-register the same account when nothing has changed", () => {
|
||||
Constants.expoConfig!.extra = {
|
||||
relay: {
|
||||
url: "https://relay.example.test/",
|
||||
},
|
||||
};
|
||||
setAgentAwarenessRelayTokenProvider(() => Promise.resolve("clerk-token-user-a"));
|
||||
|
||||
return Effect.gen(function* () {
|
||||
yield* refreshAgentAwarenessRegistration();
|
||||
expect(getAgentAwarenessRegistrationStatus()).toBe("registered");
|
||||
expect(saveAgentAwarenessRegistrationRecord).toHaveBeenCalledTimes(1);
|
||||
expect(registrationRecordStore.current).not.toBeNull();
|
||||
|
||||
// Second attempt with an identical payload must skip the relay entirely,
|
||||
// so no new registration record is written.
|
||||
vi.mocked(saveAgentAwarenessRegistrationRecord).mockClear();
|
||||
yield* refreshAgentAwarenessRegistration();
|
||||
expect(getAgentAwarenessRegistrationStatus()).toBe("registered");
|
||||
expect(saveAgentAwarenessRegistrationRecord).not.toHaveBeenCalled();
|
||||
}).pipe(Effect.provide(relayTestLayer));
|
||||
});
|
||||
|
||||
it.effect("dedupes rapid activity-token re-registrations within the replay window", () => {
|
||||
// Fetch counts are unreliable here (the module-level relay layer captures
|
||||
// the first test's fetch), so assert on the flow's own seams: a real
|
||||
// registration attempt loads the device id, a deduped one short-circuits
|
||||
// before it.
|
||||
const fetchMock = vi.fn((request: RequestInfo | URL) => {
|
||||
const url = request instanceof Request ? request.url : String(request);
|
||||
return Promise.resolve(
|
||||
Response.json(
|
||||
url.endsWith("/v1/client/dpop-token")
|
||||
? {
|
||||
access_token: "relay-dpop-token",
|
||||
issued_token_type: "urn:ietf:params:oauth:token-type:access_token",
|
||||
token_type: "DPoP",
|
||||
expires_in: 300,
|
||||
scope: "mobile:registration",
|
||||
}
|
||||
: { ok: true },
|
||||
),
|
||||
);
|
||||
});
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
Constants.expoConfig!.extra = {
|
||||
relay: {
|
||||
url: "https://relay.example.test/",
|
||||
},
|
||||
};
|
||||
const activity = {
|
||||
getPushToken: vi.fn(() => Promise.resolve("activity-token")),
|
||||
addPushTokenListener: vi.fn(),
|
||||
};
|
||||
widgetMocks.getInstances.mockReturnValue([activity] as never);
|
||||
setAgentAwarenessRelayTokenProvider(() => Promise.resolve("clerk-token-user-a"));
|
||||
|
||||
return Effect.gen(function* () {
|
||||
// Drains the sign-in refresh, which registers the activity token.
|
||||
yield* runBackgroundOperations();
|
||||
expect(activity.getPushToken).toHaveBeenCalled();
|
||||
|
||||
// A burst refresh (foreground / connection update seconds later) must
|
||||
// dedupe: it reads the token but never proceeds to a registration
|
||||
// attempt (which would load the device id first).
|
||||
vi.mocked(loadOrCreateAgentAwarenessDeviceId).mockClear();
|
||||
yield* refreshActiveLiveActivityRemoteRegistration();
|
||||
expect(loadOrCreateAgentAwarenessDeviceId).not.toHaveBeenCalled();
|
||||
}).pipe(Effect.provide(relayTestLayer));
|
||||
});
|
||||
|
||||
it.effect("re-registers when the stored account identity differs", () => {
|
||||
Constants.expoConfig!.extra = {
|
||||
relay: {
|
||||
url: "https://relay.example.test/",
|
||||
},
|
||||
};
|
||||
registrationRecordStore.current = { identity: "someone-else", signature: "stale" };
|
||||
setAgentAwarenessRelayTokenProvider(() => Promise.resolve("clerk-token-user-a"));
|
||||
|
||||
return Effect.gen(function* () {
|
||||
yield* refreshAgentAwarenessRegistration();
|
||||
expect(saveAgentAwarenessRegistrationRecord).toHaveBeenCalledTimes(1);
|
||||
}).pipe(Effect.provide(relayTestLayer));
|
||||
});
|
||||
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { addPushToStartTokenListener, type LiveActivity } from "expo-widgets";
|
||||
import { type LiveActivity } from "expo-widgets";
|
||||
import Constants from "expo-constants";
|
||||
import * as Notifications from "expo-notifications";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Schema from "effect/Schema";
|
||||
import { Platform } from "react-native";
|
||||
import { AppState, Platform } from "react-native";
|
||||
import type { EnvironmentId } from "@t3tools/contracts";
|
||||
import {
|
||||
type RelayDeviceRegistrationRequest,
|
||||
type RelayAgentActivitySnapshotResponse,
|
||||
type RelayLiveActivityRegistrationRequest,
|
||||
} from "@t3tools/contracts/relay";
|
||||
import { findErrorTraceId } from "@t3tools/client-runtime/errors";
|
||||
@@ -20,13 +21,16 @@ import {
|
||||
import type { SavedRemoteConnection } from "../../lib/connection";
|
||||
import { runtime } from "../../lib/runtime";
|
||||
import {
|
||||
clearAgentAwarenessRegistrationRecord,
|
||||
loadAgentAwarenessDeviceId,
|
||||
loadAgentAwarenessRegistrationRecord,
|
||||
loadOrCreateAgentAwarenessDeviceId,
|
||||
loadPreferences,
|
||||
saveAgentAwarenessRegistrationRecord,
|
||||
} from "../../lib/storage";
|
||||
import AgentActivity, { type AgentActivityProps } from "../../widgets/AgentActivity";
|
||||
import { resolveCloudPublicConfig } from "../cloud/publicConfig";
|
||||
import { makeRelayDeviceRegistrationRequest } from "./registrationPayload";
|
||||
import { makeRelayDeviceRegistrationRequest, resolveApsEnvironment } from "./registrationPayload";
|
||||
|
||||
const REMOTE_ACTIVITY_REGISTRATION_RETRY_MS = 15_000;
|
||||
|
||||
@@ -42,6 +46,8 @@ const AgentAwarenessOperation = Schema.Literals([
|
||||
"read-live-activity-push-token",
|
||||
"load-live-activity-registration-identifier",
|
||||
"list-active-live-activities",
|
||||
"load-live-activity-prime-preferences",
|
||||
"prime-live-activity",
|
||||
]);
|
||||
|
||||
export class AgentAwarenessOperationError extends Schema.TaggedErrorClass<AgentAwarenessOperationError>()(
|
||||
@@ -58,8 +64,49 @@ export class AgentAwarenessOperationError extends Schema.TaggedErrorClass<AgentA
|
||||
|
||||
const environmentConnections = new Map<EnvironmentId, SavedRemoteConnection>();
|
||||
const activityPushTokenListeners = new WeakSet<LiveActivity<AgentActivityProps>>();
|
||||
let pushToStartSubscription: { remove: () => void } | null = null;
|
||||
// Activity tokens the relay recently accepted, by acceptance time. The refresh
|
||||
// runs on sign-in, every app foreground, and every environment-connection
|
||||
// update, which arrive in bursts and spammed identical registrations. But the
|
||||
// registration is not a pure no-op: the relay replays the current aggregate to
|
||||
// this device on every accepted registration, and that replay is the
|
||||
// foreground reconciliation that repairs drifted or orphaned activities. So
|
||||
// dedupe only within a short window — bursts collapse to one request, while a
|
||||
// foreground after real time away still triggers a replay. Cleared on
|
||||
// sign-out/identity change alongside the device registration state.
|
||||
const ACTIVITY_TOKEN_REREGISTER_INTERVAL_MS = 60_000;
|
||||
const registeredActivityPushTokens = new Map<string, number>();
|
||||
let pushTokenSubscription: { remove: () => void } | null = null;
|
||||
let appStateSubscription: { remove: () => void } | null = null;
|
||||
|
||||
// Whether the relay has actually accepted this device's registration. The
|
||||
// notification/Live Activity settings toggles must reflect this rather than
|
||||
// only local iOS permission or saved preferences: if the registration request
|
||||
// never succeeded, the device cannot receive anything, so the switches must
|
||||
// not read as enabled.
|
||||
export type AgentAwarenessRegistrationStatus = "unknown" | "pending" | "registered" | "failed";
|
||||
let registrationStatus: AgentAwarenessRegistrationStatus = "unknown";
|
||||
const registrationStatusListeners = new Set<() => void>();
|
||||
|
||||
function setRegistrationStatus(next: AgentAwarenessRegistrationStatus): void {
|
||||
if (registrationStatus === next) {
|
||||
return;
|
||||
}
|
||||
registrationStatus = next;
|
||||
for (const listener of registrationStatusListeners) {
|
||||
listener();
|
||||
}
|
||||
}
|
||||
|
||||
export function getAgentAwarenessRegistrationStatus(): AgentAwarenessRegistrationStatus {
|
||||
return registrationStatus;
|
||||
}
|
||||
|
||||
export function subscribeAgentAwarenessRegistrationStatus(listener: () => void): () => void {
|
||||
registrationStatusListeners.add(listener);
|
||||
return () => {
|
||||
registrationStatusListeners.delete(listener);
|
||||
};
|
||||
}
|
||||
let activeLiveActivityRegistrationRetry: ReturnType<typeof setTimeout> | null = null;
|
||||
let relayTokenProvider: (() => Promise<string | null>) | null = null;
|
||||
let relayTokenProviderIdentity: string | null = null;
|
||||
@@ -74,7 +121,6 @@ let pendingDeviceRegistration: {
|
||||
} | null = null;
|
||||
|
||||
interface DeviceRegistrationInput {
|
||||
readonly pushToStartToken?: string;
|
||||
readonly observedPushToken?: string;
|
||||
}
|
||||
|
||||
@@ -120,32 +166,66 @@ export function setAgentAwarenessRelayTokenProvider(
|
||||
deviceRegistrationGeneration++;
|
||||
activeDeviceRegistration = null;
|
||||
pendingDeviceRegistration = null;
|
||||
registeredActivityPushTokens.clear();
|
||||
}
|
||||
relayTokenProvider = provider;
|
||||
relayTokenProviderIdentity = provider ? (identity ?? null) : null;
|
||||
if (!provider) {
|
||||
pushToStartSubscription?.remove();
|
||||
pushToStartSubscription = null;
|
||||
pushTokenSubscription?.remove();
|
||||
pushTokenSubscription = null;
|
||||
appStateSubscription?.remove();
|
||||
appStateSubscription = null;
|
||||
if (activeLiveActivityRegistrationRetry) {
|
||||
clearTimeout(activeLiveActivityRegistrationRetry);
|
||||
activeLiveActivityRegistrationRetry = null;
|
||||
}
|
||||
// Without a signed-in user the relay can no longer update or end these
|
||||
// activities, so they would sit orphaned on the lock screen.
|
||||
endLocalLiveActivities("live activity cleanup after cloud sign-out failed");
|
||||
setRegistrationStatus("unknown");
|
||||
// Sign-out is the only thing that invalidates a stored registration, so the
|
||||
// next sign-in re-registers.
|
||||
void clearAgentAwarenessRegistrationRecord().catch((error: unknown) => {
|
||||
logRegistrationError("clear registration record on sign-out failed", error);
|
||||
});
|
||||
return;
|
||||
}
|
||||
ensurePushToStartListener();
|
||||
ensurePushTokenListener();
|
||||
ensureAppStateListener();
|
||||
runRegistrationInBackground(
|
||||
refreshActiveLiveActivityRemoteRegistration(),
|
||||
"active live activity registration after cloud sign-in failed",
|
||||
);
|
||||
if (isExistingIdentity) {
|
||||
// Same account re-activating (e.g. Clerk token refresh) normally needs no
|
||||
// re-registration — but if the previous attempt never succeeded, this is
|
||||
// the only trigger that will retry it before the next cold start.
|
||||
if (registrationStatus !== "registered") {
|
||||
enqueueDeviceRegistration({}, "device registration retry after cloud session refresh failed");
|
||||
}
|
||||
return;
|
||||
}
|
||||
enqueueDeviceRegistration({}, "device registration after cloud sign-in failed");
|
||||
}
|
||||
|
||||
// Detach the provider and native listeners without the destructive sign-out
|
||||
// cleanup. For provider teardown while the user is still signed in (e.g. the
|
||||
// auth bridge unmounting/remounting), ending lock-screen activities and wiping
|
||||
// the persisted registration would be wrong — the relay still holds a valid
|
||||
// registration and the next mount reuses it.
|
||||
export function releaseAgentAwarenessRelayTokenProvider(): void {
|
||||
relayTokenProvider = null;
|
||||
relayTokenProviderIdentity = null;
|
||||
pushTokenSubscription?.remove();
|
||||
pushTokenSubscription = null;
|
||||
appStateSubscription?.remove();
|
||||
appStateSubscription = null;
|
||||
if (activeLiveActivityRegistrationRetry) {
|
||||
clearTimeout(activeLiveActivityRegistrationRetry);
|
||||
activeLiveActivityRegistrationRetry = null;
|
||||
}
|
||||
}
|
||||
|
||||
function iosMajorVersion(): number {
|
||||
const version = Platform.Version;
|
||||
if (typeof version === "number") {
|
||||
@@ -211,6 +291,27 @@ const relayToken = (
|
||||
});
|
||||
});
|
||||
|
||||
// Stable fingerprint of everything the relay stores for this device. When it
|
||||
// matches the last accepted registration for the same account, re-registering
|
||||
// is a no-op, so a launch that changed nothing skips the request entirely.
|
||||
function registrationSignature(body: RelayDeviceRegistrationRequest): string {
|
||||
return [
|
||||
body.deviceId,
|
||||
body.pushToken ?? "",
|
||||
body.bundleId ?? "",
|
||||
body.apsEnvironment ?? "",
|
||||
body.appVersion ?? "",
|
||||
body.label,
|
||||
body.iosMajorVersion,
|
||||
body.preferences.notificationsEnabled,
|
||||
body.preferences.liveActivitiesEnabled,
|
||||
body.preferences.notifyOnApproval,
|
||||
body.preferences.notifyOnInput,
|
||||
body.preferences.notifyOnCompletion,
|
||||
body.preferences.notifyOnFailure,
|
||||
].join("|");
|
||||
}
|
||||
|
||||
function registerDeviceWithRelay(
|
||||
body: RelayDeviceRegistrationRequest,
|
||||
expectedGeneration: number,
|
||||
@@ -223,7 +324,13 @@ function registerDeviceWithRelay(
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!readRelayConfig()) return;
|
||||
const relayConfig = readRelayConfig();
|
||||
if (!relayConfig) {
|
||||
// Nothing is in flight and nothing can succeed until configuration
|
||||
// appears; "pending" would otherwise stick forever.
|
||||
setRegistrationStatus("unknown");
|
||||
return;
|
||||
}
|
||||
const token = yield* relayToken("read-device-registration-relay-token");
|
||||
if (expectedGeneration !== deviceRegistrationGeneration) {
|
||||
logRegistrationDebug("device registration cancelled after auth lookup", {
|
||||
@@ -234,6 +341,37 @@ function registerDeviceWithRelay(
|
||||
}
|
||||
if (!token) {
|
||||
logRegistrationDebug("relay device registration skipped; user is not signed in");
|
||||
setRegistrationStatus("unknown");
|
||||
return;
|
||||
}
|
||||
|
||||
// Skip the request when this account already registered an identical
|
||||
// payload; the relay upsert would be a no-op. The record is only cleared on
|
||||
// sign-out, so a device stays registered across launches without re-hitting
|
||||
// the relay every time the app opens.
|
||||
const identity = relayTokenProviderIdentity ?? "";
|
||||
const persisted = yield* Effect.tryPromise({
|
||||
try: () => loadAgentAwarenessRegistrationRecord(),
|
||||
catch: (cause) => cause,
|
||||
}).pipe(Effect.orElseSucceed(() => null));
|
||||
if (expectedGeneration !== deviceRegistrationGeneration) {
|
||||
// Signed out while the record loaded — do not resurrect the cleared
|
||||
// record or report the previous account's registration as current.
|
||||
logRegistrationDebug("device registration cancelled after record lookup", {
|
||||
expectedGeneration,
|
||||
currentGeneration: deviceRegistrationGeneration,
|
||||
});
|
||||
return;
|
||||
}
|
||||
const payload = body;
|
||||
// The relay URL participates so pointing the app at a different relay
|
||||
// invalidates the record and re-registers there.
|
||||
const signature = `${relayConfig.url}|${registrationSignature(payload)}`;
|
||||
if (persisted && persisted.identity === identity && persisted.signature === signature) {
|
||||
setRegistrationStatus("registered");
|
||||
logRegistrationDebug("relay device registration skipped; already registered for account", {
|
||||
expectedGeneration,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -243,8 +381,27 @@ function registerDeviceWithRelay(
|
||||
});
|
||||
yield* client.registerDevice({
|
||||
clerkToken: token,
|
||||
payload: body,
|
||||
payload,
|
||||
});
|
||||
if (expectedGeneration !== deviceRegistrationGeneration) {
|
||||
// Signed out while the request was in flight: the sign-out path already
|
||||
// reset the status and cleared the record for the next account, so a
|
||||
// stale success must not overwrite either.
|
||||
logRegistrationDebug("device registration completed after sign-out; result discarded", {
|
||||
expectedGeneration,
|
||||
currentGeneration: deviceRegistrationGeneration,
|
||||
});
|
||||
return;
|
||||
}
|
||||
setRegistrationStatus("registered");
|
||||
yield* Effect.promise(() =>
|
||||
saveAgentAwarenessRegistrationRecord({
|
||||
identity,
|
||||
signature,
|
||||
}).catch((error: unknown) => {
|
||||
logRegistrationError("persist registration record failed", error);
|
||||
}),
|
||||
);
|
||||
logRegistrationDebug("relay device registration request completed", {
|
||||
expectedGeneration,
|
||||
});
|
||||
@@ -278,6 +435,91 @@ function unregisterDeviceWithRelay(input: {
|
||||
});
|
||||
}
|
||||
|
||||
// Arms the lock-screen card the moment the user starts agent work from this
|
||||
// phone, while the app is still foregrounded and the fresh activity's token
|
||||
// can be registered immediately. The seeded row is a best-effort placeholder;
|
||||
// the relay's registration replay repaints it with the authoritative
|
||||
// aggregate within seconds. No-ops when a card is already armed.
|
||||
export function armAgentAwarenessLiveActivityForLocalWork(input: {
|
||||
readonly threadTitle: string;
|
||||
readonly projectTitle: string;
|
||||
}): void {
|
||||
if (!canRegisterRemoteLiveActivities() || !relayTokenProvider) {
|
||||
return;
|
||||
}
|
||||
void loadPreferences()
|
||||
.catch(() => null)
|
||||
.then((preferences) => {
|
||||
if (preferences?.liveActivitiesEnabled === false) {
|
||||
return;
|
||||
}
|
||||
armAgentAwarenessLiveActivityForLocalWorkNow(input);
|
||||
});
|
||||
}
|
||||
|
||||
function armAgentAwarenessLiveActivityForLocalWorkNow(input: {
|
||||
readonly threadTitle: string;
|
||||
readonly projectTitle: string;
|
||||
}): void {
|
||||
try {
|
||||
if (AgentActivity.getInstances().length > 0) {
|
||||
return;
|
||||
}
|
||||
const nowIso = new Date(Date.now()).toISOString();
|
||||
const activity = AgentActivity.start({
|
||||
title: "T3 Code",
|
||||
subtitle: "Agent work in progress",
|
||||
activeCount: 1,
|
||||
updatedAt: nowIso,
|
||||
activities: [
|
||||
{
|
||||
environmentId: "",
|
||||
threadId: "",
|
||||
projectTitle: input.projectTitle,
|
||||
threadTitle: input.threadTitle,
|
||||
modelTitle: "",
|
||||
phase: "starting",
|
||||
status: "Connecting",
|
||||
updatedAt: nowIso,
|
||||
deepLink: "/",
|
||||
},
|
||||
],
|
||||
});
|
||||
logRegistrationDebug("live activity card armed for local work", {
|
||||
threadTitle: input.threadTitle,
|
||||
});
|
||||
runRegistrationInBackground(
|
||||
registerLiveActivityPushToken({ activity }).pipe(Effect.asVoid),
|
||||
"live activity arming after local task start failed",
|
||||
);
|
||||
} catch (error) {
|
||||
logRegistrationError("live activity arming failed", error);
|
||||
}
|
||||
}
|
||||
|
||||
function readAgentActivitySnapshot(): Effect.Effect<
|
||||
RelayAgentActivitySnapshotResponse | null,
|
||||
never,
|
||||
ManagedRelay.ManagedRelayClient
|
||||
> {
|
||||
return Effect.gen(function* () {
|
||||
if (!readRelayConfig()) return null;
|
||||
const token = yield* relayToken("read-live-activity-registration-relay-token");
|
||||
if (!token) {
|
||||
return null;
|
||||
}
|
||||
const client = yield* ManagedRelay.ManagedRelayClient;
|
||||
return yield* client.getAgentActivitySnapshot({ clerkToken: token });
|
||||
}).pipe(
|
||||
Effect.catch((error) =>
|
||||
Effect.sync(() => {
|
||||
logRegistrationError("agent activity snapshot read failed", error);
|
||||
return null;
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
function registerLiveActivityWithRelay(
|
||||
body: RelayLiveActivityRegistrationRequest,
|
||||
): Effect.Effect<boolean, unknown, ManagedRelay.ManagedRelayClient> {
|
||||
@@ -332,14 +574,8 @@ function mergeDeviceRegistrationInput(
|
||||
current: DeviceRegistrationInput,
|
||||
next: DeviceRegistrationInput,
|
||||
): DeviceRegistrationInput {
|
||||
return {
|
||||
...((next.pushToStartToken ?? current.pushToStartToken)
|
||||
? { pushToStartToken: next.pushToStartToken ?? current.pushToStartToken }
|
||||
: {}),
|
||||
...((next.observedPushToken ?? current.observedPushToken)
|
||||
? { observedPushToken: next.observedPushToken ?? current.observedPushToken }
|
||||
: {}),
|
||||
};
|
||||
const observedPushToken = next.observedPushToken ?? current.observedPushToken;
|
||||
return observedPushToken ? { observedPushToken } : {};
|
||||
}
|
||||
|
||||
function registrationAddsInformation(
|
||||
@@ -347,8 +583,7 @@ function registrationAddsInformation(
|
||||
next: DeviceRegistrationInput,
|
||||
): boolean {
|
||||
return (
|
||||
(next.pushToStartToken !== undefined && next.pushToStartToken !== current.pushToStartToken) ||
|
||||
(next.observedPushToken !== undefined && next.observedPushToken !== current.observedPushToken)
|
||||
next.observedPushToken !== undefined && next.observedPushToken !== current.observedPushToken
|
||||
);
|
||||
}
|
||||
|
||||
@@ -363,8 +598,10 @@ function startPendingDeviceRegistration(): void {
|
||||
logRegistrationDebug("device registration started", {
|
||||
generation,
|
||||
hasObservedPushToken: next.input.observedPushToken !== undefined,
|
||||
hasPushToStartToken: next.input.pushToStartToken !== undefined,
|
||||
});
|
||||
if (registrationStatus !== "registered") {
|
||||
setRegistrationStatus("pending");
|
||||
}
|
||||
const registration = {
|
||||
input: next.input,
|
||||
operation: Promise.resolve(),
|
||||
@@ -375,6 +612,13 @@ function startPendingDeviceRegistration(): void {
|
||||
runtime.runPromiseExit(registerDevice(next.input, generation)),
|
||||
);
|
||||
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
|
||||
// A transient failure on a later refresh (e.g. token rotation) leaves
|
||||
// the prior accepted registration intact on the relay, so an already
|
||||
// registered device stays "registered" rather than flipping the
|
||||
// settings toggles off.
|
||||
if (registrationStatus !== "registered") {
|
||||
setRegistrationStatus("failed");
|
||||
}
|
||||
logRegistrationError(next.context, squashAtomCommandFailure(result));
|
||||
}
|
||||
logRegistrationDebug("device registration finished", { generation });
|
||||
@@ -444,14 +688,16 @@ function registerDevice(
|
||||
expectedGeneration,
|
||||
notificationsEnabled: pushTokenRegistration.notificationsEnabled,
|
||||
});
|
||||
const bundleId = Constants.expoConfig?.ios?.bundleIdentifier?.trim();
|
||||
yield* registerDeviceWithRelay(
|
||||
makeRelayDeviceRegistrationRequest({
|
||||
deviceId,
|
||||
label: Constants.deviceName?.trim() || "iOS device",
|
||||
iosMajorVersion: iosMajorVersion(),
|
||||
appVersion: Constants.expoConfig?.version,
|
||||
...(bundleId ? { bundleId } : {}),
|
||||
apsEnvironment: resolveApsEnvironment(Constants.expoConfig?.extra?.appVariant),
|
||||
...(pushTokenRegistration.pushToken ? { pushToken: pushTokenRegistration.pushToken } : {}),
|
||||
...(input?.pushToStartToken ? { pushToStartToken: input.pushToStartToken } : {}),
|
||||
notificationsEnabled: pushTokenRegistration.notificationsEnabled,
|
||||
preferences,
|
||||
}),
|
||||
@@ -460,27 +706,12 @@ function registerDevice(
|
||||
});
|
||||
}
|
||||
|
||||
function registerDeviceForCurrentUser(
|
||||
pushToStartToken?: string,
|
||||
): Effect.Effect<void, unknown, ManagedRelay.ManagedRelayClient> {
|
||||
return registerDevice(pushToStartToken ? { pushToStartToken } : undefined);
|
||||
}
|
||||
|
||||
function registerPushToStartTokenForCurrentUser(pushToStartToken: string): void {
|
||||
enqueueDeviceRegistration({ pushToStartToken }, "push-to-start token registration failed");
|
||||
}
|
||||
|
||||
function ensurePushToStartListener(): void {
|
||||
if (pushToStartSubscription || !canRegisterRemoteLiveActivities()) {
|
||||
return;
|
||||
}
|
||||
|
||||
pushToStartSubscription = addPushToStartTokenListener((event) => {
|
||||
const token = event.activityPushToStartToken;
|
||||
if (token) {
|
||||
registerPushToStartTokenForCurrentUser(token);
|
||||
}
|
||||
});
|
||||
function registerDeviceForCurrentUser(): Effect.Effect<
|
||||
void,
|
||||
unknown,
|
||||
ManagedRelay.ManagedRelayClient
|
||||
> {
|
||||
return registerDevice(undefined);
|
||||
}
|
||||
|
||||
function ensurePushTokenListener(): void {
|
||||
@@ -498,14 +729,51 @@ function ensurePushTokenListener(): void {
|
||||
});
|
||||
}
|
||||
|
||||
// Re-registering activity tokens on foreground makes the relay replay the
|
||||
// current aggregate to this device, which updates content that drifted while
|
||||
// pushes could not be delivered and ends orphaned activities whose end push
|
||||
// never arrived. (Deduped by ACTIVITY_TOKEN_REREGISTER_INTERVAL_MS: rapid
|
||||
// foreground/sign-in bursts collapse to one registration, but returning after
|
||||
// real time away still replays.)
|
||||
function ensureAppStateListener(): void {
|
||||
if (appStateSubscription || !canRegisterRemoteLiveActivities()) {
|
||||
return;
|
||||
}
|
||||
|
||||
appStateSubscription = AppState.addEventListener("change", (state) => {
|
||||
if (state !== "active") {
|
||||
return;
|
||||
}
|
||||
runRegistrationInBackground(
|
||||
refreshActiveLiveActivityRemoteRegistration(),
|
||||
"active live activity reconciliation after app foreground failed",
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function endLocalLiveActivities(context: string): void {
|
||||
if (!canRegisterRemoteLiveActivities()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
for (const activity of AgentActivity.getInstances()) {
|
||||
activity.end("immediate").catch((error: unknown) => {
|
||||
logRegistrationError(context, error);
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
logRegistrationError(context, error);
|
||||
}
|
||||
}
|
||||
|
||||
export function registerAgentAwarenessConnection(connection: SavedRemoteConnection): void {
|
||||
if (!canRegisterRemoteLiveActivities()) {
|
||||
return;
|
||||
}
|
||||
|
||||
environmentConnections.set(connection.environmentId, connection);
|
||||
ensurePushToStartListener();
|
||||
ensurePushTokenListener();
|
||||
ensureAppStateListener();
|
||||
enqueueDeviceRegistration({}, "device registration failed");
|
||||
runRegistrationInBackground(
|
||||
refreshActiveLiveActivityRemoteRegistration(),
|
||||
@@ -523,10 +791,10 @@ export function unregisterAgentAwarenessConnection(environmentId: EnvironmentId)
|
||||
|
||||
export function unregisterAllAgentAwarenessConnections(): void {
|
||||
environmentConnections.clear();
|
||||
pushToStartSubscription?.remove();
|
||||
pushToStartSubscription = null;
|
||||
pushTokenSubscription?.remove();
|
||||
pushTokenSubscription = null;
|
||||
appStateSubscription?.remove();
|
||||
appStateSubscription = null;
|
||||
if (activeLiveActivityRegistrationRetry) {
|
||||
clearTimeout(activeLiveActivityRegistrationRetry);
|
||||
activeLiveActivityRegistrationRetry = null;
|
||||
@@ -541,6 +809,11 @@ export function refreshAgentAwarenessRegistration(): Effect.Effect<
|
||||
return registerDeviceForCurrentUser().pipe(
|
||||
Effect.catch((error) =>
|
||||
Effect.sync(() => {
|
||||
// Same rationale as the queued path: a failed refresh does not undo an
|
||||
// already accepted registration.
|
||||
if (registrationStatus !== "registered") {
|
||||
setRegistrationStatus("failed");
|
||||
}
|
||||
logRegistrationError("device registration refresh failed", error);
|
||||
}),
|
||||
),
|
||||
@@ -549,10 +822,10 @@ export function refreshAgentAwarenessRegistration(): Effect.Effect<
|
||||
|
||||
export function __resetAgentAwarenessRemoteRegistrationForTest(): void {
|
||||
environmentConnections.clear();
|
||||
pushToStartSubscription?.remove();
|
||||
pushToStartSubscription = null;
|
||||
pushTokenSubscription?.remove();
|
||||
pushTokenSubscription = null;
|
||||
appStateSubscription?.remove();
|
||||
appStateSubscription = null;
|
||||
if (activeLiveActivityRegistrationRetry) {
|
||||
clearTimeout(activeLiveActivityRegistrationRetry);
|
||||
activeLiveActivityRegistrationRetry = null;
|
||||
@@ -562,6 +835,9 @@ export function __resetAgentAwarenessRemoteRegistrationForTest(): void {
|
||||
deviceRegistrationGeneration++;
|
||||
activeDeviceRegistration = null;
|
||||
pendingDeviceRegistration = null;
|
||||
registrationStatus = "unknown";
|
||||
registrationStatusListeners.clear();
|
||||
registeredActivityPushTokens.clear();
|
||||
}
|
||||
|
||||
export function unregisterAgentAwarenessDeviceForCurrentUser(
|
||||
@@ -649,6 +925,13 @@ function registerLiveActivityPushTokenValue(input: {
|
||||
readonly activityPushToken: string;
|
||||
}): Effect.Effect<boolean, unknown, ManagedRelay.ManagedRelayClient> {
|
||||
return Effect.gen(function* () {
|
||||
const acceptedAt = registeredActivityPushTokens.get(input.activityPushToken);
|
||||
if (
|
||||
acceptedAt !== undefined &&
|
||||
Date.now() - acceptedAt < ACTIVITY_TOKEN_REREGISTER_INTERVAL_MS
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
const deviceId = yield* Effect.tryPromise({
|
||||
try: () => loadOrCreateAgentAwarenessDeviceId(),
|
||||
catch: (cause) =>
|
||||
@@ -662,6 +945,7 @@ function registerLiveActivityPushTokenValue(input: {
|
||||
activityPushToken: input.activityPushToken,
|
||||
});
|
||||
if (registered) {
|
||||
registeredActivityPushTokens.set(input.activityPushToken, Date.now());
|
||||
logRegistrationDebug("live activity push token registered", {
|
||||
tokenSuffix: input.activityPushToken.slice(-8),
|
||||
});
|
||||
@@ -694,7 +978,7 @@ export function refreshActiveLiveActivityRemoteRegistration(): Effect.Effect<
|
||||
return;
|
||||
}
|
||||
|
||||
const activities = yield* Effect.try({
|
||||
let activities = yield* Effect.try({
|
||||
try: () => AgentActivity.getInstances(),
|
||||
catch: (cause) =>
|
||||
new AgentAwarenessOperationError({
|
||||
@@ -710,6 +994,79 @@ export function refreshActiveLiveActivityRemoteRegistration(): Effect.Effect<
|
||||
),
|
||||
);
|
||||
|
||||
// The relay tracks exactly one card per device; if concurrent arming ever
|
||||
// produced extras, end them so only one keeps receiving updates.
|
||||
if (activities.length > 1) {
|
||||
for (const extra of activities.slice(1)) {
|
||||
extra.end("immediate").catch((error: unknown) => {
|
||||
logRegistrationError("duplicate live activity cleanup failed", error);
|
||||
});
|
||||
}
|
||||
activities = activities.slice(0, 1);
|
||||
}
|
||||
|
||||
// Activities are only ever created here, in the foreground, where the
|
||||
// update token can be observed and registered immediately — the relay
|
||||
// never remote-starts one (background push-to-start wakes proved too
|
||||
// unreliable to hand the token over). Arming is conditional: the relay is
|
||||
// asked what the card would show first, so an idle open never creates an
|
||||
// empty lock-screen card, and an armed card is born with the real
|
||||
// aggregate instead of a placeholder.
|
||||
if (activities.length === 0) {
|
||||
const preferences = yield* Effect.tryPromise({
|
||||
try: () => loadPreferences(),
|
||||
catch: (cause) =>
|
||||
new AgentAwarenessOperationError({
|
||||
operation: "load-live-activity-prime-preferences",
|
||||
cause,
|
||||
}),
|
||||
}).pipe(Effect.orElseSucceed(() => null));
|
||||
// The toggle defaults to on: an unset preference (fresh install) must
|
||||
// prime, so only an explicit false blocks it.
|
||||
if (preferences?.liveActivitiesEnabled !== false) {
|
||||
const snapshot = yield* readAgentActivitySnapshot();
|
||||
// The snapshot request yields; an arm-on-send may have created the
|
||||
// card in the meantime. Re-check so two cards are never started.
|
||||
const armedMeanwhile = yield* Effect.try({
|
||||
try: () => AgentActivity.getInstances(),
|
||||
catch: () => [] as ReadonlyArray<LiveActivity<AgentActivityProps>>,
|
||||
}).pipe(Effect.orElseSucceed(() => [] as ReadonlyArray<LiveActivity<AgentActivityProps>>));
|
||||
if (armedMeanwhile.length > 0) {
|
||||
activities = [...armedMeanwhile];
|
||||
} else if (snapshot?.aggregate && snapshot.aggregate.activeCount > 0) {
|
||||
const aggregate = snapshot.aggregate;
|
||||
const primed = yield* Effect.try({
|
||||
try: () =>
|
||||
AgentActivity.start({
|
||||
title: aggregate.title,
|
||||
subtitle: aggregate.subtitle,
|
||||
activeCount: aggregate.activeCount,
|
||||
updatedAt: aggregate.updatedAt,
|
||||
activities: aggregate.activities,
|
||||
}),
|
||||
catch: (cause) =>
|
||||
new AgentAwarenessOperationError({
|
||||
operation: "prime-live-activity",
|
||||
cause,
|
||||
}),
|
||||
}).pipe(
|
||||
Effect.catch((error) =>
|
||||
Effect.sync(() => {
|
||||
logRegistrationError("live activity priming failed", error);
|
||||
return null;
|
||||
}),
|
||||
),
|
||||
);
|
||||
if (primed) {
|
||||
logRegistrationDebug("live activity card primed", {
|
||||
activeCount: aggregate.activeCount,
|
||||
});
|
||||
activities = [primed];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const registrationResults = yield* Effect.forEach(activities, (activity) =>
|
||||
registerLiveActivityPushToken({ activity }).pipe(
|
||||
Effect.map((registered) => !registered),
|
||||
|
||||
@@ -14,9 +14,11 @@ import { runtime } from "../../lib/runtime";
|
||||
import { appAtomRegistry } from "../../state/atom-registry";
|
||||
import { useAtomCommand } from "../../state/use-atom-command";
|
||||
import {
|
||||
releaseAgentAwarenessRelayTokenProvider,
|
||||
setAgentAwarenessRelayTokenProvider,
|
||||
unregisterAgentAwarenessDeviceForCurrentUser,
|
||||
} from "../agent-awareness/remoteRegistration";
|
||||
import { clearConnectOnboardingRequest, requestConnectOnboarding } from "./connectOnboarding";
|
||||
import { resolveCloudPublicConfig, resolveRelayClerkTokenOptions } from "./publicConfig";
|
||||
|
||||
function resetManagedRelayTokenCache() {
|
||||
@@ -66,6 +68,19 @@ function CloudAuthBridge(props: { readonly children: ReactNode }) {
|
||||
const nextAccount = isSignedIn && userId ? userId : null;
|
||||
observedAccountRef.current = nextAccount;
|
||||
|
||||
// Every sign-in or account switch that completes during this session (a
|
||||
// cold start observes undefined → account and must not re-prompt) requests
|
||||
// the T3 Connect onboarding sheet — account transitions clear the
|
||||
// connected environments, so each new session starts with no devices to
|
||||
// reach. The request itself is issued after the cleanup transition inside
|
||||
// activateSession, so the sheet never lists the previous account's
|
||||
// environments; sign-out drops any not-yet-presented request instead.
|
||||
const isAccountTransition =
|
||||
previousObservedAccount !== undefined && previousObservedAccount !== nextAccount;
|
||||
if (isAccountTransition && nextAccount === null) {
|
||||
clearConnectOnboardingRequest();
|
||||
}
|
||||
|
||||
const queueAccountCleanup = (
|
||||
previous: {
|
||||
readonly userId: string;
|
||||
@@ -113,6 +128,9 @@ function CloudAuthBridge(props: { readonly children: ReactNode }) {
|
||||
}
|
||||
previousTokenProviderRef.current = { userId, provider: tokenProvider };
|
||||
activateCloudRelayAccount(userId, tokenProvider);
|
||||
if (isAccountTransition) {
|
||||
requestConnectOnboarding(userId);
|
||||
}
|
||||
};
|
||||
const activateAfterTransition = (transition: Promise<void>) => {
|
||||
void (async () => {
|
||||
@@ -143,7 +161,11 @@ function CloudAuthBridge(props: { readonly children: ReactNode }) {
|
||||
useEffect(
|
||||
() => () => {
|
||||
previousTokenProviderRef.current = null;
|
||||
deactivateCloudRelayAccount();
|
||||
// Unmounting is not a sign-out: the user is usually still signed in, so
|
||||
// detach the provider without ending lock-screen activities or wiping the
|
||||
// persisted registration (a remount reuses both).
|
||||
releaseAgentAwarenessRelayTokenProvider();
|
||||
setManagedRelaySession(appAtomRegistry, null);
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
@@ -39,7 +39,7 @@ export function CloudWaitlistEnrollment(props: { readonly onSignIn: () => void }
|
||||
You are on the waitlist
|
||||
</Text>
|
||||
<Text className="text-center font-sans text-base text-foreground-secondary">
|
||||
We will email you when your T3 Cloud access is ready.
|
||||
We will email you when your T3 Connect access is ready.
|
||||
</Text>
|
||||
<SignInAction onPress={props.onSignIn} />
|
||||
</View>
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
import { NativeHeaderToolbar } from "../../native/StackHeader";
|
||||
import { useAuth } from "@clerk/expo";
|
||||
import { StackActions, useNavigation } from "@react-navigation/native";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { Pressable, RefreshControl, ScrollView, View } from "react-native";
|
||||
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
||||
|
||||
import { reportAtomCommandResult, settlePromise } from "@t3tools/client-runtime/state/runtime";
|
||||
import { AppText as Text } from "../../components/AppText";
|
||||
import { useRemoteConnections } from "../../state/use-remote-environment-registry";
|
||||
import { CloudEnvironmentRows } from "../connection/CloudEnvironmentRows";
|
||||
import { splitEnvironmentSections } from "../connection/environmentSections";
|
||||
import { useConnectionController } from "../connection/useConnectionController";
|
||||
import { optOutOfConnectOnboarding } from "./connectOnboardingOptOut";
|
||||
import { hasCloudPublicConfig } from "./publicConfig";
|
||||
|
||||
/**
|
||||
* Post-sign-in onboarding sheet for T3 Connect. Mobile never publishes
|
||||
* environments itself — it consumes ones published elsewhere — so this simply
|
||||
* surfaces the account's T3 Connect environments right after sign-in so every
|
||||
* device can be connected in one go. It shows on every sign-in: sign-out
|
||||
* clears the connected environments, so each new session starts from zero.
|
||||
*/
|
||||
export function ConnectOnboardingRouteScreen() {
|
||||
const navigation = useNavigation();
|
||||
|
||||
// The route is deep-linkable; without cloud config the sheet would present
|
||||
// empty with no chrome to dismiss it, so bail back out instead.
|
||||
useEffect(() => {
|
||||
if (hasCloudPublicConfig()) {
|
||||
return;
|
||||
}
|
||||
if (navigation.canGoBack()) {
|
||||
navigation.goBack();
|
||||
} else {
|
||||
navigation.dispatch(StackActions.replace("Home"));
|
||||
}
|
||||
}, [navigation]);
|
||||
|
||||
return hasCloudPublicConfig() ? <ConfiguredConnectOnboardingRouteScreen /> : null;
|
||||
}
|
||||
|
||||
function ConfiguredConnectOnboardingRouteScreen() {
|
||||
const navigation = useNavigation();
|
||||
const insets = useSafeAreaInsets();
|
||||
const { isSignedIn, userId } = useAuth({ treatPendingAsSignedOut: false });
|
||||
const { connectedEnvironments, onReconnectEnvironment } = useRemoteConnections();
|
||||
const { refreshRelayEnvironments } = useConnectionController();
|
||||
const { connectedCloudEnvironments } = splitEnvironmentSections({
|
||||
connectedEnvironments,
|
||||
cloudEnvironments: null,
|
||||
});
|
||||
|
||||
// Pull-to-refresh tracks its own spinner instead of discovery's refreshing
|
||||
// flag, so background refreshes (e.g. the sign-in one) don't yank the
|
||||
// content down.
|
||||
const [isPullRefreshing, setIsPullRefreshing] = useState(false);
|
||||
const handlePullRefresh = useCallback(() => {
|
||||
void (async () => {
|
||||
setIsPullRefreshing(true);
|
||||
await refreshRelayEnvironments();
|
||||
setIsPullRefreshing(false);
|
||||
})();
|
||||
}, [refreshRelayEnvironments]);
|
||||
|
||||
const handleClose = useCallback(() => {
|
||||
navigation.goBack();
|
||||
}, [navigation]);
|
||||
|
||||
// Persist before dismissing so a quick sign-out/sign-in cannot race ahead
|
||||
// of the preference write; the write is a local secure-store update.
|
||||
const handleDontShowAgain = useCallback(() => {
|
||||
void (async () => {
|
||||
if (userId) {
|
||||
const result = await settlePromise(() => optOutOfConnectOnboarding(userId));
|
||||
reportAtomCommandResult(result, { label: "connect onboarding opt-out" });
|
||||
}
|
||||
navigation.goBack();
|
||||
})();
|
||||
}, [navigation, userId]);
|
||||
|
||||
return (
|
||||
<View collapsable={false} className="flex-1 bg-sheet">
|
||||
<NativeHeaderToolbar placement="right">
|
||||
<NativeHeaderToolbar.Button icon="xmark" onPress={handleClose} separateBackground />
|
||||
</NativeHeaderToolbar>
|
||||
<ScrollView
|
||||
alwaysBounceVertical
|
||||
contentInsetAdjustmentBehavior="automatic"
|
||||
showsVerticalScrollIndicator={false}
|
||||
style={{ flex: 1 }}
|
||||
contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
|
||||
contentContainerStyle={{
|
||||
gap: 16,
|
||||
paddingHorizontal: 20,
|
||||
paddingTop: 16,
|
||||
}}
|
||||
refreshControl={
|
||||
<RefreshControl refreshing={isPullRefreshing} onRefresh={handlePullRefresh} />
|
||||
}
|
||||
>
|
||||
{isSignedIn ? (
|
||||
<CloudEnvironmentRows
|
||||
connectedCloudEnvironments={connectedCloudEnvironments}
|
||||
onReconnectEnvironment={onReconnectEnvironment}
|
||||
showHeader={false}
|
||||
/>
|
||||
) : (
|
||||
<View collapsable={false} className="rounded-[24px] bg-card p-5">
|
||||
<Text className="text-sm leading-normal text-foreground-muted">
|
||||
Sign in to your T3 account to set up T3 Connect.
|
||||
</Text>
|
||||
</View>
|
||||
)}
|
||||
|
||||
{userId ? (
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
hitSlop={8}
|
||||
onPress={handleDontShowAgain}
|
||||
className="items-center py-1 active:opacity-70"
|
||||
>
|
||||
<Text className="text-xs text-foreground-muted">{"Don't show this again"}</Text>
|
||||
</Pressable>
|
||||
) : null}
|
||||
</ScrollView>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { Atom } from "effect/unstable/reactivity";
|
||||
|
||||
import { appAtomRegistry } from "../../state/atom-registry";
|
||||
|
||||
// Signals RootStackLayout (inside the navigation tree) that an in-session
|
||||
// sign-in just completed. Holds the account id so a sign-out between the
|
||||
// request and the navigation cannot present the sheet for the wrong account.
|
||||
export const connectOnboardingRequestAtom = Atom.make<string | null>(null).pipe(
|
||||
Atom.keepAlive,
|
||||
Atom.withLabel("mobile:connect-onboarding-request"),
|
||||
);
|
||||
|
||||
/**
|
||||
* Requests the onboarding sheet for the given account. Sign-out clears the
|
||||
* connected environments, so onboarding runs on every in-session sign-in —
|
||||
* each new session starts with no connected devices.
|
||||
*/
|
||||
export function requestConnectOnboarding(accountId: string): void {
|
||||
appAtomRegistry.set(connectOnboardingRequestAtom, accountId);
|
||||
}
|
||||
|
||||
export function clearConnectOnboardingRequest(): void {
|
||||
appAtomRegistry.set(connectOnboardingRequestAtom, null);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import { useAtomValue } from "@effect/atom-react";
|
||||
import { useNavigation } from "@react-navigation/native";
|
||||
import { useEffect } from "react";
|
||||
|
||||
import { appAtomRegistry } from "../../state/atom-registry";
|
||||
import { clearConnectOnboardingRequest, connectOnboardingRequestAtom } from "./connectOnboarding";
|
||||
import { isConnectOnboardingOptedOut } from "./connectOnboardingOptOut";
|
||||
|
||||
// Sign-in happens inside the Settings sheet; give its detent/session-state
|
||||
// transitions a beat to settle before presenting another formSheet on top.
|
||||
const PRESENT_ONBOARDING_DELAY_MS = 600;
|
||||
|
||||
/**
|
||||
* Consumes the onboarding request inside the navigation tree (RootStackLayout)
|
||||
* and presents the onboarding formSheet. Lives apart from connectOnboarding.ts
|
||||
* so non-navigation consumers (CloudAuthProvider) do not pull
|
||||
* @react-navigation into their module graph.
|
||||
*/
|
||||
export function useConnectOnboardingNavigation(): void {
|
||||
const navigation = useNavigation();
|
||||
const requestedAccountId = useAtomValue(connectOnboardingRequestAtom);
|
||||
|
||||
useEffect(() => {
|
||||
if (requestedAccountId === null) {
|
||||
return;
|
||||
}
|
||||
let cancelled = false;
|
||||
const timer = setTimeout(() => {
|
||||
void (async () => {
|
||||
// A failed preference read prefers showing the sheet.
|
||||
const optedOut = await isConnectOnboardingOptedOut(requestedAccountId).catch(() => false);
|
||||
// The cancelled flag covers effect re-runs, but a sign-out can clear
|
||||
// the request atom moments before this render commits — re-check the
|
||||
// atom so a stale request never presents the sheet.
|
||||
if (cancelled || appAtomRegistry.get(connectOnboardingRequestAtom) !== requestedAccountId) {
|
||||
return;
|
||||
}
|
||||
clearConnectOnboardingRequest();
|
||||
if (!optedOut) {
|
||||
navigation.navigate("ConnectOnboarding");
|
||||
}
|
||||
})();
|
||||
}, PRESENT_ONBOARDING_DELAY_MS);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
clearTimeout(timer);
|
||||
};
|
||||
}, [navigation, requestedAccountId]);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { loadPreferences, updatePreferences } from "../../lib/storage";
|
||||
|
||||
// Lives apart from connectOnboarding.ts so CloudAuthProvider (which imports
|
||||
// the request signal) never pulls lib/storage — expo-secure-store — into its
|
||||
// module graph; that breaks CloudAuthProvider.test.ts suite loading.
|
||||
|
||||
/** Whether the account chose "Don't show this again". */
|
||||
export async function isConnectOnboardingOptedOut(accountId: string): Promise<boolean> {
|
||||
const preferences = await loadPreferences();
|
||||
return preferences.connectOnboardingOptOutAccounts?.includes(accountId) ?? false;
|
||||
}
|
||||
|
||||
/** Persists "Don't show this again" for the account. */
|
||||
export async function optOutOfConnectOnboarding(accountId: string): Promise<void> {
|
||||
await updatePreferences((current) => {
|
||||
const optedOut = current.connectOnboardingOptOutAccounts ?? [];
|
||||
return optedOut.includes(accountId)
|
||||
? {}
|
||||
: { connectOnboardingOptOutAccounts: [...optedOut, accountId] };
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,379 @@
|
||||
import { useAuth } from "@clerk/expo";
|
||||
import { SymbolView } from "expo-symbols";
|
||||
import {
|
||||
connectionStatusText,
|
||||
type EnvironmentConnectionPhase,
|
||||
} from "@t3tools/client-runtime/connection";
|
||||
import type { EnvironmentId } from "@t3tools/contracts";
|
||||
import { useCallback, useState } from "react";
|
||||
import {
|
||||
ActivityIndicator,
|
||||
Pressable,
|
||||
Switch,
|
||||
type NativeSyntheticEvent,
|
||||
type TextLayoutEventData,
|
||||
View,
|
||||
} from "react-native";
|
||||
|
||||
import { AppText as Text } from "../../components/AppText";
|
||||
import { cn } from "../../lib/cn";
|
||||
import { copyTextWithHaptic } from "../../lib/copyTextWithHaptic";
|
||||
import { useThemeColor } from "../../lib/useThemeColor";
|
||||
import type { ConnectedEnvironmentSummary } from "../../state/remote-runtime-types";
|
||||
import { availableCloudEnvironmentPresentation } from "../cloud/cloudEnvironmentPresentation";
|
||||
import { ConnectionStatusDot } from "./ConnectionStatusDot";
|
||||
import { type RelayEnvironmentView, useConnectionController } from "./useConnectionController";
|
||||
|
||||
/**
|
||||
* "T3 Connect" section: every environment published to the signed-in account,
|
||||
* with connect switches, availability status, refresh, and loading/error
|
||||
* states. Shared between the Settings environments screen and the T3 Connect
|
||||
* onboarding sheet.
|
||||
*/
|
||||
export function CloudEnvironmentRows(props: {
|
||||
readonly connectedCloudEnvironments: ReadonlyArray<ConnectedEnvironmentSummary>;
|
||||
readonly onReconnectEnvironment: (environmentId: EnvironmentId) => void;
|
||||
/**
|
||||
* Hide the "T3 Connect" section title + refresh button for hosts that
|
||||
* provide their own chrome (the onboarding sheet's native header and
|
||||
* pull-to-refresh).
|
||||
*/
|
||||
readonly showHeader?: boolean;
|
||||
}) {
|
||||
const { isSignedIn } = useAuth({ treatPendingAsSignedOut: false });
|
||||
const controller = useConnectionController();
|
||||
const iconColor = useThemeColor("--color-icon");
|
||||
const availableCloudEnvironments = controller.availableRelayEnvironments;
|
||||
const [expandedErrorId, setExpandedErrorId] = useState<string | null>(null);
|
||||
const hasCloudRows =
|
||||
props.connectedCloudEnvironments.length > 0 || availableCloudEnvironments.length > 0;
|
||||
|
||||
const handleConnectCloudEnvironment = useCallback(
|
||||
(entry: RelayEnvironmentView) => controller.connectRelayEnvironment(entry.environment),
|
||||
[controller],
|
||||
);
|
||||
|
||||
const handleDisconnectCloudEnvironment = useCallback(
|
||||
(environmentId: EnvironmentId) => controller.removeEnvironment(environmentId),
|
||||
[controller],
|
||||
);
|
||||
|
||||
const handleToggleCloudError = useCallback((environmentId: string) => {
|
||||
setExpandedErrorId((current) => (current === environmentId ? null : environmentId));
|
||||
}, []);
|
||||
|
||||
const showHeader = props.showHeader ?? true;
|
||||
|
||||
if (!isSignedIn) return null;
|
||||
|
||||
return (
|
||||
<View collapsable={false} className={cn("gap-3", showHeader && "mt-5")}>
|
||||
{showHeader ? (
|
||||
<View className="flex-row items-center justify-between px-1">
|
||||
<Text className="text-sm font-t3-bold uppercase text-foreground-muted">T3 Connect</Text>
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
disabled={controller.relayDiscovery.isRefreshing}
|
||||
onPress={() => {
|
||||
void controller.refreshRelayEnvironments();
|
||||
}}
|
||||
className="h-9 w-9 items-center justify-center rounded-full bg-subtle active:opacity-70 disabled:opacity-50"
|
||||
>
|
||||
{controller.relayDiscovery.isRefreshing ? (
|
||||
<ActivityIndicator color={iconColor} size="small" />
|
||||
) : (
|
||||
<SymbolView
|
||||
name="arrow.clockwise"
|
||||
size={14}
|
||||
tintColor={iconColor}
|
||||
type="monochrome"
|
||||
/>
|
||||
)}
|
||||
</Pressable>
|
||||
</View>
|
||||
) : null}
|
||||
|
||||
{hasCloudRows ? (
|
||||
<View collapsable={false} className="overflow-hidden rounded-[24px] bg-card">
|
||||
{props.connectedCloudEnvironments.map((environment, index) => (
|
||||
<ConnectedCloudEnvironmentRow
|
||||
key={environment.environmentId}
|
||||
environment={environment}
|
||||
borderTop={index !== 0}
|
||||
onConnect={() => props.onReconnectEnvironment(environment.environmentId)}
|
||||
onDisconnect={() => handleDisconnectCloudEnvironment(environment.environmentId)}
|
||||
errorExpanded={expandedErrorId === environment.environmentId}
|
||||
onToggleError={() => handleToggleCloudError(environment.environmentId)}
|
||||
/>
|
||||
))}
|
||||
{availableCloudEnvironments.map((environment, index) => (
|
||||
<CloudEnvironmentRow
|
||||
key={environment.environment.environmentId}
|
||||
environment={environment}
|
||||
borderTop={props.connectedCloudEnvironments.length > 0 || index !== 0}
|
||||
onConnect={() => handleConnectCloudEnvironment(environment)}
|
||||
errorExpanded={expandedErrorId === environment.environment.environmentId}
|
||||
onToggleError={() => handleToggleCloudError(environment.environment.environmentId)}
|
||||
/>
|
||||
))}
|
||||
</View>
|
||||
) : controller.relayDiscovery.isRefreshing ? (
|
||||
<View collapsable={false} className="items-center gap-3 rounded-[24px] bg-card p-6">
|
||||
<ActivityIndicator color={iconColor} />
|
||||
<Text className="text-center text-sm leading-normal text-foreground-muted">
|
||||
Loading linked cloud environments.
|
||||
</Text>
|
||||
</View>
|
||||
) : controller.relayDiscovery.error ? null : (
|
||||
<View collapsable={false} className="rounded-[24px] bg-card p-5">
|
||||
<Text className="text-sm leading-normal text-foreground-muted">
|
||||
No additional linked cloud environments.
|
||||
</Text>
|
||||
</View>
|
||||
)}
|
||||
|
||||
{/* Rendered alongside any connected rows — a failed discovery must not
|
||||
hide behind an otherwise-healthy list. */}
|
||||
{controller.relayDiscovery.error && !controller.relayDiscovery.isRefreshing ? (
|
||||
<View collapsable={false} className="gap-3 rounded-[24px] bg-card p-5">
|
||||
<Text className="text-base font-t3-bold text-foreground">
|
||||
Could not load T3 Connect environments
|
||||
</Text>
|
||||
<Text className="text-sm text-foreground-muted">{controller.relayDiscovery.error}</Text>
|
||||
{controller.relayDiscovery.errorTraceId ? (
|
||||
<CopyTraceIdButton traceId={controller.relayDiscovery.errorTraceId} />
|
||||
) : null}
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
onPress={() => {
|
||||
void controller.refreshRelayEnvironments();
|
||||
}}
|
||||
className="self-start rounded-full bg-subtle px-3.5 py-2 active:opacity-70"
|
||||
>
|
||||
<Text className="text-xs font-t3-bold text-foreground">Try again</Text>
|
||||
</Pressable>
|
||||
</View>
|
||||
) : null}
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
function ConnectedCloudEnvironmentRow(props: {
|
||||
readonly environment: ConnectedEnvironmentSummary;
|
||||
readonly borderTop: boolean;
|
||||
readonly errorExpanded: boolean;
|
||||
readonly onConnect: () => void;
|
||||
readonly onDisconnect: () => void;
|
||||
readonly onToggleError: () => void;
|
||||
}) {
|
||||
return (
|
||||
<CloudEnvironmentRowShell
|
||||
borderTop={props.borderTop}
|
||||
connectionError={props.environment.connectionError}
|
||||
connectionErrorTraceId={props.environment.connectionErrorTraceId}
|
||||
connectionState={props.environment.connectionState}
|
||||
errorExpanded={props.errorExpanded}
|
||||
label={props.environment.environmentLabel}
|
||||
onValueChange={(enabled) => {
|
||||
if (enabled) {
|
||||
props.onConnect();
|
||||
return;
|
||||
}
|
||||
props.onDisconnect();
|
||||
}}
|
||||
onToggleError={props.onToggleError}
|
||||
value={props.environment.connectionState !== "available"}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function CloudEnvironmentRow(props: {
|
||||
readonly environment: RelayEnvironmentView;
|
||||
readonly borderTop: boolean;
|
||||
readonly errorExpanded: boolean;
|
||||
readonly onConnect: () => void;
|
||||
readonly onToggleError: () => void;
|
||||
}) {
|
||||
const presentation = availableCloudEnvironmentPresentation({
|
||||
isStatusPending: props.environment.availability === "checking",
|
||||
status: props.environment.status,
|
||||
statusError: props.environment.error,
|
||||
statusErrorTraceId: props.environment.traceId,
|
||||
});
|
||||
|
||||
return (
|
||||
<CloudEnvironmentRowShell
|
||||
borderTop={props.borderTop}
|
||||
connectionError={presentation.connectionError}
|
||||
connectionErrorTraceId={presentation.connectionErrorTraceId}
|
||||
connectionState={presentation.connectionState}
|
||||
errorExpanded={props.errorExpanded}
|
||||
label={props.environment.environment.label}
|
||||
onValueChange={(enabled) => {
|
||||
if (enabled) {
|
||||
props.onConnect();
|
||||
}
|
||||
}}
|
||||
onToggleError={props.onToggleError}
|
||||
statusText={presentation.statusText}
|
||||
value={false}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function CloudEnvironmentRowShell(props: {
|
||||
readonly borderTop: boolean;
|
||||
readonly connectionError: string | null;
|
||||
readonly connectionErrorTraceId: string | null;
|
||||
readonly connectionState: EnvironmentConnectionPhase;
|
||||
readonly disabled?: boolean;
|
||||
readonly errorExpanded: boolean;
|
||||
readonly label: string;
|
||||
readonly onToggleError: () => void;
|
||||
readonly onValueChange: (enabled: boolean) => void;
|
||||
readonly statusText?: string;
|
||||
readonly value: boolean;
|
||||
}) {
|
||||
const activeTrack = String(useThemeColor("--color-switch-active"));
|
||||
const track = String(useThemeColor("--color-secondary-border"));
|
||||
const chevron = useThemeColor("--color-chevron");
|
||||
const isRetrying =
|
||||
props.connectionState === "connecting" || props.connectionState === "reconnecting";
|
||||
const shouldPulse = isRetrying;
|
||||
const statusText =
|
||||
props.statusText ??
|
||||
connectionStatusText({
|
||||
phase: props.connectionState,
|
||||
error: props.connectionError,
|
||||
traceId: props.connectionErrorTraceId,
|
||||
});
|
||||
const statusClassName = props.connectionError
|
||||
? "text-rose-500 dark:text-rose-400"
|
||||
: "text-foreground-muted";
|
||||
const [errorMeasurement, setErrorMeasurement] = useState<{
|
||||
readonly text: string;
|
||||
readonly lineCount: number;
|
||||
} | null>(null);
|
||||
const errorTraceId = props.connectionErrorTraceId;
|
||||
const measuredErrorText = errorTraceId ? `${statusText} Trace ID: ${errorTraceId}` : statusText;
|
||||
const errorLineCount =
|
||||
errorMeasurement?.text === measuredErrorText ? errorMeasurement.lineCount : 0;
|
||||
const errorCanExpand = props.connectionError !== null && errorLineCount > 1;
|
||||
const isErrorExpanded = errorCanExpand && props.errorExpanded;
|
||||
const StatusContainer = errorCanExpand ? Pressable : View;
|
||||
const onMeasuredErrorTextLayout = useCallback(
|
||||
(event: NativeSyntheticEvent<TextLayoutEventData>) => {
|
||||
if (!props.connectionError) {
|
||||
return;
|
||||
}
|
||||
const nextLineCount = event.nativeEvent.lines.length;
|
||||
setErrorMeasurement((currentMeasurement) =>
|
||||
currentMeasurement?.text === measuredErrorText &&
|
||||
currentMeasurement.lineCount === nextLineCount
|
||||
? currentMeasurement
|
||||
: { text: measuredErrorText, lineCount: nextLineCount },
|
||||
);
|
||||
},
|
||||
[measuredErrorText, props.connectionError],
|
||||
);
|
||||
return (
|
||||
<View
|
||||
collapsable={false}
|
||||
className={cn(
|
||||
"flex-row items-center gap-3 bg-card px-4 py-3.5",
|
||||
props.borderTop && "border-t border-border",
|
||||
)}
|
||||
>
|
||||
<View className="min-w-0 flex-1 gap-0.5">
|
||||
<View className="min-w-0 flex-row items-center gap-2">
|
||||
<ConnectionStatusDot state={props.connectionState} pulse={shouldPulse} size={7} />
|
||||
<Text
|
||||
className="min-w-0 flex-shrink text-base font-t3-bold leading-snug text-foreground"
|
||||
numberOfLines={1}
|
||||
>
|
||||
{props.label}
|
||||
</Text>
|
||||
</View>
|
||||
{props.connectionError ? (
|
||||
<Text
|
||||
aria-hidden
|
||||
className={cn("absolute left-0 right-0 text-xs", statusClassName)}
|
||||
onTextLayout={onMeasuredErrorTextLayout}
|
||||
style={{ opacity: 0, zIndex: -1 }}
|
||||
>
|
||||
{measuredErrorText}
|
||||
</Text>
|
||||
) : null}
|
||||
<StatusContainer
|
||||
{...(errorCanExpand
|
||||
? { accessibilityRole: "button" as const, onPress: props.onToggleError }
|
||||
: {})}
|
||||
className="min-w-0 flex-row items-start gap-1"
|
||||
>
|
||||
<Text
|
||||
className={cn("min-w-0 flex-1 text-xs", statusClassName)}
|
||||
numberOfLines={isErrorExpanded ? undefined : 1}
|
||||
>
|
||||
{statusText}
|
||||
{errorTraceId ? (
|
||||
<>
|
||||
{" Trace ID: "}
|
||||
<Text
|
||||
accessibilityHint="Copies the trace ID"
|
||||
accessibilityRole="button"
|
||||
className={cn("text-xs underline", statusClassName)}
|
||||
onLongPress={(event) => {
|
||||
event.stopPropagation();
|
||||
copyTextWithHaptic(errorTraceId, { target: "connection-trace-id" });
|
||||
}}
|
||||
onPress={(event) => {
|
||||
event.stopPropagation();
|
||||
}}
|
||||
style={{ textDecorationStyle: "dotted" }}
|
||||
>
|
||||
{errorTraceId}
|
||||
</Text>
|
||||
</>
|
||||
) : null}
|
||||
</Text>
|
||||
{errorCanExpand ? (
|
||||
<SymbolView
|
||||
name="chevron.down"
|
||||
size={10}
|
||||
tintColor={chevron}
|
||||
type="monochrome"
|
||||
style={{
|
||||
marginTop: 3,
|
||||
transform: [{ rotate: isErrorExpanded ? "180deg" : "0deg" }],
|
||||
}}
|
||||
/>
|
||||
) : null}
|
||||
</StatusContainer>
|
||||
</View>
|
||||
<Switch
|
||||
disabled={props.disabled}
|
||||
ios_backgroundColor={track}
|
||||
onValueChange={props.onValueChange}
|
||||
trackColor={{ false: track, true: activeTrack }}
|
||||
value={props.value}
|
||||
/>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
function CopyTraceIdButton(props: { readonly traceId: string }) {
|
||||
const iconColor = useThemeColor("--color-icon");
|
||||
|
||||
return (
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
onPress={() => {
|
||||
copyTextWithHaptic(props.traceId, { target: "connection-trace-id" });
|
||||
}}
|
||||
className="self-start flex-row items-center gap-1.5 rounded-full bg-subtle px-3 py-2 active:opacity-70"
|
||||
>
|
||||
<SymbolView name="doc.on.doc" size={12} tintColor={iconColor} type="monochrome" />
|
||||
<Text className="text-xs font-t3-bold text-foreground">Copy trace ID</Text>
|
||||
</Pressable>
|
||||
);
|
||||
}
|
||||
@@ -135,7 +135,7 @@ export function ConnectionEnvironmentRow(props: {
|
||||
>
|
||||
{props.environment.isRelayManaged ? (
|
||||
<Text className="text-sm text-foreground-muted">
|
||||
Managed by T3 Cloud. Tunnel details update automatically.
|
||||
Managed by T3 Connect. Tunnel details update automatically.
|
||||
</Text>
|
||||
) : (
|
||||
<>
|
||||
|
||||
@@ -35,7 +35,7 @@ function cloudEnvironment(environmentId: string): RelayClientEnvironmentRecord {
|
||||
}
|
||||
|
||||
describe("mobile environment settings sections", () => {
|
||||
it("keeps saved relay-managed connections under T3 Cloud", () => {
|
||||
it("keeps saved relay-managed connections under T3 Connect", () => {
|
||||
const local = connectedEnvironment({
|
||||
environmentId: "environment-local",
|
||||
isRelayManaged: false,
|
||||
|
||||
@@ -112,7 +112,12 @@ function AddProjectShell(props: { readonly children: ReactNode }) {
|
||||
const insets = useSafeAreaInsets();
|
||||
|
||||
return (
|
||||
<View className="flex-1 bg-sheet">
|
||||
// collapsable={false} is load-bearing: if this wrapper is flattened, the
|
||||
// ScrollView lands directly under RNSSafeAreaView and RNS's formSheet
|
||||
// scroll-view frame correction mistakes this full-height wrapper for a
|
||||
// "header" sibling, coercing the ScrollView to zero height (blank sheet
|
||||
// as soon as the sheet re-lays-out, e.g. when the keyboard opens).
|
||||
<View collapsable={false} className="flex-1 bg-sheet">
|
||||
<ScrollView
|
||||
keyboardShouldPersistTaps="handled"
|
||||
contentInsetAdjustmentBehavior="automatic"
|
||||
|
||||
@@ -1,38 +1,18 @@
|
||||
import { useAuth } from "@clerk/expo";
|
||||
import { NativeHeaderToolbar } from "../../native/StackHeader";
|
||||
import { useNavigation } from "@react-navigation/native";
|
||||
import { SymbolView } from "expo-symbols";
|
||||
import {
|
||||
connectionStatusText,
|
||||
type EnvironmentConnectionPhase,
|
||||
} from "@t3tools/client-runtime/connection";
|
||||
import type { EnvironmentId } from "@t3tools/contracts";
|
||||
import { useCallback, useState } from "react";
|
||||
import {
|
||||
ActivityIndicator,
|
||||
Pressable,
|
||||
ScrollView,
|
||||
Switch,
|
||||
type NativeSyntheticEvent,
|
||||
type TextLayoutEventData,
|
||||
View,
|
||||
} from "react-native";
|
||||
import { ScrollView, View } from "react-native";
|
||||
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
||||
|
||||
import { AppText as Text } from "../../components/AppText";
|
||||
import {
|
||||
type RelayEnvironmentView,
|
||||
useConnectionController,
|
||||
} from "../connection/useConnectionController";
|
||||
import { hasCloudPublicConfig } from "../cloud/publicConfig";
|
||||
import { availableCloudEnvironmentPresentation } from "../cloud/cloudEnvironmentPresentation";
|
||||
import { CloudEnvironmentRows } from "../connection/CloudEnvironmentRows";
|
||||
import { ConnectionEnvironmentRow } from "../connection/ConnectionEnvironmentRow";
|
||||
import { ConnectionStatusDot } from "../connection/ConnectionStatusDot";
|
||||
import { splitEnvironmentSections } from "../connection/environmentSections";
|
||||
import { cn } from "../../lib/cn";
|
||||
import { copyTextWithHaptic } from "../../lib/copyTextWithHaptic";
|
||||
import { useThemeColor } from "../../lib/useThemeColor";
|
||||
import type { ConnectedEnvironmentSummary } from "../../state/remote-runtime-types";
|
||||
import { useRemoteConnections } from "../../state/use-remote-environment-registry";
|
||||
|
||||
export function SettingsEnvironmentsRouteScreen() {
|
||||
@@ -117,7 +97,7 @@ export function SettingsEnvironmentsRouteScreen() {
|
||||
)}
|
||||
|
||||
{hasCloudPublicConfig() ? (
|
||||
<ConfiguredCloudEnvironmentRows
|
||||
<CloudEnvironmentRows
|
||||
connectedCloudEnvironments={connectedCloudEnvironments}
|
||||
onReconnectEnvironment={onReconnectEnvironment}
|
||||
/>
|
||||
@@ -126,323 +106,3 @@ export function SettingsEnvironmentsRouteScreen() {
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
function ConfiguredCloudEnvironmentRows(props: {
|
||||
readonly connectedCloudEnvironments: ReadonlyArray<ConnectedEnvironmentSummary>;
|
||||
readonly onReconnectEnvironment: (environmentId: EnvironmentId) => void;
|
||||
}) {
|
||||
const { isSignedIn } = useAuth({ treatPendingAsSignedOut: false });
|
||||
const controller = useConnectionController();
|
||||
const iconColor = useThemeColor("--color-icon");
|
||||
const availableCloudEnvironments = controller.availableRelayEnvironments;
|
||||
const [expandedErrorId, setExpandedErrorId] = useState<string | null>(null);
|
||||
const hasCloudRows =
|
||||
props.connectedCloudEnvironments.length > 0 || availableCloudEnvironments.length > 0;
|
||||
|
||||
const handleConnectCloudEnvironment = useCallback(
|
||||
(entry: RelayEnvironmentView) => controller.connectRelayEnvironment(entry.environment),
|
||||
[controller],
|
||||
);
|
||||
|
||||
const handleDisconnectCloudEnvironment = useCallback(
|
||||
(environmentId: EnvironmentId) => controller.removeEnvironment(environmentId),
|
||||
[controller],
|
||||
);
|
||||
|
||||
const handleToggleCloudError = useCallback((environmentId: string) => {
|
||||
setExpandedErrorId((current) => (current === environmentId ? null : environmentId));
|
||||
}, []);
|
||||
|
||||
if (!isSignedIn) return null;
|
||||
|
||||
return (
|
||||
<View collapsable={false} className="mt-5 gap-3">
|
||||
<View className="flex-row items-center justify-between px-1">
|
||||
<Text className="text-sm font-t3-bold uppercase text-foreground-muted">T3 Cloud</Text>
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
disabled={controller.relayDiscovery.isRefreshing}
|
||||
onPress={() => {
|
||||
void controller.refreshRelayEnvironments();
|
||||
}}
|
||||
className="h-9 w-9 items-center justify-center rounded-full bg-subtle active:opacity-70 disabled:opacity-50"
|
||||
>
|
||||
{controller.relayDiscovery.isRefreshing ? (
|
||||
<ActivityIndicator color={iconColor} size="small" />
|
||||
) : (
|
||||
<SymbolView name="arrow.clockwise" size={14} tintColor={iconColor} type="monochrome" />
|
||||
)}
|
||||
</Pressable>
|
||||
</View>
|
||||
|
||||
{hasCloudRows ? (
|
||||
<View collapsable={false} className="overflow-hidden rounded-[24px] bg-card">
|
||||
{props.connectedCloudEnvironments.map((environment, index) => (
|
||||
<ConnectedCloudEnvironmentRow
|
||||
key={environment.environmentId}
|
||||
environment={environment}
|
||||
borderTop={index !== 0}
|
||||
onConnect={() => props.onReconnectEnvironment(environment.environmentId)}
|
||||
onDisconnect={() => handleDisconnectCloudEnvironment(environment.environmentId)}
|
||||
errorExpanded={expandedErrorId === environment.environmentId}
|
||||
onToggleError={() => handleToggleCloudError(environment.environmentId)}
|
||||
/>
|
||||
))}
|
||||
{availableCloudEnvironments.map((environment, index) => (
|
||||
<CloudEnvironmentRow
|
||||
key={environment.environment.environmentId}
|
||||
environment={environment}
|
||||
borderTop={props.connectedCloudEnvironments.length > 0 || index !== 0}
|
||||
onConnect={() => handleConnectCloudEnvironment(environment)}
|
||||
errorExpanded={expandedErrorId === environment.environment.environmentId}
|
||||
onToggleError={() => handleToggleCloudError(environment.environment.environmentId)}
|
||||
/>
|
||||
))}
|
||||
</View>
|
||||
) : controller.relayDiscovery.isRefreshing ? (
|
||||
<View collapsable={false} className="items-center gap-3 rounded-[24px] bg-card p-6">
|
||||
<ActivityIndicator color={iconColor} />
|
||||
<Text className="text-center text-sm leading-normal text-foreground-muted">
|
||||
Loading linked cloud environments.
|
||||
</Text>
|
||||
</View>
|
||||
) : controller.relayDiscovery.error ? (
|
||||
<View collapsable={false} className="gap-3 rounded-[24px] bg-card p-5">
|
||||
<Text className="text-base font-t3-bold text-foreground">
|
||||
Could not load T3 Cloud environments
|
||||
</Text>
|
||||
<Text className="text-sm text-foreground-muted">{controller.relayDiscovery.error}</Text>
|
||||
{controller.relayDiscovery.errorTraceId ? (
|
||||
<CopyTraceIdButton traceId={controller.relayDiscovery.errorTraceId} />
|
||||
) : null}
|
||||
</View>
|
||||
) : (
|
||||
<View collapsable={false} className="rounded-[24px] bg-card p-5">
|
||||
<Text className="text-sm leading-normal text-foreground-muted">
|
||||
No additional linked cloud environments.
|
||||
</Text>
|
||||
</View>
|
||||
)}
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
function ConnectedCloudEnvironmentRow(props: {
|
||||
readonly environment: ConnectedEnvironmentSummary;
|
||||
readonly borderTop: boolean;
|
||||
readonly errorExpanded: boolean;
|
||||
readonly onConnect: () => void;
|
||||
readonly onDisconnect: () => void;
|
||||
readonly onToggleError: () => void;
|
||||
}) {
|
||||
return (
|
||||
<CloudEnvironmentRowShell
|
||||
borderTop={props.borderTop}
|
||||
connectionError={props.environment.connectionError}
|
||||
connectionErrorTraceId={props.environment.connectionErrorTraceId}
|
||||
connectionState={props.environment.connectionState}
|
||||
errorExpanded={props.errorExpanded}
|
||||
label={props.environment.environmentLabel}
|
||||
onValueChange={(enabled) => {
|
||||
if (enabled) {
|
||||
props.onConnect();
|
||||
return;
|
||||
}
|
||||
props.onDisconnect();
|
||||
}}
|
||||
onToggleError={props.onToggleError}
|
||||
value={props.environment.connectionState !== "available"}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function CloudEnvironmentRow(props: {
|
||||
readonly environment: RelayEnvironmentView;
|
||||
readonly borderTop: boolean;
|
||||
readonly errorExpanded: boolean;
|
||||
readonly onConnect: () => void;
|
||||
readonly onToggleError: () => void;
|
||||
}) {
|
||||
const presentation = availableCloudEnvironmentPresentation({
|
||||
isStatusPending: props.environment.availability === "checking",
|
||||
status: props.environment.status,
|
||||
statusError: props.environment.error,
|
||||
statusErrorTraceId: props.environment.traceId,
|
||||
});
|
||||
|
||||
return (
|
||||
<CloudEnvironmentRowShell
|
||||
borderTop={props.borderTop}
|
||||
connectionError={presentation.connectionError}
|
||||
connectionErrorTraceId={presentation.connectionErrorTraceId}
|
||||
connectionState={presentation.connectionState}
|
||||
errorExpanded={props.errorExpanded}
|
||||
label={props.environment.environment.label}
|
||||
onValueChange={(enabled) => {
|
||||
if (enabled) {
|
||||
props.onConnect();
|
||||
}
|
||||
}}
|
||||
onToggleError={props.onToggleError}
|
||||
statusText={presentation.statusText}
|
||||
value={false}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function CloudEnvironmentRowShell(props: {
|
||||
readonly borderTop: boolean;
|
||||
readonly connectionError: string | null;
|
||||
readonly connectionErrorTraceId: string | null;
|
||||
readonly connectionState: EnvironmentConnectionPhase;
|
||||
readonly disabled?: boolean;
|
||||
readonly errorExpanded: boolean;
|
||||
readonly label: string;
|
||||
readonly onToggleError: () => void;
|
||||
readonly onValueChange: (enabled: boolean) => void;
|
||||
readonly statusText?: string;
|
||||
readonly value: boolean;
|
||||
}) {
|
||||
const activeTrack = String(useThemeColor("--color-switch-active"));
|
||||
const track = String(useThemeColor("--color-secondary-border"));
|
||||
const chevron = useThemeColor("--color-chevron");
|
||||
const isRetrying =
|
||||
props.connectionState === "connecting" || props.connectionState === "reconnecting";
|
||||
const shouldPulse = isRetrying;
|
||||
const statusText =
|
||||
props.statusText ??
|
||||
connectionStatusText({
|
||||
phase: props.connectionState,
|
||||
error: props.connectionError,
|
||||
traceId: props.connectionErrorTraceId,
|
||||
});
|
||||
const statusClassName = props.connectionError
|
||||
? "text-rose-500 dark:text-rose-400"
|
||||
: "text-foreground-muted";
|
||||
const [errorMeasurement, setErrorMeasurement] = useState<{
|
||||
readonly text: string;
|
||||
readonly lineCount: number;
|
||||
} | null>(null);
|
||||
const errorTraceId = props.connectionErrorTraceId;
|
||||
const measuredErrorText = errorTraceId ? `${statusText} Trace ID: ${errorTraceId}` : statusText;
|
||||
const errorLineCount =
|
||||
errorMeasurement?.text === measuredErrorText ? errorMeasurement.lineCount : 0;
|
||||
const errorCanExpand = props.connectionError !== null && errorLineCount > 1;
|
||||
const isErrorExpanded = errorCanExpand && props.errorExpanded;
|
||||
const StatusContainer = errorCanExpand ? Pressable : View;
|
||||
const onMeasuredErrorTextLayout = useCallback(
|
||||
(event: NativeSyntheticEvent<TextLayoutEventData>) => {
|
||||
if (!props.connectionError) {
|
||||
return;
|
||||
}
|
||||
const nextLineCount = event.nativeEvent.lines.length;
|
||||
setErrorMeasurement((currentMeasurement) =>
|
||||
currentMeasurement?.text === measuredErrorText &&
|
||||
currentMeasurement.lineCount === nextLineCount
|
||||
? currentMeasurement
|
||||
: { text: measuredErrorText, lineCount: nextLineCount },
|
||||
);
|
||||
},
|
||||
[measuredErrorText, props.connectionError],
|
||||
);
|
||||
return (
|
||||
<View
|
||||
collapsable={false}
|
||||
className={cn(
|
||||
"flex-row items-center gap-3 bg-card px-4 py-3.5",
|
||||
props.borderTop && "border-t border-border",
|
||||
)}
|
||||
>
|
||||
<View className="min-w-0 flex-1 gap-0.5">
|
||||
<View className="min-w-0 flex-row items-center gap-2">
|
||||
<ConnectionStatusDot state={props.connectionState} pulse={shouldPulse} size={7} />
|
||||
<Text
|
||||
className="min-w-0 flex-shrink text-base font-t3-bold leading-snug text-foreground"
|
||||
numberOfLines={1}
|
||||
>
|
||||
{props.label}
|
||||
</Text>
|
||||
</View>
|
||||
{props.connectionError ? (
|
||||
<Text
|
||||
aria-hidden
|
||||
className={cn("absolute left-0 right-0 text-xs", statusClassName)}
|
||||
onTextLayout={onMeasuredErrorTextLayout}
|
||||
style={{ opacity: 0, zIndex: -1 }}
|
||||
>
|
||||
{measuredErrorText}
|
||||
</Text>
|
||||
) : null}
|
||||
<StatusContainer
|
||||
{...(errorCanExpand
|
||||
? { accessibilityRole: "button" as const, onPress: props.onToggleError }
|
||||
: {})}
|
||||
className="min-w-0 flex-row items-start gap-1"
|
||||
>
|
||||
<Text
|
||||
className={cn("min-w-0 flex-1 text-xs", statusClassName)}
|
||||
numberOfLines={isErrorExpanded ? undefined : 1}
|
||||
>
|
||||
{statusText}
|
||||
{errorTraceId ? (
|
||||
<>
|
||||
{" Trace ID: "}
|
||||
<Text
|
||||
accessibilityHint="Copies the trace ID"
|
||||
accessibilityRole="button"
|
||||
className={cn("text-xs underline", statusClassName)}
|
||||
onLongPress={(event) => {
|
||||
event.stopPropagation();
|
||||
copyTextWithHaptic(errorTraceId, { target: "connection-trace-id" });
|
||||
}}
|
||||
onPress={(event) => {
|
||||
event.stopPropagation();
|
||||
}}
|
||||
style={{ textDecorationStyle: "dotted" }}
|
||||
>
|
||||
{errorTraceId}
|
||||
</Text>
|
||||
</>
|
||||
) : null}
|
||||
</Text>
|
||||
{errorCanExpand ? (
|
||||
<SymbolView
|
||||
name="chevron.down"
|
||||
size={10}
|
||||
tintColor={chevron}
|
||||
type="monochrome"
|
||||
style={{
|
||||
marginTop: 3,
|
||||
transform: [{ rotate: isErrorExpanded ? "180deg" : "0deg" }],
|
||||
}}
|
||||
/>
|
||||
) : null}
|
||||
</StatusContainer>
|
||||
</View>
|
||||
<Switch
|
||||
disabled={props.disabled}
|
||||
ios_backgroundColor={track}
|
||||
onValueChange={props.onValueChange}
|
||||
trackColor={{ false: track, true: activeTrack }}
|
||||
value={props.value}
|
||||
/>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
function CopyTraceIdButton(props: { readonly traceId: string }) {
|
||||
const iconColor = useThemeColor("--color-icon");
|
||||
|
||||
return (
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
onPress={() => {
|
||||
copyTextWithHaptic(props.traceId, { target: "connection-trace-id" });
|
||||
}}
|
||||
className="self-start flex-row items-center gap-1.5 rounded-full bg-subtle px-3 py-2 active:opacity-70"
|
||||
>
|
||||
<SymbolView name="doc.on.doc" size={12} tintColor={iconColor} type="monochrome" />
|
||||
<Text className="text-xs font-t3-bold text-foreground">Copy trace ID</Text>
|
||||
</Pressable>
|
||||
);
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import { useNavigation } from "@react-navigation/native";
|
||||
import { NativeStackScreenOptions } from "../../native/StackHeader";
|
||||
import { SymbolView } from "expo-symbols";
|
||||
import * as Effect from "effect/Effect";
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { useCallback, useEffect, useMemo, useState, useSyncExternalStore } from "react";
|
||||
import { Alert, Linking, Platform, ScrollView, View } from "react-native";
|
||||
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
||||
|
||||
@@ -20,7 +20,11 @@ import {
|
||||
import { AppText as Text } from "../../components/AppText";
|
||||
import { setLiveActivityUpdatesEnabled } from "../agent-awareness/liveActivityPreferences";
|
||||
import { requestAgentNotificationPermission } from "../agent-awareness/notificationPermissions";
|
||||
import { refreshAgentAwarenessRegistration } from "../agent-awareness/remoteRegistration";
|
||||
import {
|
||||
getAgentAwarenessRegistrationStatus,
|
||||
refreshAgentAwarenessRegistration,
|
||||
subscribeAgentAwarenessRegistrationStatus,
|
||||
} from "../agent-awareness/remoteRegistration";
|
||||
import { refreshManagedRelayEnvironments } from "../cloud/managedRelayState";
|
||||
import { useClerkSettingsSheetDetent } from "../cloud/ClerkSettingsSheetDetent";
|
||||
import { hasCloudPublicConfig, resolveRelayClerkTokenOptions } from "../cloud/publicConfig";
|
||||
@@ -37,6 +41,19 @@ import { SettingsSwitchRow } from "./components/SettingsSwitchRow";
|
||||
type NotificationStatus = "checking" | "enabled" | "disabled" | "unsupported";
|
||||
type LiveActivityStatus = "checking" | "enabled" | "disabled" | "signed-out" | "linking";
|
||||
|
||||
// Reflects whether the relay actually accepted this device's registration.
|
||||
// The notification and Live Activity switches are gated on this so they can
|
||||
// never read as enabled when the device cannot receive anything (e.g. the
|
||||
// registration request timed out).
|
||||
function useDeviceRegistered(): boolean {
|
||||
const status = useSyncExternalStore(
|
||||
subscribeAgentAwarenessRegistrationStatus,
|
||||
getAgentAwarenessRegistrationStatus,
|
||||
() => "unknown" as const,
|
||||
);
|
||||
return status === "registered";
|
||||
}
|
||||
|
||||
export function SettingsRouteScreen() {
|
||||
const navigation = useNavigation();
|
||||
|
||||
@@ -113,6 +130,7 @@ function ConfiguredSettingsRouteScreen() {
|
||||
const { savedConnectionsById } = useSavedRemoteConnections();
|
||||
const [notificationStatus, setNotificationStatus] = useState<NotificationStatus>("checking");
|
||||
const [liveActivityStatus, setLiveActivityStatus] = useState<LiveActivityStatus>("checking");
|
||||
const deviceRegistered = useDeviceRegistered();
|
||||
|
||||
const connections = useMemo(() => Object.values(savedConnectionsById), [savedConnectionsById]);
|
||||
const environmentCount = connections.length;
|
||||
@@ -182,10 +200,19 @@ function ConfiguredSettingsRouteScreen() {
|
||||
}
|
||||
if (result.value.type === "granted") {
|
||||
setNotificationStatus("enabled");
|
||||
Alert.alert(
|
||||
"Notifications enabled",
|
||||
"Live Activity notifications are enabled for this device.",
|
||||
);
|
||||
// Permission alone is not enough: the switch stays off until the relay
|
||||
// registration succeeds, so tell the user the truth about which happened.
|
||||
if (getAgentAwarenessRegistrationStatus() === "registered") {
|
||||
Alert.alert(
|
||||
"Notifications enabled",
|
||||
"Live Activity notifications are enabled for this device.",
|
||||
);
|
||||
} else {
|
||||
Alert.alert(
|
||||
"Couldn't finish enabling notifications",
|
||||
"Notification access was granted, but this device could not be registered with T3 Connect. Notifications will start once registration succeeds.",
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (result.value.type === "unsupported") {
|
||||
@@ -213,8 +240,8 @@ function ConfiguredSettingsRouteScreen() {
|
||||
|
||||
const promptSignIn = useCallback(() => {
|
||||
Alert.alert(
|
||||
"Request T3 Cloud access",
|
||||
"Live Activity updates require approved T3 Cloud access so relay can deliver updates to this device.",
|
||||
"Request T3 Connect access",
|
||||
"Live Activity updates require approved T3 Connect access so relay can deliver updates to this device.",
|
||||
[
|
||||
{ text: "Cancel", style: "cancel" },
|
||||
{
|
||||
@@ -271,12 +298,22 @@ function ConfiguredSettingsRouteScreen() {
|
||||
|
||||
refreshManagedRelayEnvironments();
|
||||
setLiveActivityStatus("enabled");
|
||||
Alert.alert(
|
||||
"Live Activities enabled",
|
||||
environmentCount > 0
|
||||
? `${environmentCount} environment${environmentCount === 1 ? "" : "s"} linked for Live Activity updates.`
|
||||
: "Live Activity updates are enabled. Add an environment to start receiving updates.",
|
||||
);
|
||||
// The environment link can succeed while this device's own registration
|
||||
// (the push-to-start token the relay needs) has not — don't claim Live
|
||||
// Activities are live until the device is actually registered.
|
||||
if (getAgentAwarenessRegistrationStatus() === "registered") {
|
||||
Alert.alert(
|
||||
"Live Activities enabled",
|
||||
environmentCount > 0
|
||||
? `${environmentCount} environment${environmentCount === 1 ? "" : "s"} linked for Live Activity updates.`
|
||||
: "Live Activity updates are enabled. Add an environment to start receiving updates.",
|
||||
);
|
||||
} else {
|
||||
Alert.alert(
|
||||
"Couldn't finish enabling Live Activities",
|
||||
"This device could not be registered with T3 Connect, so Live Activities won't appear yet. They'll start once registration succeeds.",
|
||||
);
|
||||
}
|
||||
}, [connections, environmentCount, getToken, isSignedIn, promptSignIn]);
|
||||
|
||||
const handleDeviceNotificationsChange = useCallback(
|
||||
@@ -395,7 +432,10 @@ function ConfiguredSettingsRouteScreen() {
|
||||
icon="bell.badge"
|
||||
label="Device Notifications"
|
||||
disabled={notificationStatus === "checking" || notificationStatus === "unsupported"}
|
||||
value={notificationStatus === "enabled"}
|
||||
// Only reads as on when this device is actually registered with the
|
||||
// relay; otherwise notifications cannot be delivered regardless of
|
||||
// the local iOS permission.
|
||||
value={notificationStatus === "enabled" && deviceRegistered}
|
||||
onValueChange={handleDeviceNotificationsChange}
|
||||
/>
|
||||
<SettingsSwitchRow
|
||||
@@ -404,7 +444,12 @@ function ConfiguredSettingsRouteScreen() {
|
||||
}
|
||||
icon="bolt.circle"
|
||||
label="Live Activity Updates"
|
||||
value={liveActivityStatus === "enabled" || liveActivityStatus === "linking"}
|
||||
// Same gate: a saved preference is meaningless until the device
|
||||
// registration the relay needs to push updates has succeeded.
|
||||
value={
|
||||
(liveActivityStatus === "enabled" || liveActivityStatus === "linking") &&
|
||||
deviceRegistered
|
||||
}
|
||||
onValueChange={handleLiveActivitiesChange}
|
||||
/>
|
||||
</SettingsSection>
|
||||
|
||||
@@ -34,6 +34,8 @@ import {
|
||||
import { useScaledTextRole } from "../settings/appearance/useScaledTextRole";
|
||||
import { getComposerDraftSnapshot } from "../../state/use-composer-drafts";
|
||||
import { useProjects } from "../../state/entities";
|
||||
import { deriveThreadTitleFromPrompt } from "../../lib/projectThreadStartTurn";
|
||||
import { armAgentAwarenessLiveActivityForLocalWork } from "../agent-awareness/remoteRegistration";
|
||||
import { enqueueThreadOutboxMessage, removeThreadOutboxMessage } from "../../state/thread-outbox";
|
||||
import { useRemoteConnectionStatus } from "../../state/use-remote-environment-registry";
|
||||
import { branchBadgeLabel, useNewTaskFlow } from "./new-task-flow-provider";
|
||||
@@ -524,6 +526,14 @@ export function NewTaskDraftScreen(props: {
|
||||
}
|
||||
|
||||
flow.setSubmitting(true);
|
||||
// Arm the lock-screen card before the async thread creation: backgrounding
|
||||
// the app right after tapping submit would otherwise reject the foreground
|
||||
// -only Activity start. If creation fails, the token registration's replay
|
||||
// finds no work and ends the card within seconds.
|
||||
armAgentAwarenessLiveActivityForLocalWork({
|
||||
threadTitle: deriveThreadTitleFromPrompt(initialMessageText),
|
||||
projectTitle: selectedProject.title,
|
||||
});
|
||||
const result = await createProjectThread({
|
||||
project: selectedProject,
|
||||
modelSelection,
|
||||
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
import ImageViewing from "react-native-image-viewing";
|
||||
import Animated, { FadeIn, FadeOut, LinearTransition } from "react-native-reanimated";
|
||||
import { useThemeColor } from "../../lib/useThemeColor";
|
||||
import { armAgentAwarenessLiveActivityForLocalWork } from "../agent-awareness/remoteRegistration";
|
||||
import { scopedThreadKey } from "../../lib/scopedEntities";
|
||||
|
||||
import { AppText as Text } from "../../components/AppText";
|
||||
@@ -499,12 +500,24 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
|
||||
const threadKey = scopedThreadKey(props.environmentId, props.selectedThread.id);
|
||||
if (inFlightThreadIdsRef.current.has(threadKey)) return;
|
||||
inFlightThreadIdsRef.current.add(threadKey);
|
||||
// Sending a prompt starts agent work: arm the lock-screen card now, while
|
||||
// the app is foregrounded and the activity token can be registered.
|
||||
armAgentAwarenessLiveActivityForLocalWork({
|
||||
threadTitle: props.selectedThread.title,
|
||||
projectTitle: props.environmentLabel ?? "T3 Code",
|
||||
});
|
||||
try {
|
||||
await onSendMessage();
|
||||
} finally {
|
||||
inFlightThreadIdsRef.current.delete(threadKey);
|
||||
}
|
||||
}, [onSendMessage, props.environmentId, props.selectedThread.id]);
|
||||
}, [
|
||||
onSendMessage,
|
||||
props.environmentId,
|
||||
props.environmentLabel,
|
||||
props.selectedThread.id,
|
||||
props.selectedThread.title,
|
||||
]);
|
||||
const handleCommandSelect = useCallback(
|
||||
(item: ComposerCommandItem) => {
|
||||
if (!composerTrigger) return;
|
||||
|
||||
@@ -13,10 +13,12 @@ import {
|
||||
const CONNECTIONS_KEY = "t3code.connections";
|
||||
const PREFERENCES_KEY = "t3code.preferences";
|
||||
const AGENT_AWARENESS_DEVICE_ID_KEY = "t3code.agent-awareness.device-id";
|
||||
const AGENT_AWARENESS_REGISTRATION_KEY = "t3code.agent-awareness.registration";
|
||||
const MobileStorageKey = Schema.Literals([
|
||||
CONNECTIONS_KEY,
|
||||
PREFERENCES_KEY,
|
||||
AGENT_AWARENESS_DEVICE_ID_KEY,
|
||||
AGENT_AWARENESS_REGISTRATION_KEY,
|
||||
]);
|
||||
type MobileStorageKeyValue = typeof MobileStorageKey.Type;
|
||||
|
||||
@@ -67,6 +69,8 @@ export interface Preferences {
|
||||
/** Code/diff font size override; null/absent means derived from baseFontSize. */
|
||||
readonly codeFontSize?: number | null;
|
||||
readonly codeWordBreak?: boolean;
|
||||
/** Cloud account ids that opted out of the T3 Connect onboarding sheet. */
|
||||
readonly connectOnboardingOptOutAccounts?: ReadonlyArray<string>;
|
||||
}
|
||||
|
||||
async function readStorageItem(key: MobileStorageKeyValue): Promise<string | null> {
|
||||
@@ -165,6 +169,7 @@ export async function loadPreferences(): Promise<Preferences> {
|
||||
markdownFontSize?: number;
|
||||
codeFontSize?: number | null;
|
||||
codeWordBreak?: boolean;
|
||||
connectOnboardingOptOutAccounts?: ReadonlyArray<string>;
|
||||
} = {};
|
||||
|
||||
if (typeof parsed.liveActivitiesEnabled === "boolean") {
|
||||
@@ -185,18 +190,37 @@ export async function loadPreferences(): Promise<Preferences> {
|
||||
if (typeof parsed.codeWordBreak === "boolean") {
|
||||
preferences.codeWordBreak = parsed.codeWordBreak;
|
||||
}
|
||||
if (Array.isArray(parsed.connectOnboardingOptOutAccounts)) {
|
||||
preferences.connectOnboardingOptOutAccounts = parsed.connectOnboardingOptOutAccounts.filter(
|
||||
(account): account is string => typeof account === "string",
|
||||
);
|
||||
}
|
||||
|
||||
return preferences;
|
||||
}
|
||||
|
||||
// Preference writes are read-modify-write over one JSON blob; concurrent
|
||||
// writers would drop each other's fields, so all writes are serialized here.
|
||||
let preferencesWriteQueue: Promise<unknown> = Promise.resolve();
|
||||
|
||||
export async function updatePreferences(
|
||||
update: (current: Preferences) => Partial<Preferences>,
|
||||
): Promise<Preferences> {
|
||||
const task = preferencesWriteQueue.then(async () => {
|
||||
const current = await loadPreferences();
|
||||
const next: Preferences = {
|
||||
...current,
|
||||
...update(current),
|
||||
};
|
||||
await writeJsonStorageItem(PREFERENCES_KEY, next);
|
||||
return next;
|
||||
});
|
||||
preferencesWriteQueue = task.catch(() => undefined);
|
||||
return task;
|
||||
}
|
||||
|
||||
export async function savePreferencesPatch(patch: Partial<Preferences>): Promise<Preferences> {
|
||||
const current = await loadPreferences();
|
||||
const next: Preferences = {
|
||||
...current,
|
||||
...patch,
|
||||
};
|
||||
await writeJsonStorageItem(PREFERENCES_KEY, next);
|
||||
return next;
|
||||
return updatePreferences(() => patch);
|
||||
}
|
||||
|
||||
export async function loadOrCreateAgentAwarenessDeviceId(): Promise<string> {
|
||||
@@ -222,3 +246,46 @@ export async function loadAgentAwarenessDeviceId(): Promise<string | null> {
|
||||
const existing = await readStorageItem(AGENT_AWARENESS_DEVICE_ID_KEY);
|
||||
return existing?.trim() ? existing : null;
|
||||
}
|
||||
|
||||
export interface AgentAwarenessRegistrationRecord {
|
||||
readonly identity: string;
|
||||
readonly signature: string;
|
||||
// Last push-to-start token the relay accepted. Registrations triggered
|
||||
// without a token event merge it back in so token absence never reads as a
|
||||
// change (which would defeat the register-once skip every launch).
|
||||
readonly pushToStartToken?: string;
|
||||
}
|
||||
|
||||
// Remembers the account identity and payload signature the relay last accepted
|
||||
// so the app does not re-register on every launch while nothing has changed.
|
||||
// Cleared only on sign-out.
|
||||
export async function loadAgentAwarenessRegistrationRecord(): Promise<AgentAwarenessRegistrationRecord | null> {
|
||||
const parsed = await readJsonStorageItem<AgentAwarenessRegistrationRecord>(
|
||||
AGENT_AWARENESS_REGISTRATION_KEY,
|
||||
);
|
||||
if (
|
||||
!parsed ||
|
||||
typeof parsed !== "object" ||
|
||||
typeof parsed.identity !== "string" ||
|
||||
typeof parsed.signature !== "string"
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
identity: parsed.identity,
|
||||
signature: parsed.signature,
|
||||
...(typeof parsed.pushToStartToken === "string" && parsed.pushToStartToken
|
||||
? { pushToStartToken: parsed.pushToStartToken }
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
|
||||
export async function saveAgentAwarenessRegistrationRecord(
|
||||
record: AgentAwarenessRegistrationRecord,
|
||||
): Promise<void> {
|
||||
await writeJsonStorageItem(AGENT_AWARENESS_REGISTRATION_KEY, record);
|
||||
}
|
||||
|
||||
export async function clearAgentAwarenessRegistrationRecord(): Promise<void> {
|
||||
await writeStorageItem(AGENT_AWARENESS_REGISTRATION_KEY, "");
|
||||
}
|
||||
@@ -2,23 +2,48 @@ import { describe, expect, it, vi } from "vite-plus/test";
|
||||
|
||||
vi.mock("@expo/ui/swift-ui", () => ({
|
||||
HStack: "HStack",
|
||||
Image: "Image",
|
||||
Spacer: "Spacer",
|
||||
Text: "Text",
|
||||
VStack: "VStack",
|
||||
ZStack: "ZStack",
|
||||
}));
|
||||
|
||||
vi.mock("@expo/ui/swift-ui/modifiers", () => ({
|
||||
font: (value: unknown) => value,
|
||||
foregroundStyle: (value: unknown) => value,
|
||||
frame: (value: unknown) => value,
|
||||
layoutPriority: (value: unknown) => value,
|
||||
lineLimit: (value: unknown) => value,
|
||||
padding: (value: unknown) => value,
|
||||
resizable: (value: unknown) => value,
|
||||
widgetURL: (value: unknown) => ({ widgetURL: value }),
|
||||
}));
|
||||
|
||||
vi.mock("expo-widgets", () => ({
|
||||
createLiveActivity: vi.fn((name: string, layout: unknown) => ({ layout, name })),
|
||||
}));
|
||||
|
||||
import { AgentActivity, type AgentActivityProps } from "./AgentActivity";
|
||||
import {
|
||||
AgentActivity,
|
||||
type AgentActivityProps,
|
||||
type AgentActivityRowProps,
|
||||
} from "./AgentActivity";
|
||||
|
||||
function makeRow(overrides: Partial<AgentActivityRowProps>): AgentActivityRowProps {
|
||||
return {
|
||||
environmentId: "env-1",
|
||||
threadId: "thread-1",
|
||||
projectTitle: "Project",
|
||||
threadTitle: "Thread",
|
||||
modelTitle: "gpt-5.4",
|
||||
phase: "running",
|
||||
status: "Working",
|
||||
updatedAt: "2026-05-25T13:07:00.000Z",
|
||||
deepLink: "/threads/env-1/thread-1",
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
const props = {
|
||||
title: "T3 Code",
|
||||
@@ -33,17 +58,226 @@ const environment = {
|
||||
isLuminanceReduced: false,
|
||||
} as const;
|
||||
|
||||
const lightEnvironment = {
|
||||
colorScheme: "light",
|
||||
isLuminanceReduced: false,
|
||||
} as const;
|
||||
|
||||
describe("AgentActivity widget layout", () => {
|
||||
it("formats its updated-at label without app-runtime helper references", () => {
|
||||
expect(JSON.stringify(AgentActivity(props, environment as never))).toContain(
|
||||
'"children":["Updated ","1:07"]',
|
||||
it("tints each row by its own phase using the web sidebar's dark palette", () => {
|
||||
const layout = AgentActivity(
|
||||
{
|
||||
...props,
|
||||
activeCount: 2,
|
||||
activities: [
|
||||
makeRow({}),
|
||||
makeRow({ threadId: "thread-2", phase: "waiting_for_approval", status: "Approval" }),
|
||||
],
|
||||
},
|
||||
environment as never,
|
||||
);
|
||||
expect(AgentActivity.toString()).not.toContain("formatAgentActivityUpdatedAtLabel");
|
||||
const banner = JSON.stringify(layout.banner);
|
||||
expect(banner).toContain("#7dd3fc"); // sky-300: running
|
||||
expect(banner).toContain("#fcd34d"); // amber-300: waiting_for_approval
|
||||
});
|
||||
|
||||
it("uses now when the updated-at timestamp is malformed", () => {
|
||||
it("switches to the web sidebar's light palette when the scheme is light", () => {
|
||||
// macOS (iPhone Mirroring / Mac notification center) renders the activity
|
||||
// on a light background; the dark-material palette is illegible there.
|
||||
const layout = AgentActivity(
|
||||
{
|
||||
...props,
|
||||
activeCount: 2,
|
||||
activities: [
|
||||
makeRow({}),
|
||||
makeRow({ threadId: "thread-2", phase: "waiting_for_approval", status: "Approval" }),
|
||||
],
|
||||
},
|
||||
lightEnvironment as never,
|
||||
);
|
||||
const banner = JSON.stringify(layout.banner);
|
||||
expect(banner).toContain("#0284c7"); // sky-600: running
|
||||
expect(banner).toContain("#d97706"); // amber-600: waiting_for_approval
|
||||
expect(banner).not.toContain("#7dd3fc");
|
||||
expect(banner).not.toContain("#fcd34d");
|
||||
});
|
||||
|
||||
it("orders rows attention-first in the banner", () => {
|
||||
const layout = AgentActivity(
|
||||
{
|
||||
...props,
|
||||
activeCount: 2,
|
||||
activities: [
|
||||
makeRow({ threadTitle: "Working thread" }),
|
||||
makeRow({
|
||||
threadId: "thread-2",
|
||||
threadTitle: "Blocked thread",
|
||||
phase: "waiting_for_approval",
|
||||
status: "Approval",
|
||||
}),
|
||||
],
|
||||
},
|
||||
environment as never,
|
||||
);
|
||||
const banner = JSON.stringify(layout.banner);
|
||||
expect(banner.indexOf("Blocked thread")).toBeGreaterThan(-1);
|
||||
expect(banner.indexOf("Blocked thread")).toBeLessThan(banner.indexOf("Working thread"));
|
||||
});
|
||||
|
||||
it("summarizes the attention count in the banner header", () => {
|
||||
const layout = AgentActivity(
|
||||
{
|
||||
...props,
|
||||
activeCount: 3,
|
||||
activities: [
|
||||
makeRow({}),
|
||||
makeRow({ threadId: "thread-2", phase: "waiting_for_input", status: "Input" }),
|
||||
],
|
||||
},
|
||||
environment as never,
|
||||
);
|
||||
const banner = JSON.stringify(layout.banner);
|
||||
expect(banner).toContain("3 active agents");
|
||||
expect(banner).toContain("1 needs attention");
|
||||
});
|
||||
|
||||
it("uses the attention tint for the compact presentations when a row needs input", () => {
|
||||
const layout = AgentActivity(
|
||||
{
|
||||
...props,
|
||||
activeCount: 2,
|
||||
activities: [
|
||||
makeRow({}),
|
||||
makeRow({ threadId: "thread-2", phase: "waiting_for_input", status: "Input" }),
|
||||
],
|
||||
},
|
||||
environment as never,
|
||||
);
|
||||
expect(JSON.stringify(layout.compactLeading)).toContain("#a5b4fc"); // indigo-300
|
||||
expect(JSON.stringify(layout.compactTrailing)).toContain("Input");
|
||||
expect(JSON.stringify(layout.minimal)).toContain("#a5b4fc");
|
||||
});
|
||||
|
||||
it("deep links the banner to the row that needs attention", () => {
|
||||
const layout = AgentActivity(
|
||||
{
|
||||
...props,
|
||||
activeCount: 2,
|
||||
activities: [
|
||||
makeRow({}),
|
||||
makeRow({
|
||||
threadId: "thread-2",
|
||||
phase: "waiting_for_approval",
|
||||
status: "Approval",
|
||||
deepLink: "/threads/env-1/thread-2",
|
||||
}),
|
||||
],
|
||||
},
|
||||
environment as never,
|
||||
);
|
||||
expect(JSON.stringify(layout.banner)).toContain(
|
||||
'"widgetURL":"t3code://threads/env-1/thread-2"',
|
||||
);
|
||||
});
|
||||
|
||||
it("deep links the banner to the first row when nothing needs attention", () => {
|
||||
const layout = AgentActivity({ ...props, activities: [makeRow({})] }, environment as never);
|
||||
expect(JSON.stringify(layout.banner)).toContain(
|
||||
'"widgetURL":"t3code://threads/env-1/thread-1"',
|
||||
);
|
||||
});
|
||||
|
||||
it("omits the deep link for unsafe paths and empty aggregates", () => {
|
||||
expect(JSON.stringify(AgentActivity(props, environment as never))).not.toContain("widgetURL");
|
||||
expect(
|
||||
JSON.stringify(AgentActivity({ ...props, updatedAt: "not-a-date" }, environment as never)),
|
||||
).toContain('"children":["Updated ","now"]');
|
||||
JSON.stringify(
|
||||
AgentActivity(
|
||||
{ ...props, activities: [makeRow({ deepLink: "//evil.example" })] },
|
||||
environment as never,
|
||||
),
|
||||
),
|
||||
).not.toContain("widgetURL");
|
||||
});
|
||||
|
||||
it("leads with the outcome instead of a zero count when nothing is active", () => {
|
||||
const layout = AgentActivity(
|
||||
{
|
||||
...props,
|
||||
subtitle: "Agent work completed",
|
||||
activeCount: 0,
|
||||
activities: [makeRow({ phase: "completed", status: "Done" })],
|
||||
},
|
||||
environment as never,
|
||||
);
|
||||
const banner = JSON.stringify(layout.banner);
|
||||
expect(banner).toContain("Agent work completed");
|
||||
expect(banner).not.toContain("0 active");
|
||||
expect(banner).toContain("#6ee7b7"); // emerald-300 header tint
|
||||
expect(JSON.stringify(layout.compactTrailing)).toContain("Done");
|
||||
expect(JSON.stringify(layout.compactTrailing)).not.toContain("0 active");
|
||||
expect(JSON.stringify(layout.expandedLeading)).toContain("Done");
|
||||
expect(JSON.stringify(layout.minimal)).toContain("checkmark.circle.fill");
|
||||
expect(JSON.stringify(layout.bannerSmall)).toContain("Done");
|
||||
});
|
||||
|
||||
it("reads Failed when the finished work ended in failure", () => {
|
||||
const layout = AgentActivity(
|
||||
{
|
||||
...props,
|
||||
subtitle: "Agent work failed",
|
||||
activeCount: 0,
|
||||
activities: [makeRow({ phase: "failed", status: "Failed" })],
|
||||
},
|
||||
environment as never,
|
||||
);
|
||||
const banner = JSON.stringify(layout.banner);
|
||||
expect(banner).toContain("Agent work failed");
|
||||
expect(banner).toContain("#fca5a5"); // red-300 header tint
|
||||
expect(JSON.stringify(layout.compactTrailing)).toContain("Failed");
|
||||
expect(JSON.stringify(layout.expandedLeading)).toContain("Failed");
|
||||
expect(JSON.stringify(layout.minimal)).toContain("xmark.octagon.fill");
|
||||
});
|
||||
|
||||
it("lets a failure dominate mixed finished outcomes across every presentation", () => {
|
||||
const layout = AgentActivity(
|
||||
{
|
||||
...props,
|
||||
// The server subtitle keys off the newest terminal row (completed
|
||||
// here); the layout must still read Failed everywhere so the header
|
||||
// text never disagrees with the tint, count slots, or minimal glyph.
|
||||
subtitle: "Agent work completed",
|
||||
activeCount: 0,
|
||||
activities: [
|
||||
makeRow({ phase: "completed", status: "Done" }),
|
||||
makeRow({ threadId: "thread-2", phase: "failed", status: "Failed" }),
|
||||
],
|
||||
},
|
||||
environment as never,
|
||||
);
|
||||
const banner = JSON.stringify(layout.banner);
|
||||
expect(banner).toContain("Agent work failed");
|
||||
expect(banner).not.toContain("Agent work completed");
|
||||
expect(banner).toContain("#fca5a5"); // red-300 header tint
|
||||
expect(JSON.stringify(layout.compactTrailing)).toContain("Failed");
|
||||
expect(JSON.stringify(layout.expandedLeading)).toContain("Failed");
|
||||
expect(JSON.stringify(layout.minimal)).toContain("xmark.octagon.fill");
|
||||
});
|
||||
|
||||
it("renders up to five rows in the banner", () => {
|
||||
const layout = AgentActivity(
|
||||
{
|
||||
...props,
|
||||
activeCount: 6,
|
||||
activities: [1, 2, 3, 4, 5, 6].map((n) =>
|
||||
makeRow({ threadId: `t${n}`, threadTitle: `Thread ${n}` }),
|
||||
),
|
||||
},
|
||||
environment as never,
|
||||
);
|
||||
const banner = JSON.stringify(layout.banner);
|
||||
for (const visible of [1, 2, 3, 4, 5]) {
|
||||
expect(banner).toContain(`Thread ${visible}`);
|
||||
}
|
||||
expect(banner).not.toContain("Thread 6");
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,15 @@
|
||||
import { HStack, Spacer, Text, VStack } from "@expo/ui/swift-ui";
|
||||
import { font, foregroundStyle, lineLimit, padding } from "@expo/ui/swift-ui/modifiers";
|
||||
import { HStack, Image, Spacer, Text, VStack, ZStack } from "@expo/ui/swift-ui";
|
||||
import type { ComponentProps } from "react";
|
||||
import {
|
||||
font,
|
||||
foregroundStyle,
|
||||
frame,
|
||||
layoutPriority,
|
||||
lineLimit,
|
||||
padding,
|
||||
resizable,
|
||||
widgetURL,
|
||||
} from "@expo/ui/swift-ui/modifiers";
|
||||
import {
|
||||
createLiveActivity,
|
||||
type LiveActivityComponent,
|
||||
@@ -37,282 +47,332 @@ export interface AgentActivityProps {
|
||||
readonly activities: ReadonlyArray<AgentActivityRowProps>;
|
||||
}
|
||||
|
||||
// This function is serialized into the widget extension's JS bundle, so it
|
||||
// must stay self-contained: no references to module-scope helpers, only the
|
||||
// imported view/modifier factories.
|
||||
export function AgentActivity(
|
||||
props: AgentActivityProps,
|
||||
environment: LiveActivityEnvironment,
|
||||
): LiveActivityLayout {
|
||||
"widget";
|
||||
|
||||
const row0 = props.activities[0];
|
||||
const row1 = props.activities[1];
|
||||
const row2 = props.activities[2];
|
||||
const updatedAtMatch = /^\d{4}-\d{2}-\d{2}T(\d{2}):(\d{2}):/.exec(props.updatedAt);
|
||||
const updatedAtHours24 = Number(updatedAtMatch?.[1]);
|
||||
const updatedAtMinutes = updatedAtMatch?.[2];
|
||||
const updatedAt =
|
||||
Number.isInteger(updatedAtHours24) &&
|
||||
updatedAtHours24 >= 0 &&
|
||||
updatedAtHours24 <= 23 &&
|
||||
updatedAtMinutes
|
||||
? `${updatedAtHours24 % 12 || 12}:${updatedAtMinutes}`
|
||||
: "now";
|
||||
const activeLabel = `${props.activeCount} active`;
|
||||
const isLight = environment.colorScheme === "light";
|
||||
const primaryForeground = isLight ? "#262626" : "#f5f5f5";
|
||||
const secondaryForeground = isLight ? "#525252" : "#a3a3a3";
|
||||
const mutedForeground = isLight ? "#737373" : "#8e8e93";
|
||||
const tint = environment.isLuminanceReduced
|
||||
? secondaryForeground
|
||||
: row0?.phase === "waiting_for_approval" || row0?.phase === "waiting_for_input"
|
||||
? "#f97316"
|
||||
: row0?.phase === "failed"
|
||||
? "#ef4444"
|
||||
: "#14b8a6";
|
||||
// Use SwiftUI's semantic label colors rather than fixed hex keyed off the
|
||||
// device color scheme. A Live Activity banner always renders over a dark
|
||||
// system material regardless of the device's light/dark setting, so
|
||||
// scheme-derived dark text read as unreadable dark-on-dark on the lock
|
||||
// screen. Semantic colors adapt to whatever material the OS places them on:
|
||||
// the dark LA banner and the (light or dark) home-screen widget alike.
|
||||
const primaryForeground = "primary";
|
||||
const secondaryForeground = "secondary";
|
||||
|
||||
// Status tints mirror the web sidebar's pills
|
||||
// (apps/web/src/components/Sidebar.logic.ts resolveThreadStatusPill): amber
|
||||
// for approval, indigo for input, sky for working, emerald for completed.
|
||||
// On iPhone the LA sits on a dark material, but macOS (iPhone Mirroring /
|
||||
// Mac notification center) renders it on a light one — so pick the web
|
||||
// palette's light (-600) or dark (-300) variant off the color scheme.
|
||||
const isLightScheme = environment.colorScheme === "light";
|
||||
const phaseTint = (phase: AgentActivityPhase | undefined): string => {
|
||||
if (environment.isLuminanceReduced) {
|
||||
return secondaryForeground;
|
||||
}
|
||||
switch (phase) {
|
||||
case "waiting_for_approval":
|
||||
return isLightScheme ? "#d97706" : "#fcd34d"; // amber-600 / amber-300
|
||||
case "waiting_for_input":
|
||||
return isLightScheme ? "#4f46e5" : "#a5b4fc"; // indigo-600 / indigo-300
|
||||
case "failed":
|
||||
return isLightScheme ? "#dc2626" : "#fca5a5"; // red-600 / red-300
|
||||
case "completed":
|
||||
return isLightScheme ? "#059669" : "#6ee7b7"; // emerald-600 / emerald-300
|
||||
case "starting":
|
||||
case "running":
|
||||
default:
|
||||
return isLightScheme ? "#0284c7" : "#7dd3fc"; // sky-600 / sky-300
|
||||
}
|
||||
};
|
||||
|
||||
// Order attention-first so whatever needs the user floats to the top of every
|
||||
// presentation, then failures, then in-flight work, then finished/stale.
|
||||
const phasePriority = (phase: AgentActivityPhase): number => {
|
||||
if (phase === "waiting_for_approval" || phase === "waiting_for_input") return 0;
|
||||
if (phase === "failed") return 1;
|
||||
if (phase === "running" || phase === "starting") return 2;
|
||||
return 3;
|
||||
};
|
||||
const ordered = [...props.activities].sort(
|
||||
(a, b) => phasePriority(a.phase) - phasePriority(b.phase),
|
||||
);
|
||||
const row0 = ordered[0];
|
||||
const row1 = ordered[1];
|
||||
const row2 = ordered[2];
|
||||
const row3 = ordered[3];
|
||||
const row4 = ordered[4];
|
||||
|
||||
const attentionRows = props.activities.filter(
|
||||
(row) => row.phase === "waiting_for_approval" || row.phase === "waiting_for_input",
|
||||
);
|
||||
const attentionRow = attentionRows[0];
|
||||
const failedRow = props.activities.find((row) => row.phase === "failed");
|
||||
const heroRow = attentionRow ?? failedRow ?? row0;
|
||||
const tint = phaseTint(heroRow?.phase);
|
||||
// Headline count leans on the accent when a human is actually blocked.
|
||||
const headerTint = attentionRow
|
||||
? phaseTint(attentionRow.phase)
|
||||
: failedRow
|
||||
? phaseTint(failedRow.phase)
|
||||
: tint;
|
||||
|
||||
// With nothing active the aggregate only carries recently finished work, so
|
||||
// "0 active agents" (and a lone "0" in the expanded island) read as broken.
|
||||
// Lead with the outcome instead. The outcome is derived here from the rows
|
||||
// rather than taken from the server subtitle (which keys off the newest
|
||||
// terminal row): every presentation — header text, tint, count slots,
|
||||
// minimal glyph — must agree, and a failure anywhere should dominate a
|
||||
// newer success.
|
||||
const allDone = props.activeCount === 0;
|
||||
const doneLabel = failedRow ? "Failed" : "Done";
|
||||
const outcomeLabel = failedRow ? "Agent work failed" : "Agent work completed";
|
||||
|
||||
// Header copy: "5 active agents" + (", 1 needs attention"). The banner renders
|
||||
// the two parts in-line so the attention half can carry the accent color;
|
||||
// `summary` is the short form for tight spots (expanded center, watch card).
|
||||
const agentWord = props.activeCount === 1 ? "agent" : "agents";
|
||||
const agentsLabel = allDone ? outcomeLabel : `${props.activeCount} active ${agentWord}`;
|
||||
const attentionSuffix =
|
||||
attentionRows.length > 0
|
||||
? `${attentionRows.length} need${attentionRows.length === 1 ? "s" : ""} attention`
|
||||
: "";
|
||||
const activeLabel = allDone ? doneLabel : `${props.activeCount} active`;
|
||||
const summary = attentionSuffix || activeLabel;
|
||||
|
||||
// Any registered scheme variant routes back to this app; taps are delivered
|
||||
// to the widget's containing app, so the prod scheme is safe for all builds.
|
||||
const deepLinkRow = attentionRow ?? row0;
|
||||
const deepLink =
|
||||
deepLinkRow && deepLinkRow.deepLink.startsWith("/") && !deepLinkRow.deepLink.startsWith("//")
|
||||
? `t3code://${deepLinkRow.deepLink.slice(1)}`
|
||||
: null;
|
||||
|
||||
// A scannable status glyph per phase — reads faster than colored words and
|
||||
// ties the compact / expanded / banner / watch presentations together.
|
||||
type SFName = NonNullable<ComponentProps<typeof Image>["systemName"]>;
|
||||
const phaseSymbol = (phase: AgentActivityPhase): SFName => {
|
||||
switch (phase) {
|
||||
case "waiting_for_approval":
|
||||
return "exclamationmark.circle.fill";
|
||||
case "waiting_for_input":
|
||||
return "questionmark.circle.fill";
|
||||
case "failed":
|
||||
return "xmark.octagon.fill";
|
||||
case "completed":
|
||||
return "checkmark.circle.fill";
|
||||
case "starting":
|
||||
return "circle.dotted";
|
||||
case "stale":
|
||||
return "clock.arrow.circlepath";
|
||||
case "running":
|
||||
default:
|
||||
return "arrow.triangle.2.circlepath";
|
||||
}
|
||||
};
|
||||
|
||||
// SF Symbols, like the logo, ignore frame/foregroundStyle applied directly to
|
||||
// the image; size + tint them through a container the resizable symbol fills.
|
||||
const renderGlyph = (systemName: SFName, size: number, color: string) => (
|
||||
<HStack modifiers={[frame({ width: size, height: size }), foregroundStyle(color)]}>
|
||||
<Image systemName={systemName} modifiers={[resizable()]} />
|
||||
</HStack>
|
||||
);
|
||||
|
||||
// Single-line row used by every presentation: glyph, title, inline project,
|
||||
// status. The project and status carry layoutPriority(1) so when space runs
|
||||
// out it's the title that truncates, never the (short) project name or the
|
||||
// status label. Single-line keeps rows inside the expanded island's hard
|
||||
// height budget (~160pt) and lets the banner fit more agents.
|
||||
const renderCompactRow = (row: AgentActivityRowProps) => (
|
||||
<HStack spacing={7} alignment="center">
|
||||
<Text
|
||||
modifiers={[
|
||||
font({ weight: "semibold", size: 13 }),
|
||||
foregroundStyle(primaryForeground),
|
||||
lineLimit(1),
|
||||
]}
|
||||
>
|
||||
{row.threadTitle}
|
||||
</Text>
|
||||
{/* No layoutPriority and no frame on the project: two bare texts take
|
||||
their ideal width when it fits and shrink proportionally only when it
|
||||
doesn't — so short rows never truncate, and long title + long project
|
||||
truncate together. (A maxWidth frame is greedy and reserved its full
|
||||
width even for short names; layoutPriority let the project starve the
|
||||
title.) */}
|
||||
<Text modifiers={[font({ size: 11 }), foregroundStyle(secondaryForeground), lineLimit(1)]}>
|
||||
{row.projectTitle}
|
||||
</Text>
|
||||
<Spacer minLength={8} />
|
||||
<Text
|
||||
modifiers={[
|
||||
font({ weight: "semibold", size: 11 }),
|
||||
foregroundStyle(phaseTint(row.phase)),
|
||||
layoutPriority(1),
|
||||
]}
|
||||
>
|
||||
{row.status}
|
||||
</Text>
|
||||
</HStack>
|
||||
);
|
||||
|
||||
// The branded T3 mark. `assetName` resolves the template image set bundled in
|
||||
// the widget extension's asset catalog. Image views only honor `resizable`
|
||||
// directly (frame/foregroundStyle are dropped), so we size it via a container
|
||||
// frame the resizable image fills and tint it through the container's
|
||||
// foreground style, which the template image inherits. The 3:2 frame matches
|
||||
// the glyph's aspect ratio so it never distorts.
|
||||
const renderLogo = (height: number, color: string) => (
|
||||
<HStack modifiers={[frame({ width: height * 1.5, height }), foregroundStyle(color)]}>
|
||||
<Image assetName="T3Mark" modifiers={[resizable()]} />
|
||||
</HStack>
|
||||
);
|
||||
|
||||
return {
|
||||
banner: (
|
||||
<VStack modifiers={[padding({ all: 14 })]}>
|
||||
<HStack>
|
||||
<VStack>
|
||||
<Text
|
||||
modifiers={[font({ weight: "bold", size: 15 }), foregroundStyle(primaryForeground)]}
|
||||
>
|
||||
{props.title}
|
||||
</Text>
|
||||
<Text
|
||||
modifiers={[font({ size: 12 }), foregroundStyle(secondaryForeground), lineLimit(1)]}
|
||||
>
|
||||
{props.subtitle}
|
||||
</Text>
|
||||
</VStack>
|
||||
<Spacer minLength={8} />
|
||||
<Text modifiers={[font({ weight: "semibold", size: 12 }), foregroundStyle(tint)]}>
|
||||
{activeLabel}
|
||||
</Text>
|
||||
</HStack>
|
||||
{row0 ? (
|
||||
<HStack modifiers={[padding({ vertical: 4 })]}>
|
||||
<VStack>
|
||||
<Text
|
||||
modifiers={[
|
||||
font({ weight: "bold", size: 13 }),
|
||||
foregroundStyle(primaryForeground),
|
||||
lineLimit(1),
|
||||
]}
|
||||
>
|
||||
{row0.threadTitle}
|
||||
</Text>
|
||||
<Text
|
||||
modifiers={[font({ size: 11 }), foregroundStyle(secondaryForeground), lineLimit(1)]}
|
||||
>
|
||||
{row0.projectTitle} - {row0.modelTitle}
|
||||
</Text>
|
||||
</VStack>
|
||||
<Spacer minLength={8} />
|
||||
<Text modifiers={[font({ weight: "semibold", size: 11 }), foregroundStyle(tint)]}>
|
||||
{row0.status}
|
||||
</Text>
|
||||
<VStack
|
||||
alignment="leading"
|
||||
spacing={6}
|
||||
modifiers={deepLink ? [padding({ all: 14 }), widgetURL(deepLink)] : [padding({ all: 14 })]}
|
||||
>
|
||||
{/* Logo pinned to the leading edge; the status texts centered across the
|
||||
full width (ZStack so the logo doesn't skew the centering). No footer —
|
||||
overflow beyond the visible rows is inferable from the count. */}
|
||||
<ZStack>
|
||||
<HStack spacing={0} alignment="center">
|
||||
{renderLogo(13, primaryForeground)}
|
||||
<Spacer minLength={0} />
|
||||
</HStack>
|
||||
) : null}
|
||||
{row1 ? (
|
||||
<HStack modifiers={[padding({ vertical: 4 })]}>
|
||||
<VStack>
|
||||
<Text
|
||||
modifiers={[
|
||||
font({ weight: "bold", size: 13 }),
|
||||
foregroundStyle(primaryForeground),
|
||||
lineLimit(1),
|
||||
]}
|
||||
>
|
||||
{row1.threadTitle}
|
||||
</Text>
|
||||
<Text
|
||||
modifiers={[font({ size: 11 }), foregroundStyle(secondaryForeground), lineLimit(1)]}
|
||||
>
|
||||
{row1.projectTitle} - {row1.modelTitle}
|
||||
</Text>
|
||||
</VStack>
|
||||
<Spacer minLength={8} />
|
||||
<Text modifiers={[font({ weight: "semibold", size: 11 }), foregroundStyle(tint)]}>
|
||||
{row1.status}
|
||||
</Text>
|
||||
</HStack>
|
||||
) : null}
|
||||
{row2 ? (
|
||||
<HStack modifiers={[padding({ vertical: 4 })]}>
|
||||
<VStack>
|
||||
<Text
|
||||
modifiers={[
|
||||
font({ weight: "bold", size: 13 }),
|
||||
foregroundStyle(primaryForeground),
|
||||
lineLimit(1),
|
||||
]}
|
||||
>
|
||||
{row2.threadTitle}
|
||||
</Text>
|
||||
<Text
|
||||
modifiers={[font({ size: 11 }), foregroundStyle(secondaryForeground), lineLimit(1)]}
|
||||
>
|
||||
{row2.projectTitle} - {row2.modelTitle}
|
||||
</Text>
|
||||
</VStack>
|
||||
<Spacer minLength={8} />
|
||||
<Text modifiers={[font({ weight: "semibold", size: 11 }), foregroundStyle(tint)]}>
|
||||
{row2.status}
|
||||
</Text>
|
||||
</HStack>
|
||||
) : null}
|
||||
<Text modifiers={[font({ size: 11 }), foregroundStyle(mutedForeground)]}>
|
||||
Updated {updatedAt}
|
||||
</Text>
|
||||
</VStack>
|
||||
),
|
||||
bannerSmall: (
|
||||
<VStack modifiers={[padding({ all: 12 })]}>
|
||||
<HStack>
|
||||
<Text
|
||||
modifiers={[font({ weight: "bold", size: 13 }), foregroundStyle(primaryForeground)]}
|
||||
>
|
||||
{props.title}
|
||||
</Text>
|
||||
<Spacer minLength={6} />
|
||||
<Text modifiers={[font({ weight: "semibold", size: 12 }), foregroundStyle(tint)]}>
|
||||
{activeLabel}
|
||||
</Text>
|
||||
</HStack>
|
||||
{row0 ? (
|
||||
<VStack>
|
||||
<HStack spacing={6} alignment="center">
|
||||
<Spacer minLength={0} />
|
||||
<Text
|
||||
modifiers={[
|
||||
font({ weight: "bold", size: 12 }),
|
||||
font({ weight: "semibold", size: 13 }),
|
||||
// The all-done header carries the outcome tint (emerald /
|
||||
// red) the way the Done/Failed status labels do.
|
||||
foregroundStyle(allDone ? headerTint : primaryForeground),
|
||||
lineLimit(1),
|
||||
]}
|
||||
>
|
||||
{agentsLabel}
|
||||
</Text>
|
||||
{attentionSuffix ? (
|
||||
<Text modifiers={[font({ size: 13 }), foregroundStyle(secondaryForeground)]}>·</Text>
|
||||
) : null}
|
||||
{attentionSuffix ? (
|
||||
<Text
|
||||
modifiers={[
|
||||
font({ weight: "semibold", size: 13 }),
|
||||
foregroundStyle(headerTint),
|
||||
lineLimit(1),
|
||||
]}
|
||||
>
|
||||
{attentionSuffix}
|
||||
</Text>
|
||||
) : null}
|
||||
<Spacer minLength={0} />
|
||||
</HStack>
|
||||
</ZStack>
|
||||
{row0 ? renderCompactRow(row0) : null}
|
||||
{row1 ? renderCompactRow(row1) : null}
|
||||
{row2 ? renderCompactRow(row2) : null}
|
||||
{row3 ? renderCompactRow(row3) : null}
|
||||
{row4 ? renderCompactRow(row4) : null}
|
||||
</VStack>
|
||||
),
|
||||
// Compact card for the watchOS Smart Stack + CarPlay (the `.small` family):
|
||||
// brand + count, then the single most important agent with its status glyph.
|
||||
bannerSmall: (
|
||||
<VStack alignment="leading" spacing={5} modifiers={[padding({ all: 10 })]}>
|
||||
<HStack spacing={7} alignment="center">
|
||||
{renderLogo(14, primaryForeground)}
|
||||
<Text
|
||||
modifiers={[
|
||||
font({ weight: "bold", size: 13 }),
|
||||
foregroundStyle(headerTint),
|
||||
lineLimit(1),
|
||||
]}
|
||||
>
|
||||
{attentionRows.length > 0 ? summary : activeLabel}
|
||||
</Text>
|
||||
<Spacer minLength={6} />
|
||||
</HStack>
|
||||
{row0 ? (
|
||||
<HStack spacing={7} alignment="center">
|
||||
<Text
|
||||
modifiers={[
|
||||
font({ weight: "semibold", size: 12 }),
|
||||
foregroundStyle(primaryForeground),
|
||||
lineLimit(1),
|
||||
]}
|
||||
>
|
||||
{row0.threadTitle}
|
||||
</Text>
|
||||
<Text
|
||||
modifiers={[font({ size: 11 }), foregroundStyle(secondaryForeground), lineLimit(1)]}
|
||||
>
|
||||
{row0.projectTitle} - {row0.status}
|
||||
</Text>
|
||||
</VStack>
|
||||
) : null}
|
||||
</VStack>
|
||||
),
|
||||
compactLeading: (
|
||||
<Text modifiers={[font({ weight: "bold", size: 11 }), foregroundStyle(tint)]}>T3</Text>
|
||||
),
|
||||
compactTrailing: (
|
||||
<Text modifiers={[font({ weight: "semibold", size: 11 }), foregroundStyle(tint)]}>
|
||||
{activeLabel}
|
||||
</Text>
|
||||
),
|
||||
minimal: (
|
||||
<Text modifiers={[font({ weight: "bold", size: 11 }), foregroundStyle(tint)]}>T3</Text>
|
||||
),
|
||||
expandedLeading: (
|
||||
<VStack modifiers={[padding({ all: 8 })]}>
|
||||
<Text modifiers={[font({ weight: "bold", size: 12 }), foregroundStyle(tint)]}>
|
||||
{activeLabel}
|
||||
</Text>
|
||||
</VStack>
|
||||
),
|
||||
expandedCenter: row0 ? (
|
||||
<VStack>
|
||||
<Text
|
||||
modifiers={[
|
||||
font({ weight: "bold", size: 12 }),
|
||||
foregroundStyle(primaryForeground),
|
||||
lineLimit(1),
|
||||
]}
|
||||
>
|
||||
{row0.threadTitle}
|
||||
</Text>
|
||||
<Text modifiers={[font({ size: 11 }), foregroundStyle(secondaryForeground), lineLimit(1)]}>
|
||||
{row0.projectTitle} - {row0.status}
|
||||
</Text>
|
||||
</VStack>
|
||||
) : null,
|
||||
expandedTrailing: (
|
||||
<Text modifiers={[font({ size: 11 }), foregroundStyle(secondaryForeground)]}>
|
||||
Updated {updatedAt}
|
||||
</Text>
|
||||
),
|
||||
expandedBottom: (
|
||||
<VStack modifiers={[padding({ all: 8 })]}>
|
||||
{row0 ? (
|
||||
<HStack modifiers={[padding({ vertical: 4 })]}>
|
||||
<VStack>
|
||||
<Text
|
||||
modifiers={[
|
||||
font({ weight: "bold", size: 13 }),
|
||||
foregroundStyle(primaryForeground),
|
||||
lineLimit(1),
|
||||
]}
|
||||
>
|
||||
{row0.threadTitle}
|
||||
</Text>
|
||||
<Text
|
||||
modifiers={[font({ size: 11 }), foregroundStyle(secondaryForeground), lineLimit(1)]}
|
||||
>
|
||||
{row0.projectTitle} - {row0.modelTitle}
|
||||
</Text>
|
||||
</VStack>
|
||||
<Spacer minLength={8} />
|
||||
<Text modifiers={[font({ weight: "semibold", size: 11 }), foregroundStyle(tint)]}>
|
||||
<Spacer minLength={6} />
|
||||
<Text modifiers={[font({ size: 11 }), foregroundStyle(phaseTint(row0.phase))]}>
|
||||
{row0.status}
|
||||
</Text>
|
||||
</HStack>
|
||||
) : null}
|
||||
{row1 ? (
|
||||
<HStack modifiers={[padding({ vertical: 4 })]}>
|
||||
<VStack>
|
||||
<Text
|
||||
modifiers={[
|
||||
font({ weight: "bold", size: 13 }),
|
||||
foregroundStyle(primaryForeground),
|
||||
lineLimit(1),
|
||||
]}
|
||||
>
|
||||
{row1.threadTitle}
|
||||
</Text>
|
||||
<Text
|
||||
modifiers={[font({ size: 11 }), foregroundStyle(secondaryForeground), lineLimit(1)]}
|
||||
>
|
||||
{row1.projectTitle} - {row1.modelTitle}
|
||||
</Text>
|
||||
</VStack>
|
||||
<Spacer minLength={8} />
|
||||
<Text modifiers={[font({ weight: "semibold", size: 11 }), foregroundStyle(tint)]}>
|
||||
{row1.status}
|
||||
</Text>
|
||||
</HStack>
|
||||
) : null}
|
||||
{row2 ? (
|
||||
<HStack modifiers={[padding({ vertical: 4 })]}>
|
||||
<VStack>
|
||||
<Text
|
||||
modifiers={[
|
||||
font({ weight: "bold", size: 13 }),
|
||||
foregroundStyle(primaryForeground),
|
||||
lineLimit(1),
|
||||
]}
|
||||
>
|
||||
{row2.threadTitle}
|
||||
</Text>
|
||||
<Text
|
||||
modifiers={[font({ size: 11 }), foregroundStyle(secondaryForeground), lineLimit(1)]}
|
||||
>
|
||||
{row2.projectTitle} - {row2.modelTitle}
|
||||
</Text>
|
||||
</VStack>
|
||||
<Spacer minLength={8} />
|
||||
<Text modifiers={[font({ weight: "semibold", size: 11 }), foregroundStyle(tint)]}>
|
||||
{row2.status}
|
||||
</Text>
|
||||
</HStack>
|
||||
) : null}
|
||||
</VStack>
|
||||
),
|
||||
compactLeading: renderLogo(14, tint),
|
||||
compactTrailing: (
|
||||
<Text modifiers={[font({ weight: "semibold", size: 11 }), foregroundStyle(tint)]}>
|
||||
{attentionRow
|
||||
? attentionRow.phase === "waiting_for_approval"
|
||||
? "Approval"
|
||||
: "Input"
|
||||
: activeLabel}
|
||||
</Text>
|
||||
),
|
||||
// The shared/minimal form is a ~22pt circle — a single signal reads there,
|
||||
// the wordmark does not. Show the blocking/outcome phase glyph, else the
|
||||
// mark (all-done shows the hero row's checkmark/cross).
|
||||
minimal:
|
||||
(attentionRow || failedRow || allDone) && heroRow
|
||||
? renderGlyph(phaseSymbol(heroRow.phase), 13, phaseTint(heroRow.phase))
|
||||
: renderLogo(11, tint),
|
||||
expandedLeading: (
|
||||
<HStack spacing={5} alignment="center" modifiers={[padding({ leading: 4, vertical: 4 })]}>
|
||||
{renderLogo(15, tint)}
|
||||
<Text modifiers={[font({ weight: "bold", size: 13 }), foregroundStyle(tint)]}>
|
||||
{allDone ? doneLabel : `${props.activeCount}`}
|
||||
</Text>
|
||||
</HStack>
|
||||
),
|
||||
// No center content: the phase glyphs + statuses in expandedBottom already
|
||||
// carry the attention signal, and the expanded island's height budget is
|
||||
// tight enough that a summary line there pushed the third row off.
|
||||
expandedCenter: null,
|
||||
// No trailing content: a timestamp is glanceable-lock-screen info, not
|
||||
// useful in a view the user is actively holding open — and the trailing
|
||||
// region hugs the island's corner radius, which clipped it anyway.
|
||||
expandedTrailing: null,
|
||||
expandedBottom: (
|
||||
// Vertical padding only: the expanded region provides its own horizontal
|
||||
// content margins, so `all` padding double-indented the rows.
|
||||
// Horizontal padding keeps both edges clear of the island's corner
|
||||
// curvature (right edge clipped status labels; titles hugged the left).
|
||||
<VStack
|
||||
alignment="leading"
|
||||
spacing={5}
|
||||
modifiers={
|
||||
deepLink
|
||||
? [padding({ vertical: 2, horizontal: 8 }), widgetURL(deepLink)]
|
||||
: [padding({ vertical: 2, horizontal: 8 })]
|
||||
}
|
||||
>
|
||||
{row0 ? renderCompactRow(row0) : null}
|
||||
{row1 ? renderCompactRow(row1) : null}
|
||||
{row2 ? renderCompactRow(row2) : null}
|
||||
</VStack>
|
||||
),
|
||||
};
|
||||
|
||||
@@ -8,7 +8,11 @@ import * as Logger from "effect/Logger";
|
||||
import * as Option from "effect/Option";
|
||||
import * as References from "effect/References";
|
||||
|
||||
import { acquireRelayClientForLink, reportCloudDisconnectResults } from "./connect.ts";
|
||||
import {
|
||||
acquireRelayClientForLink,
|
||||
isPublishAgentActivityEnabledValue,
|
||||
reportCloudDisconnectResults,
|
||||
} from "./connect.ts";
|
||||
|
||||
const managedExecutable = {
|
||||
status: "available",
|
||||
@@ -153,3 +157,10 @@ it.effect("keeps disconnect causes in structured logs and out of console warning
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it("treats only the literal 'true' as publish-enabled", () => {
|
||||
assert.equal(isPublishAgentActivityEnabledValue("true"), true);
|
||||
assert.equal(isPublishAgentActivityEnabledValue("false"), false);
|
||||
assert.equal(isPublishAgentActivityEnabledValue(null), false);
|
||||
assert.equal(isPublishAgentActivityEnabledValue("TRUE"), false);
|
||||
});
|
||||
+136
-25
@@ -29,7 +29,11 @@ import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
|
||||
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
|
||||
import * as CliState from "../cloud/CliState.ts";
|
||||
import * as CliTokenManager from "../cloud/CliTokenManager.ts";
|
||||
import { CLOUD_LINKED_USER_ID, RELAY_URL_SECRET } from "../cloud/config.ts";
|
||||
import {
|
||||
CLOUD_LINKED_USER_ID,
|
||||
PUBLISH_AGENT_ACTIVITY_SECRET,
|
||||
RELAY_URL_SECRET,
|
||||
} from "../cloud/config.ts";
|
||||
import { relayUrlConfig } from "../cloud/publicConfig.ts";
|
||||
import { headlessRelayClientTracingLayer } from "../cloud/relayTracing.ts";
|
||||
import * as ServerConfig from "../config.ts";
|
||||
@@ -46,12 +50,21 @@ function bytesToString(value: Uint8Array): string {
|
||||
return new TextDecoder().decode(value);
|
||||
}
|
||||
|
||||
function stringToBytes(value: string): Uint8Array {
|
||||
return new TextEncoder().encode(value);
|
||||
}
|
||||
|
||||
export function isPublishAgentActivityEnabledValue(value: string | null): boolean {
|
||||
return value === "true";
|
||||
}
|
||||
|
||||
interface CloudCliStatus {
|
||||
readonly desired: boolean;
|
||||
readonly authenticated: boolean;
|
||||
readonly linked: boolean;
|
||||
readonly cloudUserId: string | null;
|
||||
readonly relayUrl: string | null;
|
||||
readonly publishAgentActivity: boolean;
|
||||
readonly relayClient: RelayClient.RelayClientStatus;
|
||||
}
|
||||
|
||||
@@ -104,6 +117,7 @@ function formatCloudStatus(status: CloudCliStatus, options?: { readonly json?: b
|
||||
` Authorization: ${status.authenticated ? "stored credential" : "missing"}`,
|
||||
` Environment link: ${provisioned}`,
|
||||
` Relay: ${status.relayUrl ?? "not provisioned"}`,
|
||||
` Publish agent activity: ${status.publishAgentActivity ? "enabled" : "disabled"}`,
|
||||
...formatRelayClientStatus(status.relayClient),
|
||||
...(nextStep ? ["", `Next: ${nextStep}`] : []),
|
||||
].join("\n");
|
||||
@@ -368,31 +382,52 @@ const connectLoginCommand = Command.make("login", {
|
||||
|
||||
const connectLinkCommand = Command.make("link", {
|
||||
...projectLocationFlags,
|
||||
publishOnly: Flag.boolean("publish-only").pipe(
|
||||
Flag.withDescription(
|
||||
"Link to publish agent activity only — no managed tunnel. Reach this environment out of band (e.g. Tailscale).",
|
||||
),
|
||||
Flag.withDefault(false),
|
||||
),
|
||||
}).pipe(
|
||||
Command.withDescription("Authorize this environment for T3 Connect on next start."),
|
||||
Command.withHandler((flags) =>
|
||||
runCloudCommand(
|
||||
flags,
|
||||
Effect.gen(function* () {
|
||||
const relayClient = yield* RelayClient.RelayClient;
|
||||
const installed = yield* acquireRelayClientForLink(
|
||||
relayClient,
|
||||
confirmRelayClientInstall,
|
||||
reportRelayClientInstallProgress,
|
||||
);
|
||||
if (Option.isNone(installed)) {
|
||||
yield* Console.log("T3 Connect setup cancelled. The relay client was not installed.");
|
||||
return;
|
||||
// A publish-only link needs no Cloudflare tunnel, so skip installing the
|
||||
// relay client entirely.
|
||||
if (!flags.publishOnly) {
|
||||
const relayClient = yield* RelayClient.RelayClient;
|
||||
const installed = yield* acquireRelayClientForLink(
|
||||
relayClient,
|
||||
confirmRelayClientInstall,
|
||||
reportRelayClientInstallProgress,
|
||||
);
|
||||
if (Option.isNone(installed)) {
|
||||
yield* Console.log("T3 Connect setup cancelled. The relay client was not installed.");
|
||||
return;
|
||||
}
|
||||
yield* Console.log(
|
||||
`Using relay client ${installed.value.version} from ${installed.value.executablePath}.`,
|
||||
);
|
||||
}
|
||||
yield* Console.log(
|
||||
`Using relay client ${installed.value.version} from ${installed.value.executablePath}.`,
|
||||
);
|
||||
|
||||
const tokens = yield* CliTokenManager.CloudCliTokenManager;
|
||||
yield* tokens.get;
|
||||
yield* CliState.setCliDesiredCloudLink(true);
|
||||
yield* CliState.setCliDesiredCloudLink(
|
||||
true,
|
||||
flags.publishOnly ? "publish_only" : "managed",
|
||||
);
|
||||
if (flags.publishOnly) {
|
||||
// A publish-only link exists solely to publish; without the publish
|
||||
// flag the link would be inert and the success message a lie.
|
||||
const secrets = yield* ServerSecretStore.ServerSecretStore;
|
||||
yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, stringToBytes("true"));
|
||||
}
|
||||
yield* Console.log(
|
||||
"This T3 environment will be available through T3 Connect the next time T3 starts.",
|
||||
flags.publishOnly
|
||||
? "This environment will publish agent activity to your mobile clients the next time T3 starts (no managed tunnel)."
|
||||
: "This T3 environment will be available through T3 Connect the next time T3 starts.",
|
||||
);
|
||||
}),
|
||||
),
|
||||
@@ -411,22 +446,27 @@ const connectStatusCommand = Command.make("status", {
|
||||
const secrets = yield* ServerSecretStore.ServerSecretStore;
|
||||
const relayClient = yield* RelayClient.RelayClient;
|
||||
const tokens = yield* CliTokenManager.CloudCliTokenManager;
|
||||
const [desired, authenticated, cloudUserId, relayUrl, executable] = yield* Effect.all(
|
||||
[
|
||||
CliState.readCliDesiredCloudLink,
|
||||
tokens.hasCredential,
|
||||
secrets.get(CLOUD_LINKED_USER_ID),
|
||||
secrets.get(RELAY_URL_SECRET),
|
||||
relayClient.resolve,
|
||||
],
|
||||
{ concurrency: "unbounded" },
|
||||
);
|
||||
const [desired, authenticated, cloudUserId, relayUrl, publishAgentActivity, executable] =
|
||||
yield* Effect.all(
|
||||
[
|
||||
CliState.readCliDesiredCloudLink,
|
||||
tokens.hasCredential,
|
||||
secrets.get(CLOUD_LINKED_USER_ID),
|
||||
secrets.get(RELAY_URL_SECRET),
|
||||
secrets.get(PUBLISH_AGENT_ACTIVITY_SECRET),
|
||||
relayClient.resolve,
|
||||
],
|
||||
{ concurrency: "unbounded" },
|
||||
);
|
||||
const status: CloudCliStatus = {
|
||||
desired,
|
||||
authenticated,
|
||||
linked: Option.isSome(cloudUserId),
|
||||
cloudUserId: Option.isSome(cloudUserId) ? bytesToString(cloudUserId.value) : null,
|
||||
relayUrl: Option.isSome(relayUrl) ? bytesToString(relayUrl.value) : null,
|
||||
publishAgentActivity: isPublishAgentActivityEnabledValue(
|
||||
Option.isSome(publishAgentActivity) ? bytesToString(publishAgentActivity.value) : null,
|
||||
),
|
||||
relayClient: executable,
|
||||
};
|
||||
yield* Console.log(formatCloudStatus(status, { json: flags.json }));
|
||||
@@ -438,6 +478,76 @@ const connectStatusCommand = Command.make("status", {
|
||||
),
|
||||
);
|
||||
|
||||
const connectPublishCommand = Command.make("publish", {
|
||||
...projectLocationFlags,
|
||||
disable: Flag.boolean("disable").pipe(
|
||||
Flag.withDescription("Stop publishing agent activity to your mobile clients."),
|
||||
Flag.withDefault(false),
|
||||
),
|
||||
}).pipe(
|
||||
Command.withDescription(
|
||||
"Toggle publishing agent activity (push notifications and Live Activities) to your mobile clients.",
|
||||
),
|
||||
Command.withHandler((flags) =>
|
||||
runCloudCommand(
|
||||
flags,
|
||||
Effect.gen(function* () {
|
||||
const secrets = yield* ServerSecretStore.ServerSecretStore;
|
||||
const tokens = yield* CliTokenManager.CloudCliTokenManager;
|
||||
const enabled = !flags.disable;
|
||||
yield* secrets.set(
|
||||
PUBLISH_AGENT_ACTIVITY_SECRET,
|
||||
stringToBytes(enabled ? "true" : "false"),
|
||||
);
|
||||
if (!enabled) {
|
||||
// If enabling scheduled a publish-only link that hasn't been
|
||||
// provisioned yet, disabling must cancel it too — otherwise the next
|
||||
// start still links an environment whose only purpose was publishing.
|
||||
// A pending managed link is left alone; it exists for the tunnel.
|
||||
const linkedNow = Option.isSome(yield* secrets.get(CLOUD_LINKED_USER_ID));
|
||||
if (!linkedNow && (yield* CliState.readCliDesiredLinkMode) === "publish_only") {
|
||||
yield* CliState.setCliDesiredCloudLink(false);
|
||||
yield* Console.log("Cancelled the pending publish-only T3 Connect link.");
|
||||
}
|
||||
yield* Console.log("Publishing agent activity disabled.");
|
||||
return;
|
||||
}
|
||||
|
||||
yield* Console.log("Publishing agent activity enabled.");
|
||||
const linked = Option.isSome(yield* secrets.get(CLOUD_LINKED_USER_ID));
|
||||
if (linked) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Publishing needs the relay to know this environment belongs to you.
|
||||
// Establish a tunnel-free publish-only link automatically so signing in
|
||||
// is all it takes — the mobile client can still reach the environment
|
||||
// out of band without T3 Connect.
|
||||
if (!(yield* tokens.hasCredential)) {
|
||||
yield* Console.log(
|
||||
"Run `t3 connect login` first so this environment can be authorized to publish.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
// A link may already be desired (e.g. `t3 connect link` before the
|
||||
// server's first start). Never downgrade it: a desired managed link
|
||||
// also covers publishing, so only request a publish-only link when no
|
||||
// link is pending at all.
|
||||
if (yield* CliState.readCliDesiredCloudLink) {
|
||||
yield* Console.log(
|
||||
"A T3 Connect link is already pending. Start T3 to finish provisioning it; publishing starts once it links.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
yield* CliState.setCliDesiredCloudLink(true, "publish_only");
|
||||
yield* Console.log(
|
||||
"Restart T3 to finish authorizing this environment to publish (no managed tunnel is created).",
|
||||
);
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
const connectUnlinkCommand = Command.make("unlink", {
|
||||
...projectLocationFlags,
|
||||
}).pipe(
|
||||
@@ -461,6 +571,7 @@ export const connectCommand = Command.make("connect").pipe(
|
||||
Command.withSubcommands([
|
||||
connectLoginCommand,
|
||||
connectLinkCommand,
|
||||
connectPublishCommand,
|
||||
connectStatusCommand,
|
||||
connectUnlinkCommand,
|
||||
connectLogoutCommand,
|
||||
|
||||
@@ -56,4 +56,27 @@ it.layer(NodeServices.layer)("CliState", (it) => {
|
||||
}
|
||||
}).pipe(Effect.provide(makeTestLayer())),
|
||||
);
|
||||
|
||||
it.effect("round-trips the desired link mode and defaults legacy links to managed", () =>
|
||||
Effect.gen(function* () {
|
||||
const secrets = yield* ServerSecretStore.ServerSecretStore;
|
||||
|
||||
assert.equal(yield* CliState.readCliDesiredLinkMode, "managed");
|
||||
|
||||
yield* CliState.setCliDesiredCloudLink(true, "publish_only");
|
||||
assert.isTrue(yield* CliState.readCliDesiredCloudLink);
|
||||
assert.equal(yield* CliState.readCliDesiredLinkMode, "publish_only");
|
||||
|
||||
yield* CliState.setCliDesiredCloudLink(true, "managed");
|
||||
assert.equal(yield* CliState.readCliDesiredLinkMode, "managed");
|
||||
|
||||
// A pre-existing link persisted the literal "true"; treat it as managed.
|
||||
yield* secrets.set(CliState.CLOUD_CLI_DESIRED_LINK_SECRET, new TextEncoder().encode("true"));
|
||||
assert.isTrue(yield* CliState.readCliDesiredCloudLink);
|
||||
assert.equal(yield* CliState.readCliDesiredLinkMode, "managed");
|
||||
|
||||
yield* CliState.setCliDesiredCloudLink(false);
|
||||
assert.equal(yield* CliState.readCliDesiredLinkMode, "managed");
|
||||
}).pipe(Effect.provide(makeTestLayer())),
|
||||
);
|
||||
});
|
||||
@@ -14,19 +14,43 @@ import {
|
||||
|
||||
export const CLOUD_CLI_DESIRED_LINK_SECRET = "cloud-cli-desired-link";
|
||||
|
||||
const TRUE_BYTES = new TextEncoder().encode("true");
|
||||
// "managed" provisions a Cloudflare tunnel (default, legacy value "true").
|
||||
// "publish_only" links the environment to the relay purely to publish agent
|
||||
// activity — no tunnel, no relay-advertised endpoint — so activity can flow to
|
||||
// mobile clients even when they reach the environment out of band (Tailscale,
|
||||
// direct pairing) without T3 Connect.
|
||||
export type CliDesiredLinkMode = "managed" | "publish_only";
|
||||
|
||||
const MANAGED_BYTES = new TextEncoder().encode("managed");
|
||||
const PUBLISH_ONLY_BYTES = new TextEncoder().encode("publish_only");
|
||||
|
||||
export const readCliDesiredCloudLink = Effect.gen(function* () {
|
||||
const secrets = yield* ServerSecretStore.ServerSecretStore;
|
||||
return Option.isSome(yield* secrets.get(CLOUD_CLI_DESIRED_LINK_SECRET));
|
||||
});
|
||||
|
||||
export const readCliDesiredLinkMode = Effect.gen(function* () {
|
||||
const secrets = yield* ServerSecretStore.ServerSecretStore;
|
||||
const value = yield* secrets.get(CLOUD_CLI_DESIRED_LINK_SECRET);
|
||||
if (Option.isNone(value)) {
|
||||
return "managed" as CliDesiredLinkMode;
|
||||
}
|
||||
// Legacy links stored the literal "true" and are always managed.
|
||||
return new TextDecoder().decode(value.value) === "publish_only"
|
||||
? ("publish_only" as CliDesiredLinkMode)
|
||||
: ("managed" as CliDesiredLinkMode);
|
||||
});
|
||||
|
||||
export const setCliDesiredCloudLink = Effect.fn("cloud.cli_state.set_desired")(function* (
|
||||
desired: boolean,
|
||||
mode: CliDesiredLinkMode = "managed",
|
||||
) {
|
||||
const secrets = yield* ServerSecretStore.ServerSecretStore;
|
||||
if (desired) {
|
||||
yield* secrets.set(CLOUD_CLI_DESIRED_LINK_SECRET, TRUE_BYTES);
|
||||
yield* secrets.set(
|
||||
CLOUD_CLI_DESIRED_LINK_SECRET,
|
||||
mode === "publish_only" ? PUBLISH_ONLY_BYTES : MANAGED_BYTES,
|
||||
);
|
||||
} else {
|
||||
yield* secrets.remove(CLOUD_CLI_DESIRED_LINK_SECRET);
|
||||
}
|
||||
|
||||
@@ -11,7 +11,13 @@ import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
|
||||
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
|
||||
import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
|
||||
import * as CliTokenManager from "./CliTokenManager.ts";
|
||||
import { consumeCloudReplayGuards, reconcileDesiredCloudLink } from "./http.ts";
|
||||
import type { RelayLinkProofRequest } from "@t3tools/contracts/relay";
|
||||
import {
|
||||
consumeCloudReplayGuards,
|
||||
isSupportedLinkProviderKind,
|
||||
linkProofScopes,
|
||||
reconcileDesiredCloudLink,
|
||||
} from "./http.ts";
|
||||
import * as ManagedEndpointRuntime from "./ManagedEndpointRuntime.ts";
|
||||
import { traceAuthenticatedRelayRequest, traceRelayRequest } from "./traceRelayRequest.ts";
|
||||
|
||||
@@ -205,3 +211,32 @@ describe("reconcileDesiredCloudLink", () => {
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
describe("link proof provider kinds", () => {
|
||||
const proofRequest = (
|
||||
providerKind: RelayLinkProofRequest["endpoint"]["providerKind"],
|
||||
): RelayLinkProofRequest => ({
|
||||
challenge: "challenge",
|
||||
relayIssuer: "https://relay.example.test",
|
||||
endpoint: {
|
||||
httpBaseUrl: "http://127.0.0.1:7331",
|
||||
wsBaseUrl: "ws://127.0.0.1:7331",
|
||||
providerKind,
|
||||
},
|
||||
origin: { localHttpHost: "127.0.0.1", localHttpPort: 7331 },
|
||||
});
|
||||
|
||||
it("accepts managed and manual endpoints but not t3_relay", () => {
|
||||
expect(isSupportedLinkProviderKind(proofRequest("cloudflare_tunnel"))).toBe(true);
|
||||
expect(isSupportedLinkProviderKind(proofRequest("manual"))).toBe(true);
|
||||
expect(isSupportedLinkProviderKind(proofRequest("t3_relay"))).toBe(false);
|
||||
});
|
||||
|
||||
it("only claims the managed-tunnel scope for tunnel links", () => {
|
||||
expect(linkProofScopes(proofRequest("cloudflare_tunnel"))).toEqual([
|
||||
"agent_activity_notifications",
|
||||
"managed_tunnels",
|
||||
]);
|
||||
expect(linkProofScopes(proofRequest("manual"))).toEqual(["agent_activity_notifications"]);
|
||||
});
|
||||
});
|
||||
@@ -68,7 +68,7 @@ import {
|
||||
RELAY_URL_SECRET,
|
||||
} from "./config.ts";
|
||||
import { relayUrlConfig } from "./publicConfig.ts";
|
||||
import { setCliDesiredCloudLink } from "./CliState.ts";
|
||||
import { readCliDesiredLinkMode, setCliDesiredCloudLink } from "./CliState.ts";
|
||||
import * as CliTokenManager from "./CliTokenManager.ts";
|
||||
import { getOrCreateEnvironmentKeyPairFromSecretStore } from "./environmentKeys.ts";
|
||||
import { traceRelayRequest } from "./traceRelayRequest.ts";
|
||||
@@ -299,8 +299,23 @@ function isAllowedEndpointOrigin(input: {
|
||||
return input.origin.localHttpPort === endpointRequestPort(url);
|
||||
}
|
||||
|
||||
function providerKindMatchesRequestedLinkScopes(request: RelayLinkProofRequest): boolean {
|
||||
return request.endpoint.providerKind === "cloudflare_tunnel";
|
||||
// A managed (Cloudflare tunnel) endpoint is provisioned by the relay and must
|
||||
// point at a loopback origin. A manual endpoint is reached out of band (e.g.
|
||||
// Tailscale) or not advertised at all for publish-only links, so it is not
|
||||
// tied to the managed-tunnel scope.
|
||||
export function isSupportedLinkProviderKind(request: RelayLinkProofRequest): boolean {
|
||||
return (
|
||||
request.endpoint.providerKind === "cloudflare_tunnel" ||
|
||||
request.endpoint.providerKind === "manual"
|
||||
);
|
||||
}
|
||||
|
||||
export function linkProofScopes(
|
||||
request: RelayLinkProofRequest,
|
||||
): RelayEnvironmentLinkProofPayload["scopes"] {
|
||||
return request.endpoint.providerKind === "cloudflare_tunnel"
|
||||
? ["agent_activity_notifications", "managed_tunnels"]
|
||||
: ["agent_activity_notifications"];
|
||||
}
|
||||
|
||||
function hasExactScope(input: {
|
||||
@@ -352,7 +367,7 @@ const makeCloudLinkProof = Effect.fn("environment.cloud.makeLinkProof")(function
|
||||
) {
|
||||
const keyPair = yield* getOrCreateEnvironmentKeyPairFromSecretStore(dependencies.secrets);
|
||||
if (
|
||||
!providerKindMatchesRequestedLinkScopes(request) ||
|
||||
!isSupportedLinkProviderKind(request) ||
|
||||
!isAllowedEndpointOrigin({
|
||||
origin: request.origin,
|
||||
requestUrl,
|
||||
@@ -379,7 +394,7 @@ const makeCloudLinkProof = Effect.fn("environment.cloud.makeLinkProof")(function
|
||||
environmentPublicKey: normalizePemForSignedPayload(keyPair.publicKey),
|
||||
endpoint: request.endpoint,
|
||||
origin: request.origin,
|
||||
scopes: ["agent_activity_notifications", "managed_tunnels"],
|
||||
scopes: linkProofScopes(request),
|
||||
} satisfies RelayEnvironmentLinkProofPayload;
|
||||
return yield* signRelayJwt({
|
||||
privateKey: keyPair.privateKey,
|
||||
@@ -537,6 +552,8 @@ const reconcileDesiredCloudLinkWith = Effect.fn("environment.cloud.reconcileDesi
|
||||
}),
|
||||
),
|
||||
);
|
||||
const mode = yield* readCliDesiredLinkMode;
|
||||
const managedTunnelsEnabled = mode !== "publish_only";
|
||||
const relayUrl = yield* requireRelayUrl;
|
||||
const challenge = yield* relayClientRequest(dependencies, {
|
||||
url: `${relayUrl}/v1/client/environment-link-challenges`,
|
||||
@@ -544,7 +561,7 @@ const reconcileDesiredCloudLinkWith = Effect.fn("environment.cloud.reconcileDesi
|
||||
payload: {
|
||||
notificationsEnabled: true,
|
||||
liveActivitiesEnabled: true,
|
||||
managedTunnelsEnabled: true,
|
||||
managedTunnelsEnabled,
|
||||
},
|
||||
schema: RelayEnvironmentLinkChallengeResponse,
|
||||
});
|
||||
@@ -556,7 +573,7 @@ const reconcileDesiredCloudLinkWith = Effect.fn("environment.cloud.reconcileDesi
|
||||
endpoint: {
|
||||
httpBaseUrl: localOrigin,
|
||||
wsBaseUrl: localWsOrigin,
|
||||
providerKind: "cloudflare_tunnel",
|
||||
providerKind: managedTunnelsEnabled ? "cloudflare_tunnel" : "manual",
|
||||
},
|
||||
origin: {
|
||||
localHttpHost: localUrl.hostname,
|
||||
@@ -572,11 +589,11 @@ const reconcileDesiredCloudLinkWith = Effect.fn("environment.cloud.reconcileDesi
|
||||
proof,
|
||||
notificationsEnabled: true,
|
||||
liveActivitiesEnabled: true,
|
||||
managedTunnelsEnabled: true,
|
||||
managedTunnelsEnabled,
|
||||
},
|
||||
schema: RelayEnvironmentLinkResponse,
|
||||
});
|
||||
yield* setCliDesiredCloudLink(true);
|
||||
yield* setCliDesiredCloudLink(true, mode);
|
||||
return yield* applyCloudRelayConfig(dependencies, {
|
||||
relayUrl,
|
||||
relayIssuer: link.relayIssuer,
|
||||
@@ -608,20 +625,25 @@ export const reconcileDesiredCloudLink = Effect.fn("environment.cloud.reconcileD
|
||||
const readCloudLinkState = Effect.fn("environment.cloud.readLinkState")(function* (
|
||||
dependencies: CloudHttpDependencies,
|
||||
) {
|
||||
const [cloudUserId, relayUrl, relayIssuer, publishAgentActivity] = yield* Effect.all(
|
||||
[
|
||||
dependencies.secrets.get(CLOUD_LINKED_USER_ID),
|
||||
dependencies.secrets.get(RELAY_URL_SECRET),
|
||||
dependencies.secrets.get(RELAY_ISSUER_SECRET),
|
||||
dependencies.secrets.get(PUBLISH_AGENT_ACTIVITY_SECRET),
|
||||
],
|
||||
{ concurrency: 4 },
|
||||
);
|
||||
const [cloudUserId, relayUrl, relayIssuer, endpointRuntimeConfig, publishAgentActivity] =
|
||||
yield* Effect.all(
|
||||
[
|
||||
dependencies.secrets.get(CLOUD_LINKED_USER_ID),
|
||||
dependencies.secrets.get(RELAY_URL_SECRET),
|
||||
dependencies.secrets.get(RELAY_ISSUER_SECRET),
|
||||
dependencies.secrets.get(CLOUD_ENDPOINT_RUNTIME_CONFIG),
|
||||
dependencies.secrets.get(PUBLISH_AGENT_ACTIVITY_SECRET),
|
||||
],
|
||||
{ concurrency: 5 },
|
||||
);
|
||||
return {
|
||||
linked: Option.isSome(cloudUserId),
|
||||
cloudUserId: Option.isSome(cloudUserId) ? bytesToString(cloudUserId.value) : null,
|
||||
relayUrl: Option.isSome(relayUrl) ? bytesToString(relayUrl.value) : null,
|
||||
relayIssuer: Option.isSome(relayIssuer) ? bytesToString(relayIssuer.value) : null,
|
||||
// The managed tunnel runtime config is only stored for managed links; a
|
||||
// publish-only link leaves it absent.
|
||||
managedTunnelActive: Option.isSome(endpointRuntimeConfig),
|
||||
publishAgentActivity: Option.isSome(publishAgentActivity)
|
||||
? bytesToString(publishAgentActivity.value) === "true"
|
||||
: false,
|
||||
|
||||
@@ -203,6 +203,26 @@ const makePublishProof = Effect.fn("makePublishProof")(function* (input: {
|
||||
return yield* signRelayAgentActivityPublishProof({ privateKey: input.privateKey, payload });
|
||||
});
|
||||
|
||||
// Compact, log-safe view of the fields the awareness phase ladder reads.
|
||||
export function describeThreadShellForAwareness(
|
||||
thread: Option.Option<OrchestrationThreadShell>,
|
||||
): Record<string, unknown> {
|
||||
if (Option.isNone(thread)) {
|
||||
return { found: false };
|
||||
}
|
||||
const shell = thread.value;
|
||||
return {
|
||||
found: true,
|
||||
sessionStatus: shell.session?.status ?? null,
|
||||
sessionActiveTurnId: shell.session?.activeTurnId ?? null,
|
||||
latestTurnId: shell.latestTurn?.turnId ?? null,
|
||||
latestTurnState: shell.latestTurn?.state ?? null,
|
||||
latestTurnCompletedAt: shell.latestTurn?.completedAt ?? null,
|
||||
hasPendingApprovals: shell.hasPendingApprovals,
|
||||
hasPendingUserInput: shell.hasPendingUserInput,
|
||||
};
|
||||
}
|
||||
|
||||
export function resolveAgentAwarenessRelayPublishSnapshot(input: {
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly threadId: ThreadId;
|
||||
@@ -306,6 +326,14 @@ export const make = Effect.gen(function* () {
|
||||
transformClient: relayEnvironmentClient(relayConfig.environmentCredential),
|
||||
}).pipe(Effect.provide(FetchHttpClient.layer));
|
||||
|
||||
// Deadlines for publishes that need confirmation (tombstones and
|
||||
// first-state completions). The confirming publish is re-enqueued through
|
||||
// the same drainable worker as every other publish, so a confirmed
|
||||
// tombstone can never race an in-flight live update; a recovered state
|
||||
// clears the deadline. Assigned after the worker exists.
|
||||
const publishConfirmDeadlines = new Map<ThreadId, number>();
|
||||
let schedulePublishConfirm: (threadId: ThreadId) => Effect.Effect<void> = () => Effect.void;
|
||||
|
||||
const publishThreadUnsafe = Effect.fn("publishThreadUnsafe")(function* (threadId: ThreadId) {
|
||||
const publishAgentActivity = yield* readPublishAgentActivityEnabled.pipe(
|
||||
Effect.orElseSucceed(() => false),
|
||||
@@ -382,6 +410,11 @@ export const make = Effect.gen(function* () {
|
||||
const publishIdentity = agentAwarenessPublishIdentity(snapshot.state);
|
||||
const publishedStateByThread = yield* Ref.get(publishedStateByThreadRef);
|
||||
if (publishedStateByThread.get(threadId) === publishIdentity) {
|
||||
// The projection is back at (or never left) the last published state, so
|
||||
// any pending deferred confirmation is moot. Leaving the deadline in
|
||||
// place would let a much later transient null find it already expired
|
||||
// and publish a tombstone immediately, skipping the deferral window.
|
||||
publishConfirmDeadlines.delete(threadId);
|
||||
yield* Effect.logDebug("agent activity publish skipped; projected state unchanged", {
|
||||
environmentId,
|
||||
threadId,
|
||||
@@ -390,6 +423,50 @@ export const make = Effect.gen(function* () {
|
||||
return;
|
||||
}
|
||||
|
||||
// Two projections need confirmation before publishing, because both can
|
||||
// appear transiently while the projector is mid-write and publishing them
|
||||
// immediately is destructive or noisy:
|
||||
// - null (tombstone) while the previous published state was live: deletes
|
||||
// the thread from every armed card mid-conversation.
|
||||
// - completed as the thread's FIRST published state: sessions boot at
|
||||
// "ready" before their first turn, which projects as completed for an
|
||||
// instant and sends a spurious Done notification at thread birth.
|
||||
// Defer, schedule a re-publish through the ordinary worker queue, and
|
||||
// only publish if the projection still holds when it drains.
|
||||
const requiresConfirmation =
|
||||
(snapshot.state === null &&
|
||||
publishedStateByThread.get(threadId) !== agentAwarenessPublishIdentity(null)) ||
|
||||
(snapshot.state?.phase === "completed" && !publishedStateByThread.has(threadId));
|
||||
if (requiresConfirmation) {
|
||||
const nowMs = (yield* DateTime.now).epochMilliseconds;
|
||||
const deadline = publishConfirmDeadlines.get(threadId);
|
||||
if (deadline === undefined) {
|
||||
publishConfirmDeadlines.set(threadId, nowMs + 5_000);
|
||||
yield* Effect.logInfo("agent activity publish deferred pending confirmation", {
|
||||
environmentId,
|
||||
threadId,
|
||||
reason: snapshot.reason,
|
||||
statePhase: snapshot.state?.phase ?? null,
|
||||
shell: describeThreadShellForAwareness(thread),
|
||||
});
|
||||
yield* schedulePublishConfirm(threadId);
|
||||
return;
|
||||
}
|
||||
if (nowMs < deadline) {
|
||||
return;
|
||||
}
|
||||
publishConfirmDeadlines.delete(threadId);
|
||||
yield* Effect.logInfo("agent activity deferred publish confirmed", {
|
||||
environmentId,
|
||||
threadId,
|
||||
reason: snapshot.reason,
|
||||
statePhase: snapshot.state?.phase ?? null,
|
||||
shell: describeThreadShellForAwareness(thread),
|
||||
});
|
||||
} else {
|
||||
publishConfirmDeadlines.delete(threadId);
|
||||
}
|
||||
|
||||
if (snapshot.reason === "thread-not-found") {
|
||||
yield* Effect.logDebug("publishing agent activity tombstone; thread not found", {
|
||||
environmentId,
|
||||
@@ -478,6 +555,19 @@ export const make = Effect.gen(function* () {
|
||||
|
||||
const worker = yield* makeDrainableWorker(publishThread);
|
||||
|
||||
schedulePublishConfirm = (threadId) =>
|
||||
Effect.forkDetach(
|
||||
Effect.sleep("5 seconds").pipe(
|
||||
Effect.andThen(worker.enqueue(threadId)),
|
||||
Effect.catchCause((cause) =>
|
||||
Effect.logWarning("deferred agent activity confirmation failed", {
|
||||
threadId,
|
||||
cause: Cause.pretty(cause),
|
||||
}),
|
||||
),
|
||||
),
|
||||
).pipe(Effect.asVoid);
|
||||
|
||||
const start: AgentAwarenessRelay["Service"]["start"] = Effect.fn("AgentAwarenessRelay.start")(
|
||||
function* () {
|
||||
const [relayConfig, publishEnabled] = yield* Effect.all([
|
||||
|
||||
@@ -1700,7 +1700,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
|
||||
);
|
||||
|
||||
it.effect("rejects managed cloud link proofs for manual endpoint providers", () =>
|
||||
it.effect("rejects cloud link proofs for unsupported endpoint providers", () =>
|
||||
Effect.gen(function* () {
|
||||
yield* buildAppUnderTest();
|
||||
|
||||
@@ -1721,7 +1721,8 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
wsBaseUrl: linkProofUrl
|
||||
.replace("http://", "ws://")
|
||||
.replace("/api/connect/link-proof", "/ws"),
|
||||
providerKind: "manual",
|
||||
// "manual" and "cloudflare_tunnel" are supported; "t3_relay" is not.
|
||||
providerKind: "t3_relay",
|
||||
},
|
||||
origin: {
|
||||
localHttpHost: "127.0.0.1",
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import * as Schema from "effect/Schema";
|
||||
|
||||
/**
|
||||
* Accounts that opted out of the post-sign-in T3 Connect onboarding wizard
|
||||
* ("Don't show this again"). The wizard otherwise shows on every sign-in,
|
||||
* since sign-out clears the connected environments.
|
||||
*/
|
||||
export const CONNECT_ONBOARDING_OPT_OUT_STORAGE_KEY = "t3code:connect-onboarding-opt-out:v1";
|
||||
|
||||
export const ConnectOnboardingOptOutSchema = Schema.Struct({
|
||||
optOutAccounts: Schema.Array(Schema.String),
|
||||
});
|
||||
|
||||
export type ConnectOnboardingOptOutState = typeof ConnectOnboardingOptOutSchema.Type;
|
||||
|
||||
export const EMPTY_CONNECT_ONBOARDING_OPT_OUT_STATE: ConnectOnboardingOptOutState = {
|
||||
optOutAccounts: [],
|
||||
};
|
||||
@@ -204,6 +204,7 @@ describe("web cloud link environment client", () => {
|
||||
cloudUserId: "user-1",
|
||||
relayUrl: "https://relay.example.test",
|
||||
relayIssuer: "https://relay.example.test",
|
||||
managedTunnelActive: true,
|
||||
publishAgentActivity: false,
|
||||
}),
|
||||
);
|
||||
@@ -217,6 +218,7 @@ describe("web cloud link environment client", () => {
|
||||
cloudUserId: "user-1",
|
||||
relayUrl: "https://relay.example.test",
|
||||
relayIssuer: "https://relay.example.test",
|
||||
managedTunnelActive: true,
|
||||
publishAgentActivity: false,
|
||||
}),
|
||||
);
|
||||
@@ -234,6 +236,7 @@ describe("web cloud link environment client", () => {
|
||||
cloudUserId: "user-1",
|
||||
relayUrl: "https://relay.example.test",
|
||||
relayIssuer: "https://relay.example.test",
|
||||
managedTunnelActive: true,
|
||||
publishAgentActivity: false,
|
||||
}),
|
||||
);
|
||||
@@ -261,6 +264,7 @@ describe("web cloud link environment client", () => {
|
||||
cloudUserId: "user-1",
|
||||
relayUrl: "https://relay.example.test",
|
||||
relayIssuer: "https://relay.example.test",
|
||||
managedTunnelActive: true,
|
||||
publishAgentActivity: true,
|
||||
}),
|
||||
);
|
||||
@@ -339,6 +343,57 @@ describe("web cloud link environment client", () => {
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("links publish-only without a managed tunnel", () =>
|
||||
Effect.gen(function* () {
|
||||
const fetchMock = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce(
|
||||
Response.json({
|
||||
challenge: "challenge",
|
||||
expiresAt: "2026-06-06T00:05:00.000Z",
|
||||
}),
|
||||
)
|
||||
.mockResolvedValueOnce(Response.json("signed-proof"))
|
||||
.mockResolvedValueOnce(
|
||||
Response.json({
|
||||
ok: true,
|
||||
environmentId: TARGET.environmentId,
|
||||
endpoint: {
|
||||
httpBaseUrl: TARGET.httpBaseUrl,
|
||||
wsBaseUrl: TARGET.wsBaseUrl,
|
||||
providerKind: "manual",
|
||||
},
|
||||
endpointRuntime: null,
|
||||
relayIssuer: "https://relay.example.test",
|
||||
cloudUserId: "user-1",
|
||||
environmentCredential: "environment-credential",
|
||||
cloudMintPublicKey: "public-key",
|
||||
}),
|
||||
)
|
||||
.mockResolvedValueOnce(
|
||||
Response.json({ ok: true, endpointRuntimeStatus: { status: "disabled" } }),
|
||||
);
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
|
||||
yield* withServices(
|
||||
linkPrimaryEnvironmentToCloud({
|
||||
target: TARGET,
|
||||
clerkToken: "clerk-token",
|
||||
mode: "publish_only",
|
||||
}),
|
||||
);
|
||||
|
||||
// @effect-diagnostics-next-line preferSchemaOverJson:off
|
||||
expect(JSON.parse(bodyText(fetchMock.mock.calls[0]?.[1]?.body))).toMatchObject({
|
||||
managedTunnelsEnabled: false,
|
||||
});
|
||||
// @effect-diagnostics-next-line preferSchemaOverJson:off
|
||||
expect(JSON.parse(bodyText(fetchMock.mock.calls[1]?.[1]?.body))).toMatchObject({
|
||||
endpoint: { providerKind: "manual" },
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("installs a missing relay client before linking", () =>
|
||||
Effect.gen(function* () {
|
||||
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(Response.json({ malformed: true })));
|
||||
|
||||
@@ -383,9 +383,17 @@ export function unlinkPrimaryEnvironmentFromCloud(input: {
|
||||
}).pipe(Effect.provide(primaryEnvironmentHttpLayer));
|
||||
}
|
||||
|
||||
// "publish_only" links the environment to the relay for agent-activity
|
||||
// publishing alone: no managed tunnel is provisioned, so it can be toggled
|
||||
// independently of T3 Connect while clients reach the environment out of band.
|
||||
export type CloudLinkMode = "managed" | "publish_only";
|
||||
|
||||
const PUBLISH_ONLY_PROVIDER_KIND = "manual" satisfies RelayManagedEndpointProviderKind;
|
||||
|
||||
export function linkPrimaryEnvironmentToCloud(input: {
|
||||
readonly target: CloudLinkTarget;
|
||||
readonly clerkToken: string;
|
||||
readonly mode?: CloudLinkMode;
|
||||
}): Effect.Effect<
|
||||
void,
|
||||
CloudEnvironmentLinkError,
|
||||
@@ -398,9 +406,15 @@ export function linkPrimaryEnvironmentToCloud(input: {
|
||||
message: "T3CODE_RELAY_URL is not configured.",
|
||||
});
|
||||
}
|
||||
const managedTunnelsEnabled = (input.mode ?? "managed") === "managed";
|
||||
const providerKind = managedTunnelsEnabled
|
||||
? MANAGED_ENDPOINT_PROVIDER_KIND
|
||||
: PUBLISH_ONLY_PROVIDER_KIND;
|
||||
const relayClient = yield* ManagedRelay.ManagedRelayClient;
|
||||
const environmentClient = yield* makeEnvironmentHttpApiClient(input.target.httpBaseUrl);
|
||||
yield* ensureRelayClientAvailable(EnvironmentId.make(input.target.environmentId));
|
||||
if (managedTunnelsEnabled) {
|
||||
yield* ensureRelayClientAvailable(EnvironmentId.make(input.target.environmentId));
|
||||
}
|
||||
|
||||
const challenge = yield* relayClient
|
||||
.createEnvironmentLinkChallenge({
|
||||
@@ -408,7 +422,7 @@ export function linkPrimaryEnvironmentToCloud(input: {
|
||||
payload: {
|
||||
notificationsEnabled: true,
|
||||
liveActivitiesEnabled: true,
|
||||
managedTunnelsEnabled: true,
|
||||
managedTunnelsEnabled,
|
||||
},
|
||||
})
|
||||
.pipe(
|
||||
@@ -427,7 +441,7 @@ export function linkPrimaryEnvironmentToCloud(input: {
|
||||
endpoint: {
|
||||
httpBaseUrl: input.target.httpBaseUrl,
|
||||
wsBaseUrl: input.target.wsBaseUrl,
|
||||
providerKind: MANAGED_ENDPOINT_PROVIDER_KIND,
|
||||
providerKind,
|
||||
},
|
||||
origin: endpointOrigin(input.target.httpBaseUrl),
|
||||
},
|
||||
@@ -440,7 +454,7 @@ export function linkPrimaryEnvironmentToCloud(input: {
|
||||
proof,
|
||||
notificationsEnabled: true,
|
||||
liveActivitiesEnabled: true,
|
||||
managedTunnelsEnabled: true,
|
||||
managedTunnelsEnabled,
|
||||
},
|
||||
})
|
||||
.pipe(
|
||||
@@ -450,7 +464,7 @@ export function linkPrimaryEnvironmentToCloud(input: {
|
||||
);
|
||||
yield* ensureLinkedEnvironmentMatches({
|
||||
expectedEnvironmentId: input.target.environmentId,
|
||||
expectedProviderKind: MANAGED_ENDPOINT_PROVIDER_KIND,
|
||||
expectedProviderKind: providerKind,
|
||||
link,
|
||||
});
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
import { connectionAtomRuntime } from "../connection/runtime";
|
||||
import {
|
||||
linkPrimaryEnvironmentToCloud,
|
||||
type CloudLinkMode,
|
||||
type CloudLinkTarget,
|
||||
unlinkPrimaryEnvironmentFromCloud,
|
||||
updatePrimaryCloudPreferences,
|
||||
@@ -21,8 +22,11 @@ export const linkPrimaryEnvironment = createRuntimeCommand(connectionAtomRuntime
|
||||
label: "web:cloud:link-primary-environment",
|
||||
scheduler: cloudLinkScheduler,
|
||||
concurrency: cloudLinkConcurrency,
|
||||
execute: (input: { readonly target: CloudLinkTarget; readonly clerkToken: string }) =>
|
||||
linkPrimaryEnvironmentToCloud(input),
|
||||
execute: (input: {
|
||||
readonly target: CloudLinkTarget;
|
||||
readonly clerkToken: string;
|
||||
readonly mode?: CloudLinkMode;
|
||||
}) => linkPrimaryEnvironmentToCloud(input),
|
||||
});
|
||||
|
||||
export const unlinkPrimaryEnvironment = createRuntimeCommand(connectionAtomRuntime, {
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
import { useAuth } from "@clerk/react";
|
||||
import { findErrorTraceId } from "@t3tools/client-runtime/errors";
|
||||
import {
|
||||
isAtomCommandInterrupted,
|
||||
settlePromise,
|
||||
squashAtomCommandFailure,
|
||||
} from "@t3tools/client-runtime/state/runtime";
|
||||
import { useState } from "react";
|
||||
|
||||
import { toastManager } from "../components/ui/toast";
|
||||
import { relayEnvironmentDiscovery } from "../state/relay";
|
||||
import { useAtomCommand } from "../state/use-atom-command";
|
||||
import {
|
||||
linkPrimaryEnvironment as linkPrimaryEnvironmentAtom,
|
||||
unlinkPrimaryEnvironment as unlinkPrimaryEnvironmentAtom,
|
||||
updatePrimaryEnvironmentPreferences as updatePrimaryEnvironmentPreferencesAtom,
|
||||
} from "./linkEnvironmentAtoms";
|
||||
import { usePrimaryCloudLinkState } from "./primaryCloudLinkState";
|
||||
import { resolveRelayClerkTokenOptions } from "./publicConfig";
|
||||
|
||||
export interface CloudLinkDesiredState {
|
||||
readonly managedTunnel: boolean;
|
||||
readonly publish: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drives the primary environment's T3 Connect link. T3 Connect (managed
|
||||
* tunnel) and agent-activity publishing are independent capabilities backed by
|
||||
* a single relay link, so consumers express the full desired state and
|
||||
* `reconcileCloudState` applies it: unlink when neither is wanted, otherwise
|
||||
* (re)link with the mode the managed-tunnel bit implies and set the publish
|
||||
* preference. Re-linking only happens when the managed-tunnel mode actually
|
||||
* changes, so flipping publish alone is cheap.
|
||||
*/
|
||||
export function useCloudLinkController() {
|
||||
const { getToken, isSignedIn } = useAuth();
|
||||
const refreshRelayEnvironments = useAtomCommand(relayEnvironmentDiscovery.refresh, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const linkPrimaryEnvironment = useAtomCommand(linkPrimaryEnvironmentAtom, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const unlinkPrimaryEnvironment = useAtomCommand(unlinkPrimaryEnvironmentAtom, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const updatePrimaryEnvironmentPreferences = useAtomCommand(
|
||||
updatePrimaryEnvironmentPreferencesAtom,
|
||||
{ reportFailure: false },
|
||||
);
|
||||
const primaryCloudLinkState = usePrimaryCloudLinkState();
|
||||
const [operationError, setOperationError] = useState<string | null>(null);
|
||||
|
||||
const reportUpdateFailure = (cause: unknown) => {
|
||||
const message = cause instanceof Error ? cause.message : "Could not update T3 Connect access.";
|
||||
const traceId = findErrorTraceId(cause);
|
||||
console.error("[t3-connect] Could not update T3 Connect", { message, traceId, cause });
|
||||
setOperationError(traceId ? `${message} Trace ID: ${traceId}` : message);
|
||||
toastManager.add({
|
||||
type: "error",
|
||||
title: "Could not update T3 Connect",
|
||||
description: message,
|
||||
data: traceId
|
||||
? {
|
||||
secondaryActionProps: {
|
||||
children: "Copy trace ID",
|
||||
onClick: () => void navigator.clipboard?.writeText(traceId),
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
};
|
||||
|
||||
// Older environment servers predate the managedTunnelActive field; for them a
|
||||
// link always implies a managed tunnel, so fall back to `linked`.
|
||||
const managedTunnelActive =
|
||||
primaryCloudLinkState.data?.managedTunnelActive ?? primaryCloudLinkState.data?.linked ?? false;
|
||||
const publishAgentActivity = primaryCloudLinkState.data?.publishAgentActivity ?? false;
|
||||
const linked = primaryCloudLinkState.data?.linked ?? false;
|
||||
|
||||
const reconcileCloudState = async (desired: CloudLinkDesiredState): Promise<boolean> => {
|
||||
setOperationError(null);
|
||||
const target = primaryCloudLinkState.target;
|
||||
if (!target) {
|
||||
reportUpdateFailure(new Error("Local environment is not ready yet."));
|
||||
return false;
|
||||
}
|
||||
const tokenResult = await settlePromise(() => getToken(resolveRelayClerkTokenOptions()));
|
||||
const wantsLink = desired.managedTunnel || desired.publish;
|
||||
|
||||
// A failure after this point may follow a partially applied mutation (e.g.
|
||||
// the link succeeded but the preference update did not), so every exit —
|
||||
// success or failure — refreshes the rendered state to whatever the server
|
||||
// actually holds now.
|
||||
if (!wantsLink) {
|
||||
// Unlink works without a relay token — a failed token read must not
|
||||
// leave the user unable to turn T3 Connect off.
|
||||
const unlinkResult = await unlinkPrimaryEnvironment({
|
||||
target,
|
||||
clerkToken: tokenResult._tag === "Success" ? (tokenResult.value ?? null) : null,
|
||||
});
|
||||
if (unlinkResult._tag === "Failure") {
|
||||
if (!isAtomCommandInterrupted(unlinkResult)) {
|
||||
reportUpdateFailure(squashAtomCommandFailure(unlinkResult));
|
||||
}
|
||||
primaryCloudLinkState.refresh();
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (tokenResult._tag === "Failure") {
|
||||
reportUpdateFailure(squashAtomCommandFailure(tokenResult));
|
||||
return false;
|
||||
}
|
||||
const clerkToken = tokenResult.value;
|
||||
if (!clerkToken) {
|
||||
reportUpdateFailure(new Error("Sign in to T3 Connect before enabling this."));
|
||||
return false;
|
||||
}
|
||||
if (!linked || managedTunnelActive !== desired.managedTunnel) {
|
||||
const linkResult = await linkPrimaryEnvironment({
|
||||
target,
|
||||
clerkToken,
|
||||
mode: desired.managedTunnel ? "managed" : "publish_only",
|
||||
});
|
||||
if (linkResult._tag === "Failure") {
|
||||
if (!isAtomCommandInterrupted(linkResult)) {
|
||||
reportUpdateFailure(squashAtomCommandFailure(linkResult));
|
||||
}
|
||||
primaryCloudLinkState.refresh();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const prefResult = await updatePrimaryEnvironmentPreferences({
|
||||
target,
|
||||
publishAgentActivity: desired.publish,
|
||||
});
|
||||
if (prefResult._tag === "Failure") {
|
||||
if (!isAtomCommandInterrupted(prefResult)) {
|
||||
reportUpdateFailure(squashAtomCommandFailure(prefResult));
|
||||
}
|
||||
primaryCloudLinkState.refresh();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
primaryCloudLinkState.refresh();
|
||||
const refreshResult = await refreshRelayEnvironments();
|
||||
if (refreshResult._tag === "Failure" && !isAtomCommandInterrupted(refreshResult)) {
|
||||
reportUpdateFailure(squashAtomCommandFailure(refreshResult));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
return {
|
||||
isSignedIn,
|
||||
linkState: primaryCloudLinkState,
|
||||
linked,
|
||||
managedTunnelActive,
|
||||
publishAgentActivity,
|
||||
operationError,
|
||||
reconcileCloudState,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { cn } from "~/lib/utils";
|
||||
import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
|
||||
|
||||
type ConnectionStatusDotProps = {
|
||||
tooltipText?: string | null;
|
||||
dotClassName: string;
|
||||
pingClassName?: string | null;
|
||||
};
|
||||
|
||||
export function ConnectionStatusDot({
|
||||
tooltipText,
|
||||
dotClassName,
|
||||
pingClassName,
|
||||
}: ConnectionStatusDotProps) {
|
||||
const dotContent = (
|
||||
<>
|
||||
{pingClassName ? (
|
||||
<span
|
||||
className={cn(
|
||||
"absolute inline-flex h-full w-full animate-ping rounded-full",
|
||||
pingClassName,
|
||||
)}
|
||||
/>
|
||||
) : null}
|
||||
<span className={cn("relative inline-flex size-2 rounded-full", dotClassName)} />
|
||||
</>
|
||||
);
|
||||
|
||||
if (!tooltipText) {
|
||||
return (
|
||||
<span className="relative flex size-3 shrink-0 items-center justify-center">
|
||||
{dotContent}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
const dot = (
|
||||
<button
|
||||
type="button"
|
||||
title={tooltipText}
|
||||
aria-label={tooltipText}
|
||||
className="relative flex size-3 shrink-0 cursor-help items-center justify-center rounded-full outline-hidden"
|
||||
>
|
||||
{dotContent}
|
||||
</button>
|
||||
);
|
||||
|
||||
return (
|
||||
<Tooltip>
|
||||
<TooltipTrigger render={dot} />
|
||||
<TooltipPopup side="top" className="max-w-80 whitespace-pre-wrap leading-tight">
|
||||
{tooltipText}
|
||||
</TooltipPopup>
|
||||
</Tooltip>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
import { findErrorTraceId } from "@t3tools/client-runtime/errors";
|
||||
import {
|
||||
RelayConnectionRegistration,
|
||||
RelayConnectionTarget,
|
||||
} from "@t3tools/client-runtime/connection";
|
||||
import {
|
||||
isAtomCommandInterrupted,
|
||||
squashAtomCommandFailure,
|
||||
} from "@t3tools/client-runtime/state/runtime";
|
||||
import type { EnvironmentId } from "@t3tools/contracts";
|
||||
import type { RelayClientEnvironmentRecord } from "@t3tools/contracts/relay";
|
||||
import * as Option from "effect/Option";
|
||||
import { type ReactNode, useCallback, useEffect, useMemo, useState } from "react";
|
||||
|
||||
import { environmentCatalog } from "~/connection/catalog";
|
||||
import { cn } from "~/lib/utils";
|
||||
import { relayEnvironmentDiscovery } from "~/state/relay";
|
||||
import { useRelayEnvironmentDiscovery } from "~/state/environments";
|
||||
import { useAtomCommand } from "~/state/use-atom-command";
|
||||
import { ConnectionStatusDot } from "../ConnectionStatusDot";
|
||||
import { ITEM_ROW_CLASSNAME, ITEM_ROW_INNER_CLASSNAME } from "../settings/itemRows";
|
||||
import { Button } from "../ui/button";
|
||||
import { Skeleton } from "../ui/skeleton";
|
||||
import { toastManager } from "../ui/toast";
|
||||
|
||||
export function RemoteEnvironmentRowsSkeleton() {
|
||||
return (
|
||||
<div className={ITEM_ROW_CLASSNAME}>
|
||||
<div className={ITEM_ROW_INNER_CLASSNAME}>
|
||||
<div className="min-w-0 flex-1 space-y-2">
|
||||
<Skeleton className="h-4 w-32 rounded-full" />
|
||||
<Skeleton className="h-3 w-20 rounded-full" />
|
||||
</div>
|
||||
<Skeleton className="h-7 w-16 rounded-md" />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The user's T3 Connect environments from relay discovery, each with a
|
||||
* Connect button. The primary environment is always excluded; already-saved
|
||||
* environments are hidden unless `showSavedAsConnected` renders them as
|
||||
* connected instead (used by onboarding, where the full device mesh should be
|
||||
* visible).
|
||||
*/
|
||||
export function CloudEnvironmentConnectRows({
|
||||
primaryEnvironmentId,
|
||||
savedEnvironmentIds,
|
||||
showSavedAsConnected = false,
|
||||
empty = null,
|
||||
}: {
|
||||
readonly primaryEnvironmentId: EnvironmentId | null;
|
||||
readonly savedEnvironmentIds: ReadonlyArray<EnvironmentId>;
|
||||
readonly showSavedAsConnected?: boolean;
|
||||
readonly empty?: ReactNode;
|
||||
}) {
|
||||
const environmentsState = useRelayEnvironmentDiscovery();
|
||||
const registerEnvironment = useAtomCommand(environmentCatalog.register, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const refreshRelayEnvironments = useAtomCommand(relayEnvironmentDiscovery.refresh, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const connectRelayEnvironment = useCallback(
|
||||
(environment: RelayClientEnvironmentRecord) =>
|
||||
registerEnvironment(
|
||||
new RelayConnectionRegistration({
|
||||
target: new RelayConnectionTarget({
|
||||
environmentId: environment.environmentId,
|
||||
label: environment.label,
|
||||
}),
|
||||
}),
|
||||
),
|
||||
[registerEnvironment],
|
||||
);
|
||||
const [connectingEnvironmentId, setConnectingEnvironmentId] = useState<EnvironmentId | null>(
|
||||
null,
|
||||
);
|
||||
const savedIds = useMemo(() => new Set(savedEnvironmentIds), [savedEnvironmentIds]);
|
||||
|
||||
useEffect(() => {
|
||||
void refreshRelayEnvironments();
|
||||
}, [refreshRelayEnvironments]);
|
||||
|
||||
const connectEnvironment = async (environment: RelayClientEnvironmentRecord) => {
|
||||
setConnectingEnvironmentId(environment.environmentId);
|
||||
const result = await connectRelayEnvironment(environment);
|
||||
setConnectingEnvironmentId(null);
|
||||
if (result._tag === "Success") {
|
||||
toastManager.add({
|
||||
type: "success",
|
||||
title: "Environment connected",
|
||||
description: `${environment.label} is available through T3 Connect.`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (isAtomCommandInterrupted(result)) {
|
||||
return;
|
||||
}
|
||||
const cause = squashAtomCommandFailure(result);
|
||||
const message =
|
||||
cause instanceof Error ? cause.message : "Could not connect the T3 Connect environment.";
|
||||
const traceId = findErrorTraceId(cause);
|
||||
console.error("[t3-connect] Could not connect environment", { message, traceId, cause });
|
||||
toastManager.add({
|
||||
type: "error",
|
||||
title: "Could not connect environment",
|
||||
description: message,
|
||||
data: traceId
|
||||
? {
|
||||
secondaryActionProps: {
|
||||
children: "Copy trace ID",
|
||||
onClick: () => void navigator.clipboard?.writeText(traceId),
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
};
|
||||
|
||||
const visibleEnvironments = [...environmentsState.environments.values()].filter(
|
||||
({ environment }) =>
|
||||
environment.environmentId !== primaryEnvironmentId &&
|
||||
(showSavedAsConnected || !savedIds.has(environment.environmentId)),
|
||||
);
|
||||
|
||||
const standalone = showSavedAsConnected || savedEnvironmentIds.length === 0;
|
||||
|
||||
if (
|
||||
standalone &&
|
||||
visibleEnvironments.length === 0 &&
|
||||
environmentsState.refreshing &&
|
||||
environmentsState.environments.size === 0
|
||||
) {
|
||||
return <RemoteEnvironmentRowsSkeleton />;
|
||||
}
|
||||
|
||||
if (standalone && visibleEnvironments.length === 0) {
|
||||
// A failed or offline discovery is not "no environments" — misreporting it
|
||||
// as empty would read as the user's devices having disappeared.
|
||||
const discoveryProblem = environmentsState.offline
|
||||
? "You appear to be offline."
|
||||
: (Option.getOrNull(environmentsState.error)?.message ?? null);
|
||||
if (discoveryProblem !== null && !environmentsState.refreshing) {
|
||||
return (
|
||||
<div className={ITEM_ROW_CLASSNAME}>
|
||||
<p className="text-sm font-medium text-destructive">
|
||||
Could not load T3 Connect environments
|
||||
</p>
|
||||
<p className="mt-1 text-xs text-muted-foreground">{discoveryProblem}</p>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
className="mt-3"
|
||||
onClick={() => void refreshRelayEnvironments()}
|
||||
>
|
||||
Try again
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return empty;
|
||||
}
|
||||
|
||||
return visibleEnvironments.map(({ environment, availability, error }) => {
|
||||
const alreadyConnected = savedIds.has(environment.environmentId);
|
||||
return (
|
||||
<div key={environment.environmentId} className={ITEM_ROW_CLASSNAME}>
|
||||
<div className={ITEM_ROW_INNER_CLASSNAME}>
|
||||
<div className="min-w-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<ConnectionStatusDot
|
||||
dotClassName={
|
||||
availability === "online"
|
||||
? "bg-success"
|
||||
: availability === "error"
|
||||
? "bg-destructive"
|
||||
: availability === "checking"
|
||||
? "bg-warning"
|
||||
: "bg-muted-foreground/35"
|
||||
}
|
||||
pingClassName={availability === "checking" ? "bg-warning/60 duration-2000" : null}
|
||||
tooltipText={
|
||||
availability === "online"
|
||||
? "Relay online"
|
||||
: availability === "offline"
|
||||
? "Relay offline"
|
||||
: availability === "checking"
|
||||
? "Checking relay status"
|
||||
: (Option.getOrNull(error)?.message ?? "Relay status unavailable")
|
||||
}
|
||||
/>
|
||||
<p className="truncate text-sm font-medium">{environment.label}</p>
|
||||
</div>
|
||||
<p
|
||||
className={cn(
|
||||
"mt-1 truncate text-xs",
|
||||
availability === "error" ? "text-destructive" : "text-muted-foreground",
|
||||
)}
|
||||
>
|
||||
{availability === "online"
|
||||
? "Available · Relay online"
|
||||
: availability === "offline"
|
||||
? "Available · Relay offline"
|
||||
: availability === "checking"
|
||||
? "Available · Checking relay status…"
|
||||
: (Option.getOrNull(error)?.message ?? "Available · Relay status unavailable")}
|
||||
</p>
|
||||
</div>
|
||||
{alreadyConnected ? (
|
||||
<Button size="sm" variant="outline" disabled>
|
||||
Connected
|
||||
</Button>
|
||||
) : (
|
||||
<Button
|
||||
size="sm"
|
||||
disabled={connectingEnvironmentId !== null}
|
||||
onClick={() => void connectEnvironment(environment)}
|
||||
>
|
||||
{connectingEnvironmentId === environment.environmentId ? "Connecting…" : "Connect"}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,437 @@
|
||||
import { useAuth } from "@clerk/react";
|
||||
import { AuthAdministrativeScopes, AuthRelayWriteScope } from "@t3tools/contracts";
|
||||
import { CheckIcon } from "lucide-react";
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
|
||||
import {
|
||||
CONNECT_ONBOARDING_OPT_OUT_STORAGE_KEY,
|
||||
ConnectOnboardingOptOutSchema,
|
||||
EMPTY_CONNECT_ONBOARDING_OPT_OUT_STATE,
|
||||
} from "~/cloud/connectOnboarding";
|
||||
import { hasCloudPublicConfig } from "~/cloud/publicConfig";
|
||||
import { useCloudLinkController } from "~/cloud/useCloudLinkController";
|
||||
import { usePrimarySessionState } from "~/environments/primary";
|
||||
import { useLocalStorage } from "~/hooks/useLocalStorage";
|
||||
import { cn } from "~/lib/utils";
|
||||
import { useEnvironments, usePrimaryEnvironment } from "~/state/environments";
|
||||
import { CloudEnvironmentConnectRows } from "./CloudEnvironmentConnectList";
|
||||
import { Button } from "../ui/button";
|
||||
import { Checkbox } from "../ui/checkbox";
|
||||
import {
|
||||
Dialog,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogPanel,
|
||||
DialogPopup,
|
||||
DialogTitle,
|
||||
} from "../ui/dialog";
|
||||
import { Switch } from "../ui/switch";
|
||||
import { toastManager } from "../ui/toast";
|
||||
|
||||
/**
|
||||
* Post-sign-in onboarding wizard for T3 Connect. Opens on every in-session
|
||||
* sign-in — sign-out removes the connected relay environments, so each new
|
||||
* session starts with no devices to reach. It first prompts to publish this
|
||||
* environment (managed tunnel + agent activity, both defaulting on) when the
|
||||
* current session is authorized to manage the relay link, then lists the
|
||||
* account's T3 Connect environments so every device can be connected right
|
||||
* away. A cold load with a restored session does not count as a sign-in.
|
||||
*/
|
||||
export function ConnectOnboardingDialog() {
|
||||
if (!hasCloudPublicConfig()) return null;
|
||||
|
||||
return <ConfiguredConnectOnboardingDialog />;
|
||||
}
|
||||
|
||||
type OnboardingStep = "publish" | "devices";
|
||||
|
||||
function ConfiguredConnectOnboardingDialog() {
|
||||
// Mirrors ManagedRelayAuthProvider: a pending Clerk session must not read as
|
||||
// signed-out, or its later activation would look like a fresh sign-in.
|
||||
const { isLoaded, isSignedIn, userId } = useAuth({ treatPendingAsSignedOut: false });
|
||||
const [optOutState, setOptOutState] = useLocalStorage(
|
||||
CONNECT_ONBOARDING_OPT_OUT_STORAGE_KEY,
|
||||
EMPTY_CONNECT_ONBOARDING_OPT_OUT_STATE,
|
||||
ConnectOnboardingOptOutSchema,
|
||||
);
|
||||
|
||||
const desktopBridge = window.desktopBridge;
|
||||
const primarySessionState = usePrimarySessionState();
|
||||
const currentSessionScopes = desktopBridge
|
||||
? AuthAdministrativeScopes
|
||||
: primarySessionState.data?.authenticated
|
||||
? (primarySessionState.data.scopes ?? null)
|
||||
: null;
|
||||
const canManageRelay = currentSessionScopes?.includes(AuthRelayWriteScope) ?? false;
|
||||
// The publish step is only offered when we know the answer; opening the
|
||||
// wizard before the session state resolves would let the step set change
|
||||
// mid-flight. A failed session read still opens the wizard — it just means
|
||||
// no publish step.
|
||||
const sessionScopesKnown =
|
||||
Boolean(desktopBridge) ||
|
||||
primarySessionState.data !== null ||
|
||||
primarySessionState.error !== null;
|
||||
|
||||
const controller = useCloudLinkController();
|
||||
const showPublishStep = canManageRelay && controller.linkState.target !== null;
|
||||
const steps: ReadonlyArray<OnboardingStep> = showPublishStep
|
||||
? ["publish", "devices"]
|
||||
: ["devices"];
|
||||
|
||||
const [requestedAccount, setRequestedAccount] = useState<string | null>(null);
|
||||
const [openForAccount, setOpenForAccount] = useState<string | null>(null);
|
||||
const [step, setStep] = useState<OnboardingStep>("devices");
|
||||
const [exposeEnvironment, setExposeEnvironment] = useState(true);
|
||||
const [publishAgentActivity, setPublishAgentActivity] = useState(true);
|
||||
const [dontShowAgain, setDontShowAgain] = useState(false);
|
||||
const [isApplying, setIsApplying] = useState(false);
|
||||
const prefilledFromLinkStateRef = useRef(false);
|
||||
const observedAccountRef = useRef<string | null | undefined>(undefined);
|
||||
|
||||
const optOutAccounts = optOutState.optOutAccounts;
|
||||
|
||||
// Every sign-in or account switch that completes during this session
|
||||
// requests the wizard — account transitions clear the connected relay
|
||||
// environments, so each new session starts with no devices to reach. A cold
|
||||
// load observes undefined → account and must not re-prompt.
|
||||
useEffect(() => {
|
||||
if (!isLoaded) return;
|
||||
// A loaded-but-incomplete snapshot (signed in, user id not yet populated)
|
||||
// must not be recorded as signed-out — the next render would then look
|
||||
// like a fresh sign-in on a cold load.
|
||||
if (isSignedIn && !userId) return;
|
||||
const previousAccount = observedAccountRef.current;
|
||||
const nextAccount = isSignedIn && userId ? userId : null;
|
||||
observedAccountRef.current = nextAccount;
|
||||
if (previousAccount !== undefined && previousAccount !== nextAccount && nextAccount !== null) {
|
||||
setRequestedAccount(nextAccount);
|
||||
}
|
||||
}, [isLoaded, isSignedIn, userId]);
|
||||
|
||||
// A manageable session implies a primary environment, so when the scopes
|
||||
// allow publishing, wait for the connection target too — otherwise the
|
||||
// wizard could open on the devices step moments before the publish step
|
||||
// becomes available and freeze there.
|
||||
const publishStepDecided = !canManageRelay || controller.linkState.target !== null;
|
||||
|
||||
// Open once the session scopes resolve so the step set is stable. Accounts
|
||||
// that chose "Don't show this again" are skipped.
|
||||
useEffect(() => {
|
||||
if (requestedAccount === null || openForAccount !== null) return;
|
||||
if (optOutAccounts.includes(requestedAccount)) {
|
||||
setRequestedAccount(null);
|
||||
return;
|
||||
}
|
||||
if (!sessionScopesKnown || !publishStepDecided) return;
|
||||
setRequestedAccount(null);
|
||||
prefilledFromLinkStateRef.current = false;
|
||||
setExposeEnvironment(true);
|
||||
setPublishAgentActivity(true);
|
||||
setDontShowAgain(false);
|
||||
setStep(canManageRelay && controller.linkState.target !== null ? "publish" : "devices");
|
||||
setOpenForAccount(requestedAccount);
|
||||
}, [
|
||||
canManageRelay,
|
||||
controller.linkState.target,
|
||||
openForAccount,
|
||||
optOutAccounts,
|
||||
publishStepDecided,
|
||||
requestedAccount,
|
||||
sessionScopesKnown,
|
||||
]);
|
||||
|
||||
// Signing out (or switching accounts) mid-wizard invalidates everything the
|
||||
// wizard would do — close it and let the sign-in trigger re-evaluate.
|
||||
useEffect(() => {
|
||||
if (openForAccount !== null && (!isSignedIn || userId !== openForAccount)) {
|
||||
setOpenForAccount(null);
|
||||
}
|
||||
if (requestedAccount !== null && (!isSignedIn || userId !== requestedAccount)) {
|
||||
setRequestedAccount(null);
|
||||
}
|
||||
}, [isSignedIn, openForAccount, requestedAccount, userId]);
|
||||
|
||||
// Toggles default on, but an environment that is already linked should show
|
||||
// its actual configuration instead of silently proposing to rewrite it.
|
||||
// Only when the link belongs to the account being onboarded, though — after
|
||||
// an account switch the cached link state can still describe the previous
|
||||
// account's setup.
|
||||
const linkStateData = controller.linkState.data;
|
||||
useEffect(() => {
|
||||
if (openForAccount === null || prefilledFromLinkStateRef.current || linkStateData === null) {
|
||||
return;
|
||||
}
|
||||
prefilledFromLinkStateRef.current = true;
|
||||
if (linkStateData.linked && linkStateData.cloudUserId === openForAccount) {
|
||||
setExposeEnvironment(linkStateData.managedTunnelActive ?? linkStateData.linked);
|
||||
setPublishAgentActivity(linkStateData.publishAgentActivity);
|
||||
}
|
||||
}, [linkStateData, openForAccount]);
|
||||
|
||||
const complete = () => {
|
||||
// Keep the wizard up while a link request is in flight so its outcome
|
||||
// (and any failure) stays visible.
|
||||
if (isApplying) return;
|
||||
const account = openForAccount;
|
||||
setOpenForAccount(null);
|
||||
if (account !== null && dontShowAgain) {
|
||||
setOptOutState((state) =>
|
||||
state.optOutAccounts.includes(account)
|
||||
? state
|
||||
: { optOutAccounts: [...state.optOutAccounts, account] },
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const applyPublishSelection = async () => {
|
||||
// The wizard only ever enables — with both toggles off there is nothing to
|
||||
// apply, and an existing link must not be torn down from onboarding.
|
||||
if (!exposeEnvironment && !publishAgentActivity) {
|
||||
setStep("devices");
|
||||
return;
|
||||
}
|
||||
setIsApplying(true);
|
||||
const ok = await controller.reconcileCloudState({
|
||||
managedTunnel: exposeEnvironment,
|
||||
publish: publishAgentActivity,
|
||||
});
|
||||
setIsApplying(false);
|
||||
if (!ok) return;
|
||||
toastManager.add({
|
||||
type: "success",
|
||||
title: "T3 Connect enabled",
|
||||
description: exposeEnvironment
|
||||
? "This environment is available to your other devices through T3 Connect."
|
||||
: "This environment publishes agent activity to your mobile clients.",
|
||||
});
|
||||
setStep("devices");
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
open={openForAccount !== null}
|
||||
onOpenChange={(open) => {
|
||||
// Keep the dialog up while a link request is in flight so its outcome
|
||||
// (and any failure) stays visible.
|
||||
if (!open && !isApplying) complete();
|
||||
}}
|
||||
>
|
||||
<DialogPopup className="max-w-xl">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Set up T3 Connect</DialogTitle>
|
||||
<DialogDescription>
|
||||
Mesh your devices together — publish this environment and connect the rest, all in one
|
||||
place.
|
||||
</DialogDescription>
|
||||
{steps.length > 1 ? (
|
||||
<OnboardingStepper
|
||||
steps={steps}
|
||||
currentStep={step}
|
||||
disabled={isApplying}
|
||||
onStepSelect={setStep}
|
||||
/>
|
||||
) : null}
|
||||
</DialogHeader>
|
||||
<DialogPanel>
|
||||
{step === "publish" ? (
|
||||
<PublishStep
|
||||
exposeEnvironment={exposeEnvironment}
|
||||
publishAgentActivity={publishAgentActivity}
|
||||
disabled={isApplying}
|
||||
operationError={controller.operationError}
|
||||
onExposeEnvironmentChange={setExposeEnvironment}
|
||||
onPublishAgentActivityChange={setPublishAgentActivity}
|
||||
/>
|
||||
) : (
|
||||
<DevicesStep />
|
||||
)}
|
||||
</DialogPanel>
|
||||
<DialogFooter variant="bare" className="sm:justify-between">
|
||||
<label className="flex cursor-pointer items-center gap-2 self-start text-xs text-muted-foreground sm:self-center">
|
||||
<Checkbox
|
||||
checked={dontShowAgain}
|
||||
onCheckedChange={(checked) => setDontShowAgain(checked === true)}
|
||||
/>
|
||||
Don't show this again
|
||||
</label>
|
||||
<div className="flex flex-col-reverse gap-2 sm:flex-row">
|
||||
{step === "publish" ? (
|
||||
<>
|
||||
<Button variant="ghost" disabled={isApplying} onClick={() => setStep("devices")}>
|
||||
Not now
|
||||
</Button>
|
||||
<Button
|
||||
disabled={
|
||||
isApplying || (controller.linkState.isPending && linkStateData === null)
|
||||
}
|
||||
onClick={() => void applyPublishSelection()}
|
||||
>
|
||||
{isApplying ? "Enabling…" : "Continue"}
|
||||
</Button>
|
||||
</>
|
||||
) : (
|
||||
<Button disabled={isApplying} onClick={complete}>
|
||||
Done
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</DialogFooter>
|
||||
</DialogPopup>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
const STEP_LABELS: Record<OnboardingStep, string> = {
|
||||
publish: "Publish",
|
||||
devices: "Connect devices",
|
||||
};
|
||||
|
||||
function OnboardingStepper({
|
||||
steps,
|
||||
currentStep,
|
||||
disabled,
|
||||
onStepSelect,
|
||||
}: {
|
||||
readonly steps: ReadonlyArray<OnboardingStep>;
|
||||
readonly currentStep: OnboardingStep;
|
||||
readonly disabled: boolean;
|
||||
readonly onStepSelect: (step: OnboardingStep) => void;
|
||||
}) {
|
||||
const currentIndex = steps.indexOf(currentStep);
|
||||
return (
|
||||
<div className="grid grid-cols-2 gap-2">
|
||||
{steps.map((step, index) => (
|
||||
<button
|
||||
key={step}
|
||||
type="button"
|
||||
disabled={disabled}
|
||||
className={cn(
|
||||
"grid min-w-0 grid-cols-[1rem_minmax(0,1fr)] gap-x-2 rounded-lg border px-3 py-2 text-left",
|
||||
index === currentIndex
|
||||
? "border-primary bg-primary/10 ring-1 ring-primary/25"
|
||||
: index < currentIndex
|
||||
? "border-border bg-background"
|
||||
: "border-border bg-muted/40",
|
||||
)}
|
||||
onClick={() => onStepSelect(step)}
|
||||
>
|
||||
<span
|
||||
className={cn(
|
||||
"row-span-2 mt-0.5 grid size-4 place-items-center rounded-full border",
|
||||
index < currentIndex
|
||||
? "border-primary bg-primary text-primary-foreground"
|
||||
: index === currentIndex
|
||||
? "border-primary bg-background"
|
||||
: "border-muted-foreground/35 bg-background",
|
||||
)}
|
||||
aria-hidden
|
||||
>
|
||||
{index < currentIndex ? <CheckIcon className="size-3" /> : null}
|
||||
</span>
|
||||
<span className="text-[10px] font-medium uppercase text-muted-foreground">
|
||||
Step {index + 1}
|
||||
</span>
|
||||
<span className="truncate text-xs font-semibold text-foreground">
|
||||
{STEP_LABELS[step]}
|
||||
</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function PublishStep({
|
||||
exposeEnvironment,
|
||||
publishAgentActivity,
|
||||
disabled,
|
||||
operationError,
|
||||
onExposeEnvironmentChange,
|
||||
onPublishAgentActivityChange,
|
||||
}: {
|
||||
readonly exposeEnvironment: boolean;
|
||||
readonly publishAgentActivity: boolean;
|
||||
readonly disabled: boolean;
|
||||
readonly operationError: string | null;
|
||||
readonly onExposeEnvironmentChange: (enabled: boolean) => void;
|
||||
readonly onPublishAgentActivityChange: (enabled: boolean) => void;
|
||||
}) {
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
<div className="rounded-lg border">
|
||||
<OnboardingToggleRow
|
||||
title="Publish this environment"
|
||||
description="Make this environment available to your other devices through T3 Connect."
|
||||
checked={exposeEnvironment}
|
||||
disabled={disabled}
|
||||
onCheckedChange={onExposeEnvironmentChange}
|
||||
/>
|
||||
<OnboardingToggleRow
|
||||
title="Publish agent activity"
|
||||
description="Send activity from this environment to your mobile clients for push notifications and Live Activities."
|
||||
checked={publishAgentActivity}
|
||||
disabled={disabled}
|
||||
onCheckedChange={onPublishAgentActivityChange}
|
||||
/>
|
||||
</div>
|
||||
{operationError ? <p className="text-xs text-destructive">{operationError}</p> : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function OnboardingToggleRow({
|
||||
title,
|
||||
description,
|
||||
checked,
|
||||
disabled,
|
||||
onCheckedChange,
|
||||
}: {
|
||||
readonly title: string;
|
||||
readonly description: string;
|
||||
readonly checked: boolean;
|
||||
readonly disabled: boolean;
|
||||
readonly onCheckedChange: (enabled: boolean) => void;
|
||||
}) {
|
||||
return (
|
||||
<div className="flex items-start justify-between gap-4 border-t border-border/60 px-4 py-3 first:border-t-0">
|
||||
<div className="min-w-0">
|
||||
<p className="text-sm font-medium">{title}</p>
|
||||
<p className="mt-1 text-xs text-muted-foreground">{description}</p>
|
||||
</div>
|
||||
<Switch
|
||||
aria-label={title}
|
||||
checked={checked}
|
||||
disabled={disabled}
|
||||
onCheckedChange={onCheckedChange}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function DevicesStep() {
|
||||
const { environments } = useEnvironments();
|
||||
const primaryEnvironment = usePrimaryEnvironment();
|
||||
const savedEnvironmentIds = useMemo(
|
||||
() =>
|
||||
environments
|
||||
.filter((environment) => environment.entry.target._tag !== "PrimaryConnectionTarget")
|
||||
.map((environment) => environment.environmentId),
|
||||
[environments],
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="overflow-hidden rounded-lg border">
|
||||
<CloudEnvironmentConnectRows
|
||||
primaryEnvironmentId={primaryEnvironment?.environmentId ?? null}
|
||||
savedEnvironmentIds={savedEnvironmentIds}
|
||||
showSavedAsConnected
|
||||
empty={
|
||||
<p className="px-4 py-6 text-center text-sm text-muted-foreground">
|
||||
No other environments are published to your account yet. Publish one from another device
|
||||
and it will show up here.
|
||||
</p>
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -7,8 +7,7 @@ import {
|
||||
TerminalIcon,
|
||||
TriangleAlertIcon,
|
||||
} from "lucide-react";
|
||||
import { useAuth } from "@clerk/react";
|
||||
import { type ReactNode, memo, useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { type ReactNode, memo, useCallback, useMemo, useState } from "react";
|
||||
import {
|
||||
AuthAccessReadScope,
|
||||
AuthAccessWriteScope,
|
||||
@@ -30,18 +29,11 @@ import {
|
||||
type DesktopWslState,
|
||||
type EnvironmentId,
|
||||
} from "@t3tools/contracts";
|
||||
import {
|
||||
connectionStatusText,
|
||||
RelayConnectionRegistration,
|
||||
RelayConnectionTarget,
|
||||
} from "@t3tools/client-runtime/connection";
|
||||
import { findErrorTraceId } from "@t3tools/client-runtime/errors";
|
||||
import { connectionStatusText } from "@t3tools/client-runtime/connection";
|
||||
import {
|
||||
isAtomCommandInterrupted,
|
||||
settlePromise,
|
||||
squashAtomCommandFailure,
|
||||
} from "@t3tools/client-runtime/state/runtime";
|
||||
import type { RelayClientEnvironmentRecord } from "@t3tools/contracts/relay";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import * as Option from "effect/Option";
|
||||
|
||||
@@ -50,7 +42,6 @@ import { cn } from "../../lib/utils";
|
||||
import { formatElapsedDurationLabel, formatExpiresInLabel } from "../../timestampFormat";
|
||||
import { resolveDesktopPairingUrl, resolveHostedPairingUrl } from "./pairingUrls";
|
||||
import { applyWslEnableSelection } from "./ConnectionsSettings.logic";
|
||||
import { resolveRelayClerkTokenOptions } from "../../cloud/publicConfig";
|
||||
import {
|
||||
SettingsPageContainer,
|
||||
SettingsRow,
|
||||
@@ -82,7 +73,6 @@ import {
|
||||
} from "../ui/alert-dialog";
|
||||
import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
|
||||
import { QRCodeSvg } from "../ui/qr-code";
|
||||
import { Skeleton } from "../ui/skeleton";
|
||||
import { Spinner } from "../ui/spinner";
|
||||
import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
|
||||
import { Switch } from "../ui/switch";
|
||||
@@ -117,13 +107,8 @@ import {
|
||||
import { isDesktopLocalConnectionTarget } from "~/connection/desktopLocal";
|
||||
import { useUiStateStore } from "~/uiStateStore";
|
||||
import { resolveServerConfigVersionMismatch } from "~/versionSkew";
|
||||
import { usePrimaryCloudLinkState } from "~/cloud/primaryCloudLinkState";
|
||||
import { hasCloudPublicConfig } from "~/cloud/publicConfig";
|
||||
import {
|
||||
linkPrimaryEnvironment as linkPrimaryEnvironmentAtom,
|
||||
unlinkPrimaryEnvironment as unlinkPrimaryEnvironmentAtom,
|
||||
updatePrimaryEnvironmentPreferences as updatePrimaryEnvironmentPreferencesAtom,
|
||||
} from "~/cloud/linkEnvironmentAtoms";
|
||||
import { useCloudLinkController } from "~/cloud/useCloudLinkController";
|
||||
import { authEnvironment } from "~/state/auth";
|
||||
import { environmentCatalog } from "~/connection/catalog";
|
||||
import {
|
||||
@@ -141,10 +126,11 @@ import {
|
||||
type EnvironmentPresentation,
|
||||
useEnvironments,
|
||||
usePrimaryEnvironment,
|
||||
useRelayEnvironmentDiscovery,
|
||||
} from "~/state/environments";
|
||||
import { relayEnvironmentDiscovery } from "~/state/relay";
|
||||
import { useAtomCommand } from "../../state/use-atom-command";
|
||||
import { ConnectionStatusDot } from "../ConnectionStatusDot";
|
||||
import { CloudEnvironmentConnectRows } from "../cloud/CloudEnvironmentConnectList";
|
||||
import { ITEM_ROW_CLASSNAME, ITEM_ROW_INNER_CLASSNAME } from "./itemRows";
|
||||
|
||||
const DEFAULT_TAILSCALE_SERVE_PORT = 443;
|
||||
const EMPTY_ADVERTISED_ENDPOINTS: ReadonlyArray<AdvertisedEndpoint> = [];
|
||||
@@ -260,60 +246,6 @@ function AccessScopeSummary({
|
||||
);
|
||||
}
|
||||
|
||||
type ConnectionStatusDotProps = {
|
||||
tooltipText?: string | null;
|
||||
dotClassName: string;
|
||||
pingClassName?: string | null;
|
||||
};
|
||||
|
||||
function ConnectionStatusDot({
|
||||
tooltipText,
|
||||
dotClassName,
|
||||
pingClassName,
|
||||
}: ConnectionStatusDotProps) {
|
||||
const dotContent = (
|
||||
<>
|
||||
{pingClassName ? (
|
||||
<span
|
||||
className={cn(
|
||||
"absolute inline-flex h-full w-full animate-ping rounded-full",
|
||||
pingClassName,
|
||||
)}
|
||||
/>
|
||||
) : null}
|
||||
<span className={cn("relative inline-flex size-2 rounded-full", dotClassName)} />
|
||||
</>
|
||||
);
|
||||
|
||||
if (!tooltipText) {
|
||||
return (
|
||||
<span className="relative flex size-3 shrink-0 items-center justify-center">
|
||||
{dotContent}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
const dot = (
|
||||
<button
|
||||
type="button"
|
||||
title={tooltipText}
|
||||
aria-label={tooltipText}
|
||||
className="relative flex size-3 shrink-0 cursor-help items-center justify-center rounded-full outline-hidden"
|
||||
>
|
||||
{dotContent}
|
||||
</button>
|
||||
);
|
||||
|
||||
return (
|
||||
<Tooltip>
|
||||
<TooltipTrigger render={dot} />
|
||||
<TooltipPopup side="top" className="max-w-80 whitespace-pre-wrap leading-tight">
|
||||
{tooltipText}
|
||||
</TooltipPopup>
|
||||
</Tooltip>
|
||||
);
|
||||
}
|
||||
|
||||
function formatDesktopSshTarget(target: DesktopSshEnvironmentTarget): string {
|
||||
const authority = target.username ? `${target.username}@${target.hostname}` : target.hostname;
|
||||
return target.port ? `${authority}:${target.port}` : authority;
|
||||
@@ -437,13 +369,8 @@ function formatDesktopSshConnectionError(error: unknown): string {
|
||||
return withoutTaggedErrorPrefix.trim() || fallback;
|
||||
}
|
||||
|
||||
/** Direct row in the card – same pattern as the Provider / ACP-agent list rows. */
|
||||
const ITEM_ROW_CLASSNAME = "border-t border-border/60 px-4 py-4 first:border-t-0 sm:px-5";
|
||||
const ENDPOINT_ROW_CLASSNAME = "border-t border-border/60 px-4 py-2.5 first:border-t-0 sm:px-5";
|
||||
|
||||
const ITEM_ROW_INNER_CLASSNAME =
|
||||
"flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between";
|
||||
|
||||
type AccessSectionPresentation = "current" | "endpoint-rail";
|
||||
|
||||
function accessRowClassName(_presentation: AccessSectionPresentation) {
|
||||
@@ -1602,15 +1529,17 @@ function CloudLinkSwitch({
|
||||
disabled,
|
||||
disabledReason,
|
||||
onCheckedChange,
|
||||
ariaLabel = "Enable T3 Connect",
|
||||
}: {
|
||||
readonly checked: boolean;
|
||||
readonly disabled: boolean;
|
||||
readonly disabledReason: string | null;
|
||||
readonly onCheckedChange?: (enabled: boolean) => void;
|
||||
readonly ariaLabel?: string;
|
||||
}) {
|
||||
const control = (
|
||||
<Switch
|
||||
aria-label="Enable T3 Connect"
|
||||
aria-label={ariaLabel}
|
||||
checked={checked}
|
||||
disabled={disabled}
|
||||
{...(onCheckedChange ? { onCheckedChange } : {})}
|
||||
@@ -1627,185 +1556,94 @@ function CloudLinkSwitch({
|
||||
}
|
||||
|
||||
function ConfiguredCloudLinkRow({ canManageRelay }: { readonly canManageRelay: boolean }) {
|
||||
const { getToken, isSignedIn } = useAuth();
|
||||
const refreshRelayEnvironments = useAtomCommand(relayEnvironmentDiscovery.refresh, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const linkPrimaryEnvironment = useAtomCommand(linkPrimaryEnvironmentAtom, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const unlinkPrimaryEnvironment = useAtomCommand(unlinkPrimaryEnvironmentAtom, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const updatePrimaryEnvironmentPreferences = useAtomCommand(
|
||||
updatePrimaryEnvironmentPreferencesAtom,
|
||||
{ reportFailure: false },
|
||||
);
|
||||
const primaryCloudLinkState = usePrimaryCloudLinkState();
|
||||
const [operationError, setOperationError] = useState<string | null>(null);
|
||||
const {
|
||||
isSignedIn,
|
||||
linkState: primaryCloudLinkState,
|
||||
managedTunnelActive,
|
||||
publishAgentActivity,
|
||||
operationError,
|
||||
reconcileCloudState,
|
||||
} = useCloudLinkController();
|
||||
const [isUpdating, setIsUpdating] = useState(false);
|
||||
const [isUpdatingPreference, setIsUpdatingPreference] = useState(false);
|
||||
|
||||
const reportUpdateFailure = (cause: unknown) => {
|
||||
const message = cause instanceof Error ? cause.message : "Could not update T3 Connect access.";
|
||||
const traceId = findErrorTraceId(cause);
|
||||
console.error("[t3-connect] Could not update T3 Connect", { message, traceId, cause });
|
||||
setOperationError(traceId ? `${message} Trace ID: ${traceId}` : message);
|
||||
toastManager.add({
|
||||
type: "error",
|
||||
title: "Could not update T3 Connect",
|
||||
description: message,
|
||||
data: traceId
|
||||
? {
|
||||
secondaryActionProps: {
|
||||
children: "Copy trace ID",
|
||||
onClick: () => void navigator.clipboard?.writeText(traceId),
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
};
|
||||
|
||||
const updateLink = async (enabled: boolean) => {
|
||||
setIsUpdating(true);
|
||||
setOperationError(null);
|
||||
const tokenResult = await settlePromise(() => getToken(resolveRelayClerkTokenOptions()));
|
||||
if (tokenResult._tag === "Failure") {
|
||||
reportUpdateFailure(squashAtomCommandFailure(tokenResult));
|
||||
setIsUpdating(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const target = primaryCloudLinkState.target;
|
||||
if (!target) {
|
||||
reportUpdateFailure(new Error("Local environment is not ready yet."));
|
||||
setIsUpdating(false);
|
||||
return;
|
||||
}
|
||||
if (enabled && !tokenResult.value) {
|
||||
reportUpdateFailure(new Error("Sign in to T3 Connect before linking this environment."));
|
||||
setIsUpdating(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const linkResult =
|
||||
enabled && tokenResult.value
|
||||
? await linkPrimaryEnvironment({
|
||||
target,
|
||||
clerkToken: tokenResult.value,
|
||||
})
|
||||
: await unlinkPrimaryEnvironment({
|
||||
target,
|
||||
clerkToken: tokenResult.value ?? null,
|
||||
});
|
||||
if (linkResult._tag === "Failure") {
|
||||
if (!isAtomCommandInterrupted(linkResult)) {
|
||||
reportUpdateFailure(squashAtomCommandFailure(linkResult));
|
||||
}
|
||||
setIsUpdating(false);
|
||||
return;
|
||||
}
|
||||
|
||||
primaryCloudLinkState.refresh();
|
||||
const refreshResult = await refreshRelayEnvironments();
|
||||
if (refreshResult._tag === "Failure") {
|
||||
if (!isAtomCommandInterrupted(refreshResult)) {
|
||||
reportUpdateFailure(squashAtomCommandFailure(refreshResult));
|
||||
}
|
||||
setIsUpdating(false);
|
||||
return;
|
||||
}
|
||||
|
||||
toastManager.add({
|
||||
type: "success",
|
||||
title: enabled ? "T3 Connect linked" : "T3 Connect unlinked",
|
||||
description: enabled
|
||||
? "This environment is available through T3 Connect."
|
||||
: "This environment is no longer available through T3 Connect.",
|
||||
});
|
||||
setIsUpdating(false);
|
||||
};
|
||||
|
||||
const updatePublishAgentActivity = async (enabled: boolean) => {
|
||||
const target = primaryCloudLinkState.target;
|
||||
if (!target) {
|
||||
reportUpdateFailure(new Error("Local environment is not ready yet."));
|
||||
return;
|
||||
}
|
||||
|
||||
setIsUpdatingPreference(true);
|
||||
setOperationError(null);
|
||||
const updateResult = await updatePrimaryEnvironmentPreferences({
|
||||
target,
|
||||
publishAgentActivity: enabled,
|
||||
});
|
||||
if (updateResult._tag === "Failure") {
|
||||
if (!isAtomCommandInterrupted(updateResult)) {
|
||||
reportUpdateFailure(squashAtomCommandFailure(updateResult));
|
||||
}
|
||||
setIsUpdatingPreference(false);
|
||||
return;
|
||||
}
|
||||
|
||||
primaryCloudLinkState.refresh();
|
||||
toastManager.add({
|
||||
type: "success",
|
||||
title: enabled ? "Agent activity enabled" : "Agent activity disabled",
|
||||
description: enabled
|
||||
? "This environment can publish agent activity to your mobile clients."
|
||||
: "This environment will stop publishing agent activity.",
|
||||
});
|
||||
setIsUpdatingPreference(false);
|
||||
};
|
||||
const disabledReason = !isSignedIn
|
||||
? "Sign in to T3 Connect to manage this environment."
|
||||
: !canManageRelay
|
||||
? "Your session does not have permission to manage T3 Connect access."
|
||||
: null;
|
||||
const linked = primaryCloudLinkState.data?.linked ?? false;
|
||||
const isBusy = isUpdating || isUpdatingPreference;
|
||||
|
||||
const updateManagedTunnel = async (enabled: boolean) => {
|
||||
setIsUpdating(true);
|
||||
const ok = await reconcileCloudState({ managedTunnel: enabled, publish: publishAgentActivity });
|
||||
if (ok) {
|
||||
// Turning the tunnel off while publishing stays on downgrades the link
|
||||
// rather than removing it — say so instead of claiming an unlink.
|
||||
toastManager.add({
|
||||
type: "success",
|
||||
title: enabled
|
||||
? "T3 Connect linked"
|
||||
: publishAgentActivity
|
||||
? "T3 Connect tunnel disabled"
|
||||
: "T3 Connect unlinked",
|
||||
description: enabled
|
||||
? "This environment is available through T3 Connect."
|
||||
: publishAgentActivity
|
||||
? "The managed tunnel was removed. Agent activity publishing stays on."
|
||||
: "This environment is no longer available through T3 Connect.",
|
||||
});
|
||||
}
|
||||
setIsUpdating(false);
|
||||
};
|
||||
|
||||
const updatePublishAgentActivity = async (enabled: boolean) => {
|
||||
setIsUpdatingPreference(true);
|
||||
const ok = await reconcileCloudState({ managedTunnel: managedTunnelActive, publish: enabled });
|
||||
if (ok) {
|
||||
toastManager.add({
|
||||
type: "success",
|
||||
title: enabled ? "Agent activity enabled" : "Agent activity disabled",
|
||||
description: enabled
|
||||
? "This environment publishes agent activity to your mobile clients."
|
||||
: "This environment will stop publishing agent activity.",
|
||||
});
|
||||
}
|
||||
setIsUpdatingPreference(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<SettingsRow
|
||||
title="T3 Connect"
|
||||
description={
|
||||
linked
|
||||
managedTunnelActive
|
||||
? "This environment is available to your other devices through T3 Connect."
|
||||
: "Make this environment available to your other devices through T3 Connect."
|
||||
}
|
||||
status={operationError ?? primaryCloudLinkState.error}
|
||||
control={
|
||||
<CloudLinkSwitch
|
||||
checked={linked}
|
||||
disabled={
|
||||
!canManageRelay || !isSignedIn || primaryCloudLinkState.isPending || isUpdating
|
||||
}
|
||||
checked={managedTunnelActive}
|
||||
disabled={!canManageRelay || !isSignedIn || primaryCloudLinkState.isPending || isBusy}
|
||||
disabledReason={disabledReason}
|
||||
onCheckedChange={(enabled) => void updateLink(enabled)}
|
||||
onCheckedChange={(enabled) => void updateManagedTunnel(enabled)}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
<SettingsRow
|
||||
title="Publish agent activity"
|
||||
description="Send activity from this environment to your mobile clients for push notifications and Live Activities. Works without a T3 Connect tunnel."
|
||||
control={
|
||||
<CloudLinkSwitch
|
||||
ariaLabel="Publish agent activity to mobile clients"
|
||||
checked={publishAgentActivity}
|
||||
disabled={!canManageRelay || !isSignedIn || primaryCloudLinkState.isPending || isBusy}
|
||||
disabledReason={disabledReason}
|
||||
onCheckedChange={(enabled) => void updatePublishAgentActivity(enabled)}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
{linked ? (
|
||||
<SettingsRow
|
||||
title="Publish agent activity"
|
||||
description="Send activity from this environment to your mobile clients for push notifications and Live Activities."
|
||||
className="bg-muted/20 pl-7 sm:pl-8"
|
||||
control={
|
||||
<Switch
|
||||
aria-label="Publish agent activity to mobile clients"
|
||||
checked={primaryCloudLinkState.data?.publishAgentActivity ?? false}
|
||||
disabled={
|
||||
!canManageRelay ||
|
||||
!isSignedIn ||
|
||||
primaryCloudLinkState.isPending ||
|
||||
isUpdating ||
|
||||
isUpdatingPreference
|
||||
}
|
||||
onCheckedChange={(enabled) => void updatePublishAgentActivity(enabled)}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
) : null}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -1832,163 +1670,6 @@ function EmptyRemoteEnvironments({ cloudEnabled = true }: { readonly cloudEnable
|
||||
);
|
||||
}
|
||||
|
||||
function RemoteEnvironmentRowsSkeleton() {
|
||||
return (
|
||||
<div className={ITEM_ROW_CLASSNAME}>
|
||||
<div className={ITEM_ROW_INNER_CLASSNAME}>
|
||||
<div className="min-w-0 flex-1 space-y-2">
|
||||
<Skeleton className="h-4 w-32 rounded-full" />
|
||||
<Skeleton className="h-3 w-20 rounded-full" />
|
||||
</div>
|
||||
<Skeleton className="h-7 w-16 rounded-md" />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function ConfiguredCloudRemoteEnvironmentRows({
|
||||
primaryEnvironmentId,
|
||||
savedEnvironmentIds,
|
||||
}: {
|
||||
readonly primaryEnvironmentId: EnvironmentId | null;
|
||||
readonly savedEnvironmentIds: ReadonlyArray<EnvironmentId>;
|
||||
}) {
|
||||
const environmentsState = useRelayEnvironmentDiscovery();
|
||||
const registerEnvironment = useAtomCommand(environmentCatalog.register, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const refreshRelayEnvironments = useAtomCommand(relayEnvironmentDiscovery.refresh, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const connectRelayEnvironment = useCallback(
|
||||
(environment: RelayClientEnvironmentRecord) =>
|
||||
registerEnvironment(
|
||||
new RelayConnectionRegistration({
|
||||
target: new RelayConnectionTarget({
|
||||
environmentId: environment.environmentId,
|
||||
label: environment.label,
|
||||
}),
|
||||
}),
|
||||
),
|
||||
[registerEnvironment],
|
||||
);
|
||||
const [connectingEnvironmentId, setConnectingEnvironmentId] = useState<EnvironmentId | null>(
|
||||
null,
|
||||
);
|
||||
const savedIds = useMemo(() => new Set(savedEnvironmentIds), [savedEnvironmentIds]);
|
||||
|
||||
useEffect(() => {
|
||||
void refreshRelayEnvironments();
|
||||
}, [refreshRelayEnvironments]);
|
||||
|
||||
const connectEnvironment = async (environment: RelayClientEnvironmentRecord) => {
|
||||
setConnectingEnvironmentId(environment.environmentId);
|
||||
const result = await connectRelayEnvironment(environment);
|
||||
setConnectingEnvironmentId(null);
|
||||
if (result._tag === "Success") {
|
||||
toastManager.add({
|
||||
type: "success",
|
||||
title: "Environment connected",
|
||||
description: `${environment.label} is available through T3 Connect.`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (isAtomCommandInterrupted(result)) {
|
||||
return;
|
||||
}
|
||||
const cause = squashAtomCommandFailure(result);
|
||||
const message =
|
||||
cause instanceof Error ? cause.message : "Could not connect the T3 Connect environment.";
|
||||
const traceId = findErrorTraceId(cause);
|
||||
console.error("[t3-connect] Could not connect environment", { message, traceId, cause });
|
||||
toastManager.add({
|
||||
type: "error",
|
||||
title: "Could not connect environment",
|
||||
description: message,
|
||||
data: traceId
|
||||
? {
|
||||
secondaryActionProps: {
|
||||
children: "Copy trace ID",
|
||||
onClick: () => void navigator.clipboard?.writeText(traceId),
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
};
|
||||
|
||||
const connectableEnvironments = [...environmentsState.environments.values()].filter(
|
||||
({ environment }) =>
|
||||
environment.environmentId !== primaryEnvironmentId &&
|
||||
!savedIds.has(environment.environmentId),
|
||||
);
|
||||
|
||||
if (
|
||||
savedEnvironmentIds.length === 0 &&
|
||||
environmentsState.refreshing &&
|
||||
environmentsState.environments.size === 0
|
||||
) {
|
||||
return <RemoteEnvironmentRowsSkeleton />;
|
||||
}
|
||||
|
||||
if (savedEnvironmentIds.length === 0 && connectableEnvironments.length === 0) {
|
||||
return <EmptyRemoteEnvironments />;
|
||||
}
|
||||
|
||||
return connectableEnvironments.map(({ environment, availability, error }) => (
|
||||
<div key={environment.environmentId} className={ITEM_ROW_CLASSNAME}>
|
||||
<div className={ITEM_ROW_INNER_CLASSNAME}>
|
||||
<div className="min-w-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<ConnectionStatusDot
|
||||
dotClassName={
|
||||
availability === "online"
|
||||
? "bg-success"
|
||||
: availability === "error"
|
||||
? "bg-destructive"
|
||||
: availability === "checking"
|
||||
? "bg-warning"
|
||||
: "bg-muted-foreground/35"
|
||||
}
|
||||
pingClassName={availability === "checking" ? "bg-warning/60 duration-2000" : null}
|
||||
tooltipText={
|
||||
availability === "online"
|
||||
? "Relay online"
|
||||
: availability === "offline"
|
||||
? "Relay offline"
|
||||
: availability === "checking"
|
||||
? "Checking relay status"
|
||||
: (Option.getOrNull(error)?.message ?? "Relay status unavailable")
|
||||
}
|
||||
/>
|
||||
<p className="truncate text-sm font-medium">{environment.label}</p>
|
||||
</div>
|
||||
<p
|
||||
className={cn(
|
||||
"mt-1 truncate text-xs",
|
||||
availability === "error" ? "text-destructive" : "text-muted-foreground",
|
||||
)}
|
||||
>
|
||||
{availability === "online"
|
||||
? "Available · Relay online"
|
||||
: availability === "offline"
|
||||
? "Available · Relay offline"
|
||||
: availability === "checking"
|
||||
? "Available · Checking relay status…"
|
||||
: (Option.getOrNull(error)?.message ?? "Available · Relay status unavailable")}
|
||||
</p>
|
||||
</div>
|
||||
<Button
|
||||
size="sm"
|
||||
disabled={connectingEnvironmentId !== null}
|
||||
onClick={() => void connectEnvironment(environment)}
|
||||
>
|
||||
{connectingEnvironmentId === environment.environmentId ? "Connecting…" : "Connect"}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
));
|
||||
}
|
||||
|
||||
function CloudRemoteEnvironmentRows({
|
||||
primaryEnvironmentId,
|
||||
savedEnvironmentIds,
|
||||
@@ -1997,9 +1678,10 @@ function CloudRemoteEnvironmentRows({
|
||||
readonly savedEnvironmentIds: ReadonlyArray<EnvironmentId>;
|
||||
}) {
|
||||
return hasCloudPublicConfig() ? (
|
||||
<ConfiguredCloudRemoteEnvironmentRows
|
||||
<CloudEnvironmentConnectRows
|
||||
primaryEnvironmentId={primaryEnvironmentId}
|
||||
savedEnvironmentIds={savedEnvironmentIds}
|
||||
empty={<EmptyRemoteEnvironments />}
|
||||
/>
|
||||
) : savedEnvironmentIds.length === 0 ? (
|
||||
<EmptyRemoteEnvironments cloudEnabled={false} />
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
/** Direct row in the card – same pattern as the Provider / ACP-agent list rows. */
|
||||
export const ITEM_ROW_CLASSNAME = "border-t border-border/60 px-4 py-4 first:border-t-0 sm:px-5";
|
||||
|
||||
export const ITEM_ROW_INNER_CLASSNAME =
|
||||
"flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between";
|
||||
@@ -2,7 +2,7 @@ import { useNavigate } from "@tanstack/react-router";
|
||||
import { useAtomValue } from "@effect/atom-react";
|
||||
import type { ServerProvider } from "@t3tools/contracts";
|
||||
import { CircleCheckIcon, DownloadIcon, LoaderIcon, TriangleAlertIcon, XIcon } from "lucide-react";
|
||||
import { useCallback, useEffect, useState, type CSSProperties } from "react";
|
||||
import { useCallback, useEffect, useRef, useState, type CSSProperties } from "react";
|
||||
|
||||
import { primaryServerProvidersAtom } from "../../state/server";
|
||||
import {
|
||||
@@ -28,62 +28,19 @@ const PROVIDER_UPDATE_PILL_PROGRESS_STYLES = {
|
||||
error: "bg-destructive/14",
|
||||
} as const;
|
||||
|
||||
function latestProviderCheckedAt(
|
||||
providers: ReadonlyArray<Pick<ServerProvider, "checkedAt">>,
|
||||
): string | undefined {
|
||||
return providers.reduce<string | undefined>(
|
||||
(latest, provider) =>
|
||||
latest === undefined || provider.checkedAt > latest ? provider.checkedAt : latest,
|
||||
undefined,
|
||||
);
|
||||
function useMountedIsoTimestamp(): string {
|
||||
const [mountedIsoTimestamp] = useState(() => new Date().toISOString());
|
||||
return mountedIsoTimestamp;
|
||||
}
|
||||
|
||||
export function SidebarProviderUpdatePill() {
|
||||
const navigate = useNavigate();
|
||||
const providers = useAtomValue(primaryServerProvidersAtom);
|
||||
const [dismissedKeys, setDismissedKeys] = useState<ReadonlySet<string>>(() => new Set());
|
||||
const [renderedView, setRenderedView] = useState<ProviderUpdateSidebarPillView | null>(null);
|
||||
const [pendingView, setPendingView] = useState<ProviderUpdateSidebarPillView | null>(null);
|
||||
const [exitingKey, setExitingKey] = useState<string | null>(null);
|
||||
const [dismissAfterExitKey, setDismissAfterExitKey] = useState<string | null>(null);
|
||||
const [visibleAfterIso, setVisibleAfterIso] = useState<string | undefined>();
|
||||
const effectiveVisibleAfterIso = visibleAfterIso ?? latestProviderCheckedAt(providers);
|
||||
const view = getProviderUpdateSidebarPillView(providers, {
|
||||
...(effectiveVisibleAfterIso !== undefined
|
||||
? { visibleAfterIso: effectiveVisibleAfterIso }
|
||||
: {}),
|
||||
dismissedKeys,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (visibleAfterIso === undefined && effectiveVisibleAfterIso !== undefined) {
|
||||
setVisibleAfterIso(effectiveVisibleAfterIso);
|
||||
}
|
||||
}, [effectiveVisibleAfterIso, visibleAfterIso]);
|
||||
|
||||
const openProviderSettings = useCallback(() => {
|
||||
void navigate({ to: "/settings/providers" });
|
||||
}, [navigate]);
|
||||
const displayedView = renderedView ?? view;
|
||||
const dismissAfterVisibleMs = displayedView?.dismissAfterVisibleMs;
|
||||
const viewKey = displayedView?.key ?? null;
|
||||
const showDismissProgress =
|
||||
dismissAfterVisibleMs !== undefined &&
|
||||
displayedView?.tone !== "loading" &&
|
||||
exitingKey !== viewKey;
|
||||
|
||||
const startExit = useCallback(
|
||||
(key: string, nextView: ProviderUpdateSidebarPillView | null, dismissKey?: string) => {
|
||||
if (exitingKey === key) {
|
||||
return;
|
||||
}
|
||||
setPendingView(nextView);
|
||||
setExitingKey(key);
|
||||
setDismissAfterExitKey(dismissKey ?? null);
|
||||
},
|
||||
[exitingKey],
|
||||
);
|
||||
|
||||
function useProviderUpdateRenderedViewSynchronization(input: {
|
||||
readonly exitingKey: string | null;
|
||||
readonly renderedView: ProviderUpdateSidebarPillView | null;
|
||||
readonly setRenderedView: (view: ProviderUpdateSidebarPillView | null) => void;
|
||||
readonly startExit: (key: string, nextView: ProviderUpdateSidebarPillView | null) => void;
|
||||
readonly view: ProviderUpdateSidebarPillView | null;
|
||||
}) {
|
||||
const { exitingKey, renderedView, setRenderedView, startExit, view } = input;
|
||||
useEffect(() => {
|
||||
if (exitingKey !== null) {
|
||||
return;
|
||||
@@ -102,8 +59,20 @@ export function SidebarProviderUpdatePill() {
|
||||
startExit(renderedView.key, view);
|
||||
return;
|
||||
}
|
||||
}, [exitingKey, renderedView, startExit, view]);
|
||||
}, [exitingKey, renderedView, setRenderedView, startExit, view]);
|
||||
}
|
||||
|
||||
function useProviderUpdateAutoDismiss(input: {
|
||||
readonly dismissAfterVisibleMs: number | undefined;
|
||||
readonly exitingKey: string | null;
|
||||
readonly startExit: (
|
||||
key: string,
|
||||
nextView: ProviderUpdateSidebarPillView | null,
|
||||
dismissKey?: string,
|
||||
) => void;
|
||||
readonly viewKey: string | null;
|
||||
}) {
|
||||
const { dismissAfterVisibleMs, exitingKey, startExit, viewKey } = input;
|
||||
useEffect(() => {
|
||||
if (!dismissAfterVisibleMs || !viewKey) {
|
||||
return;
|
||||
@@ -117,6 +86,60 @@ export function SidebarProviderUpdatePill() {
|
||||
|
||||
return () => window.clearTimeout(timeoutId);
|
||||
}, [dismissAfterVisibleMs, exitingKey, startExit, viewKey]);
|
||||
}
|
||||
|
||||
export function SidebarProviderUpdatePill() {
|
||||
const navigate = useNavigate();
|
||||
const providers = useAtomValue(primaryServerProvidersAtom);
|
||||
const [dismissedKeys, setDismissedKeys] = useState<ReadonlySet<string>>(() => new Set());
|
||||
const visibleAfterIso = useMountedIsoTimestamp();
|
||||
const view = getProviderUpdateSidebarPillView(providers, {
|
||||
visibleAfterIso,
|
||||
dismissedKeys,
|
||||
});
|
||||
const [renderedView, setRenderedView] = useState<ProviderUpdateSidebarPillView | null>(
|
||||
() => view,
|
||||
);
|
||||
const pendingViewRef = useRef<ProviderUpdateSidebarPillView | null>(null);
|
||||
const [exitingKey, setExitingKey] = useState<string | null>(null);
|
||||
const dismissAfterExitKeyRef = useRef<string | null>(null);
|
||||
|
||||
const openProviderSettings = useCallback(() => {
|
||||
void navigate({ to: "/settings/providers" });
|
||||
}, [navigate]);
|
||||
const displayedView = renderedView ?? view;
|
||||
const dismissAfterVisibleMs = displayedView?.dismissAfterVisibleMs;
|
||||
const viewKey = displayedView?.key ?? null;
|
||||
const showDismissProgress =
|
||||
dismissAfterVisibleMs !== undefined &&
|
||||
displayedView?.tone !== "loading" &&
|
||||
exitingKey !== viewKey;
|
||||
|
||||
const startExit = useCallback(
|
||||
(key: string, nextView: ProviderUpdateSidebarPillView | null, dismissKey?: string) => {
|
||||
if (exitingKey === key) {
|
||||
return;
|
||||
}
|
||||
pendingViewRef.current = nextView;
|
||||
dismissAfterExitKeyRef.current = dismissKey ?? null;
|
||||
setExitingKey(key);
|
||||
},
|
||||
[exitingKey],
|
||||
);
|
||||
|
||||
useProviderUpdateRenderedViewSynchronization({
|
||||
exitingKey,
|
||||
renderedView,
|
||||
setRenderedView,
|
||||
startExit,
|
||||
view,
|
||||
});
|
||||
useProviderUpdateAutoDismiss({
|
||||
dismissAfterVisibleMs,
|
||||
exitingKey,
|
||||
startExit,
|
||||
viewKey,
|
||||
});
|
||||
|
||||
if (!displayedView) {
|
||||
return null;
|
||||
@@ -138,13 +161,13 @@ export function SidebarProviderUpdatePill() {
|
||||
if (!displayedView || exitingKey !== displayedView.key) {
|
||||
return;
|
||||
}
|
||||
if (dismissAfterExitKey === displayedView.key) {
|
||||
if (dismissAfterExitKeyRef.current === displayedView.key) {
|
||||
setDismissedKeys((previous) => new Set(previous).add(displayedView.key));
|
||||
}
|
||||
setRenderedView(pendingView);
|
||||
setPendingView(null);
|
||||
setRenderedView(pendingViewRef.current);
|
||||
pendingViewRef.current = null;
|
||||
setExitingKey(null);
|
||||
setDismissAfterExitKey(null);
|
||||
dismissAfterExitKeyRef.current = null;
|
||||
}}
|
||||
>
|
||||
{showDismissProgress ? (
|
||||
|
||||
@@ -182,7 +182,7 @@ const capabilitiesLayer = Layer.effectContext(
|
||||
if (session === null) {
|
||||
return yield* new ConnectionBlockedError({
|
||||
reason: "authentication",
|
||||
detail: "Sign in to T3 Cloud to connect this environment.",
|
||||
detail: "Sign in to T3 Connect to connect this environment.",
|
||||
});
|
||||
}
|
||||
const token = yield* session.readClerkToken().pipe(
|
||||
@@ -197,7 +197,7 @@ const capabilitiesLayer = Layer.effectContext(
|
||||
if (token === null) {
|
||||
return yield* new ConnectionBlockedError({
|
||||
reason: "authentication",
|
||||
detail: "The T3 Cloud session is unavailable.",
|
||||
detail: "The T3 Connect session is unavailable.",
|
||||
});
|
||||
}
|
||||
return token;
|
||||
|
||||
@@ -14,6 +14,7 @@ import { APP_BASE_NAME, APP_DISPLAY_NAME, APP_STAGE_LABEL } from "../branding";
|
||||
import { resolveServerBackedAppDisplayName } from "../branding.logic";
|
||||
import { AppSidebarLayout } from "../components/AppSidebarLayout";
|
||||
import { CommandPalette } from "../components/CommandPalette";
|
||||
import { ConnectOnboardingDialog } from "../components/cloud/ConnectOnboardingDialog";
|
||||
import { RelayClientInstallDialog } from "../components/cloud/RelayClientInstallDialog";
|
||||
import { SshPasswordPromptDialog } from "../components/desktop/SshPasswordPromptDialog";
|
||||
import { ProviderUpdateLaunchNotification } from "../components/ProviderUpdateLaunchNotification";
|
||||
@@ -128,6 +129,7 @@ function RootRouteView() {
|
||||
<DocumentTitleSync />
|
||||
{primaryEnvironmentAuthenticated ? <AuthenticatedTracingBootstrap /> : null}
|
||||
<RelayClientInstallDialog />
|
||||
<ConnectOnboardingDialog />
|
||||
<SshPasswordPromptDialog />
|
||||
<SlowRpcRequestToastCoordinator />
|
||||
<HostedStaticEnvironmentBootstrap />
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
ALTER TABLE "relay_mobile_devices" ADD COLUMN "bundle_id" varchar(255);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "relay_mobile_devices" ADD COLUMN "aps_environment" varchar(16);
|
||||
File diff suppressed because it is too large.
Load diff
@@ -11,6 +11,8 @@
|
||||
"dependencies": {
|
||||
"@clerk/backend": "catalog:",
|
||||
"@effect/sql-pg": "catalog:",
|
||||
"@noble/curves": "catalog:",
|
||||
"@noble/hashes": "catalog:",
|
||||
"@t3tools/client-runtime": "workspace:*",
|
||||
"@t3tools/contracts": "workspace:*",
|
||||
"@t3tools/shared": "workspace:*",
|
||||
|
||||
@@ -28,6 +28,8 @@ function target(deviceId: string): LiveActivities.TargetRow {
|
||||
platform: "ios",
|
||||
ios_major_version: 18,
|
||||
app_version: "1.0.0",
|
||||
bundle_id: null,
|
||||
aps_environment: null,
|
||||
push_token: null,
|
||||
push_to_start_token: "start-token",
|
||||
preferences_json: "{}",
|
||||
@@ -60,6 +62,7 @@ function makeAgentActivityRows(
|
||||
return {
|
||||
upsert: () => Effect.void,
|
||||
remove: () => Effect.void,
|
||||
pruneTerminal: () => Effect.void,
|
||||
listForUser: () => Effect.succeed([state]),
|
||||
...overrides,
|
||||
};
|
||||
@@ -306,7 +309,7 @@ describe("AgentActivityPublisher", () => {
|
||||
const sentAggregates: Array<
|
||||
Parameters<ApnsDeliveries.ApnsDeliveries["Service"]["sendForTarget"]>[0]
|
||||
> = [];
|
||||
const removals: Array<Parameters<AgentActivityRows.AgentActivityRows["Service"]["remove"]>[0]> =
|
||||
const upserts: Array<Parameters<AgentActivityRows.AgentActivityRows["Service"]["upsert"]>[0]> =
|
||||
[];
|
||||
|
||||
return Effect.gen(function* () {
|
||||
@@ -326,9 +329,9 @@ describe("AgentActivityPublisher", () => {
|
||||
Layer.succeed(
|
||||
AgentActivityRows.AgentActivityRows,
|
||||
makeAgentActivityRows({
|
||||
remove: (input) =>
|
||||
upsert: (input) =>
|
||||
Effect.sync(() => {
|
||||
removals.push(input);
|
||||
upserts.push(input);
|
||||
}),
|
||||
listForUser: () => Effect.succeed([]),
|
||||
}),
|
||||
@@ -378,11 +381,12 @@ describe("AgentActivityPublisher", () => {
|
||||
ok: true,
|
||||
},
|
||||
]);
|
||||
expect(removals).toEqual([
|
||||
// Terminal states are persisted (and later pruned by the cron) so the
|
||||
// finished thread can keep a Done row in later aggregates.
|
||||
expect(upserts).toEqual([
|
||||
{
|
||||
environmentId: "env",
|
||||
environmentPublicKey: "environment-public-key",
|
||||
threadId: "thread",
|
||||
state: completedState,
|
||||
},
|
||||
]);
|
||||
expect(sentAggregates).toHaveLength(1);
|
||||
@@ -628,3 +632,182 @@ describe("AgentActivityPublisher", () => {
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("isExpiredAgentActivityState", () => {
|
||||
const hourMs = 60 * 60 * 1_000;
|
||||
|
||||
it("expires running rows after two hours without an update", () => {
|
||||
expect(AgentActivityPublisher.isExpiredAgentActivityState(state, 2 * hourMs - 1)).toBe(false);
|
||||
expect(AgentActivityPublisher.isExpiredAgentActivityState(state, 2 * hourMs + 1)).toBe(true);
|
||||
});
|
||||
|
||||
it("keeps waiting rows for a day", () => {
|
||||
const waiting: RelayAgentActivityState = { ...state, phase: "waiting_for_approval" };
|
||||
expect(AgentActivityPublisher.isExpiredAgentActivityState(waiting, 23 * hourMs)).toBe(false);
|
||||
expect(AgentActivityPublisher.isExpiredAgentActivityState(waiting, 25 * hourMs)).toBe(true);
|
||||
});
|
||||
|
||||
it("treats rows with unparseable timestamps as expired", () => {
|
||||
expect(
|
||||
AgentActivityPublisher.isExpiredAgentActivityState({ ...state, updatedAt: "not-a-date" }, 0),
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("makeAggregateState", () => {
|
||||
const hourMs = 60 * 60 * 1_000;
|
||||
|
||||
it("drops expired rows from the aggregate", () => {
|
||||
const fresh: RelayAgentActivityState = {
|
||||
...state,
|
||||
threadId: "thread-fresh" as RelayAgentActivityState["threadId"],
|
||||
updatedAt: "1970-01-01T03:00:00.000Z",
|
||||
};
|
||||
const aggregate = AgentActivityPublisher.makeAggregateState({
|
||||
activeStates: [state, fresh],
|
||||
terminalState: null,
|
||||
nowMs: 3 * hourMs,
|
||||
});
|
||||
|
||||
expect(aggregate?.activeCount).toBe(1);
|
||||
expect(aggregate?.activities).toMatchObject([{ threadId: "thread-fresh" }]);
|
||||
});
|
||||
|
||||
it("returns null when every row has expired and nothing terminal remains", () => {
|
||||
expect(
|
||||
AgentActivityPublisher.makeAggregateState({
|
||||
activeStates: [state],
|
||||
terminalState: null,
|
||||
nowMs: 3 * hourMs,
|
||||
}),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("still reports the terminal state when active rows have expired", () => {
|
||||
const terminalState: RelayAgentActivityState = {
|
||||
...state,
|
||||
phase: "completed",
|
||||
updatedAt: "1970-01-01T03:00:00.000Z",
|
||||
};
|
||||
const aggregate = AgentActivityPublisher.makeAggregateState({
|
||||
activeStates: [state],
|
||||
terminalState,
|
||||
nowMs: 3 * hourMs,
|
||||
});
|
||||
|
||||
expect(aggregate?.activeCount).toBe(0);
|
||||
expect(aggregate?.activities).toMatchObject([{ phase: "completed" }]);
|
||||
});
|
||||
|
||||
it("keeps a recently finished thread visible as Done beside active agents", () => {
|
||||
const active: RelayAgentActivityState = {
|
||||
...state,
|
||||
threadId: "thread-active" as RelayAgentActivityState["threadId"],
|
||||
updatedAt: "1970-01-01T00:58:00.000Z",
|
||||
};
|
||||
const justCompleted: RelayAgentActivityState = {
|
||||
...state,
|
||||
threadId: "thread-done" as RelayAgentActivityState["threadId"],
|
||||
phase: "completed",
|
||||
updatedAt: "1970-01-01T00:59:00.000Z",
|
||||
};
|
||||
const aggregate = AgentActivityPublisher.makeAggregateState({
|
||||
activeStates: [active, justCompleted],
|
||||
terminalState: null,
|
||||
nowMs: hourMs,
|
||||
});
|
||||
|
||||
expect(aggregate?.activeCount).toBe(1);
|
||||
expect(aggregate?.subtitle).toBe("Agent work in progress");
|
||||
expect(aggregate?.activities).toMatchObject([
|
||||
{ threadId: "thread-active", phase: "running" },
|
||||
{ threadId: "thread-done", phase: "completed", status: "Done" },
|
||||
]);
|
||||
expect(aggregate?.updatedAt).toBe("1970-01-01T00:59:00.000Z");
|
||||
});
|
||||
|
||||
it("drops finished threads from the aggregate after the display window", () => {
|
||||
const active: RelayAgentActivityState = {
|
||||
...state,
|
||||
threadId: "thread-active" as RelayAgentActivityState["threadId"],
|
||||
updatedAt: "1970-01-01T00:58:00.000Z",
|
||||
};
|
||||
const staleCompleted: RelayAgentActivityState = {
|
||||
...state,
|
||||
threadId: "thread-done" as RelayAgentActivityState["threadId"],
|
||||
phase: "completed",
|
||||
updatedAt: "1970-01-01T00:44:00.000Z",
|
||||
};
|
||||
const aggregate = AgentActivityPublisher.makeAggregateState({
|
||||
activeStates: [active, staleCompleted],
|
||||
terminalState: null,
|
||||
nowMs: hourMs,
|
||||
});
|
||||
|
||||
expect(aggregate?.activities).toMatchObject([{ threadId: "thread-active" }]);
|
||||
});
|
||||
|
||||
it("keeps showing recently finished work when nothing is active", () => {
|
||||
const lingeringCompleted: RelayAgentActivityState = {
|
||||
...state,
|
||||
phase: "completed",
|
||||
updatedAt: "1970-01-01T00:59:00.000Z",
|
||||
};
|
||||
const aggregate = AgentActivityPublisher.makeAggregateState({
|
||||
activeStates: [lingeringCompleted],
|
||||
terminalState: null,
|
||||
nowMs: hourMs,
|
||||
});
|
||||
|
||||
// An armed card never renders an empty state: recently finished threads
|
||||
// keep Done content on it, and once they age out the aggregate becomes
|
||||
// null and the delivery layer ends the card.
|
||||
expect(aggregate).toMatchObject({
|
||||
activeCount: 0,
|
||||
subtitle: "Agent work completed",
|
||||
activities: [{ phase: "completed", status: "Done" }],
|
||||
});
|
||||
expect(
|
||||
AgentActivityPublisher.makeAggregateState({
|
||||
activeStates: [{ ...lingeringCompleted, updatedAt: "1970-01-01T00:44:00.000Z" }],
|
||||
terminalState: null,
|
||||
nowMs: hourMs,
|
||||
}),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("gives active agents the display slots before finished ones", () => {
|
||||
const mkActive = (id: string): RelayAgentActivityState => ({
|
||||
...state,
|
||||
threadId: id as RelayAgentActivityState["threadId"],
|
||||
updatedAt: "1970-01-01T00:58:00.000Z",
|
||||
});
|
||||
const justCompleted: RelayAgentActivityState = {
|
||||
...state,
|
||||
threadId: "thread-done" as RelayAgentActivityState["threadId"],
|
||||
phase: "completed",
|
||||
updatedAt: "1970-01-01T00:59:00.000Z",
|
||||
};
|
||||
const aggregate = AgentActivityPublisher.makeAggregateState({
|
||||
activeStates: [
|
||||
mkActive("a-1"),
|
||||
mkActive("a-2"),
|
||||
mkActive("a-3"),
|
||||
mkActive("a-4"),
|
||||
mkActive("a-5"),
|
||||
justCompleted,
|
||||
],
|
||||
terminalState: null,
|
||||
nowMs: hourMs,
|
||||
});
|
||||
|
||||
expect(aggregate?.activeCount).toBe(5);
|
||||
expect(aggregate?.activities).toMatchObject([
|
||||
{ threadId: "a-1" },
|
||||
{ threadId: "a-2" },
|
||||
{ threadId: "a-3" },
|
||||
{ threadId: "a-4" },
|
||||
{ threadId: "a-5" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -8,8 +8,16 @@ import * as Context from "effect/Context";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Option from "effect/Option";
|
||||
|
||||
import { sanitizeAgentActivityAggregateState } from "./agentActivityPayloads.ts";
|
||||
import {
|
||||
isExpiredAgentActivityState,
|
||||
isTerminalPhase,
|
||||
MAX_ACTIVITY_ROWS,
|
||||
sanitizeAgentActivityAggregateState,
|
||||
} from "./agentActivityPayloads.ts";
|
||||
|
||||
export { isExpiredAgentActivityState } from "./agentActivityPayloads.ts";
|
||||
import * as AgentActivityRows from "./AgentActivityRows.ts";
|
||||
import * as EnvironmentLinks from "../environments/EnvironmentLinks.ts";
|
||||
import * as LiveActivities from "./LiveActivities.ts";
|
||||
@@ -55,6 +63,7 @@ export const make = Effect.gen(function* () {
|
||||
? makeAggregateState({
|
||||
activeStates,
|
||||
terminalState: input.state && isTerminalPhase(input.state) ? input.state : null,
|
||||
nowMs: input.nowMs,
|
||||
})
|
||||
: null;
|
||||
const notificationOnlyAggregate =
|
||||
@@ -64,6 +73,7 @@ export const make = Effect.gen(function* () {
|
||||
? makeAggregateState({
|
||||
activeStates: isTerminalPhase(input.state) ? [] : [input.state],
|
||||
terminalState: isTerminalPhase(input.state) ? input.state : null,
|
||||
nowMs: input.nowMs,
|
||||
})
|
||||
: null;
|
||||
const targets = yield* liveActivities.listTargets({ userId: input.deliveryUser.userId });
|
||||
@@ -110,8 +120,12 @@ export const make = Effect.gen(function* () {
|
||||
if (target === null) {
|
||||
return null;
|
||||
}
|
||||
const aggregate = makeAggregateState({ activeStates, terminalState: null });
|
||||
const now = yield* DateTime.now;
|
||||
const aggregate = makeAggregateState({
|
||||
activeStates,
|
||||
terminalState: null,
|
||||
nowMs: now.epochMilliseconds,
|
||||
});
|
||||
return yield* apnsDeliveries.sendForTarget({
|
||||
target,
|
||||
aggregate,
|
||||
@@ -124,7 +138,11 @@ export const make = Effect.gen(function* () {
|
||||
"relay.thread_id": input.threadId,
|
||||
"relay.agent_activity.phase": input.state?.phase ?? "deleted",
|
||||
});
|
||||
if (input.state && !isTerminalPhase(input.state)) {
|
||||
if (input.state) {
|
||||
// Terminal states are persisted too (pruned by the cron after they
|
||||
// age out) so a thread that finishes while other agents are active
|
||||
// stays visible as Done/Failed in subsequent aggregates instead of
|
||||
// silently vanishing from the Live Activity.
|
||||
yield* rows.upsert({
|
||||
environmentPublicKey: input.environmentPublicKey,
|
||||
state: input.state,
|
||||
@@ -174,7 +192,9 @@ function statusForPhase(phase: RelayAgentActivityState["phase"]): string {
|
||||
case "failed":
|
||||
return "Failed";
|
||||
case "starting":
|
||||
return "Starting";
|
||||
// Matches the web sidebar's pill wording (Sidebar.logic.ts) so the same
|
||||
// thread reads the same across surfaces.
|
||||
return "Connecting";
|
||||
case "running":
|
||||
return "Working";
|
||||
case "stale":
|
||||
@@ -182,10 +202,6 @@ function statusForPhase(phase: RelayAgentActivityState["phase"]): string {
|
||||
}
|
||||
}
|
||||
|
||||
function isTerminalPhase(state: RelayAgentActivityState): boolean {
|
||||
return state.phase === "completed" || state.phase === "failed";
|
||||
}
|
||||
|
||||
function aggregateRowForState(state: RelayAgentActivityState) {
|
||||
return {
|
||||
environmentId: state.environmentId,
|
||||
@@ -210,15 +226,68 @@ function terminalAggregateState(state: RelayAgentActivityState): RelayAgentActiv
|
||||
});
|
||||
}
|
||||
|
||||
function makeAggregateState(input: {
|
||||
// How long a finished thread keeps its Done/Failed row in the aggregate while
|
||||
// other agents are still active. Long enough to be seen on the lock screen,
|
||||
// short enough that the activity list stays about live work.
|
||||
export const TERMINAL_AGENT_ACTIVITY_DISPLAY_TTL_MS = 15 * 60 * 1_000;
|
||||
|
||||
function isRecentTerminalState(state: RelayAgentActivityState, nowMs: number): boolean {
|
||||
if (!isTerminalPhase(state)) {
|
||||
return false;
|
||||
}
|
||||
const updatedAtMs = Option.match(DateTime.make(state.updatedAt), {
|
||||
onNone: () => Number.NaN,
|
||||
onSome: (dt) => dt.epochMilliseconds,
|
||||
});
|
||||
if (Number.isNaN(updatedAtMs)) {
|
||||
return false;
|
||||
}
|
||||
return nowMs - updatedAtMs <= TERMINAL_AGENT_ACTIVITY_DISPLAY_TTL_MS;
|
||||
}
|
||||
|
||||
export function makeAggregateState(input: {
|
||||
readonly activeStates: ReadonlyArray<RelayAgentActivityState>;
|
||||
readonly terminalState: RelayAgentActivityState | null;
|
||||
readonly nowMs: number;
|
||||
}): RelayAgentActivityAggregateState | null {
|
||||
const activeStates = input.activeStates.filter((state) => !isTerminalPhase(state));
|
||||
const activeStates = input.activeStates.filter(
|
||||
(state) => !isTerminalPhase(state) && !isExpiredAgentActivityState(state, input.nowMs),
|
||||
);
|
||||
if (activeStates.length === 0) {
|
||||
return input.terminalState === null ? null : terminalAggregateState(input.terminalState);
|
||||
if (input.terminalState !== null) {
|
||||
return terminalAggregateState(input.terminalState);
|
||||
}
|
||||
// With no live work, recently finished threads keep the card showing
|
||||
// Done/Failed content (an armed card never renders an empty state). The
|
||||
// newly-terminal alert rules key off the previously delivered aggregate,
|
||||
// so replays repaint this without buzzing. Once the terminal rows age
|
||||
// out, the aggregate is null and the delivery layer ends the card.
|
||||
const recentTerminal = input.activeStates
|
||||
.filter((state) => isRecentTerminalState(state, input.nowMs))
|
||||
.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
|
||||
const newest = recentTerminal[0];
|
||||
if (!newest) {
|
||||
return null;
|
||||
}
|
||||
return sanitizeAgentActivityAggregateState({
|
||||
title: "T3 Code",
|
||||
subtitle: newest.phase === "failed" ? "Agent work failed" : "Agent work completed",
|
||||
activeCount: 0,
|
||||
updatedAt: newest.updatedAt,
|
||||
activities: recentTerminal.slice(0, MAX_ACTIVITY_ROWS).map(aggregateRowForState),
|
||||
});
|
||||
}
|
||||
const updatedAt = activeStates.reduce((latest, state) =>
|
||||
// Recently finished threads ride along after the active ones (display slots
|
||||
// permitting) so a completion is visible as Done/Failed instead of the row
|
||||
// silently vanishing while other agents keep the activity alive.
|
||||
const recentTerminalStates = input.activeStates
|
||||
.filter((state) => isRecentTerminalState(state, input.nowMs))
|
||||
.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
|
||||
const displayedStates = [
|
||||
...activeStates.slice(0, MAX_ACTIVITY_ROWS),
|
||||
...recentTerminalStates,
|
||||
].slice(0, MAX_ACTIVITY_ROWS);
|
||||
const updatedAt = [...activeStates, ...recentTerminalStates].reduce((latest, state) =>
|
||||
state.updatedAt.localeCompare(latest.updatedAt) > 0 ? state : latest,
|
||||
).updatedAt;
|
||||
return sanitizeAgentActivityAggregateState({
|
||||
@@ -226,7 +295,7 @@ function makeAggregateState(input: {
|
||||
subtitle: "Agent work in progress",
|
||||
activeCount: activeStates.length,
|
||||
updatedAt,
|
||||
activities: activeStates.slice(0, 3).map(aggregateRowForState),
|
||||
activities: displayedStates.map(aggregateRowForState),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import * as Function from "effect/Function";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Schema from "effect/Schema";
|
||||
import { and, desc, eq, isNull } from "drizzle-orm";
|
||||
import { and, desc, eq, isNull, lt, sql } from "drizzle-orm";
|
||||
|
||||
import * as RelayDb from "../db.ts";
|
||||
import { relayAgentActivityRows, relayEnvironmentLinks } from "../persistence/schema.ts";
|
||||
@@ -38,6 +38,18 @@ export class AgentActivityRowDeletePersistenceError extends Schema.TaggedErrorCl
|
||||
}
|
||||
}
|
||||
|
||||
export class AgentActivityRowPruneTerminalPersistenceError extends Schema.TaggedErrorClass<AgentActivityRowPruneTerminalPersistenceError>()(
|
||||
"AgentActivityRowPruneTerminalPersistenceError",
|
||||
{
|
||||
updatedBefore: Schema.String,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Failed to prune terminal agent activity rows updated before ${this.updatedBefore}.`;
|
||||
}
|
||||
}
|
||||
|
||||
export class AgentActivityRowListPersistenceError extends Schema.TaggedErrorClass<AgentActivityRowListPersistenceError>()(
|
||||
"AgentActivityRowListPersistenceError",
|
||||
{
|
||||
@@ -57,6 +69,9 @@ export class AgentActivityRows extends Context.Service<
|
||||
readonly environmentPublicKey: string;
|
||||
readonly state: RelayAgentActivityState;
|
||||
}) => Effect.Effect<void, AgentActivityRowUpsertPersistenceError>;
|
||||
readonly pruneTerminal: (input: {
|
||||
readonly updatedBefore: string;
|
||||
}) => Effect.Effect<void, AgentActivityRowPruneTerminalPersistenceError>;
|
||||
readonly remove: (input: {
|
||||
readonly environmentId: string;
|
||||
readonly environmentPublicKey: string;
|
||||
@@ -163,6 +178,29 @@ export const make = Effect.gen(function* () {
|
||||
);
|
||||
}),
|
||||
|
||||
pruneTerminal: Effect.fn("relay.agent_activity_rows.prune_terminal")(function* (input) {
|
||||
yield* Effect.annotateCurrentSpan({
|
||||
"relay.agent_activity_prune.before": input.updatedBefore,
|
||||
});
|
||||
yield* db
|
||||
.delete(relayAgentActivityRows)
|
||||
.where(
|
||||
and(
|
||||
sql`${relayAgentActivityRows.stateJson} ->> 'phase' IN ('completed', 'failed')`,
|
||||
lt(relayAgentActivityRows.updatedAt, input.updatedBefore),
|
||||
),
|
||||
)
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new AgentActivityRowPruneTerminalPersistenceError({
|
||||
updatedBefore: input.updatedBefore,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}),
|
||||
|
||||
listForUser: Effect.fn("relay.agent_activity_rows.list_for_user")(function* (input) {
|
||||
return yield* db
|
||||
.select({ stateJson: relayAgentActivityRows.stateJson })
|
||||
|
||||
@@ -10,14 +10,17 @@ import * as Redacted from "effect/Redacted";
|
||||
import * as Schema from "effect/Schema";
|
||||
import * as HttpClient from "effect/unstable/http/HttpClient";
|
||||
import * as HttpClientError from "effect/unstable/http/HttpClientError";
|
||||
import * as HttpClientResponse from "effect/unstable/http/HttpClientResponse";
|
||||
|
||||
import type { ApnsCredentials } from "../Config.ts";
|
||||
import * as ApnsClient from "./ApnsClient.ts";
|
||||
import * as ApnsProviderTokens from "./ApnsProviderTokens.ts";
|
||||
|
||||
const isApnsJwtSigningError = Schema.is(ApnsClient.ApnsJwtSigningError);
|
||||
const isApnsHttpRequestError = Schema.is(ApnsClient.ApnsHttpRequestError);
|
||||
|
||||
const TestLayer = ApnsClient.layer.pipe(
|
||||
Layer.provide(ApnsProviderTokens.layer),
|
||||
Layer.provide(
|
||||
Layer.succeed(
|
||||
HttpClient.HttpClient,
|
||||
@@ -96,6 +99,32 @@ describe("ApnsClient", () => {
|
||||
}).pipe(Effect.provide(TestLayer)),
|
||||
);
|
||||
|
||||
it.effect("builds a high-priority alerting update payload when an alert is attached", () =>
|
||||
Effect.gen(function* () {
|
||||
const apns = yield* ApnsClient.ApnsClient;
|
||||
const request = apns.makeLiveActivityRequest({
|
||||
event: "update",
|
||||
token: "token",
|
||||
state,
|
||||
alert: { title: "Thread", body: "Approval: Project" },
|
||||
nowEpochSeconds: Math.floor(now.epochMilliseconds / 1_000),
|
||||
nowIso: DateTime.formatIso(now),
|
||||
});
|
||||
|
||||
expect(request.priority).toBe("10");
|
||||
expect(request.payload).toMatchObject({
|
||||
aps: {
|
||||
event: "update",
|
||||
alert: {
|
||||
title: "Thread",
|
||||
body: "Approval: Project",
|
||||
sound: "default",
|
||||
},
|
||||
},
|
||||
});
|
||||
}).pipe(Effect.provide(TestLayer)),
|
||||
);
|
||||
|
||||
it.effect("builds an end payload with a dismissal date", () =>
|
||||
Effect.gen(function* () {
|
||||
const apns = yield* ApnsClient.ApnsClient;
|
||||
@@ -114,6 +143,22 @@ describe("ApnsClient", () => {
|
||||
"dismissal-date": 300,
|
||||
},
|
||||
});
|
||||
|
||||
// Without final content the card would freeze on its previous state;
|
||||
// contentless ends dismiss quickly instead.
|
||||
const contentless = apns.makeLiveActivityRequest({
|
||||
event: "end",
|
||||
token: "token",
|
||||
state: null,
|
||||
nowEpochSeconds: Math.floor(now.epochMilliseconds / 1_000),
|
||||
nowIso: DateTime.formatIso(now),
|
||||
});
|
||||
expect(contentless.payload).toMatchObject({
|
||||
aps: {
|
||||
event: "end",
|
||||
"dismissal-date": 15,
|
||||
},
|
||||
});
|
||||
}).pipe(Effect.provide(TestLayer)),
|
||||
);
|
||||
|
||||
@@ -181,7 +226,10 @@ describe("ApnsClient", () => {
|
||||
expect(error).toMatchObject({
|
||||
teamId: "team-1",
|
||||
keyId: "key-1",
|
||||
issuedAtUnixSeconds: 123,
|
||||
// The provider-token service quantizes iat to the reuse window, so
|
||||
// the signing context carries the window start rather than the raw
|
||||
// request time.
|
||||
issuedAtUnixSeconds: 0,
|
||||
cause: expect.any(Error),
|
||||
message: "Failed to sign APNs JWT for key key-1.",
|
||||
});
|
||||
@@ -210,6 +258,7 @@ describe("ApnsClient", () => {
|
||||
),
|
||||
);
|
||||
const layer = ApnsClient.layer.pipe(
|
||||
Layer.provide(ApnsProviderTokens.layer),
|
||||
Layer.provide(Layer.succeed(HttpClient.HttpClient, failingHttpClient)),
|
||||
);
|
||||
|
||||
@@ -254,4 +303,62 @@ describe("ApnsClient", () => {
|
||||
});
|
||||
}).pipe(Effect.provide(layer));
|
||||
});
|
||||
|
||||
it.effect("reuses the signed provider JWT across pushes within the reuse window", () => {
|
||||
ApnsProviderTokens.__resetApnsProviderTokenCacheForTest();
|
||||
const { privateKey } = NodeCrypto.generateKeyPairSync("ec", {
|
||||
namedCurve: "prime256v1",
|
||||
privateKeyEncoding: { type: "pkcs8", format: "pem" },
|
||||
publicKeyEncoding: { type: "spki", format: "pem" },
|
||||
});
|
||||
const credentials = {
|
||||
teamId: "team-jwt-cache",
|
||||
keyId: "key-jwt-cache",
|
||||
privateKey: Redacted.make(privateKey),
|
||||
bundleId: "com.t3tools.test",
|
||||
environment: "sandbox",
|
||||
} satisfies ApnsCredentials;
|
||||
const authorizations: Array<string> = [];
|
||||
const capturingHttpClient = HttpClient.make((request) => {
|
||||
authorizations.push(request.headers.authorization ?? "");
|
||||
return Effect.succeed(
|
||||
HttpClientResponse.fromWeb(
|
||||
request,
|
||||
new Response("", { status: 200, headers: { "apns-id": "apns-id-1" } }),
|
||||
),
|
||||
);
|
||||
});
|
||||
const layer = ApnsClient.layer.pipe(
|
||||
Layer.provide(ApnsProviderTokens.layer),
|
||||
Layer.provide(Layer.succeed(HttpClient.HttpClient, capturingHttpClient)),
|
||||
);
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const apns = yield* ApnsClient.ApnsClient;
|
||||
const request = apns.makePushNotificationRequest({
|
||||
token: "push-token",
|
||||
notification: {
|
||||
title: "Thread",
|
||||
body: "Input: Project",
|
||||
environmentId: "env",
|
||||
threadId: "thread",
|
||||
deepLink: "/threads/env/thread",
|
||||
},
|
||||
});
|
||||
const send = (issuedAtUnixSeconds: number) =>
|
||||
apns.sendPushNotificationRequest({ credentials, request, issuedAtUnixSeconds });
|
||||
|
||||
const window = ApnsProviderTokens.APNS_JWT_REUSE_SECONDS;
|
||||
yield* send(window + 10);
|
||||
yield* send(window * 2 - 1);
|
||||
yield* send(window * 2);
|
||||
|
||||
expect(authorizations).toHaveLength(3);
|
||||
// Within the 45-minute window APNs must see the byte-identical token;
|
||||
// refreshing it per push trips TooManyProviderTokenUpdates.
|
||||
expect(authorizations[1]).toBe(authorizations[0]);
|
||||
expect(authorizations[2]).not.toBe(authorizations[0]);
|
||||
ApnsProviderTokens.__resetApnsProviderTokenCacheForTest();
|
||||
}).pipe(Effect.provide(layer));
|
||||
});
|
||||
});
|
||||
@@ -1,22 +1,30 @@
|
||||
import * as NodeCrypto from "node:crypto";
|
||||
|
||||
import type { RelayAgentActivityAggregateState } from "@t3tools/contracts/relay";
|
||||
import * as Context from "effect/Context";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Encoding from "effect/Encoding";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Redacted from "effect/Redacted";
|
||||
import * as Schema from "effect/Schema";
|
||||
import * as Headers from "effect/unstable/http/Headers";
|
||||
import * as HttpClient from "effect/unstable/http/HttpClient";
|
||||
import * as HttpClientRequest from "effect/unstable/http/HttpClientRequest";
|
||||
import { ApnsEnvironment as ApnsEnvironmentSchema, type ApnsCredentials } from "../Config.ts";
|
||||
import type { ApnsNotificationPayload } from "./apnsDeliveryJobs.ts";
|
||||
import type { ApnsLiveActivityAlert, ApnsNotificationPayload } from "./apnsDeliveryJobs.ts";
|
||||
import { ApnsJwtEncodingError, ApnsJwtSigningError } from "./apnsJwt.ts";
|
||||
import * as ApnsProviderTokens from "./ApnsProviderTokens.ts";
|
||||
|
||||
export { ApnsJwtEncodingError, ApnsJwtSigningError } from "./apnsJwt.ts";
|
||||
|
||||
const LIVE_ACTIVITY_NAME = "AgentActivity";
|
||||
const STALE_AFTER_SECONDS = 2 * 60;
|
||||
// Updates only flow on domain events, so a healthy agent can be silent for
|
||||
// minutes (long tool calls, pending approvals). Two minutes made iOS dim
|
||||
// perfectly healthy activities; ten minutes still bounds how long a dead
|
||||
// environment can look alive.
|
||||
const STALE_AFTER_SECONDS = 10 * 60;
|
||||
const DISMISS_AFTER_SECONDS = 5 * 60;
|
||||
// An end without a final content-state leaves whatever the card last showed
|
||||
// frozen on the lock screen until dismissal — get it off quickly instead of
|
||||
// parading stale state for the full window.
|
||||
const CONTENTLESS_DISMISS_AFTER_SECONDS = 15;
|
||||
|
||||
const ApnsLiveActivityEventSchema = Schema.Literals(["start", "update", "end"]);
|
||||
export type ApnsLiveActivityEvent = typeof ApnsLiveActivityEventSchema.Type;
|
||||
@@ -43,35 +51,6 @@ export interface ApnsDeliveryResult {
|
||||
readonly apnsId: string | null;
|
||||
}
|
||||
|
||||
export class ApnsJwtEncodingError extends Schema.TaggedErrorClass<ApnsJwtEncodingError>()(
|
||||
"ApnsJwtEncodingError",
|
||||
{
|
||||
component: Schema.Literals(["header", "payload"]),
|
||||
teamId: Schema.String,
|
||||
keyId: Schema.String,
|
||||
issuedAtUnixSeconds: Schema.Number,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Failed to encode APNs JWT ${this.component} for key ${this.keyId}.`;
|
||||
}
|
||||
}
|
||||
|
||||
export class ApnsJwtSigningError extends Schema.TaggedErrorClass<ApnsJwtSigningError>()(
|
||||
"ApnsJwtSigningError",
|
||||
{
|
||||
teamId: Schema.String,
|
||||
keyId: Schema.String,
|
||||
issuedAtUnixSeconds: Schema.Number,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Failed to sign APNs JWT for key ${this.keyId}.`;
|
||||
}
|
||||
}
|
||||
|
||||
export class ApnsHttpRequestError extends Schema.TaggedErrorClass<ApnsHttpRequestError>()(
|
||||
"ApnsHttpRequestError",
|
||||
{
|
||||
@@ -104,82 +83,6 @@ const decodeApnsErrorResponseJson = Schema.decodeUnknownOption(
|
||||
}),
|
||||
),
|
||||
);
|
||||
const encodeApnsJwtHeaderJson = Schema.encodeEffect(
|
||||
Schema.fromJsonString(
|
||||
Schema.Struct({
|
||||
alg: Schema.Literal("ES256"),
|
||||
kid: Schema.String,
|
||||
}),
|
||||
),
|
||||
);
|
||||
const encodeApnsJwtPayloadJson = Schema.encodeEffect(
|
||||
Schema.fromJsonString(
|
||||
Schema.Struct({
|
||||
iss: Schema.String,
|
||||
iat: Schema.Number,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const makeApnsJwt = Effect.fn("relay.apns.make_jwt")(function* (input: {
|
||||
readonly teamId: ApnsCredentials["teamId"];
|
||||
readonly keyId: ApnsCredentials["keyId"];
|
||||
readonly privateKey: ApnsCredentials["privateKey"];
|
||||
readonly issuedAtUnixSeconds: number;
|
||||
}) {
|
||||
const headerJson = yield* encodeApnsJwtHeaderJson({ alg: "ES256", kid: input.keyId }).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ApnsJwtEncodingError({
|
||||
component: "header",
|
||||
teamId: input.teamId,
|
||||
keyId: input.keyId,
|
||||
issuedAtUnixSeconds: input.issuedAtUnixSeconds,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
const payloadJson = yield* encodeApnsJwtPayloadJson({
|
||||
iss: input.teamId,
|
||||
iat: input.issuedAtUnixSeconds,
|
||||
}).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ApnsJwtEncodingError({
|
||||
component: "payload",
|
||||
teamId: input.teamId,
|
||||
keyId: input.keyId,
|
||||
issuedAtUnixSeconds: input.issuedAtUnixSeconds,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const privateKey = Redacted.value(input.privateKey);
|
||||
const header = Encoding.encodeBase64Url(headerJson);
|
||||
const payload = Encoding.encodeBase64Url(payloadJson);
|
||||
const signingInput = `${header}.${payload}`;
|
||||
|
||||
return yield* Effect.try({
|
||||
try: () => {
|
||||
const signature = NodeCrypto.createSign("sha256")
|
||||
.update(signingInput)
|
||||
.sign({
|
||||
key: privateKey.replace(/\\n/g, "\n"),
|
||||
dsaEncoding: "ieee-p1363",
|
||||
});
|
||||
return `${signingInput}.${Encoding.encodeBase64Url(signature)}`;
|
||||
},
|
||||
catch: (cause) =>
|
||||
new ApnsJwtSigningError({
|
||||
teamId: input.teamId,
|
||||
keyId: input.keyId,
|
||||
issuedAtUnixSeconds: input.issuedAtUnixSeconds,
|
||||
cause,
|
||||
}),
|
||||
});
|
||||
});
|
||||
|
||||
function contentState(state: RelayAgentActivityAggregateState) {
|
||||
return {
|
||||
name: LIVE_ACTIVITY_NAME,
|
||||
@@ -197,12 +100,27 @@ type MakeLiveActivityRequestInput =
|
||||
| (LiveActivityRequestBase & {
|
||||
readonly event: "end";
|
||||
readonly state: RelayAgentActivityAggregateState | null;
|
||||
readonly alert?: ApnsLiveActivityAlert | null;
|
||||
})
|
||||
| (LiveActivityRequestBase & {
|
||||
readonly event: "start" | "update";
|
||||
readonly state: RelayAgentActivityAggregateState;
|
||||
readonly alert?: ApnsLiveActivityAlert | null;
|
||||
});
|
||||
|
||||
// An alert dict on an update/end makes it an "alerting" update: iOS wakes the
|
||||
// screen and plays the haptic (the Apple Sports score-change behavior) instead
|
||||
// of silently redrawing the activity.
|
||||
function liveActivityAlertPayload(alert: ApnsLiveActivityAlert) {
|
||||
return {
|
||||
alert: {
|
||||
title: alert.title,
|
||||
body: alert.body,
|
||||
sound: "default",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function makeLiveActivityRequest(input: MakeLiveActivityRequestInput): ApnsLiveActivityRequest {
|
||||
const timestamp = input.nowEpochSeconds;
|
||||
if (input.event === "end") {
|
||||
@@ -215,7 +133,9 @@ function makeLiveActivityRequest(input: MakeLiveActivityRequestInput): ApnsLiveA
|
||||
timestamp,
|
||||
event: "end",
|
||||
...(input.state ? { "content-state": contentState(input.state) } : {}),
|
||||
"dismissal-date": timestamp + DISMISS_AFTER_SECONDS,
|
||||
...(input.alert ? liveActivityAlertPayload(input.alert) : {}),
|
||||
"dismissal-date":
|
||||
timestamp + (input.state ? DISMISS_AFTER_SECONDS : CONTENTLESS_DISMISS_AFTER_SECONDS),
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -225,7 +145,9 @@ function makeLiveActivityRequest(input: MakeLiveActivityRequestInput): ApnsLiveA
|
||||
return {
|
||||
token: input.token,
|
||||
event: input.event,
|
||||
priority: input.event === "update" ? "5" : "10",
|
||||
// Alerting updates must land immediately; routine redraws stay at the
|
||||
// budget-friendly low priority.
|
||||
priority: input.event === "update" && !input.alert ? "5" : "10",
|
||||
payload: {
|
||||
aps: {
|
||||
timestamp,
|
||||
@@ -241,6 +163,7 @@ function makeLiveActivityRequest(input: MakeLiveActivityRequestInput): ApnsLiveA
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
...(input.event === "update" && input.alert ? liveActivityAlertPayload(input.alert) : {}),
|
||||
"content-state": contentState(state),
|
||||
"stale-date": timestamp + STALE_AFTER_SECONDS,
|
||||
},
|
||||
@@ -300,12 +223,13 @@ export class ApnsClient extends Context.Service<
|
||||
|
||||
export const make = Effect.gen(function* () {
|
||||
const httpClient = yield* HttpClient.HttpClient;
|
||||
const providerTokens = yield* ApnsProviderTokens.ApnsProviderTokens;
|
||||
|
||||
const sendLiveActivityRequest: ApnsClient["Service"]["sendLiveActivityRequest"] = Effect.fn(
|
||||
"relay.apns.send_live_activity_request",
|
||||
)(function* (input) {
|
||||
yield* Effect.annotateCurrentSpan({ "relay.apns.event": input.request.event });
|
||||
const jwt = yield* makeApnsJwt({
|
||||
const jwt = yield* providerTokens.getJwt({
|
||||
...input.credentials,
|
||||
issuedAtUnixSeconds: input.issuedAtUnixSeconds,
|
||||
});
|
||||
@@ -363,7 +287,7 @@ export const make = Effect.gen(function* () {
|
||||
const sendPushNotificationRequest: ApnsClient["Service"]["sendPushNotificationRequest"] =
|
||||
Effect.fn("relay.apns.send_push_notification_request")(function* (input) {
|
||||
yield* Effect.annotateCurrentSpan({ "relay.apns.event": "push_notification" });
|
||||
const jwt = yield* makeApnsJwt({
|
||||
const jwt = yield* providerTokens.getJwt({
|
||||
...input.credentials,
|
||||
issuedAtUnixSeconds: input.issuedAtUnixSeconds,
|
||||
});
|
||||
|
||||
@@ -5,6 +5,7 @@ import type {
|
||||
import * as NodeCryptoLayer from "@effect/platform-node/NodeCrypto";
|
||||
import { describe, expect, it } from "@effect/vitest";
|
||||
import * as NodeCrypto from "node:crypto";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Logger from "effect/Logger";
|
||||
@@ -26,8 +27,10 @@ import * as DeliveryAttempts from "./DeliveryAttempts.ts";
|
||||
import * as LiveActivities from "./LiveActivities.ts";
|
||||
import * as RelayConfiguration from "../Config.ts";
|
||||
import * as ApnsDeliveryQueue from "./ApnsDeliveryQueue.ts";
|
||||
import * as AgentActivityRows from "./AgentActivityRows.ts";
|
||||
import * as ApnsDeliveries from "./ApnsDeliveries.ts";
|
||||
import * as ApnsClient from "./ApnsClient.ts";
|
||||
import * as ApnsProviderTokens from "./ApnsProviderTokens.ts";
|
||||
|
||||
const config = RelayConfiguration.RelayConfiguration.of({
|
||||
relayIssuer: "https://relay.example.test",
|
||||
@@ -127,6 +130,8 @@ const target: LiveActivities.TargetRow = {
|
||||
platform: "ios",
|
||||
ios_major_version: 18,
|
||||
app_version: "1.0.0",
|
||||
bundle_id: null,
|
||||
aps_environment: null,
|
||||
push_token: null,
|
||||
push_to_start_token: "start-token",
|
||||
preferences_json: enabledPreferences,
|
||||
@@ -156,15 +161,31 @@ function makeLayer(input: {
|
||||
>;
|
||||
readonly currentTargets?: ReadonlyArray<LiveActivities.TargetRow>;
|
||||
readonly config?: RelayConfiguration.RelayConfiguration["Service"];
|
||||
// Live agent-activity rows returned by the delivery-time start recheck.
|
||||
// Defaults to one freshly-updated running thread so queued starts stay
|
||||
// deliverable in tests that don't care about the recheck.
|
||||
readonly activityStates?: ReadonlyArray<RelayAgentActivityState>;
|
||||
readonly execute?: (
|
||||
request: HttpClientRequest.HttpClientRequest,
|
||||
) => Effect.Effect<HttpClientResponse.HttpClientResponse>;
|
||||
}) {
|
||||
return ApnsDeliveries.layer.pipe(
|
||||
Layer.provide(ApnsClient.layer),
|
||||
Layer.provide(ApnsProviderTokens.layer),
|
||||
Layer.provide(ApnsDeliveryQueue.layer.pipe(Layer.provide(NodeCryptoLayer.layer))),
|
||||
Layer.provide(
|
||||
Layer.mergeAll(
|
||||
Layer.succeed(AgentActivityRows.AgentActivityRows, {
|
||||
upsert: () => Effect.void,
|
||||
remove: () => Effect.void,
|
||||
pruneTerminal: () => Effect.void,
|
||||
listForUser: () =>
|
||||
input.activityStates !== undefined
|
||||
? Effect.succeed([...input.activityStates])
|
||||
: DateTime.now.pipe(
|
||||
Effect.map((now) => [{ ...state, updatedAt: DateTime.formatIso(now) }]),
|
||||
),
|
||||
} satisfies AgentActivityRows.AgentActivityRows["Service"]),
|
||||
Layer.succeed(ApnsDeliveryQueue.ApnsDeliveryQueueSender, {
|
||||
send: (body) =>
|
||||
Effect.sync(() => {
|
||||
@@ -225,62 +246,61 @@ function makeLayer(input: {
|
||||
}
|
||||
|
||||
describe("ApnsDeliveries", () => {
|
||||
it.effect("queues a restart using the push-to-start token", () => {
|
||||
it.effect("never starts an activity remotely when no update token is registered", () => {
|
||||
const attempts: Array<DeliveryAttempts.DeliveryAttemptInput> = [];
|
||||
const queuedJobs: Array<SignedApnsDeliveryJob> = [];
|
||||
const queuedStarts: Array<
|
||||
Parameters<LiveActivities.LiveActivities["Service"]["markStartQueued"]>[0]
|
||||
> = [];
|
||||
const markedDeliveries: Array<
|
||||
Parameters<LiveActivities.LiveActivities["Service"]["markDelivery"]>[0]
|
||||
> = [];
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const deliveries = yield* ApnsDeliveries.ApnsDeliveries;
|
||||
const result = yield* deliveries.sendForTarget({
|
||||
target: {
|
||||
...target,
|
||||
activity_push_token: null,
|
||||
remote_started_at: null,
|
||||
ended_at: "1970-01-01T00:00:05.000Z",
|
||||
},
|
||||
aggregate,
|
||||
nowMs: 10_000,
|
||||
});
|
||||
|
||||
expect(result?.kind).toBe("live_activity_start");
|
||||
expect(result?.ok).toBe(true);
|
||||
expect(queuedJobs).toMatchObject([
|
||||
{
|
||||
payload: {
|
||||
kind: "live_activity_start",
|
||||
target: {
|
||||
token: "start-token",
|
||||
},
|
||||
},
|
||||
},
|
||||
]);
|
||||
// Activities are armed by the app in the foreground; the relay never
|
||||
// uses the push-to-start token, so the only fallback is the push
|
||||
// notification channel (none here: the aggregate is not an attention
|
||||
// phase).
|
||||
expect(result).toBeNull();
|
||||
expect(queuedJobs).toEqual([]);
|
||||
expect(queuedStarts).toEqual([]);
|
||||
expect(attempts).toEqual([]);
|
||||
expect(queuedStarts).toMatchObject([
|
||||
{
|
||||
userId: target.user_id,
|
||||
deviceId: target.device_id,
|
||||
},
|
||||
]);
|
||||
expect(markedDeliveries).toEqual([]);
|
||||
}).pipe(Effect.provide(makeLayer({ attempts, queuedJobs, queuedStarts, markedDeliveries })));
|
||||
}).pipe(Effect.provide(makeLayer({ attempts, queuedJobs, queuedStarts })));
|
||||
});
|
||||
|
||||
it.effect("queues an end using the activity token", () => {
|
||||
it.effect("ends the armed card when nothing remains to show", () => {
|
||||
const attempts: Array<DeliveryAttempts.DeliveryAttemptInput> = [];
|
||||
const queuedJobs: Array<SignedApnsDeliveryJob> = [];
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const deliveries = yield* ApnsDeliveries.ApnsDeliveries;
|
||||
const result = yield* deliveries.sendForTarget({
|
||||
// Within the freshly-armed grace window an empty aggregate delivers
|
||||
// nothing: the environment's first publish may still be in flight.
|
||||
const graced = yield* deliveries.sendForTarget({
|
||||
target,
|
||||
aggregate: null,
|
||||
nowMs: 5_000,
|
||||
});
|
||||
expect(graced).toBeNull();
|
||||
|
||||
const result = yield* deliveries.sendForTarget({
|
||||
target,
|
||||
aggregate: null,
|
||||
nowMs: 5_000 + 3 * 60 * 1_000,
|
||||
});
|
||||
|
||||
// An armed card always shows content (live or recently finished work);
|
||||
// once the aggregate is empty the card ends rather than rendering an
|
||||
// empty state. The app re-arms on the next open with content.
|
||||
expect(result?.kind).toBe("live_activity_end");
|
||||
expect(result?.ok).toBe(true);
|
||||
expect(queuedJobs).toMatchObject([
|
||||
@@ -353,7 +373,7 @@ describe("ApnsDeliveries", () => {
|
||||
...aggregate,
|
||||
title: longTitle,
|
||||
subtitle: longTitle,
|
||||
activities: [0, 1, 2, 3].map((index) =>
|
||||
activities: [0, 1, 2, 3, 4, 5].map((index) =>
|
||||
Object.assign({}, aggregate.activities[0]!, {
|
||||
projectTitle: longTitle,
|
||||
threadTitle: longTitle,
|
||||
@@ -368,12 +388,7 @@ describe("ApnsDeliveries", () => {
|
||||
return Effect.gen(function* () {
|
||||
const deliveries = yield* ApnsDeliveries.ApnsDeliveries;
|
||||
yield* deliveries.sendForTarget({
|
||||
target: {
|
||||
...target,
|
||||
activity_push_token: null,
|
||||
remote_started_at: null,
|
||||
ended_at: "1970-01-01T00:00:05.000Z",
|
||||
},
|
||||
target,
|
||||
aggregate: inputAggregate,
|
||||
nowMs: 10_000,
|
||||
});
|
||||
@@ -381,7 +396,7 @@ describe("ApnsDeliveries", () => {
|
||||
const payloadAggregate = queuedJobs[0]?.payload.aggregate;
|
||||
expect(payloadAggregate?.title.length).toBeLessThanOrEqual(120);
|
||||
expect(payloadAggregate?.subtitle.length).toBeLessThanOrEqual(120);
|
||||
expect(payloadAggregate?.activities).toHaveLength(3);
|
||||
expect(payloadAggregate?.activities).toHaveLength(5);
|
||||
expect(payloadAggregate?.activities[0]?.projectTitle.length).toBeLessThanOrEqual(120);
|
||||
expect(payloadAggregate?.activities[0]?.status.length).toBeLessThanOrEqual(40);
|
||||
expect(payloadAggregate?.activities[0]?.deepLink).toBe("/");
|
||||
@@ -389,6 +404,196 @@ describe("ApnsDeliveries", () => {
|
||||
}).pipe(Effect.provide(makeLayer({ attempts, queuedJobs })));
|
||||
});
|
||||
|
||||
it.effect("queues Live Activity jobs with the device's APNs routing", () => {
|
||||
const attempts: Array<DeliveryAttempts.DeliveryAttemptInput> = [];
|
||||
const queuedJobs: Array<SignedApnsDeliveryJob> = [];
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const deliveries = yield* ApnsDeliveries.ApnsDeliveries;
|
||||
yield* deliveries.sendForTarget({
|
||||
target: {
|
||||
...target,
|
||||
bundle_id: "com.t3tools.t3code.preview",
|
||||
aps_environment: "production",
|
||||
ended_at: "1970-01-01T00:00:05.000Z",
|
||||
},
|
||||
aggregate,
|
||||
nowMs: 10_000,
|
||||
});
|
||||
|
||||
expect(queuedJobs).toMatchObject([
|
||||
{
|
||||
payload: {
|
||||
kind: "live_activity_update",
|
||||
target: {
|
||||
token: "activity-token",
|
||||
bundleId: "com.t3tools.t3code.preview",
|
||||
apsEnvironment: "production",
|
||||
},
|
||||
},
|
||||
},
|
||||
]);
|
||||
}).pipe(Effect.provide(makeLayer({ attempts, queuedJobs })));
|
||||
});
|
||||
|
||||
it.effect("sends signed jobs to the device's APNs environment and bundle topic", () => {
|
||||
const attempts: Array<DeliveryAttempts.DeliveryAttemptInput> = [];
|
||||
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
|
||||
const payload = makeApnsDeliveryJobPayload({
|
||||
kind: "live_activity_update",
|
||||
userId: target.user_id,
|
||||
deviceId: target.device_id,
|
||||
token: "activity-token",
|
||||
bundleId: "com.t3tools.t3code.preview",
|
||||
apsEnvironment: "sandbox",
|
||||
aggregate,
|
||||
createdAt: "1970-01-01T00:00:00.000Z",
|
||||
expiresAt: "1970-01-01T00:10:00.000Z",
|
||||
jobId: "job-routing-1",
|
||||
});
|
||||
const signed = signApnsDeliveryJob({
|
||||
secret: config.apnsDeliveryJobSigningSecret,
|
||||
payload,
|
||||
});
|
||||
const execute = (request: HttpClientRequest.HttpClientRequest) =>
|
||||
Effect.sync(() => {
|
||||
requests.push(request);
|
||||
return HttpClientResponse.fromWeb(request, new Response("", { status: 200 }));
|
||||
});
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const deliveries = yield* ApnsDeliveries.ApnsDeliveries;
|
||||
const result = yield* deliveries.processSignedJob(signed);
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
expect(requests).toHaveLength(1);
|
||||
expect(requests[0]?.url).toBe("https://api.sandbox.push.apple.com/3/device/activity-token");
|
||||
expect(requests[0]?.headers["apns-topic"]).toBe(
|
||||
"com.t3tools.t3code.preview.push-type.liveactivity",
|
||||
);
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
makeLayer({
|
||||
attempts,
|
||||
config: signingConfig,
|
||||
execute,
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it.effect(
|
||||
"suppresses all deliveries when the aggregate is unchanged while a row awaits input",
|
||||
() => {
|
||||
const attempts: Array<DeliveryAttempts.DeliveryAttemptInput> = [];
|
||||
const queuedJobs: Array<SignedApnsDeliveryJob> = [];
|
||||
const waitingAggregate: RelayAgentActivityAggregateState = {
|
||||
...aggregate,
|
||||
activities: [
|
||||
{
|
||||
...aggregate.activities[0]!,
|
||||
phase: "waiting_for_input",
|
||||
status: "Input",
|
||||
},
|
||||
],
|
||||
};
|
||||
const waitingAggregateJson = JSON.stringify(waitingAggregate);
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const deliveries = yield* ApnsDeliveries.ApnsDeliveries;
|
||||
const result = yield* deliveries.sendForTarget({
|
||||
target: {
|
||||
...target,
|
||||
// A registered alert token must not turn the suppressed Live
|
||||
// Activity update into an alert push on every republish.
|
||||
push_token: "apns-device-token",
|
||||
last_aggregate_json: waitingAggregateJson,
|
||||
last_live_activity_delivery_at: "1970-01-01T00:00:04.000Z",
|
||||
},
|
||||
aggregate: waitingAggregate,
|
||||
nowMs: 5_000,
|
||||
});
|
||||
|
||||
expect(result).toBeNull();
|
||||
expect(queuedJobs).toEqual([]);
|
||||
expect(attempts).toEqual([]);
|
||||
}).pipe(Effect.provide(makeLayer({ attempts, queuedJobs })));
|
||||
},
|
||||
);
|
||||
|
||||
it.effect(
|
||||
"queues an update inside the throttle window when a changed aggregate awaits input",
|
||||
() => {
|
||||
const attempts: Array<DeliveryAttempts.DeliveryAttemptInput> = [];
|
||||
const queuedJobs: Array<SignedApnsDeliveryJob> = [];
|
||||
const waitingAggregate: RelayAgentActivityAggregateState = {
|
||||
...aggregate,
|
||||
activities: [
|
||||
{
|
||||
...aggregate.activities[0]!,
|
||||
phase: "waiting_for_input",
|
||||
status: "Input",
|
||||
},
|
||||
],
|
||||
};
|
||||
const previousAggregateJson = JSON.stringify(aggregate);
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const deliveries = yield* ApnsDeliveries.ApnsDeliveries;
|
||||
const result = yield* deliveries.sendForTarget({
|
||||
target: {
|
||||
...target,
|
||||
last_aggregate_json: previousAggregateJson,
|
||||
last_live_activity_delivery_at: "1970-01-01T00:00:04.000Z",
|
||||
},
|
||||
aggregate: waitingAggregate,
|
||||
nowMs: 5_000,
|
||||
});
|
||||
|
||||
expect(result?.kind).toBe("live_activity_update");
|
||||
expect(queuedJobs).toMatchObject([
|
||||
{
|
||||
payload: {
|
||||
kind: "live_activity_update",
|
||||
target: {
|
||||
token: "activity-token",
|
||||
},
|
||||
},
|
||||
},
|
||||
]);
|
||||
}).pipe(Effect.provide(makeLayer({ attempts, queuedJobs })));
|
||||
},
|
||||
);
|
||||
|
||||
it.effect(
|
||||
"throttles updates for changed aggregates with stable counts and no pending attention",
|
||||
() => {
|
||||
const attempts: Array<DeliveryAttempts.DeliveryAttemptInput> = [];
|
||||
const queuedJobs: Array<SignedApnsDeliveryJob> = [];
|
||||
const changedAggregate: RelayAgentActivityAggregateState = {
|
||||
...aggregate,
|
||||
updatedAt: "1970-01-01T00:00:04.000Z",
|
||||
};
|
||||
const previousAggregateJson = JSON.stringify(aggregate);
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const deliveries = yield* ApnsDeliveries.ApnsDeliveries;
|
||||
const result = yield* deliveries.sendForTarget({
|
||||
target: {
|
||||
...target,
|
||||
last_aggregate_json: previousAggregateJson,
|
||||
last_live_activity_delivery_at: "1970-01-01T00:00:04.000Z",
|
||||
},
|
||||
aggregate: changedAggregate,
|
||||
nowMs: 5_000,
|
||||
});
|
||||
|
||||
expect(result).toBeNull();
|
||||
expect(queuedJobs).toEqual([]);
|
||||
}).pipe(Effect.provide(makeLayer({ attempts, queuedJobs })));
|
||||
},
|
||||
);
|
||||
|
||||
it.effect("queues an end for an active Live Activity when Live Activities are disabled", () => {
|
||||
const attempts: Array<DeliveryAttempts.DeliveryAttemptInput> = [];
|
||||
const queuedJobs: Array<SignedApnsDeliveryJob> = [];
|
||||
@@ -629,6 +834,48 @@ describe("ApnsDeliveries", () => {
|
||||
}).pipe(Effect.provide(makeLayer({ attempts })));
|
||||
});
|
||||
|
||||
it.effect("skips a queued start when the user no longer has live work", () => {
|
||||
const attempts: Array<DeliveryAttempts.DeliveryAttemptInput> = [];
|
||||
const clearedStarts: Array<
|
||||
Parameters<LiveActivities.LiveActivities["Service"]["clearStartQueued"]>[0]
|
||||
> = [];
|
||||
const payload = makeApnsDeliveryJobPayload({
|
||||
kind: "live_activity_start",
|
||||
userId: target.user_id,
|
||||
deviceId: target.device_id,
|
||||
token: target.push_to_start_token ?? "start-token",
|
||||
aggregate,
|
||||
createdAt: "1970-01-01T00:00:00.000Z",
|
||||
expiresAt: "1970-01-01T00:10:00.000Z",
|
||||
jobId: "job-start-1",
|
||||
});
|
||||
const signed = signApnsDeliveryJob({
|
||||
secret: config.apnsDeliveryJobSigningSecret,
|
||||
payload,
|
||||
});
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const deliveries = yield* ApnsDeliveries.ApnsDeliveries;
|
||||
const result = yield* deliveries.processSignedJob(signed);
|
||||
|
||||
// The start was decided from an aggregate that a newer terminal publish
|
||||
// has since invalidated; delivering it would birth an orphan activity.
|
||||
expect(result).toMatchObject({
|
||||
kind: "live_activity_start",
|
||||
ok: true,
|
||||
apnsStatus: null,
|
||||
});
|
||||
expect(attempts).toMatchObject([
|
||||
{
|
||||
kind: "live_activity_start",
|
||||
sourceJobId: "job-start-1",
|
||||
apnsReason: "Stale APNs start job skipped.",
|
||||
},
|
||||
]);
|
||||
expect(clearedStarts).toMatchObject([{ userId: target.user_id, deviceId: target.device_id }]);
|
||||
}).pipe(Effect.provide(makeLayer({ attempts, clearedStarts, activityStates: [] })));
|
||||
});
|
||||
|
||||
it.effect("processes signed jobs through APNs and records attempts", () => {
|
||||
const attempts: Array<DeliveryAttempts.DeliveryAttemptInput> = [];
|
||||
const transportErrors: Array<ApnsDeliveries.ApnsDeliveryTransportError> = [];
|
||||
@@ -1177,3 +1424,171 @@ describe("ApnsDeliveries", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("live activity alert decisions", () => {
|
||||
const preferences = {
|
||||
liveActivitiesEnabled: true,
|
||||
notificationsEnabled: true,
|
||||
notifyOnApproval: true,
|
||||
notifyOnInput: true,
|
||||
notifyOnCompletion: true,
|
||||
notifyOnFailure: true,
|
||||
};
|
||||
|
||||
const attentionRow = {
|
||||
...aggregate.activities[0]!,
|
||||
threadId: "thread-2" as RelayAgentActivityState["threadId"],
|
||||
threadTitle: "Blocked thread",
|
||||
phase: "waiting_for_approval" as const,
|
||||
status: "Approval",
|
||||
};
|
||||
|
||||
it("alerts when a thread newly enters an attention phase", () => {
|
||||
const alert = ApnsDeliveries.alertForAttentionTransition({
|
||||
previousAggregate: aggregate,
|
||||
nextAggregate: {
|
||||
...aggregate,
|
||||
activeCount: 2,
|
||||
activities: [...aggregate.activities, attentionRow],
|
||||
},
|
||||
preferences,
|
||||
});
|
||||
expect(alert).toEqual({ title: "Blocked thread", body: "Approval: Project" });
|
||||
});
|
||||
|
||||
it("stays silent when the attention phase was already delivered", () => {
|
||||
const withAttention = {
|
||||
...aggregate,
|
||||
activeCount: 2,
|
||||
activities: [...aggregate.activities, attentionRow],
|
||||
};
|
||||
expect(
|
||||
ApnsDeliveries.alertForAttentionTransition({
|
||||
previousAggregate: withAttention,
|
||||
nextAggregate: withAttention,
|
||||
preferences,
|
||||
}),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("stays silent without a delivered baseline so replays cannot buzz", () => {
|
||||
expect(
|
||||
ApnsDeliveries.alertForAttentionTransition({
|
||||
previousAggregate: null,
|
||||
nextAggregate: { ...aggregate, activities: [attentionRow] },
|
||||
preferences,
|
||||
}),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("honors the per-event notification switch for attention alerts", () => {
|
||||
expect(
|
||||
ApnsDeliveries.alertForAttentionTransition({
|
||||
previousAggregate: aggregate,
|
||||
nextAggregate: {
|
||||
...aggregate,
|
||||
activeCount: 2,
|
||||
activities: [...aggregate.activities, attentionRow],
|
||||
},
|
||||
preferences: { ...preferences, notifyOnApproval: false },
|
||||
}),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("summarizes multiple newly blocked threads in one alert", () => {
|
||||
const secondAttentionRow = {
|
||||
...attentionRow,
|
||||
threadId: "thread-3" as RelayAgentActivityState["threadId"],
|
||||
threadTitle: "Other blocked thread",
|
||||
phase: "waiting_for_input" as const,
|
||||
status: "Input",
|
||||
};
|
||||
const alert = ApnsDeliveries.alertForAttentionTransition({
|
||||
previousAggregate: aggregate,
|
||||
nextAggregate: {
|
||||
...aggregate,
|
||||
activeCount: 3,
|
||||
activities: [...aggregate.activities, attentionRow, secondAttentionRow],
|
||||
},
|
||||
preferences,
|
||||
});
|
||||
expect(alert).toEqual({
|
||||
title: "2 agents need attention",
|
||||
body: "Blocked thread, Other blocked thread",
|
||||
});
|
||||
});
|
||||
|
||||
it("alerts for a terminal aggregate and honors the completion switch", () => {
|
||||
const terminalAggregate = {
|
||||
...aggregate,
|
||||
activeCount: 0,
|
||||
activities: [{ ...aggregate.activities[0]!, phase: "completed" as const, status: "Done" }],
|
||||
};
|
||||
expect(
|
||||
ApnsDeliveries.alertForTerminalAggregate({ aggregate: terminalAggregate, preferences }),
|
||||
).toEqual({ title: "Thread", body: "Done: Project" });
|
||||
expect(
|
||||
ApnsDeliveries.alertForTerminalAggregate({
|
||||
aggregate: terminalAggregate,
|
||||
preferences: { ...preferences, notifyOnCompletion: false },
|
||||
}),
|
||||
).toBeNull();
|
||||
expect(ApnsDeliveries.alertForTerminalAggregate({ aggregate: null, preferences })).toBeNull();
|
||||
});
|
||||
|
||||
it("alerts when a previously active thread finishes mid-flight", () => {
|
||||
const doneRow = {
|
||||
...aggregate.activities[0]!,
|
||||
phase: "completed" as const,
|
||||
status: "Done",
|
||||
};
|
||||
const next = {
|
||||
...aggregate,
|
||||
activeCount: 0,
|
||||
activities: [attentionRow, doneRow],
|
||||
};
|
||||
expect(
|
||||
ApnsDeliveries.alertForNewlyTerminal({
|
||||
previousAggregate: aggregate,
|
||||
nextAggregate: next,
|
||||
preferences,
|
||||
nowMs: 0,
|
||||
}),
|
||||
).toEqual({ title: "Thread", body: "Done: Project" });
|
||||
// The completion switch mutes it.
|
||||
expect(
|
||||
ApnsDeliveries.alertForNewlyTerminal({
|
||||
previousAggregate: aggregate,
|
||||
nextAggregate: next,
|
||||
preferences: { ...preferences, notifyOnCompletion: false },
|
||||
nowMs: 0,
|
||||
}),
|
||||
).toBeNull();
|
||||
// No baseline means no transition to ring on.
|
||||
expect(
|
||||
ApnsDeliveries.alertForNewlyTerminal({
|
||||
previousAggregate: null,
|
||||
nextAggregate: next,
|
||||
preferences,
|
||||
nowMs: 0,
|
||||
}),
|
||||
).toBeNull();
|
||||
// A Done row that was already terminal (or absent) before stays silent.
|
||||
expect(
|
||||
ApnsDeliveries.alertForNewlyTerminal({
|
||||
previousAggregate: next,
|
||||
nextAggregate: next,
|
||||
preferences,
|
||||
nowMs: 0,
|
||||
}),
|
||||
).toBeNull();
|
||||
expect(
|
||||
ApnsDeliveries.alertForNewlyTerminal({
|
||||
previousAggregate: { ...aggregate, activities: [attentionRow] },
|
||||
nextAggregate: next,
|
||||
preferences,
|
||||
nowMs: 0,
|
||||
}),
|
||||
).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -18,6 +18,8 @@ import * as Redacted from "effect/Redacted";
|
||||
import * as Schema from "effect/Schema";
|
||||
|
||||
import {
|
||||
isExpiredAgentActivityState,
|
||||
isTerminalPhase,
|
||||
sanitizeAgentActivityAggregateState,
|
||||
sanitizeApnsNotificationPayload,
|
||||
} from "./agentActivityPayloads.ts";
|
||||
@@ -26,12 +28,14 @@ import {
|
||||
ApnsDeliveryJobLiveActivityAggregateMissing,
|
||||
ApnsDeliveryJobPushNotificationMissing,
|
||||
ApnsDeliveryJobQueuePayloadInvalid,
|
||||
type ApnsLiveActivityAlert,
|
||||
type ApnsNotificationPayload,
|
||||
SignedApnsDeliveryJob,
|
||||
isApnsDeliveryJobVerificationError,
|
||||
verifySignedApnsDeliveryJob,
|
||||
type ApnsDeliveryJobVerificationError,
|
||||
} from "./apnsDeliveryJobs.ts";
|
||||
import * as AgentActivityRows from "./AgentActivityRows.ts";
|
||||
import * as DeliveryAttempts from "./DeliveryAttempts.ts";
|
||||
import * as LiveActivities from "./LiveActivities.ts";
|
||||
import * as RelayConfiguration from "../Config.ts";
|
||||
@@ -39,6 +43,10 @@ import * as ApnsDeliveryQueue from "./ApnsDeliveryQueue.ts";
|
||||
import { withSpanAttributes } from "../observability.ts";
|
||||
|
||||
const MIN_LIVE_ACTIVITY_UPDATE_INTERVAL_MS = 15_000;
|
||||
// How long a just-armed card may sit with an empty aggregate before an end is
|
||||
// warranted; covers the gap between arming on send and the environment's
|
||||
// first publish reaching the relay.
|
||||
const FRESHLY_ARMED_GRACE_MS = 2 * 60 * 1_000;
|
||||
const PERMANENT_APNS_TOKEN_REASONS = new Set([
|
||||
"BadDeviceToken",
|
||||
"DeviceTokenNotForTopic",
|
||||
@@ -55,11 +63,13 @@ type ChosenLiveActivityDelivery =
|
||||
readonly kind: "live_activity_start" | "live_activity_update";
|
||||
readonly token: string;
|
||||
readonly aggregate: RelayAgentActivityAggregateState;
|
||||
readonly alert: ApnsLiveActivityAlert | null;
|
||||
}
|
||||
| {
|
||||
readonly kind: "live_activity_end";
|
||||
readonly token: string;
|
||||
readonly aggregate: RelayAgentActivityAggregateState | null;
|
||||
readonly alert: ApnsLiveActivityAlert | null;
|
||||
};
|
||||
|
||||
type ChosenPushNotificationDelivery = {
|
||||
@@ -130,6 +140,154 @@ function parsePreferences(value: string): RelayAgentAwarenessPreferences | null
|
||||
return Option.getOrNull(decodeRelayAgentAwarenessPreferencesJson(value));
|
||||
}
|
||||
|
||||
function aggregateNeedsAttention(aggregate: RelayAgentActivityAggregateState): boolean {
|
||||
return aggregate.activities.some(
|
||||
(row) => row.phase === "waiting_for_approval" || row.phase === "waiting_for_input",
|
||||
);
|
||||
}
|
||||
|
||||
function isAttentionPhase(phase: string): boolean {
|
||||
return phase === "waiting_for_approval" || phase === "waiting_for_input";
|
||||
}
|
||||
|
||||
// Honors the same per-event notification switches the push channel uses; a
|
||||
// missing/corrupt preferences blob only disables nothing (matching how the
|
||||
// liveActivitiesEnabled check treats it), since every registration writes one.
|
||||
function alertAllowedForPhase(
|
||||
preferences: RelayAgentAwarenessPreferences | null,
|
||||
phase: string,
|
||||
): boolean {
|
||||
if (preferences === null) {
|
||||
return true;
|
||||
}
|
||||
switch (phase) {
|
||||
case "waiting_for_approval":
|
||||
return preferences.notifyOnApproval;
|
||||
case "waiting_for_input":
|
||||
return preferences.notifyOnInput;
|
||||
case "completed":
|
||||
return preferences.notifyOnCompletion;
|
||||
case "failed":
|
||||
return preferences.notifyOnFailure;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Alert copy for an update whose aggregate contains threads that were NOT in an
|
||||
// attention phase in the previously delivered aggregate. A null previous
|
||||
// aggregate means there is no known baseline (fresh registration, replay after
|
||||
// data loss) — alerting there would buzz on reconnect, not on a transition.
|
||||
export function alertForAttentionTransition(input: {
|
||||
readonly previousAggregate: RelayAgentActivityAggregateState | null;
|
||||
readonly nextAggregate: RelayAgentActivityAggregateState;
|
||||
readonly preferences: RelayAgentAwarenessPreferences | null;
|
||||
}): ApnsLiveActivityAlert | null {
|
||||
if (input.previousAggregate === null) {
|
||||
return null;
|
||||
}
|
||||
const previouslyAttention = new Set(
|
||||
input.previousAggregate.activities
|
||||
.filter((row) => isAttentionPhase(row.phase))
|
||||
.map((row) => row.threadId),
|
||||
);
|
||||
const newlyAttention = input.nextAggregate.activities.filter(
|
||||
(row) =>
|
||||
isAttentionPhase(row.phase) &&
|
||||
!previouslyAttention.has(row.threadId) &&
|
||||
alertAllowedForPhase(input.preferences, row.phase),
|
||||
);
|
||||
const first = newlyAttention[0];
|
||||
if (!first) {
|
||||
return null;
|
||||
}
|
||||
if (newlyAttention.length === 1) {
|
||||
return { title: first.threadTitle, body: `${first.status}: ${first.projectTitle}` };
|
||||
}
|
||||
return {
|
||||
title: `${newlyAttention.length} agents need attention`,
|
||||
body: newlyAttention.map((row) => row.threadTitle).join(", "),
|
||||
};
|
||||
}
|
||||
|
||||
// Alert copy for an update whose aggregate contains threads that finished
|
||||
// (Done/Failed) since the previously delivered aggregate — the mid-flight
|
||||
// completion buzz while other agents keep the activity alive. Requires the
|
||||
// thread to have been present and non-terminal before, so a baseline-less
|
||||
// replay or a row that merely fell off the display cap never rings.
|
||||
function newlyTerminalRows(
|
||||
previousAggregate: RelayAgentActivityAggregateState | null,
|
||||
nextAggregate: RelayAgentActivityAggregateState,
|
||||
): ReadonlyArray<RelayAgentActivityAggregateState["activities"][number]> {
|
||||
if (previousAggregate === null) {
|
||||
return [];
|
||||
}
|
||||
const previousPhases = new Map(
|
||||
previousAggregate.activities.map((row) => [row.threadId, row.phase]),
|
||||
);
|
||||
return nextAggregate.activities.filter((row) => {
|
||||
if (row.phase !== "completed" && row.phase !== "failed") {
|
||||
return false;
|
||||
}
|
||||
const previousPhase = previousPhases.get(row.threadId);
|
||||
return (
|
||||
previousPhase !== undefined && previousPhase !== "completed" && previousPhase !== "failed"
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function isFreshTerminalRow(
|
||||
row: RelayAgentActivityAggregateState["activities"][number],
|
||||
nowMs: number,
|
||||
): boolean {
|
||||
const updatedAtMs = Option.match(DateTime.make(row.updatedAt), {
|
||||
onNone: () => null,
|
||||
onSome: (dt) => dt.epochMilliseconds,
|
||||
});
|
||||
return updatedAtMs !== null && nowMs - updatedAtMs <= TERMINAL_NOTIFICATION_FRESHNESS_MS;
|
||||
}
|
||||
|
||||
export function alertForNewlyTerminal(input: {
|
||||
readonly previousAggregate: RelayAgentActivityAggregateState | null;
|
||||
readonly nextAggregate: RelayAgentActivityAggregateState;
|
||||
readonly preferences: RelayAgentAwarenessPreferences | null;
|
||||
readonly nowMs: number;
|
||||
}): ApnsLiveActivityAlert | null {
|
||||
const newlyTerminal = newlyTerminalRows(input.previousAggregate, input.nextAggregate).filter(
|
||||
(row) =>
|
||||
alertAllowedForPhase(input.preferences, row.phase) &&
|
||||
// Replays of old aggregates (server restarts, redeliveries) repaint
|
||||
// state without ringing; only fresh completions buzz.
|
||||
isFreshTerminalRow(row, input.nowMs),
|
||||
);
|
||||
const first = newlyTerminal[0];
|
||||
if (!first) {
|
||||
return null;
|
||||
}
|
||||
if (newlyTerminal.length === 1) {
|
||||
return { title: first.threadTitle, body: `${first.status}: ${first.projectTitle}` };
|
||||
}
|
||||
return {
|
||||
title: `${newlyTerminal.length} agents finished`,
|
||||
body: newlyTerminal.map((row) => row.threadTitle).join(", "),
|
||||
};
|
||||
}
|
||||
|
||||
// Alert copy for an end event carrying a terminal (Done/Failed) aggregate.
|
||||
export function alertForTerminalAggregate(input: {
|
||||
readonly aggregate: RelayAgentActivityAggregateState | null;
|
||||
readonly preferences: RelayAgentAwarenessPreferences | null;
|
||||
}): ApnsLiveActivityAlert | null {
|
||||
const row = input.aggregate?.activities[0];
|
||||
if (!row || (row.phase !== "completed" && row.phase !== "failed")) {
|
||||
return null;
|
||||
}
|
||||
if (!alertAllowedForPhase(input.preferences, row.phase)) {
|
||||
return null;
|
||||
}
|
||||
return { title: row.threadTitle, body: `${row.status}: ${row.projectTitle}` };
|
||||
}
|
||||
|
||||
function shouldUpdateLiveActivity(input: {
|
||||
readonly previousAggregate: RelayAgentActivityAggregateState | null;
|
||||
readonly nextAggregate: RelayAgentActivityAggregateState;
|
||||
@@ -139,11 +297,20 @@ function shouldUpdateLiveActivity(input: {
|
||||
if (!input.previousAggregate) {
|
||||
return true;
|
||||
}
|
||||
if (JSON.stringify(input.previousAggregate) === JSON.stringify(input.nextAggregate)) {
|
||||
return false;
|
||||
}
|
||||
if (input.previousAggregate.activeCount !== input.nextAggregate.activeCount) {
|
||||
return true;
|
||||
}
|
||||
if (JSON.stringify(input.previousAggregate) === JSON.stringify(input.nextAggregate)) {
|
||||
return false;
|
||||
if (aggregateNeedsAttention(input.nextAggregate)) {
|
||||
return true;
|
||||
}
|
||||
// A thread finishing must never be throttled away: when a completion and a
|
||||
// new start land in the same window, activeCount is unchanged and the Done
|
||||
// transition (and its alert) would otherwise be suppressed.
|
||||
if (newlyTerminalRows(input.previousAggregate, input.nextAggregate).length > 0) {
|
||||
return true;
|
||||
}
|
||||
const lastDeliveryAtMs =
|
||||
input.lastDeliveryAt === null
|
||||
@@ -159,9 +326,14 @@ function shouldUpdateLiveActivity(input: {
|
||||
);
|
||||
}
|
||||
|
||||
// Completions replayed long after the fact (server restarts republish every
|
||||
// recently-finished thread) must not ring the device again.
|
||||
const TERMINAL_NOTIFICATION_FRESHNESS_MS = 2 * 60 * 1_000;
|
||||
|
||||
function notificationForAggregate(input: {
|
||||
readonly target: LiveActivities.TargetRow;
|
||||
readonly aggregate: RelayAgentActivityAggregateState | null;
|
||||
readonly nowMs: number;
|
||||
}): ApnsNotificationPayload | null {
|
||||
if (!input.target.push_token || input.aggregate === null) {
|
||||
return null;
|
||||
@@ -174,6 +346,15 @@ function notificationForAggregate(input: {
|
||||
if (!activity) {
|
||||
return null;
|
||||
}
|
||||
if (activity.phase === "completed" || activity.phase === "failed") {
|
||||
const updatedAtMs = Option.match(DateTime.make(activity.updatedAt), {
|
||||
onNone: () => null,
|
||||
onSome: (dt) => dt.epochMilliseconds,
|
||||
});
|
||||
if (updatedAtMs === null || input.nowMs - updatedAtMs > TERMINAL_NOTIFICATION_FRESHNESS_MS) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
const enabled =
|
||||
(activity.phase === "waiting_for_approval" && preferences.notifyOnApproval) ||
|
||||
(activity.phase === "waiting_for_input" && preferences.notifyOnInput) ||
|
||||
@@ -191,62 +372,85 @@ function notificationForAggregate(input: {
|
||||
};
|
||||
}
|
||||
|
||||
// "suppressed" means a Live Activity owns this state but no update is due
|
||||
// (unchanged or throttled); callers must not fall back to an alert push, or
|
||||
// every republish of a waiting aggregate would ring the device.
|
||||
function chooseLiveActivityDelivery(input: {
|
||||
readonly target: LiveActivities.TargetRow;
|
||||
readonly aggregate: RelayAgentActivityAggregateState | null;
|
||||
readonly nowMs: number;
|
||||
}): ChosenLiveActivityDelivery | null {
|
||||
const hasActiveActivity =
|
||||
input.target.ended_at === null &&
|
||||
(input.target.remote_start_queued_at !== null ||
|
||||
input.target.remote_started_at !== null ||
|
||||
input.target.activity_push_token !== null);
|
||||
}): ChosenLiveActivityDelivery | "suppressed" | null {
|
||||
const preferences = parsePreferences(input.target.preferences_json);
|
||||
if (preferences?.liveActivitiesEnabled === false) {
|
||||
return hasActiveActivity && input.target.activity_push_token
|
||||
return input.target.activity_push_token
|
||||
? {
|
||||
kind: "live_activity_end",
|
||||
token: input.target.activity_push_token,
|
||||
aggregate: null,
|
||||
alert: null,
|
||||
}
|
||||
: null;
|
||||
}
|
||||
if (input.aggregate === null || input.aggregate.activeCount === 0) {
|
||||
return hasActiveActivity && input.target.activity_push_token
|
||||
? {
|
||||
kind: "live_activity_end",
|
||||
token: input.target.activity_push_token,
|
||||
aggregate: input.aggregate,
|
||||
}
|
||||
: null;
|
||||
}
|
||||
if (!hasActiveActivity) {
|
||||
return input.target.push_to_start_token
|
||||
? {
|
||||
kind: "live_activity_start",
|
||||
token: input.target.push_to_start_token,
|
||||
aggregate: input.aggregate,
|
||||
}
|
||||
: null;
|
||||
}
|
||||
// Activities are started by the app in the foreground, never remotely.
|
||||
// Without a registered token there is nothing addressable; attention
|
||||
// transitions fall back to the push notification channel until the user
|
||||
// next arms the card from the app.
|
||||
if (!input.target.activity_push_token) {
|
||||
return null;
|
||||
}
|
||||
// An armed card always shows content: live agents, or recently finished
|
||||
// ones (the publisher keeps Done/Failed rows in the aggregate for a
|
||||
// while). A null aggregate means there is truly nothing left to show, so
|
||||
// the card ends — arming is cheap now that the app re-arms on any open
|
||||
// with content.
|
||||
if (input.aggregate === null) {
|
||||
// Except right after arming: the app arms the card the moment the user
|
||||
// starts work, and the token registration's replay can land before the
|
||||
// environment's first publish for the brand-new thread. Ending here
|
||||
// would retire the token and orphan the card at its seed content, so a
|
||||
// freshly armed card keeps its seed until real state arrives.
|
||||
const armedAtMs = Option.match(
|
||||
input.target.remote_started_at === null
|
||||
? Option.none()
|
||||
: DateTime.make(input.target.remote_started_at),
|
||||
{ onNone: () => null, onSome: (dt) => dt.epochMilliseconds },
|
||||
);
|
||||
if (armedAtMs !== null && input.nowMs - armedAtMs < FRESHLY_ARMED_GRACE_MS) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
kind: "live_activity_end",
|
||||
token: input.target.activity_push_token,
|
||||
aggregate: null,
|
||||
alert: null,
|
||||
};
|
||||
}
|
||||
const nextAggregate = input.aggregate;
|
||||
const previousAggregate = parseAggregate(input.target.last_aggregate_json);
|
||||
return shouldUpdateLiveActivity({
|
||||
previousAggregate: parseAggregate(input.target.last_aggregate_json),
|
||||
nextAggregate: input.aggregate,
|
||||
previousAggregate,
|
||||
nextAggregate,
|
||||
lastDeliveryAt: input.target.last_live_activity_delivery_at,
|
||||
nowMs: input.nowMs,
|
||||
}) ||
|
||||
input.aggregate.activities.some(
|
||||
(row) => row.phase === "waiting_for_approval" || row.phase === "waiting_for_input",
|
||||
)
|
||||
})
|
||||
? {
|
||||
kind: "live_activity_update",
|
||||
token: input.target.activity_push_token,
|
||||
aggregate: input.aggregate,
|
||||
aggregate: nextAggregate,
|
||||
alert:
|
||||
alertForAttentionTransition({
|
||||
previousAggregate,
|
||||
nextAggregate,
|
||||
preferences,
|
||||
}) ??
|
||||
alertForNewlyTerminal({
|
||||
previousAggregate,
|
||||
nextAggregate,
|
||||
preferences,
|
||||
nowMs: input.nowMs,
|
||||
}),
|
||||
}
|
||||
: null;
|
||||
: "suppressed";
|
||||
}
|
||||
|
||||
function chooseDelivery(input: {
|
||||
@@ -255,6 +459,9 @@ function chooseDelivery(input: {
|
||||
readonly nowMs: number;
|
||||
}): ChosenDelivery | null {
|
||||
const liveActivityDelivery = chooseLiveActivityDelivery(input);
|
||||
if (liveActivityDelivery === "suppressed") {
|
||||
return null;
|
||||
}
|
||||
if (liveActivityDelivery) {
|
||||
return liveActivityDelivery;
|
||||
}
|
||||
@@ -365,6 +572,24 @@ const recoverApnsDeliveryTransportError = (
|
||||
interface LiveActivityDeliveryTarget {
|
||||
readonly user_id: string;
|
||||
readonly device_id: string;
|
||||
readonly bundle_id?: string | null;
|
||||
readonly aps_environment?: "sandbox" | "production" | null;
|
||||
}
|
||||
|
||||
// Devices register the bundle id and APS environment of the build they run
|
||||
// (dev/preview/prod variants have distinct bundle ids; development-signed
|
||||
// builds get sandbox tokens). Sending with mismatched routing yields
|
||||
// DeviceTokenNotForTopic/BadDeviceToken, so per-device values override the
|
||||
// relay-wide defaults when present.
|
||||
function credentialsForTarget(
|
||||
credentials: RelayConfiguration.RelayConfiguration["Service"]["apns"],
|
||||
target: LiveActivityDeliveryTarget,
|
||||
): RelayConfiguration.RelayConfiguration["Service"]["apns"] {
|
||||
return {
|
||||
...credentials,
|
||||
...(target.bundle_id ? { bundleId: target.bundle_id } : {}),
|
||||
...(target.aps_environment ? { environment: target.aps_environment } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
function expectedCurrentToken(input: {
|
||||
@@ -392,10 +617,12 @@ export type SendLiveActivityDeliveryInput =
|
||||
| (SendLiveActivityDeliveryInputBase & {
|
||||
readonly kind: "live_activity_start" | "live_activity_update";
|
||||
readonly aggregate: RelayAgentActivityAggregateState;
|
||||
readonly alert?: ApnsLiveActivityAlert | null;
|
||||
})
|
||||
| (SendLiveActivityDeliveryInputBase & {
|
||||
readonly kind: "live_activity_end";
|
||||
readonly aggregate: RelayAgentActivityAggregateState | null;
|
||||
readonly alert?: ApnsLiveActivityAlert | null;
|
||||
});
|
||||
|
||||
function makeLiveActivityDeliveryRequest(
|
||||
@@ -419,6 +646,7 @@ function makeLiveActivityDeliveryRequest(
|
||||
...base,
|
||||
event: deliveryEvent(input.kind),
|
||||
state: input.aggregate,
|
||||
alert: input.alert ?? null,
|
||||
}),
|
||||
};
|
||||
case "live_activity_end":
|
||||
@@ -429,6 +657,7 @@ function makeLiveActivityDeliveryRequest(
|
||||
...base,
|
||||
event: "end",
|
||||
state: input.aggregate,
|
||||
alert: input.alert ?? null,
|
||||
}),
|
||||
};
|
||||
}
|
||||
@@ -467,6 +696,31 @@ export const make = Effect.gen(function* () {
|
||||
const deliveryQueue = yield* ApnsDeliveryQueue.ApnsDeliveryQueue;
|
||||
const config = yield* RelayConfiguration.RelayConfiguration;
|
||||
const apns = yield* Apns.ApnsClient;
|
||||
const activityRows = yield* AgentActivityRows.AgentActivityRows;
|
||||
|
||||
// Start jobs are decided at publish time, but consecutive publishes land in
|
||||
// the same queue batch: a start chosen from a running aggregate can be
|
||||
// delivered moments after a newer terminal publish already ended the user's
|
||||
// work, birthing an orphan activity that shows stale content forever (no
|
||||
// token is ever registered for it, so nothing can update or end it).
|
||||
// Re-validate at delivery time that the user still has live work; fail open
|
||||
// on persistence errors so a database hiccup never drops a legitimate start.
|
||||
const userStillHasLiveWork = Effect.fnUntraced(function* (userId: string) {
|
||||
const now = yield* DateTime.now;
|
||||
return yield* activityRows.listForUser({ userId }).pipe(
|
||||
Effect.map((states) =>
|
||||
states.some(
|
||||
(state) =>
|
||||
!isTerminalPhase(state) && !isExpiredAgentActivityState(state, now.epochMilliseconds),
|
||||
),
|
||||
),
|
||||
Effect.catchCause((cause) =>
|
||||
Effect.logWarning("live-work recheck failed; allowing queued start", { cause }).pipe(
|
||||
Effect.as(true),
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
const isCurrentSignedJobToken = Effect.fnUntraced(function* (input: {
|
||||
readonly target: LiveActivityDeliveryTarget;
|
||||
@@ -538,9 +792,25 @@ export const make = Effect.gen(function* () {
|
||||
return staleJobResult({ deviceId: input.target.device_id, kind: input.kind });
|
||||
}
|
||||
}
|
||||
if (
|
||||
input.kind === "live_activity_start" &&
|
||||
!(yield* userStillHasLiveWork(input.target.user_id))
|
||||
) {
|
||||
yield* liveActivities.clearStartQueued({
|
||||
userId: input.target.user_id,
|
||||
deviceId: input.target.device_id,
|
||||
});
|
||||
if (input.sourceJobId) {
|
||||
yield* attempts.completeSourceJob({
|
||||
sourceJobId: input.sourceJobId,
|
||||
apnsReason: "Stale APNs start job skipped.",
|
||||
});
|
||||
}
|
||||
return staleJobResult({ deviceId: input.target.device_id, kind: input.kind });
|
||||
}
|
||||
const result = yield* apns
|
||||
.sendLiveActivityRequest({
|
||||
credentials: config.apns,
|
||||
credentials: credentialsForTarget(config.apns, input.target),
|
||||
request,
|
||||
issuedAtUnixSeconds: epochSeconds,
|
||||
})
|
||||
@@ -663,7 +933,7 @@ export const make = Effect.gen(function* () {
|
||||
}
|
||||
const result = yield* apns
|
||||
.sendPushNotificationRequest({
|
||||
credentials: config.apns,
|
||||
credentials: credentialsForTarget(config.apns, input.target),
|
||||
request,
|
||||
issuedAtUnixSeconds: epochSeconds,
|
||||
})
|
||||
@@ -752,22 +1022,28 @@ export const make = Effect.gen(function* () {
|
||||
target: {
|
||||
user_id: payload.target.userId,
|
||||
device_id: payload.target.deviceId,
|
||||
bundle_id: payload.target.bundleId ?? null,
|
||||
aps_environment: payload.target.apsEnvironment ?? null,
|
||||
},
|
||||
token: payload.target.token,
|
||||
sourceJobId: payload.jobId,
|
||||
kind: payload.kind,
|
||||
aggregate: payload.aggregate,
|
||||
alert: payload.alert ?? null,
|
||||
});
|
||||
case "live_activity_end":
|
||||
return sendLiveActivity({
|
||||
target: {
|
||||
user_id: payload.target.userId,
|
||||
device_id: payload.target.deviceId,
|
||||
bundle_id: payload.target.bundleId ?? null,
|
||||
aps_environment: payload.target.apsEnvironment ?? null,
|
||||
},
|
||||
token: payload.target.token,
|
||||
sourceJobId: payload.jobId,
|
||||
kind: payload.kind,
|
||||
aggregate: payload.aggregate,
|
||||
alert: payload.alert ?? null,
|
||||
});
|
||||
case "push_notification":
|
||||
if (payload.notification === null) {
|
||||
@@ -783,6 +1059,8 @@ export const make = Effect.gen(function* () {
|
||||
target: {
|
||||
user_id: payload.target.userId,
|
||||
device_id: payload.target.deviceId,
|
||||
bundle_id: payload.target.bundleId ?? null,
|
||||
aps_environment: payload.target.apsEnvironment ?? null,
|
||||
},
|
||||
token: payload.target.token,
|
||||
sourceJobId: payload.jobId,
|
||||
@@ -797,13 +1075,20 @@ export const make = Effect.gen(function* () {
|
||||
sendPushNotification,
|
||||
processSignedJob,
|
||||
sendPushNotificationForTarget: Effect.fnUntraced(function* (input) {
|
||||
const notification = notificationForAggregate(input);
|
||||
const now = yield* DateTime.now;
|
||||
const notification = notificationForAggregate({
|
||||
target: input.target,
|
||||
aggregate: input.aggregate,
|
||||
nowMs: now.epochMilliseconds,
|
||||
});
|
||||
const token = input.target.push_token;
|
||||
return yield* notification && token
|
||||
? deliveryQueue.enqueuePushNotification({
|
||||
userId: input.target.user_id,
|
||||
deviceId: input.target.device_id,
|
||||
token,
|
||||
bundleId: input.target.bundle_id,
|
||||
apsEnvironment: input.target.aps_environment,
|
||||
notification,
|
||||
})
|
||||
: Effect.succeed(null);
|
||||
@@ -822,26 +1107,47 @@ export const make = Effect.gen(function* () {
|
||||
userId: input.target.user_id,
|
||||
deviceId: input.target.device_id,
|
||||
token: delivery.token,
|
||||
bundleId: input.target.bundle_id,
|
||||
apsEnvironment: input.target.aps_environment,
|
||||
notification: delivery.notification,
|
||||
});
|
||||
return result;
|
||||
}
|
||||
const notification = notificationForAggregate({
|
||||
target: input.target,
|
||||
aggregate: input.aggregate,
|
||||
nowMs: input.nowMs,
|
||||
});
|
||||
// The end event doubles as the "task finished" moment. When a companion
|
||||
// push notification is about to ring the device (below), the activity end
|
||||
// stays silent; otherwise the end itself carries the alert so LA-only
|
||||
// users still get the buzz.
|
||||
const alert =
|
||||
delivery.kind === "live_activity_end"
|
||||
? notification && input.target.push_token
|
||||
? null
|
||||
: alertForTerminalAggregate({
|
||||
aggregate: delivery.aggregate,
|
||||
preferences: parsePreferences(input.target.preferences_json),
|
||||
})
|
||||
: delivery.alert;
|
||||
const result = yield* deliveryQueue.enqueueLiveActivity({
|
||||
userId: input.target.user_id,
|
||||
deviceId: input.target.device_id,
|
||||
kind: delivery.kind,
|
||||
token: delivery.token,
|
||||
bundleId: input.target.bundle_id,
|
||||
apsEnvironment: input.target.aps_environment,
|
||||
aggregate: delivery.aggregate,
|
||||
});
|
||||
const notification = notificationForAggregate({
|
||||
target: input.target,
|
||||
aggregate: input.aggregate,
|
||||
alert,
|
||||
});
|
||||
if (delivery.kind === "live_activity_end" && notification && input.target.push_token) {
|
||||
yield* deliveryQueue.enqueuePushNotification({
|
||||
userId: input.target.user_id,
|
||||
deviceId: input.target.device_id,
|
||||
token: input.target.push_token,
|
||||
bundleId: input.target.bundle_id,
|
||||
apsEnvironment: input.target.aps_environment,
|
||||
notification,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -58,12 +58,17 @@ export class ApnsDeliveryQueue extends Context.Service<
|
||||
readonly userId: string;
|
||||
readonly deviceId: string;
|
||||
readonly token: string;
|
||||
readonly bundleId?: string | null;
|
||||
readonly apsEnvironment?: "sandbox" | "production" | null;
|
||||
readonly aggregate: ApnsDeliveryJobPayload["aggregate"];
|
||||
readonly alert?: ApnsDeliveryJobPayload["alert"];
|
||||
}) => Effect.Effect<RelayDeliveryResult, ApnsDeliveryQueueError>;
|
||||
readonly enqueuePushNotification: (input: {
|
||||
readonly userId: string;
|
||||
readonly deviceId: string;
|
||||
readonly token: string;
|
||||
readonly bundleId?: string | null;
|
||||
readonly apsEnvironment?: "sandbox" | "production" | null;
|
||||
readonly notification: NonNullable<ApnsDeliveryJobPayload["notification"]>;
|
||||
}) => Effect.Effect<RelayDeliveryResult, ApnsDeliveryQueueError>;
|
||||
}
|
||||
@@ -160,6 +165,8 @@ export const make = Effect.gen(function* () {
|
||||
userId: input.userId,
|
||||
deviceId: input.deviceId,
|
||||
token: input.token,
|
||||
bundleId: input.bundleId,
|
||||
apsEnvironment: input.apsEnvironment,
|
||||
aggregate: null,
|
||||
notification: sanitizeApnsNotificationPayload(input.notification),
|
||||
jobId,
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
import * as NodeCrypto from "node:crypto";
|
||||
|
||||
import { describe, expect, it } from "@effect/vitest";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Redacted from "effect/Redacted";
|
||||
import * as Schema from "effect/Schema";
|
||||
|
||||
import * as ApnsProviderTokens from "./ApnsProviderTokens.ts";
|
||||
|
||||
const { privateKey } = NodeCrypto.generateKeyPairSync("ec", {
|
||||
namedCurve: "prime256v1",
|
||||
privateKeyEncoding: { type: "pkcs8", format: "pem" },
|
||||
publicKeyEncoding: { type: "spki", format: "pem" },
|
||||
});
|
||||
|
||||
const signingInput = {
|
||||
teamId: "team-1",
|
||||
keyId: "key-1",
|
||||
privateKey: Redacted.make(privateKey),
|
||||
};
|
||||
|
||||
const WINDOW = ApnsProviderTokens.APNS_JWT_REUSE_SECONDS;
|
||||
|
||||
const decodeJwtPayload = Schema.decodeEffect(
|
||||
Schema.fromJsonString(Schema.Struct({ iat: Schema.Number })),
|
||||
);
|
||||
|
||||
describe("ApnsProviderTokens", () => {
|
||||
it.effect("derives the byte-identical token across isolates within a window", () => {
|
||||
ApnsProviderTokens.__resetApnsProviderTokenCacheForTest();
|
||||
return Effect.gen(function* () {
|
||||
const tokens = yield* ApnsProviderTokens.ApnsProviderTokens;
|
||||
const first = yield* tokens.getJwt({ ...signingInput, issuedAtUnixSeconds: WINDOW + 10 });
|
||||
|
||||
// A fresh isolate has no cache; deterministic signing plus quantized
|
||||
// iat must still reproduce the exact same JWT for the same window.
|
||||
ApnsProviderTokens.__resetApnsProviderTokenCacheForTest();
|
||||
const second = yield* tokens.getJwt({
|
||||
...signingInput,
|
||||
issuedAtUnixSeconds: WINDOW * 2 - 1,
|
||||
});
|
||||
expect(second).toBe(first);
|
||||
|
||||
const payload = yield* decodeJwtPayload(
|
||||
Buffer.from(first.split(".")[1]!, "base64url").toString("utf8"),
|
||||
);
|
||||
expect(payload.iat).toBe(WINDOW);
|
||||
ApnsProviderTokens.__resetApnsProviderTokenCacheForTest();
|
||||
}).pipe(Effect.provide(ApnsProviderTokens.layer));
|
||||
});
|
||||
|
||||
it.effect("rolls to a new token at the window boundary", () => {
|
||||
ApnsProviderTokens.__resetApnsProviderTokenCacheForTest();
|
||||
return Effect.gen(function* () {
|
||||
const tokens = yield* ApnsProviderTokens.ApnsProviderTokens;
|
||||
const first = yield* tokens.getJwt({ ...signingInput, issuedAtUnixSeconds: WINDOW + 10 });
|
||||
const next = yield* tokens.getJwt({ ...signingInput, issuedAtUnixSeconds: WINDOW * 2 });
|
||||
expect(next).not.toBe(first);
|
||||
ApnsProviderTokens.__resetApnsProviderTokenCacheForTest();
|
||||
}).pipe(Effect.provide(ApnsProviderTokens.layer));
|
||||
});
|
||||
|
||||
it.effect("serves repeat pushes from the isolate cache without re-signing", () => {
|
||||
ApnsProviderTokens.__resetApnsProviderTokenCacheForTest();
|
||||
return Effect.gen(function* () {
|
||||
const tokens = yield* ApnsProviderTokens.ApnsProviderTokens;
|
||||
const first = yield* tokens.getJwt({ ...signingInput, issuedAtUnixSeconds: WINDOW + 10 });
|
||||
const again = yield* tokens.getJwt({ ...signingInput, issuedAtUnixSeconds: WINDOW + 500 });
|
||||
// Deterministic signing makes equality hold either way; toBe on the
|
||||
// exact string documents the cache contract.
|
||||
expect(again).toBe(first);
|
||||
ApnsProviderTokens.__resetApnsProviderTokenCacheForTest();
|
||||
}).pipe(Effect.provide(ApnsProviderTokens.layer));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,63 @@
|
||||
import * as Context from "effect/Context";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
|
||||
import {
|
||||
apnsProviderTokenCacheKey,
|
||||
makeApnsJwt,
|
||||
type ApnsJwtError,
|
||||
type ApnsJwtSigningInput,
|
||||
} from "./apnsJwt.ts";
|
||||
|
||||
// APNs requires REUSING the provider token: refreshing it more than roughly
|
||||
// once per 20 minutes returns 429 TooManyProviderTokenUpdates and drops the
|
||||
// push (observed live: bursty Live Activity updates got 429'd, leaving stale
|
||||
// lock-screen state). Reuse each signed JWT for most of its 60-minute
|
||||
// validity.
|
||||
export const APNS_JWT_REUSE_SECONDS = 45 * 60;
|
||||
|
||||
export class ApnsProviderTokens extends Context.Service<
|
||||
ApnsProviderTokens,
|
||||
{
|
||||
readonly getJwt: (input: ApnsJwtSigningInput) => Effect.Effect<string, ApnsJwtError>;
|
||||
}
|
||||
>()("t3code-relay/agentActivity/ApnsProviderTokens") {}
|
||||
|
||||
interface CachedProviderToken {
|
||||
readonly jwt: string;
|
||||
readonly issuedAtUnixSeconds: number;
|
||||
}
|
||||
|
||||
// Signing is deterministic (RFC 6979) and iat is quantized below, so every
|
||||
// isolate independently derives the byte-identical token for a window; this
|
||||
// map only avoids re-signing on every push. No shared storage is needed, and
|
||||
// no provider token is ever written anywhere.
|
||||
const isolateTokenCache = new Map<string, CachedProviderToken>();
|
||||
|
||||
export function __resetApnsProviderTokenCacheForTest(): void {
|
||||
isolateTokenCache.clear();
|
||||
}
|
||||
|
||||
// Quantize iat to the reuse window so all isolates agree on it. The token's
|
||||
// age stays under APNs' 60-minute limit, and the whole fleet rolls to the
|
||||
// next token at the same instant — one provider-token update per window.
|
||||
export function quantizedApnsJwtIssuedAt(nowUnixSeconds: number): number {
|
||||
return Math.floor(nowUnixSeconds / APNS_JWT_REUSE_SECONDS) * APNS_JWT_REUSE_SECONDS;
|
||||
}
|
||||
|
||||
export const make = () =>
|
||||
ApnsProviderTokens.of({
|
||||
getJwt: Effect.fnUntraced(function* (input) {
|
||||
const issuedAtUnixSeconds = quantizedApnsJwtIssuedAt(input.issuedAtUnixSeconds);
|
||||
const cacheKey = apnsProviderTokenCacheKey(input);
|
||||
const cached = isolateTokenCache.get(cacheKey);
|
||||
if (cached && cached.issuedAtUnixSeconds === issuedAtUnixSeconds) {
|
||||
return cached.jwt;
|
||||
}
|
||||
const jwt = yield* makeApnsJwt({ ...input, issuedAtUnixSeconds });
|
||||
isolateTokenCache.set(cacheKey, { jwt, issuedAtUnixSeconds });
|
||||
return jwt;
|
||||
}),
|
||||
});
|
||||
|
||||
export const layer = Layer.succeed(ApnsProviderTokens, make());
|
||||
@@ -15,6 +15,8 @@ const registration: RelayDeviceRegistrationRequest = {
|
||||
platform: "ios",
|
||||
iosMajorVersion: 18,
|
||||
appVersion: "1.0.0" as RelayDeviceRegistrationRequest["appVersion"],
|
||||
bundleId: "com.t3tools.t3code.preview" as RelayDeviceRegistrationRequest["bundleId"],
|
||||
apsEnvironment: "production",
|
||||
pushToken: "apns-device-token" as RelayDeviceRegistrationRequest["pushToken"],
|
||||
pushToStartToken: "push-to-start-token" as RelayDeviceRegistrationRequest["pushToStartToken"],
|
||||
preferences: {
|
||||
@@ -106,6 +108,8 @@ describe("Devices", () => {
|
||||
expect.objectContaining({
|
||||
userId: "user-2",
|
||||
deviceId: "device-1",
|
||||
bundleId: "com.t3tools.t3code.preview",
|
||||
apsEnvironment: "production",
|
||||
pushToken: "apns-device-token",
|
||||
pushToStartToken: "push-to-start-token",
|
||||
}),
|
||||
|
||||
@@ -81,45 +81,45 @@ export const make = Effect.gen(function* () {
|
||||
const updatedAt = DateTime.formatIso(yield* DateTime.now);
|
||||
const registration = input.registration;
|
||||
|
||||
yield* Effect.all(
|
||||
[
|
||||
registration.pushToken
|
||||
? db
|
||||
.update(relayMobileDevices)
|
||||
.set({ pushToken: null, updatedAt })
|
||||
.where(eq(relayMobileDevices.pushToken, registration.pushToken))
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new DeviceRegistrationPersistenceError({
|
||||
userId: input.userId,
|
||||
deviceId: registration.deviceId,
|
||||
stage: "claim-push-token",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
)
|
||||
: Effect.void,
|
||||
registration.pushToStartToken
|
||||
? db
|
||||
.update(relayMobileDevices)
|
||||
.set({ pushToStartToken: null, updatedAt })
|
||||
.where(eq(relayMobileDevices.pushToStartToken, registration.pushToStartToken))
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new DeviceRegistrationPersistenceError({
|
||||
userId: input.userId,
|
||||
deviceId: registration.deviceId,
|
||||
stage: "claim-push-to-start-token",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
)
|
||||
: Effect.void,
|
||||
],
|
||||
{ discard: true },
|
||||
);
|
||||
// The drizzle handle is alchemy's lazy proxy chain: it only becomes a
|
||||
// real Effect when consumed via `yield*`. Handing it to Effect.all sends
|
||||
// the raw Proxy into the fiber runtime, which spins the isolate at 100%
|
||||
// CPU (registrations then hang until the client aborts) — keep every db
|
||||
// chain directly yielded.
|
||||
if (registration.pushToken) {
|
||||
yield* db
|
||||
.update(relayMobileDevices)
|
||||
.set({ pushToken: null, updatedAt })
|
||||
.where(eq(relayMobileDevices.pushToken, registration.pushToken))
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new DeviceRegistrationPersistenceError({
|
||||
userId: input.userId,
|
||||
deviceId: registration.deviceId,
|
||||
stage: "claim-push-token",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
if (registration.pushToStartToken) {
|
||||
yield* db
|
||||
.update(relayMobileDevices)
|
||||
.set({ pushToStartToken: null, updatedAt })
|
||||
.where(eq(relayMobileDevices.pushToStartToken, registration.pushToStartToken))
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new DeviceRegistrationPersistenceError({
|
||||
userId: input.userId,
|
||||
deviceId: registration.deviceId,
|
||||
stage: "claim-push-to-start-token",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
yield* db
|
||||
.insert(relayMobileDevices)
|
||||
@@ -130,6 +130,8 @@ export const make = Effect.gen(function* () {
|
||||
platform: registration.platform,
|
||||
iosMajorVersion: registration.iosMajorVersion,
|
||||
appVersion: registration.appVersion ?? null,
|
||||
bundleId: registration.bundleId ?? null,
|
||||
apsEnvironment: registration.apsEnvironment ?? null,
|
||||
pushToken: registration.pushToken ?? null,
|
||||
pushToStartToken: registration.pushToStartToken ?? null,
|
||||
preferencesJson: registration.preferences,
|
||||
@@ -143,6 +145,13 @@ export const make = Effect.gen(function* () {
|
||||
label: registration.label,
|
||||
iosMajorVersion: registration.iosMajorVersion,
|
||||
appVersion: registration.appVersion ?? null,
|
||||
// Preserve routing from newer app builds when an older build
|
||||
// re-registers without these fields.
|
||||
bundleId: sql`coalesce(excluded.bundle_id, ${relayMobileDevices.bundleId})`,
|
||||
apsEnvironment: sql`coalesce(
|
||||
excluded.aps_environment,
|
||||
${relayMobileDevices.apsEnvironment}
|
||||
)`,
|
||||
pushToken: sql`coalesce(excluded.push_token, ${relayMobileDevices.pushToken})`,
|
||||
pushToStartToken: sql`coalesce(
|
||||
excluded.push_to_start_token,
|
||||
@@ -168,49 +177,46 @@ export const make = Effect.gen(function* () {
|
||||
yield* Effect.annotateCurrentSpan({
|
||||
"relay.mobile.device_id": input.deviceId,
|
||||
});
|
||||
yield* Effect.all(
|
||||
[
|
||||
db
|
||||
.delete(relayLiveActivities)
|
||||
.where(
|
||||
and(
|
||||
eq(relayLiveActivities.userId, input.userId),
|
||||
eq(relayLiveActivities.deviceId, input.deviceId),
|
||||
),
|
||||
)
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new DeviceUnregistrationPersistenceError({
|
||||
userId: input.userId,
|
||||
deviceId: input.deviceId,
|
||||
stage: "delete-live-activity",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
),
|
||||
db
|
||||
.delete(relayMobileDevices)
|
||||
.where(
|
||||
and(
|
||||
eq(relayMobileDevices.userId, input.userId),
|
||||
eq(relayMobileDevices.deviceId, input.deviceId),
|
||||
),
|
||||
)
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new DeviceUnregistrationPersistenceError({
|
||||
userId: input.userId,
|
||||
deviceId: input.deviceId,
|
||||
stage: "delete-device",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
),
|
||||
],
|
||||
{ discard: true },
|
||||
);
|
||||
// Same proxy-chain constraint as register above: db chains must be
|
||||
// consumed via `yield*`, never passed to Effect.all.
|
||||
yield* db
|
||||
.delete(relayLiveActivities)
|
||||
.where(
|
||||
and(
|
||||
eq(relayLiveActivities.userId, input.userId),
|
||||
eq(relayLiveActivities.deviceId, input.deviceId),
|
||||
),
|
||||
)
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new DeviceUnregistrationPersistenceError({
|
||||
userId: input.userId,
|
||||
deviceId: input.deviceId,
|
||||
stage: "delete-live-activity",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
yield* db
|
||||
.delete(relayMobileDevices)
|
||||
.where(
|
||||
and(
|
||||
eq(relayMobileDevices.userId, input.userId),
|
||||
eq(relayMobileDevices.deviceId, input.deviceId),
|
||||
),
|
||||
)
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new DeviceUnregistrationPersistenceError({
|
||||
userId: input.userId,
|
||||
deviceId: input.deviceId,
|
||||
stage: "delete-device",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}),
|
||||
listForUser: Effect.fn("relay.devices.listForUser")(function* (input) {
|
||||
const rows = yield* db
|
||||
|
||||
@@ -198,6 +198,57 @@ describe("LiveActivities", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("retires the previous activity token when a start or end is delivered", () => {
|
||||
const conflictConfigs: Array<{ readonly set?: Record<string, unknown> }> = [];
|
||||
const fakeDb = {
|
||||
insert: () => ({
|
||||
values: () => ({
|
||||
onConflictDoUpdate: (config: { readonly set?: Record<string, unknown> }) => {
|
||||
conflictConfigs.push(config);
|
||||
return Effect.void;
|
||||
},
|
||||
}),
|
||||
}),
|
||||
} as unknown as RelayDb.RelayDb["Service"];
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const liveActivities = yield* LiveActivities.LiveActivities;
|
||||
const mark = (kind: "live_activity_start" | "live_activity_update" | "live_activity_end") =>
|
||||
liveActivities.markDelivery({
|
||||
userId: "user-2",
|
||||
deviceId: "device-1",
|
||||
kind,
|
||||
aggregate,
|
||||
deliveredAt: "2026-05-25T00:00:10.000Z",
|
||||
});
|
||||
yield* mark("live_activity_start");
|
||||
yield* mark("live_activity_update");
|
||||
yield* mark("live_activity_end");
|
||||
|
||||
// A start begins a new activity generation and an end retires the
|
||||
// current one; both must drop the stored update token so later
|
||||
// deliveries can't route to the dead activity. Plain updates keep it.
|
||||
expect(conflictConfigs[0]?.set).toEqual(
|
||||
expect.objectContaining({
|
||||
activityPushToken: null,
|
||||
remoteStartedAt: "2026-05-25T00:00:10.000Z",
|
||||
endedAt: null,
|
||||
}),
|
||||
);
|
||||
expect(conflictConfigs[1]?.set?.activityPushToken).not.toBeNull();
|
||||
expect(conflictConfigs[2]?.set).toEqual(
|
||||
expect.objectContaining({
|
||||
activityPushToken: null,
|
||||
endedAt: "2026-05-25T00:00:10.000Z",
|
||||
}),
|
||||
);
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
LiveActivities.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb))),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("preserves correlation context and causes for persistence failures", () => {
|
||||
const cause = new Error("database unavailable");
|
||||
const registration: RelayLiveActivityRegistrationRequest = {
|
||||
|
||||
@@ -69,6 +69,8 @@ export interface DeviceRow {
|
||||
readonly platform: "ios";
|
||||
readonly ios_major_version: number;
|
||||
readonly app_version: string | null;
|
||||
readonly bundle_id: string | null;
|
||||
readonly aps_environment: "sandbox" | "production" | null;
|
||||
readonly push_token: string | null;
|
||||
readonly push_to_start_token: string | null;
|
||||
readonly preferences_json: string;
|
||||
@@ -196,6 +198,8 @@ export const make = Effect.gen(function* () {
|
||||
platform: relayMobileDevices.platform,
|
||||
ios_major_version: relayMobileDevices.iosMajorVersion,
|
||||
app_version: relayMobileDevices.appVersion,
|
||||
bundle_id: relayMobileDevices.bundleId,
|
||||
aps_environment: relayMobileDevices.apsEnvironment,
|
||||
push_token: relayMobileDevices.pushToken,
|
||||
push_to_start_token: relayMobileDevices.pushToStartToken,
|
||||
preferences_json: relayMobileDevices.preferencesJson,
|
||||
@@ -276,10 +280,26 @@ export const make = Effect.gen(function* () {
|
||||
.onConflictDoUpdate({
|
||||
target: [relayLiveActivities.userId, relayLiveActivities.deviceId],
|
||||
set: {
|
||||
remoteStartedAt: sql`coalesce(
|
||||
${relayLiveActivities.remoteStartedAt},
|
||||
excluded.remote_started_at
|
||||
)`,
|
||||
// A delivered start begins a NEW activity generation: the stored
|
||||
// update token belongs to the previous activity (dead once a new
|
||||
// one starts, and certainly dead after an end), so keep it only
|
||||
// for plain updates. Deliveries pause until the app registers
|
||||
// the fresh activity's token; registerLiveActivity + replay then
|
||||
// reconcile content (or end the activity if work already
|
||||
// finished). Without this, updates and ends route to the dead
|
||||
// token and the new lock-screen card is stranded at its start
|
||||
// content forever.
|
||||
activityPushToken:
|
||||
input.kind === "live_activity_update"
|
||||
? sql`${relayLiveActivities.activityPushToken}`
|
||||
: null,
|
||||
remoteStartedAt:
|
||||
input.kind === "live_activity_start"
|
||||
? input.deliveredAt
|
||||
: sql`coalesce(
|
||||
${relayLiveActivities.remoteStartedAt},
|
||||
excluded.remote_started_at
|
||||
)`,
|
||||
remoteStartQueuedAt: null,
|
||||
endedAt:
|
||||
input.kind === "live_activity_start"
|
||||
|
||||
@@ -20,6 +20,7 @@ import * as RelayConfiguration from "../Config.ts";
|
||||
import * as AgentActivityPublisher from "./AgentActivityPublisher.ts";
|
||||
import * as ApnsDeliveries from "./ApnsDeliveries.ts";
|
||||
import * as ApnsClient from "./ApnsClient.ts";
|
||||
import * as ApnsProviderTokens from "./ApnsProviderTokens.ts";
|
||||
import * as ApnsDeliveryQueue from "./ApnsDeliveryQueue.ts";
|
||||
import * as MobileRegistrations from "./MobileRegistrations.ts";
|
||||
|
||||
@@ -70,6 +71,7 @@ function makeAgentActivityRows(
|
||||
return {
|
||||
upsert: () => Effect.void,
|
||||
remove: () => Effect.void,
|
||||
pruneTerminal: () => Effect.void,
|
||||
listForUser: () => {
|
||||
const activeState: RelayAgentActivityState = {
|
||||
environmentId: "env-1" as RelayAgentActivityState["environmentId"],
|
||||
@@ -147,7 +149,11 @@ function makeRegistrationReplayLayer(input: {
|
||||
}) {
|
||||
return MobileRegistrations.layer.pipe(
|
||||
Layer.provide(AgentActivityPublisher.layer),
|
||||
Layer.provide(ApnsDeliveries.layer.pipe(Layer.provide(ApnsClient.layer))),
|
||||
Layer.provide(
|
||||
ApnsDeliveries.layer.pipe(
|
||||
Layer.provide(ApnsClient.layer.pipe(Layer.provide(ApnsProviderTokens.layer))),
|
||||
),
|
||||
),
|
||||
Layer.provide(ApnsDeliveryQueue.layer.pipe(Layer.provide(NodeCryptoLayer.layer))),
|
||||
Layer.provide(
|
||||
Layer.mergeAll(
|
||||
@@ -207,6 +213,7 @@ describe("MobileRegistrations", () => {
|
||||
}),
|
||||
),
|
||||
Layer.succeed(LiveActivities.LiveActivities, makeLiveActivities()),
|
||||
Layer.succeed(AgentActivityRows.AgentActivityRows, makeAgentActivityRows()),
|
||||
Layer.succeed(
|
||||
AgentActivityPublisher.AgentActivityPublisher,
|
||||
makeAgentActivityPublisher({
|
||||
@@ -249,6 +256,7 @@ describe("MobileRegistrations", () => {
|
||||
Layer.mergeAll(
|
||||
Layer.succeed(Devices.Devices, makeDevices()),
|
||||
Layer.succeed(LiveActivities.LiveActivities, makeLiveActivities()),
|
||||
Layer.succeed(AgentActivityRows.AgentActivityRows, makeAgentActivityRows()),
|
||||
Layer.succeed(
|
||||
AgentActivityPublisher.AgentActivityPublisher,
|
||||
makeAgentActivityPublisher({
|
||||
@@ -300,6 +308,7 @@ describe("MobileRegistrations", () => {
|
||||
}),
|
||||
),
|
||||
Layer.succeed(LiveActivities.LiveActivities, makeLiveActivities()),
|
||||
Layer.succeed(AgentActivityRows.AgentActivityRows, makeAgentActivityRows()),
|
||||
Layer.succeed(
|
||||
AgentActivityPublisher.AgentActivityPublisher,
|
||||
makeAgentActivityPublisher(),
|
||||
@@ -344,6 +353,7 @@ describe("MobileRegistrations", () => {
|
||||
Layer.provide(
|
||||
Layer.mergeAll(
|
||||
Layer.succeed(Devices.Devices, makeDevices()),
|
||||
Layer.succeed(AgentActivityRows.AgentActivityRows, makeAgentActivityRows()),
|
||||
Layer.succeed(
|
||||
LiveActivities.LiveActivities,
|
||||
makeLiveActivities({
|
||||
@@ -381,8 +391,36 @@ describe("MobileRegistrations", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it.effect("returns the current aggregate for the app's arming decision", () => {
|
||||
return Effect.gen(function* () {
|
||||
const registrations = yield* MobileRegistrations.MobileRegistrations;
|
||||
const snapshot = yield* registrations.getAgentActivitySnapshot({ userId: "dev:julius" });
|
||||
|
||||
expect(snapshot.aggregate).toMatchObject({
|
||||
activeCount: 1,
|
||||
activities: [{ threadId: "thread-1", phase: "running" }],
|
||||
});
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
MobileRegistrations.layer.pipe(
|
||||
Layer.provide(
|
||||
Layer.mergeAll(
|
||||
Layer.succeed(Devices.Devices, makeDevices()),
|
||||
Layer.succeed(AgentActivityRows.AgentActivityRows, makeAgentActivityRows()),
|
||||
Layer.succeed(LiveActivities.LiveActivities, makeLiveActivities()),
|
||||
Layer.succeed(
|
||||
AgentActivityPublisher.AgentActivityPublisher,
|
||||
makeAgentActivityPublisher(),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it.effect(
|
||||
"starts a remote Live Activity through the real publisher and APNs queue when a device registers after work is already active",
|
||||
"does not remotely start a Live Activity when a device registers after work is already active",
|
||||
() => {
|
||||
const queuedJobs: Array<SignedApnsDeliveryJob> = [];
|
||||
const queuedStarts: Array<
|
||||
@@ -404,6 +442,8 @@ describe("MobileRegistrations", () => {
|
||||
platform: "ios",
|
||||
ios_major_version: 18,
|
||||
app_version: "1.0.0",
|
||||
bundle_id: null,
|
||||
aps_environment: null,
|
||||
push_token: "apns-device-token",
|
||||
push_to_start_token: "push-to-start-token",
|
||||
preferences_json: JSON.stringify(device.preferences),
|
||||
@@ -432,46 +472,13 @@ describe("MobileRegistrations", () => {
|
||||
},
|
||||
});
|
||||
|
||||
// Activities are armed by the app in the foreground; a device
|
||||
// registration alone never remote-starts one, even when work is
|
||||
// already active and a push-to-start token is on file.
|
||||
expect(result).toEqual({ ok: true });
|
||||
expect(registeredDevices).toEqual([
|
||||
{
|
||||
userId: "dev:julius",
|
||||
registration: {
|
||||
...device,
|
||||
pushToken: "apns-device-token",
|
||||
pushToStartToken: "push-to-start-token",
|
||||
},
|
||||
},
|
||||
]);
|
||||
expect(queuedStarts).toMatchObject([
|
||||
{
|
||||
userId: "dev:julius",
|
||||
deviceId: "device-1",
|
||||
},
|
||||
]);
|
||||
expect(queuedJobs).toHaveLength(1);
|
||||
expect(queuedJobs[0]?.payload).toMatchObject({
|
||||
kind: "live_activity_start",
|
||||
target: {
|
||||
userId: "dev:julius",
|
||||
deviceId: "device-1",
|
||||
token: "push-to-start-token",
|
||||
},
|
||||
aggregate: {
|
||||
title: "T3 Code",
|
||||
subtitle: "Agent work in progress",
|
||||
activeCount: 1,
|
||||
activities: [
|
||||
{
|
||||
environmentId: "env-1",
|
||||
threadId: "thread-1",
|
||||
threadTitle: "Implement APNs",
|
||||
status: "Working",
|
||||
},
|
||||
],
|
||||
},
|
||||
notification: null,
|
||||
});
|
||||
expect(registeredDevices).toHaveLength(1);
|
||||
expect(queuedStarts).toEqual([]);
|
||||
expect(queuedJobs).toEqual([]);
|
||||
}).pipe(Effect.provide(makeRegistrationReplayLayer({ devices, liveActivities, queuedJobs })));
|
||||
},
|
||||
);
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import type {
|
||||
RelayAgentActivitySnapshotResponse,
|
||||
RelayDeviceRegistrationRequest,
|
||||
RelayLiveActivityRegistrationRequest,
|
||||
} from "@t3tools/contracts/relay";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import * as Context from "effect/Context";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
|
||||
import * as AgentActivityRows from "./AgentActivityRows.ts";
|
||||
import * as Devices from "./Devices.ts";
|
||||
import * as LiveActivities from "./LiveActivities.ts";
|
||||
import * as AgentActivityPublisher from "./AgentActivityPublisher.ts";
|
||||
@@ -13,7 +16,8 @@ import * as AgentActivityPublisher from "./AgentActivityPublisher.ts";
|
||||
export type MobileRegistrationError =
|
||||
| Devices.DeviceRegistrationPersistenceError
|
||||
| Devices.DeviceUnregistrationPersistenceError
|
||||
| LiveActivities.LiveActivityRegistrationPersistenceError;
|
||||
| LiveActivities.LiveActivityRegistrationPersistenceError
|
||||
| AgentActivityRows.AgentActivityRowListPersistenceError;
|
||||
|
||||
export class MobileRegistrations extends Context.Service<
|
||||
MobileRegistrations,
|
||||
@@ -30,10 +34,14 @@ export class MobileRegistrations extends Context.Service<
|
||||
readonly userId: string;
|
||||
readonly deviceId: string;
|
||||
}) => Effect.Effect<{ readonly ok: true }, MobileRegistrationError>;
|
||||
readonly getAgentActivitySnapshot: (input: {
|
||||
readonly userId: string;
|
||||
}) => Effect.Effect<RelayAgentActivitySnapshotResponse, MobileRegistrationError>;
|
||||
}
|
||||
>()("t3code-relay/agentActivity/MobileRegistrations") {}
|
||||
|
||||
export const make = Effect.gen(function* () {
|
||||
const rows = yield* AgentActivityRows.AgentActivityRows;
|
||||
const devices = yield* Devices.Devices;
|
||||
const liveActivities = yield* LiveActivities.LiveActivities;
|
||||
const publisher = yield* AgentActivityPublisher.AgentActivityPublisher;
|
||||
@@ -82,6 +90,19 @@ export const make = Effect.gen(function* () {
|
||||
return { ok: true as const };
|
||||
},
|
||||
),
|
||||
getAgentActivitySnapshot: Effect.fn("relay.mobile_registrations.get_agent_activity_snapshot")(
|
||||
function* (input) {
|
||||
const activeStates = yield* rows.listForUser({ userId: input.userId });
|
||||
const now = yield* DateTime.now;
|
||||
return {
|
||||
aggregate: AgentActivityPublisher.makeAggregateState({
|
||||
activeStates,
|
||||
terminalState: null,
|
||||
nowMs: now.epochMilliseconds,
|
||||
}),
|
||||
};
|
||||
},
|
||||
),
|
||||
unregisterDevice: Effect.fn("relay.mobile_registrations.unregister_device")(function* (input) {
|
||||
yield* Effect.annotateCurrentSpan({
|
||||
"relay.mobile.device_id": input.deviceId,
|
||||
|
||||
@@ -1,13 +1,51 @@
|
||||
import type {
|
||||
RelayAgentActivityAggregateRow,
|
||||
RelayAgentActivityAggregateState,
|
||||
RelayAgentActivityState,
|
||||
} from "@t3tools/contracts/relay";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import * as Option from "effect/Option";
|
||||
|
||||
import type { ApnsNotificationPayload } from "./apnsDeliveryJobs.ts";
|
||||
|
||||
export function isTerminalPhase(state: RelayAgentActivityState): boolean {
|
||||
return state.phase === "completed" || state.phase === "failed";
|
||||
}
|
||||
|
||||
// Rows are only removed when their environment publishes a terminal state. An
|
||||
// environment that dies mid-run (machine off, process killed) never does, so
|
||||
// without an age cutoff its threads inflate activeCount forever. Actively
|
||||
// running phases expire quickly; waiting phases can legitimately sit for hours
|
||||
// while a user ignores an approval prompt, so they get a longer window. The
|
||||
// underlying database row is left in place: a late publish for the thread
|
||||
// refreshes updatedAt and the row becomes visible again.
|
||||
const RUNNING_AGENT_ACTIVITY_ROW_TTL_MS = 2 * 60 * 60 * 1_000;
|
||||
const WAITING_AGENT_ACTIVITY_ROW_TTL_MS = 24 * 60 * 60 * 1_000;
|
||||
|
||||
export function isExpiredAgentActivityState(
|
||||
state: RelayAgentActivityState,
|
||||
nowMs: number,
|
||||
): boolean {
|
||||
const updatedAtMs = Option.match(DateTime.make(state.updatedAt), {
|
||||
onNone: () => Number.NaN,
|
||||
onSome: (dt) => dt.epochMilliseconds,
|
||||
});
|
||||
if (Number.isNaN(updatedAtMs)) {
|
||||
return true;
|
||||
}
|
||||
const ttlMs =
|
||||
state.phase === "running" || state.phase === "starting"
|
||||
? RUNNING_AGENT_ACTIVITY_ROW_TTL_MS
|
||||
: WAITING_AGENT_ACTIVITY_ROW_TTL_MS;
|
||||
return nowMs - updatedAtMs > ttlMs;
|
||||
}
|
||||
|
||||
const MAX_SUMMARY_TEXT_LENGTH = 120;
|
||||
const MAX_STATUS_TEXT_LENGTH = 40;
|
||||
const MAX_DEEP_LINK_LENGTH = 512;
|
||||
const MAX_ACTIVITY_ROWS = 3;
|
||||
// The Live Activity banner (lock screen / Notification Center) renders up to
|
||||
// five rows; the expanded Dynamic Island shows the top three of these.
|
||||
export const MAX_ACTIVITY_ROWS = 5;
|
||||
|
||||
function truncateText(value: string, maxLength: number): string {
|
||||
const trimmed = value.trim();
|
||||
|
||||
@@ -41,6 +41,15 @@ export const ApnsNotificationPayload = Schema.Struct({
|
||||
});
|
||||
export type ApnsNotificationPayload = typeof ApnsNotificationPayload.Type;
|
||||
|
||||
// Alert copy attached to a Live Activity update/end push. Its presence makes
|
||||
// the update "alerting": iOS wakes the screen, plays the haptic, and briefly
|
||||
// expands the Dynamic Island instead of silently redrawing.
|
||||
export const ApnsLiveActivityAlert = Schema.Struct({
|
||||
title: Schema.String,
|
||||
body: Schema.String,
|
||||
});
|
||||
export type ApnsLiveActivityAlert = typeof ApnsLiveActivityAlert.Type;
|
||||
|
||||
export const ApnsDeliveryJobPayload = Schema.Struct({
|
||||
version: Schema.Literal(1),
|
||||
jobId: Schema.String,
|
||||
@@ -49,9 +58,15 @@ export const ApnsDeliveryJobPayload = Schema.Struct({
|
||||
userId: Schema.String,
|
||||
deviceId: Schema.String,
|
||||
token: Schema.String,
|
||||
// Per-device APNs routing; absent on jobs queued by older relay builds,
|
||||
// which fall back to the configured defaults.
|
||||
bundleId: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
apsEnvironment: Schema.optional(Schema.NullOr(Schema.Literals(["sandbox", "production"]))),
|
||||
}),
|
||||
aggregate: Schema.NullOr(RelayAgentActivityAggregateState),
|
||||
notification: Schema.NullOr(ApnsNotificationPayload),
|
||||
// Optional so jobs queued by older relay builds still decode.
|
||||
alert: Schema.optional(Schema.NullOr(ApnsLiveActivityAlert)),
|
||||
createdAt: Schema.String,
|
||||
expiresAt: Schema.String,
|
||||
});
|
||||
@@ -224,8 +239,11 @@ export function makeApnsDeliveryJobPayload(input: {
|
||||
readonly userId: string;
|
||||
readonly deviceId: string;
|
||||
readonly token: string;
|
||||
readonly bundleId?: string | null | undefined;
|
||||
readonly apsEnvironment?: "sandbox" | "production" | null | undefined;
|
||||
readonly aggregate: ApnsDeliveryJobPayload["aggregate"];
|
||||
readonly notification?: ApnsNotificationPayload | null;
|
||||
readonly alert?: ApnsLiveActivityAlert | null | undefined;
|
||||
readonly createdAt: string;
|
||||
readonly expiresAt: string;
|
||||
readonly jobId: string;
|
||||
@@ -238,9 +256,14 @@ export function makeApnsDeliveryJobPayload(input: {
|
||||
userId: input.userId,
|
||||
deviceId: input.deviceId,
|
||||
token: input.token,
|
||||
...(input.bundleId ? { bundleId: input.bundleId } : {}),
|
||||
...(input.apsEnvironment ? { apsEnvironment: input.apsEnvironment } : {}),
|
||||
},
|
||||
aggregate: input.aggregate,
|
||||
notification: input.notification ?? null,
|
||||
// Omitted (not null) when absent so signatures stay identical to jobs from
|
||||
// relay builds that predate the field.
|
||||
...(input.alert ? { alert: input.alert } : {}),
|
||||
createdAt: input.createdAt,
|
||||
expiresAt: input.expiresAt,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
import * as NodeCrypto from "node:crypto";
|
||||
|
||||
import { p256 } from "@noble/curves/nist";
|
||||
import { sha256 } from "@noble/hashes/sha2";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Encoding from "effect/Encoding";
|
||||
import * as Redacted from "effect/Redacted";
|
||||
import * as Result from "effect/Result";
|
||||
import * as Schema from "effect/Schema";
|
||||
|
||||
import type { ApnsCredentials } from "../Config.ts";
|
||||
|
||||
export class ApnsJwtEncodingError extends Schema.TaggedErrorClass<ApnsJwtEncodingError>()(
|
||||
"ApnsJwtEncodingError",
|
||||
{
|
||||
component: Schema.Literals(["header", "payload"]),
|
||||
teamId: Schema.String,
|
||||
keyId: Schema.String,
|
||||
issuedAtUnixSeconds: Schema.Number,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Failed to encode APNs JWT ${this.component} for key ${this.keyId}.`;
|
||||
}
|
||||
}
|
||||
|
||||
export class ApnsJwtSigningError extends Schema.TaggedErrorClass<ApnsJwtSigningError>()(
|
||||
"ApnsJwtSigningError",
|
||||
{
|
||||
teamId: Schema.String,
|
||||
keyId: Schema.String,
|
||||
issuedAtUnixSeconds: Schema.Number,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Failed to sign APNs JWT for key ${this.keyId}.`;
|
||||
}
|
||||
}
|
||||
|
||||
export type ApnsJwtError = ApnsJwtEncodingError | ApnsJwtSigningError;
|
||||
|
||||
const encodeApnsJwtHeaderJson = Schema.encodeEffect(
|
||||
Schema.fromJsonString(
|
||||
Schema.Struct({
|
||||
alg: Schema.Literal("ES256"),
|
||||
kid: Schema.String,
|
||||
}),
|
||||
),
|
||||
);
|
||||
const encodeApnsJwtPayloadJson = Schema.encodeEffect(
|
||||
Schema.fromJsonString(
|
||||
Schema.Struct({
|
||||
iss: Schema.String,
|
||||
iat: Schema.Number,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export interface ApnsJwtSigningInput {
|
||||
readonly teamId: ApnsCredentials["teamId"];
|
||||
readonly keyId: ApnsCredentials["keyId"];
|
||||
readonly privateKey: ApnsCredentials["privateKey"];
|
||||
readonly issuedAtUnixSeconds: number;
|
||||
}
|
||||
|
||||
export const makeApnsJwt = Effect.fn("relay.apns.make_jwt")(function* (input: ApnsJwtSigningInput) {
|
||||
const headerJson = yield* encodeApnsJwtHeaderJson({ alg: "ES256", kid: input.keyId }).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ApnsJwtEncodingError({
|
||||
component: "header",
|
||||
teamId: input.teamId,
|
||||
keyId: input.keyId,
|
||||
issuedAtUnixSeconds: input.issuedAtUnixSeconds,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
const payloadJson = yield* encodeApnsJwtPayloadJson({
|
||||
iss: input.teamId,
|
||||
iat: input.issuedAtUnixSeconds,
|
||||
}).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ApnsJwtEncodingError({
|
||||
component: "payload",
|
||||
teamId: input.teamId,
|
||||
keyId: input.keyId,
|
||||
issuedAtUnixSeconds: input.issuedAtUnixSeconds,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const privateKey = Redacted.value(input.privateKey);
|
||||
const header = Encoding.encodeBase64Url(headerJson);
|
||||
const payload = Encoding.encodeBase64Url(payloadJson);
|
||||
const signingInput = `${header}.${payload}`;
|
||||
|
||||
return yield* Effect.try({
|
||||
try: () => {
|
||||
// Deterministic ES256 (RFC 6979 via noble) instead of node's randomized
|
||||
// signer: identical (key, iat) yields the byte-identical JWT on every
|
||||
// worker isolate, so the fleet presents one stable provider token to
|
||||
// APNs without any shared storage. Node crypto only converts the PEM to
|
||||
// the raw scalar noble signs with.
|
||||
const scalar = apnsSigningScalar(privateKey);
|
||||
const signature = p256
|
||||
.sign(sha256(new TextEncoder().encode(signingInput)), scalar, { prehash: false })
|
||||
.toCompactRawBytes();
|
||||
return `${signingInput}.${Encoding.encodeBase64Url(signature)}`;
|
||||
},
|
||||
catch: (cause) =>
|
||||
new ApnsJwtSigningError({
|
||||
teamId: input.teamId,
|
||||
keyId: input.keyId,
|
||||
issuedAtUnixSeconds: input.issuedAtUnixSeconds,
|
||||
cause,
|
||||
}),
|
||||
});
|
||||
});
|
||||
|
||||
// PEM parsing is pure and the key set is static per deployment; memoize the
|
||||
// extracted P-256 scalar so signing never re-parses the PKCS8 document.
|
||||
const signingScalarCache = new Map<string, Uint8Array>();
|
||||
|
||||
function apnsSigningScalar(privateKeyPem: string): Uint8Array {
|
||||
const cached = signingScalarCache.get(privateKeyPem);
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
const jwk = NodeCrypto.createPrivateKey(privateKeyPem.replace(/\\n/g, "\n")).export({
|
||||
format: "jwk",
|
||||
});
|
||||
if (jwk.crv !== "P-256" || typeof jwk.d !== "string") {
|
||||
throw new Error("APNs signing key is not a P-256 private key.");
|
||||
}
|
||||
const scalar = Result.getOrThrowWith(
|
||||
Encoding.decodeBase64Url(jwk.d),
|
||||
() => new Error("APNs signing key scalar is not valid base64url."),
|
||||
);
|
||||
signingScalarCache.set(privateKeyPem, scalar);
|
||||
return scalar;
|
||||
}
|
||||
|
||||
// Fingerprint the key material so rotated credentials never reuse a JWT
|
||||
// signed by the previous key.
|
||||
export function apnsProviderTokenCacheKey(input: {
|
||||
readonly teamId: string;
|
||||
readonly keyId: string;
|
||||
readonly privateKey: ApnsCredentials["privateKey"];
|
||||
}): string {
|
||||
const keyFingerprint = NodeCrypto.createHash("sha256")
|
||||
.update(Redacted.value(input.privateKey))
|
||||
.digest("hex")
|
||||
.slice(0, 16);
|
||||
return `${input.teamId}:${input.keyId}:${keyFingerprint}`;
|
||||
}
|
||||
@@ -109,6 +109,7 @@ const makeRequest = Effect.gen(function* () {
|
||||
function testLayer(input?: {
|
||||
readonly upsert?: EnvironmentLinks.EnvironmentLinks["Service"]["upsert"];
|
||||
readonly consume?: DpopProofs.DpopProofReplay["Service"]["consume"];
|
||||
readonly deprovision?: ManagedEndpointProvider.ManagedEndpointProvider["Service"]["deprovision"];
|
||||
}) {
|
||||
return EnvironmentLinker.layer.pipe(
|
||||
Layer.provideMerge(RelayTokens.layer),
|
||||
@@ -135,7 +136,7 @@ function testLayer(input?: {
|
||||
revokeForEnvironmentPublicKey: () => Effect.succeed(false),
|
||||
}),
|
||||
Layer.succeed(ManagedEndpointProvider.ManagedEndpointProvider, {
|
||||
deprovision: () => Effect.void,
|
||||
deprovision: input?.deprovision ?? (() => Effect.void),
|
||||
provision: () =>
|
||||
Effect.succeed({
|
||||
endpoint: {
|
||||
@@ -173,6 +174,79 @@ describe("EnvironmentLinker", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("links a publish-only environment with a non-secure nominal endpoint", () => {
|
||||
let persistedEndpoint: string | null = null;
|
||||
let deprovisionedEnvironmentId: string | null = null;
|
||||
return Effect.gen(function* () {
|
||||
const now = yield* DateTime.now;
|
||||
const expiresAt = DateTime.add(now, { minutes: 5 });
|
||||
const relayTokens = yield* RelayTokens.RelayTokens;
|
||||
const challenge = yield* relayTokens.issueLinkChallenge({
|
||||
userId: "user_123",
|
||||
request: {
|
||||
notificationsEnabled: true,
|
||||
liveActivitiesEnabled: true,
|
||||
managedTunnelsEnabled: false,
|
||||
},
|
||||
jti: "publish-only-challenge-jti",
|
||||
issuedAtEpochSeconds: Math.floor(now.epochMilliseconds / 1_000),
|
||||
expiresAtEpochSeconds: Math.floor(expiresAt.epochMilliseconds / 1_000),
|
||||
});
|
||||
const payload = {
|
||||
iss: "t3-env:env-link-test",
|
||||
aud: "https://relay.example.test",
|
||||
sub: "env-link-test",
|
||||
jti: "publish-only-proof-jti",
|
||||
iat: Math.floor(now.epochMilliseconds / 1_000),
|
||||
exp: Math.floor(expiresAt.epochMilliseconds / 1_000),
|
||||
challenge,
|
||||
environmentId: "env-link-test" as RelayEnvironmentLinkProofPayload["environmentId"],
|
||||
descriptor: {
|
||||
environmentId: "env-link-test" as RelayEnvironmentLinkProofPayload["environmentId"],
|
||||
label: "Link Test Environment",
|
||||
platform: { os: "darwin", arch: "arm64" },
|
||||
serverVersion: "0.0.0-test",
|
||||
capabilities: { repositoryIdentity: true },
|
||||
},
|
||||
environmentPublicKey: environmentKeyPair.publicKey.trim(),
|
||||
endpoint: {
|
||||
httpBaseUrl: "http://127.0.0.1:3773/",
|
||||
wsBaseUrl: "ws://127.0.0.1:3773/",
|
||||
providerKind: "manual",
|
||||
},
|
||||
origin: { localHttpHost: "127.0.0.1", localHttpPort: 3773 },
|
||||
scopes: ["agent_activity_notifications"],
|
||||
} satisfies RelayEnvironmentLinkProofPayload;
|
||||
const request = {
|
||||
proof: signTestJwt(payload, RELAY_LINK_PROOF_TYP, environmentKeyPair.privateKey),
|
||||
notificationsEnabled: true,
|
||||
liveActivitiesEnabled: true,
|
||||
managedTunnelsEnabled: false,
|
||||
} satisfies RelayEnvironmentLinkRequest;
|
||||
const linker = yield* EnvironmentLinker.EnvironmentLinker;
|
||||
const result = yield* linker.link({ userId: "user_123", request });
|
||||
expect(result.environmentCredential).toBe("t3env_credential_secret");
|
||||
expect(result.endpointRuntime).toBeNull();
|
||||
expect(persistedEndpoint).toBe("http://127.0.0.1:3773/");
|
||||
// Downgrading from a managed link must release the previously provisioned
|
||||
// tunnel; nothing else cleans it up before a full unlink.
|
||||
expect(deprovisionedEnvironmentId).toBe("env-link-test");
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
testLayer({
|
||||
upsert: (input) =>
|
||||
Effect.sync(() => {
|
||||
persistedEndpoint = input.endpoint.httpBaseUrl;
|
||||
}),
|
||||
deprovision: (input) =>
|
||||
Effect.sync(() => {
|
||||
deprovisionedEnvironmentId = input.environmentId;
|
||||
}),
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("rejects a tampered compact proof before persistence", () => {
|
||||
let persisted = false;
|
||||
return Effect.gen(function* () {
|
||||
|
||||
@@ -287,6 +287,27 @@ const make = Effect.gen(function* () {
|
||||
stage: "validate_origin",
|
||||
});
|
||||
}
|
||||
// Downgrading a managed link to publish-only must release the tunnel and
|
||||
// DNS that were provisioned for it — nothing else cleans them up until a
|
||||
// full unlink. Best effort: a cleanup failure must not block the link
|
||||
// itself, and the provider treats an absent allocation as already
|
||||
// deprovisioned, so retrying on every non-tunnel link is cheap.
|
||||
if (!input.request.managedTunnelsEnabled) {
|
||||
yield* managedEndpointProvider
|
||||
.deprovision({
|
||||
userId: input.userId,
|
||||
environmentId: verified.environmentId,
|
||||
})
|
||||
.pipe(
|
||||
Effect.tapError((error) =>
|
||||
Effect.logWarning("managed endpoint deprovision on publish-only link failed", {
|
||||
environmentId: verified.environmentId,
|
||||
errorTag: error._tag,
|
||||
}),
|
||||
),
|
||||
Effect.ignore,
|
||||
);
|
||||
}
|
||||
const provisioned = input.request.managedTunnelsEnabled
|
||||
? yield* managedEndpointProvider.provision({
|
||||
userId: input.userId,
|
||||
@@ -295,7 +316,11 @@ const make = Effect.gen(function* () {
|
||||
})
|
||||
: null;
|
||||
const endpoint = provisioned?.endpoint ?? verified.endpoint;
|
||||
if (!isSecureManagedEndpoint(endpoint)) {
|
||||
// The secure-endpoint requirement only matters when the relay advertises
|
||||
// this endpoint for other devices to reach (managed tunnel). Publish-only
|
||||
// links are reached out of band (e.g. Tailscale) and their stored endpoint
|
||||
// is never used for routing, so a nominal endpoint is acceptable.
|
||||
if (input.request.managedTunnelsEnabled && !isSecureManagedEndpoint(endpoint)) {
|
||||
return yield* new EnvironmentLinkProofInvalid({
|
||||
userId: input.userId,
|
||||
environmentId: verified.environmentId,
|
||||
|
||||
@@ -2,11 +2,14 @@ import { createClerkClient, verifyToken } from "@clerk/backend";
|
||||
import { describe, expect, it } from "@effect/vitest";
|
||||
import { vi } from "vite-plus/test";
|
||||
import * as Context from "effect/Context";
|
||||
import * as Duration from "effect/Duration";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Fiber from "effect/Fiber";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Predicate from "effect/Predicate";
|
||||
import * as Redacted from "effect/Redacted";
|
||||
import * as TestClock from "effect/testing/TestClock";
|
||||
import * as Tracer from "effect/Tracer";
|
||||
import * as HttpRouter from "effect/unstable/http/HttpRouter";
|
||||
import * as HttpServerRequest from "effect/unstable/http/HttpServerRequest";
|
||||
@@ -14,6 +17,7 @@ import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
|
||||
import { RelayEnvironmentAuth } from "@t3tools/contracts/relay";
|
||||
|
||||
import {
|
||||
RELAY_REQUEST_DEADLINE_MS,
|
||||
relayCors,
|
||||
relayDocsRedirectRoute,
|
||||
relayEnvironmentAuthLayer,
|
||||
@@ -203,6 +207,33 @@ describe("relay request tracing", () => {
|
||||
expect(Option.getOrUndefined(spans[1]!.parent)?.spanId).toBe(spans[0]?.spanId);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("fails hung requests with a 504 before the client's 10s abort", () =>
|
||||
Effect.gen(function* () {
|
||||
const spans: Array<Tracer.NativeSpan> = [];
|
||||
const tracer = Tracer.make({
|
||||
span: (options) => {
|
||||
const span = new Tracer.NativeSpan(options);
|
||||
spans.push(span);
|
||||
return span;
|
||||
},
|
||||
});
|
||||
const request = HttpServerRequest.fromWeb(
|
||||
new Request("https://relay.test/v1/mobile/devices", { method: "POST" }),
|
||||
);
|
||||
|
||||
const fiber = yield* traceRelayHttpRequestWith(
|
||||
Effect.never,
|
||||
Layer.succeed(Tracer.Tracer, tracer),
|
||||
).pipe(Effect.provideService(HttpServerRequest.HttpServerRequest, request), Effect.forkChild);
|
||||
yield* TestClock.adjust(Duration.millis(RELAY_REQUEST_DEADLINE_MS));
|
||||
const response = yield* Fiber.join(fiber);
|
||||
|
||||
expect(response.status).toBe(504);
|
||||
expect(spans[0]?.attributes.get("relay.request.deadline_exceeded")).toBe(true);
|
||||
expect(spans[0]?.attributes.get("http.response.status_code")).toBe(504);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
describe("relay routing fallback", () => {
|
||||
|
||||
@@ -6,6 +6,7 @@ import * as DateTime from "effect/DateTime";
|
||||
import * as Duration from "effect/Duration";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Record from "effect/Record";
|
||||
import * as Redacted from "effect/Redacted";
|
||||
import * as Schema from "effect/Schema";
|
||||
@@ -155,6 +156,47 @@ export const relayDocsRedirectRoute = HttpRouter.add(
|
||||
HttpServerResponse.redirect("/docs"),
|
||||
);
|
||||
|
||||
// Shorter than the mobile client's 10s request timeout on purpose: when a
|
||||
// request hangs (e.g. a stuck upstream query), the client would otherwise
|
||||
// abort first, the invocation would die with the request span still open, and
|
||||
// the batched spans would never export — leaving no server-side trace at all.
|
||||
// Failing server-side first turns the hang into a completed 504 whose trace
|
||||
// contains the exact child span that stalled, and the response still carries
|
||||
// the traceparent back to the client.
|
||||
export const RELAY_REQUEST_DEADLINE_MS = 9_000;
|
||||
|
||||
const relayRequestDeadline = <E, R>(
|
||||
httpEffect: Effect.Effect<
|
||||
HttpServerResponse.HttpServerResponse,
|
||||
E,
|
||||
HttpServerRequest.HttpServerRequest | R
|
||||
>,
|
||||
) =>
|
||||
httpEffect.pipe(
|
||||
Effect.timeoutOption(Duration.millis(RELAY_REQUEST_DEADLINE_MS)),
|
||||
Effect.flatMap(
|
||||
Option.match({
|
||||
onNone: () =>
|
||||
Effect.gen(function* () {
|
||||
const request = yield* HttpServerRequest.HttpServerRequest;
|
||||
yield* Effect.logError("relay request exceeded deadline", {
|
||||
"http.method": request.method,
|
||||
"http.url": request.url,
|
||||
"relay.request.deadline_ms": RELAY_REQUEST_DEADLINE_MS,
|
||||
});
|
||||
yield* Effect.annotateCurrentSpan({
|
||||
"relay.request.deadline_exceeded": true,
|
||||
});
|
||||
return HttpServerResponse.jsonUnsafe(
|
||||
{ error: "relay_request_deadline_exceeded" },
|
||||
{ status: 504 },
|
||||
);
|
||||
}),
|
||||
onSome: Effect.succeed,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export const traceRelayHttpRequest = <E, R>(
|
||||
httpEffect: Effect.Effect<
|
||||
HttpServerResponse.HttpServerResponse,
|
||||
@@ -164,7 +206,7 @@ export const traceRelayHttpRequest = <E, R>(
|
||||
) =>
|
||||
// HttpMiddleware finalizes its span on the dispatcher; do not close a request-scoped exporter first.
|
||||
HttpMiddleware.tracer(
|
||||
appendRelayTraceContextResponseHeader.pipe(Effect.andThen(httpEffect)),
|
||||
appendRelayTraceContextResponseHeader.pipe(Effect.andThen(relayRequestDeadline(httpEffect))),
|
||||
).pipe(Effect.ensuring(Effect.yieldNow));
|
||||
|
||||
export const traceRelayHttpRequestWith = <E, R, LayerError, LayerRequirements>(
|
||||
@@ -391,6 +433,17 @@ export const mobileApi = HttpApiBuilder.group(
|
||||
return yield* registrations.registerLiveActivity({ userId, payload });
|
||||
}, mapRelayCommonApiErrors("invalid_dpop")),
|
||||
)
|
||||
.handle(
|
||||
"getAgentActivitySnapshot",
|
||||
Effect.fn("relay.api.mobile.getAgentActivitySnapshot")(function* () {
|
||||
const { userId, token } = yield* RelayClientPrincipal;
|
||||
const proofKeyThumbprint = yield* requireDpopPrincipalScope("mobile:registration");
|
||||
yield* requireDpopThumbprint(proofKeyThumbprint, {
|
||||
expectedAccessToken: token,
|
||||
}).pipe(Effect.provideService(DpopProofs.DpopProofReplay, dpopProofs));
|
||||
return yield* registrations.getAgentActivitySnapshot({ userId });
|
||||
}, mapRelayCommonApiErrors("invalid_dpop")),
|
||||
)
|
||||
.handle(
|
||||
"unregisterDevice",
|
||||
Effect.fn("relay.api.mobile.unregisterDevice")(function* (args) {
|
||||
|
||||
@@ -24,6 +24,8 @@ export const relayMobileDevices = pgTable(
|
||||
platform: varchar("platform", { length: 16 }).notNull().$type<"ios">(),
|
||||
iosMajorVersion: integer("ios_major_version").notNull(),
|
||||
appVersion: varchar("app_version", { length: 64 }),
|
||||
bundleId: varchar("bundle_id", { length: 255 }),
|
||||
apsEnvironment: varchar("aps_environment", { length: 16 }).$type<"sandbox" | "production">(),
|
||||
pushToken: text("push_token"),
|
||||
pushToStartToken: text("push_to_start_token"),
|
||||
preferencesJson: jsonb("preferences_json").notNull().$type<RelayAgentAwarenessPreferences>(),
|
||||
|
||||
@@ -2,6 +2,7 @@ import * as Alchemy from "alchemy";
|
||||
import * as Cloudflare from "alchemy/Cloudflare";
|
||||
import * as Drizzle from "alchemy/Drizzle";
|
||||
import * as Config from "effect/Config";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import * as Crypto from "effect/Crypto";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
@@ -47,6 +48,7 @@ import { RelayApnsDeliveryDeadLetterQueue, RelayApnsDeliveryQueue } from "./queu
|
||||
import * as RelayConfiguration from "./Config.ts";
|
||||
import * as AgentActivityPublisher from "./agentActivity/AgentActivityPublisher.ts";
|
||||
import * as ApnsClient from "./agentActivity/ApnsClient.ts";
|
||||
import * as ApnsProviderTokens from "./agentActivity/ApnsProviderTokens.ts";
|
||||
import * as ApnsDeliveryQueue from "./agentActivity/ApnsDeliveryQueue.ts";
|
||||
import * as ApnsDeliveries from "./agentActivity/ApnsDeliveries.ts";
|
||||
import * as EnvironmentConnector from "./environments/EnvironmentConnector.ts";
|
||||
@@ -196,7 +198,7 @@ export default class Api extends Cloudflare.Worker<Api>()(
|
||||
),
|
||||
Layer.provideMerge(DpopProofs.layer),
|
||||
Layer.provideMerge(ApnsDeliveries.layer),
|
||||
Layer.provideMerge(ApnsClient.layer),
|
||||
Layer.provideMerge(ApnsClient.layer.pipe(Layer.provideMerge(ApnsProviderTokens.layer))),
|
||||
Layer.provideMerge(
|
||||
ApnsDeliveryQueue.layerCloudflareQueues(apnsDeliveryQueueSender, alchemyRuntimeContext),
|
||||
),
|
||||
@@ -242,7 +244,18 @@ export default class Api extends Cloudflare.Worker<Api>()(
|
||||
yield* Cloudflare.cron("*/5 * * * *").subscribe(() =>
|
||||
DpopProofs.DpopProofReplay.pipe(
|
||||
Effect.flatMap((dpopProofs) => dpopProofs.pruneExpired),
|
||||
Effect.withSpan("relay.cron.prune_expired_dpop_proofs"),
|
||||
// Terminal thread rows are kept briefly so finished agents show as
|
||||
// Done/Failed in the Live Activity; sweep them once they age out.
|
||||
Effect.andThen(
|
||||
Effect.all([AgentActivityRows.AgentActivityRows, DateTime.now]).pipe(
|
||||
Effect.flatMap(([activityRows, now]) =>
|
||||
activityRows.pruneTerminal({
|
||||
updatedBefore: DateTime.formatIso(DateTime.subtract(now, { minutes: 30 })),
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
Effect.withSpan("relay.cron.prune_expired_state"),
|
||||
Effect.provide(runtimeLayer),
|
||||
),
|
||||
);
|
||||
|
||||
+1
-1
@@ -48,7 +48,7 @@
|
||||
"engines": {
|
||||
"node": "^24.13.1"
|
||||
},
|
||||
"packageManager": "pnpm@10.24.0",
|
||||
"packageManager": "pnpm@11.10.0",
|
||||
"msw": {
|
||||
"workerDirectory": [
|
||||
"apps/web/public"
|
||||
|
||||
@@ -84,6 +84,7 @@ function relayClient(
|
||||
registerDevice: () => unsupported("registerDevice"),
|
||||
unregisterDevice: () => unsupported("unregisterDevice"),
|
||||
registerLiveActivity: () => unsupported("registerLiveActivity"),
|
||||
getAgentActivitySnapshot: () => unsupported("getAgentActivitySnapshot"),
|
||||
resetTokenCache: Effect.void,
|
||||
});
|
||||
}
|
||||
@@ -449,6 +450,7 @@ describe("ConnectionResolver", () => {
|
||||
new ManagedRelay.ManagedRelayRequestTimeoutError({
|
||||
activity: "Relay environment connection",
|
||||
timeoutMs: ManagedRelay.MANAGED_RELAY_REQUEST_TIMEOUT_MS,
|
||||
traceId: null,
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
@@ -112,6 +112,7 @@ const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
|
||||
registerDevice: () => Effect.die("unused"),
|
||||
unregisterDevice: () => Effect.die("unused"),
|
||||
registerLiveActivity: () => Effect.die("unused"),
|
||||
getAgentActivitySnapshot: () => Effect.die("unused"),
|
||||
resetTokenCache: Effect.void,
|
||||
} satisfies ManagedRelay.ManagedRelayClient["Service"]);
|
||||
const connectivity = Connectivity.Connectivity.of({
|
||||
@@ -261,6 +262,7 @@ describe("RelayEnvironmentDiscovery", () => {
|
||||
new ManagedRelay.ManagedRelayRequestTimeoutError({
|
||||
activity: "Relay environment listing",
|
||||
timeoutMs: ManagedRelay.MANAGED_RELAY_REQUEST_TIMEOUT_MS,
|
||||
traceId: null,
|
||||
}),
|
||||
),
|
||||
getEnvironmentStatus: () => Effect.die("unused"),
|
||||
@@ -272,6 +274,7 @@ describe("RelayEnvironmentDiscovery", () => {
|
||||
registerDevice: () => Effect.die("unused"),
|
||||
unregisterDevice: () => Effect.die("unused"),
|
||||
registerLiveActivity: () => Effect.die("unused"),
|
||||
getAgentActivitySnapshot: () => Effect.die("unused"),
|
||||
resetTokenCache: Effect.void,
|
||||
} satisfies ManagedRelay.ManagedRelayClient["Service"]);
|
||||
const layer = RelayEnvironmentDiscovery.layer.pipe(
|
||||
@@ -339,6 +342,55 @@ describe("RelayEnvironmentDiscovery", () => {
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("refreshes proactively when credentials change before any manual refresh", () =>
|
||||
Effect.gen(function* () {
|
||||
const harness = yield* makeHarness();
|
||||
yield* Effect.gen(function* () {
|
||||
const discovery = yield* RelayEnvironmentDiscovery.RelayEnvironmentDiscovery;
|
||||
const requests = yield* Ref.get(harness.statusRequests);
|
||||
for (const environment of environments) {
|
||||
yield* Deferred.succeed(
|
||||
requests.get(environment.environmentId)!,
|
||||
status(environment, "online"),
|
||||
);
|
||||
}
|
||||
|
||||
// Let the scoped wakeup subscription start before emitting, mirroring
|
||||
// a real sign-in which always happens long after service start.
|
||||
yield* Effect.yieldNow;
|
||||
|
||||
// Sign-in activates the session and emits credentials-changed; the
|
||||
// list must populate without any screen having asked for a refresh.
|
||||
yield* harness.wake("credentials-changed");
|
||||
const populated = yield* SubscriptionRef.changes(discovery.state).pipe(
|
||||
Stream.filter(
|
||||
(state) => state.environments.size === environments.length && !state.refreshing,
|
||||
),
|
||||
Stream.runHead,
|
||||
Effect.map(Option.getOrThrow),
|
||||
);
|
||||
expect(Option.isNone(populated.error)).toBe(true);
|
||||
expect(yield* Ref.get(harness.listCalls)).toBe(1);
|
||||
}).pipe(Effect.provide(harness.layer), Effect.scoped);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("settles to a clean empty state when refreshed while signed out", () =>
|
||||
Effect.gen(function* () {
|
||||
const harness = yield* makeHarness();
|
||||
yield* Effect.gen(function* () {
|
||||
const discovery = yield* RelayEnvironmentDiscovery.RelayEnvironmentDiscovery;
|
||||
yield* Ref.set(harness.clerkToken, null);
|
||||
yield* discovery.refresh;
|
||||
|
||||
const state = yield* SubscriptionRef.get(discovery.state);
|
||||
expect(state.environments.size).toBe(0);
|
||||
expect(state.refreshing).toBe(false);
|
||||
expect(Option.isNone(state.error)).toBe(true);
|
||||
}).pipe(Effect.provide(harness.layer));
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("does not republish stale rows after sign-out invalidates an in-flight refresh", () =>
|
||||
Effect.gen(function* () {
|
||||
const harness = yield* makeHarness();
|
||||
|
||||
@@ -222,7 +222,27 @@ export const make = Effect.fn("RelayEnvironmentDiscovery.make")(function* () {
|
||||
error: Option.none(),
|
||||
});
|
||||
|
||||
const clerkToken = yield* session.clerkToken;
|
||||
// Signed out is the idle state, not a failure: the proactive refresh on
|
||||
// credentials-changed also runs on sign-out and must settle back to a
|
||||
// clean empty list. Only the session-level "no credentials" error is
|
||||
// benign — relay-side auth failures (expired/invalid tokens) happen
|
||||
// after this point and must surface as errors.
|
||||
const tokenResult = yield* Effect.result(session.clerkToken);
|
||||
if (tokenResult._tag === "Failure") {
|
||||
const failure = tokenResult.failure;
|
||||
if (failure._tag === "ConnectionBlockedError" && failure.reason === "authentication") {
|
||||
if ((yield* Ref.get(accountGeneration)) !== generation) {
|
||||
return;
|
||||
}
|
||||
yield* SubscriptionRef.update(state, (current) => ({
|
||||
...current,
|
||||
refreshing: false,
|
||||
}));
|
||||
return;
|
||||
}
|
||||
return yield* Effect.fail(failure);
|
||||
}
|
||||
const clerkToken = tokenResult.success;
|
||||
if ((yield* Ref.get(accountGeneration)) !== generation) {
|
||||
return;
|
||||
}
|
||||
@@ -311,11 +331,12 @@ export const make = Effect.fn("RelayEnvironmentDiscovery.make")(function* () {
|
||||
yield* Ref.update(accountGeneration, (current) => current + 1);
|
||||
yield* Ref.set(activeAccountId, Option.none());
|
||||
yield* Ref.set(offlineReportFingerprints, new Map());
|
||||
const shouldRefresh = yield* Ref.get(hasRefreshed);
|
||||
yield* SubscriptionRef.set(state, EMPTY_RELAY_ENVIRONMENT_DISCOVERY_STATE);
|
||||
if (shouldRefresh) {
|
||||
yield* refresh.pipe(Effect.forkScoped);
|
||||
}
|
||||
// Refresh proactively — this wakeup fires when a session activates
|
||||
// (sign-in or cold start), and the list should be populated before
|
||||
// any screen asks for it. A signed-out refresh settles back to the
|
||||
// clean empty state.
|
||||
yield* refresh.pipe(Effect.forkScoped);
|
||||
})
|
||||
: Effect.void,
|
||||
),
|
||||
|
||||
@@ -17,11 +17,13 @@ import {
|
||||
RelayExchangeDpopAccessTokenEndpoint,
|
||||
RelayGetEnvironmentStatusEndpoint,
|
||||
RelayJwtSubjectTokenType,
|
||||
type RelayAgentActivitySnapshotResponse,
|
||||
type RelayLiveActivityRegistrationRequest,
|
||||
RelayMobileRegistrationScope,
|
||||
type RelayOkResponse,
|
||||
type RelayPublicClientId,
|
||||
RelayRegisterDeviceEndpoint,
|
||||
RelayAgentActivitySnapshotEndpoint,
|
||||
RelayRegisterLiveActivityEndpoint,
|
||||
RelayProtectedError,
|
||||
type RelayProtectedError as RelayProtectedErrorType,
|
||||
@@ -92,6 +94,7 @@ export const ManagedRelayRequestAction = Schema.Literals([
|
||||
"register relay mobile device",
|
||||
"unregister relay mobile device",
|
||||
"register relay live activity",
|
||||
"read relay agent activity snapshot",
|
||||
]);
|
||||
export type ManagedRelayRequestAction = typeof ManagedRelayRequestAction.Type;
|
||||
|
||||
@@ -107,6 +110,7 @@ export const ManagedRelayRequestActivity = Schema.Literals([
|
||||
"Relay mobile device registration",
|
||||
"Relay mobile device unregistration",
|
||||
"Relay Live Activity registration",
|
||||
"Relay agent activity snapshot",
|
||||
]);
|
||||
export type ManagedRelayRequestActivity = typeof ManagedRelayRequestActivity.Type;
|
||||
|
||||
@@ -115,6 +119,10 @@ export class ManagedRelayRequestTimeoutError extends Schema.TaggedErrorClass<Man
|
||||
{
|
||||
activity: ManagedRelayRequestActivity,
|
||||
timeoutMs: Schema.Number,
|
||||
// The CLIENT span's trace id. A timed-out request has no server response
|
||||
// to take an id from, but the client span was exported, so carrying its id
|
||||
// makes the failure searchable instead of logging `traceId: null`.
|
||||
traceId: Schema.NullOr(Schema.String),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
@@ -286,6 +294,9 @@ export class ManagedRelayClient extends Context.Service<
|
||||
readonly clerkToken: string;
|
||||
readonly payload: RelayLiveActivityRegistrationRequest;
|
||||
}) => Effect.Effect<RelayOkResponse, ManagedRelayClientError>;
|
||||
readonly getAgentActivitySnapshot: (input: {
|
||||
readonly clerkToken: string;
|
||||
}) => Effect.Effect<RelayAgentActivitySnapshotResponse, ManagedRelayClientError>;
|
||||
readonly resetTokenCache: Effect.Effect<void>;
|
||||
}
|
||||
>()("@t3tools/client-runtime/relay/managedRelay/ManagedRelayClient") {}
|
||||
@@ -326,11 +337,18 @@ function timeoutRelayRequest(activity: ManagedRelayRequestActivity) {
|
||||
Effect.flatMap(
|
||||
Option.match({
|
||||
onNone: () =>
|
||||
Effect.fail(
|
||||
new ManagedRelayRequestTimeoutError({
|
||||
activity,
|
||||
timeoutMs: MANAGED_RELAY_REQUEST_TIMEOUT_MS,
|
||||
}),
|
||||
Effect.currentParentSpan.pipe(
|
||||
Effect.map((span) => span.traceId),
|
||||
Effect.orElseSucceed(() => null),
|
||||
Effect.flatMap((traceId) =>
|
||||
Effect.fail(
|
||||
new ManagedRelayRequestTimeoutError({
|
||||
activity,
|
||||
timeoutMs: MANAGED_RELAY_REQUEST_TIMEOUT_MS,
|
||||
traceId,
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
onSome: Effect.succeed,
|
||||
}),
|
||||
@@ -399,6 +417,7 @@ function disabledManagedRelayClient(relayUrl: string): ManagedRelayClient["Servi
|
||||
registerDevice: unavailable("clientRuntime.managedRelay.registerDevice"),
|
||||
unregisterDevice: unavailable("clientRuntime.managedRelay.unregisterDevice"),
|
||||
registerLiveActivity: unavailable("clientRuntime.managedRelay.registerLiveActivity"),
|
||||
getAgentActivitySnapshot: unavailable("clientRuntime.managedRelay.getAgentActivitySnapshot"),
|
||||
resetTokenCache: Effect.void.pipe(
|
||||
Effect.withSpan("clientRuntime.managedRelay.resetTokenCache"),
|
||||
),
|
||||
@@ -446,6 +465,10 @@ export const make = Effect.fn("ManagedRelayClient.make")(function* (
|
||||
method: RelayUnregisterDeviceEndpoint.method,
|
||||
url: urlBuilder.mobile.unregisterDevice({ params: { deviceId } }),
|
||||
}),
|
||||
getAgentActivitySnapshot: (): DpopProofTarget => ({
|
||||
method: RelayAgentActivitySnapshotEndpoint.method,
|
||||
url: urlBuilder.mobile.getAgentActivitySnapshot(),
|
||||
}),
|
||||
registerLiveActivity: (): DpopProofTarget => ({
|
||||
method: RelayRegisterLiveActivityEndpoint.method,
|
||||
url: urlBuilder.mobile.registerLiveActivity(),
|
||||
@@ -845,6 +868,27 @@ export const make = Effect.fn("ManagedRelayClient.make")(function* (
|
||||
Effect.withSpan("clientRuntime.managedRelay.unregisterDevice"),
|
||||
withRelayClientTracing,
|
||||
),
|
||||
getAgentActivitySnapshot: Effect.fnUntraced(
|
||||
function* (input) {
|
||||
return yield* mobileRegistrationRequest(
|
||||
{
|
||||
clerkToken: input.clerkToken,
|
||||
target: dpopProofTargets.getAgentActivitySnapshot(),
|
||||
},
|
||||
(authorization) =>
|
||||
client.mobile
|
||||
.getAgentActivitySnapshot({
|
||||
headers: dpopHeaders(authorization),
|
||||
})
|
||||
.pipe(
|
||||
Effect.mapError(relayRequestError("read relay agent activity snapshot")),
|
||||
timeoutRelayRequest("Relay agent activity snapshot"),
|
||||
),
|
||||
);
|
||||
},
|
||||
Effect.withSpan("clientRuntime.managedRelay.getAgentActivitySnapshot"),
|
||||
withRelayClientTracing,
|
||||
),
|
||||
registerLiveActivity: Effect.fnUntraced(
|
||||
function* (input) {
|
||||
return yield* mobileRegistrationRequest(
|
||||
|
||||
@@ -83,6 +83,7 @@ function createManager(
|
||||
registerDevice: () => Effect.die("unused"),
|
||||
unregisterDevice: () => Effect.die("unused"),
|
||||
registerLiveActivity: () => Effect.die("unused"),
|
||||
getAgentActivitySnapshot: () => Effect.die("unused"),
|
||||
resetTokenCache: Effect.void,
|
||||
...overrides,
|
||||
});
|
||||
|
||||
@@ -123,7 +123,7 @@ export function createManagedRelaySession(input: ManagedRelaySessionInput): Mana
|
||||
try: () => readCachedClerkToken(nowMillis),
|
||||
catch: (cause) =>
|
||||
new ManagedRelaySessionError({
|
||||
message: "Could not obtain the T3 Cloud session token.",
|
||||
message: "Could not obtain the T3 Connect session token.",
|
||||
cause,
|
||||
}),
|
||||
});
|
||||
@@ -181,7 +181,7 @@ function readSessionClerkToken(
|
||||
? Effect.succeed(token)
|
||||
: Effect.fail(
|
||||
new ManagedRelaySessionError({
|
||||
message: "The T3 Cloud session token is unavailable.",
|
||||
message: "The T3 Connect session token is unavailable.",
|
||||
}),
|
||||
),
|
||||
),
|
||||
@@ -226,7 +226,7 @@ function requireClerkToken(
|
||||
if (!session || session.accountId !== accountId) {
|
||||
return Effect.fail(
|
||||
new ManagedRelaySessionError({
|
||||
message: "Sign in to T3 Cloud before loading relay data.",
|
||||
message: "Sign in to T3 Connect before loading relay data.",
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -296,7 +296,7 @@ export function readManagedRelaySnapshotState<A>(
|
||||
let errorTraceId: string | null = null;
|
||||
if (result._tag === "Failure") {
|
||||
const cause = Cause.squash(result.cause);
|
||||
error = cause instanceof Error ? cause.message : "Could not load T3 Cloud data.";
|
||||
error = cause instanceof Error ? cause.message : "Could not load T3 Connect data.";
|
||||
errorTraceId = findErrorTraceId(cause);
|
||||
}
|
||||
return {
|
||||
|
||||
@@ -155,6 +155,46 @@ describe("vcsActionState", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("clears running presentation state once the action finishes", () => {
|
||||
const initial = beginVcsActionState({
|
||||
operation: "run_change_request",
|
||||
label: "Running source control action",
|
||||
actionId,
|
||||
});
|
||||
const pushing = applyVcsActionProgressEvent(
|
||||
initial,
|
||||
progress({
|
||||
actionId,
|
||||
action,
|
||||
cwd,
|
||||
kind: "phase_started",
|
||||
phase: "push",
|
||||
label: "Pushing...",
|
||||
}),
|
||||
);
|
||||
const finished = applyVcsActionProgressEvent(
|
||||
pushing,
|
||||
progress({
|
||||
actionId,
|
||||
action,
|
||||
cwd,
|
||||
kind: "action_finished",
|
||||
result,
|
||||
}),
|
||||
);
|
||||
|
||||
expect(finished).toMatchObject({
|
||||
isRunning: false,
|
||||
operation: "run_change_request",
|
||||
actionId,
|
||||
action,
|
||||
currentLabel: null,
|
||||
currentPhaseLabel: null,
|
||||
lastOutputLine: null,
|
||||
error: null,
|
||||
});
|
||||
});
|
||||
|
||||
it("retains a terminal action error for presentation", () => {
|
||||
const initial = beginVcsActionState({
|
||||
operation: "run_change_request",
|
||||
|
||||
@@ -386,12 +386,10 @@ export function applyVcsActionProgressEvent(
|
||||
};
|
||||
case "action_finished":
|
||||
return {
|
||||
...current,
|
||||
isRunning: false,
|
||||
...EMPTY_VCS_ACTION_STATE,
|
||||
actionId: event.actionId,
|
||||
action: event.action,
|
||||
operation: "run_change_request",
|
||||
error: null,
|
||||
};
|
||||
case "action_failed":
|
||||
return {
|
||||
|
||||
@@ -342,6 +342,11 @@ export const EnvironmentCloudLinkStateResult = Schema.Struct({
|
||||
cloudUserId: Schema.NullOr(Schema.String),
|
||||
relayUrl: Schema.NullOr(Schema.String),
|
||||
relayIssuer: Schema.NullOr(Schema.String),
|
||||
// A managed Cloudflare tunnel is provisioned for this link. False for a
|
||||
// publish-only link (activity publishing without a relay-managed tunnel), so
|
||||
// clients can present the two capabilities as independent settings.
|
||||
// Optional so newer clients tolerate older environment servers.
|
||||
managedTunnelActive: Schema.optional(Schema.Boolean),
|
||||
publishAgentActivity: Schema.Boolean,
|
||||
});
|
||||
export type EnvironmentCloudLinkStateResult = typeof EnvironmentCloudLinkStateResult.Type;
|
||||
|
||||
@@ -35,12 +35,21 @@ export const RelayAgentAwarenessPreferences = Schema.Struct({
|
||||
});
|
||||
export type RelayAgentAwarenessPreferences = typeof RelayAgentAwarenessPreferences.Type;
|
||||
|
||||
export const RelayApnsEnvironment = Schema.Literals(["sandbox", "production"]);
|
||||
export type RelayApnsEnvironment = typeof RelayApnsEnvironment.Type;
|
||||
|
||||
export const RelayDeviceRegistrationRequest = Schema.Struct({
|
||||
deviceId: TrimmedNonEmptyString,
|
||||
label: TrimmedNonEmptyString,
|
||||
platform: RelayAgentAwarenessPlatform,
|
||||
iosMajorVersion: Schema.Int.check(Schema.isGreaterThanOrEqualTo(18)),
|
||||
appVersion: Schema.optional(TrimmedNonEmptyString),
|
||||
// APNs routing for this install: the topic must match the app's bundle id
|
||||
// (dev/preview/prod variants differ) and development-signed builds receive
|
||||
// sandbox tokens. Optional so older app builds keep registering; the relay
|
||||
// falls back to its configured defaults.
|
||||
bundleId: Schema.optional(TrimmedNonEmptyString),
|
||||
apsEnvironment: Schema.optional(RelayApnsEnvironment),
|
||||
pushToken: Schema.optional(TrimmedNonEmptyString),
|
||||
pushToStartToken: Schema.optional(TrimmedNonEmptyString),
|
||||
preferences: RelayAgentAwarenessPreferences,
|
||||
@@ -864,6 +873,24 @@ export const RelayRegisterLiveActivityEndpoint = HttpApiEndpoint.post(
|
||||
},
|
||||
).annotate(OpenApi.Summary, "Register a Live Activity push token");
|
||||
|
||||
export const RelayAgentActivitySnapshotResponse = Schema.Struct({
|
||||
aggregate: Schema.NullOr(RelayAgentActivityAggregateState),
|
||||
});
|
||||
export type RelayAgentActivitySnapshotResponse = typeof RelayAgentActivitySnapshotResponse.Type;
|
||||
|
||||
// Lets the app decide whether arming a Live Activity is worthwhile before
|
||||
// creating one (no empty lock-screen card when nothing is running) and seed
|
||||
// the card with the real aggregate instead of a placeholder.
|
||||
export const RelayAgentActivitySnapshotEndpoint = HttpApiEndpoint.get(
|
||||
"getAgentActivitySnapshot",
|
||||
"/v1/mobile/agent-activity",
|
||||
{
|
||||
headers: RelayDpopRequestHeaders,
|
||||
success: RelayAgentActivitySnapshotResponse,
|
||||
error: RelayAuthAndInternalErrors,
|
||||
},
|
||||
).annotate(OpenApi.Summary, "Read the current Live Activity aggregate");
|
||||
|
||||
export const RelayUnregisterDeviceEndpoint = HttpApiEndpoint.delete(
|
||||
"unregisterDevice",
|
||||
"/v1/mobile/devices/:deviceId",
|
||||
@@ -879,6 +906,7 @@ export const RelayMobileGroup = HttpApiGroup.make("mobile")
|
||||
.add(
|
||||
RelayRegisterDeviceEndpoint,
|
||||
RelayRegisterLiveActivityEndpoint,
|
||||
RelayAgentActivitySnapshotEndpoint,
|
||||
RelayUnregisterDeviceEndpoint,
|
||||
)
|
||||
.annotate(OpenApi.Description, "Mobile push-notification and Live Activity registration.")
|
||||
|
||||
@@ -102,6 +102,58 @@ describe("projectThreadAwareness", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("projects completed turns as completed even when teardown settled them as interrupted", () => {
|
||||
const finishedTurn = {
|
||||
turnId: "turn-1" as TurnId,
|
||||
state: "interrupted" as const,
|
||||
requestedAt: NOW,
|
||||
startedAt: NOW,
|
||||
completedAt: NOW,
|
||||
assistantMessageId: null,
|
||||
};
|
||||
const state = projectThreadAwareness({
|
||||
environmentId: "env-1" as EnvironmentId,
|
||||
project,
|
||||
thread: thread({ latestTurn: finishedTurn }),
|
||||
});
|
||||
|
||||
// Session teardown settles still-running turns by session status, and
|
||||
// that write can race turn.completed; the completion timestamp is the
|
||||
// durable signal. Without this the thread resolves to null persistently
|
||||
// and gets tombstoned off the lock-screen card instead of showing Done.
|
||||
expect(state?.phase).toBe("completed");
|
||||
|
||||
const trulyInterrupted = projectThreadAwareness({
|
||||
environmentId: "env-1" as EnvironmentId,
|
||||
project,
|
||||
thread: thread({ latestTurn: { ...finishedTurn, completedAt: null } }),
|
||||
});
|
||||
expect(trulyInterrupted).toBeNull();
|
||||
});
|
||||
|
||||
it("projects ready sessions with no materialized turn as completed", () => {
|
||||
// Quick threads without code changes never get a checkpoint, so the SQL
|
||||
// shell has no latestTurn row and latest_turn_id is cleared when the
|
||||
// session settles; the ready session is the only completion signal left.
|
||||
const state = projectThreadAwareness({
|
||||
environmentId: "env-1" as EnvironmentId,
|
||||
project,
|
||||
thread: thread({
|
||||
session: {
|
||||
threadId: "thread-1" as ThreadId,
|
||||
status: "ready",
|
||||
providerName: "Codex",
|
||||
runtimeMode: "full-access",
|
||||
activeTurnId: null,
|
||||
lastError: null,
|
||||
updatedAt: NOW,
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
expect(state?.phase).toBe("completed");
|
||||
});
|
||||
|
||||
it("projects failures with the session error detail", () => {
|
||||
const state = projectThreadAwareness({
|
||||
environmentId: "env-1" as EnvironmentId,
|
||||
|
||||
@@ -103,6 +103,24 @@ function resolveThreadAwarenessPhase(
|
||||
if (thread.latestTurn?.state === "completed") {
|
||||
return "completed";
|
||||
}
|
||||
// A turn that finished can still read as "interrupted" here: session
|
||||
// teardown settles still-running turns by session status, and that write
|
||||
// can race the turn.completed one. completedAt survives the race — a turn
|
||||
// that has a completion timestamp finished, whatever the state column says.
|
||||
// Without this, quick finish-then-teardown threads resolve to null
|
||||
// persistently and get tombstoned instead of published as completed.
|
||||
if (thread.latestTurn?.state === "interrupted" && thread.latestTurn.completedAt !== null) {
|
||||
return "completed";
|
||||
}
|
||||
// Threads whose turns never produce a checkpoint (no code changes) have no
|
||||
// materialized latestTurn in the shell at all, and the session-set
|
||||
// projection clears latest_turn_id the moment the session settles. The
|
||||
// session status is then the only surviving completion signal: a live
|
||||
// session at "ready"/"idle" with nothing pending and nothing running means
|
||||
// the agent finished and is waiting for the next prompt — Done.
|
||||
if (thread.session?.status === "ready" || thread.session?.status === "idle") {
|
||||
return "completed";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,42 @@
|
||||
diff --git a/android/src/main/java/com/swmansion/rnscreens/ScreenStackHeaderConfigViewManager.kt b/android/src/main/java/com/swmansion/rnscreens/ScreenStackHeaderConfigViewManager.kt
|
||||
index 03d8d9f3a5c403c93282d53cbacf1a339e877e85..54d5eabc6c8a408e711a5be97286fdbb8ab1c461 100644
|
||||
--- a/android/src/main/java/com/swmansion/rnscreens/ScreenStackHeaderConfigViewManager.kt
|
||||
+++ b/android/src/main/java/com/swmansion/rnscreens/ScreenStackHeaderConfigViewManager.kt
|
||||
@@ -103,6 +103,34 @@ class ScreenStackHeaderConfigViewManager :
|
||||
config.setTitle(title)
|
||||
}
|
||||
|
||||
+ // The t3 patch adds these iOS glass-chrome props to the native spec; they have
|
||||
+ // no Android implementation, but the codegen-generated interface requires the
|
||||
+ // setters to exist. No-ops keep the Android build compiling.
|
||||
+ override fun setSubtitle(
|
||||
+ config: ScreenStackHeaderConfig,
|
||||
+ subtitle: String?,
|
||||
+ ) = Unit
|
||||
+
|
||||
+ override fun setLargeSubtitle(
|
||||
+ config: ScreenStackHeaderConfig,
|
||||
+ largeSubtitle: String?,
|
||||
+ ) = Unit
|
||||
+
|
||||
+ override fun setNavigationItemStyle(
|
||||
+ config: ScreenStackHeaderConfig,
|
||||
+ navigationItemStyle: String?,
|
||||
+ ) = Unit
|
||||
+
|
||||
+ override fun setHeaderCenterBarButtonItems(
|
||||
+ config: ScreenStackHeaderConfig,
|
||||
+ headerCenterBarButtonItems: ReadableArray?,
|
||||
+ ) = Unit
|
||||
+
|
||||
+ override fun setHeaderToolbarItems(
|
||||
+ config: ScreenStackHeaderConfig,
|
||||
+ headerToolbarItems: ReadableArray?,
|
||||
+ ) = Unit
|
||||
+
|
||||
override fun setTitleFontFamily(
|
||||
config: ScreenStackHeaderConfig,
|
||||
titleFontFamily: String?,
|
||||
diff --git a/ios/RNSBarButtonItem.h b/ios/RNSBarButtonItem.h
|
||||
index ea5325ea8d17b1ddfa790ff8dab48ce83142d4d3..acd2fd7ceb7162ed300abc4d3c9f0c24f4d63898 100644
|
||||
--- a/ios/RNSBarButtonItem.h
|
||||
@@ -1067,7 +1106,7 @@ index b7568ecfebd4f4420f2a8d3fec5184d7fc728dd1..41ca1f273f0175929e73105faa463978
|
||||
onPressHeaderBarButtonMenuItem?: CT.DirectEventHandler<OnPressHeaderBarButtonMenuItemEvent> | undefined;
|
||||
synchronousShadowStateUpdatesEnabled?: CT.WithDefault<boolean, false>;
|
||||
diff --git a/lib/typescript/types.d.ts b/lib/typescript/types.d.ts
|
||||
index 3b384e03891e38e936f370372a682d73440e7ec2..1e3a966806c8b8b312f75fb367f758efe49dde9d 100644
|
||||
index 3b384e03891e38e936f370372a682d73440e7ec2..861ffed850e90f27916c427853b503dbe6ee9840 100644
|
||||
--- a/lib/typescript/types.d.ts
|
||||
+++ b/lib/typescript/types.d.ts
|
||||
@@ -10,6 +10,7 @@ export type SearchBarCommands = {
|
||||
|
||||
Generated
+548
-564
File diff suppressed because it is too large.
Load diff
+30
-15
@@ -5,14 +5,31 @@ packages:
|
||||
- packages/*
|
||||
- scripts
|
||||
|
||||
# Successor to onlyBuiltDependencies/ignoredBuiltDependencies (pnpm 11).
|
||||
# true = allowed to run build scripts; false mirrors the pnpm 10 behavior
|
||||
# where anything outside onlyBuiltDependencies was silently not built.
|
||||
allowBuilds:
|
||||
browser-tabs-lock: false
|
||||
bufferutil: false
|
||||
core-js: false
|
||||
electron: true
|
||||
electron-winstaller: false
|
||||
esbuild: true
|
||||
msgpackr-extract: true
|
||||
msw: false
|
||||
node-pty: true
|
||||
sharp: true
|
||||
utf-8-validate: false
|
||||
workerd: false
|
||||
|
||||
catalog:
|
||||
"@clerk/backend": 3.8.4
|
||||
"@clerk/clerk-js": 6.22.0
|
||||
"@clerk/electron": 0.0.5
|
||||
"@clerk/backend": 3.11.1
|
||||
"@clerk/clerk-js": 6.25.0
|
||||
"@clerk/electron": 0.0.10
|
||||
"@clerk/electron-passkeys": 0.0.3
|
||||
"@clerk/expo": 3.6.2
|
||||
"@clerk/react": 6.11.1
|
||||
"@clerk/shared": 4.22.0
|
||||
"@clerk/expo": 3.7.1
|
||||
"@clerk/react": 6.12.0
|
||||
"@clerk/shared": 4.25.0
|
||||
"@effect/atom-react": 4.0.0-beta.78
|
||||
"@effect/openapi-generator": 4.0.0-beta.78
|
||||
"@effect/platform-bun": 4.0.0-beta.78
|
||||
@@ -34,15 +51,13 @@ catalog:
|
||||
vite-plus: 0.2.2
|
||||
yaml: ^2.9.0
|
||||
|
||||
ignoredBuiltDependencies:
|
||||
- msw
|
||||
|
||||
onlyBuiltDependencies:
|
||||
- electron
|
||||
- esbuild
|
||||
- msgpackr-extract
|
||||
- node-pty
|
||||
- sharp
|
||||
minimumReleaseAgeExclude:
|
||||
- "@clerk/[email protected]"
|
||||
- "@clerk/[email protected]"
|
||||
- "@clerk/[email protected]"
|
||||
- "@clerk/[email protected]"
|
||||
- "@clerk/[email protected]"
|
||||
- "@clerk/[email protected]"
|
||||
|
||||
overrides:
|
||||
"@clerk/backend": "catalog:"
|
||||
|
||||
@@ -11,7 +11,7 @@ import { ChildProcessSpawner } from "effect/unstable/process";
|
||||
import {
|
||||
BuildCommandFailedError,
|
||||
createStageWorkspaceConfig,
|
||||
createStagePnpmConfig,
|
||||
createStagePatchedDependencies,
|
||||
createBuildConfig,
|
||||
DESKTOP_ASAR_UNPACK,
|
||||
InvalidMacPasskeyRpDomainError,
|
||||
@@ -168,7 +168,7 @@ it.layer(NodeServices.layer)("build-desktop-artifact", (it) => {
|
||||
|
||||
it("carries only staged dependency patch metadata into staged desktop installs", () => {
|
||||
assert.deepStrictEqual(
|
||||
createStagePnpmConfig(
|
||||
createStagePatchedDependencies(
|
||||
{
|
||||
"@expo/[email protected]": "patches/@expo%[email protected]",
|
||||
"@ff-labs/[email protected]": "patches/@[email protected]",
|
||||
@@ -183,28 +183,26 @@ it.layer(NodeServices.layer)("build-desktop-artifact", (it) => {
|
||||
},
|
||||
),
|
||||
{
|
||||
patchedDependencies: {
|
||||
"@ff-labs/[email protected]": "patches/@[email protected].patch",
|
||||
"@pierre/[email protected]": "patches/@pierre%[email protected].patch",
|
||||
"[email protected]": "patches/[email protected]",
|
||||
},
|
||||
"@ff-labs/[email protected]": "patches/@[email protected]",
|
||||
"@pierre/[email protected]": "patches/@pierre%[email protected].patch",
|
||||
"[email protected]": "patches/[email protected].patch",
|
||||
},
|
||||
);
|
||||
|
||||
assert.equal(
|
||||
createStagePnpmConfig(
|
||||
assert.deepStrictEqual(
|
||||
createStagePatchedDependencies(
|
||||
{
|
||||
"@expo/[email protected]": "patches/@expo%[email protected]",
|
||||
},
|
||||
{ effect: "4.0.0-beta.73" },
|
||||
),
|
||||
undefined,
|
||||
{},
|
||||
);
|
||||
});
|
||||
|
||||
it("installs optional native dependencies for the target desktop architecture", () => {
|
||||
assert.deepStrictEqual(STAGE_INSTALL_ARGS, ["install", "--prod"]);
|
||||
assert.deepStrictEqual(createStageWorkspaceConfig("mac", "x64"), {
|
||||
assert.deepStrictEqual(createStageWorkspaceConfig({ platform: "mac", arch: "x64" }), {
|
||||
supportedArchitectures: {
|
||||
os: ["darwin"],
|
||||
cpu: ["x64"],
|
||||
@@ -212,21 +210,21 @@ it.layer(NodeServices.layer)("build-desktop-artifact", (it) => {
|
||||
});
|
||||
// Windows artifacts also bundle the same-architecture WSL (Linux, glibc) backend, so the
|
||||
// staged install must fetch its native optional deps (e.g. ffi-rs) too.
|
||||
assert.deepStrictEqual(createStageWorkspaceConfig("win", "x64"), {
|
||||
assert.deepStrictEqual(createStageWorkspaceConfig({ platform: "win", arch: "x64" }), {
|
||||
supportedArchitectures: {
|
||||
os: ["win32", "linux"],
|
||||
cpu: ["x64"],
|
||||
libc: ["glibc"],
|
||||
},
|
||||
});
|
||||
assert.deepStrictEqual(createStageWorkspaceConfig("win", "arm64"), {
|
||||
assert.deepStrictEqual(createStageWorkspaceConfig({ platform: "win", arch: "arm64" }), {
|
||||
supportedArchitectures: {
|
||||
os: ["win32", "linux"],
|
||||
cpu: ["arm64"],
|
||||
libc: ["glibc"],
|
||||
},
|
||||
});
|
||||
assert.deepStrictEqual(createStageWorkspaceConfig("mac", "universal"), {
|
||||
assert.deepStrictEqual(createStageWorkspaceConfig({ platform: "mac", arch: "universal" }), {
|
||||
supportedArchitectures: {
|
||||
os: ["darwin"],
|
||||
cpu: ["arm64", "x64"],
|
||||
@@ -234,6 +232,62 @@ it.layer(NodeServices.layer)("build-desktop-artifact", (it) => {
|
||||
});
|
||||
});
|
||||
|
||||
it("stages pnpm 11 allowBuilds and patchedDependencies in the workspace yaml", () => {
|
||||
assert.deepStrictEqual(
|
||||
createStageWorkspaceConfig({
|
||||
platform: "linux",
|
||||
arch: "x64",
|
||||
allowBuilds: {
|
||||
electron: true,
|
||||
"node-pty": true,
|
||||
"browser-tabs-lock": false,
|
||||
},
|
||||
patchedDependencies: {
|
||||
"[email protected]": "patches/[email protected]",
|
||||
},
|
||||
overrides: {
|
||||
effect: "4.0.0-beta.73",
|
||||
},
|
||||
}),
|
||||
{
|
||||
supportedArchitectures: {
|
||||
os: ["linux"],
|
||||
cpu: ["x64"],
|
||||
},
|
||||
allowBuilds: {
|
||||
electron: true,
|
||||
"node-pty": true,
|
||||
"browser-tabs-lock": false,
|
||||
},
|
||||
patchedDependencies: {
|
||||
"[email protected]": "patches/[email protected]",
|
||||
},
|
||||
overrides: {
|
||||
effect: "4.0.0-beta.73",
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
// Empty maps must not be written — pnpm would still require reviewed
|
||||
// packages if allowBuilds is present but incomplete, and omitting empty
|
||||
// patchedDependencies keeps the stage yaml minimal.
|
||||
assert.deepStrictEqual(
|
||||
createStageWorkspaceConfig({
|
||||
platform: "mac",
|
||||
arch: "arm64",
|
||||
allowBuilds: {},
|
||||
patchedDependencies: {},
|
||||
overrides: {},
|
||||
}),
|
||||
{
|
||||
supportedArchitectures: {
|
||||
os: ["darwin"],
|
||||
cpu: ["arm64"],
|
||||
},
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it("unpacks the fff shared library for filesystem and FFI access", () => {
|
||||
assert.deepStrictEqual(DESKTOP_ASAR_UNPACK, ["node_modules/@ff-labs/fff-bin-*/**/*"]);
|
||||
});
|
||||
|
||||
@@ -40,6 +40,7 @@ const WorkspaceConfig = Schema.Struct({
|
||||
catalog: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
||||
overrides: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
||||
patchedDependencies: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
||||
allowBuilds: Schema.optional(Schema.Record(Schema.String, Schema.Boolean)),
|
||||
});
|
||||
type WorkspaceConfig = typeof WorkspaceConfig.Type;
|
||||
|
||||
@@ -49,7 +50,14 @@ const StageWorkspaceConfig = Schema.Struct({
|
||||
cpu: Schema.Array(Schema.String),
|
||||
libc: Schema.optional(Schema.Array(Schema.String)),
|
||||
}),
|
||||
// pnpm 11 only reads these from pnpm-workspace.yaml (not package.json#pnpm).
|
||||
// Without allowBuilds the staged `vp install --prod` fails with
|
||||
// ERR_PNPM_IGNORED_BUILDS for packages that have lifecycle scripts.
|
||||
allowBuilds: Schema.optional(Schema.Record(Schema.String, Schema.Boolean)),
|
||||
patchedDependencies: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
||||
overrides: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
||||
});
|
||||
type StageWorkspaceConfig = typeof StageWorkspaceConfig.Type;
|
||||
|
||||
const RepoRoot = Effect.service(Path.Path).pipe(
|
||||
Effect.flatMap((path) => path.fromFileUrl(new URL("..", import.meta.url))),
|
||||
@@ -563,10 +571,6 @@ interface StagePackageJson {
|
||||
readonly devDependencies: {
|
||||
readonly electron: string;
|
||||
};
|
||||
readonly overrides: Record<string, unknown>;
|
||||
readonly pnpm?: {
|
||||
readonly patchedDependencies?: Record<string, string>;
|
||||
};
|
||||
}
|
||||
|
||||
export const STAGE_INSTALL_ARGS = ["install", "--prod"] as const;
|
||||
@@ -876,10 +880,14 @@ const stageClerkPasskeyNativeBinaries = Effect.fn("stageClerkPasskeyNativeBinari
|
||||
}
|
||||
});
|
||||
|
||||
export function createStageWorkspaceConfig(
|
||||
platform: typeof BuildPlatform.Type,
|
||||
arch: typeof BuildArch.Type,
|
||||
): typeof StageWorkspaceConfig.Type {
|
||||
export function createStageWorkspaceConfig(input: {
|
||||
readonly platform: typeof BuildPlatform.Type;
|
||||
readonly arch: typeof BuildArch.Type;
|
||||
readonly allowBuilds?: Record<string, boolean>;
|
||||
readonly patchedDependencies?: Record<string, string>;
|
||||
readonly overrides?: Record<string, string>;
|
||||
}): StageWorkspaceConfig {
|
||||
const { platform, arch, allowBuilds, patchedDependencies, overrides } = input;
|
||||
const hostOs = platform === "mac" ? "darwin" : platform === "win" ? "win32" : "linux";
|
||||
const hostCpu = arch === "universal" ? ["arm64", "x64"] : [arch];
|
||||
// Windows artifacts also bundle the same-architecture WSL Linux backend, which loads
|
||||
@@ -887,36 +895,37 @@ export function createStageWorkspaceConfig(
|
||||
// Pull the Linux (glibc) variants in addition to the host platform's so
|
||||
// they ship in the asar; without them the WSL backend crash-loops on require
|
||||
// ("Cannot find module '@yuuang/ffi-rs-linux-x64-gnu'").
|
||||
if (platform === "win") {
|
||||
return {
|
||||
supportedArchitectures: {
|
||||
os: Array.from(new Set([hostOs, "linux"])),
|
||||
cpu: hostCpu,
|
||||
libc: ["glibc"],
|
||||
},
|
||||
};
|
||||
}
|
||||
const supportedArchitectures =
|
||||
platform === "win"
|
||||
? {
|
||||
os: Array.from(new Set([hostOs, "linux"])),
|
||||
cpu: hostCpu,
|
||||
libc: ["glibc"],
|
||||
}
|
||||
: {
|
||||
os: [hostOs],
|
||||
cpu: hostCpu,
|
||||
};
|
||||
|
||||
return {
|
||||
supportedArchitectures: {
|
||||
os: [hostOs],
|
||||
cpu: hostCpu,
|
||||
},
|
||||
supportedArchitectures,
|
||||
...(allowBuilds && Object.keys(allowBuilds).length > 0 ? { allowBuilds } : {}),
|
||||
...(patchedDependencies && Object.keys(patchedDependencies).length > 0
|
||||
? { patchedDependencies }
|
||||
: {}),
|
||||
...(overrides && Object.keys(overrides).length > 0 ? { overrides } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
export function createStagePnpmConfig(
|
||||
export function createStagePatchedDependencies(
|
||||
patchedDependencies: Record<string, string>,
|
||||
dependencies: Record<string, unknown>,
|
||||
): StagePackageJson["pnpm"] | undefined {
|
||||
const stagePatchedDependencies = Object.fromEntries(
|
||||
): Record<string, string> {
|
||||
return Object.fromEntries(
|
||||
Object.entries(patchedDependencies).filter(([patchKey]) =>
|
||||
Object.hasOwn(dependencies, getPatchedDependencyPackageName(patchKey)),
|
||||
),
|
||||
);
|
||||
|
||||
return Object.keys(stagePatchedDependencies).length > 0
|
||||
? { patchedDependencies: stagePatchedDependencies }
|
||||
: undefined;
|
||||
}
|
||||
|
||||
function getPatchedDependencyPackageName(patchKey: string): string {
|
||||
@@ -1540,6 +1549,7 @@ const buildDesktopArtifact = Effect.fn("buildDesktopArtifact")(function* (
|
||||
const workspaceCatalog = workspaceConfig.catalog ?? {};
|
||||
const workspaceOverrides = workspaceConfig.overrides ?? {};
|
||||
const workspacePatchedDependencies = workspaceConfig.patchedDependencies ?? {};
|
||||
const workspaceAllowBuilds = workspaceConfig.allowBuilds ?? {};
|
||||
|
||||
const platformConfig = PLATFORM_CONFIG[options.platform];
|
||||
if (!platformConfig) {
|
||||
@@ -1709,7 +1719,10 @@ const buildDesktopArtifact = Effect.fn("buildDesktopArtifact")(function* (
|
||||
)
|
||||
: {}),
|
||||
};
|
||||
const stagePnpmConfig = createStagePnpmConfig(workspacePatchedDependencies, stageDependencies);
|
||||
const stagePatchedDependencies = createStagePatchedDependencies(
|
||||
workspacePatchedDependencies,
|
||||
stageDependencies,
|
||||
);
|
||||
const stagePackageJson: StagePackageJson = {
|
||||
name: "t3code",
|
||||
version: appVersion,
|
||||
@@ -1738,20 +1751,24 @@ const buildDesktopArtifact = Effect.fn("buildDesktopArtifact")(function* (
|
||||
devDependencies: {
|
||||
electron: electronVersion,
|
||||
},
|
||||
overrides: resolvedOverrides,
|
||||
...(stagePnpmConfig ? { pnpm: stagePnpmConfig } : {}),
|
||||
};
|
||||
|
||||
const stagePackageJsonString = yield* encodeJsonString(stagePackageJson);
|
||||
yield* fs.writeFileString(path.join(stageAppDir, "package.json"), `${stagePackageJsonString}\n`);
|
||||
const stageWorkspaceConfig = createStageWorkspaceConfig(options.platform, options.arch);
|
||||
const stageWorkspaceConfig = createStageWorkspaceConfig({
|
||||
platform: options.platform,
|
||||
arch: options.arch,
|
||||
allowBuilds: workspaceAllowBuilds,
|
||||
patchedDependencies: stagePatchedDependencies,
|
||||
overrides: resolvedOverrides,
|
||||
});
|
||||
const stageWorkspaceConfigString = yield* encodeStageWorkspaceConfig(stageWorkspaceConfig);
|
||||
yield* fs.writeFileString(
|
||||
path.join(stageAppDir, "pnpm-workspace.yaml"),
|
||||
stageWorkspaceConfigString,
|
||||
);
|
||||
|
||||
if (Object.keys(workspacePatchedDependencies).length > 0) {
|
||||
if (Object.keys(stagePatchedDependencies).length > 0) {
|
||||
yield* fs.copy(path.join(repoRoot, "patches"), path.join(stageAppDir, "patches"));
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user