agents-block.ts
This file
Path
packages/agent-kit/src/agents-block.ts
112 lines · 3.7 KiB · TypeScript
Last changed by
Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
History of this fileTake 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–112why, one step back1import { Brand } from "@repo/brand";2import type { Policy } from "@repo/protocol/domain/policy";3import type { LandedSha, ProjectName } from "@repo/protocol/ids";4import { Effect, Option } from "effect";56import { contentHash } from "./generated-file.ts";7import { contributeSkillName, contributeSkillPaths } from "./project-skill.ts";8import { lanesOf, loopSteps, unfencedNorms } from "./sections.ts";9import { code, listed, page } from "./text.ts";1011export interface AgentsBlock {12 readonly begin: string;13 readonly end: string;14 readonly body: string;15}1617const BEGIN = `<!-- BEGIN ${Brand.slug} contribute`;1819const END = `<!-- END ${Brand.slug} contribute -->`;2021const rules = (policy: Policy) => {22 const { judge, run, weakening } = policy;23 const closure = [...judge.paths, ...judge.pointers];2425 return [26 "- Change only files in the ask's territory; a push that changes anything else is refused with `OutOfScope`.",27 ...(closure.length === 028 ? []29 : [30 `- Never change the judge closure (${closure.map(code).join(", ")}); a push that does is refused with \`TouchedJudge\`.`,31 ]),32 ...(weakening.markers.length === 033 ? []34 : [35 `- Never add a marker that turns a check off (${weakening.markers.map(code).join(", ")}); one without a reason is refused with \`Weakened\`.`,36 ]),37 `- Test one file with ${code(run.test)}. The lanes are ${listed(lanesOf(run).map(([lane, command]) => `${code(lane)} (${code(command)})`))}.`,38 "- Every refusal has a `_tag`, `retryable` and `next`: run `next`. Add `--json` to any command for JSON.",39 ];40};4142const bodyOf = (project: ProjectName, policy: Policy) => {43 const [agentsPath] = contributeSkillPaths(project);4445 return page([46 `## Contributing to ${project}`,47 "",48 `This project takes changes through ${Brand.name}: an answer to one of its asks lands only by passing the gate. This block is generated from the config folder, and the gate decides what lands. The full guide is the ${code(contributeSkillName(project))} skill (${code(agentsPath)}).`,49 "",50 ...loopSteps(),51 "",52 ...rules(policy),53 "",54 "No fence yet: nothing checks these, so the gate won't refuse you for them.",55 "",56 ...unfencedNorms(policy.agents),57 ]);58};5960export const agentsBlock = (input: {61 readonly project: ProjectName;62 readonly policy: Policy;63 readonly landed: LandedSha;64}): Effect.Effect<AgentsBlock> => {65 const body = bodyOf(input.project, input.policy);6667 return contentHash(body).pipe(68 Effect.map((hash) => ({69 begin: `${BEGIN} (generated; content-hash: ${hash}) -->`,70 body,71 end: END,72 }))73 );74};7576const written = (block: AgentsBlock) =>77 `${block.begin}\n${block.body}${block.body.endsWith("\n") ? "" : "\n"}${block.end}`;7879const locate = (80 existing: string,81 end: string82): Option.Option<{ readonly from: number; readonly to: number }> => {83 const to = existing.indexOf(end);8485 if (to === -1) {86 return Option.none();87 }8889 const from = existing.lastIndexOf(BEGIN, to);9091 const marker =92 from !== -1 &&93 (from === 0 || existing[from - 1] === "\n") &&94 existing[from + BEGIN.length] === " ";9596 return marker ? Option.some({ from, to: to + end.length }) : Option.none();97};9899const separator = (existing: string) => {100 if (existing === "" || existing.endsWith("\n\n")) {101 return "";102 }103104 return existing.endsWith("\n") ? "\n" : "\n\n";105};106107export const upsertAgentsBlock = (existing: string, block: AgentsBlock) =>108 Option.match(locate(existing, block.end), {109 onNone: () => `${existing}${separator(existing)}${written(block)}\n`,110 onSome: ({ from, to }) =>111 `${existing.slice(0, from)}${written(block)}${existing.slice(to)}`,112 });