Skip to content
Myr

policy.ts

This file

Path

apps/cli/src/policy.ts

100 lines · 2.8 KiB · TypeScript

Last changed by

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 1–58why, one step back
1import { detectHarness } from "@repo/protocol/audit";
2import { ConfigProvider, Context, Effect, Layer, Option, Stdio } from "effect";34export type Mode = "human" | "agent" | "automation";56export interface Invocation {7 readonly mode: Mode;8 readonly harness: Option.Option<string>;9 readonly reason: string;10}1112export interface Surroundings {13 readonly env: Readonly<Record<string, string | undefined>>;14 readonly isTTY: boolean;15}1617const isSet = (value: string | undefined) =>18 value !== undefined && value !== "";1920export const detectMode = ({ env, isTTY }: Surroundings): Invocation => {21 const harness = Option.fromNullishOr(detectHarness(env).harness);2223 if (Option.isSome(harness)) {24 return {25 harness,26 mode: "agent",27 reason: `the env names the harness ${harness.value}`,28 };29 }3031 if (isSet(env.CI)) {32 return { harness, mode: "automation", reason: "CI is set" };33 }3435 return isTTY36 ? { harness, mode: "human", reason: "stdout is a terminal" }37 : { harness, mode: "automation", reason: "stdout is not a terminal" };38};3940export class Policy extends Context.Service<Policy, Invocation>()(41 "cli/Policy"42) {}4344export const policyFor = (surroundings: Surroundings): Layer.Layer<Policy> =>45 Layer.succeed(Policy)(detectMode(surroundings));4647const childrenOf = (node: ConfigProvider.Node): readonly string[] => {48 if ("keys" in node) {49 return [...node.keys];50 }5152 if ("length" in node) {53 return Array.from({ length: node.length }, (_, index) => String(index));54 }5556 return [];57};58

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

line 59why, one step back
59/** The provider splits names on `_` into a tree, so this walks it back together. */

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 60–85why, one step back
60const envOf = (provider: ConfigProvider.ConfigProvider) => {
61 const walk = (62 path: readonly string[]63 ): Effect.Effect<64 readonly (readonly [string, string])[],65 ConfigProvider.SourceError66 > =>67 Effect.flatMap(provider.load(path), (node) => {68 if (node === undefined) {69 return Effect.succeed([]);70 }7172 const here =73 path.length > 0 && node.value !== undefined74 ? [[path.join("_"), node.value] as const]75 : [];7677 return Effect.map(78 Effect.forEach(childrenOf(node), (key) => walk([...path, key])),79 (below) => [...here, ...below.flat()]80 );81 });8283 return Effect.map(walk([]), (entries) => Object.fromEntries(entries));84};85

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

line 86why, one step back
86/** An env that cannot be read reads as empty, so the mode then rests on the terminal alone. */

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 87–100why, one step back
87export const CurrentMode: Layer.Layer<Policy, never, Stdio.Stdio> =
88 Layer.effect(Policy)(89 Effect.gen(function* () {90 const provider = yield* ConfigProvider.ConfigProvider;91 const env = yield* Effect.orElseSucceed(envOf(provider), () => ({}));9293 const isTTY = yield* Effect.flatMap(94 Stdio.Stdio,95 (stdio) => stdio.stdoutIsTerminal96 );9798 return detectMode({ env, isTTY });99 })100 );