@app-vitals/agent-admin 1.27.0 → 1.29.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/e2e/plugins-section.e2e.ts +168 -0
- package/e2e/test-server.ts +42 -10
- package/openapi.platform.json +390 -0
- package/package.json +2 -2
- package/src/admin-dev-login.smoke.test.ts +1 -0
- package/src/admin-local-agent.smoke.test.ts +1 -0
- package/src/admin-ui-pages.ts +17 -2
- package/src/admin-ui-pages.unit.test.ts +74 -1
- package/src/admin-ui.fixtures.ts +1 -0
- package/src/admin-ui.smoke.test.ts +146 -0
- package/src/admin-ui.ts +75 -1
- package/src/agents-api.smoke.test.ts +278 -0
- package/src/agents-api.ts +270 -0
- package/src/chat.smoke.test.ts +1 -0
- package/src/github-reconcile-routes.integration.test.ts +202 -0
- package/src/pwa-https-gating.smoke.test.ts +3 -0
- package/src/pwa-routes.smoke.test.ts +3 -0
- package/src/reconcile-apply-service.ts +30 -0
- package/src/reconcile-apply-service.unit.test.ts +42 -0
- package/src/server.smoke.test.ts +5 -1
- package/src/slack-provisioning.integration.test.ts +3 -0
package/src/admin-ui-pages.ts
CHANGED
|
@@ -1287,7 +1287,7 @@ export function renderAgentDetailPage(
|
|
|
1287
1287
|
|
|
1288
1288
|
const pluginRows =
|
|
1289
1289
|
plugins.length === 0
|
|
1290
|
-
? `<tr><td colspan="
|
|
1290
|
+
? `<tr><td colspan="5" class="empty-state">No plugins installed.</td></tr>`
|
|
1291
1291
|
: plugins
|
|
1292
1292
|
.map(
|
|
1293
1293
|
(p) => `<tr>
|
|
@@ -1295,6 +1295,12 @@ export function renderAgentDetailPage(
|
|
|
1295
1295
|
<td class="mono">${p.version ? escapeHtml(p.version) : "latest"}</td>
|
|
1296
1296
|
<td><span class="badge ${p.enabled ? "badge-green" : "badge-gray"}">${p.enabled ? "enabled" : "disabled"}</span></td>
|
|
1297
1297
|
<td>${escapeHtml(new Date(p.createdAt).toLocaleDateString("en-US", { timeZone: timezone }))}</td>
|
|
1298
|
+
<td>
|
|
1299
|
+
<form method="POST" action="/admin/agents/${escapeHtml(agent.id)}/plugins/delete" style="display:inline">
|
|
1300
|
+
<input type="hidden" name="pluginId" value="${escapeHtml(p.id)}" />
|
|
1301
|
+
<button type="submit" class="btn btn-danger" style="font-size:11px;padding:3px 8px">Remove</button>
|
|
1302
|
+
</form>
|
|
1303
|
+
</td>
|
|
1298
1304
|
</tr>`,
|
|
1299
1305
|
)
|
|
1300
1306
|
.join("\n");
|
|
@@ -1725,16 +1731,20 @@ export function renderAgentDetailPage(
|
|
|
1725
1731
|
</div>
|
|
1726
1732
|
</div>`;
|
|
1727
1733
|
|
|
1734
|
+
const pluginMarketplaceNames = [
|
|
1735
|
+
...new Set([...marketplaces.map((m) => m.name), ...bakedMarketplaces]),
|
|
1736
|
+
];
|
|
1728
1737
|
const pluginsSection = `<div class="card">
|
|
1729
1738
|
<div class="card-title">Plugins</div>
|
|
1730
1739
|
<div class="data-table-wrapper">
|
|
1731
|
-
<table class="data-table">
|
|
1740
|
+
<table class="data-table" id="plugins-table">
|
|
1732
1741
|
<thead>
|
|
1733
1742
|
<tr>
|
|
1734
1743
|
<th>Package</th>
|
|
1735
1744
|
<th>Version</th>
|
|
1736
1745
|
<th>Status</th>
|
|
1737
1746
|
<th>Created</th>
|
|
1747
|
+
<th></th>
|
|
1738
1748
|
</tr>
|
|
1739
1749
|
</thead>
|
|
1740
1750
|
<tbody>
|
|
@@ -1742,6 +1752,11 @@ export function renderAgentDetailPage(
|
|
|
1742
1752
|
</tbody>
|
|
1743
1753
|
</table>
|
|
1744
1754
|
</div>
|
|
1755
|
+
<form method="POST" action="/admin/agents/${escapeHtml(agent.id)}/plugins" id="add-plugin-form" style="display:flex;gap:8px;flex-wrap:wrap;margin-top:12px">
|
|
1756
|
+
<input name="spec" type="text" class="form-input mono" list="plugin-marketplaces" placeholder="plugin@marketplace (bare name = shipwright)" required />
|
|
1757
|
+
<datalist id="plugin-marketplaces">${pluginMarketplaceNames.map((n) => `<option value="${escapeHtml(n)}"></option>`).join("")}</datalist>
|
|
1758
|
+
<button type="submit" class="btn btn-primary">Add plugin</button>
|
|
1759
|
+
</form>
|
|
1745
1760
|
</div>`;
|
|
1746
1761
|
|
|
1747
1762
|
const marketplacesSection = `<div class="card">
|
|
@@ -1891,7 +1891,7 @@ describe("renderAgentDetailPage — plugins", () => {
|
|
|
1891
1891
|
test("Plugins table is wrapped in .data-table-wrapper", () => {
|
|
1892
1892
|
const html = render([PLUGIN_ENABLED]);
|
|
1893
1893
|
expect(html).toMatch(
|
|
1894
|
-
/<div class="data-table-wrapper">\s*<table class="data-table"
|
|
1894
|
+
/<div class="data-table-wrapper">\s*<table class="data-table"[^>]*>\s*<thead>\s*<tr>\s*<th>Package<\/th>/,
|
|
1895
1895
|
);
|
|
1896
1896
|
});
|
|
1897
1897
|
|
|
@@ -4586,3 +4586,76 @@ describe("renderAgentDetailPage — marketplaces section (PMK-1.5)", () => {
|
|
|
4586
4586
|
expect(html).toContain("<b>x</b>");
|
|
4587
4587
|
});
|
|
4588
4588
|
});
|
|
4589
|
+
|
|
4590
|
+
describe("renderAgentDetailPage — plugins add/remove (PUI-1.2)", () => {
|
|
4591
|
+
const render = (
|
|
4592
|
+
plugins: Array<{
|
|
4593
|
+
id: string;
|
|
4594
|
+
name: string;
|
|
4595
|
+
version: string | null;
|
|
4596
|
+
enabled: boolean;
|
|
4597
|
+
createdAt: Date;
|
|
4598
|
+
}>,
|
|
4599
|
+
marketplaces: Array<{
|
|
4600
|
+
id: string;
|
|
4601
|
+
name: string;
|
|
4602
|
+
source: string;
|
|
4603
|
+
ref: string | null;
|
|
4604
|
+
createdAt: Date;
|
|
4605
|
+
}> = [],
|
|
4606
|
+
bakedMarketplaces?: string[],
|
|
4607
|
+
) =>
|
|
4608
|
+
renderAgentDetailPage(
|
|
4609
|
+
{ ...AGENT, bakedMarketplaces },
|
|
4610
|
+
{},
|
|
4611
|
+
[],
|
|
4612
|
+
[],
|
|
4613
|
+
[],
|
|
4614
|
+
plugins,
|
|
4615
|
+
[],
|
|
4616
|
+
USER_NAME,
|
|
4617
|
+
true,
|
|
4618
|
+
{ timezone: "UTC", marketplaces },
|
|
4619
|
+
);
|
|
4620
|
+
const plugin = {
|
|
4621
|
+
id: "pl1",
|
|
4622
|
+
name: "shipwright@shipwright",
|
|
4623
|
+
version: null,
|
|
4624
|
+
enabled: true,
|
|
4625
|
+
createdAt: new Date("2024-01-01"),
|
|
4626
|
+
};
|
|
4627
|
+
|
|
4628
|
+
test("rows carry a remove form posting pluginId", () => {
|
|
4629
|
+
const html = render([plugin]);
|
|
4630
|
+
expect(html).toContain(`/admin/agents/${AGENT.id}/plugins/delete`);
|
|
4631
|
+
expect(html).toContain('name="pluginId" value="pl1"');
|
|
4632
|
+
});
|
|
4633
|
+
|
|
4634
|
+
test("add form posts a spec with a datalist of marketplace names", () => {
|
|
4635
|
+
const html = render(
|
|
4636
|
+
[],
|
|
4637
|
+
[
|
|
4638
|
+
{
|
|
4639
|
+
id: "mk1",
|
|
4640
|
+
name: "remote-one",
|
|
4641
|
+
source: "a/b",
|
|
4642
|
+
ref: null,
|
|
4643
|
+
createdAt: new Date(),
|
|
4644
|
+
},
|
|
4645
|
+
],
|
|
4646
|
+
["baked-one"],
|
|
4647
|
+
);
|
|
4648
|
+
const form = html.match(/<form[^>]*id="add-plugin-form".*?<\/form>/s)?.[0];
|
|
4649
|
+
expect(form).toContain(`action="/admin/agents/${AGENT.id}/plugins"`);
|
|
4650
|
+
expect(form).toContain('name="spec"');
|
|
4651
|
+
expect(form).toContain("Add plugin");
|
|
4652
|
+
expect(html).toMatch(
|
|
4653
|
+
/<datalist id="plugin-marketplaces">.*remote-one.*baked-one.*<\/datalist>/s,
|
|
4654
|
+
);
|
|
4655
|
+
});
|
|
4656
|
+
|
|
4657
|
+
test("escapes plugin names", () => {
|
|
4658
|
+
const html = render([{ ...plugin, name: "<b>x</b>" }]);
|
|
4659
|
+
expect(html).not.toContain("<b>x</b>");
|
|
4660
|
+
});
|
|
4661
|
+
});
|
package/src/admin-ui.fixtures.ts
CHANGED
|
@@ -3840,6 +3840,152 @@ describe("admin UI — marketplaces (PMK-1.5)", () => {
|
|
|
3840
3840
|
});
|
|
3841
3841
|
});
|
|
3842
3842
|
|
|
3843
|
+
describe("admin UI — plugin add/remove (PUI-1.2)", () => {
|
|
3844
|
+
const MEMBER = "member@example.com";
|
|
3845
|
+
const form = (fields: Record<string, string>, cookie: string) => ({
|
|
3846
|
+
method: "POST",
|
|
3847
|
+
body: new URLSearchParams(fields).toString(),
|
|
3848
|
+
headers: {
|
|
3849
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
3850
|
+
Cookie: `admin_session=${cookie}`,
|
|
3851
|
+
},
|
|
3852
|
+
});
|
|
3853
|
+
const pluginService = (calls: unknown[][], removed: string[]) => ({
|
|
3854
|
+
list: async () => [],
|
|
3855
|
+
add: async (...args: unknown[]) => {
|
|
3856
|
+
calls.push(args);
|
|
3857
|
+
return {} as never;
|
|
3858
|
+
},
|
|
3859
|
+
remove: async (_id: string, pid: string) => {
|
|
3860
|
+
removed.push(pid);
|
|
3861
|
+
},
|
|
3862
|
+
});
|
|
3863
|
+
|
|
3864
|
+
it("admin add passes the spec through and redirects", async () => {
|
|
3865
|
+
const calls: unknown[][] = [];
|
|
3866
|
+
const app = createAdminUIApp(
|
|
3867
|
+
makeMockDeps({ agentPluginService: pluginService(calls, []) }),
|
|
3868
|
+
);
|
|
3869
|
+
const res = await app.request(
|
|
3870
|
+
`/admin/agents/${AGENT_ID}/plugins`,
|
|
3871
|
+
form({ spec: " foo@mkt " }, await makeSessionCookie()),
|
|
3872
|
+
);
|
|
3873
|
+
expect(res.status).toBe(302);
|
|
3874
|
+
expect(res.headers.get("location")).toBe(`/admin/agents/${AGENT_ID}`);
|
|
3875
|
+
expect(calls).toEqual([[AGENT_ID, "foo@mkt"]]);
|
|
3876
|
+
});
|
|
3877
|
+
|
|
3878
|
+
it("a bare name defaults to the shipwright marketplace", async () => {
|
|
3879
|
+
const calls: unknown[][] = [];
|
|
3880
|
+
const app = createAdminUIApp(
|
|
3881
|
+
makeMockDeps({ agentPluginService: pluginService(calls, []) }),
|
|
3882
|
+
);
|
|
3883
|
+
await app.request(
|
|
3884
|
+
`/admin/agents/${AGENT_ID}/plugins`,
|
|
3885
|
+
form({ spec: "foo" }, await makeSessionCookie()),
|
|
3886
|
+
);
|
|
3887
|
+
expect(calls).toEqual([[AGENT_ID, "foo@shipwright"]]);
|
|
3888
|
+
});
|
|
3889
|
+
|
|
3890
|
+
it("a malformed spec redirects with an error and does not add", async () => {
|
|
3891
|
+
const calls: unknown[][] = [];
|
|
3892
|
+
const app = createAdminUIApp(
|
|
3893
|
+
makeMockDeps({ agentPluginService: pluginService(calls, []) }),
|
|
3894
|
+
);
|
|
3895
|
+
for (const spec of ["a@b@c", "foo@", "@mkt", "has space", ""]) {
|
|
3896
|
+
const res = await app.request(
|
|
3897
|
+
`/admin/agents/${AGENT_ID}/plugins`,
|
|
3898
|
+
form({ spec }, await makeSessionCookie()),
|
|
3899
|
+
);
|
|
3900
|
+
expect(res.status).toBe(302);
|
|
3901
|
+
expect(res.headers.get("location")).toContain("error=");
|
|
3902
|
+
}
|
|
3903
|
+
expect(calls).toEqual([]);
|
|
3904
|
+
});
|
|
3905
|
+
|
|
3906
|
+
it("an UnprocessableEntityError from the service surfaces via ?error=", async () => {
|
|
3907
|
+
const app = createAdminUIApp(
|
|
3908
|
+
makeMockDeps({
|
|
3909
|
+
agentPluginService: {
|
|
3910
|
+
list: async () => [],
|
|
3911
|
+
add: async () => {
|
|
3912
|
+
throw new UnprocessableEntityError("bad plugin spec");
|
|
3913
|
+
},
|
|
3914
|
+
remove: async () => {},
|
|
3915
|
+
},
|
|
3916
|
+
}),
|
|
3917
|
+
);
|
|
3918
|
+
const res = await app.request(
|
|
3919
|
+
`/admin/agents/${AGENT_ID}/plugins`,
|
|
3920
|
+
form({ spec: "foo@bar" }, await makeSessionCookie()),
|
|
3921
|
+
);
|
|
3922
|
+
expect(res.headers.get("location")).toContain("error=bad%20plugin%20spec");
|
|
3923
|
+
});
|
|
3924
|
+
|
|
3925
|
+
it("remove calls the service with pluginId", async () => {
|
|
3926
|
+
const removed: string[] = [];
|
|
3927
|
+
const app = createAdminUIApp(
|
|
3928
|
+
makeMockDeps({ agentPluginService: pluginService([], removed) }),
|
|
3929
|
+
);
|
|
3930
|
+
const res = await app.request(
|
|
3931
|
+
`/admin/agents/${AGENT_ID}/plugins/delete`,
|
|
3932
|
+
form({ pluginId: "pl1" }, await makeSessionCookie()),
|
|
3933
|
+
);
|
|
3934
|
+
expect(res.status).toBe(302);
|
|
3935
|
+
expect(removed).toEqual(["pl1"]);
|
|
3936
|
+
});
|
|
3937
|
+
|
|
3938
|
+
it("a member can add and remove; a non-member gets 403", async () => {
|
|
3939
|
+
const calls: unknown[][] = [];
|
|
3940
|
+
const removed: string[] = [];
|
|
3941
|
+
const memberCookie = await makeSessionCookie(
|
|
3942
|
+
SESSION_SECRET,
|
|
3943
|
+
"google-sub-member",
|
|
3944
|
+
MEMBER,
|
|
3945
|
+
false,
|
|
3946
|
+
);
|
|
3947
|
+
const outsiderCookie = await makeSessionCookie(
|
|
3948
|
+
SESSION_SECRET,
|
|
3949
|
+
"google-sub-outsider",
|
|
3950
|
+
"outsider@example.com",
|
|
3951
|
+
false,
|
|
3952
|
+
);
|
|
3953
|
+
const app = createAdminUIApp(
|
|
3954
|
+
makeMockDeps({
|
|
3955
|
+
agentMemberService: {
|
|
3956
|
+
listByEmail: async (email: string) =>
|
|
3957
|
+
email === MEMBER
|
|
3958
|
+
? [{ id: "m1", agentId: AGENT_ID, email, createdAt: new Date() }]
|
|
3959
|
+
: [],
|
|
3960
|
+
exists: async (_a: string, email: string) => email === MEMBER,
|
|
3961
|
+
add: async () => ({}) as never,
|
|
3962
|
+
remove: async () => {},
|
|
3963
|
+
listByAgentId: async () => [],
|
|
3964
|
+
},
|
|
3965
|
+
agentPluginService: pluginService(calls, removed),
|
|
3966
|
+
}),
|
|
3967
|
+
);
|
|
3968
|
+
const add = await app.request(
|
|
3969
|
+
`/admin/agents/${AGENT_ID}/plugins`,
|
|
3970
|
+
form({ spec: "foo" }, memberCookie),
|
|
3971
|
+
);
|
|
3972
|
+
const del = await app.request(
|
|
3973
|
+
`/admin/agents/${AGENT_ID}/plugins/delete`,
|
|
3974
|
+
form({ pluginId: "pl1" }, memberCookie),
|
|
3975
|
+
);
|
|
3976
|
+
expect([add.status, del.status]).toEqual([302, 302]);
|
|
3977
|
+
for (const path of ["plugins", "plugins/delete"]) {
|
|
3978
|
+
const res = await app.request(
|
|
3979
|
+
`/admin/agents/${AGENT_ID}/${path}`,
|
|
3980
|
+
form({ spec: "foo", pluginId: "pl1" }, outsiderCookie),
|
|
3981
|
+
);
|
|
3982
|
+
expect(res.status).toBe(403);
|
|
3983
|
+
}
|
|
3984
|
+
expect(calls.length).toBe(1);
|
|
3985
|
+
expect(removed).toEqual(["pl1"]);
|
|
3986
|
+
});
|
|
3987
|
+
});
|
|
3988
|
+
|
|
3843
3989
|
describe("admin UI — manifest sync route", () => {
|
|
3844
3990
|
let adminCookie: string;
|
|
3845
3991
|
|
package/src/admin-ui.ts
CHANGED
|
@@ -313,7 +313,9 @@ export interface AdminUIDeps {
|
|
|
313
313
|
AgentTokenService,
|
|
314
314
|
"listForAgent" | "create" | "revoke"
|
|
315
315
|
>;
|
|
316
|
-
|
|
316
|
+
/** `remove` is optional (additive to the exported AdminUIDeps); absent makes the remove route a no-op. */
|
|
317
|
+
agentPluginService: Pick<AgentPluginService, "list" | "add"> &
|
|
318
|
+
Partial<Pick<AgentPluginService, "remove">>;
|
|
317
319
|
/**
|
|
318
320
|
* PMK-1.5: remote marketplaces for the agent page. Optional (additive to the
|
|
319
321
|
* exported AdminUIDeps); absent hides the add/remove routes' effect and
|
|
@@ -805,6 +807,22 @@ export function createAgentAccessChecks(deps: {
|
|
|
805
807
|
|
|
806
808
|
// ─── App factory ──────────────────────────────────────────────────────────────
|
|
807
809
|
|
|
810
|
+
/** Marketplace a bare plugin spec resolves to (the bundled one). */
|
|
811
|
+
const DEFAULT_PLUGIN_MARKETPLACE = "shipwright";
|
|
812
|
+
|
|
813
|
+
/**
|
|
814
|
+
* Validate a plugin install spec and return its canonical
|
|
815
|
+
* "<plugin>@<marketplace>" form. Throws UnprocessableEntityError if malformed.
|
|
816
|
+
*/
|
|
817
|
+
function normalizePluginSpec(spec: string): string {
|
|
818
|
+
if (!/^[^\s@]+(@[^\s@]+)?$/.test(spec)) {
|
|
819
|
+
throw new UnprocessableEntityError(
|
|
820
|
+
"plugin must be <plugin>@<marketplace> or a bare plugin name",
|
|
821
|
+
);
|
|
822
|
+
}
|
|
823
|
+
return spec.includes("@") ? spec : `${spec}@${DEFAULT_PLUGIN_MARKETPLACE}`;
|
|
824
|
+
}
|
|
825
|
+
|
|
808
826
|
export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
809
827
|
const {
|
|
810
828
|
prisma,
|
|
@@ -2630,6 +2648,62 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
2630
2648
|
return c.redirect(`/admin/agents/${agentId}`, 302);
|
|
2631
2649
|
});
|
|
2632
2650
|
|
|
2651
|
+
// ─── Plugin mutations (PUI-1.2) ───────────────────────────────────────────
|
|
2652
|
+
// Same access rule as the marketplace routes. The spec is the canonical
|
|
2653
|
+
// Claude install spec: "<plugin>@<marketplace>", or a bare "<plugin>" which
|
|
2654
|
+
// defaults to the built-in shipwright marketplace.
|
|
2655
|
+
|
|
2656
|
+
app.post("/admin/agents/:id/plugins", requireAuth, async (c) => {
|
|
2657
|
+
const agentId = c.req.param("id");
|
|
2658
|
+
if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
|
|
2659
|
+
return new Response("Forbidden", { status: 403 });
|
|
2660
|
+
}
|
|
2661
|
+
let raw: string | undefined;
|
|
2662
|
+
try {
|
|
2663
|
+
const formData = await c.req.formData();
|
|
2664
|
+
raw = formData.get("spec")?.toString().trim();
|
|
2665
|
+
} catch {
|
|
2666
|
+
return c.redirect(`/admin/agents/${agentId}`, 302);
|
|
2667
|
+
}
|
|
2668
|
+
const redirectError = (message: string) =>
|
|
2669
|
+
c.redirect(
|
|
2670
|
+
`/admin/agents/${agentId}?error=${encodeURIComponent(message)}`,
|
|
2671
|
+
302,
|
|
2672
|
+
);
|
|
2673
|
+
if (!raw) return redirectError("missing_fields");
|
|
2674
|
+
try {
|
|
2675
|
+
await agentPluginService.add(agentId, normalizePluginSpec(raw));
|
|
2676
|
+
} catch (err) {
|
|
2677
|
+
if (err instanceof UnprocessableEntityError) {
|
|
2678
|
+
return redirectError(err.message);
|
|
2679
|
+
}
|
|
2680
|
+
// other errors — redirect back silently, like the tool routes
|
|
2681
|
+
}
|
|
2682
|
+
return c.redirect(`/admin/agents/${agentId}`, 302);
|
|
2683
|
+
});
|
|
2684
|
+
|
|
2685
|
+
app.post("/admin/agents/:id/plugins/delete", requireAuth, async (c) => {
|
|
2686
|
+
const agentId = c.req.param("id");
|
|
2687
|
+
if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
|
|
2688
|
+
return new Response("Forbidden", { status: 403 });
|
|
2689
|
+
}
|
|
2690
|
+
let pluginId: string | undefined;
|
|
2691
|
+
try {
|
|
2692
|
+
const formData = await c.req.formData();
|
|
2693
|
+
pluginId = formData.get("pluginId")?.toString();
|
|
2694
|
+
} catch {
|
|
2695
|
+
return c.redirect(`/admin/agents/${agentId}`, 302);
|
|
2696
|
+
}
|
|
2697
|
+
if (pluginId) {
|
|
2698
|
+
try {
|
|
2699
|
+
await agentPluginService.remove?.(agentId, pluginId);
|
|
2700
|
+
} catch {
|
|
2701
|
+
// already gone, ignore
|
|
2702
|
+
}
|
|
2703
|
+
}
|
|
2704
|
+
return c.redirect(`/admin/agents/${agentId}`, 302);
|
|
2705
|
+
});
|
|
2706
|
+
|
|
2633
2707
|
// ─── Token mutations ──────────────────────────────────────────────────────
|
|
2634
2708
|
|
|
2635
2709
|
app.post("/admin/agents/:id/tokens", requireAuth, async (c) => {
|
|
@@ -6900,3 +6900,281 @@ describe("admin API — GET /agents/:id/github-access (GHE-2.4)", () => {
|
|
|
6900
6900
|
);
|
|
6901
6901
|
});
|
|
6902
6902
|
});
|
|
6903
|
+
|
|
6904
|
+
// ─── GHE-3.4: POST /agents/:id/github-reconcile/{plan,apply} ─────────────────
|
|
6905
|
+
|
|
6906
|
+
describe("admin API — GitHub reconcile routes (GHE-3.4)", () => {
|
|
6907
|
+
const OPS = "ghr-ops-token";
|
|
6908
|
+
const READER = "ghr-read-token";
|
|
6909
|
+
const AGENT_KEY = "ghr-agent-token";
|
|
6910
|
+
const DIFF = {
|
|
6911
|
+
installationAdds: ["acme/api"],
|
|
6912
|
+
installationRemoves: [],
|
|
6913
|
+
configAdds: ["acme/api"],
|
|
6914
|
+
configRemoves: ["acme/old"],
|
|
6915
|
+
};
|
|
6916
|
+
const PLAN = {
|
|
6917
|
+
skipped: false,
|
|
6918
|
+
diff: DIFF,
|
|
6919
|
+
skippedOrgs: [],
|
|
6920
|
+
rejectedAdds: [],
|
|
6921
|
+
warnings: ["heads up"],
|
|
6922
|
+
empty: false,
|
|
6923
|
+
planHash: "hash-1",
|
|
6924
|
+
};
|
|
6925
|
+
const SKIPPED_PLAN = {
|
|
6926
|
+
...PLAN,
|
|
6927
|
+
skipped: true,
|
|
6928
|
+
skipReason: "agent has no bound GitHub username",
|
|
6929
|
+
diff: {
|
|
6930
|
+
installationAdds: [],
|
|
6931
|
+
installationRemoves: [],
|
|
6932
|
+
configAdds: [],
|
|
6933
|
+
configRemoves: [],
|
|
6934
|
+
},
|
|
6935
|
+
warnings: [],
|
|
6936
|
+
empty: true,
|
|
6937
|
+
planHash: "hash-skip",
|
|
6938
|
+
};
|
|
6939
|
+
type Reconcile = NonNullable<AdminDeps["githubReconcile"]>;
|
|
6940
|
+
|
|
6941
|
+
function setup(reconcile?: Partial<Reconcile>) {
|
|
6942
|
+
const base = makeMockDeps();
|
|
6943
|
+
const calls: { fn: string; args: unknown[] }[] = [];
|
|
6944
|
+
const port: Reconcile = {
|
|
6945
|
+
planWithOpenWork: async (...args) => {
|
|
6946
|
+
calls.push({ fn: "plan", args });
|
|
6947
|
+
return {
|
|
6948
|
+
plan: PLAN,
|
|
6949
|
+
openWork: [{ id: "T-1", repo: "acme/old", title: "fix it" }],
|
|
6950
|
+
};
|
|
6951
|
+
},
|
|
6952
|
+
apply: async (input) => {
|
|
6953
|
+
calls.push({ fn: "apply", args: [input] });
|
|
6954
|
+
return {
|
|
6955
|
+
status: "applied",
|
|
6956
|
+
plan: PLAN,
|
|
6957
|
+
auditId: "audit-1",
|
|
6958
|
+
warnings: [],
|
|
6959
|
+
};
|
|
6960
|
+
},
|
|
6961
|
+
...reconcile,
|
|
6962
|
+
};
|
|
6963
|
+
const deps: AdminDeps = {
|
|
6964
|
+
...base,
|
|
6965
|
+
adminApiKeys: parseAdminApiKeys(
|
|
6966
|
+
`root:${ADMIN_API_KEY}:*,operator:${OPS}:platform.ops,reader:${READER}:platform.read`,
|
|
6967
|
+
),
|
|
6968
|
+
agentTokenService: {
|
|
6969
|
+
...base.agentTokenService,
|
|
6970
|
+
validate: async (t: string) =>
|
|
6971
|
+
t === AGENT_KEY ? { agentId: AGENT_ID } : null,
|
|
6972
|
+
},
|
|
6973
|
+
githubReconcile: reconcile === undefined ? undefined : port,
|
|
6974
|
+
};
|
|
6975
|
+
return { app: createAdminApp(deps), calls, port };
|
|
6976
|
+
}
|
|
6977
|
+
const withPort = () => setup({});
|
|
6978
|
+
|
|
6979
|
+
const post = (
|
|
6980
|
+
app: ReturnType<typeof createAdminApp>,
|
|
6981
|
+
action: "plan" | "apply",
|
|
6982
|
+
headers: Record<string, string>,
|
|
6983
|
+
body: unknown = {},
|
|
6984
|
+
id = AGENT_ID,
|
|
6985
|
+
) =>
|
|
6986
|
+
app.request(`/agents/${id}/github-reconcile/${action}`, {
|
|
6987
|
+
method: "POST",
|
|
6988
|
+
headers: { "Content-Type": "application/json", ...headers },
|
|
6989
|
+
body: JSON.stringify(body),
|
|
6990
|
+
});
|
|
6991
|
+
const asAdmin = { Authorization: `Bearer ${ADMIN_API_KEY}` };
|
|
6992
|
+
|
|
6993
|
+
it("403 for a session member, ops key, read key and agent key", async () => {
|
|
6994
|
+
const { app, calls } = withPort();
|
|
6995
|
+
const memberApp = createAdminApp({
|
|
6996
|
+
...makeMockDeps(),
|
|
6997
|
+
callerScopeResolver: async () => ({
|
|
6998
|
+
kind: "scoped",
|
|
6999
|
+
accountId: "acct-a",
|
|
7000
|
+
agentIds: [AGENT_ID],
|
|
7001
|
+
}),
|
|
7002
|
+
});
|
|
7003
|
+
const cookie = await sign(
|
|
7004
|
+
{
|
|
7005
|
+
isAdmin: false,
|
|
7006
|
+
userId: "user-acct",
|
|
7007
|
+
email: "owner@acct-a.example.com",
|
|
7008
|
+
iat: Math.floor(Date.now() / 1000),
|
|
7009
|
+
exp: Math.floor(Date.now() / 1000) + 3600,
|
|
7010
|
+
},
|
|
7011
|
+
SESSION_SECRET,
|
|
7012
|
+
"HS256",
|
|
7013
|
+
);
|
|
7014
|
+
for (const action of ["plan", "apply"] as const) {
|
|
7015
|
+
const res = await post(
|
|
7016
|
+
memberApp,
|
|
7017
|
+
action,
|
|
7018
|
+
{ Cookie: `admin_session=${cookie}` },
|
|
7019
|
+
{ planHash: "x" },
|
|
7020
|
+
);
|
|
7021
|
+
expect(res.status).toBe(403);
|
|
7022
|
+
for (const key of [OPS, READER, AGENT_KEY]) {
|
|
7023
|
+
const r = await post(
|
|
7024
|
+
app,
|
|
7025
|
+
action,
|
|
7026
|
+
{ Authorization: `Bearer ${key}` },
|
|
7027
|
+
{ planHash: "x" },
|
|
7028
|
+
);
|
|
7029
|
+
expect(r.status).toBe(403);
|
|
7030
|
+
}
|
|
7031
|
+
}
|
|
7032
|
+
expect(calls).toEqual([]);
|
|
7033
|
+
});
|
|
7034
|
+
|
|
7035
|
+
it("plan returns the diff, warnings, hash and affected open work", async () => {
|
|
7036
|
+
const { app, calls } = withPort();
|
|
7037
|
+
const res = await post(app, "plan", asAdmin, { addRepos: ["acme/api"] });
|
|
7038
|
+
expect(res.status).toBe(200);
|
|
7039
|
+
const body = (await res.json()) as Record<string, unknown>;
|
|
7040
|
+
expect(body).toMatchObject({
|
|
7041
|
+
status: "planned",
|
|
7042
|
+
planHash: "hash-1",
|
|
7043
|
+
warnings: ["heads up"],
|
|
7044
|
+
diff: DIFF,
|
|
7045
|
+
empty: false,
|
|
7046
|
+
affectedOpenWork: [{ id: "T-1", repo: "acme/old", title: "fix it" }],
|
|
7047
|
+
});
|
|
7048
|
+
expect(calls).toEqual([{ fn: "plan", args: [AGENT_ID, ["acme/api"]] }]);
|
|
7049
|
+
});
|
|
7050
|
+
|
|
7051
|
+
it("plan with no body works", async () => {
|
|
7052
|
+
const { app } = withPort();
|
|
7053
|
+
const res = await app.request(`/agents/${AGENT_ID}/github-reconcile/plan`, {
|
|
7054
|
+
method: "POST",
|
|
7055
|
+
headers: asAdmin,
|
|
7056
|
+
});
|
|
7057
|
+
expect(res.status).toBe(200);
|
|
7058
|
+
});
|
|
7059
|
+
|
|
7060
|
+
it("plan on a skipped state is 200 with status skipped and a reason", async () => {
|
|
7061
|
+
const { app } = setup({
|
|
7062
|
+
planWithOpenWork: async () => ({ plan: SKIPPED_PLAN, openWork: [] }),
|
|
7063
|
+
});
|
|
7064
|
+
const res = await post(app, "plan", asAdmin);
|
|
7065
|
+
expect(res.status).toBe(200);
|
|
7066
|
+
expect(await res.json()).toMatchObject({
|
|
7067
|
+
status: "skipped",
|
|
7068
|
+
reason: "agent has no bound GitHub username",
|
|
7069
|
+
});
|
|
7070
|
+
});
|
|
7071
|
+
|
|
7072
|
+
it("with no reconcile port wired, plan and apply are skipped, not errors", async () => {
|
|
7073
|
+
const { app } = setup();
|
|
7074
|
+
for (const action of ["plan", "apply"] as const) {
|
|
7075
|
+
const res = await post(app, action, asAdmin, { planHash: "x" });
|
|
7076
|
+
expect(res.status).toBe(200);
|
|
7077
|
+
const body = (await res.json()) as { status: string; reason: string };
|
|
7078
|
+
expect(body.status).toBe("skipped");
|
|
7079
|
+
expect(body.reason).toContain("not configured");
|
|
7080
|
+
}
|
|
7081
|
+
});
|
|
7082
|
+
|
|
7083
|
+
it("404 for an unknown agent", async () => {
|
|
7084
|
+
const { app } = setup({
|
|
7085
|
+
planWithOpenWork: async () => {
|
|
7086
|
+
throw new Error("agent no-such-agent not found");
|
|
7087
|
+
},
|
|
7088
|
+
apply: async () => ({ status: "agent_not_found" }),
|
|
7089
|
+
});
|
|
7090
|
+
expect((await post(app, "plan", asAdmin, {}, "no-such-agent")).status).toBe(
|
|
7091
|
+
404,
|
|
7092
|
+
);
|
|
7093
|
+
expect(
|
|
7094
|
+
(await post(app, "apply", asAdmin, { planHash: "x" }, "no-such-agent"))
|
|
7095
|
+
.status,
|
|
7096
|
+
).toBe(404);
|
|
7097
|
+
});
|
|
7098
|
+
|
|
7099
|
+
it("apply returns the audit id and result and passes the caller", async () => {
|
|
7100
|
+
const { app, calls } = withPort();
|
|
7101
|
+
const res = await post(app, "apply", asAdmin, {
|
|
7102
|
+
planHash: "hash-1",
|
|
7103
|
+
addRepos: ["acme/api"],
|
|
7104
|
+
confirmRemovals: true,
|
|
7105
|
+
});
|
|
7106
|
+
expect(res.status).toBe(200);
|
|
7107
|
+
expect(await res.json()).toMatchObject({
|
|
7108
|
+
status: "applied",
|
|
7109
|
+
auditId: "audit-1",
|
|
7110
|
+
planHash: "hash-1",
|
|
7111
|
+
diff: DIFF,
|
|
7112
|
+
warnings: [],
|
|
7113
|
+
});
|
|
7114
|
+
const input = calls[0]?.args[0] as Record<string, unknown>;
|
|
7115
|
+
expect(input).toMatchObject({
|
|
7116
|
+
agentId: AGENT_ID,
|
|
7117
|
+
planHash: "hash-1",
|
|
7118
|
+
namedAdds: ["acme/api"],
|
|
7119
|
+
confirmRemovals: true,
|
|
7120
|
+
});
|
|
7121
|
+
expect(typeof input.caller).toBe("string");
|
|
7122
|
+
});
|
|
7123
|
+
|
|
7124
|
+
it("apply on a skipped or noop plan is 200 with that status", async () => {
|
|
7125
|
+
const skipped = setup({
|
|
7126
|
+
apply: async () => ({ status: "skipped", plan: SKIPPED_PLAN }),
|
|
7127
|
+
});
|
|
7128
|
+
const r1 = await post(skipped.app, "apply", asAdmin, { planHash: "x" });
|
|
7129
|
+
expect(r1.status).toBe(200);
|
|
7130
|
+
expect(await r1.json()).toMatchObject({
|
|
7131
|
+
status: "skipped",
|
|
7132
|
+
reason: "agent has no bound GitHub username",
|
|
7133
|
+
});
|
|
7134
|
+
const noop = setup({
|
|
7135
|
+
apply: async () => ({ status: "noop", plan: { ...PLAN, empty: true } }),
|
|
7136
|
+
});
|
|
7137
|
+
const r2 = await post(noop.app, "apply", asAdmin, { planHash: "x" });
|
|
7138
|
+
expect(r2.status).toBe(200);
|
|
7139
|
+
expect(((await r2.json()) as { status: string }).status).toBe("noop");
|
|
7140
|
+
});
|
|
7141
|
+
|
|
7142
|
+
it("apply 409 on a stale plan hash, 422 over the removal cap", async () => {
|
|
7143
|
+
const stale = setup({
|
|
7144
|
+
apply: async () => ({ status: "stale_plan", plan: PLAN }),
|
|
7145
|
+
});
|
|
7146
|
+
expect(
|
|
7147
|
+
(await post(stale.app, "apply", asAdmin, { planHash: "old" })).status,
|
|
7148
|
+
).toBe(409);
|
|
7149
|
+
const cap = setup({
|
|
7150
|
+
apply: async () => ({ status: "removal_cap", plan: PLAN, removals: 11 }),
|
|
7151
|
+
});
|
|
7152
|
+
const res = await post(cap.app, "apply", asAdmin, { planHash: "hash-1" });
|
|
7153
|
+
expect(res.status).toBe(422);
|
|
7154
|
+
expect(((await res.json()) as { error: string }).error).toContain(
|
|
7155
|
+
"confirmRemovals",
|
|
7156
|
+
);
|
|
7157
|
+
});
|
|
7158
|
+
|
|
7159
|
+
it("apply failure is reported with the audit id", async () => {
|
|
7160
|
+
const { app } = setup({
|
|
7161
|
+
apply: async () => ({
|
|
7162
|
+
status: "failed",
|
|
7163
|
+
plan: PLAN,
|
|
7164
|
+
auditId: "audit-2",
|
|
7165
|
+
error: "boom",
|
|
7166
|
+
}),
|
|
7167
|
+
});
|
|
7168
|
+
const res = await post(app, "apply", asAdmin, { planHash: "hash-1" });
|
|
7169
|
+
expect(res.status).toBe(200);
|
|
7170
|
+
expect(await res.json()).toMatchObject({
|
|
7171
|
+
status: "failed",
|
|
7172
|
+
auditId: "audit-2",
|
|
7173
|
+
});
|
|
7174
|
+
});
|
|
7175
|
+
|
|
7176
|
+
it("400 when apply has no planHash", async () => {
|
|
7177
|
+
const { app } = withPort();
|
|
7178
|
+
expect((await post(app, "apply", asAdmin, {})).status).toBe(400);
|
|
7179
|
+
});
|
|
7180
|
+
});
|