~ruther/agent-environments

ref: ae8afa8070c6828d41db8b68dc28b190f54a27f8 agent-environments/checks/default.nix -rw-r--r-- 10.5 KiB
ae8afa80 — František Boháček Add behaviour checks with stub agents 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
{ pkgs, self }:
let
  inherit (pkgs) lib;
  inherit (self.lib) mkEnvironment mkSkill mkSkillModule;

  # Stub agents record what the launcher handed them, so the checks can assert on
  # the contract between launcher and agent without needing a login or network.
  stub =
    name:
    pkgs.writeShellScriptBin name ''
      log="$STUB_LOG"
      : >"$log.args"
      if [ $# -gt 0 ]; then printf '%s\n' "$@" >"$log.args"; fi
      {
        echo "CLAUDE_CONFIG_DIR=''${CLAUDE_CONFIG_DIR:-}"
        echo "CODEX_HOME=''${CODEX_HOME:-}"
        if command -v hello >/dev/null; then echo "HELLO=yes"; else echo "HELLO=no"; fi
      } >"$log.env"
    '';
  stubs = {
    claude.package = stub "claude";
    codex.package = stub "codex";
  };

  skillSrc =
    name:
    pkgs.writeTextDir "SKILL.md" ''
      ---
      name: ${name}
      description: test skill ${name}
      ---
    '';
  # `leaf` needs the `hello` tool; `top` depends on `leaf`.
  leaf = mkSkill {
    inherit pkgs;
    name = "leaf";
    src = skillSrc "leaf";
    runtimeInputs = [ pkgs.hello ];
  };
  top = mkSkill {
    inherit pkgs;
    name = "top";
    src = skillSrc "top";
    skills = [ leaf ];
  };

  full = mkEnvironment {
    inherit pkgs;
    modules = [
      stubs
      (mkSkillModule top)
      {
        claude.enable = true;
        codex.enable = true;
        modules.skills.top.enable = true;
        # Names sort opposite to their order, so the check proves `order` wins.
        instructions.a-second = {
          text = "SECOND";
          order = 20;
        };
        instructions.z-first = {
          text = "FIRST";
          order = 10;
        };
        claude.settings.model = "opus";
        codex.settings.model = "gpt-5";
        launchers = {
          claude-work.agent = "claude";
          claude-pers.agent = "claude";
          codex-work.agent = "codex";
          # Same state dir as claude-work, used to test switching managed content.
          claude-work-bare = {
            agent = "claude";
            id = "claude-work";
          };
        };
      }
    ];
  };

  # Same launcher id as full's claude-work, but no skills, instructions or settings.
  bare = mkEnvironment {
    inherit pkgs;
    modules = [
      stubs
      {
        claude.enable = true;
        codex.enable = true;
        launchers.claude-work.agent = "claude";
        launchers.codex-bare.agent = "codex";
      }
    ];
  };

  fails =
    modules: !(builtins.tryEval (mkEnvironment { inherit pkgs modules; }).package.drvPath).success;

  otherLeaf = mkSkill {
    inherit pkgs;
    name = "leaf";
    src = skillSrc "leaf-other";
  };

  runTest =
    name: script:
    pkgs.runCommand "agent-environments-${name}" { } ''
      set -euo pipefail
      export HOME=$TMPDIR/home
      mkdir -p $HOME
      export STUB_LOG=$TMPDIR/stub
      fail() { echo "FAIL: $*" >&2; exit 1; }
      ${script}
      touch $out
    '';
in
{
  launch-contract = runTest "launch-contract" ''
    ${full.launchers.claude-work}/bin/claude-work --resume x
    s=$HOME/.local/agent-environments/claude-work
    grep -qxF "CLAUDE_CONFIG_DIR=$s" $STUB_LOG.env || fail "CLAUDE_CONFIG_DIR not set to state dir"
    grep -qxF "CODEX_HOME=" $STUB_LOG.env || fail "claude launcher leaked CODEX_HOME"

    # Instructions: order field wins over names, fragments separated by a blank line.
    [ "$(cat $s/CLAUDE.md)" = "$(printf 'FIRST\n\nSECOND')" ] || fail "CLAUDE.md content: $(cat $s/CLAUDE.md)"

    # Skills: dependency closure linked, and the dependency's tool is on PATH.
    [ -f $s/skills/top/SKILL.md ] || fail "top skill missing"
    [ -f $s/skills/leaf/SKILL.md ] || fail "dependency skill leaf missing"
    grep -qxF "HELLO=yes" $STUB_LOG.env || fail "skill runtime input not on PATH"

    # Settings layered via --settings, user args passed through after it.
    [ "$(sed -n 1p $STUB_LOG.args)" = "--settings" ] || fail "missing --settings"
    grep -q '"model": *"opus"' "$(sed -n 2p $STUB_LOG.args)" || fail "settings file content"
    [ "$(sed -n '3,$p' $STUB_LOG.args)" = "$(printf -- '--resume\nx')" ] || fail "user args not passed through"
    [ ! -e $s/settings.json ] || fail "launcher must not own settings.json"

    ${full.launchers.codex-work}/bin/codex-work exec hi
    c=$HOME/.local/agent-environments/codex-work
    grep -qxF "CODEX_HOME=$c" $STUB_LOG.env || fail "CODEX_HOME not set"
    [ "$(cat $c/AGENTS.md)" = "$(printf 'FIRST\n\nSECOND')" ] || fail "AGENTS.md content"
    [ -f $c/skills/leaf/SKILL.md ] || fail "codex skills missing"
    grep -q 'model = "gpt-5"' $c/agent-environments.config.toml || fail "codex profile content"
    [ "$(cat $STUB_LOG.args)" = "$(printf -- '--profile\nagent-environments\nexec\nhi')" ] || fail "codex args: $(cat $STUB_LOG.args)"
    [ ! -e $c/config.toml ] || fail "launcher must not own config.toml"
  '';

  # Separate launchers from one environment must not share a login.
  separate-logins = runTest "separate-logins" ''
    ${full.launchers.claude-work}/bin/claude-work
    a=$(grep CLAUDE_CONFIG_DIR $STUB_LOG.env)
    ${full.launchers.claude-pers}/bin/claude-pers
    b=$(grep CLAUDE_CONFIG_DIR $STUB_LOG.env)
    [ "$a" != "$b" ] || fail "claude-work and claude-pers share $a"
    [ "$b" = "CLAUDE_CONFIG_DIR=$HOME/.local/agent-environments/claude-pers" ] || fail "$b"
  '';

  # Without settings, no flags are injected and no files are managed.
  no-settings = runTest "no-settings" ''
    ${bare.launchers.claude-work}/bin/claude-work
    [ ! -s $STUB_LOG.args ] || fail "unexpected args: $(cat $STUB_LOG.args)"
    ${bare.launchers.codex-bare}/bin/codex-bare
    [ ! -s $STUB_LOG.args ] || fail "unexpected codex args: $(cat $STUB_LOG.args)"
    c=$HOME/.local/agent-environments/codex-bare
    [ ! -e $c/AGENTS.md ] && [ ! -e $c/agent-environments.config.toml ] || fail "files managed without config"
  '';

  # Switching to an environment with fewer skills removes only what we linked.
  cleanup-keeps-user-files = runTest "cleanup-keeps-user-files" ''
    s=$HOME/.local/agent-environments/claude-work
    mkdir -p $s/skills/.system $s/skills/mine
    ln -s /nix/store $s/skills/user-link
    ${full.launchers.claude-work}/bin/claude-work
    [ -L $s/skills/top ] || fail "top not linked"
    ${bare.launchers.claude-work}/bin/claude-work
    [ ! -e $s/skills/top ] && [ ! -e $s/skills/leaf ] || fail "stale skills kept"
    [ ! -e $s/CLAUDE.md ] || fail "stale CLAUDE.md kept"
    [ -d $s/skills/.system ] && [ -d $s/skills/mine ] && [ -L $s/skills/user-link ] || fail "user files removed"
    # And back again: the manifest must let us re-create the links.
    ${full.launchers.claude-work-bare}/bin/claude-work-bare
    [ -L $s/skills/top ] || fail "relink failed"
  '';

  # User-owned files at managed paths are never replaced.
  refuses-unmanaged = runTest "refuses-unmanaged" ''
    s=$HOME/.local/agent-environments/claude-work
    mkdir -p $s/skills
    echo mine >$s/CLAUDE.md
    if ${full.launchers.claude-work}/bin/claude-work 2>err; then fail "launcher replaced user CLAUDE.md"; fi
    grep -q "refusing to replace '$s/CLAUDE.md'" err || fail "unclear error: $(cat err)"
    [ "$(cat $s/CLAUDE.md)" = mine ] || fail "user CLAUDE.md modified"
    [ ! -e $STUB_LOG.args ] || fail "agent started despite conflict"

    rm $s/CLAUDE.md
    ln -s /nix/store $s/skills/top
    if ${full.launchers.claude-work}/bin/claude-work 2>err; then fail "launcher replaced user symlink"; fi
    [ "$(readlink $s/skills/top)" = /nix/store ] || fail "user symlink modified"

    # A path we managed before, but which the user replaced with real data, is theirs now.
    rm $s/skills/top
    ${full.launchers.claude-work}/bin/claude-work
    rm $s/CLAUDE.md && echo edited >$s/CLAUDE.md
    if ${full.launchers.claude-work}/bin/claude-work 2>err; then fail "launcher replaced user file at managed path"; fi
    [ "$(cat $s/CLAUDE.md)" = edited ] || fail "user file modified"
  '';

  # The launcher's own bookkeeping paths must not write through symlinks into
  # files or directories outside the state dir.
  no-writes-through-symlinks = runTest "no-writes-through-symlinks" ''
    s=$HOME/.local/agent-environments/claude-work
    mkdir -p $s/skills
    echo precious >$TMPDIR/precious
    ln -s $TMPDIR/precious $s/.agent-environments.lock
    ${full.launchers.claude-work}/bin/claude-work
    [ "$(cat $TMPDIR/precious)" = precious ] || fail "lock open truncated a symlinked file"

    p=$HOME/.local/agent-environments/claude-pers
    mkdir -p $p $TMPDIR/shared-skills
    ln -s $TMPDIR/shared-skills $p/skills
    if ${full.launchers.claude-pers}/bin/claude-pers 2>err; then fail "managed links through a symlinked skills dir"; fi
    [ -z "$(ls -A $TMPDIR/shared-skills)" ] || fail "wrote into the shared skills dir"
  '';

  # stateDir is data, not shell code: only a leading ~/ is expanded.
  literal-state-dir =
    let
      env = mkEnvironment {
        inherit pkgs;
        modules = [
          stubs
          {
            claude.enable = true;
            launchers.claude-odd = {
              agent = "claude";
              stateDir = ''~/odd "dir"/$(touch injected)'';
            };
          }
        ];
      };
    in
    runTest "literal-state-dir" ''
      cd $TMPDIR
      ${env.launchers.claude-odd}/bin/claude-odd
      [ ! -e injected ] || fail "stateDir was executed as shell code"
      grep -qxF "CLAUDE_CONFIG_DIR=$HOME/odd \"dir\"/\$(touch injected)" $STUB_LOG.env || fail "$(cat $STUB_LOG.env)"
    '';

  skill-without-skill-md = pkgs.testers.testBuildFailure (mkSkill {
    inherit pkgs;
    name = "broken";
    src = pkgs.writeTextDir "README.md" "";
  });

  # Configuration mistakes are caught at evaluation time.
  eval-errors =
    assert fails [
      stubs
      {
        claude.enable = true;
        launchers.x = {
          agent = "claude";
          stateDir = "relative/dir";
        };
      }
    ];
    assert fails [
      stubs
      { launchers.x.agent = "claude"; }
    ];
    assert fails [
      stubs
      {
        claude.enable = true;
        launchers.x = {
          agent = "claude";
          id = "../escape";
        };
      }
    ];
    assert fails [
      stubs
      {
        claude.enable = true;
        launchers."bad name".agent = "claude";
      }
    ];
    assert fails [
      stubs
      {
        claude.enable = true;
        skills.packages = [
          top
          otherLeaf
        ];
        launchers.x.agent = "claude";
      }
    ];
    assert
      !(builtins.tryEval
        (mkSkill {
          inherit pkgs;
          name = "a/b";
          src = skillSrc "x";
        }).drvPath
      ).success;
    pkgs.runCommand "agent-environments-eval-errors" { } "touch $out";
}