Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f2de92067f | |||
| df66bea5e4 | |||
| d09806daad | |||
| c8ffb9cc6f | |||
| 32806120b3 |
@@ -1,21 +0,0 @@
|
||||
---
|
||||
description: bot-agents lint has prefer-readonly-parameter-types and the size/complexity rules off (Deno had no limits) and no overrides; nothing else
|
||||
---
|
||||
|
||||
The repo follows [[lint-config-shape]] with one recorded exception, present
|
||||
when the migration PR was approved:
|
||||
|
||||
- `typescript/prefer-readonly-parameter-types` is `"off"` in
|
||||
`scripts/project-lint.ts`, as in the explicit-types template, because
|
||||
Node/web types (`Response`, `Stats`, `RequestInit`) can never satisfy it.
|
||||
- `vite.config.ts` has no `overrides`. The `scripts/**` override for
|
||||
`typescript/no-unsafe-enum-comparison` was removed (2026-10-16):
|
||||
`scripts/project-lint.ts` compares node types with `AST_NODE_TYPES` members
|
||||
from `@typescript-eslint/types` instead of string literals
|
||||
([[lint-overrides-policy]]).
|
||||
|
||||
Size and complexity limits match Deno, which had none here, so every Oxlint
|
||||
limit rule is `"off"` ([[lint-config-shape]]). `src/run/index.ts` and `src/post/index.ts` keep
|
||||
main's single-file layout (2026-10-17 revert of the size splits). Sequential
|
||||
awaits in loops became recursion or a `reduce` chain (`no-await-in-loop`) to
|
||||
stay sequential. Add no other opt-out without the user.
|
||||
@@ -72,7 +72,7 @@ import { x } from "../x.ts"; // no: no ../ anywhere
|
||||
- An area used from outside exposes its API through its alias target, and
|
||||
callers import the alias (`#ui`), not files inside it.
|
||||
- Barrels are also the standard fix for `import/max-dependencies`: one barrel
|
||||
import replaces many direct ones ([[lint-overrides-policy]]).
|
||||
import replaces many direct ones ([[lint-config]]).
|
||||
- `import/no-cycle` is on; a barrel must not create a cycle.
|
||||
|
||||
Relative imports are written with the `.ts` extension.
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
---
|
||||
description: vite.config.ts lint uses Oxlint categories at error with Deno size limits, names no rule itself, and spreads configs from the plugins and scripts/project-lint.ts
|
||||
---
|
||||
|
||||
The lint config is short and category-driven (user decisions 2026-10-14/15):
|
||||
turn on whole categories, then list only what parity or a real need adds.
|
||||
|
||||
```ts
|
||||
lint: {
|
||||
plugins: ["typescript", "unicorn", "oxc", "import"],
|
||||
jsPlugins: ["eslint-plugin-explicit-types", "./scripts/project-lint.ts"],
|
||||
categories: { correctness: "error", suspicious: "error", pedantic: "error", perf: "error" },
|
||||
rules: { ...explicitTypes.configs.concrete.rules, ...projectLint.configs.project.rules },
|
||||
ignorePatterns: ["src/generated/**"], // generated files only, see below
|
||||
options: { typeAware: true, typeCheck: true, maxWarnings: 0, reportUnusedDisableDirectives: "error" },
|
||||
},
|
||||
```
|
||||
|
||||
- **Categories:** `correctness`, `suspicious`, `pedantic`, `perf` at `error`.
|
||||
`style` and `restriction` are never turned on wholesale; pick single rules
|
||||
from them in `configs.project`. Extra rules that categories bring are fine.
|
||||
- **Size limits match Deno** (2026-10-17, replaces the 2026-10-16 "default
|
||||
limits stay"): repos behave as they did on Deno. bot-agents ran `deno lint`
|
||||
with no config, so it had no size or complexity limit, and every Oxlint
|
||||
limit rule (`complexity`, `max-lines`, `max-params`, `id-length`,
|
||||
`no-nested-ternary`, `max-lines-per-function`, `max-classes-per-file`,
|
||||
`max-depth`, `max-nested-callbacks`, `max-statements`,
|
||||
`import/max-dependencies`, `unicorn/max-nested-calls`) is `"off"` in
|
||||
`configs.project`. No file is split for size. Do not add new opt-outs
|
||||
anywhere without asking the user.
|
||||
- **`vite.config.ts` names no rule.** Every rule id, built-in ones included,
|
||||
lives in `configs.project` of `scripts/project-lint.ts`
|
||||
([[project-lint-plugin]]). `vite.config.ts` only spreads configs.
|
||||
- **Published plugins are consumed through their configs**: ESLint flat config
|
||||
uses `plugin.configs.<name>`; Oxlint lists the package in `jsPlugins` and
|
||||
spreads `plugin.configs.<name>.rules`, since Oxlint plugins carry no configs.
|
||||
Use the configs equivalent to what the repo enabled under Deno.
|
||||
- **`maxWarnings: 0`** keeps Deno's "every diagnostic fails" behaviour.
|
||||
- **`ignorePatterns`** is only for generated files (compiled output, codegen).
|
||||
Hand-written code is never ignored.
|
||||
- **No lint disable comments**: [[lint-no-disable-comments]].
|
||||
- **Parity:** [[lint-parity-never-shrinks]]. **Naming:** [[naming-id-match]].
|
||||
- Don't add a second import sorter; the import-structure plugin's
|
||||
`enforce-import-order` is the only one, and Oxfmt's `sortImports` stays off.
|
||||
|
||||
`perf` came last: in the template it flagged nine `.includes` on constant
|
||||
arrays, fixed as `ReadonlySet` + `.has()` rather than by turning a rule off.
|
||||
That is the expected response to any new rule: change the code.
|
||||
@@ -0,0 +1,51 @@
|
||||
---
|
||||
description: Lint config shape: every non-nursery Oxlint category on, plugins via `all`, a blacklist of offs in configs.project, per-file exceptions only in projectOverrides, ignorePatterns only for generated output
|
||||
---
|
||||
|
||||
Policy (2026-10-19): a blacklist, not a whitelist. Replaces the older
|
||||
category-subset and "no overrides" notes.
|
||||
|
||||
- **Categories:** `correctness`, `suspicious`, `pedantic`, `perf`, `style`,
|
||||
`restriction` at `error`. `nursery` stays off (no line for it).
|
||||
- **Plugins** are enabled whole through their `all` config
|
||||
(`explicitTypes.configs.all.rules`, `importStructure.configs.all.rules`).
|
||||
- **Blacklist:** repo-wide offs and option tweaks live in
|
||||
`projectLint.configs.project.rules` in `scripts/project-lint.ts`, spread
|
||||
last in `rules`, each with a one-line reason. Never re-list a rule a
|
||||
category already turns on. bot-agents runs on Node everywhere, so
|
||||
`import/no-nodejs-modules`, `unicorn/no-process-exit`, and `no-console` are
|
||||
off repo-wide.
|
||||
- **Per-file exceptions** only via `projectOverrides`, the whole `overrides`
|
||||
array exported from `scripts/project-lint.ts`: the Node code entry
|
||||
(`vite.config.ts`, `scripts/**`, `nodeRules`) first, then one entry per
|
||||
exception with a why-comment. `vite.config.ts` has exactly
|
||||
`overrides: projectOverrides`.
|
||||
- **No `allowFiles`** options in custom rules, and no disable comments
|
||||
([[lint-no-disable-comments]]).
|
||||
- **`ignorePatterns`** only for generated or build output, never hand-written
|
||||
code. A hand-written file is linted, with override entries for only what it
|
||||
legitimately breaks. bot-agents has no generated code, so none.
|
||||
- **Precedence:** categories < `rules` (a later spread wins) < `overrides` (a
|
||||
later entry wins).
|
||||
- Size and complexity limits (`complexity`, `max-lines`, ...) are set to the
|
||||
smallest value the current code passes.
|
||||
|
||||
```ts
|
||||
// scripts/project-lint.ts
|
||||
export const projectOverrides: OxlintOverride[] = [
|
||||
{ files: ["vite.config.ts", "scripts/**"], rules: nodeRules },
|
||||
// Why: src/example.ts must ... (a per-file exception, if one is ever needed)
|
||||
// { files: ["src/example.ts"], rules: { "some-rule": "off" } },
|
||||
];
|
||||
|
||||
// vite.config.ts
|
||||
rules: {
|
||||
...explicitTypes.configs.all.rules,
|
||||
...importStructure.configs.all.rules,
|
||||
...projectLint.configs.project.rules,
|
||||
},
|
||||
overrides: projectOverrides,
|
||||
```
|
||||
|
||||
A new category hit is fixed in the code; an off or exception is added only
|
||||
for a real collision or churn, with its reason. Rules: [[project-lint-plugin]].
|
||||
@@ -1,33 +0,0 @@
|
||||
---
|
||||
description: No lint overrides for source files; narrow ones only for scripts/**; generated files go in ignorePatterns; size limits match Deno, so nothing is split for size
|
||||
---
|
||||
|
||||
Final policy (2026-10-16). The user considers per-file `overrides` bad
|
||||
practice.
|
||||
|
||||
- **Source files: never.** No `overrides` entry may target `src/**` or any
|
||||
other hand-written production or test code.
|
||||
- **The one exception** is in lint-plugin repos: `import/max-dependencies`
|
||||
off for `src/rules/index.ts`, the rule registry, which must import every
|
||||
rule. It is not a precedent for apps.
|
||||
- **`scripts/**`:** a narrow override is allowed when a tooling script truly
|
||||
needs it, e.g. `no-console` in a CLI script.
|
||||
- **Generated files:** list them in `ignorePatterns`, never `overrides`.
|
||||
- **Anything else needs the user's approval first.** When the user is away,
|
||||
don't add it: refactor, or leave the violation failing and log the question.
|
||||
|
||||
## Fix the code instead
|
||||
|
||||
Category rule violations are fixed in the code without behavior change,
|
||||
covered by the tests, inside the same migration PR; e.g. `prefer-set-has`
|
||||
becomes `const K: ReadonlySet<string> = new Set([...])` with `.has`.
|
||||
|
||||
Size and complexity limits are not refactored for: they match what Deno
|
||||
enforced (2026-10-17, replacing the earlier "refactor to Oxlint's defaults"),
|
||||
which for bot-agents is none, so those rules are `"off"`
|
||||
([[lint-config-shape]]).
|
||||
|
||||
Alias rule: an alias may point anywhere and owns its target (a folder's
|
||||
`index.ts` owns the folder and below, any other file owns only itself); no
|
||||
two aliases may own the same file, and `../` is banned everywhere
|
||||
([[imports-aliases-barrels]]).
|
||||
@@ -52,5 +52,5 @@ export default plugin;
|
||||
and every repo should look the same.
|
||||
|
||||
`scripts/` is tooling, not production code: never packed or published, and
|
||||
no tests. Narrow `overrides` for `scripts/**` are allowed when a script truly
|
||||
needs one (e.g. `no-console`); see [[lint-overrides-policy]].
|
||||
no tests. Its offs sit in the Node code entry of `projectOverrides`; see
|
||||
[[lint-config]].
|
||||
|
||||
@@ -50,9 +50,11 @@ relative imports. `scripts/` is flat: one file per tool, no subfolders and no `i
|
||||
([.agents/memory/imports-aliases-barrels.md](.agents/memory/imports-aliases-barrels.md)).
|
||||
|
||||
The only config files are `package.json`, `tsconfig.json`, and
|
||||
`vite.config.ts`, plus `package-lock.json`. Repo-local lint rules and the rule
|
||||
list live in `scripts/project-lint.ts`. Lint disable comments are not allowed;
|
||||
fix the source instead.
|
||||
`vite.config.ts`, plus `package-lock.json`. Repo-local lint rules, the rule
|
||||
list, and the `overrides` array (`projectOverrides`) live in
|
||||
`scripts/project-lint.ts`
|
||||
([.agents/memory/lint-config.md](.agents/memory/lint-config.md)). Lint disable
|
||||
comments are not allowed; fix the source instead.
|
||||
|
||||
The composite action uses npm only to install the externally distributed
|
||||
Claude Code, Codex, and OpenCode command-line tools; do not treat those
|
||||
|
||||
+134
-69
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* `project` — lint rules this repository holds its own source to, on top of
|
||||
* the Oxlint categories in `vite.config.ts`. Its rules of its own are
|
||||
* `no-disable-comments` and `no-asset-imports`.
|
||||
* `project` — this repository's changes to the Oxlint categories in
|
||||
* `vite.config.ts`. Its rules of its own are `no-disable-comments` and
|
||||
* `no-asset-imports`.
|
||||
*
|
||||
* Loaded by `vite.config.ts` as an Oxlint `jsPlugins` entry.
|
||||
*
|
||||
@@ -10,11 +10,11 @@
|
||||
|
||||
import type { LanguageOptions, RuleDefinition, RuleVisitor, TextSourceCode } from "@eslint/core";
|
||||
import { AST_NODE_TYPES, type TSESTree } from "@typescript-eslint/types";
|
||||
import type { DummyRuleMap } from "vite-plus/lint";
|
||||
import type { DummyRuleMap, OxlintOverride } from "vite-plus/lint";
|
||||
|
||||
/** The source code both hosts hand a rule, with the comment list. */
|
||||
interface ProjectSourceCode extends TextSourceCode {
|
||||
getAllComments(): TSESTree.Comment[];
|
||||
getAllComments: () => TSESTree.Comment[];
|
||||
}
|
||||
|
||||
/** ESLint's default rule types, reporting TypeScript-ESTree nodes. */
|
||||
@@ -161,9 +161,9 @@ const noAssetImportsRule: ProjectRule = {
|
||||
};
|
||||
|
||||
/**
|
||||
* This repository's own rule list, spread by `vite.config.ts`: rules that
|
||||
* sit outside the Oxlint categories, plus the template's extras. Built-in
|
||||
* Oxlint rule ids sit here too, so `vite.config.ts` lists no rule by name.
|
||||
* This repository's changes to the Oxlint categories in `vite.config.ts`,
|
||||
* which turn every non-nursery category on: its own rules, the category
|
||||
* rules it turns off, and options that differ from a rule's defaults.
|
||||
*/
|
||||
const rules: DummyRuleMap = {
|
||||
// The explicit-types template's `camelcase` stand-in: no `_` inside a name
|
||||
@@ -174,31 +174,6 @@ const rules: DummyRuleMap = {
|
||||
"^_*[^_]*_*$|^[^a-z]*$",
|
||||
{ properties: false, classFields: false, onlyDeclarations: false, ignoreDestructuring: true },
|
||||
],
|
||||
// Rules outside the categories.
|
||||
"no-array-constructor": "error",
|
||||
"no-case-declarations": "error",
|
||||
"no-empty": "error",
|
||||
"no-fallthrough": "error",
|
||||
"no-inner-declarations": "error",
|
||||
"no-prototype-builtins": "error",
|
||||
"no-redeclare": "error",
|
||||
"no-regex-spaces": "error",
|
||||
"no-var": "error",
|
||||
"prefer-const": "error",
|
||||
"require-await": "error",
|
||||
"require-yield": "error",
|
||||
"typescript/adjacent-overload-signatures": "error",
|
||||
"typescript/ban-ts-comment": "error",
|
||||
"typescript/no-empty-interface": "error",
|
||||
"typescript/no-empty-object-type": "error",
|
||||
"typescript/no-explicit-any": "error",
|
||||
"typescript/no-namespace": "error",
|
||||
"typescript/no-non-null-asserted-optional-chain": "error",
|
||||
"typescript/no-unsafe-function-type": "error",
|
||||
"typescript/no-wrapper-object-types": "error",
|
||||
"typescript/prefer-as-const": "error",
|
||||
"typescript/prefer-namespace-keyword": "error",
|
||||
"typescript/triple-slash-reference": "error",
|
||||
// On Node, these come from an explicit `node:` import instead of the global scope.
|
||||
"no-restricted-globals": [
|
||||
"error",
|
||||
@@ -209,47 +184,137 @@ const rules: DummyRuleMap = {
|
||||
"setImmediate",
|
||||
"clearImmediate",
|
||||
],
|
||||
// The explicit-types template's extras.
|
||||
"import/no-cycle": "error",
|
||||
// `../` is banned in every file; anything above a file needs a `#` alias.
|
||||
"import/no-relative-parent-imports": "error",
|
||||
"typescript/consistent-type-imports": "error",
|
||||
"typescript/no-import-type-side-effects": "error",
|
||||
"typescript/no-non-null-assertion": "error",
|
||||
// No disable comments at all.
|
||||
// Single capital type parameters, which explicit-types requires, may be one letter.
|
||||
"id-length": ["error", { exceptionPatterns: ["^[A-Z]$"] }],
|
||||
// Option tweaks.
|
||||
"one-var": ["error", "never"],
|
||||
"no-use-before-define": ["error", { functions: false }],
|
||||
"func-style": ["error", "declaration", { allowArrowFunctions: true }],
|
||||
"unicorn/import-style": ["error", { styles: { "node:path": { named: true } } }],
|
||||
// Each limit is the smallest value the current code passes.
|
||||
complexity: ["error", 26],
|
||||
"max-depth": ["error", 6],
|
||||
"max-lines": ["error", { max: 2048, skipBlankLines: false, skipComments: false }],
|
||||
"max-lines-per-function": ["error", 96],
|
||||
"max-params": ["error", 4],
|
||||
"max-statements": ["error", 32],
|
||||
"import/max-dependencies": ["error", { max: 13 }],
|
||||
"unicorn/max-nested-calls": ["error", { max: 4 }],
|
||||
// No lint suppression and only `.ts` imports, as in the plugin template.
|
||||
"project/no-disable-comments": "error",
|
||||
"project/no-asset-imports": "error",
|
||||
"unicorn/no-abusive-eslint-disable": "error",
|
||||
// Size and complexity limits match Deno's: this repo's `deno lint` ran the
|
||||
// `recommended` set with no config file, so no size or complexity rule
|
||||
// applied and none applies here. Each Oxlint limit rule, from a category or
|
||||
// not, is off:
|
||||
// - `complexity`: Deno had no `max-complexity`.
|
||||
// - `max-lines`: Deno had no `max-file-lines`.
|
||||
// - `max-params`: Deno had no `max-params`.
|
||||
// - `id-length`: Deno had no `max-identifier-length`.
|
||||
// - `no-nested-ternary`: Deno had no `max-nest-ternaries`.
|
||||
// - `max-lines-per-function`, `max-classes-per-file`, `max-depth`,
|
||||
// `max-nested-callbacks`, `max-statements`, `import/max-dependencies`,
|
||||
// `unicorn/max-nested-calls`: Deno had none of these.
|
||||
complexity: "off",
|
||||
"max-lines": "off",
|
||||
"max-params": "off",
|
||||
"id-length": "off",
|
||||
"no-nested-ternary": "off",
|
||||
"max-lines-per-function": "off",
|
||||
"max-classes-per-file": "off",
|
||||
"max-depth": "off",
|
||||
"max-nested-callbacks": "off",
|
||||
"max-statements": "off",
|
||||
"import/max-dependencies": "off",
|
||||
"unicorn/max-nested-calls": "off",
|
||||
// Pedantic, but Node's and the web's own types (`Response`, `Stats`,
|
||||
// `RequestInit`) are mutable, so no parameter of theirs can pass; the
|
||||
// explicit-types template and the app repos turn it off too.
|
||||
// Off, collides with a repo or plugin rule, Oxfmt, or another category rule:
|
||||
// Collides with `#private`: members are public unless marked so.
|
||||
"typescript/explicit-member-accessibility": "off",
|
||||
// Collides with `typescript/prefer-readonly-parameter-types` being off.
|
||||
"typescript/prefer-readonly": "off",
|
||||
// Collides with Node's and the web's mutable types (`Response`, `Stats`).
|
||||
"typescript/prefer-readonly-parameter-types": "off",
|
||||
// Collides with explicit-types `no-utility-types`, which bans `Record`.
|
||||
"typescript/consistent-indexed-object-style": "off",
|
||||
// Collides with explicitly annotated declarations.
|
||||
"typescript/no-inferrable-types": "off",
|
||||
// Collides with import-structure `enforce-import-order`.
|
||||
"sort-imports": "off",
|
||||
// Collides with import-structure `enforce-mod-file` barrels.
|
||||
"oxc/no-barrel-file": "off",
|
||||
// Collides with exporting each declaration where it stands.
|
||||
"import/group-exports": "off",
|
||||
// Collides with exporting each declaration where it stands.
|
||||
"import/exports-last": "off",
|
||||
// Collides with `import/no-default-export`.
|
||||
"import/prefer-default-export": "off",
|
||||
// Collides with `unicorn/prefer-ternary`.
|
||||
"no-ternary": "off",
|
||||
// Collides with `require-await` and `promise-function-async`.
|
||||
"oxc/no-async-await": "off",
|
||||
// Collides with `typescript/prefer-optional-chain`.
|
||||
"oxc/no-optional-chaining": "off",
|
||||
// Collides with file mode checks such as `mode & 0o077`.
|
||||
"no-bitwise": "off",
|
||||
// Collides with explicit-types `no-optional-null`, which makes `undefined` the absent value.
|
||||
"no-undefined": "off",
|
||||
// Collides with `typescript/no-floating-promises`, which `void` satisfies.
|
||||
"no-void": "off",
|
||||
// Collides with JSON and Node APIs, which return `null`.
|
||||
"unicorn/no-null": "off",
|
||||
// Collides with `import/no-default-export`.
|
||||
"import/no-named-export": "off",
|
||||
// Collides with `typescript/consistent-type-imports`.
|
||||
"import/consistent-type-specifier-style": "off",
|
||||
// Collides with `require-await`, which fails an `async` wrapper with no `await`.
|
||||
"typescript/promise-function-async": "off",
|
||||
// Collides with explicit-types `no-undefined-type`: a `let` that `try`
|
||||
// assigns would need `T | undefined`.
|
||||
"init-declarations": "off",
|
||||
// Collides with `no-await-in-loop`: sequential steps chain through `reduce`.
|
||||
"unicorn/no-array-reduce": "off",
|
||||
// Collides with literal Gitea Actions `${{ }}` expressions in workflow strings.
|
||||
"no-template-curly-in-string": "off",
|
||||
// Node code, everywhere: this action runs on Node.
|
||||
// Node built-ins are the action's runtime.
|
||||
"import/no-nodejs-modules": "off",
|
||||
// Command-line entry points set their exit status.
|
||||
"unicorn/no-process-exit": "off",
|
||||
// The action reports to the job log through the console.
|
||||
"no-console": "off",
|
||||
// PENDING USER: union members are values in profiles.json, the Gitea API,
|
||||
// and posted comments; renaming them changes behavior.
|
||||
"explicit-types/upper-snake-string-unions": "off",
|
||||
// PENDING USER: a fitting `name` needs a constructor and changes logged
|
||||
// error names.
|
||||
"unicorn/custom-error-definition": "off",
|
||||
// Off, too much churn:
|
||||
// Literal numbers read better than a constant per use.
|
||||
"no-magic-numbers": "off",
|
||||
// Keys follow meaning, not the alphabet.
|
||||
"sort-keys": "off",
|
||||
// Comments may start with code or lowercase identifiers.
|
||||
"capitalized-comments": "off",
|
||||
// explicit-types `require-return-type` covers return types.
|
||||
"typescript/explicit-function-return-type": "off",
|
||||
// explicit-types `require-return-type` covers return types.
|
||||
"typescript/explicit-module-boundary-types": "off",
|
||||
// Empty callbacks are legitimate no-ops.
|
||||
"no-empty-function": "off",
|
||||
// Methods may not need `this`.
|
||||
"class-methods-use-this": "off",
|
||||
// Braces only where a case declares.
|
||||
"unicorn/switch-case-braces": "off",
|
||||
// Plain property reads are fine.
|
||||
"prefer-destructuring": "off",
|
||||
// Object rest and spread are idiomatic.
|
||||
"oxc/no-rest-spread-properties": "off",
|
||||
// Short numbers need no separators.
|
||||
"unicorn/numeric-separators-style": "off",
|
||||
// `continue` keeps loop bodies flat.
|
||||
"no-continue": "off",
|
||||
};
|
||||
|
||||
/** Node code (`vite.config.ts` and `scripts/`): the first `projectOverrides` entry. */
|
||||
const nodeRules: DummyRuleMap = {
|
||||
// Vite and the Oxlint plugin host read a default export.
|
||||
"import/no-default-export": "off",
|
||||
// Node code: tooling imports Node's built-ins.
|
||||
"import/no-nodejs-modules": "off",
|
||||
// Node code: a CLI exits with a status.
|
||||
"unicorn/no-process-exit": "off",
|
||||
// Node code: a CLI reports through the console.
|
||||
"no-console": "off",
|
||||
};
|
||||
|
||||
/**
|
||||
* The whole `overrides` array of `vite.config.ts`: the Node code entry first,
|
||||
* then each per-file exception, each with a comment saying why. Later entries
|
||||
* win over earlier ones, and all of them over `rules`.
|
||||
*/
|
||||
export const projectOverrides: OxlintOverride[] = [
|
||||
{
|
||||
files: ["vite.config.ts", "scripts/**"],
|
||||
rules: nodeRules,
|
||||
},
|
||||
];
|
||||
|
||||
/**
|
||||
* The repo-local plugin, namespace `project`: its own rules, enabled as
|
||||
* `project/<rule>`, and `configs.project`, the rule list above.
|
||||
|
||||
+10
-8
@@ -19,10 +19,10 @@ test("retries once after an invalid refresh token", async () => {
|
||||
() =>
|
||||
Promise.resolve({
|
||||
code: 1,
|
||||
error: attempts++ === 0 ? "ERROR: invalid_refresh_token" : "still failed",
|
||||
error: (attempts += 1) === 1 ? "ERROR: invalid_refresh_token" : "still failed",
|
||||
}),
|
||||
() => {
|
||||
relogins++;
|
||||
relogins += 1;
|
||||
return Promise.resolve();
|
||||
},
|
||||
);
|
||||
@@ -39,14 +39,14 @@ test("does not retry an unrelated failure", async () => {
|
||||
let relogins = 0;
|
||||
await retryInvalidToken(
|
||||
() => {
|
||||
attempts++;
|
||||
attempts += 1;
|
||||
return Promise.resolve({
|
||||
code: 1,
|
||||
error: "rate limited",
|
||||
});
|
||||
},
|
||||
() => {
|
||||
relogins++;
|
||||
relogins += 1;
|
||||
return Promise.resolve();
|
||||
},
|
||||
);
|
||||
@@ -87,11 +87,11 @@ test("re-logs in after the job 9837 revoked-session stderr", async () => {
|
||||
const result = await retryInvalidToken(
|
||||
() =>
|
||||
Promise.resolve({
|
||||
code: attempts++ > 0 ? 0 : 1,
|
||||
code: (attempts += 1) > 1 ? 0 : 1,
|
||||
error: attempts === 1 ? REVOKED_CODEX.join("\n") : "",
|
||||
}),
|
||||
() => {
|
||||
relogins++;
|
||||
relogins += 1;
|
||||
return Promise.resolve();
|
||||
},
|
||||
);
|
||||
@@ -144,7 +144,8 @@ test.skipIf(IS_WINDOWS)("stages and persists only a regular private Codex login"
|
||||
|
||||
const staged = await stageCodexAuth(canonical, home, currentUid(), currentGid());
|
||||
expect(await readFile(staged, "utf8")).toStrictEqual(AUTH);
|
||||
expect((await stat(staged)).mode & 0o777).toBe(0o600);
|
||||
const stagedStat = await stat(staged);
|
||||
expect(stagedStat.mode & 0o777).toBe(0o600);
|
||||
|
||||
const refreshed = JSON.stringify({
|
||||
auth_mode: "chatgpt",
|
||||
@@ -153,7 +154,8 @@ test.skipIf(IS_WINDOWS)("stages and persists only a regular private Codex login"
|
||||
await writeFile(staged, refreshed);
|
||||
await persistCodexAuth(staged, canonical);
|
||||
expect(await readFile(canonical, "utf8")).toStrictEqual(refreshed);
|
||||
expect((await stat(canonical)).mode & 0o777).toBe(0o600);
|
||||
const canonicalStat = await stat(canonical);
|
||||
expect(canonicalStat.mode & 0o777).toBe(0o600);
|
||||
} finally {
|
||||
await rm(root, { recursive: true });
|
||||
}
|
||||
|
||||
+12
-9
@@ -12,7 +12,7 @@ import {
|
||||
} from "node:fs/promises";
|
||||
import { dirname, join } from "node:path";
|
||||
|
||||
import { isNotFound, isObject } from "#common";
|
||||
import { field, isNotFound, isObject } from "#common";
|
||||
|
||||
interface CodexApiKey {
|
||||
kind: "api-key";
|
||||
@@ -89,15 +89,16 @@ async function readCodexAuth(path: string): Promise<string> {
|
||||
} catch {
|
||||
throw new Error("invalid Codex login");
|
||||
}
|
||||
const tokens = isObject(value) ? value["tokens"] : undefined;
|
||||
const authMode = field(value, "auth_mode");
|
||||
const tokens = field(value, "tokens");
|
||||
if (
|
||||
!isObject(value) ||
|
||||
typeof value["auth_mode"] !== "string" ||
|
||||
value["auth_mode"] === "" ||
|
||||
typeof authMode !== "string" ||
|
||||
authMode === "" ||
|
||||
!isObject(tokens) ||
|
||||
!["access_token", "refresh_token", "id_token", "account_id"].every(
|
||||
(name) => typeof tokens[name] === "string" && tokens[name] !== "",
|
||||
)
|
||||
!["access_token", "refresh_token", "id_token", "account_id"].every((name) => {
|
||||
const token = field(tokens, name);
|
||||
return typeof token === "string" && token !== "";
|
||||
})
|
||||
) {
|
||||
throw new Error("invalid Codex login");
|
||||
}
|
||||
@@ -120,7 +121,9 @@ export async function stageCodexAuth(
|
||||
await chmod(stagedPath, 0o600);
|
||||
await chown(stagedPath, uid, gid);
|
||||
} catch (error) {
|
||||
if (!isNotFound(error)) throw error;
|
||||
if (!isNotFound(error)) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
return stagedPath;
|
||||
}
|
||||
|
||||
+21
-13
@@ -1,4 +1,4 @@
|
||||
import { isObject } from "#common";
|
||||
import { field, isObject } from "#common";
|
||||
import { AgentAuthError } from "#profiles";
|
||||
|
||||
export function openCodeArgs(model: string, prompt: string): string[] {
|
||||
@@ -49,20 +49,23 @@ export async function collectOpenCodeOutput(
|
||||
export function openCodeError(output: string): Error | null {
|
||||
let failure: Error | null = null;
|
||||
for (const line of output.split("\n")) {
|
||||
if (line.trim() === "") continue;
|
||||
if (line.trim() === "") {
|
||||
continue;
|
||||
}
|
||||
let event: unknown;
|
||||
try {
|
||||
event = JSON.parse(line);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
if (!isObject(event)) continue;
|
||||
const { type, error } = event;
|
||||
if (type !== "error" || !isObject(error)) {
|
||||
const error = field(event, "error");
|
||||
if (field(event, "type") !== "error" || !isObject(error)) {
|
||||
continue;
|
||||
}
|
||||
const { name, data } = error;
|
||||
const { message, statusCode } = isObject(data) ? data : {};
|
||||
const name = field(error, "name");
|
||||
const data = field(error, "data");
|
||||
const message = field(data, "message");
|
||||
const statusCode = field(data, "statusCode");
|
||||
const detail = `opencode ${typeof name === "string" ? name : "error"}${
|
||||
typeof statusCode === "number" ? ` (API error ${statusCode})` : ""
|
||||
}: ${typeof message === "string" ? message : "unknown error"}`;
|
||||
@@ -79,13 +82,18 @@ export function openCodeError(output: string): Error | null {
|
||||
export function parseOpenCodeEvents(output: string): string {
|
||||
let result: string | null = null;
|
||||
for (const line of output.split("\n")) {
|
||||
if (line.trim() === "") continue;
|
||||
if (line.trim() === "") {
|
||||
continue;
|
||||
}
|
||||
const event: unknown = JSON.parse(line);
|
||||
if (!isObject(event)) continue;
|
||||
const { type, part } = event;
|
||||
if (type !== "text" || !isObject(part)) continue;
|
||||
const { text } = part;
|
||||
if (typeof text === "string" && text.trim() !== "") result = text;
|
||||
const part = field(event, "part");
|
||||
if (field(event, "type") !== "text" || !isObject(part)) {
|
||||
continue;
|
||||
}
|
||||
const text = field(part, "text");
|
||||
if (typeof text === "string" && text.trim() !== "") {
|
||||
result = text;
|
||||
}
|
||||
}
|
||||
if (result === null) {
|
||||
throw openCodeError(output) ?? new Error("OpenCode produced no text output");
|
||||
|
||||
+1
-2
@@ -1,4 +1,3 @@
|
||||
// Helpers every phase shares: error and JSON value checks.
|
||||
export { isNotFound } from "./errors.ts";
|
||||
export { isMapping, isObject } from "./json.ts";
|
||||
export type { JsonObject } from "./json.ts";
|
||||
export { field, isMapping, isObject } from "./json.ts";
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import { expect, test } from "vite-plus/test";
|
||||
|
||||
import { field, isMapping, isObject } from "./json.ts";
|
||||
|
||||
test("reads a named field of a parsed object", () => {
|
||||
const value: unknown = JSON.parse('{"login":"bot","user":{"login":"nested"}}');
|
||||
expect(field(value, "login")).toBe("bot");
|
||||
expect(field(field(value, "user"), "login")).toBe("nested");
|
||||
expect(field(value, "missing")).toBeUndefined();
|
||||
});
|
||||
|
||||
test("reads no field of a value that is not an object", () => {
|
||||
for (const value of [null, undefined, "text", 1, true]) {
|
||||
expect(field(value, "login")).toBeUndefined();
|
||||
}
|
||||
});
|
||||
|
||||
test("counts arrays as objects but not as mappings", () => {
|
||||
expect(isObject([])).toBe(true);
|
||||
expect(isMapping([])).toBe(false);
|
||||
expect(isMapping({})).toBe(true);
|
||||
expect(field(["first"], "0")).toBe("first");
|
||||
});
|
||||
+12
-5
@@ -1,12 +1,19 @@
|
||||
// Type guards for values read from JSON, which arrive as `unknown`.
|
||||
export type JsonObject = NodeJS.Dict<unknown>;
|
||||
// Checks and field reads for values parsed from JSON, which arrive as
|
||||
// `unknown`. Callers read the fields they know by name into their own
|
||||
// declared interfaces.
|
||||
|
||||
// Any non-null object, arrays included: reading a missing key yields undefined.
|
||||
export function isObject(value: unknown): value is JsonObject {
|
||||
// Any non-null object, arrays included.
|
||||
export function isObject(value: unknown): value is object {
|
||||
return typeof value === "object" && value !== null;
|
||||
}
|
||||
|
||||
// A plain mapping, which an array is not.
|
||||
export function isMapping(value: unknown): value is JsonObject {
|
||||
export function isMapping(value: unknown): value is object {
|
||||
return isObject(value) && !Array.isArray(value);
|
||||
}
|
||||
|
||||
// The named field of an object, or undefined for a missing field or a value
|
||||
// that is not an object.
|
||||
export function field(value: unknown, name: string): unknown {
|
||||
return isObject(value) ? Reflect.get(value, name) : undefined;
|
||||
}
|
||||
|
||||
+3
-1
@@ -8,7 +8,9 @@ export { type Fetcher, type PostContext, postRuntime } from "./post.ts";
|
||||
|
||||
function required(name: string): string {
|
||||
const value = process.env[name];
|
||||
if (value === undefined || value === "") throw new Error(`${name} is not set`);
|
||||
if (value === undefined || value === "") {
|
||||
throw new Error(`${name} is not set`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
+12
-4
@@ -45,9 +45,15 @@ async function writeArtifacts(
|
||||
review?: string,
|
||||
anchors?: unknown,
|
||||
): Promise<void> {
|
||||
if (!outcome.ok) return;
|
||||
if (review !== undefined) await writePrivate(outcome.reviewPath, review);
|
||||
if (anchors !== undefined) await writePrivate(outcome.anchorsPath, JSON.stringify(anchors));
|
||||
if (!outcome.ok) {
|
||||
return;
|
||||
}
|
||||
if (review !== undefined) {
|
||||
await writePrivate(outcome.reviewPath, review);
|
||||
}
|
||||
if (anchors !== undefined) {
|
||||
await writePrivate(outcome.anchorsPath, JSON.stringify(anchors));
|
||||
}
|
||||
}
|
||||
|
||||
function manifestFor(runtimeDir: string, profileHome: string): RuntimeManifest {
|
||||
@@ -106,7 +112,9 @@ const missing = async (path: string): Promise<boolean> => {
|
||||
await stat(path);
|
||||
return false;
|
||||
} catch (error) {
|
||||
if (isNotFound(error)) return true;
|
||||
if (isNotFound(error)) {
|
||||
return true;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
+53
-37
@@ -2,7 +2,7 @@ import type { Stats } from "node:fs";
|
||||
import { lstat, readFile, rm } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { isNotFound, isObject } from "#common";
|
||||
import { field, isNotFound, isObject } from "#common";
|
||||
import { failureFooter, type FailureReason, type ProfileFailure, withProfile } from "#profiles";
|
||||
import { readRuntimeManifest, type RuntimeManifest, type RuntimeOutcome } from "#runtime";
|
||||
|
||||
@@ -22,12 +22,12 @@ interface Anchor {
|
||||
|
||||
const ANSI = new RegExp(`${String.fromCodePoint(27)}\\[[0-9;]*[A-Za-z]`, "gu");
|
||||
const stripAnsi = (text: string): string => text.replace(ANSI, "");
|
||||
const VERDICTS: NodeJS.Dict<[event: string, mark: string]> = {
|
||||
Approved: ["APPROVED", "✅"],
|
||||
"Changes requested": ["REQUEST_CHANGES", "🛑"],
|
||||
};
|
||||
const PROFILE_TYPES: Set<unknown> = new Set(["claude", "codex", "opencode"]);
|
||||
const FAILURE_REASONS: Set<unknown> = new Set(["sign-in failed", "usage exhausted", "failed"]);
|
||||
const VERDICTS = new Map<string, [event: string, mark: string]>([
|
||||
["Approved", ["APPROVED", "✅"]],
|
||||
["Changes requested", ["REQUEST_CHANGES", "🛑"]],
|
||||
]);
|
||||
const PROFILE_TYPES = new Set<unknown>(["claude", "codex", "opencode"]);
|
||||
const FAILURE_REASONS = new Set<unknown>(["sign-in failed", "usage exhausted", "failed"]);
|
||||
const isProfileType = (value: unknown): value is ProfileFailure["type"] => PROFILE_TYPES.has(value);
|
||||
const isFailureReason = (value: unknown): value is FailureReason => FAILURE_REASONS.has(value);
|
||||
const failureComment = (failures: ProfileFailure[]): string =>
|
||||
@@ -40,13 +40,19 @@ class GiteaRequestError extends Error {}
|
||||
|
||||
function parseAnchors(text: string, sourcePath: string): Anchor[] {
|
||||
const entries: unknown = JSON.parse(text);
|
||||
if (!Array.isArray(entries)) throw new TypeError(`${sourcePath}: not an array`);
|
||||
if (!Array.isArray(entries)) {
|
||||
throw new TypeError(`${sourcePath}: not an array`);
|
||||
}
|
||||
return entries.map((entry: unknown, index) => {
|
||||
const at = `${sourcePath}[${index}]`;
|
||||
if (!isObject(entry)) {
|
||||
throw new TypeError(`${at}: not an object`);
|
||||
}
|
||||
const { path, line, side = "new", body } = entry;
|
||||
const path = field(entry, "path");
|
||||
const line = field(entry, "line");
|
||||
const givenSide = field(entry, "side");
|
||||
const side = givenSide === undefined ? "new" : givenSide;
|
||||
const body = field(entry, "body");
|
||||
if (typeof path !== "string" || path === "") {
|
||||
throw new TypeError(`${at}.path: expected a repository path`);
|
||||
}
|
||||
@@ -68,43 +74,42 @@ async function readOutcome(path: string): Promise<RuntimeOutcome> {
|
||||
if (!isObject(value) || !("ok" in value)) {
|
||||
throw new Error("invalid agent outcome");
|
||||
}
|
||||
const outcome = value;
|
||||
if (outcome["ok"] === false) {
|
||||
const ok = field(value, "ok");
|
||||
if (ok === false) {
|
||||
// Only known labels reach the comment, never raw agent output.
|
||||
const failures: unknown[] = Array.isArray(outcome["failures"]) ? outcome["failures"] : [];
|
||||
const listed = field(value, "failures");
|
||||
const failures: unknown[] = Array.isArray(listed) ? listed : [];
|
||||
return {
|
||||
ok: false,
|
||||
failures: failures.flatMap((entry: unknown) => {
|
||||
const { type, model, reason } = isObject(entry) ? entry : {};
|
||||
const type = field(entry, "type");
|
||||
const model = field(entry, "model");
|
||||
const reason = field(entry, "reason");
|
||||
return isProfileType(type) && typeof model === "string" && isFailureReason(reason)
|
||||
? [{ type, model, reason }]
|
||||
: [];
|
||||
}),
|
||||
};
|
||||
}
|
||||
const body = field(value, "body");
|
||||
const reviewPath = field(value, "reviewPath");
|
||||
const anchorsPath = field(value, "anchorsPath");
|
||||
const profile = field(value, "profile");
|
||||
if (
|
||||
outcome["ok"] !== true ||
|
||||
typeof outcome["body"] !== "string" ||
|
||||
typeof outcome["reviewPath"] !== "string" ||
|
||||
typeof outcome["anchorsPath"] !== "string" ||
|
||||
!isObject(outcome["profile"])
|
||||
ok !== true ||
|
||||
typeof body !== "string" ||
|
||||
typeof reviewPath !== "string" ||
|
||||
typeof anchorsPath !== "string" ||
|
||||
!isObject(profile)
|
||||
) {
|
||||
throw new Error("invalid agent outcome");
|
||||
}
|
||||
const profile = outcome["profile"];
|
||||
if (!isProfileType(profile["type"]) || typeof profile["model"] !== "string") {
|
||||
const type = field(profile, "type");
|
||||
const model = field(profile, "model");
|
||||
if (!isProfileType(type) || typeof model !== "string") {
|
||||
throw new Error("invalid agent outcome");
|
||||
}
|
||||
return {
|
||||
ok: true,
|
||||
body: outcome["body"],
|
||||
reviewPath: outcome["reviewPath"],
|
||||
anchorsPath: outcome["anchorsPath"],
|
||||
profile: {
|
||||
type: profile["type"],
|
||||
model: profile["model"],
|
||||
},
|
||||
};
|
||||
return { ok: true, body, reviewPath, anchorsPath, profile: { type, model } };
|
||||
}
|
||||
|
||||
async function readAgentArtifact(
|
||||
@@ -113,12 +118,16 @@ async function readAgentArtifact(
|
||||
name: string,
|
||||
): Promise<string | null> {
|
||||
const profile = manifest.profiles.find((entry) => join(entry.home, name) === path);
|
||||
if (profile === undefined) throw new Error(`${name}: outside profile home`);
|
||||
if (profile === undefined) {
|
||||
throw new Error(`${name}: outside profile home`);
|
||||
}
|
||||
let stat: Stats;
|
||||
try {
|
||||
stat = await lstat(path);
|
||||
} catch (error) {
|
||||
if (isNotFound(error)) return null;
|
||||
if (isNotFound(error)) {
|
||||
return null;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
if (
|
||||
@@ -140,7 +149,8 @@ export async function postRuntime(
|
||||
): Promise<void> {
|
||||
const manifest = await readRuntimeManifest(manifestPath);
|
||||
try {
|
||||
const reviewerToken = (await readFile(manifest.reviewerFile, "utf8")).trim();
|
||||
const reviewerText = await readFile(manifest.reviewerFile, "utf8");
|
||||
const reviewerToken = reviewerText.trim();
|
||||
const request = async (path: string, body: unknown): Promise<void> => {
|
||||
let response: Response;
|
||||
try {
|
||||
@@ -188,7 +198,7 @@ export async function postRuntime(
|
||||
}
|
||||
|
||||
const [verdict = "", ...rest] = review.split("\n");
|
||||
const [event, mark] = VERDICTS[verdict.trim()] ?? ["COMMENT", "💬"];
|
||||
const [event, mark] = VERDICTS.get(verdict.trim()) ?? ["COMMENT", "💬"];
|
||||
const anchorText = await readAgentArtifact(manifest, outcome.anchorsPath, "anchors.json");
|
||||
const anchors = anchorText === null ? [] : parseAnchors(anchorText, outcome.anchorsPath);
|
||||
const postReview = (entries: Anchor[]): Promise<void> =>
|
||||
@@ -205,20 +215,26 @@ export async function postRuntime(
|
||||
})),
|
||||
});
|
||||
await postReview(anchors).catch(async (error: unknown) => {
|
||||
if (anchors.length === 0) throw error;
|
||||
if (anchors.length === 0) {
|
||||
throw error;
|
||||
}
|
||||
console.error(
|
||||
`inline comments rejected: ${error instanceof Error ? error.message : String(error)}`,
|
||||
);
|
||||
await postReview([]);
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof GiteaRequestError) throw error;
|
||||
if (error instanceof GiteaRequestError) {
|
||||
throw error;
|
||||
}
|
||||
await postComment(failureComment([{ ...outcome.profile, reason: "failed" }]));
|
||||
throw error;
|
||||
}
|
||||
} finally {
|
||||
await rm(manifest.runtimeDir, { recursive: true }).catch((error: unknown) => {
|
||||
if (!isNotFound(error)) throw error;
|
||||
if (!isNotFound(error)) {
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,9 @@ import { prepareRuntime } from "#runtime";
|
||||
|
||||
const env = (name: string): string => {
|
||||
const value = process.env[name];
|
||||
if (value === undefined || value === "") throw new Error(`${name} is not set`);
|
||||
if (value === undefined || value === "") {
|
||||
throw new Error(`${name} is not set`);
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
|
||||
@@ -25,14 +25,14 @@ function runAs(
|
||||
selected: RuntimeProfile,
|
||||
command: string,
|
||||
args: string[],
|
||||
env?: NodeJS.ProcessEnv,
|
||||
env?: ReadonlyMap<string, string>,
|
||||
): SpawnSyncReturns<string> {
|
||||
return spawnSync(command, args, {
|
||||
cwd: "/",
|
||||
uid: selected.uid,
|
||||
gid: selected.gid,
|
||||
encoding: "utf8",
|
||||
...(env !== undefined && { env }),
|
||||
...(env !== undefined && { env: Object.fromEntries(env) }),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -96,7 +96,9 @@ test.skipIf(!IS_ROOT)("cleanup leaves no runnable daemonized process", async ()
|
||||
await killProfileProcesses(selected.uid);
|
||||
|
||||
const status = await readFile(`/proc/${pid}/status`, "utf8").catch((error: unknown) => {
|
||||
if (isNotFound(error)) return null;
|
||||
if (isNotFound(error)) {
|
||||
return null;
|
||||
}
|
||||
throw error;
|
||||
});
|
||||
expect(status === null || !isRunnableProfileProcess(status, selected.uid)).toBe(true);
|
||||
@@ -104,9 +106,12 @@ test.skipIf(!IS_ROOT)("cleanup leaves no runnable daemonized process", async ()
|
||||
|
||||
test.skipIf(!IS_ROOT)("the agent process cannot read its coordinator environment", () => {
|
||||
const selected = profile(20_020, "/tmp");
|
||||
const result = runAs(selected, "/usr/bin/cat", [`/proc/${process.pid}/environ`], {
|
||||
PARENT_SECRET: "must-not-cross-boundary",
|
||||
});
|
||||
const result = runAs(
|
||||
selected,
|
||||
"/usr/bin/cat",
|
||||
[`/proc/${process.pid}/environ`],
|
||||
new Map([["PARENT_SECRET", "must-not-cross-boundary"]]),
|
||||
);
|
||||
|
||||
expect(result.status).not.toBe(0);
|
||||
});
|
||||
|
||||
@@ -6,8 +6,8 @@ import { isNotFound } from "#common";
|
||||
import type { RuntimeProfile } from "#runtime";
|
||||
|
||||
export function isRunnableProfileProcess(status: string, uid: number): boolean {
|
||||
const owner = /^Uid:\s+(\d+)/mu.exec(status)?.[1];
|
||||
const state = /^State:\s+(\w)/mu.exec(status)?.[1];
|
||||
const owner = /^Uid:\s+(?<uid>\d+)/mu.exec(status)?.[1];
|
||||
const state = /^State:\s+(?<state>\w)/mu.exec(status)?.[1];
|
||||
return owner !== undefined && Number(owner) === uid && state !== "Z";
|
||||
}
|
||||
|
||||
@@ -33,12 +33,16 @@ export async function killProfileProcesses(uid: number, attempt = 0): Promise<vo
|
||||
throw new Error(`profile UID ${uid} still owns processes after cleanup`);
|
||||
}
|
||||
const ids = await profileProcessIds(uid);
|
||||
if (ids.length === 0) return;
|
||||
if (ids.length === 0) {
|
||||
return;
|
||||
}
|
||||
for (const pid of ids) {
|
||||
try {
|
||||
process.kill(pid, "SIGKILL");
|
||||
} catch (error) {
|
||||
if (!isNotFound(error)) throw error;
|
||||
if (!isNotFound(error)) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
await sleep(10);
|
||||
|
||||
@@ -62,7 +62,9 @@ test("recognizes provider API exhaustion status", async () => {
|
||||
{ type: "codex", model: "gpt-5.6-terra" },
|
||||
],
|
||||
(profile) => {
|
||||
if (profile.type === "claude") throw new Error(message);
|
||||
if (profile.type === "claude") {
|
||||
throw new Error(message);
|
||||
}
|
||||
return Promise.resolve("fallback");
|
||||
},
|
||||
),
|
||||
@@ -84,7 +86,9 @@ test("falls through after a sign-in failure and records each try", async () => {
|
||||
if (profile.type === "codex") {
|
||||
throw new AgentAuthError("codex sign-in failed");
|
||||
}
|
||||
if (profile.type === "claude") throw new Error("API Error: 429");
|
||||
if (profile.type === "claude") {
|
||||
throw new Error("API Error: 429");
|
||||
}
|
||||
return Promise.resolve("fallback");
|
||||
},
|
||||
failures,
|
||||
@@ -116,7 +120,8 @@ test("throws the last sign-in failure when every profile fails", async () => {
|
||||
),
|
||||
).rejects.toThrow(
|
||||
expect.toSatisfy(
|
||||
(e: unknown) => e instanceof AgentAuthError && e.message.includes("claude sign-in failed"),
|
||||
(error: unknown) =>
|
||||
error instanceof AgentAuthError && error.message.includes("claude sign-in failed"),
|
||||
),
|
||||
);
|
||||
expect(failures.map(({ reason }) => reason)).toStrictEqual(["sign-in failed", "sign-in failed"]);
|
||||
@@ -157,20 +162,20 @@ test("does not mistake incidental quota text for exhaustion", async () => {
|
||||
});
|
||||
|
||||
test("exposes only the selected profile credential to the agent", () => {
|
||||
const base = {
|
||||
PATH: "/usr/bin",
|
||||
HOME: "/home/bot",
|
||||
UNRELATED_SECRET: "must-not-reach-agent",
|
||||
BOT_AGENTS_MASTER_KEY: "master",
|
||||
PROFILES: "contains every secret",
|
||||
REVIEWER_TOKEN: "reviewer",
|
||||
CLAUDE_CODE_OAUTH_TOKEN: "old-claude",
|
||||
ANTHROPIC_API_KEY: "old-anthropic-key",
|
||||
ANTHROPIC_AUTH_TOKEN: "old-anthropic-token",
|
||||
OPENAI_API_KEY: "old-openai",
|
||||
CODEX_API_KEY: "old-codex-key",
|
||||
CODEX_ACCESS_TOKEN: "old-codex-token",
|
||||
};
|
||||
const base = new Map([
|
||||
["PATH", "/usr/bin"],
|
||||
["HOME", "/home/bot"],
|
||||
["UNRELATED_SECRET", "must-not-reach-agent"],
|
||||
["BOT_AGENTS_MASTER_KEY", "master"],
|
||||
["PROFILES", "contains every secret"],
|
||||
["REVIEWER_TOKEN", "reviewer"],
|
||||
["CLAUDE_CODE_OAUTH_TOKEN", "old-claude"],
|
||||
["ANTHROPIC_API_KEY", "old-anthropic-key"],
|
||||
["ANTHROPIC_AUTH_TOKEN", "old-anthropic-token"],
|
||||
["OPENAI_API_KEY", "old-openai"],
|
||||
["CODEX_API_KEY", "old-codex-key"],
|
||||
["CODEX_ACCESS_TOKEN", "old-codex-token"],
|
||||
]);
|
||||
const profile = {
|
||||
index: 0,
|
||||
type: "claude" as const,
|
||||
@@ -181,15 +186,19 @@ test("exposes only the selected profile credential to the agent", () => {
|
||||
tokenFile: "/runtime/profile-0/token",
|
||||
};
|
||||
|
||||
expect(agentEnvironment(base, profile, "selected")).toStrictEqual({
|
||||
PATH: "/usr/bin",
|
||||
HOME: "/runtime/profile-0",
|
||||
CLAUDE_CODE_OAUTH_TOKEN: "selected",
|
||||
});
|
||||
expect(sanitizedEnvironment(base)).toStrictEqual({
|
||||
PATH: "/usr/bin",
|
||||
HOME: "/home/bot",
|
||||
});
|
||||
expect(agentEnvironment(base, profile, "selected")).toStrictEqual(
|
||||
new Map([
|
||||
["PATH", "/usr/bin"],
|
||||
["HOME", "/runtime/profile-0"],
|
||||
["CLAUDE_CODE_OAUTH_TOKEN", "selected"],
|
||||
]),
|
||||
);
|
||||
expect(sanitizedEnvironment(base)).toStrictEqual(
|
||||
new Map([
|
||||
["PATH", "/usr/bin"],
|
||||
["HOME", "/home/bot"],
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
test("uses a private Codex home without inventing an API credential", () => {
|
||||
@@ -203,11 +212,13 @@ test("uses a private Codex home without inventing an API credential", () => {
|
||||
tokenFile: null,
|
||||
};
|
||||
|
||||
expect(agentEnvironment({ PATH: "/bin" }, profile)).toStrictEqual({
|
||||
PATH: "/bin",
|
||||
HOME: "/runtime/profile-1",
|
||||
CODEX_HOME: "/runtime/profile-1/.codex",
|
||||
});
|
||||
expect(agentEnvironment(new Map([["PATH", "/bin"]]), profile)).toStrictEqual(
|
||||
new Map([
|
||||
["PATH", "/bin"],
|
||||
["HOME", "/runtime/profile-1"],
|
||||
["CODEX_HOME", "/runtime/profile-1/.codex"],
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
test("exposes an OpenRouter key only to the OpenCode profile", () => {
|
||||
@@ -221,9 +232,11 @@ test("exposes an OpenRouter key only to the OpenCode profile", () => {
|
||||
tokenFile: "/runtime/profile-2/token",
|
||||
};
|
||||
|
||||
expect(agentEnvironment({ PATH: "/bin" }, profile, "selected")).toStrictEqual({
|
||||
PATH: "/bin",
|
||||
HOME: "/runtime/profile-2",
|
||||
OPENROUTER_API_KEY: "selected",
|
||||
});
|
||||
expect(agentEnvironment(new Map([["PATH", "/bin"]]), profile, "selected")).toStrictEqual(
|
||||
new Map([
|
||||
["PATH", "/bin"],
|
||||
["HOME", "/runtime/profile-2"],
|
||||
["OPENROUTER_API_KEY", "selected"],
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
+26
-12
@@ -38,12 +38,16 @@ function isUsageExhausted(error: unknown): boolean {
|
||||
}
|
||||
|
||||
function failureReason(error: unknown): FailureReason {
|
||||
if (error instanceof AgentAuthError) return "sign-in failed";
|
||||
if (error instanceof AgentAuthError) {
|
||||
return "sign-in failed";
|
||||
}
|
||||
return isUsageExhausted(error) ? "usage exhausted" : "failed";
|
||||
}
|
||||
|
||||
export function failureFooter(failures: ProfileFailure[]): string {
|
||||
if (failures.length === 0) return "> Profile: none";
|
||||
if (failures.length === 0) {
|
||||
return "> Profile: none";
|
||||
}
|
||||
return `> Tried: ${failures
|
||||
.map(({ type, model, reason }) => `\`${type}\` · \`${model}\` (${reason})`)
|
||||
.join(", ")}`;
|
||||
@@ -66,7 +70,9 @@ async function attemptFrom<T, P extends ProfileIdentity>(
|
||||
} catch (error) {
|
||||
const reason = failureReason(error);
|
||||
failures.push({ type: profile.type, model: profile.model, reason });
|
||||
if (reason === "failed" || index === profiles.length - 1) throw error;
|
||||
if (reason === "failed" || index === profiles.length - 1) {
|
||||
throw error;
|
||||
}
|
||||
console.error(`${profile.type} ${reason}; trying next profile`);
|
||||
return attemptFrom(profiles, attempt, failures, index + 1);
|
||||
}
|
||||
@@ -80,8 +86,12 @@ export function runProfiles<T, P extends ProfileIdentity>(
|
||||
return attemptFrom(profiles, attempt, failures, 0);
|
||||
}
|
||||
|
||||
export function sanitizedEnvironment(environment: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
|
||||
const result: NodeJS.ProcessEnv = {};
|
||||
// Environments are maps from variable name to value, converted to the object
|
||||
// form only where a child process is spawned.
|
||||
export function sanitizedEnvironment(
|
||||
environment: ReadonlyMap<string, string>,
|
||||
): Map<string, string> {
|
||||
const result = new Map<string, string>();
|
||||
for (const name of [
|
||||
"PATH",
|
||||
"HOME",
|
||||
@@ -108,27 +118,31 @@ export function sanitizedEnvironment(environment: NodeJS.ProcessEnv): NodeJS.Pro
|
||||
"SSL_CERT_DIR",
|
||||
"NODE_EXTRA_CA_CERTS",
|
||||
]) {
|
||||
const value = environment[name];
|
||||
if (value !== undefined) result[name] = value;
|
||||
const value = environment.get(name);
|
||||
if (value !== undefined) {
|
||||
result.set(name, value);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export function agentEnvironment(
|
||||
environment: NodeJS.ProcessEnv,
|
||||
environment: ReadonlyMap<string, string>,
|
||||
profile: ProfileHome,
|
||||
token?: string,
|
||||
): NodeJS.ProcessEnv {
|
||||
): Map<string, string> {
|
||||
const result = sanitizedEnvironment(environment);
|
||||
result["HOME"] = profile.home;
|
||||
if (profile.type === "codex") result["CODEX_HOME"] = `${profile.home}/.codex`;
|
||||
result.set("HOME", profile.home);
|
||||
if (profile.type === "codex") {
|
||||
result.set("CODEX_HOME", `${profile.home}/.codex`);
|
||||
}
|
||||
if (token !== undefined && token !== "") {
|
||||
const tokenName = {
|
||||
claude: "CLAUDE_CODE_OAUTH_TOKEN",
|
||||
codex: "OPENAI_API_KEY",
|
||||
opencode: "OPENROUTER_API_KEY",
|
||||
}[profile.type];
|
||||
result[tokenName] = token;
|
||||
result.set(tokenName, token);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
+64
-49
@@ -24,7 +24,7 @@ import {
|
||||
retryInvalidToken,
|
||||
stageCodexAuth,
|
||||
} from "#agents";
|
||||
import { isObject } from "#common";
|
||||
import { field, isObject } from "#common";
|
||||
import { echo, exitCode, killProfileProcesses, readProfileFile } from "#processes";
|
||||
import {
|
||||
AgentAuthError,
|
||||
@@ -58,15 +58,17 @@ interface RenderedPrompt {
|
||||
|
||||
const env = (name: string): string => {
|
||||
const value = process.env[name];
|
||||
if (value === undefined) throw new Error(`${name} is not set`);
|
||||
if (value === undefined) {
|
||||
throw new Error(`${name} is not set`);
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
function definedEnvironment(): NodeJS.ProcessEnv {
|
||||
const result: NodeJS.ProcessEnv = {};
|
||||
function definedEnvironment(): Map<string, string> {
|
||||
const result = new Map<string, string>();
|
||||
for (const [name, value] of Object.entries(process.env)) {
|
||||
if (value !== undefined) {
|
||||
result[name] = value;
|
||||
result.set(name, value);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
@@ -104,17 +106,24 @@ async function postAuthorComment(body: string): Promise<void> {
|
||||
}
|
||||
|
||||
async function runRoot(command: string, args: string[]): Promise<void> {
|
||||
const code = await exitCode(spawn(command, args, { env: PROCESS_ENV, stdio: "ignore" }));
|
||||
if (code !== 0) throw new Error(`${command} ${args[0]} exited with ${code}`);
|
||||
const code = await exitCode(
|
||||
spawn(command, args, { env: Object.fromEntries(PROCESS_ENV), stdio: "ignore" }),
|
||||
);
|
||||
if (code !== 0) {
|
||||
throw new Error(`${command} ${args[0]} exited with ${code}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function configureGitAuthor(): Promise<void> {
|
||||
const user: unknown = await (await gitea("user")).json();
|
||||
if (!isObject(user) || typeof user["login"] !== "string" || typeof user["email"] !== "string") {
|
||||
throw new Error("user: unexpected Gitea response");
|
||||
const userResponse = await gitea("user");
|
||||
const user: unknown = await userResponse.json();
|
||||
const login = field(user, "login");
|
||||
const email = field(user, "email");
|
||||
if (typeof login !== "string" || typeof email !== "string") {
|
||||
throw new TypeError("user: unexpected Gitea response");
|
||||
}
|
||||
await runRoot("git", ["config", "user.name", user["login"]]);
|
||||
await runRoot("git", ["config", "user.email", user["email"]]);
|
||||
await runRoot("git", ["config", "user.name", login]);
|
||||
await runRoot("git", ["config", "user.email", email]);
|
||||
}
|
||||
|
||||
async function readProfileToken(profile: RuntimeProfile): Promise<string | null> {
|
||||
@@ -130,13 +139,15 @@ async function runAgentCommand(
|
||||
): Promise<void> {
|
||||
const child = spawn(command, args, {
|
||||
cwd: WORKSPACE,
|
||||
env: agentEnvironment(PROCESS_ENV, profile, token),
|
||||
env: Object.fromEntries(agentEnvironment(PROCESS_ENV, profile, token)),
|
||||
uid: profile.uid,
|
||||
gid: profile.gid,
|
||||
stdio: "ignore",
|
||||
});
|
||||
const code = await exitCode(child);
|
||||
if (code !== 0) throw new Error(`${command} ${args[0]} exited with ${code}`);
|
||||
if (code !== 0) {
|
||||
throw new Error(`${command} ${args[0]} exited with ${code}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function prepareProfile(profile: RuntimeProfile): Promise<void> {
|
||||
@@ -159,36 +170,36 @@ async function prepareProfile(profile: RuntimeProfile): Promise<void> {
|
||||
|
||||
// One issue comment as the prompt quotes it.
|
||||
function formatComment(comment: unknown): string {
|
||||
const { user, created_at: createdAt, body } = isObject(comment) ? comment : {};
|
||||
const login = isObject(user) ? user["login"] : undefined;
|
||||
const login = field(field(comment, "user"), "login");
|
||||
const createdAt = field(comment, "created_at");
|
||||
const body = field(comment, "body");
|
||||
return `## ${String(login)} at ${String(createdAt)}\n\n${String(body)}\n`;
|
||||
}
|
||||
|
||||
async function renderPrompt(profile: RuntimeProfile): Promise<RenderedPrompt> {
|
||||
const reviewPath = await makeProfileFile(profile, "review.md");
|
||||
const anchorsPath = await makeProfileFile(profile, "anchors.json");
|
||||
const comments: unknown = await (
|
||||
await gitea(`repos/${REPO}/issues/${INDEX}/comments?limit=100`)
|
||||
).json();
|
||||
const commentsResponse = await gitea(`repos/${REPO}/issues/${INDEX}/comments?limit=100`);
|
||||
const comments: unknown = await commentsResponse.json();
|
||||
if (!Array.isArray(comments)) {
|
||||
throw new TypeError("comments: unexpected Gitea response");
|
||||
}
|
||||
const values: NodeJS.ProcessEnv = {
|
||||
COMMENT: env("COMMENT"),
|
||||
ISSUE_COMMENTS: comments.map((comment: unknown) => formatComment(comment)).join("\n"),
|
||||
CODE_RULES: await (await gitea(RULES_PATH)).text(),
|
||||
EVENT_NAME: EVENT,
|
||||
GITEA_API_URL: API,
|
||||
GITEA_REPOSITORY: REPO,
|
||||
ISSUE_INDEX: INDEX,
|
||||
REVIEW_PATH: reviewPath,
|
||||
ANCHORS_PATH: anchorsPath,
|
||||
};
|
||||
const values = new Map<string, string>([
|
||||
["COMMENT", env("COMMENT")],
|
||||
["ISSUE_COMMENTS", comments.map((comment: unknown) => formatComment(comment)).join("\n")],
|
||||
["CODE_RULES", await gitea(RULES_PATH).then((response) => response.text())],
|
||||
["EVENT_NAME", EVENT],
|
||||
["GITEA_API_URL", API],
|
||||
["GITEA_REPOSITORY", REPO],
|
||||
["ISSUE_INDEX", INDEX],
|
||||
["REVIEW_PATH", reviewPath],
|
||||
["ANCHORS_PATH", anchorsPath],
|
||||
]);
|
||||
const template = await readFile(new URL("../../prompt.md", import.meta.url), "utf8");
|
||||
return {
|
||||
prompt: template.replaceAll(
|
||||
/\$\{(\w+)\}/gu,
|
||||
(match: string, name: string) => values[name] ?? match,
|
||||
/\$\{(?<name>\w+)\}/gu,
|
||||
(match: string, name: string) => values.get(name) ?? match,
|
||||
),
|
||||
reviewPath,
|
||||
anchorsPath,
|
||||
@@ -210,10 +221,7 @@ async function runClaude(profile: RuntimeProfile, token: string, prompt: string)
|
||||
],
|
||||
{
|
||||
cwd: WORKSPACE,
|
||||
env: {
|
||||
...agentEnvironment(PROCESS_ENV, profile, token),
|
||||
IS_SANDBOX: "1",
|
||||
},
|
||||
env: Object.fromEntries(agentEnvironment(PROCESS_ENV, profile, token).set("IS_SANDBOX", "1")),
|
||||
uid: profile.uid,
|
||||
gid: profile.gid,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
@@ -234,17 +242,20 @@ async function runClaude(profile: RuntimeProfile, token: string, prompt: string)
|
||||
if (line !== "") {
|
||||
const event: unknown = JSON.parse(line);
|
||||
if (isObject(event)) {
|
||||
const content = isObject(event["message"]) ? event["message"]["content"] : undefined;
|
||||
const content = field(field(event, "message"), "content");
|
||||
for (const part of Array.isArray(content) ? content : []) {
|
||||
if (isObject(part)) {
|
||||
const text = part["thinking"] ?? part["text"] ?? part["name"];
|
||||
const text = field(part, "thinking") ?? field(part, "text") ?? field(part, "name");
|
||||
if (typeof text === "string" && text !== "") {
|
||||
console.log(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (event["type"] === "result") {
|
||||
const { is_error: isError, result: answer, subtype, api_error_status: apiStatus } = event;
|
||||
if (field(event, "type") === "result") {
|
||||
const isError = field(event, "is_error");
|
||||
const answer = field(event, "result");
|
||||
const subtype = field(event, "subtype");
|
||||
const apiStatus = field(event, "api_error_status");
|
||||
result = {
|
||||
isError: typeof isError === "boolean" ? isError : undefined,
|
||||
result: typeof answer === "string" ? answer : undefined,
|
||||
@@ -289,11 +300,11 @@ async function pollForInstructions(
|
||||
|
||||
async function codexDeviceLogin(
|
||||
profile: RuntimeProfile,
|
||||
environment: NodeJS.ProcessEnv,
|
||||
environment: ReadonlyMap<string, string>,
|
||||
): Promise<void> {
|
||||
const login = spawn("codex", ["login", "--device-auth"], {
|
||||
cwd: WORKSPACE,
|
||||
env: environment,
|
||||
env: Object.fromEntries(environment),
|
||||
uid: profile.uid,
|
||||
gid: profile.gid,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
@@ -318,12 +329,12 @@ async function codexDeviceLogin(
|
||||
|
||||
async function codexApiLogin(
|
||||
profile: RuntimeProfile,
|
||||
environment: NodeJS.ProcessEnv,
|
||||
environment: ReadonlyMap<string, string>,
|
||||
token: string,
|
||||
): Promise<void> {
|
||||
const login = spawn("codex", ["login", "--with-api-key"], {
|
||||
cwd: WORKSPACE,
|
||||
env: environment,
|
||||
env: Object.fromEntries(environment),
|
||||
uid: profile.uid,
|
||||
gid: profile.gid,
|
||||
stdio: ["pipe", "inherit", "pipe"],
|
||||
@@ -357,12 +368,14 @@ async function runCodex(
|
||||
} else {
|
||||
const check = spawn("codex", ["login", "status"], {
|
||||
cwd: WORKSPACE,
|
||||
env: environment,
|
||||
env: Object.fromEntries(environment),
|
||||
uid: profile.uid,
|
||||
gid: profile.gid,
|
||||
stdio: "ignore",
|
||||
});
|
||||
if ((await exitCode(check)) !== 0) await codexDeviceLogin(profile, environment);
|
||||
if ((await exitCode(check)) !== 0) {
|
||||
await codexDeviceLogin(profile, environment);
|
||||
}
|
||||
}
|
||||
const outputPath = await makeProfileFile(profile, "result.md");
|
||||
const attempt = async (): Promise<CommandResult> => {
|
||||
@@ -379,7 +392,7 @@ async function runCodex(
|
||||
],
|
||||
{
|
||||
cwd: WORKSPACE,
|
||||
env: environment,
|
||||
env: Object.fromEntries(environment),
|
||||
uid: profile.uid,
|
||||
gid: profile.gid,
|
||||
stdio: ["ignore", "inherit", "pipe"],
|
||||
@@ -420,7 +433,7 @@ async function runOpenCode(
|
||||
): Promise<string> {
|
||||
const child = spawn("opencode", openCodeArgs(profile.model, prompt), {
|
||||
cwd: WORKSPACE,
|
||||
env: agentEnvironment(PROCESS_ENV, profile, token),
|
||||
env: Object.fromEntries(agentEnvironment(PROCESS_ENV, profile, token)),
|
||||
uid: profile.uid,
|
||||
gid: profile.gid,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
@@ -473,7 +486,9 @@ async function runProfile(profile: RuntimeProfile): Promise<AttemptResult> {
|
||||
await persistCodexAuth(stagedCodexAuth, CODEX_AUTH);
|
||||
}
|
||||
}
|
||||
if (resultPath !== null) body = await readProfileFile(profile, resultPath);
|
||||
if (resultPath !== null) {
|
||||
body = await readProfileFile(profile, resultPath);
|
||||
}
|
||||
if (body === null || reviewPath === null || anchorsPath === null) {
|
||||
throw new Error("agent attempt produced no result");
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { decryptCredential, encryptCredential, parseCredentials } from "./creden
|
||||
|
||||
const MASTER_KEY = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8";
|
||||
const OTHER_KEY = "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE";
|
||||
const NONCE = Uint8Array.from({ length: 12 }, (_, index) => index + 32);
|
||||
const NONCE = Uint8Array.from({ length: 12 }, (_value, index) => index + 32);
|
||||
const ENCRYPTED = "v1:ICEiIyQlJicoKSor:oV_FAgnsN3p1FyegJxwGVdyjfyPio6qDeSUwbA";
|
||||
|
||||
test("decrypts a known AES-256-GCM credential", async () => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { webcrypto } from "node:crypto";
|
||||
|
||||
import { isMapping } from "#common";
|
||||
import { field, isMapping } from "#common";
|
||||
import type { BotProfile } from "#profiles";
|
||||
|
||||
interface DefaultModels {
|
||||
@@ -47,7 +47,9 @@ export async function decryptCredential(encrypted: string, masterKey: string): P
|
||||
throw new Error("credential must use v1:<nonce>:<ciphertext>");
|
||||
}
|
||||
const nonce = decodeBase64Url(encodedNonce);
|
||||
if (nonce.length !== 12) throw new Error("credential nonce must be 12 bytes");
|
||||
if (nonce.length !== 12) {
|
||||
throw new Error("credential nonce must be 12 bytes");
|
||||
}
|
||||
const plaintext = await webcrypto.subtle.decrypt(
|
||||
{ name: "AES-GCM", iv: buffer(nonce) },
|
||||
await importMasterKey(masterKey, "decrypt"),
|
||||
@@ -61,7 +63,9 @@ export async function encryptCredential(
|
||||
masterKey: string,
|
||||
nonce: Uint8Array = webcrypto.getRandomValues(new Uint8Array(12)),
|
||||
): Promise<string> {
|
||||
if (nonce.length !== 12) throw new Error("AES-GCM nonce must be 12 bytes");
|
||||
if (nonce.length !== 12) {
|
||||
throw new Error("AES-GCM nonce must be 12 bytes");
|
||||
}
|
||||
const ciphertext = await webcrypto.subtle.encrypt(
|
||||
{ name: "AES-GCM", iv: buffer(nonce) },
|
||||
await importMasterKey(masterKey, "encrypt"),
|
||||
@@ -80,7 +84,9 @@ async function parseProfile(entry: unknown, index: number, masterKey: string): P
|
||||
throw new Error(`${at}.${key}: unknown field`);
|
||||
}
|
||||
}
|
||||
const { type, model, token } = entry;
|
||||
const type = field(entry, "type");
|
||||
const model = field(entry, "model");
|
||||
const token = field(entry, "token");
|
||||
if (type !== "claude" && type !== "codex" && type !== "opencode") {
|
||||
throw new Error(`${at}.type: expected "claude", "codex", or "opencode"`);
|
||||
}
|
||||
@@ -112,7 +118,8 @@ export async function parseCredentials(text: string, masterKey: string): Promise
|
||||
throw new Error(`${key}: unknown field`);
|
||||
}
|
||||
}
|
||||
const { "reviewer-token": reviewerToken, profiles } = value;
|
||||
const reviewerToken = field(value, "reviewer-token");
|
||||
const profiles = field(value, "profiles");
|
||||
if (typeof reviewerToken !== "string") {
|
||||
throw new TypeError("reviewer-token: expected an encrypted credential");
|
||||
}
|
||||
|
||||
@@ -49,7 +49,8 @@ test.skipIf(!IS_ROOT)("prepares root-only reviewer and isolated profile credenti
|
||||
assert(claude.tokenFile !== null);
|
||||
expect(await readFile(claude.tokenFile, "utf8")).toBe("secret-token");
|
||||
await expectPrivate(claude.tokenFile, claude.uid, 0o600);
|
||||
expect((await stat(codex.home)).uid).toStrictEqual(codex.uid);
|
||||
const codexHome = await stat(codex.home);
|
||||
expect(codexHome.uid).toStrictEqual(codex.uid);
|
||||
expect(codex.tokenFile).toBe(null);
|
||||
|
||||
const ownToken = spawnSync("/bin/cat", [claude.tokenFile], {
|
||||
@@ -83,12 +84,12 @@ test.skipIf(!IS_ROOT)("removes prepared credentials when output publication fail
|
||||
process.execPath,
|
||||
[fileURLToPath(new URL("../prepare/index.ts", import.meta.url))],
|
||||
{
|
||||
env: {
|
||||
ACTION_PATH: action,
|
||||
RUNNER_TEMP: root,
|
||||
BOT_AGENTS_MASTER_KEY: MASTER_KEY,
|
||||
GITHUB_OUTPUT: action,
|
||||
},
|
||||
env: Object.fromEntries([
|
||||
["ACTION_PATH", action],
|
||||
["RUNNER_TEMP", root],
|
||||
["BOT_AGENTS_MASTER_KEY", MASTER_KEY],
|
||||
["GITHUB_OUTPUT", action],
|
||||
]),
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { chmod, chown, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { isMapping } from "#common";
|
||||
import { field, isMapping } from "#common";
|
||||
import type { ProfileFailure, ProfileIdentity } from "#profiles";
|
||||
|
||||
import { parseCredentials } from "./credentials.ts";
|
||||
@@ -107,11 +107,11 @@ export async function prepareRuntime(
|
||||
function isRuntimeManifest(value: unknown): value is RuntimeManifest {
|
||||
return (
|
||||
isMapping(value) &&
|
||||
typeof value["runtimeDir"] === "string" &&
|
||||
typeof value["manifestPath"] === "string" &&
|
||||
typeof value["reviewerFile"] === "string" &&
|
||||
typeof value["outcomeFile"] === "string" &&
|
||||
Array.isArray(value["profiles"])
|
||||
typeof field(value, "runtimeDir") === "string" &&
|
||||
typeof field(value, "manifestPath") === "string" &&
|
||||
typeof field(value, "reviewerFile") === "string" &&
|
||||
typeof field(value, "outcomeFile") === "string" &&
|
||||
Array.isArray(field(value, "profiles"))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
+18
-4
@@ -1,5 +1,5 @@
|
||||
// Test support: the current user's IDs, a fresh temporary directory, and a
|
||||
// plain command runner. It imports no source module, so the repository-level
|
||||
// Test support: the current user's IDs, a fresh temporary directory, the
|
||||
// inherited environment, and a plain command runner. It imports no source module, so the repository-level
|
||||
// tests in tests/, which reach src/ only through `#testing`, never touch
|
||||
// internals.
|
||||
import { spawn } from "node:child_process";
|
||||
@@ -33,14 +33,28 @@ export interface CommandResult {
|
||||
stderr: string;
|
||||
}
|
||||
|
||||
// This process's environment as a map from variable name to value.
|
||||
export function inheritedEnvironment(): Map<string, string> {
|
||||
const result = new Map<string, string>();
|
||||
for (const [name, value] of Object.entries(process.env)) {
|
||||
if (value !== undefined) {
|
||||
result.set(name, value);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Runs a command with stdin closed and only the given environment, and
|
||||
// collects its stderr.
|
||||
export async function runCommand(
|
||||
command: string,
|
||||
args: string[],
|
||||
env: NodeJS.ProcessEnv,
|
||||
env: ReadonlyMap<string, string>,
|
||||
): Promise<CommandResult> {
|
||||
const child = spawn(command, args, { env, stdio: ["ignore", "ignore", "pipe"] });
|
||||
const child = spawn(command, args, {
|
||||
env: Object.fromEntries(env),
|
||||
stdio: ["ignore", "ignore", "pipe"],
|
||||
});
|
||||
const [stderr, closed] = await Promise.all([text(child.stderr), once(child, "close")]);
|
||||
const exitCode: unknown = closed[0];
|
||||
return { success: exitCode === 0, stderr };
|
||||
|
||||
@@ -15,7 +15,11 @@ const PHASES: [phase: string, firstVariable: string][] = [
|
||||
|
||||
test.each(PHASES)("Node loads the %s phase", async (phase, firstVariable) => {
|
||||
const path = fileURLToPath(new URL(`../src/${phase}/index.ts`, import.meta.url));
|
||||
const result = await runCommand(process.execPath, [path], { PATH: process.env["PATH"] ?? "" });
|
||||
const result = await runCommand(
|
||||
process.execPath,
|
||||
[path],
|
||||
new Map([["PATH", process.env["PATH"] ?? ""]]),
|
||||
);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.stderr).toContain(`${firstVariable} is not set`);
|
||||
});
|
||||
|
||||
+51
-45
@@ -3,24 +3,22 @@ import process from "node:process";
|
||||
import { assert, expect, test } from "vite-plus/test";
|
||||
import { parse } from "yaml";
|
||||
|
||||
import { makeTempDir, runCommand } from "#testing";
|
||||
import { inheritedEnvironment, makeTempDir, runCommand } from "#testing";
|
||||
|
||||
type JsonObject = NodeJS.Dict<unknown>;
|
||||
// A parsed YAML or JSON mapping, keyed by its dynamic field names.
|
||||
type Mapping = Map<string, unknown>;
|
||||
|
||||
const isMapping = (value: unknown): value is JsonObject =>
|
||||
typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
|
||||
const mapping = (value: unknown, at: string): JsonObject => {
|
||||
assert(isMapping(value), at);
|
||||
return value;
|
||||
const mapping = (value: unknown, at: string): Mapping => {
|
||||
assert(typeof value === "object" && value !== null && !Array.isArray(value), at);
|
||||
return new Map(Object.entries(value));
|
||||
};
|
||||
|
||||
const sequence = (value: unknown, at: string): JsonObject[] => {
|
||||
const sequence = (value: unknown, at: string): Mapping[] => {
|
||||
assert(Array.isArray(value), at);
|
||||
return value.map((entry: unknown, index) => mapping(entry, `${at}[${index}]`));
|
||||
};
|
||||
|
||||
async function yaml(path: string): Promise<JsonObject> {
|
||||
async function yaml(path: string): Promise<Mapping> {
|
||||
const value: unknown = parse(await readFile(path, "utf8"));
|
||||
return mapping(value, path);
|
||||
}
|
||||
@@ -33,8 +31,8 @@ async function yamlFiles(): Promise<string[]> {
|
||||
.filter((path) => !/^(?:\.git|\.superpowers|node_modules)\//u.test(path));
|
||||
}
|
||||
|
||||
const findStep = (steps: JsonObject[], key: string, value: string): JsonObject => {
|
||||
const step = steps.find((entry) => entry[key] === value);
|
||||
const findStep = (steps: Mapping[], key: string, value: string): Mapping => {
|
||||
const step = steps.find((entry) => entry.get(key) === value);
|
||||
assert(step !== undefined, `${key}: ${value}`);
|
||||
return step;
|
||||
};
|
||||
@@ -47,11 +45,12 @@ test("all committed YAML parses", async () => {
|
||||
|
||||
test("the action exposes only the master key and scopes it to preparation", async () => {
|
||||
const action = await yaml("action.yml");
|
||||
expect(Object.keys(mapping(action["inputs"], "inputs"))).toStrictEqual(["master-key"]);
|
||||
const steps = sequence(mapping(action["runs"], "runs")["steps"], "runs.steps");
|
||||
expect([...mapping(action.get("inputs"), "inputs").keys()]).toStrictEqual(["master-key"]);
|
||||
const steps = sequence(mapping(action.get("runs"), "runs").get("steps"), "runs.steps");
|
||||
for (const step of steps) {
|
||||
if (typeof step["run"] === "string") {
|
||||
expect(step["run"].includes("\n"), String(step["name"])).toBe(false);
|
||||
const run = step.get("run");
|
||||
if (typeof run === "string") {
|
||||
expect(run.includes("\n"), String(step.get("name"))).toBe(false);
|
||||
}
|
||||
}
|
||||
const checkout = findStep(steps, "uses", "actions/checkout@v4");
|
||||
@@ -59,16 +58,16 @@ test("the action exposes only the master key and scopes it to preparation", asyn
|
||||
const agent = findStep(steps, "id", "agent");
|
||||
const post = findStep(steps, "id", "post");
|
||||
findStep(steps, "id", "source");
|
||||
expect(mapping(checkout["with"], "checkout.with")["token"]).toBe("${{ gitea.token }}");
|
||||
expect(mapping(prepare["env"], "prepare.env")["BOT_AGENTS_MASTER_KEY"]).toBe(
|
||||
expect(mapping(checkout.get("with"), "checkout.with").get("token")).toBe("${{ gitea.token }}");
|
||||
expect(mapping(prepare.get("env"), "prepare.env").get("BOT_AGENTS_MASTER_KEY")).toBe(
|
||||
"${{ inputs.master-key }}",
|
||||
);
|
||||
expect(JSON.stringify(agent).includes("BOT_AGENTS_MASTER_KEY")).toBe(false);
|
||||
expect(JSON.stringify(post).includes("BOT_AGENTS_MASTER_KEY")).toBe(false);
|
||||
expect(mapping(agent["env"], "agent.env")["GITEA_TOKEN"]).toBe("${{ gitea.token }}");
|
||||
assert(String(post["if"]).includes("always()"));
|
||||
expect(JSON.stringify(Object.fromEntries(agent)).includes("BOT_AGENTS_MASTER_KEY")).toBe(false);
|
||||
expect(JSON.stringify(Object.fromEntries(post)).includes("BOT_AGENTS_MASTER_KEY")).toBe(false);
|
||||
expect(mapping(agent.get("env"), "agent.env").get("GITEA_TOKEN")).toBe("${{ gitea.token }}");
|
||||
assert(String(post.get("if")).includes("always()"));
|
||||
for (const step of [prepare, agent, post]) {
|
||||
assert(String(step["run"]).includes("steps.source.outputs.path"));
|
||||
assert(String(step.get("run")).includes("steps.source.outputs.path"));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -76,15 +75,15 @@ test("the source protection script stages immutable action files", async () => {
|
||||
const temp = await makeTempDir();
|
||||
const outputFile = `${temp}/output`;
|
||||
try {
|
||||
const result = await runCommand("/bin/bash", ["scripts/protect-source.sh"], {
|
||||
...process.env,
|
||||
ACTION_PATH: process.cwd(),
|
||||
GITHUB_OUTPUT: outputFile,
|
||||
RUNNER_TEMP: temp,
|
||||
});
|
||||
const environment = inheritedEnvironment();
|
||||
environment.set("ACTION_PATH", process.cwd());
|
||||
environment.set("GITHUB_OUTPUT", outputFile);
|
||||
environment.set("RUNNER_TEMP", temp);
|
||||
const result = await runCommand("/bin/bash", ["scripts/protect-source.sh"], environment);
|
||||
assert(result.success, result.stderr);
|
||||
|
||||
const path = (await readFile(outputFile, "utf8")).trim().slice(5);
|
||||
const output = await readFile(outputFile, "utf8");
|
||||
const path = output.trim().slice(5);
|
||||
const files = [
|
||||
"src/prepare/index.ts",
|
||||
"src/run/index.ts",
|
||||
@@ -108,7 +107,7 @@ test("the source protection script stages immutable action files", async () => {
|
||||
test("module-relative runtime assets exist", async () => {
|
||||
const module = new URL("../src/run/index.ts", import.meta.url);
|
||||
const source = await readFile(module, "utf8");
|
||||
const assets = [...source.matchAll(/new URL\("([^"]+)", import\.meta\.url\)/gu)].map(
|
||||
const assets = [...source.matchAll(/new URL\("(?<asset>[^"]+)", import\.meta\.url\)/gu)].map(
|
||||
([, asset = ""]) => new URL(asset, module),
|
||||
);
|
||||
const stats = await Promise.all(assets.map((asset) => stat(asset)));
|
||||
@@ -119,27 +118,30 @@ test("module-relative runtime assets exist", async () => {
|
||||
|
||||
test("the repository uses its trusted main action", async () => {
|
||||
const workflow = await yaml(".gitea/workflows/agents.yml");
|
||||
const job = mapping(mapping(workflow["jobs"], "jobs")["agent"], "jobs.agent");
|
||||
const permissions = mapping(job["permissions"], "jobs.agent.permissions");
|
||||
expect(permissions).toStrictEqual({
|
||||
const job = mapping(mapping(workflow.get("jobs"), "jobs").get("agent"), "jobs.agent");
|
||||
const permissions = mapping(job.get("permissions"), "jobs.agent.permissions");
|
||||
expect(Object.fromEntries(permissions)).toStrictEqual({
|
||||
code: "write",
|
||||
issues: "write",
|
||||
"pull-requests": "write",
|
||||
});
|
||||
const steps = sequence(job["steps"], "jobs.agent.steps");
|
||||
expect(steps.findIndex((step) => step["uses"] === "./")).toStrictEqual(-1);
|
||||
const steps = sequence(job.get("steps"), "jobs.agent.steps");
|
||||
expect(steps.findIndex((step) => step.get("uses") === "./")).toStrictEqual(-1);
|
||||
const trusted = findStep(steps, "uses", "https://git.capsulizers.com/commons/bot-agents@main");
|
||||
expect(mapping(trusted["with"], "trusted.with")["master-key"]).toBe(
|
||||
expect(mapping(trusted.get("with"), "trusted.with").get("master-key")).toBe(
|
||||
"${{ secrets.BOT_AGENTS_MASTER_KEY }}",
|
||||
);
|
||||
expect(JSON.stringify(workflow).includes("gitea.actor != 'bot'")).toBe(true);
|
||||
expect(JSON.stringify(Object.fromEntries(workflow)).includes("gitea.actor != 'bot'")).toBe(true);
|
||||
});
|
||||
|
||||
test("CI checks every phase and the root isolation suite", async () => {
|
||||
const workflow = await yaml(".gitea/workflows/check.yml");
|
||||
const job = mapping(mapping(workflow["jobs"], "jobs")["check"], "jobs.check");
|
||||
const commands = sequence(job["steps"], "jobs.check.steps")
|
||||
.map((step) => (typeof step["run"] === "string" ? step["run"] : ""))
|
||||
const job = mapping(mapping(workflow.get("jobs"), "jobs").get("check"), "jobs.check");
|
||||
const commands = sequence(job.get("steps"), "jobs.check.steps")
|
||||
.map((step) => {
|
||||
const run = step.get("run");
|
||||
return typeof run === "string" ? run : "";
|
||||
})
|
||||
.join("\n");
|
||||
// `vp check` type-checks every phase, and `vp test` runs each of them under
|
||||
// Node as well as the root-only isolation tests.
|
||||
@@ -176,8 +178,12 @@ test("profile preparation does not receive provider credentials", async () => {
|
||||
|
||||
test("central profiles fall back from Codex to Claude to OpenCode", async () => {
|
||||
const credentials: unknown = JSON.parse(await readFile("profiles.json", "utf8"));
|
||||
const profiles = sequence(mapping(credentials, "profiles.json")["profiles"], "profiles");
|
||||
expect(profiles.map((profile) => profile["type"])).toStrictEqual(["codex", "claude", "opencode"]);
|
||||
expect(profiles[2]?.["model"]).toBe("openrouter/qwen/qwen3.8-27b");
|
||||
assert(String(profiles[2]?.["token"]).startsWith("v1:"));
|
||||
const profiles = sequence(mapping(credentials, "profiles.json").get("profiles"), "profiles");
|
||||
expect(profiles.map((profile) => profile.get("type"))).toStrictEqual([
|
||||
"codex",
|
||||
"claude",
|
||||
"opencode",
|
||||
]);
|
||||
expect(profiles[2]?.get("model")).toBe("openrouter/qwen/qwen3.8-27b");
|
||||
assert(String(profiles[2]?.get("token")).startsWith("v1:"));
|
||||
});
|
||||
|
||||
+12
-5
@@ -2,7 +2,7 @@ import explicitTypes from "eslint-plugin-explicit-types";
|
||||
import importStructure from "eslint-plugin-import-structure";
|
||||
import { defineConfig } from "vite-plus";
|
||||
|
||||
import projectLint from "./scripts/project-lint.ts";
|
||||
import projectLint, { projectOverrides } from "./scripts/project-lint.ts";
|
||||
|
||||
export default defineConfig({
|
||||
lint: {
|
||||
@@ -12,13 +12,20 @@ export default defineConfig({
|
||||
"eslint-plugin-import-structure",
|
||||
"./scripts/project-lint.ts",
|
||||
],
|
||||
categories: { correctness: "error", suspicious: "error", pedantic: "error", perf: "error" },
|
||||
categories: {
|
||||
correctness: "error",
|
||||
suspicious: "error",
|
||||
pedantic: "error",
|
||||
perf: "error",
|
||||
style: "error",
|
||||
restriction: "error",
|
||||
},
|
||||
rules: {
|
||||
...explicitTypes.configs.concrete.rules,
|
||||
...explicitTypes.configs.absence.rules,
|
||||
...importStructure.configs.modules.rules,
|
||||
...explicitTypes.configs.all.rules,
|
||||
...importStructure.configs.all.rules,
|
||||
...projectLint.configs.project.rules,
|
||||
},
|
||||
overrides: projectOverrides,
|
||||
options: {
|
||||
typeAware: true,
|
||||
typeCheck: true,
|
||||
|
||||
Reference in New Issue
Block a user