Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepgatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
agent-runtime.test.ts8.9 KB · 253 lines
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
/**
 * Block K1 — Agent runtime unit tests.
 *
 * Covers the pure helpers (state machine + log truncation) plus the
 * runSandboxed primitive via real subprocesses. DB helpers are intentionally
 * not tested here — they hit Neon and are covered by integration flow.
 */

import { describe, it, expect } from "bun:test";
import {
  type AgentRunStatus,
  canTransition,
  isTerminalStatus,
  runSandboxed,
  truncateError,
  truncateLog,
  __internal,
} from "../lib/agent-runtime";

const ALL_STATUSES: AgentRunStatus[] = [
  "queued",
  "running",
  "succeeded",
  "failed",
  "killed",
  "timeout",
];

describe("agent-runtime — isTerminalStatus", () => {
  it("treats queued + running as non-terminal", () => {
    expect(isTerminalStatus("queued")).toBe(false);
    expect(isTerminalStatus("running")).toBe(false);
  });

  it("treats succeeded / failed / killed / timeout as terminal", () => {
    expect(isTerminalStatus("succeeded")).toBe(true);
    expect(isTerminalStatus("failed")).toBe(true);
    expect(isTerminalStatus("killed")).toBe(true);
    expect(isTerminalStatus("timeout")).toBe(true);
  });

  it("is exhaustive over AgentRunStatus", () => {
    // Every status value must have a defined answer (no undefined bubbling).
    for (const s of ALL_STATUSES) {
      expect(typeof isTerminalStatus(s)).toBe("boolean");
    }
  });
});

describe("agent-runtime — canTransition", () => {
  it("allows the happy path queued → running → succeeded", () => {
    expect(canTransition("queued", "running")).toBe(true);
    expect(canTransition("running", "succeeded")).toBe(true);
  });

  it("allows running → failed | timeout | killed", () => {
    expect(canTransition("running", "failed")).toBe(true);
    expect(canTransition("running", "timeout")).toBe(true);
    expect(canTransition("running", "killed")).toBe(true);
  });

  it("allows queued → killed (operator cancel before start)", () => {
    expect(canTransition("queued", "killed")).toBe(true);
  });

  it("forbids going backward from any terminal state", () => {
    for (const terminal of ["succeeded", "failed", "killed", "timeout"] as const) {
      for (const to of ALL_STATUSES) {
        expect(canTransition(terminal, to)).toBe(false);
      }
    }
  });

  it("forbids self-transitions", () => {
    for (const s of ALL_STATUSES) {
      expect(canTransition(s, s)).toBe(false);
    }
  });

  it("forbids skipping running (queued → succeeded/failed/timeout)", () => {
    expect(canTransition("queued", "succeeded")).toBe(false);
    expect(canTransition("queued", "failed")).toBe(false);
    expect(canTransition("queued", "timeout")).toBe(false);
  });

  it("forbids running → queued (no re-queue)", () => {
    expect(canTransition("running", "queued")).toBe(false);
  });
});

describe("agent-runtime — truncateLog", () => {
  it("returns a concatenation when well under the cap", () => {
    const out = truncateLog("abc", "def", 1024);
    expect(out).toBe("abcdef");
  });

  it("returns exactly the combined string at the cap boundary", () => {
    const existing = "a".repeat(500);
    const addition = "b".repeat(500);
    const out = truncateLog(existing, addition, 1000);
    expect(out.length).toBe(1000);
    expect(out).toBe(existing + addition);
  });

  it("prepends the sentinel and keeps the last maxBytes when over the cap", () => {
    const existing = "x".repeat(2000);
    const addition = "y".repeat(500);
    const out = truncateLog(existing, addition, 1000);
    expect(out.startsWith(__internal.LOG_TRUNCATED_SENTINEL)).toBe(true);
    // Tail length == maxBytes; we keep the rightmost characters of existing+addition.
    const tail = out.slice(__internal.LOG_TRUNCATED_SENTINEL.length);
    expect(tail.length).toBe(1000);
    // Since addition ('y'*500) sits at the very end, the last 500 chars must be 'y'.
    expect(tail.endsWith("y".repeat(500))).toBe(true);
    // And the middle should be the tail of the 'x' run.
    expect(tail.startsWith("x".repeat(500))).toBe(true);
  });

  it("does not double the sentinel on subsequent truncations", () => {
    // First truncation.
    const first = truncateLog("x".repeat(2000), "", 1000);
    expect(first.startsWith(__internal.LOG_TRUNCATED_SENTINEL)).toBe(true);
    // Feed the already-truncated log back in with more content that forces
    // another truncation.
    const second = truncateLog(first, "z".repeat(1500), 1000);
    expect(second.startsWith(__internal.LOG_TRUNCATED_SENTINEL)).toBe(true);
    // Only one sentinel should appear in total.
    const occurrences = second.split(__internal.LOG_TRUNCATED_SENTINEL).length - 1;
    expect(occurrences).toBe(1);
    // Most of the tail should be 'z'.
    expect(second.endsWith("z".repeat(500))).toBe(true);
  });

  it("handles empty existing and empty addition safely", () => {
    expect(truncateLog("", "", 100)).toBe("");
    expect(truncateLog("", "hi", 100)).toBe("hi");
    expect(truncateLog("hi", "", 100)).toBe("hi");
  });
});

