~ruther/agent-environments

agent-environments/checks/default.nix -rw-r--r-- 17.8 KiB
8a0ec343 — František Boháček Update codex and claude-code inputs a month 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
{ 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:-}"
        echo "AGENT_ENV_LAUNCHER=''${AGENT_ENV_LAUNCHER:-}"
        echo "PATH=$PATH"
        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 ];
  };

  fixturePlugin = pkgs.writeTextDir ".claude-plugin/plugin.json" (
    builtins.toJSON { name = "fixture"; }
  );

  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";
        claude.plugins = [ fixturePlugin ];
        claude.mcpServers.probe = {
          type = "http";
          url = "https://probe.invalid/mcp";
        };
        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"

    # MCP servers loaded via --mcp-config, settings layered via --settings,
    # plugins loaded with --plugin-dir, user args passed through after them.
    [ "$(sed -n 1p $STUB_LOG.args)" = "--mcp-config" ] || fail "missing --mcp-config"
    grep -q '"url": *"https://probe.invalid/mcp"' "$(sed -n 2p $STUB_LOG.args)" || fail "mcp config content"
    [ "$(sed -n 3p $STUB_LOG.args)" = "--settings" ] || fail "missing --settings"
    grep -q '"model": *"opus"' "$(sed -n 4p $STUB_LOG.args)" || fail "settings file content"
    [ "$(sed -n 5,6p $STUB_LOG.args)" = "$(printf -- "--plugin-dir\n${fixturePlugin}")" ] || fail "plugin not loaded"
    [ "$(sed -n '7,$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"
  '';

  # Hooks in agent settings call tools that no skill provides; `packages` puts
  # them on PATH, and an environment without them must not have them.
  packages-on-path =
    let
      env = mkEnvironment {
        inherit pkgs;
        modules = [
          stubs
          {
            codex.enable = true;
            packages = [ pkgs.hello ];
            launchers.codex-tools.agent = "codex";
          }
        ];
      };
    in
    runTest "packages-on-path" ''
      ${bare.launchers.codex-bare}/bin/codex-bare
      grep -qxF "HELLO=no" $STUB_LOG.env || fail "hello on PATH without packages"
      ${env.launchers.codex-tools}/bin/codex-tools
      grep -qxF "HELLO=yes" $STUB_LOG.env || fail "packages not on PATH"
    '';

  # --mcp-config takes every following argument until the next flag, so a
  # launcher with only MCP servers must still put a flag between it and the
  # user's arguments, or a prompt would be read as another config file.
  mcp-config-not-last =
    let
      env = mkEnvironment {
        inherit pkgs;
        modules = [
          stubs
          {
            claude.enable = true;
            claude.mcpServers.probe.url = "https://probe.invalid/mcp";
            launchers.claude-mcp.agent = "claude";
          }
        ];
      };
    in
    runTest "mcp-config-not-last" ''
      ${env.launchers.claude-mcp}/bin/claude-mcp "a prompt"
      [ "$(sed -n 1p $STUB_LOG.args)" = "--mcp-config" ] || fail "missing --mcp-config"
      [ "$(sed -n 3p $STUB_LOG.args)" = "--settings" ] || fail "--mcp-config not followed by a flag: $(cat $STUB_LOG.args)"
      [ "$(sed -n '5,$p' $STUB_LOG.args)" = "a prompt" ] || fail "user args not passed through"
    '';

  # 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"
  '';

  # A shared directory is a managed link to the other launcher's copy. The copy
  # is created on demand, so the link works whichever launcher starts first, and
  # undeclaring the share removes the link but never the data behind it.
  shared-dirs =
    let
      withShare = share: {
        claude.enable = true;
        launchers.claude-work.agent = "claude";
        launchers.claude-pers = {
          agent = "claude";
          sharedDirs = share;
        };
      };
      sharing = mkEnvironment {
        inherit pkgs;
        modules = [
          stubs
          (withShare { projects = "claude-work"; })
        ];
      };
      alone = mkEnvironment {
        inherit pkgs;
        modules = [
          stubs
          (withShare { })
        ];
      };
      work = "$HOME/.local/agent-environments/claude-work";
      pers = "$HOME/.local/agent-environments/claude-pers";
    in
    runTest "shared-dirs" ''
      ${sharing.launchers.claude-pers}/bin/claude-pers
      [ -d ${work}/projects ] || fail "shared target not created"
      [ "$(readlink ${pers}/projects)" = ${work}/projects ] || fail "link: $(readlink ${pers}/projects)"
      grep -qx projects ${pers}/.agent-environments-managed || fail "link not in manifest"
      echo data >${pers}/projects/session.jsonl
      [ "$(cat ${work}/projects/session.jsonl)" = data ] || fail "write did not land in the shared dir"

      ${alone.launchers.claude-pers}/bin/claude-pers
      [ ! -L ${pers}/projects ] || fail "undeclared share still linked"
      [ "$(cat ${work}/projects/session.jsonl)" = data ] || fail "shared data removed with the link"
    '';

  # 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)"
    '';

  # With companions, a session can start every profile of its environment, but a
  # companion session cannot start further companions.
  companions =
    let
      env = mkEnvironment {
        inherit pkgs;
        modules = [
          stubs
          {
            claude.enable = true;
            codex.enable = true;
            companions.enable = true;
            launchers = {
              claude-work.agent = "claude";
              codex-pers.agent = "codex";
            };
          }
        ];
      };
    in
    runTest "companions" ''
      ${env.launchers.claude-work}/bin/claude-work
      grep -qxF "AGENT_ENV_LAUNCHER=claude-work" $STUB_LOG.env || fail "AGENT_ENV_LAUNCHER not exported"
      session_path=$(sed -n 's/^PATH=//p' $STUB_LOG.env)
      companion=$(PATH=$session_path command -v codex-pers) || fail "codex-pers not on the session PATH"
      PATH=$session_path command -v claude-work >/dev/null || fail "own profile not available as companion"

      skill=$HOME/.local/agent-environments/claude-work/skills/companions/SKILL.md
      grep -qF '`codex-pers exec --sandbox read-only' $skill || fail "codex call missing from skill"
      grep -qF '`claude-work -p "<prompt>"' $skill || fail "claude call missing from skill"
      grep -qF -- '--model' $skill || fail "model choice missing from skill"

      # The companion runs with its own login and without companions of its own.
      $companion exec x
      grep -qxF "CODEX_HOME=$HOME/.local/agent-environments/codex-pers" $STUB_LOG.env || fail "companion login"
      grep -qxF "AGENT_ENV_LAUNCHER=codex-pers" $STUB_LOG.env || fail "companion name"
      nested_path=$(sed -n 's/^PATH=//p' $STUB_LOG.env)
      if PATH=$nested_path command -v claude-work >/dev/null; then fail "companion can start companions"; fi
    '';

  # The stubs only prove what the launcher hands over; this proves the real Codex
  # reads it: AGENTS.md, linked skills and the settings profile all reach the prompt.
  codex-integration =
    let
      env = mkEnvironment {
        inherit pkgs;
        modules = [
          {
            codex.enable = true;
            instructions.probe.text = "PROBE_INSTRUCTION_MARKER";
            skills.packages = [
              (mkSkill {
                inherit pkgs;
                name = "probe";
                src = pkgs.writeTextDir "SKILL.md" ''
                  ---
                  name: probe
                  description: PROBE_SKILL_MARKER use when probing
                  ---
                '';
              })
            ];
            codex.settings.developer_instructions = "PROBE_PROFILE_MARKER";
            launchers.codex-probe.agent = "codex";
          }
        ];
      };
    in
    runTest "codex-integration" ''
      ${env.launchers.codex-probe}/bin/codex-probe debug prompt-input hi >prompt.json
      for m in PROBE_INSTRUCTION_MARKER PROBE_SKILL_MARKER PROBE_PROFILE_MARKER; do
        grep -q $m prompt.json || fail "codex prompt lacks $m"
      done
    '';

  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;
    # A shared dir must name another, existing launcher and a safe path component.
    assert lib.all (share: fails [
      stubs
      {
        claude.enable = true;
        launchers.x.agent = "claude";
        launchers.y = {
          agent = "claude";
          sharedDirs = share;
        };
      }
    ]) [
      { projects = "nope"; }
      { projects = "y"; }
      { "../escape" = "x"; }
    ];
    pkgs.runCommand "agent-environments-eval-errors" { } "touch $out";
}