Files
PaperClipAI/server/src/__tests__/project-skill-sources.test.ts
T
DottaandPaperclip 124b2cd0ad fix(server): certify background deadlines for idle tenants (#19001)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Background services own future checks, retries, and recovery work.
> - An idle tenant can stop only when a durable wake covers its
remaining duties.
> - The current alarm catalog covers several domains but leaves these
owners blocked.
> - A saved future timestamp alone cannot prove that reconstruction or
admitted work has settled.
> - This pull request binds durable domain snapshots to the actual
registered worker revisions.
> - The tenant can hand off supported future duties and retain all
checks for uncertain or active work.

## Linked Issues or Issue Description

No matching public issue was found. Related work: [domain deadline
handoff #18656](https://github.com/paperclipai/paperclip/pull/18656),
[owned blocker diagnostics
#18632](https://github.com/paperclipai/paperclip/pull/18632), and
[completed history proof
#18646](https://github.com/paperclipai/paperclip/pull/18646). The
separate [native idle-session change
#18988](https://github.com/paperclipai/paperclip/pull/18988) covers
runtime retirement. [Far-future retry re-probes
#13534](https://github.com/paperclipai/paperclip/pull/13534) changes
recovery responsiveness after an upstream success; this change preserves
existing retry decisions.

**What happened?**

Future status-card, external-object, skill-sync, watchdog, retry, and
quota duties lacked complete owner-bound coverage in the idle deadline
proof. A completed quota task could also retain its monitor policy and
timestamp and remain blocked after the scheduler had disarmed it.

**Expected behavior**

A settled registered owner can hand off its exact supported future
deadline. Active work, dirty reconstruction, transaction uncertainty,
changed revisions, claims, unknown state, and unsupported duties keep
the tenant awake. Completed history retains its data and stops recurring
work when the owner has no remaining duty.

**Steps to reproduce**

1. Produce a supported future duty through its normal service.
2. Let the registered worker finish reconstruction and admitted work.
3. Request an owned idle report with the negotiated domain deadline
catalog.
4. Check the saved earliest deadline and generation before durable alarm
registration.
5. Stop and reconstruct the worker. Check that ordinary admission and
claims resume useful work once due.
6. Complete a quota task. Keep its saved policy and timestamp. Check
that linked unfinished work still blocks sleep and that settled history
does not.

**Paperclip version or commit**

The owned patch was qualified at
`699c8569fa34338dd75cdc5731381ec1e2efdb45`. Publication head
`491fac368ee72eaac8ae105d53e6bf6b305d8a6a` normally merges actual master
`039e0dad2c51c80251e1302a19c5566e039c357a`. All 43 owned files are
byte-identical. All 29 new upstream paths are preserved. The later
normal current-master integration is
`8c912e1ba4fc66397a7341ade59373af8075068f`, described separately below.

**Deployment mode**

Source checkout with real PostgreSQL 17 and a controlled local Node
process. No deployed alarm, tenant suspension, database suspension, or
fleet result is claimed.

## What Changed

- Add registered-worker reconciliation revisions and invalidate them on
stop.
- Keep status-card, external-object, and skill deadline interpretation
with their owners.
- Reuse the watchdog classifier against the supplied database snapshot.
- Validate the exact saved retry run and paired queued wake.
- Validate legacy quota source bindings and the current assignee or
reviewer policy.
- Preserve common issue scope, dependency, sibling, and state checks for
covered waits.
- Compose normalized owner snapshots within the existing bounded
deadline proof and recheck revisions after inspection.
- Bind actual service and worker instances from server startup. Missing
bindings preserve existing blockers.
- Retain completed quota policy and timestamps while independently
rejecting linked unfinished runs or wakes.
- Block unconsumed budget evaluation inputs through the owning queue
predicate. Accounting policy and settlement stay unchanged.
- Bind completed quota history through fixed-size UUID-array parameters,
preserve exact company/task/source associations, and guard external
references before the run-ID join.
- Batch watchdog subtrees and shared classifier facts without changing
the 64-query or two-second proof budget; preserve exact company pairing
for first-run evidence.
- Extend production-path regression controls and document supported
coverage and limits.

## Verification

Current integration head: `688abc6cac6016b9fb50e5832f81e2975ed5d49f`.
Fresh exact-head checks completed with **58 successes, three intentional
skips and one neutral review**, including aggregate `ci / verify`. The
complete current Greptile review is **5/5**. The complete current
Ponytail verdict is **Ship (4/5)**, with only optional cleanup of an
unused MCP proof input; both prior correctness findings are resolved.
All review threads and nested pages were inspected. The normal master
merges preserve the registered retry deadline owner and current startup
completion log after the same readiness await. The earlier focused
evidence below remains tied to its tested source revisions.


Current-master integration `8c912e1ba4fc66397a7341ade59373af8075068f`:

- Normally merge actual master
`46fb933b0155d183ce64a8bdc4950d1b58c926f6`. Every nonoverlapping tree
blob retains its owning parent across 14,044 paths. All 42 owner-only
files and 15 critical binding/proof/readiness files remain
byte-identical. Three automatic overlaps preserve the existing watchdog
binding, conservative unavailable-reader fallback and optional actual
owner arguments. The two scoped-deadline conflicts retain the entire
upstream resource-reservation/mid-pass admission/abort algorithm inside
the existing reconciliation revision/running/failure fence, with both
parent test sets.
- Actual TypeScript 7.0.2 server noEmit passed in 9.434 seconds, normal
source declaration layers in 12.102 seconds, and four selected
test/helper/config layers extending the actual CI configuration in 3.167
seconds. No compiler option weakening, heap override, donor declarations
or SDK replacement stub.
- Focused PG0 controls passed: 102 scheduler/proof/owner controls in
16.451 seconds, including all 21 scoped-deadline cases; 121 startup
controls; and all 107 unchanged instance-settings route cases in 6.456
seconds. The latter first hit a local prewire-guard
listener-registration race. Only the ignored guard was corrected to
register the same actual OS-bound server port immediately after native
listen returns. Closed, unknown, foreign, offhost and PG destinations
remain blocked. Original failure evidence is retained; product code and
assertions were unchanged.
- The exact current UI policy command now reports zero findings (0.362
seconds); its seven scanner controls pass. The earlier dependent PR used
an old scanner/source baseline under a newly enforcing trusted workflow
and reported 397 findings. Carrying the genuine current master resolves
that mismatch without editing unrelated UI sites or relaxing the gate.
- Every meaningful worker checked its executing PID/thread/preload hash
and matching receipt. PG0 controls opened no PostgreSQL sockets; all
owned processes exited. Original dependency-cache/DNS, ignored-harness
syntax/type and guard failures remain separate from product results.
- This is fresh semantic and focused runtime qualification, not a local
whole-repository or current-head PostgreSQL acceptance rerun. The
upstream workspace-busy parked-retry/terminal-holder notification path
remains unchanged by this merge and needs its separate current-graph
producer check. Historical six-case/scale results below stay tied to
their measured sources. A matching controlled default-cadence follow-up
is in #19191; no fresh current-head two-cycle or deployed suspension
claim follows from this merge.
- All 59 exact-head check contexts now pass (55 success, one neutral,
three intentional skips), plus the legacy security status. The complete
edited Greptile review is 5/5 for this exact commit, with all
root/nested pages inspected and no unresolved threads. Ponytail stopped
without completing its new turn; it supplied no new verdict or finding.
Prior actual findings remain addressed. Earlier scores/checks below
belong to their explicit earlier commits.


On frozen clean merged source `699c8569fa`:

- Real PostgreSQL acceptance: **6/6 passed**,83.483 seconds wall. Actual
retry producer creates the exact saved run/wake; stopped and
reconstructed owner resumes through current admission, one durable
start, real local process output and normal task completion. Includes
cancellation, drain arrival, retained quota history with queued-run/wake
negatives, watchdog review settlement and budget-input
empty/pending/consumed control using the actual owner.
- Affected coordinator, deadline, scoped-deadline, recovery and
composed-proof controls: **78/78 in five files passed**,32.114 seconds
wall, worker 1/no file parallelism.
- `pnpm exec tsc --noEmit -p server/tsconfig.json`: **passed**,17.467
seconds. Native TypeScript 7.0.2 and Node 24.22.2.
- PostgreSQL tests use one dedicated test database, matching
fixture/admin URLs, pool limit four, one worker, and no embedded
fallback. Every run recorded unchanged source hashes and a clean
checkout.
- The eight-second real-clock retry is a domain execution check. It is
below the Cloud minimum wake lead and is not a positive accepted hosted
sleep cycle.
- The completed quota-history product failure and original
fixture/protocol failures were preserved; the owning-reader fix passed a
real PostgreSQL red/green regression before the fresh merged six-case
pass.
- After runtime completion, the dedicated database had zero other
clients and no owned runtime processes. All three qualification process
PIDs exited. The coordinator independently audits and drops the
database.
- Comment-only cleanup has equal strict TypeScript parser ASTs across 15
files. The final lexical audit finds no added comments relative to
actual master. `git diff --check` passed.
- Earlier retry fixtures omitted responsible-user membership, advanced
JavaScript time past the real PostgreSQL lease clock, or produced stdout
without normal task completion. Corrected fixtures use real membership,
an eight-second real deadline, and ordinary task comment/completion. The
last case exposed legitimate disposition repair rather than duplicate
retry promotion.
- The quota and watchdog controls first ran before admitted work or the
existing 30-second reconstruction duty settled. The stronger tests
require an interim blocked report and then exact settled-owner proof.
Production deadlines were not changed. The separate
completed-quota-history defect was fixed in its reader and verified with
linked unfinished-run and wake controls.
- The status/object and skill owners reviewed the actual final startup,
route and post-backup bindings. Their domain source evidence carries
where token-equivalent. The master coordinator coalescing change was
reviewed separately. Historical domain PostgreSQL results are not
presented as fresh full-suite results on this head.

The later master merge adds retry predecessor cleanup checks under the
existing issue claim lock. The source audit confirms unchanged saved
deadlines and run/wake identities. The three actual retry
lifecycle/cancellation/drain cases passed on the batched candidate with
this parent (31.817 seconds). Those local dependency controls preceded
the current exact-head remote run. Native backup preservation and
TestOps changes from master are retained. No pass is inferred for
changed dependency behavior solely from unchanged owned files.

Review repair published as `643f1e7400`:

- The original reader returned `unknown` after seven actual watchdog
reviews settled across seven companies. The unchanged composed-report
regression passed after batching; the final reader passed in 61.841
seconds, including active queued-work and stopped-owner rejection.
- A separate two-company red/green control rejects terminal-run context
from another company as first-run grace evidence. Shared facts are
serialized once, with per-root IDs and review summaries.
- The affected recovery, proof and diagnostic unit controls passed 47/47
(14.575 seconds). Startup owner-contract fixtures passed 121/121 (10.589
seconds).
- Diagnostic PostgreSQL controls passed7/7 (12.406 seconds), including
the normal budget-input producer and its exact blocker reason. Initial
empty-state setup uses the same cleanup as per-case teardown when a
dedicated test database is reused.
- Actual repository `pnpm exec tsc --noEmit -p server/tsconfig.json`
passed 12.710 seconds with no heap override. Final source has no added
code comments; whitespace checks pass.
- The original query-budget failure, two-company discriminator, initial
cross-suite fixture failure and an interrupted incorrect local-driver
selector are retained separately. The interrupted run is not a pass.
Independent cleanup found zero owned clients or runtime processes.

Quota-scale repair published as `6696a37f8a`:

- The exact 10,000-row completed-history case reproduced
`MAX_PARAMETERS_EXCEEDED`:90,002 bound parameters exceeded the driver's
65,534 limit, and the composed report returned `unknown` despite settled
owners. Original failure and actual driver diagnostics are retained.
- Fixed pending-work queries use three typed UUID-array parameters plus
the result limit, keeping exact company/issue/source tuple bindings. The
primary run-ID column stays uncast; a guarded CASE validates the raw
external reference before conversion. The 10,000-row cap and
64-query/two-second proof budget are unchanged.
- The same 10,000-row composed-report case passed 18.860 seconds, with
unfinished run/wake rejection, malformed-reference safety and
conservative 10,001-row overflow. Its fixture derives retained history
from the actual quota producer and normal task completion, expands valid
persisted issue/source pairs, then starts the actual registered owners.
- The existing actual quota producer case passed 14.187 seconds,
including exact future deadline, retained completion history, unfinished
work, real foreign-company source references and noncanonical/malformed
references.
- Affected owner/proof controls passed 22/22 (26.444 seconds).
Production server noEmit passed 16.039 seconds.
- CI on 643 identified TS7034/TS7005 in the settled-watchdog fixture's
initially untyped collection. It is now explicitly typed. A separate
affected-entry semantic check extending the repository's actual
`server/src/__tests__/tsconfig.json` passed 15.093 seconds: six affected
tests, two owner files, ambient declarations and current imported
source, with unchanged strict/module/lib/path settings. The production
server config excludes that test directory and is not presented as
test-layer proof. No custom suppression or replacement SDK stub was
added.
- All runs were sequential with pool max 4, worker 1,
source/config/command hashes and cleared inherited
provider/LIVE/other-database variables. Cleanup found zero owned
database clients or local runtime processes. At prior head
`6696a37f8a5718cd0f79054969a0084462d41d47`, all required CI gates pass.
The exact-head inventory has 55 successes, one neutral review and three
intentional skips. Greptile is 5/5. The complete latest Ponytail verdict
confirms the quota finding is fixed; its only remaining note is optional
unused MCP proof state, not a behavior defect. All three review threads
are resolved. The earlier 643 result remains historical.

The repository-wide typecheck, tests, build and browser gates passed in
the prior `6696a37f8a5718cd0f79054969a0084462d41d47` CI run. The new
`8c912e1ba4fc66397a7341ade59373af8075068f` full CI also passes. They
were not run as a full local workspace command in the focused PostgreSQL
lease. Local evidence above uses bounded external databases and does not
claim embedded-cluster, live-provider or deployed-alarm qualification.

## Risks

- An unsupported or uncertain owner state must remain blocked. An
optimistic exemption could lose work while compute is stopped.
- Changes during reconstruction or inspection must invalidate the proof.
A memory-only timer is not durable alarm coverage.
- Current authorization, budgets, cancellation, accounting, and resource
ownership still apply on dispatch. The proof does not grant execution
authority.
- The supported writer model uses in-process transaction signals and
startup/resume reconstruction. Independent SQL writers and other
replicas need an explicit wake contract.
- Native, review, coalesced, and taskless retry variants remain
conservative until their owner cases qualify. Reviewer quota and hosted
alarm qualification remain open.
- The shared catalog remains opt-in. Legacy callers and missing owner
bindings retain their old blockers.

## Model Used

OpenAI `gpt-6.1-sol` was the configured model for the delegated service
implementations and reviews. OpenAI Codex, based on GPT-6, performed
managing review and integration. Both used repository tools and code
execution. The exact served backend snapshots and context-window limits
were not exposed.

## 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 and contains no internal
Paperclip ticket id
- [x] I have run tests locally and they pass on the final merged head
- [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 — exact current-head run passes
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups —
complete current-head review and all paginated threads inspected
- [ ] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-11-06 13:48:55 -06:00

955 lines
68 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { resolveExternalTestDatabaseUrl } from './helpers/external-test-database.js';
import { applyPendingMigrations } from '@paperclipai/db';
import { reserveInitialSkillWork } from '../services/skill-source-import-budget.js';
import { admitDeferredSkillImports, lockSkillImportQueue, MAX_COMPANY_PENDING_SKILL_IMPORTS, MAX_PROJECT_PENDING_SKILL_IMPORTS, SKILL_IMPORT_CAPACITY_WAIT } from '../services/skill-source-import-queue.js';
import { randomUUID } from 'node:crypto';
import { promises as fs } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
import { and, eq, sql } from 'drizzle-orm';
import { activityLog, agents, authUsers, companies, companyMemberships, companySkillSources, companySkillImportBudgets, companySkills, companySkillVersions, createDb, projects,
projectWorkspaces, projectSkillSources, projectAccessMembers } from '@paperclipai/db';
import { startEmbeddedPostgresTestDatabase, getEmbeddedPostgresTestSupport } from './helpers/embedded-postgres.js';
import { githubFixture } from './helpers/github-skills.js';
import { enqueueProjectSkillSource, detachProjectSkillSource, projectSkillSetupIdentity } from '../services/project-skill-source-bindings.js';
import { adoptProjectSkillRepository, projectSkillRepositories, skillSourceSyncService } from '../services/skill-source-sync.js';
import { skillSourceService, type SkillSourceContext } from '../services/skill-sources.js';
import { companySkillService } from '../services/company-skills.js';
import { skillPackagePaths } from '../services/skill-package-store.js';
import { projectService } from '../services/projects.js';
import { skillAccessService, withSkillActor } from '../services/skill-access.js';
import type { AuthorizationActor } from '../services/authorization.js';
import { forbidden, unprocessable } from '../errors.js';
const externalUrl = resolveExternalTestDatabaseUrl('PAPERCLIP_PROJECT_SKILLS_TEST_DATABASE_URL');
const support = externalUrl ? { supported: true } : await getEmbeddedPostgresTestSupport();
describe.skipIf(!support.supported)('automatic project skill sources', () => {
let testDb: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
let db: ReturnType<typeof createDb>;
let home: string;
const oldHome = process.env.PAPERCLIP_HOME;
const companyId = randomUUID();
const md = (name: string) => `---\nname: ${name}\ndescription: Test workflow\n---\nFollow the workflow.\n`;
let files: Record<string, string> = {};
let commit = 'a'.repeat(40);
const downloads = vi.fn();
const context: SkillSourceContext = { actor: { type: 'system' }, setup: { userId: null, localTrusted: true },
read: () => {
const fixture = githubFixture(files, {}, commit), open = fixture.openSnapshot;
fixture.openSnapshot = async (...args) => { downloads(); return open(...args); };
return fixture;
}, authorize: async () => {}, audit: async () => {} };
beforeAll(async () => {
if (externalUrl) await applyPendingMigrations(externalUrl);
else testDb = await startEmbeddedPostgresTestDatabase('paperclip-project-skills-');
db = createDb(externalUrl ?? testDb.connectionString, { maxConnections: 4 });
home = await fs.mkdtemp(path.join(os.tmpdir(), 'paperclip-project-skills-home-'));
process.env.PAPERCLIP_HOME = home;
await db.insert(companies).values({ id: companyId, name: 'Project skills', issuePrefix: 'PSK' });
}, 30000);
it('rechecks the locked source ref when branch repair races with project attachment', async () => {
const companyId = randomUUID();
await db.insert(companies).values({ id: companyId, name: 'Branch repair race', issuePrefix: 'RCE' });
const content = { 'race/SKILL.md': md('race') };
const sourceContext = { ...context, read: () => githubFixture(content) };
const service = skillSourceService(db);
const created = await service.create(companyId, { repositoryUrl: 'https://github.com/acme/skills', trackingRef: 'configured-branch',
commitSha: 'a'.repeat(40), selectedPaths: ['race/SKILL.md'] }, sourceContext);
const [project] = await db.insert(projects).values({ companyId, name: 'Configured branch' }).returning();
const [row] = await db.insert(projectWorkspaces).values({ companyId, projectId: project!.id, name: 'Repository',
repoUrl: created.source.repositoryUrl, defaultRef: 'configured-branch' }).returning();
await enqueueProjectSkillSource(db, row!, { refreshUserId: null, refreshAgentId: null, refreshLocalTrusted: true, connectionId: null, refreshGrantId: null });
let importChecks = 0;
const worker = skillSourceSyncService(db, { context: async () => ({ ...sourceContext, authorize: async action => {
// The second authorization occurs after matching the existing source,
// before the worker locks it for attachment.
if (action === 'skills.import' && ++importChecks === 2) await service.settings(companyId, created.source.id,
{ revision: created.source.revision, trackingRef: 'HEAD' }, sourceContext);
} }) });
try {
await worker.tick();
const [blocked] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, row!.id));
expect(blocked!.sourceId).toBeNull();
expect(blocked!.status).toBe('error');
expect(blocked!.lastError).toContain('branch changed');
await worker.tick(new Date(Date.now() + 6 * 60_000));
const [attached] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, row!.id));
expect(attached!.sourceId).not.toBe(created.source.id);
expect((await service.detail(companyId, attached!.sourceId!)).trackingRef).toBe('configured-branch');
} finally {
await worker.stop();
await db.update(companies).set({ status: 'archived' }).where(eq(companies.id, companyId));
}
});
it('requires explicit repair before an agent-created repository can search a person’s GitHub grants', async () => {
const row = await workspace('agent-repair', 'open');
const binding = await enqueueProjectSkillSource(db, row);
const [agent] = await db.insert(agents).values({ companyId, name: 'Agent repository setup', adapterType: 'codex_local', status: 'idle' }).returning();
await db.update(projectSkillSources).set({ refreshUserId: 'local-board', refreshAgentId: agent!.id, refreshLocalTrusted: true }).where(eq(projectSkillSources.id, binding!.id));
const provider = vi.spyOn(globalThis, 'fetch');
try {
await skillSourceSyncService(db).tick();
const [saved] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.id, binding!.id));
expect(saved!.lastError).toMatch(/Choose a GitHub connection/);
expect(saved!.active).toBe(false);
expect(provider).not.toHaveBeenCalled();
} finally { provider.mockRestore(); }
});
afterAll(async () => {
if (oldHome === undefined) delete process.env.PAPERCLIP_HOME; else process.env.PAPERCLIP_HOME = oldHome;
await db?.$client.end({ timeout: 1 });
await testDb?.cleanup();
if (home) await fs.rm(home, { recursive: true, force: true });
});
async function workspace(ref: string, visibility = 'open') {
const [project] = await db.insert(projects).values({ companyId, name: ref, visibility }).returning();
const [row] = await db.insert(projectWorkspaces).values({ companyId, projectId: project!.id, name: ref,
repoUrl: 'https://github.com/acme/skills', defaultRef: ref }).returning();
return row!;
}
const actor = (userId: string): AuthorizationActor => ({ type: 'board', userId, source: 'session', companyIds: [companyId], ignoreInstanceAdmin: true });
it('bounds per-project pending imports and admits deferred work after capacity frees', async () => {
await db.update(projectSkillSources).set({ active: false, lastError: null });
const root = await workspace('quota-root');
const rows = [];
for (let index = 0; index <= MAX_PROJECT_PENDING_SKILL_IMPORTS; index++) {
const [row] = await db.insert(projectWorkspaces).values({ companyId, projectId: root.projectId, name: `Repository ${index}`,
repoUrl: root.repoUrl, defaultRef: `project-quota-${index}` }).returning();
rows.push(row!); await enqueueProjectSkillSource(db, row!);
}
const pending = await db.select().from(projectSkillSources).where(eq(projectSkillSources.projectId, root.projectId));
expect(pending.filter(row => row.active)).toHaveLength(MAX_PROJECT_PENDING_SKILL_IMPORTS);
expect(pending.filter(row => !row.active)).toEqual([expect.objectContaining({ status: 'pending', lastError: SKILL_IMPORT_CAPACITY_WAIT })]);
await detachProjectSkillSource(db, companyId, rows[0]!.id);
await admitDeferredSkillImports(db, companyId, new Date());
const [admitted] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, rows.at(-1)!.id));
expect(admitted).toMatchObject({ active: true, lastError: null });
const [extra] = await db.insert(projectWorkspaces).values({ companyId, projectId: root.projectId, name: 'Detached waiting repository', repoUrl: root.repoUrl, defaultRef: 'quota-detached' }).returning();
await enqueueProjectSkillSource(db, extra!);
await detachProjectSkillSource(db, companyId, extra!.id);
await detachProjectSkillSource(db, companyId, rows[1]!.id);
await admitDeferredSkillImports(db, companyId, new Date());
const [detached] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, extra!.id));
expect(detached!.active).toBe(false);
await db.update(projectSkillSources).set({ active: false, lastError: null });
});
it('does not reactivate a deferred import detached after admission selection', async () => {
const row = await workspace('deferred-removal-race');
const binding = await enqueueProjectSkillSource(db, row);
await db.update(projectSkillSources).set({ active: false, lastError: SKILL_IMPORT_CAPACITY_WAIT }).where(eq(projectSkillSources.id, binding!.id));
let detached = false;
const database = new Proxy(db, { get(target, key) {
if (key !== 'transaction') return Reflect.get(target, key);
return (callback: (tx: unknown) => Promise<unknown>) => target.transaction(async tx => callback(new Proxy(tx, { get(target, key) {
if (key !== 'update') return Reflect.get(target, key);
return (table: unknown) => {
const update = target.update(table as typeof projectSkillSources);
if (table !== projectSkillSources) return update;
return { set: (values: Parameters<typeof update.set>[0]) => {
const set = update.set(values);
return { where: async (condition: Parameters<typeof set.where>[0]) => {
if (!detached) { detached = true; await detachProjectSkillSource(db, companyId, row.id); }
return set.where(condition);
} };
} };
};
} })));
} });
await admitDeferredSkillImports(database, companyId, new Date());
const [saved] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.id, binding!.id));
expect(detached).toBe(true);
expect(saved!.active).toBe(false);
expect(saved!.lastError).toBeNull();
});
it('bounds the company pending queue across projects without losing deferred provenance', async () => {
const projectsToInsert = Array.from({ length: MAX_COMPANY_PENDING_SKILL_IMPORTS + 1 }, (_, index) => ({ id: randomUUID(), companyId, name: `Quota project ${index}` }));
await db.insert(projects).values(projectsToInsert);
const workspaces = await db.insert(projectWorkspaces).values(projectsToInsert.map((project, index) => ({ companyId, projectId: project.id,
name: 'Repository', repoUrl: 'https://github.com/acme/skills', defaultRef: `company-quota-${index}` }))).returning();
for (const row of workspaces) await enqueueProjectSkillSource(db, row);
const pending = await db.select().from(projectSkillSources).where(and(eq(projectSkillSources.companyId, companyId), eq(projectSkillSources.active, true)));
expect(pending.filter(row => !row.sourceId)).toHaveLength(MAX_COMPANY_PENDING_SKILL_IMPORTS);
const deferred = await db.select().from(projectSkillSources).where(eq(projectSkillSources.lastError, SKILL_IMPORT_CAPACITY_WAIT));
expect(deferred).toHaveLength(1);
expect(deferred[0]!.workspaceId).toBeTruthy();
await db.update(projectSkillSources).set({ active: false, lastError: null });
});
it('admits another project beyond a full project’s oldest waiting batch', async () => {
const full = await workspace('full-admission-project');
const other = await workspace('other-admission-project');
const rows = await db.insert(projectWorkspaces).values(Array.from({ length: 120 }, (_, index) => ({ companyId, projectId: full.projectId,
name: `Queued ${index}`, repoUrl: full.repoUrl, defaultRef: `starvation-${index}` }))).returning();
await db.insert(projectSkillSources).values(rows.map((row, index) => ({ companyId, projectId: row.projectId, workspaceId: row.id,
repositoryUrl: row.repoUrl!, trackingRef: row.defaultRef!, active: index < 20,
lastError: index < 20 ? null : SKILL_IMPORT_CAPACITY_WAIT, createdAt: new Date(0) })));
const [waiting] = await db.insert(projectSkillSources).values({ companyId, projectId: other.projectId, workspaceId: other.id,
repositoryUrl: other.repoUrl!, trackingRef: other.defaultRef!, active: false, lastError: SKILL_IMPORT_CAPACITY_WAIT }).returning();
await admitDeferredSkillImports(db, companyId, new Date());
const [saved] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.id, waiting!.id));
expect(saved!.active).toBe(true);
await db.update(projectSkillSources).set({ active: false, lastError: null });
});
it('adoption waits for the company queue before taking repository binding locks', async () => {
const row = await workspace('adoption-lock-order');
const binding = await enqueueProjectSkillSource(db, row);
const [source] = await db.insert(companySkillSources).values({ companyId, repositoryUrl: row.repoUrl!, fullName: 'acme/skills', trackingRef: row.defaultRef! }).returning();
await db.update(projectSkillSources).set({ sourceId: source!.id }).where(eq(projectSkillSources.id, binding!.id));
let unlock!: () => void, locked!: () => void;
const release = new Promise<void>(resolve => { unlock = resolve; });
const ready = new Promise<void>(resolve => { locked = resolve; });
let editTx!: typeof db;
const edit = db.transaction(async tx => { editTx = tx as unknown as typeof db; await lockSkillImportQueue(editTx, companyId); locked(); await release; });
await ready;
const adoption = withSkillActor({ type: 'board', source: 'local_implicit', companyIds: [companyId] },
() => adoptProjectSkillRepository(db, companyId, row.id, context.authorize));
try {
await vi.waitFor(async () => {
const result = await db.execute(sql`select 1 from pg_stat_activity where wait_event_type = 'Lock' and query ilike '%companies%for no key update%'`);
expect(result.length).toBeGreaterThan(0);
});
// A repository edit holding the company lock must still be able to lock
// this binding. The old inverse adoption order fails with lock_not_available.
await editTx.execute(sql`select id from project_skill_sources where id = ${binding!.id} for update nowait`);
} finally { unlock(); await edit; await adoption; }
});
it('meters failed initial downloads across workers and retries before provider reads', async () => {
const budgetCompany = randomUUID();
await db.insert(companies).values({ id: budgetCompany, name: 'Initial work credits', issuePrefix: 'IWC' });
const [project] = await db.insert(projects).values({ companyId: budgetCompany, name: 'Repeated failed downloads' }).returning();
const rows = await db.insert(projectWorkspaces).values(Array.from({ length: 5 }, (_, index) => ({ companyId: budgetCompany,
projectId: project!.id, name: `Failed ${index}`, repoUrl: 'https://github.com/acme/skills', defaultRef: `failed-${index}` }))).returning();
for (const row of rows) await enqueueProjectSkillSource(db, row);
const reads = vi.fn(() => {
const reader = githubFixture({});
reader.openSnapshot = async () => { throw unprocessable('Git download exceeds 128 MiB.'); };
return reader;
});
const factory = async (setup: { companyId: string }) => setup.companyId === budgetCompany ? { ...context, read: reads } : context;
const now = new Date();
try {
await skillSourceSyncService(db, { context: factory }).tick(now);
expect(reads).toHaveBeenCalledTimes(4);
const [budget] = await db.select().from(companySkillImportBudgets).where(eq(companySkillImportBudgets.companyId, budgetCompany));
expect(budget!.availableBytes).toBe(0);
const worker = skillSourceSyncService(db, { context: factory });
await worker.tick(new Date(now.getTime() + 5 * 60_000));
expect(reads).toHaveBeenCalledTimes(4);
const waiting = await db.select().from(projectSkillSources).where(eq(projectSkillSources.companyId, budgetCompany));
expect(waiting.every(row => row.active && row.status === 'error' && row.lastError?.includes('limited'))).toBe(true);
await worker.tick(new Date(now.getTime() + 15 * 60_000));
expect(reads).toHaveBeenCalledTimes(5);
} finally { await db.update(companies).set({ status: 'archived' }).where(eq(companies.id, budgetCompany)); }
});
it('refunds measured small initial imports while completed jobs keep a company budget', async () => {
const budgetCompany = randomUUID();
await db.insert(companies).values({ id: budgetCompany, name: 'Small initial imports', issuePrefix: 'SII' });
const [project] = await db.insert(projects).values({ companyId: budgetCompany, name: 'Small repositories' }).returning();
const rows = await db.insert(projectWorkspaces).values(Array.from({ length: 6 }, (_, index) => ({ companyId: budgetCompany,
projectId: project!.id, name: `Small ${index}`, repoUrl: 'https://github.com/acme/skills', defaultRef: `small-${index}` }))).returning();
for (const row of rows) await enqueueProjectSkillSource(db, row);
const opened = vi.fn();
const reads = vi.fn(() => {
const reader = githubFixture({}), open = reader.openSnapshot!;
reader.openSnapshot = async (...args) => { opened(); return open(...args); };
return reader;
});
try {
await skillSourceSyncService(db, { context: async setup => setup.companyId === budgetCompany ? { ...context, read: reads } : context }).tick();
const bindings = await db.select().from(projectSkillSources).where(eq(projectSkillSources.companyId, budgetCompany));
expect(bindings.every(row => row.status === 'ready' && row.sourceId)).toBe(true);
expect(opened).toHaveBeenCalledTimes(6);
const [budget] = await db.select().from(companySkillImportBudgets).where(eq(companySkillImportBudgets.companyId, budgetCompany));
expect(budget!.availableBytes).toBeGreaterThan(500 * 1024 * 1024);
} finally { await db.update(companies).set({ status: 'archived' }).where(eq(companies.id, budgetCompany)); }
});
it('does not replenish initial work credits when large measured jobs complete', async () => {
const budgetCompany = randomUUID();
await db.insert(companies).values({ id: budgetCompany, name: 'Completed work credits', issuePrefix: 'CWC' });
const [project] = await db.insert(projects).values({ companyId: budgetCompany, name: 'Large measured imports' }).returning();
const rows = await db.insert(projectWorkspaces).values(Array.from({ length: 5 }, (_, index) => ({ companyId: budgetCompany,
projectId: project!.id, name: `Measured ${index}`, repoUrl: 'https://github.com/acme/skills', defaultRef: `measured-${index}` }))).returning();
for (const row of rows) await enqueueProjectSkillSource(db, row);
const opened = vi.fn();
const reads = vi.fn(() => {
const reader = githubFixture({}), open = reader.openSnapshot!;
reader.openSnapshot = async (...args) => { opened(); return { ...await open(...args), downloadBytes: 128 * 1024 * 1024 }; };
return reader;
});
try {
await skillSourceSyncService(db, { context: async setup => setup.companyId === budgetCompany ? { ...context, read: reads } : context }).tick();
const bindings = await db.select().from(projectSkillSources).where(eq(projectSkillSources.companyId, budgetCompany));
expect(bindings.filter(row => row.status === 'ready' && row.sourceId)).toHaveLength(4);
expect(bindings.filter(row => row.status === 'error' && row.lastError?.includes('limited'))).toHaveLength(1);
expect(opened).toHaveBeenCalledTimes(4);
const [budget] = await db.select().from(companySkillImportBudgets).where(eq(companySkillImportBudgets.companyId, budgetCompany));
expect(budget!.availableBytes).toBe(0);
} finally { await db.update(companies).set({ status: 'archived' }).where(eq(companies.id, budgetCompany)); }
});
it('never counts refill time twice when an older sweep reserves after a newer one', async () => {
const row = await workspace('monotonic-initial-work');
const binding = await enqueueProjectSkillSource(db, row);
const token = randomUUID();
await db.update(projectSkillSources).set({ leaseToken: token }).where(eq(projectSkillSources.id, binding!.id));
await db.delete(companySkillImportBudgets).where(eq(companySkillImportBudgets.companyId, companyId));
const older = new Date(), newer = new Date(older.getTime() + 15 * 60_000);
try {
await reserveInitialSkillWork(db, binding!, token, newer);
await reserveInitialSkillWork(db, binding!, token, older);
const [afterOlder] = await db.select().from(companySkillImportBudgets).where(eq(companySkillImportBudgets.companyId, companyId));
expect(afterOlder!.updatedAt).toEqual(newer);
await reserveInitialSkillWork(db, binding!, token, newer);
const [saved] = await db.select().from(companySkillImportBudgets).where(eq(companySkillImportBudgets.companyId, companyId));
expect(saved!.availableBytes).toBe(128 * 1024 * 1024);
} finally {
await detachProjectSkillSource(db, companyId, row.id);
await db.delete(companySkillImportBudgets).where(eq(companySkillImportBudgets.companyId, companyId));
}
});
it('does not adopt a stale repository after an editor commits a new selection', async () => {
const row = await workspace('adoption-stale-selection');
await enqueueProjectSkillSource(db, row);
try {
await withSkillActor({ type: 'board', source: 'local_implicit', companyIds: [companyId] }, async () => {
await expect(adoptProjectSkillRepository(db, companyId, row.id, async () => {
await projectService(db).updateWorkspace(row.projectId, row.id, { repoUrl: 'https://github.com/acme/skills-next', defaultRef: 'edited-branch' });
})).rejects.toThrow(/repository changed/i);
});
const saved = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, row.id));
expect(saved.find(binding => binding.trackingRef === 'edited-branch')!.active).toBe(true);
expect(saved.find(binding => binding.trackingRef === row.defaultRef)!.active).toBe(false);
} finally { await detachProjectSkillSource(db, companyId, row.id); }
});
it('does not read GitHub or retry an initial job denied by import policy', async () => {
await db.update(companySkillSources).set({ automaticUpdates: false });
const row = await workspace('denied-preflight');
await enqueueProjectSkillSource(db, row);
const read = vi.fn(context.read);
const worker = skillSourceSyncService(db, { context: async () => ({ ...context, read,
authorize: async () => { throw forbidden('Skill import denied by policy.'); } }) });
expect((await worker.tick()).imported).toBe(0);
expect(read).not.toHaveBeenCalled();
const [blocked] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, row.id));
expect(blocked).toMatchObject({ active: false, status: 'error', sourceId: null });
await worker.tick(new Date(Date.now() + 60 * 60_000));
expect(read).not.toHaveBeenCalled();
});
it('does not add readers when an import overlaps private source removal', async () => {
files = { 'race/SKILL.md': md('race workflow') };
const originalWorkspace = await workspace('removal-race', 'private');
await enqueueProjectSkillSource(db, originalWorkspace);
await skillSourceSyncService(db, { context: async () => context }).tick();
const [origin] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, originalWorkspace.id));
const sources = skillSourceService(db), skills = companySkillService(db);
const source = await sources.detail(companyId, origin!.sourceId!);
const skillId = source.entries[0]!.skillId!;
const pendingWorkspace = await workspace('removal-race', 'open');
await enqueueProjectSkillSource(db, pendingWorkspace);
let removed = false, authorizations = 0;
const racingContext = { ...context, authorize: async () => {
if (++authorizations !== 2 || removed) return;
removed = true;
await sources.disconnect(companyId, source.id, context, { revision: source.revision, skills: 'copy' });
await skills.updateFile(companyId, skillId, 'SKILL.md', md('Restricted race edits'));
} };
const worker = skillSourceSyncService(db, { context: async () => racingContext });
expect((await worker.tick()).imported).toBe(0);
const [pending] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, pendingWorkspace.id));
expect(pending).toMatchObject({ sourceId: null, status: 'error' });
const readerId = randomUUID();
await db.insert(authUsers).values({ id: readerId, name: 'Open reader', email: `${readerId}@example.test`, createdAt: new Date(), updatedAt: new Date() });
await db.insert(companyMemberships).values({ companyId, principalType: 'user', principalId: readerId, membershipRole: 'member', status: 'active' });
await withSkillActor(actor(readerId), async () => {
expect(await skills.getById(companyId, skillId)).toBeNull();
expect(await skills.readFile(companyId, skillId, 'SKILL.md')).toBeNull();
});
expect((await worker.tick(new Date(Date.now() + 6 * 60_000))).imported).toBe(1);
const [retried] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, pendingWorkspace.id));
expect(retried!.sourceId).not.toBe(source.id);
});
it('imports after durable enqueue, imports later additions and avoids unchanged downloads', async () => {
files = { 'one/SKILL.md': md('one') }; commit = 'a'.repeat(40); downloads.mockClear();
const row = await workspace('automatic');
const binding = await enqueueProjectSkillSource(db, row);
await enqueueProjectSkillSource(db, row);
expect(await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, row.id))).toHaveLength(1);
expect((await db.select().from(companySkillSources)).filter(source => source.trackingRef === 'automatic')).toHaveLength(0);
const worker = skillSourceSyncService(db, { context: async () => context });
expect((await worker.tick()).imported).toBe(1);
const [ready] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.id, binding!.id));
const sourceId = ready!.sourceId!;
const first = await skillSourceService(db).detail(companyId, sourceId);
expect(first.accessScope).toBe('projects');
expect(first.importNewSkills).toBe(true);
const installedId = first.entries[0]!.skillId!;
const original = (await companySkillService(db).getById(companyId, installedId))!;
expect(original.accessSourceId).toBe(sourceId);
const downloadCount = downloads.mock.calls.length;
await worker.tick(new Date(Date.now() + 6 * 60_000));
expect(downloads).toHaveBeenCalledTimes(downloadCount);
files['one/SKILL.md'] = md('one improved'); files['two/SKILL.md'] = md('two'); commit = 'b'.repeat(40);
await worker.tick(new Date(Date.now() + 6 * 60_000));
const updated = await skillSourceService(db).detail(companyId, sourceId);
expect(updated.entries.filter(entry => entry.skillId)).toHaveLength(2);
const latest = (await companySkillService(db).getById(companyId, installedId))!;
expect(latest.id).toBe(original.id);
expect(latest.currentVersionId).not.toBe(original.currentVersionId);
const pinned = await companySkillService(db).getVersion(companyId, installedId, original.currentVersionId!);
expect(pinned!.fileInventory.find(file => file.path === 'SKILL.md')!.content).toBe(md('one'));
await detachProjectSkillSource(db, companyId, row.id);
const before = downloads.mock.calls.length;
await worker.tick(new Date(Date.now() + 6 * 60_000));
expect(downloads).toHaveBeenCalledTimes(before);
expect(await companySkillService(db).getById(companyId, installedId)).not.toBeNull();
expect(await skillSourceService(db).detail(companyId, sourceId)).toMatchObject({ trackingActive: false, syncState: 'paused' });
});
it('watches empty repositories, retains folder exclusions and coalesces worker sweeps', async () => {
files = {}; commit = 'c'.repeat(40);
const row = await workspace('empty');
await enqueueProjectSkillSource(db, row);
const worker = skillSourceSyncService(db, { context: async () => context });
const first = worker.tick();
expect(worker.tick()).toBe(first);
await first;
const [binding] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, row.id));
expect(binding!.status).toBe('ready');
const service = skillSourceService(db);
const source = await service.detail(companyId, binding!.sourceId!);
await service.refresh(companyId, source.id, context, { revision: source.revision, selectedPaths: [], excludedFolders: ['excluded'] });
files = { 'excluded/secret/SKILL.md': md('excluded'), 'new/SKILL.md': md('new') }; commit = 'd'.repeat(40);
await worker.tick(new Date(Date.now() + 6 * 60_000));
const result = await service.detail(companyId, source.id);
expect(result.entries.find(entry => entry.path === 'new/SKILL.md')!.skillId).toBeTruthy();
expect(result.entries.find(entry => entry.path.includes('secret'))!.selection).toBe('excluded');
});
it('inherits either project access, rechecks revocation and fails closed after project deletion', async () => {
files = { 'private/SKILL.md': md('private workflow') }; commit = 'e'.repeat(40);
const row = await workspace('private', 'private');
await enqueueProjectSkillSource(db, row);
await skillSourceSyncService(db, { context: async () => context }).tick();
const [binding] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, row.id));
expect(binding!.status, binding!.lastError ?? 'initial private binding').toBe('ready');
const source = await skillSourceService(db).detail(companyId, binding!.sourceId!);
const skillId = source.entries[0]!.skillId!;
let privateSnapshotPath = '';
let privatePackagePath = '';
const privateDerivatives: string[] = [];
for (const user of ['alice', 'bob']) {
await db.insert(authUsers).values({ id: user, name: user, email: `${user}@example.test`, createdAt: new Date(), updatedAt: new Date() });
await db.insert(companyMemberships).values({ companyId, principalType: 'user', principalId: user, membershipRole: 'member', status: 'active' });
}
await db.insert(projectAccessMembers).values({ companyId, projectId: row.projectId, subjectType: 'user', subjectId: 'alice' });
await withSkillActor(actor('alice'), async () => {
expect(await companySkillService(db).getById(companyId, skillId)).not.toBeNull();
expect((await companySkillService(db).list(companyId)).some(skill => skill.id === skillId)).toBe(true);
const skill = (await companySkillService(db).getById(companyId, skillId))!;
const [runtime] = await companySkillService(db).listRuntimeSkillEntries(companyId, { onlyKeys: new Set([skill.key]) });
privateSnapshotPath = runtime!.source;
await expect(companySkillService(db).importFromSource(companyId, runtime!.source)).rejects.toThrow(/Project-derived skill/);
const [version] = await db.select({ packageId: companySkillVersions.packageSnapshotId })
.from(companySkillVersions).where(eq(companySkillVersions.id, skill.currentVersionId!));
expect(version?.packageId).toBeTruthy();
privatePackagePath = skillPackagePaths(companyId, version!.packageId!).directory;
expect((await fs.stat(privatePackagePath)).isDirectory()).toBe(true);
for (const candidate of [privatePackagePath, path.join(privatePackagePath, 'private'), path.dirname(privatePackagePath)]) {
await expect(companySkillService(db).importFromSource(companyId, candidate)).rejects.toMatchObject({
status: 422, details: { code: 'skill_package_cache_import_denied' },
});
}
const fork = await companySkillService(db).forkSkill(companyId, skillId, { slug: 'private-copy' });
expect(fork.skill.accessSourceId).not.toBe(source.id);
expect(fork.skill.accessSourceId).toBeTruthy();
privateDerivatives.push(fork.skill.id);
await expect(companySkillService(db).updateSkill(companyId, fork.skill.id, { sharingScope: 'public_link' })).rejects.toThrow(/public link/);
const copy = await companySkillService(db).createLocalSkill(companyId, { name: 'Private derived copy', forkedFromSkillId: skillId });
expect(copy.accessSourceId).not.toBe(source.id);
expect(copy.accessSourceId).toBeTruthy();
privateDerivatives.push(copy.id);
await expect(companySkillService(db).createLocalSkill(companyId, { name: 'Unsafe publication', forkedFromSkillId: skillId, sharingScope: 'public_link' })).rejects.toThrow(/public link/);
});
await withSkillActor(actor('bob'), async () => {
const branchRead = vi.fn(context.read(null));
await expect(skillSourceService(db).branches(companyId, source.id, {}, { ...context, read: () => branchRead })).rejects.toThrow(/not found/);
expect(branchRead).not.toHaveBeenCalled();
await expect(companySkillService(db).importFromSource(companyId, privateSnapshotPath)).rejects.toThrow(/not found/);
await expect(companySkillService(db).importFromSource(companyId, privatePackagePath)).rejects.toMatchObject({
status: 422, details: { code: 'skill_package_cache_import_denied' },
});
expect(await companySkillService(db).getById(companyId, skillId)).toBeNull();
expect(await companySkillService(db).readFile(companyId, skillId, 'SKILL.md')).toBeNull();
expect((await skillSourceService(db).list(companyId)).some(item => item.id === source.id)).toBe(false);
await expect(skillAccessService(db).assertSkill(companyId, skillId)).rejects.toThrow(/not found/);
});
const [reader] = await db.insert(agents).values({ companyId, name: 'Private skill reader', adapterType: 'codex_local', status: 'idle' }).returning();
const [outsider] = await db.insert(agents).values({ companyId, name: 'Unassigned reader', adapterType: 'codex_local', status: 'idle' }).returning();
for (const agent of [reader!, outsider!]) await db.insert(companyMemberships).values({ companyId, principalType: 'agent', principalId: agent.id, membershipRole: 'member', status: 'active' });
await db.insert(projectAccessMembers).values({ companyId, projectId: row.projectId, subjectType: 'agent', subjectId: reader!.id });
const agentActor = (agentId: string, userId: string): AuthorizationActor => ({
type: 'agent', source: 'agent_jwt', companyId, agentId, runId: randomUUID(), onBehalfOfUserId: userId,
});
await withSkillActor(agentActor(reader!.id, 'alice'), async () => {
expect(await companySkillService(db).readFile(companyId, skillId, 'SKILL.md')).not.toBeNull();
const skill = (await companySkillService(db).getById(companyId, skillId))!;
expect(await companySkillService(db).getVersion(companyId, skillId, skill.currentVersionId!)).not.toBeNull();
expect((await companySkillService(db).listRuntimeSkillEntries(companyId)).some(entry => entry.key === skill.key)).toBe(true);
});
for (const denied of [agentActor(reader!.id, 'bob'), agentActor(outsider!.id, 'alice'), { ...agentActor(reader!.id, 'alice'), companyId: randomUUID() }]) {
await withSkillActor(denied, async () => {
const branchRead = vi.fn(context.read(null));
await expect(skillSourceService(db).branches(companyId, source.id, {}, { ...context, read: () => branchRead })).rejects.toThrow(/not found/);
expect(branchRead).not.toHaveBeenCalled();
expect(await companySkillService(db).readFile(companyId, skillId, 'SKILL.md')).toBeNull();
expect((await companySkillService(db).listRuntimeSkillEntries(companyId)).some(entry => entry.key.includes('private'))).toBe(false);
});
}
const second = await workspace('private', 'private');
await db.insert(projectAccessMembers).values({ companyId, projectId: second.projectId, subjectType: 'user', subjectId: 'bob' });
await enqueueProjectSkillSource(db, second);
await skillSourceSyncService(db, { context: async () => context }).tick();
const [linked] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, second.id));
expect(linked!.status, linked!.lastError ?? 'second project binding').toBe('ready');
expect(linked!.sourceId).toBe(source.id);
await withSkillActor(actor('bob'), async () => expect(await companySkillService(db).getById(companyId, skillId)).not.toBeNull());
await db.delete(projectAccessMembers).where(and(eq(projectAccessMembers.projectId, second.projectId), eq(projectAccessMembers.subjectId, 'bob')));
await withSkillActor(actor('bob'), async () => expect(await companySkillService(db).getById(companyId, skillId)).toBeNull());
const opened = await workspace('private', 'open');
await enqueueProjectSkillSource(db, opened);
await skillSourceSyncService(db, { context: async () => context }).tick();
await withSkillActor(actor('bob'), async () => {
expect(await companySkillService(db).getById(companyId, skillId)).not.toBeNull();
for (const derivativeId of privateDerivatives) {
expect(await companySkillService(db).getById(companyId, derivativeId)).toBeNull();
expect(await companySkillService(db).readFile(companyId, derivativeId, 'SKILL.md')).toBeNull();
}
});
await withSkillActor(agentActor(reader!.id, 'bob'), async () => {
expect(await companySkillService(db).getById(companyId, skillId)).not.toBeNull();
for (const derivativeId of privateDerivatives) expect(await companySkillService(db).getById(companyId, derivativeId)).toBeNull();
});
await db.delete(projects).where(eq(projects.id, row.projectId));
await db.delete(projects).where(eq(projects.id, second.projectId));
await withSkillActor(actor('alice'), async () => {
for (const id of privateDerivatives) expect(await companySkillService(db).getById(companyId, id)).toBeNull();
});
await db.delete(projects).where(eq(projects.id, opened.projectId));
await withSkillActor(actor('alice'), async () => expect(await companySkillService(db).getById(companyId, skillId)).toBeNull());
expect((await db.select().from(companySkills).where(eq(companySkills.id, skillId)))[0]!.accessSourceId).toBe(source.id);
});
it.each(['copy', 'delete'] as const)('removes project tracking with %s while retaining dynamic copy/fork access', async choice => {
files = { 'private/SKILL.md': md('retained private workflow') }; commit = '9'.repeat(40);
const row = await workspace(`removed-${choice}`, 'private');
await enqueueProjectSkillSource(db, row);
const worker = skillSourceSyncService(db, { context: async () => context });
await worker.tick();
const [binding] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, row.id));
const service = skillSourceService(db), skills = companySkillService(db);
const source = await service.detail(companyId, binding!.sourceId!);
const original = (await skills.getById(companyId, source.entries[0]!.skillId!))!;
const userId = randomUUID();
await db.insert(authUsers).values({ id: userId, name: 'Source owner', email: `${userId}@example.test`, createdAt: new Date(), updatedAt: new Date() });
await db.insert(companyMemberships).values({ companyId, principalType: 'user', principalId: userId, membershipRole: 'member', status: 'active' });
await db.insert(projectAccessMembers).values({ companyId, projectId: row.projectId, subjectType: 'user', subjectId: userId });
await withSkillActor(actor(userId), async () => {
const fork = await skills.forkSkill(companyId, original.id, { slug: `retained-${choice}-fork` });
await skills.updateFile(companyId, fork.skill.id, 'SKILL.md', md('Private fork edits') + '\nConfidential original-project notes.\n');
const result = await service.disconnect(companyId, source.id, context, { revision: source.revision, skills: choice });
expect(result).toMatchObject({ copiedCount: choice === 'copy' ? 1 : 0, deletedCount: choice === 'delete' ? 1 : 0 });
await expect(service.detail(companyId, source.id)).rejects.toThrow(/not found/);
expect((await service.list(companyId)).some(item => item.id === source.id)).toBe(false);
expect((await projectSkillRepositories(db, companyId, actor(userId))).find(item => item.workspaceId === row.id)).toMatchObject({ sourceId: null, status: 'available' });
expect(await skills.getById(companyId, fork.skill.id)).not.toBeNull();
let retainedVersionId = original.currentVersionId;
if (choice === 'copy') {
expect(await skills.getById(companyId, original.id)).toMatchObject({ sourceType: 'managed', sourceLocator: null, accessSourceId: source.id });
expect((await skills.readFile(companyId, original.id, '.paperclip-repository/private/SKILL.md'))?.content).toBe(md('retained private workflow'));
expect((await skills.readFile(companyId, original.id, 'SKILL.md'))?.content).toContain('.paperclip-repository/private/SKILL.md');
await skills.updateFile(companyId, original.id, 'SKILL.md', md('Private copy edits') + '\nConfidential original-project notes.\n');
retainedVersionId = (await skills.createVersion(companyId, original.id, { label: 'Private edited copy' })).id;
} else expect(await skills.getById(companyId, original.id)).toBeNull();
await db.update(companySkillSources).set({ automaticUpdates: false }).where(eq(companySkillSources.companyId, companyId));
const before = downloads.mock.calls.length;
await worker.tick(new Date(Date.now() + 6 * 60_000));
expect(downloads).toHaveBeenCalledTimes(before);
// Reattachment retains disconnection; adoption is the deliberate restart action.
await enqueueProjectSkillSource(db, row);
await worker.tick(new Date(Date.now() + 6 * 60_000));
expect(downloads).toHaveBeenCalledTimes(before);
const legacy = await workspace(`removed-${choice}`, 'open');
await db.insert(projectAccessMembers).values({ companyId, projectId: legacy.projectId, subjectType: 'user', subjectId: userId });
// This older workspace has no binding/source ID; one explicit adoption must work.
await adoptProjectSkillRepository(db, companyId, legacy.id, context.authorize);
await worker.tick(new Date(Date.now() + 6 * 60_000));
await expect(service.detail(companyId, source.id)).rejects.toThrow(/not found/);
const [adopted] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, legacy.id));
expect(adopted!.status, adopted!.lastError ?? 'adopted binding').toBe('ready');
expect(adopted!.sourceId).toBeTruthy();
expect(adopted!.sourceId).not.toBe(source.id);
const restored = await service.detail(companyId, adopted!.sourceId!);
expect(restored.enabled).toBe(true);
expect(restored.lastError).toBeNull();
expect(restored.entries.filter(entry => entry.skillId)).toHaveLength(1);
expect(restored.entries[0]!.skillId).not.toBe(original.id);
if (choice === 'copy') expect(await skills.getById(companyId, original.id)).toMatchObject({ key: original.key, sourceType: 'managed', currentVersionId: retainedVersionId });
const readerId = randomUUID();
await db.insert(authUsers).values({ id: readerId, name: 'Open project reader', email: `${readerId}@example.test`, createdAt: new Date(), updatedAt: new Date() });
await db.insert(companyMemberships).values({ companyId, principalType: 'user', principalId: readerId, membershipRole: 'member', status: 'active' });
await withSkillActor(actor(readerId), async () => {
const upstream = restored.entries[0]!.skillId!;
expect(await skills.getById(companyId, upstream)).not.toBeNull();
expect(await skills.getById(companyId, fork.skill.id)).toBeNull();
expect(await skills.readFile(companyId, fork.skill.id, 'SKILL.md')).toBeNull();
if (choice === 'copy') {
expect(await skills.getById(companyId, original.id)).toBeNull();
expect(await skills.readFile(companyId, original.id, 'SKILL.md')).toBeNull();
expect(await skills.getVersion(companyId, original.id, retainedVersionId!)).toBeNull();
}
expect((await skills.readFile(companyId, upstream, '.paperclip-repository/private/SKILL.md'))?.content).not.toContain('Confidential');
// A guessed hidden slug cannot replace or erase someone else's private fork.
const publicFork = await skills.forkSkill(companyId, upstream, { slug: `retained-${choice}-fork` });
expect(publicFork.skill.id).not.toBe(fork.skill.id);
expect(publicFork.skill.slug).not.toBe(fork.skill.slug);
});
expect((await skills.readFile(companyId, fork.skill.id, 'SKILL.md'))?.content).toContain('Confidential original-project notes');
// Explicit adoption of the original workspace also moves tracking, not provenance.
await adoptProjectSkillRepository(db, companyId, row.id, context.authorize);
await worker.tick(new Date(Date.now() + 6 * 60_000));
const [rebound] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, row.id));
expect(rebound!.sourceId).toBe(restored.id);
expect(await skills.getById(companyId, fork.skill.id)).not.toBeNull();
await db.delete(projects).where(eq(projects.id, legacy.projectId));
await db.delete(projectAccessMembers).where(and(eq(projectAccessMembers.projectId, row.projectId), eq(projectAccessMembers.subjectId, userId)));
expect(await skills.getById(companyId, fork.skill.id)).toBeNull();
if (choice === 'copy') expect(await skills.getById(companyId, original.id)).toBeNull();
await db.delete(projects).where(eq(projects.id, row.projectId));
expect(await skills.getById(companyId, fork.skill.id)).toBeNull();
});
});
it('recovers expired jobs, competes through durable leases and respects failed-refresh backoff', async () => {
await db.update(companySkillSources).set({ automaticUpdates: false });
files = { 'leased/SKILL.md': md('leased') }; commit = 'f'.repeat(40);
const row = await workspace('leased');
const binding = await enqueueProjectSkillSource(db, row);
await db.update(projectSkillSources).set({ leaseToken: randomUUID(), leaseExpiresAt: new Date(Date.now() - 1_000) })
.where(eq(projectSkillSources.id, binding!.id));
let release!: () => void, scanned!: () => void;
const gate = new Promise<void>(resolve => { release = resolve; });
const started = new Promise<void>(resolve => { scanned = resolve; });
const blocking: SkillSourceContext = { ...context, read: () => {
const fixture = context.read(null), open = fixture.openSnapshot!;
fixture.openSnapshot = async (...args) => { scanned(); await gate; return open(...args); };
return fixture;
} };
const first = skillSourceSyncService(db, { context: async () => blocking }).tick();
await started;
expect((await skillSourceSyncService(db, { context: async () => context }).tick()).imported).toBe(0);
release(); expect((await first).imported).toBe(1);
const [ready] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.id, binding!.id));
const sourceId = ready!.sourceId!;
const service = skillSourceService(db);
const original = await service.detail(companyId, sourceId);
const rateLimited = async () => {
throw unprocessable('GitHub request limit reached.', { retryAfterMs: 7_200_000 });
};
const failure: SkillSourceContext = { ...context, read: () => Object.assign(rateLimited, { openSnapshot: rateLimited }) };
const failedAt = new Date();
await db.update(companySkillSources).set({ nextSyncAt: failedAt }).where(eq(companySkillSources.id, sourceId));
await skillSourceSyncService(db, { context: async () => failure }).tick(failedAt);
const [failed] = await db.select().from(companySkillSources).where(eq(companySkillSources.id, sourceId));
expect(failed!.failureCount).toBe(1);
expect(failed!.nextSyncAt.getTime() - failedAt.getTime()).toBeGreaterThanOrEqual(7_200_000);
expect((await service.detail(companyId, sourceId)).entries[0]!.skillId).toBe(original.entries[0]!.skillId);
const retryRead = vi.fn(context.read);
await skillSourceSyncService(db, { context: async () => ({ ...context, read: retryRead }) })
.tick(new Date(failedAt.getTime() + 6 * 60_000));
expect(retryRead).not.toHaveBeenCalled();
await db.update(companySkillSources).set({ automaticUpdates: false, nextSyncAt: new Date() }).where(eq(companySkillSources.id, sourceId));
await skillSourceSyncService(db, { context: async () => ({ ...context, read: retryRead }) }).tick();
expect(retryRead).not.toHaveBeenCalled();
});
it('skips live leases before bounding import and refresh batches', async () => {
await db.update(companySkillSources).set({ automaticUpdates: false });
files = { 'fair/SKILL.md': md('fair') }; commit = '9'.repeat(40);
const now = new Date();
const expires = new Date(now.getTime() + 60 * 60_000);
const busyJobs: string[] = [];
for (let index = 0; index < 10; index++) {
const row = await workspace(`busy-import-${index}`);
const binding = await enqueueProjectSkillSource(db, row);
busyJobs.push(binding!.id);
await db.update(projectSkillSources).set({ leaseToken: randomUUID(), leaseExpiresAt: expires,
nextSyncAt: new Date(now.getTime() - (20 - index) * 1_000) }).where(eq(projectSkillSources.id, binding!.id));
}
const ready = await enqueueProjectSkillSource(db, await workspace('ready-import'));
await db.update(projectSkillSources).set({ nextSyncAt: now }).where(eq(projectSkillSources.id, ready!.id));
const worker = skillSourceSyncService(db, { context: async () => context });
expect((await worker.tick(now)).imported).toBe(1);
const [imported] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.id, ready!.id));
expect(imported!.sourceId).toBeTruthy();
for (const id of busyJobs) {
const [job] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.id, id));
expect(job).toMatchObject({ sourceId: null, leaseExpiresAt: expires, status: 'pending' });
}
const [template] = await db.select().from(companySkillSources).where(eq(companySkillSources.id, imported!.sourceId!));
await db.update(companySkillSources).set({ automaticUpdates: false });
const readySourceId = randomUUID();
await db.insert(companySkillSources).values(Array.from({ length: 11 }, (_, index) => ({
...template!, id: index === 10 ? readySourceId : randomUUID(), trackingRef: `fair-refresh-${index}`,
accessScope: 'company' as const, automaticUpdates: true, nextSyncAt: new Date(now.getTime() - (20 - index) * 1_000),
leaseToken: index === 10 ? null : randomUUID(), leaseExpiresAt: index === 10 ? null : expires,
})));
expect((await worker.tick(now)).checked).toBe(1);
const [refreshed] = await db.select().from(companySkillSources).where(eq(companySkillSources.id, readySourceId));
expect(refreshed!.nextSyncAt.getTime()).toBeGreaterThan(now.getTime());
});
it('releases interrupted initial imports so a new worker can recover them', async () => {
await db.update(companySkillSources).set({ automaticUpdates: false });
files = { 'recovered/SKILL.md': md('recovered') }; commit = '1'.repeat(40);
const row = await workspace('restart');
const binding = await enqueueProjectSkillSource(db, row);
let scanned!: () => void;
const started = new Promise<void>(resolve => { scanned = resolve; });
const blocking: SkillSourceContext = { ...context, read: () => {
const fixture = context.read(null);
fixture.openSnapshot = async (_input, options) => {
scanned();
await new Promise<void>((_resolve, reject) => {
options!.signal!.addEventListener('abort', () => reject(options!.signal!.reason), { once: true });
});
throw new Error('Unreachable');
};
return fixture;
} };
const worker = skillSourceSyncService(db, { context: async () => blocking });
const sweep = worker.tick();
await started;
await worker.stop();
await sweep;
const [interrupted] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.id, binding!.id));
expect(interrupted).toMatchObject({ status: 'pending', failureCount: 0, sourceId: null, leaseToken: null, lastError: null });
expect((await skillSourceSyncService(db, { context: async () => context }).tick()).imported).toBe(1);
});
it('keeps deleted setup agents attributable and requires explicit connection repair', async () => {
await db.update(companySkillSources).set({ automaticUpdates: false });
files = { 'owned/SKILL.md': md('owned') }; commit = '2'.repeat(40);
const row = await workspace('deleted-owner');
const [owner] = await db.insert(agents).values({ companyId, name: 'Source setup agent', adapterType: 'codex_local', status: 'idle' }).returning();
await enqueueProjectSkillSource(db, row);
const ownerContext: SkillSourceContext = { ...context, setup: { userId: 'local-board', localTrusted: true, agentId: owner!.id } };
await skillSourceSyncService(db, { context: async () => ownerContext }).tick();
const [binding] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, row.id));
const sourceId = binding!.sourceId!;
await db.delete(agents).where(eq(agents.id, owner!.id));
await db.update(companySkillSources).set({ nextSyncAt: new Date() }).where(eq(companySkillSources.id, sourceId));
const [before] = await db.select().from(companySkillSources).where(eq(companySkillSources.id, sourceId));
expect(before!.refreshAgentId).toBe(owner!.id);
await skillSourceSyncService(db).tick();
const [failed] = await db.select().from(companySkillSources).where(eq(companySkillSources.id, sourceId));
expect(failed!.lastError).toContain('agent who connected');
expect(failed!.failureCount).toBe(1);
expect((await skillSourceService(db).detail(companyId, sourceId)).entries[0]!.skillId).toBeTruthy();
});
it('backs off a policy denial before acquiring a source lease', async () => {
await db.update(companySkillSources).set({ automaticUpdates: false });
files = { 'policy/SKILL.md': md('policy') }; commit = '3'.repeat(40);
const row = await workspace('policy-denial');
await enqueueProjectSkillSource(db, row);
await skillSourceSyncService(db, { context: async () => context }).tick();
const [binding] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, row.id));
const now = new Date();
await db.update(companySkillSources).set({ nextSyncAt: now }).where(eq(companySkillSources.id, binding!.sourceId!));
const authorize = vi.fn(async () => { throw unprocessable('Automatic updates are denied by company policy.'); });
const worker = skillSourceSyncService(db, { context: async () => ({ ...context, authorize }) });
await worker.tick(now);
const [failed] = await db.select().from(companySkillSources).where(eq(companySkillSources.id, binding!.sourceId!));
expect(failed).toMatchObject({ failureCount: 1, leaseToken: null, lastError: 'Automatic updates are denied by company policy.' });
expect(failed!.nextSyncAt.getTime()).toBeGreaterThan(now.getTime());
await worker.tick(new Date(now.getTime() + 1000));
expect(authorize).toHaveBeenCalledTimes(1);
});
it('uses bounded skill packages when a tracked repository outgrows the repository limit', async () => {
await db.update(companySkillSources).set({ automaticUpdates: false });
files = { 'small/SKILL.md': md('small') }; commit = '4'.repeat(40);
const row = await workspace('growing-repository');
await enqueueProjectSkillSource(db, row);
await skillSourceSyncService(db, { context: async () => context }).tick();
const [binding] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, row.id));
const service = skillSourceService(db), original = await service.detail(companyId, binding!.sourceId!);
expect(original.packageMode).toBe('repository');
const skillId = original.entries[0]!.skillId!;
files['small/SKILL.md'] = md('small updated'); commit = '5'.repeat(40);
const growing: SkillSourceContext = { ...context, read: () => {
const fixture = context.read(null), open = fixture.openSnapshot;
fixture.openSnapshot = async (...args) => {
const snapshot = await open(...args);
for (let index = 0; index < 10000; index++) snapshot.entries.push({ path: `unrelated/${index}.txt`, type: 'blob', mode: '100644', sha: String(index + 1), size: 0 });
return snapshot;
};
return fixture;
} };
const result = await service.refresh(companyId, original.id, growing);
expect(result.source.packageMode).toBe('repository');
expect(result.warnings.join(' ')).toContain('repository exceeds');
expect(result.source.entries[0]!.skillId).toBe(skillId);
expect(result.updated).toHaveLength(1);
files = { 'small/SKILL.md': md('small with shared guide') + '\nRead [guide](../shared/guide.md).', 'shared/guide.md': 'Now fits in the repository package.' };
commit = '6'.repeat(40);
const complete = await service.refresh(companyId, original.id, context);
expect(complete.source.packageMode).toBe('repository');
expect(complete.source.entries.find(entry => entry.path === 'small/SKILL.md')!.skillId).toBe(skillId);
expect((await companySkillService(db).readFile(companyId, skillId, '.paperclip-repository/shared/guide.md'))?.content)
.toBe('Now fits in the repository package.');
});
it('enqueues repository changes atomically, leaves ordinary legacy edits alone and retains old references', async () => {
await db.update(companySkillSources).set({ automaticUpdates: false });
const legacy = await workspace('lifecycle');
const service = projectService(db, { captureWorkspaceUpdates: false });
await service.updateWorkspace(legacy.projectId, legacy.id, { name: 'Renamed older workspace' });
expect(await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, legacy.id))).toHaveLength(0);
await expect(db.transaction(async tx => {
await projectService(tx as unknown as typeof db, { captureWorkspaceUpdates: false }).createWorkspace(legacy.projectId,
{ repoUrl: 'https://github.com/acme/skills', defaultRef: 'rolled-back' });
throw new Error('Required project audit failed');
})).rejects.toThrow('Required project audit failed');
expect(await db.select().from(projectWorkspaces).where(eq(projectWorkspaces.defaultRef, 'rolled-back'))).toHaveLength(0);
expect(await db.select().from(projectSkillSources).where(eq(projectSkillSources.trackingRef, 'rolled-back'))).toHaveLength(0);
files = { 'lifecycle/SKILL.md': md('lifecycle') }; commit = '6'.repeat(40);
const created = (await service.createWorkspace(legacy.projectId,
{ repoUrl: 'https://github.com/acme/skills', defaultRef: 'lifecycle-created' }))!;
const worker = skillSourceSyncService(db, { context: async () => context });
await worker.tick();
const [originalBinding] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, created.id));
const source = await skillSourceService(db).detail(companyId, originalBinding!.sourceId!);
const skillId = source.entries[0]!.skillId!;
const versionId = (await companySkillService(db).getById(companyId, skillId))!.currentVersionId;
await service.updateWorkspace(legacy.projectId, created.id, { defaultRef: 'lifecycle-next' });
await worker.tick();
const bindings = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, created.id));
expect(bindings).toHaveLength(2);
expect(bindings.find(binding => binding.id === originalBinding!.id)?.active).toBe(false);
const current = bindings.find(binding => binding.active)!;
expect(current.sourceId).not.toBe(source.id);
const retained = (await companySkillService(db).getById(companyId, skillId))!;
expect(retained.currentVersionId).toBe(versionId);
const newSource = await skillSourceService(db).detail(companyId, current.sourceId!);
expect(newSource.entries[0]!.skillId).not.toBe(skillId);
await service.updateWorkspace(legacy.projectId, created.id, { defaultRef: 'lifecycle-next' });
expect(await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, created.id))).toHaveLength(2);
await service.removeWorkspace(legacy.projectId, created.id);
expect(await skillSourceService(db).detail(companyId, current.sourceId!)).toMatchObject({ trackingActive: false });
expect(await companySkillService(db).getById(companyId, skillId)).not.toBeNull();
});
it('lets API-key agents attach repositories without inventing a setup person', async () => {
await db.update(companySkillSources).set({ automaticUpdates: false });
const project = await workspace('key-agent-parent');
const [agent] = await db.insert(agents).values({ companyId, name: 'API-key project creator', adapterType: 'codex_local', status: 'idle' }).returning();
const keyActor: AuthorizationActor = { type: 'agent', source: 'agent_key', agentId: agent!.id, companyId, onBehalfOfUserId: 'unverified-person' };
const attached = await withSkillActor(keyActor, () => projectService(db, { captureWorkspaceUpdates: false }).createWorkspace(project.projectId,
{ repoUrl: 'https://github.com/acme/skills', defaultRef: 'key-agent' }));
expect(attached).not.toBeNull();
const [binding] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, attached!.id));
expect(binding).toMatchObject({ status: 'pending', refreshUserId: null, refreshAgentId: agent!.id, refreshLocalTrusted: false, sourceId: null });
await detachProjectSkillSource(db, companyId, attached!.id);
});
it('requires connection repair before scheduled refresh of API-key public imports', async () => {
await db.update(companySkillSources).set({ automaticUpdates: false });
files = {};
const [agent] = await db.insert(agents).values({ companyId, name: 'API-key source importer', adapterType: 'codex_local', status: 'idle' }).returning();
const setup = await projectSkillSetupIdentity(db, companyId, { type: 'agent', source: 'agent_key', companyId,
agentId: agent!.id, onBehalfOfUserId: 'local-board' });
const imported = await skillSourceService(db).create(companyId, { repositoryUrl: 'https://github.com/acme/skills',
trackingRef: 'key-generic-public', commitSha: commit, selectedPaths: [] }, { ...context,
actor: { type: 'agent', agentId: agent!.id }, setup: { userId: setup.refreshUserId, agentId: setup.refreshAgentId, localTrusted: setup.refreshLocalTrusted } });
const [stored] = await db.select().from(companySkillSources).where(eq(companySkillSources.id, imported.source.id));
expect(stored).toMatchObject({ refreshUserId: null, refreshAgentId: agent!.id, refreshLocalTrusted: false, connectionId: null });
await db.insert(activityLog).values({ companyId, actorType: 'agent', actorId: agent!.id, agentId: agent!.id,
responsibleUserId: 'local-board', action: 'company.skill_source_refreshed', entityType: 'company_skill_source', entityId: imported.source.id });
await skillSourceSyncService(db).tick(new Date(Date.now() + 6 * 60_000));
const [checked] = await db.select().from(companySkillSources).where(eq(companySkillSources.id, imported.source.id));
expect(checked!.lastError).toMatch(/Choose a GitHub connection/);
expect(checked!.refreshUserId).toBeNull();
expect(checked!.lastSuccessAt).toEqual(stored!.lastSuccessAt);
// Migrated sources may lack refreshAgentId; API-key audit responsibility
// still cannot stand in for an authenticated run's credential authority.
await db.update(companySkillSources).set({ refreshAgentId: null }).where(eq(companySkillSources.id, imported.source.id));
await skillSourceSyncService(db).tick(new Date(Date.now() + 12 * 60_000));
const [legacy] = await db.select().from(companySkillSources).where(eq(companySkillSources.id, imported.source.id));
expect(legacy!.refreshUserId).toBeNull();
expect(legacy!.lastError).toMatch(/Choose a GitHub connection/);
});
it('follows a changed default branch while an explicit branch remains fixed', async () => {
await db.update(companySkillSources).set({ automaticUpdates: false });
let defaultBranch = 'main';
const originalFiles = { 'workflow/SKILL.md': md('original default') }, nextFiles = { 'workflow/SKILL.md': md('new default') };
const moving: SkillSourceContext = { ...context, read: () => {
const reader = (async (url: string) => {
if (url === '/repos/acme/skills') return { id: 42, full_name: 'acme/skills', default_branch: defaultBranch };
if (url.startsWith('/repos/acme/skills/commits/')) return { sha: decodeURIComponent(url.split('/commits/')[1]!) === 'main' ? '7'.repeat(40) : '8'.repeat(40) };
throw new Error('Unexpected request');
}) as ReturnType<SkillSourceContext['read']>;
reader.openSnapshot = async (input, options) => githubFixture(input.ref === 'main' ? originalFiles : nextFiles, {}, input.ref === 'main' ? '7'.repeat(40) : '8'.repeat(40)).openSnapshot(input, options);
return reader;
} };
const implicit = await workspace('HEAD');
await enqueueProjectSkillSource(db, implicit);
const worker = skillSourceSyncService(db, { context: async () => moving });
await worker.tick();
const [defaultBinding] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, implicit.id));
const sourceService = skillSourceService(db);
const original = await sourceService.detail(companyId, defaultBinding!.sourceId!);
expect(original.trackingRef).toBe('HEAD');
const skillId = original.entries[0]!.skillId!;
const versionId = (await companySkillService(db).getById(companyId, skillId))!.currentVersionId;
const explicit = await workspace('main');
await enqueueProjectSkillSource(db, explicit);
await worker.tick();
const [fixedBinding] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, explicit.id));
expect(fixedBinding!.sourceId).not.toBe(original.id);
defaultBranch = 'next-default';
await worker.tick(new Date(Date.now() + 6 * 60_000));
expect(await sourceService.detail(companyId, original.id)).toMatchObject({ trackingRef: 'HEAD', lastScanCommit: '8'.repeat(40) });
expect(await sourceService.detail(companyId, fixedBinding!.sourceId!)).toMatchObject({ trackingRef: 'main', lastScanCommit: '7'.repeat(40) });
const latest = (await companySkillService(db).getById(companyId, skillId))!;
expect(latest.currentVersionId).not.toBe(versionId);
expect(latest.markdown).toBe(md('new default'));
expect((await companySkillService(db).getVersion(companyId, skillId, versionId!))!.fileInventory[0]!.content).toBe(md('original default'));
const repeated = await workspace('HEAD');
await enqueueProjectSkillSource(db, repeated);
await worker.tick();
const [reused] = await db.select().from(projectSkillSources).where(eq(projectSkillSources.workspaceId, repeated.id));
expect(reused!.sourceId).toBe(original.id);
});
});