-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Surface execution environment and repository identity metadata #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
2
commits into
main
Choose a base branch
from
t3code/remote-host-model
base: main
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
2 commits
Select commit
Hold shift + click to select a range
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
33 changes: 33 additions & 0 deletions
33
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,33 @@ | ||
| import * as NodeServices from "@effect/platform-node/NodeServices"; | ||
| import { expect, it } from "@effect/vitest"; | ||
| import { Effect, FileSystem, Layer } from "effect"; | ||
|
|
||
| import { ServerConfig } 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))); | ||
|
|
||
| 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); | ||
| }), | ||
| ); | ||
| }); |
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,99 @@ | ||
| import { randomUUID } from "node:crypto"; | ||
| import { EnvironmentId, type ExecutionEnvironmentDescriptor } from "@t3tools/contracts"; | ||
| import { Effect, FileSystem, Layer, Path } from "effect"; | ||
|
|
||
| import { ServerConfig } from "../../config.ts"; | ||
| import { ServerEnvironment, type ServerEnvironmentShape } from "../Services/ServerEnvironment.ts"; | ||
| import { version } from "../../../package.json" with { type: "json" }; | ||
|
|
||
| const ENVIRONMENT_ID_FILENAME = "environment-id"; | ||
|
|
||
| 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.gen(function* () { | ||
| const fileSystem = yield* FileSystem.FileSystem; | ||
| const path = yield* Path.Path; | ||
| const serverConfig = yield* ServerConfig; | ||
| const environmentIdPath = path.join(serverConfig.stateDir, ENVIRONMENT_ID_FILENAME); | ||
|
|
||
| const readPersistedEnvironmentId = Effect.gen(function* () { | ||
| const exists = yield* fileSystem | ||
| .exists(environmentIdPath) | ||
| .pipe(Effect.orElseSucceed(() => false)); | ||
| if (!exists) { | ||
| return null; | ||
| } | ||
|
|
||
| const raw = yield* fileSystem.readFileString(environmentIdPath).pipe( | ||
| Effect.orElseSucceed(() => ""), | ||
| Effect.map((value) => value.trim()), | ||
| ); | ||
|
|
||
| return raw.length > 0 ? raw : null; | ||
| }); | ||
|
|
||
| const persistEnvironmentId = (value: string) => | ||
| fileSystem.writeFileString(environmentIdPath, `${value}\n`); | ||
|
|
||
| const environmentIdRaw = yield* readPersistedEnvironmentId.pipe( | ||
| Effect.flatMap((persisted) => { | ||
| if (persisted) { | ||
| return Effect.succeed(persisted); | ||
| } | ||
|
|
||
| const generated = randomUUID(); | ||
| return persistEnvironmentId(generated).pipe(Effect.as(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
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
76 changes: 76 additions & 0 deletions
76
apps/server/src/project/Layers/RepositoryIdentityResolver.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,76 @@ | ||
| import * as NodeServices from "@effect/platform-node/NodeServices"; | ||
| import { expect, it } from "@effect/vitest"; | ||
| import { Effect, FileSystem } from "effect"; | ||
|
|
||
| import { runProcess } from "../../processRunner.ts"; | ||
| import { RepositoryIdentityResolver } from "../Services/RepositoryIdentityResolver.ts"; | ||
| import { RepositoryIdentityResolverLive } from "./RepositoryIdentityResolver.ts"; | ||
|
|
||
| const git = (cwd: string, args: ReadonlyArray<string>) => | ||
| Effect.promise(() => runProcess("git", ["-C", cwd, ...args])); | ||
|
|
||
| it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => { | ||
| it.effect("normalizes equivalent GitHub remotes into a stable repository identity", () => | ||
| Effect.gen(function* () { | ||
| const fileSystem = yield* FileSystem.FileSystem; | ||
| const cwd = yield* fileSystem.makeTempDirectoryScoped({ | ||
| prefix: "t3-repository-identity-test-", | ||
| }); | ||
|
|
||
| yield* git(cwd, ["init"]); | ||
| yield* git(cwd, ["remote", "add", "origin", "git@github.com:T3Tools/t3code.git"]); | ||
|
|
||
| const resolver = yield* RepositoryIdentityResolver; | ||
| const identity = yield* resolver.resolve(cwd); | ||
|
|
||
| expect(identity).not.toBeNull(); | ||
| expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code"); | ||
| expect(identity?.displayName).toBe("T3Tools/t3code"); | ||
| expect(identity?.provider).toBe("github"); | ||
| expect(identity?.owner).toBe("T3Tools"); | ||
| expect(identity?.name).toBe("t3code"); | ||
| }).pipe(Effect.provide(RepositoryIdentityResolverLive)), | ||
| ); | ||
|
|
||
| it.effect("returns null for non-git folders and repos without remotes", () => | ||
| Effect.gen(function* () { | ||
| const fileSystem = yield* FileSystem.FileSystem; | ||
| const nonGitDir = yield* fileSystem.makeTempDirectoryScoped({ | ||
| prefix: "t3-repository-identity-non-git-", | ||
| }); | ||
| const gitDir = yield* fileSystem.makeTempDirectoryScoped({ | ||
| prefix: "t3-repository-identity-no-remote-", | ||
| }); | ||
|
|
||
| yield* git(gitDir, ["init"]); | ||
|
|
||
| const resolver = yield* RepositoryIdentityResolver; | ||
| const nonGitIdentity = yield* resolver.resolve(nonGitDir); | ||
| const noRemoteIdentity = yield* resolver.resolve(gitDir); | ||
|
|
||
| expect(nonGitIdentity).toBeNull(); | ||
| expect(noRemoteIdentity).toBeNull(); | ||
| }).pipe(Effect.provide(RepositoryIdentityResolverLive)), | ||
| ); | ||
|
|
||
| it.effect("prefers upstream over origin when both remotes are configured", () => | ||
| Effect.gen(function* () { | ||
| const fileSystem = yield* FileSystem.FileSystem; | ||
| const cwd = yield* fileSystem.makeTempDirectoryScoped({ | ||
| prefix: "t3-repository-identity-upstream-test-", | ||
| }); | ||
|
|
||
| yield* git(cwd, ["init"]); | ||
| yield* git(cwd, ["remote", "add", "origin", "git@github.com:julius/t3code.git"]); | ||
| yield* git(cwd, ["remote", "add", "upstream", "git@github.com:T3Tools/t3code.git"]); | ||
|
|
||
| const resolver = yield* RepositoryIdentityResolver; | ||
| const identity = yield* resolver.resolve(cwd); | ||
|
|
||
| expect(identity).not.toBeNull(); | ||
| expect(identity?.locator.remoteName).toBe("upstream"); | ||
| expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code"); | ||
| expect(identity?.displayName).toBe("T3Tools/t3code"); | ||
| }).pipe(Effect.provide(RepositoryIdentityResolverLive)), | ||
| ); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant no-op identity map in effect pipeline
Low Severity
Effect.map((option) => option)is a no-op identity transformation that does nothing. This appears to be a leftover from refactoring whereEffect.map(Option.map(...))was replaced — theEffect.mapwrapper wasn't removed when the inner logic moved into the subsequentEffect.flatMap.Reviewed by Cursor Bugbot for commit 410dbe8. Configure here.