mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-29 21:07:40 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Agent adapters run agent sessions through the ACPX engine > - The ACPX engine handled one run attempt as a long implicit procedure > - That shape made resource ownership, cleanup order, and failure behavior hard to verify > - This pull request gives the attempt a coordinator, a typed resource ledger, separate run sites, and explicit turn and settlement sequences > - The benefit is clear ownership, one cleanup path, safer session reuse, and testable failure behavior ## Linked Issues or Issue Description **What existing behavior does this improve?** The ACPX engine manages startup, turn execution, session reuse, and cleanup inside one large run procedure. **Current behavior** The run procedure owns several resources through implicit control flow. Cleanup and session reuse behavior depend on lane-specific branches and error paths. **Proposed behavior** The coordinator owns the run attempt. A typed ledger records six resources and their states. Host and sandbox run sites own lane-specific acquisition. Turn and settlement sequences expose typed outcomes. The engine emits allowlisted phase telemetry. **Reason and benefit** Explicit ownership makes cleanup and failure behavior easier to inspect. The fault matrix and characterization tests protect the external result while the refactor reduces hidden control flow. **Breaking changes** None to the public adapter contract. The host warm-save path now closes and relaunches the runtime because a transferred runtime could retain a run-scoped credential. A cold session-handshake failure now closes the created runtime. **Additional context** This pull request contains the ACPX engine lifecycle refactor, its tests, and the lifecycle document. ## What Changed - Add a run coordinator for startup, turn execution, settlement, and result reproduction. - Add a typed resource ledger with open, sealed, and consumed states. - Add host and sandbox run sites for lane-specific resource acquisition. - Replace separate runtime maps with a generic session reuse store. - Split session fingerprint identity from the outer session key. - Add typed turn and settlement sequences with one cleanup owner. - Add a closed allowlist for phase telemetry. - Add characterization tests and a 17-case fault matrix. - Add `doc/acp-run-lifecycle.md`. ## Verification - `npx vitest run packages/adapter-utils/src/acpx-engine/` passes 18 files and 286 tests at the submitted commit. - `pnpm --filter @paperclipai/adapter-utils typecheck` reports 0 errors at the submitted commit. - Run the full pull request checks after GitHub starts CI. - Run Greptile review after the pull request opens. ## Risks - The refactor changes internal control flow across the ACPX engine. - Host warm-save behavior now closes and relaunches the runtime. - Settlement changes the handling of a cold session-handshake failure from a leak to a close. - The characterization baselines and fault matrix reduce the risk of an external behavior change. > Paperclip is the open source app people use to manage AI agents for work > The adapter layer runs agent sessions through the ACPX engine > The engine needs explicit lifecycle ownership for reliable cleanup > This pull request adds coordinator-owned phases and a typed resource ledger > The result makes lifecycle behavior easier to test and review ## Model Used OpenAI GPT-5 Codex. Exact model ID: GPT-5. The model used tool execution, repository inspection, and code review support. The implementation author supplied the submitted code. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
96 lines
3.9 KiB
TypeScript
96 lines
3.9 KiB
TypeScript
// Identity split and launch-environment finalization tests (Phase 17).
|
|
//
|
|
// Two tests are compile-time: the real assertion is that this file typechecks.
|
|
// A `@ts-expect-error` marks an assignment that MUST fail; if it starts to
|
|
// compile, tsc reports the unused directive and the typecheck fails. Each
|
|
// compile-time assertion lives in a declared, unexecuted function, so vitest
|
|
// never runs a synthetic value.
|
|
|
|
import { describe, expect, it } from "vitest";
|
|
import { buildSessionFingerprint, finalizeLaunchEnvironment } from "./execute.js";
|
|
import type {
|
|
LaunchEnvironment,
|
|
RunScopedContribution,
|
|
RunSiteReuseCandidate,
|
|
SessionFingerprintIdentity,
|
|
} from "./run-contracts.js";
|
|
|
|
// A complete fingerprint identity with representative values for the 17 fields.
|
|
const SAMPLE_FINGERPRINT_IDENTITY: SessionFingerprintIdentity = {
|
|
acpxAgent: "claude",
|
|
agentCommand: "claude",
|
|
cwd: "/work",
|
|
mode: "persistent",
|
|
permissionMode: "approve-all",
|
|
nonInteractivePermissions: "deny",
|
|
requestedModel: "",
|
|
requestedThinkingEffort: "",
|
|
fastMode: false,
|
|
remoteExecutionIdentity: null,
|
|
additionalSourcesIdentity: {},
|
|
skillsIdentity: {},
|
|
skillPromptInstructions: "",
|
|
paperclipClaudeSettings: null,
|
|
mcpServers: [],
|
|
secretManifestHash: "0000",
|
|
adapterEnvHash: "0000",
|
|
};
|
|
|
|
describe("acpx identity split and launch environment", () => {
|
|
it("test_fingerprint_builder_accepts_only_session_fingerprint_identity", () => {
|
|
// The builder accepts a fingerprint identity and returns a stable hash.
|
|
const first = buildSessionFingerprint(SAMPLE_FINGERPRINT_IDENTITY);
|
|
const second = buildSessionFingerprint(SAMPLE_FINGERPRINT_IDENTITY);
|
|
expect(first).toBe(second);
|
|
expect(first).toMatch(/^[0-9a-f]{16}$/);
|
|
|
|
// The compile-time assertions live in an unexecuted function.
|
|
function _assertOnlyIdentity(): void {
|
|
// An outer-key field is not a fingerprint field, so the builder rejects it.
|
|
// @ts-expect-error companyId is not a SessionFingerprintIdentity field.
|
|
buildSessionFingerprint({ ...SAMPLE_FINGERPRINT_IDENTITY, companyId: "c" });
|
|
// @ts-expect-error taskKey is not a SessionFingerprintIdentity field.
|
|
buildSessionFingerprint({ ...SAMPLE_FINGERPRINT_IDENTITY, taskKey: "t" });
|
|
}
|
|
void _assertOnlyIdentity;
|
|
});
|
|
|
|
it("test_finalize_launch_environment_is_sole_constructor_of_branded_environment", () => {
|
|
// The finalizer merges every contribution and freezes the launch env.
|
|
const baseEnv: Record<string, string> = { LAUNCH_ENV_TEST_BASE: "base" };
|
|
const launchEnvironment = finalizeLaunchEnvironment(baseEnv, [
|
|
{ scope: "session", env: { LAUNCH_ENV_TEST_CONTRIB: "contrib" } },
|
|
]);
|
|
expect(launchEnvironment.env.LAUNCH_ENV_TEST_BASE).toBe("base");
|
|
expect(launchEnvironment.env.LAUNCH_ENV_TEST_CONTRIB).toBe("contrib");
|
|
expect(Object.isFrozen(launchEnvironment.env)).toBe(true);
|
|
|
|
// No plain object literal can stand in for the branded environment; it lacks
|
|
// the private brand that only `finalizeLaunchEnvironment` sets.
|
|
function _assertBrand(): void {
|
|
// @ts-expect-error missing the private launch-environment brand.
|
|
const _fake: LaunchEnvironment = { env: {} };
|
|
void _fake;
|
|
}
|
|
void _assertBrand;
|
|
});
|
|
|
|
it("test_run_scoped_contribution_is_not_assignable_to_a_reuse_payload", () => {
|
|
// A run-scoped contribution and the branded launch environment must never
|
|
// enter a reuse payload (Amendment B). The reuse candidate types reject both.
|
|
function _assertNotReusable(
|
|
contribution: RunScopedContribution,
|
|
launchEnvironment: LaunchEnvironment,
|
|
): void {
|
|
// @ts-expect-error a run-scoped contribution is not a reuse candidate.
|
|
const _a: RunSiteReuseCandidate = contribution;
|
|
// @ts-expect-error the branded launch environment is not a reuse candidate.
|
|
const _b: RunSiteReuseCandidate = launchEnvironment;
|
|
void _a;
|
|
void _b;
|
|
}
|
|
void _assertNotReusable;
|
|
expect(true).toBe(true);
|
|
});
|
|
});
|