describe("agent-runtime — truncateError", () => {
  it("leaves short strings alone", () => {
    expect(truncateError("boom")).toBe("boom");
  });

  it("trims long strings and appends a marker", () => {
    const long = "e".repeat(10_000);
    const out = truncateError(long, 4096);
    expect(out.length).toBeLessThanOrEqual(4096 + 32); // body + marker
    expect(out.startsWith("e".repeat(4096))).toBe(true);
    expect(out).toContain("truncated");
  });

  it("handles empty / null-ish input", () => {
    expect(truncateError("")).toBe("");
    // @ts-expect-error — runtime guard against null callers.
    expect(truncateError(null)).toBe("");
  });
});

describe("agent-runtime — runSandboxed", () => {
  it("runs a successful command and captures stdout", async () => {
    const res = await runSandboxed("/bin/echo", ["hello world"], {
      cwd: "/tmp",
      timeoutMs: 2000,
    });
    expect(res.exitCode).toBe(0);
    expect(res.timedOut).toBe(false);
    expect(res.stdout).toContain("hello world");
    expect(res.stderr).toBe("");
  });

  it("returns a non-zero exit code for failing commands", async () => {
    // `false` always exits 1.
    const res = await runSandboxed("/bin/sh", ["-c", "exit 7"], {
      cwd: "/tmp",
      timeoutMs: 2000,
    });
    expect(res.exitCode).toBe(7);
    expect(res.timedOut).toBe(false);
  });

  it("kills long-running processes on timeout", async () => {
    const started = Date.now();
    const res = await runSandboxed("/bin/sleep", ["10"], {
      cwd: "/tmp",
      timeoutMs: 200,
    });
    const elapsed = Date.now() - started;
    expect(res.timedOut).toBe(true);
    // 200ms timeout + up to 5s SIGTERM grace before SIGKILL; sleep(1) responds
    // to SIGTERM immediately so in practice well under a second.
    expect(elapsed).toBeLessThan(6000);
    expect(res.stderr).toContain("timeout");
  }, 10_000);

  it("caps stdout at the configured stream cap", async () => {
    // Write ~200 KB of 'a's to stdout via a tiny python-free shell loop.
    // `yes` is simpler: it spams a string forever. Combined with head
    // we get deterministic size without relying on Python/Node.
    const res = await runSandboxed(
      "/bin/sh",
      ["-c", "yes aaaaaaaaaa | head -c 204800"],
      { cwd: "/tmp", timeoutMs: 5000, stdoutCapBytes: 64 * 1024 }
    );
    expect(res.exitCode).toBe(0);
    // Capped stream = 64 KB content + '\n[... truncated ...]' suffix.
    expect(res.stdout.length).toBeGreaterThanOrEqual(64 * 1024);
    expect(res.stdout.length).toBeLessThan(64 * 1024 + 64);
    expect(res.stdout).toContain("truncated");
  }, 10_000);

  it("reports spawn failure without throwing", async () => {
    const res = await runSandboxed(
      "/nonexistent/definitely-not-a-real-binary-xyz",
      [],
      { cwd: "/tmp", timeoutMs: 1000 }
    );
    // Bun either returns exitCode=null + stderr message, or exit!=0 — both
    // are acceptable "failure surfaced" outcomes. What must NOT happen is a
    // throw. Assert the call resolved and returned a result object.
    expect(typeof res.timedOut).toBe("boolean");
    expect(res.exitCode === null || res.exitCode !== 0).toBe(true);
  });

  it("passes a minimal env by default (no process.env leakage)", async () => {
    // Set a secret-looking var in the parent process and ensure it does NOT
    // leak into the sandbox's environment when the caller passes no env.
    process.env.GLUECRON_TEST_SECRET = "leak-me-please";
    const res = await runSandboxed(
      "/bin/sh",
      ["-c", "echo \"S=${GLUECRON_TEST_SECRET:-unset}\""],
      { cwd: "/tmp", timeoutMs: 2000 }
    );
    delete process.env.GLUECRON_TEST_SECRET;
    expect(res.exitCode).toBe(0);
    expect(res.stdout).toContain("S=unset");
  });

  it("honours a caller-supplied env verbatim", async () => {
    const res = await runSandboxed(
      "/bin/sh",
      ["-c", "echo \"V=$FOO\""],
      {
        cwd: "/tmp",
        timeoutMs: 2000,
        env: { PATH: process.env.PATH ?? "/usr/bin:/bin", FOO: "bar" },
      }
    );
    expect(res.exitCode).toBe(0);
    expect(res.stdout).toContain("V=bar");
  });
});