-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Prepare datamodel for multi-environment #1765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
juliusmarminge
wants to merge
24
commits into
t3code/pr-1708/web/atomic-store-refactor
Choose a base branch
from
t3code/remote-host-model
base: t3code/pr-1708/web/atomic-store-refactor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
8ccfe3e
Surface environment and repository identity metadata
juliusmarminge d1437f1
Include client-runtime in release smoke checks
juliusmarminge deb215e
Treat explicit null environments distinctly
juliusmarminge a16ee82
Support nested repo remotes and late URL resolution
juliusmarminge 40ab9d9
Require environment IDs in web state sync
juliusmarminge 97ba9f9
Preserve remote scoped state across snapshot syncs
juliusmarminge 74250e9
non empty
juliusmarminge 240438d
fine
juliusmarminge 938f3d6
Cache repository identity lookups by TTL
juliusmarminge fc2de81
move
juliusmarminge 403ac96
kewl
juliusmarminge 70d3731
Handle ports in remote URL normalization
juliusmarminge babe73c
Handle empty server URLs and stale sidebar threads
juliusmarminge 54f905c
Adapt multi-environment store to atomic refactor base
juliusmarminge 010f452
Scope thread state by environment
juliusmarminge 24ac444
Add draft routes for logical projects
juliusmarminge 46c3251
Scope web native API and git queries by environment
juliusmarminge 5e27490
Rename native API wrappers to local and environment APIs
juliusmarminge 29b6385
Split local and environment API helpers
juliusmarminge 580fe19
Canonicalize promoted drafts to server thread routes
juliusmarminge aab603f
Fix server environment test config generation
juliusmarminge 74abf72
Scope bootstrap state to environment keys
juliusmarminge 583f97c
Handle promoted drafts across scoped thread refs
juliusmarminge 3e7db1d
Fix lint suppression comment syntax
juliusmarminge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
apps/server/src/environment/Layers/ServerEnvironment.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import * as nodePath from "node:path"; | ||
| import * as NodeServices from "@effect/platform-node/NodeServices"; | ||
| import { expect, it } from "@effect/vitest"; | ||
| import { Effect, Exit, FileSystem, Layer, PlatformError } from "effect"; | ||
|
|
||
| import { deriveServerPaths, ServerConfig, type ServerConfigShape } from "../../config.ts"; | ||
| import { ServerEnvironment } from "../Services/ServerEnvironment.ts"; | ||
| import { ServerEnvironmentLive } from "./ServerEnvironment.ts"; | ||
|
|
||
| const makeServerEnvironmentLayer = (baseDir: string) => | ||
| ServerEnvironmentLive.pipe(Layer.provide(ServerConfig.layerTest(process.cwd(), baseDir))); | ||
|
|
||
| const makeServerConfig = Effect.fn(function* (baseDir: string) { | ||
| const derivedPaths = yield* deriveServerPaths(baseDir, undefined); | ||
|
|
||
| return { | ||
| ...derivedPaths, | ||
| logLevel: "Error", | ||
| traceMinLevel: "Info", | ||
| traceTimingEnabled: true, | ||
| traceBatchWindowMs: 200, | ||
| traceMaxBytes: 10 * 1024 * 1024, | ||
| traceMaxFiles: 10, | ||
| otlpTracesUrl: undefined, | ||
| otlpMetricsUrl: undefined, | ||
| otlpExportIntervalMs: 10_000, | ||
| otlpServiceName: "t3-server", | ||
| cwd: process.cwd(), | ||
| baseDir, | ||
| mode: "web", | ||
| autoBootstrapProjectFromCwd: false, | ||
| logWebSocketEvents: false, | ||
| port: 0, | ||
| host: undefined, | ||
| authToken: undefined, | ||
| staticDir: undefined, | ||
| devUrl: undefined, | ||
| noBrowser: false, | ||
| } satisfies ServerConfigShape; | ||
| }); | ||
|
|
||
| it.layer(NodeServices.layer)("ServerEnvironmentLive", (it) => { | ||
| it.effect("persists the environment id across service restarts", () => | ||
| Effect.gen(function* () { | ||
| const fileSystem = yield* FileSystem.FileSystem; | ||
| const baseDir = yield* fileSystem.makeTempDirectoryScoped({ | ||
| prefix: "t3-server-environment-test-", | ||
| }); | ||
|
|
||
| const first = yield* Effect.gen(function* () { | ||
| const serverEnvironment = yield* ServerEnvironment; | ||
| return yield* serverEnvironment.getDescriptor; | ||
| }).pipe(Effect.provide(makeServerEnvironmentLayer(baseDir))); | ||
| const second = yield* Effect.gen(function* () { | ||
| const serverEnvironment = yield* ServerEnvironment; | ||
| return yield* serverEnvironment.getDescriptor; | ||
| }).pipe(Effect.provide(makeServerEnvironmentLayer(baseDir))); | ||
|
|
||
| expect(first.environmentId).toBe(second.environmentId); | ||
| expect(second.capabilities.repositoryIdentity).toBe(true); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("fails instead of overwriting a persisted id when reading the file errors", () => | ||
| Effect.gen(function* () { | ||
| const fileSystem = yield* FileSystem.FileSystem; | ||
| const baseDir = yield* fileSystem.makeTempDirectoryScoped({ | ||
| prefix: "t3-server-environment-read-error-test-", | ||
| }); | ||
| const serverConfig = yield* makeServerConfig(baseDir); | ||
| const environmentIdPath = serverConfig.environmentIdPath; | ||
| yield* fileSystem.makeDirectory(nodePath.dirname(environmentIdPath), { recursive: true }); | ||
| yield* fileSystem.writeFileString(environmentIdPath, "persisted-environment-id\n"); | ||
| const writeAttempts: string[] = []; | ||
| const failingFileSystemLayer = FileSystem.layerNoop({ | ||
| exists: (path) => Effect.succeed(path === environmentIdPath), | ||
| readFileString: (path) => | ||
| path === environmentIdPath | ||
| ? Effect.fail( | ||
| PlatformError.systemError({ | ||
| _tag: "PermissionDenied", | ||
| module: "FileSystem", | ||
| method: "readFileString", | ||
| description: "permission denied", | ||
| pathOrDescriptor: path, | ||
| }), | ||
| ) | ||
| : Effect.fail( | ||
| PlatformError.systemError({ | ||
| _tag: "NotFound", | ||
| module: "FileSystem", | ||
| method: "readFileString", | ||
| description: "not found", | ||
| pathOrDescriptor: path, | ||
| }), | ||
| ), | ||
| writeFileString: (path) => { | ||
| writeAttempts.push(path); | ||
| return Effect.void; | ||
| }, | ||
| }); | ||
|
|
||
| const exit = yield* Effect.gen(function* () { | ||
| const serverEnvironment = yield* ServerEnvironment; | ||
| return yield* serverEnvironment.getDescriptor; | ||
| }).pipe( | ||
| Effect.provide( | ||
| ServerEnvironmentLive.pipe( | ||
| Layer.provide( | ||
| Layer.merge(Layer.succeed(ServerConfig, serverConfig), failingFileSystemLayer), | ||
| ), | ||
| ), | ||
| ), | ||
| Effect.exit, | ||
| ); | ||
|
|
||
| expect(Exit.isFailure(exit)).toBe(true); | ||
| expect(writeAttempts).toEqual([]); | ||
| expect(yield* fileSystem.readFileString(environmentIdPath)).toBe( | ||
| "persisted-environment-id\n", | ||
| ); | ||
| }), | ||
| ); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import { EnvironmentId, type ExecutionEnvironmentDescriptor } from "@t3tools/contracts"; | ||
| import { Effect, FileSystem, Layer, Path, Random } from "effect"; | ||
|
|
||
| import { ServerConfig } from "../../config.ts"; | ||
| import { ServerEnvironment, type ServerEnvironmentShape } from "../Services/ServerEnvironment.ts"; | ||
| import { version } from "../../../package.json" with { type: "json" }; | ||
|
|
||
| function platformOs(): ExecutionEnvironmentDescriptor["platform"]["os"] { | ||
| switch (process.platform) { | ||
| case "darwin": | ||
| return "darwin"; | ||
| case "linux": | ||
| return "linux"; | ||
| case "win32": | ||
| return "windows"; | ||
| default: | ||
| return "unknown"; | ||
| } | ||
| } | ||
|
|
||
| function platformArch(): ExecutionEnvironmentDescriptor["platform"]["arch"] { | ||
| switch (process.arch) { | ||
| case "arm64": | ||
| return "arm64"; | ||
| case "x64": | ||
| return "x64"; | ||
| default: | ||
| return "other"; | ||
| } | ||
| } | ||
|
|
||
| export const makeServerEnvironment = Effect.fn("makeServerEnvironment")(function* () { | ||
| const fileSystem = yield* FileSystem.FileSystem; | ||
| const path = yield* Path.Path; | ||
| const serverConfig = yield* ServerConfig; | ||
|
|
||
| const readPersistedEnvironmentId = Effect.gen(function* () { | ||
| const exists = yield* fileSystem | ||
| .exists(serverConfig.environmentIdPath) | ||
| .pipe(Effect.orElseSucceed(() => false)); | ||
| if (!exists) { | ||
| return null; | ||
| } | ||
|
|
||
| const raw = yield* fileSystem | ||
| .readFileString(serverConfig.environmentIdPath) | ||
| .pipe(Effect.map((value) => value.trim())); | ||
|
|
||
| return raw.length > 0 ? raw : null; | ||
| }); | ||
macroscopeapp[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const persistEnvironmentId = (value: string) => | ||
| fileSystem.writeFileString(serverConfig.environmentIdPath, `${value}\n`); | ||
|
|
||
| const environmentIdRaw = yield* Effect.gen(function* () { | ||
| const persisted = yield* readPersistedEnvironmentId; | ||
| if (persisted) { | ||
| return persisted; | ||
| } | ||
|
|
||
| const generated = yield* Random.nextUUIDv4; | ||
| yield* persistEnvironmentId(generated); | ||
| return generated; | ||
| }); | ||
|
|
||
| const environmentId = EnvironmentId.makeUnsafe(environmentIdRaw); | ||
| const cwdBaseName = path.basename(serverConfig.cwd).trim(); | ||
| const label = | ||
| serverConfig.mode === "desktop" | ||
| ? "Local environment" | ||
| : cwdBaseName.length > 0 | ||
| ? cwdBaseName | ||
| : "T3 environment"; | ||
|
|
||
| const descriptor: ExecutionEnvironmentDescriptor = { | ||
| environmentId, | ||
| label, | ||
| platform: { | ||
| os: platformOs(), | ||
| arch: platformArch(), | ||
| }, | ||
| serverVersion: version, | ||
| capabilities: { | ||
| repositoryIdentity: true, | ||
| }, | ||
| }; | ||
|
|
||
| return { | ||
| getEnvironmentId: Effect.succeed(environmentId), | ||
| getDescriptor: Effect.succeed(descriptor), | ||
| } satisfies ServerEnvironmentShape; | ||
| }); | ||
|
|
||
| export const ServerEnvironmentLive = Layer.effect(ServerEnvironment, makeServerEnvironment()); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import type { EnvironmentId, ExecutionEnvironmentDescriptor } from "@t3tools/contracts"; | ||
| import { ServiceMap } from "effect"; | ||
| import type { Effect } from "effect"; | ||
|
|
||
| export interface ServerEnvironmentShape { | ||
| readonly getEnvironmentId: Effect.Effect<EnvironmentId>; | ||
| readonly getDescriptor: Effect.Effect<ExecutionEnvironmentDescriptor>; | ||
| } | ||
|
|
||
| export class ServerEnvironment extends ServiceMap.Service< | ||
| ServerEnvironment, | ||
| ServerEnvironmentShape | ||
| >()("t3/environment/Services/ServerEnvironment") {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.