@sentropic/artifacts 0.2.17 → 0.2.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/dist/adapters/github-forge.d.ts +4 -0
- package/dist/adapters/github-forge.js +88 -0
- package/dist/adapters/github-git-port.d.ts +5 -0
- package/dist/adapters/github-git-port.js +102 -0
- package/dist/adapters/github-http.d.ts +5 -0
- package/dist/adapters/github-http.js +57 -0
- package/dist/adapters/github-receive-pack.d.ts +4 -0
- package/dist/adapters/github-receive-pack.js +49 -0
- package/dist/adapters/postgres-git-bindings.d.ts +7 -0
- package/dist/adapters/postgres-git-bindings.js +110 -0
- package/dist/adapters/postgres-live.d.ts +4 -0
- package/dist/adapters/postgres-live.js +97 -0
- package/dist/adapters/postgres.d.ts +1 -0
- package/dist/adapters/postgres.js +1 -0
- package/dist/git-tool-definitions.d.ts +606 -0
- package/dist/git-tool-definitions.js +14 -0
- package/dist/http.d.ts +6 -0
- package/dist/http.js +20 -2
- package/dist/index.d.ts +13 -0
- package/dist/index.js +12 -0
- package/dist/mcp-core.d.ts +3 -3
- package/dist/mcp-core.js +15 -2
- package/dist/profile/contracts.d.ts +1 -1
- package/dist/profile/contracts.js +4 -0
- package/dist/profile/index.js +8 -1
- package/dist/profile/schemas.js +19 -0
- package/dist/profile/validation.js +5 -1
- package/dist/server/build-contract.d.ts +64 -0
- package/dist/server/build-contract.js +58 -0
- package/dist/server/build-dependencies.d.ts +3 -0
- package/dist/server/build-dependencies.js +122 -0
- package/dist/server/consent-grant.d.ts +91 -0
- package/dist/server/consent-grant.js +89 -0
- package/dist/server/consent-screen.d.ts +21 -0
- package/dist/server/consent-screen.js +52 -0
- package/dist/server/failures.d.ts +3 -2
- package/dist/server/failures.js +30 -0
- package/dist/server/git-bindings.d.ts +8 -0
- package/dist/server/git-bindings.js +113 -0
- package/dist/server/git-contracts.d.ts +98 -0
- package/dist/server/git-contracts.js +1 -0
- package/dist/server/git-port-policy.d.ts +9 -0
- package/dist/server/git-port-policy.js +76 -0
- package/dist/server/git-port-read.d.ts +3 -0
- package/dist/server/git-port-read.js +95 -0
- package/dist/server/git-port-session.d.ts +38 -0
- package/dist/server/git-port-session.js +59 -0
- package/dist/server/git-port-tree.d.ts +8 -0
- package/dist/server/git-port-tree.js +92 -0
- package/dist/server/git-port-write.d.ts +6 -0
- package/dist/server/git-port-write.js +102 -0
- package/dist/server/git-port.d.ts +147 -0
- package/dist/server/git-port.js +8 -0
- package/dist/server/git-projection.d.ts +16 -0
- package/dist/server/git-projection.js +20 -0
- package/dist/server/git-registry.d.ts +37 -0
- package/dist/server/git-registry.js +39 -0
- package/dist/server/live-body.d.ts +3 -0
- package/dist/server/live-body.js +28 -0
- package/dist/server/live-commit-outcome.d.ts +6 -0
- package/dist/server/live-commit-outcome.js +10 -0
- package/dist/server/live-commit.d.ts +11 -0
- package/dist/server/live-commit.js +133 -0
- package/dist/server/live-contracts.d.ts +62 -0
- package/dist/server/live-contracts.js +8 -0
- package/dist/server/live-crypto.d.ts +14 -0
- package/dist/server/live-crypto.js +50 -0
- package/dist/server/live-drafts.d.ts +61 -0
- package/dist/server/live-drafts.js +119 -0
- package/dist/server/live-errors.d.ts +23 -0
- package/dist/server/live-errors.js +9 -0
- package/dist/server/live-mutations.d.ts +18 -0
- package/dist/server/live-mutations.js +83 -0
- package/dist/server/live-read.d.ts +15 -0
- package/dist/server/live-read.js +95 -0
- package/dist/server/live-service.d.ts +3 -0
- package/dist/server/live-service.js +181 -0
- package/dist/server/live-store.d.ts +111 -0
- package/dist/server/live-store.js +80 -0
- package/dist/server/live-tree.d.ts +22 -0
- package/dist/server/live-tree.js +148 -0
- package/dist/server/live-worker.d.ts +10 -0
- package/dist/server/live-worker.js +55 -0
- package/migrations/0010_git_bindings.sql +82 -0
- package/migrations/0011_live_drafts.sql +42 -0
- package/package.json +13 -1
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic host binding for persistent reader consent
|
|
3
|
+
* (design-merged §4.1, §4.3). Thin host binding over the generic
|
|
4
|
+
* ArtifactReaderStore, scoped to one storage scope per service instance: the
|
|
5
|
+
* reader revokes only their own consents resolved from the verified session
|
|
6
|
+
* (never a body field); a platform admin may revoke any reader's consent
|
|
7
|
+
* (incident response, never grant); the artifact owner cannot revoke consent
|
|
8
|
+
* — the owner's instrument is share revocation. Revoking for an unreadable
|
|
9
|
+
* artifact reads `not_found`, never 403, so the existence oracle stays closed.
|
|
10
|
+
*/
|
|
11
|
+
import { assertConsentScopeId, decideGateB } from './consent.js';
|
|
12
|
+
export { consentScreenPath, renderConsentScreen } from './consent-screen.js';
|
|
13
|
+
export function readerConsentRequired(scopeId) {
|
|
14
|
+
return { code: 'reader_consent_required', scopeId, retryable: false };
|
|
15
|
+
}
|
|
16
|
+
const denied = (code) => new Error(code);
|
|
17
|
+
export function createReaderConsentService(deps) {
|
|
18
|
+
const now = deps.now ?? (() => new Date().toISOString());
|
|
19
|
+
const key = (input) => ({ storageScopeId: deps.storageScopeId,
|
|
20
|
+
readerAccountId: input.readerAccountId, artifactId: input.artifactId,
|
|
21
|
+
...(input.scopeId === undefined ? {} : { scopeId: input.scopeId }) });
|
|
22
|
+
return {
|
|
23
|
+
/**
|
|
24
|
+
* First-party grant with a locked fresh-rights re-check: the host's
|
|
25
|
+
* checkRights (immo getRights) runs immediately before the store write,
|
|
26
|
+
* under the host's own authority discipline.
|
|
27
|
+
*/
|
|
28
|
+
async grantWithFreshRights(input, checkRights) {
|
|
29
|
+
assertConsentScopeId(input.scopeId);
|
|
30
|
+
if (!await checkRights())
|
|
31
|
+
throw denied('access_denied');
|
|
32
|
+
return deps.store.grantConsent({ storageScopeId: deps.storageScopeId, ...input,
|
|
33
|
+
consentId: crypto.randomUUID(), grantedAt: now(), grantedVia: 'reader_screen' });
|
|
34
|
+
},
|
|
35
|
+
/**
|
|
36
|
+
* J1.3b — fresh-rights probe for the first-party grant route: the same
|
|
37
|
+
* readability check the service was built with, so the grant route never
|
|
38
|
+
* invents its own authority rule.
|
|
39
|
+
*/
|
|
40
|
+
async checkReaderFreshRights(input) {
|
|
41
|
+
return deps.canReadArtifact(input.artifactId, input.readerAccountId);
|
|
42
|
+
},
|
|
43
|
+
/**
|
|
44
|
+
* J1.3b — Gate B for the generic reader gate route: live consent at the
|
|
45
|
+
* host-declared version plus fresh rights, evaluated on this call.
|
|
46
|
+
*/
|
|
47
|
+
async evaluateGate(input) {
|
|
48
|
+
assertConsentScopeId(input.scopeId);
|
|
49
|
+
const consent = await deps.store.findLiveConsent({ storageScopeId: deps.storageScopeId,
|
|
50
|
+
readerAccountId: input.readerAccountId, artifactId: input.artifactId,
|
|
51
|
+
scopeId: input.scopeId, scopeVersion: input.scopeVersion });
|
|
52
|
+
return decideGateB({ consent, currentScopeVersion: input.scopeVersion,
|
|
53
|
+
rights: input.rights, scopeId: input.scopeId });
|
|
54
|
+
},
|
|
55
|
+
async listOwnConsents(readerAccountId) {
|
|
56
|
+
const rows = await deps.store.listConsentsForReader(deps.storageScopeId, readerAccountId);
|
|
57
|
+
const live = rows.filter((row) => row.revokedAt === undefined);
|
|
58
|
+
return Promise.all(live.map(async (row) => ({ artifactId: row.artifactId,
|
|
59
|
+
title: await deps.resolveTitle?.(row.artifactId) ?? row.artifactId,
|
|
60
|
+
scopeId: row.scopeId, scopeVersion: row.scopeVersion, grantedAt: row.grantedAt })));
|
|
61
|
+
},
|
|
62
|
+
async revokeOwnConsent(input) {
|
|
63
|
+
if (input.scopeId !== undefined)
|
|
64
|
+
assertConsentScopeId(input.scopeId);
|
|
65
|
+
if (!await deps.canReadArtifact(input.artifactId, input.readerAccountId))
|
|
66
|
+
throw denied('not_found');
|
|
67
|
+
const result = await deps.store.revokeAllConsents(key(input), 'self', now(), { actor: input.readerAccountId, surface: 'api' });
|
|
68
|
+
return { changed: result.changed, revokedAt: result.changed ? now() : null };
|
|
69
|
+
},
|
|
70
|
+
async revokeAllOwnConsents(input) {
|
|
71
|
+
if (!await deps.canReadArtifact(input.artifactId, input.readerAccountId))
|
|
72
|
+
throw denied('not_found');
|
|
73
|
+
const result = await deps.store.revokeAllConsents(key(input), 'self', now(), { actor: input.readerAccountId, surface: 'api' });
|
|
74
|
+
return { changed: result.changed, revoked: result.revoked };
|
|
75
|
+
},
|
|
76
|
+
/**
|
|
77
|
+
* Platform-admin incident revocation. The admin never grants (reduce-only,
|
|
78
|
+
* mirroring canManageAccess); the artifact owner is refused here.
|
|
79
|
+
*/
|
|
80
|
+
async adminRevokeConsent(input, adminAccountId) {
|
|
81
|
+
if (input.scopeId !== undefined)
|
|
82
|
+
assertConsentScopeId(input.scopeId);
|
|
83
|
+
if (!await deps.isAdmin(adminAccountId))
|
|
84
|
+
throw denied('access_denied');
|
|
85
|
+
const result = await deps.store.revokeAllConsents(key(input), 'admin', now(), { actor: adminAccountId, surface: 'api' });
|
|
86
|
+
return { changed: result.changed, revokedAt: result.changed ? now() : null };
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Neutral host-parameterizable reader consent screen (generic grant
|
|
3
|
+
* contract). No tenant strings: brand, title and support URL come from the
|
|
4
|
+
* host declaration. The single prescribed navigation from a typed 403 is
|
|
5
|
+
* `consentScreenPath(artifactId, scopeId, workspaceId)`; the screen posts once to the
|
|
6
|
+
* sibling `grant` route (JSON or form), so a reader without consent reaches
|
|
7
|
+
* it in one click with no loop.
|
|
8
|
+
*/
|
|
9
|
+
export interface ConsentScreenParams {
|
|
10
|
+
brand: string;
|
|
11
|
+
title: string;
|
|
12
|
+
supportUrl?: string;
|
|
13
|
+
artifactId: string;
|
|
14
|
+
scopeId: string;
|
|
15
|
+
scopeVersion: string;
|
|
16
|
+
workspaceId: string;
|
|
17
|
+
}
|
|
18
|
+
/** Single prescribed navigation from the typed 403 `{ code, scopeId, retryable: false }`. */
|
|
19
|
+
export declare function consentScreenPath(artifactId: string, scopeId: string, workspaceId: string): string;
|
|
20
|
+
/** Neutral consent screen HTML; posts once to the sibling `grant` route. */
|
|
21
|
+
export declare function renderConsentScreen(params: ConsentScreenParams): string;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Neutral host-parameterizable reader consent screen (generic grant
|
|
3
|
+
* contract). No tenant strings: brand, title and support URL come from the
|
|
4
|
+
* host declaration. The single prescribed navigation from a typed 403 is
|
|
5
|
+
* `consentScreenPath(artifactId, scopeId, workspaceId)`; the screen posts once to the
|
|
6
|
+
* sibling `grant` route (JSON or form), so a reader without consent reaches
|
|
7
|
+
* it in one click with no loop.
|
|
8
|
+
*/
|
|
9
|
+
/** Single prescribed navigation from the typed 403 `{ code, scopeId, retryable: false }`. */
|
|
10
|
+
export function consentScreenPath(artifactId, scopeId, workspaceId) {
|
|
11
|
+
return `reader-consents/screen?artifactId=${encodeURIComponent(artifactId)}&scopeId=${encodeURIComponent(scopeId)}&workspace_id=${encodeURIComponent(workspaceId)}`;
|
|
12
|
+
}
|
|
13
|
+
function esc(value) {
|
|
14
|
+
return value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
|
15
|
+
.replace(/"/g, '"');
|
|
16
|
+
}
|
|
17
|
+
/** Neutral consent screen HTML; posts once to the sibling `grant` route. */
|
|
18
|
+
export function renderConsentScreen(params) {
|
|
19
|
+
const brand = esc(params.brand);
|
|
20
|
+
const title = esc(params.title);
|
|
21
|
+
const artifactId = esc(params.artifactId);
|
|
22
|
+
const scopeId = esc(params.scopeId);
|
|
23
|
+
const scopeVersion = esc(params.scopeVersion);
|
|
24
|
+
const action = `grant?workspace_id=${encodeURIComponent(params.workspaceId)}`;
|
|
25
|
+
const support = params.supportUrl === undefined ? '' :
|
|
26
|
+
`<p><a href="${esc(params.supportUrl)}">Support</a></p>`;
|
|
27
|
+
return `<!doctype html>
|
|
28
|
+
<html lang="en">
|
|
29
|
+
<head>
|
|
30
|
+
<meta charset="utf-8">
|
|
31
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
32
|
+
<title>${title} - ${brand}</title>
|
|
33
|
+
</head>
|
|
34
|
+
<body>
|
|
35
|
+
<main>
|
|
36
|
+
<h1>${title}</h1>
|
|
37
|
+
<p>${brand} requests access to artifact <code>${artifactId}</code>.</p>
|
|
38
|
+
<dl>
|
|
39
|
+
<dt>Scope</dt><dd>${scopeId}</dd>
|
|
40
|
+
<dt>Version</dt><dd>${scopeVersion}</dd>
|
|
41
|
+
</dl>
|
|
42
|
+
<form method="post" action="${esc(action)}">
|
|
43
|
+
<input type="hidden" name="artifactId" value="${artifactId}">
|
|
44
|
+
<input type="hidden" name="scopeId" value="${scopeId}">
|
|
45
|
+
<input type="hidden" name="scopeVersion" value="${scopeVersion}">
|
|
46
|
+
<button type="submit">Allow access</button>
|
|
47
|
+
</form>
|
|
48
|
+
${support}
|
|
49
|
+
</main>
|
|
50
|
+
</body>
|
|
51
|
+
</html>`;
|
|
52
|
+
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { type LiveErrorCode } from './live-errors.js';
|
|
1
2
|
/** Trusted server failures (r2 E1–E3). No driver text participates in classification. */
|
|
2
3
|
export type ArtifactCommitState = 'not_committed' | 'committed' | 'unknown';
|
|
3
4
|
/** Structurally identical to L0's server-derived SHA-256 internal key, never a caller key. */
|
|
4
5
|
export type ArtifactInternalKey = string & {
|
|
5
6
|
readonly __brand: 'internal-key';
|
|
6
7
|
};
|
|
7
|
-
export type ArtifactErrorKind = 'provider_unavailable' | 'command_conflict' | 'pointer_conflict' | 'unauthorized_at_commit' | 'capability_unselected' | 'admission_unavailable' | 'invalid_request' | 'anonymous' | 'access_denied' | 'not_found' | 'gone' | 'release_unavailable' | 'limit_exceeded' | 'unprocessable' | 'verification_failed' | 'edit_mismatch' | 'precondition_required';
|
|
8
|
+
export type ArtifactErrorKind = LiveErrorCode | 'provider_unavailable' | 'command_conflict' | 'pointer_conflict' | 'unauthorized_at_commit' | 'capability_unselected' | 'admission_unavailable' | 'invalid_request' | 'anonymous' | 'access_denied' | 'not_found' | 'gone' | 'release_unavailable' | 'limit_exceeded' | 'unprocessable' | 'verification_failed' | 'edit_mismatch' | 'precondition_required' | 'git_binding_conflict' | 'git_repo_not_registered' | 'git_ref_not_allowed' | 'git_head_moved' | 'git_path_refused' | 'git_branch_missing' | 'build_descriptor_invalid' | 'build_script_missing' | 'lock_missing' | 'dep_outside_lock' | 'install_script_not_allowlisted';
|
|
8
9
|
/** Optimistic-concurrency payload: the caller re-reads and merges against this. */
|
|
9
10
|
export interface ArtifactConflictDetails {
|
|
10
11
|
readonly artifactId?: string;
|
|
@@ -48,7 +49,7 @@ export interface ArtifactOperationalFailure extends ArtifactFailureFields {
|
|
|
48
49
|
}
|
|
49
50
|
export interface ArtifactOrdinaryFailure extends Omit<ArtifactFailureFields, 'variant' | 'retriable'> {
|
|
50
51
|
readonly kind: 'ordinary';
|
|
51
|
-
readonly category: 'invalid_request' | 'not_found' | 'gone' | 'release_unavailable' | 'limit_exceeded' | 'unprocessable' | 'verification_failed' | 'edit_mismatch' | 'precondition_required';
|
|
52
|
+
readonly category: 'invalid_request' | 'not_found' | 'gone' | 'release_unavailable' | 'limit_exceeded' | LiveErrorCode | 'unprocessable' | 'verification_failed' | 'edit_mismatch' | 'precondition_required' | 'build_descriptor_invalid' | 'build_script_missing' | 'lock_missing' | 'dep_outside_lock' | 'install_script_not_allowlisted' | 'git_path_refused' | 'git_branch_missing' | 'git_ref_not_allowed' | 'git_repo_not_registered';
|
|
52
53
|
readonly retriable: false;
|
|
53
54
|
}
|
|
54
55
|
/** Entry authority outcomes are separate from ordinary and operational failures. */
|
package/dist/server/failures.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { LIVE_FAILURE_STATUS } from './live-errors.js';
|
|
1
2
|
export class ArtifactOperationError extends Error {
|
|
2
3
|
kind;
|
|
3
4
|
details;
|
|
@@ -30,6 +31,9 @@ const DOMAIN_STATUS = {
|
|
|
30
31
|
invalid_request: 400, anonymous: 401, access_denied: 403,
|
|
31
32
|
not_found: 404, gone: 410, release_unavailable: 410, limit_exceeded: 413, unprocessable: 422, verification_failed: 422, edit_mismatch: 422,
|
|
32
33
|
precondition_required: 428,
|
|
34
|
+
build_descriptor_invalid: 422, build_script_missing: 422, lock_missing: 422,
|
|
35
|
+
dep_outside_lock: 422, install_script_not_allowlisted: 422,
|
|
36
|
+
git_path_refused: 422, git_branch_missing: 422, git_ref_not_allowed: 422, git_repo_not_registered: 422,
|
|
33
37
|
};
|
|
34
38
|
function field(value, name) {
|
|
35
39
|
if (typeof value !== 'object' || value === null)
|
|
@@ -61,9 +65,31 @@ const conflict = (variant = 'key_contention') => ({ kind: 'operational', categor
|
|
|
61
65
|
const internal = () => ({ kind: 'operational', category: 'provider_error', variant: 'unclassified', httpStatus: 500, publicCode: 'internal_error', retriable: false });
|
|
62
66
|
function operationProjection(error) {
|
|
63
67
|
const { kind } = error;
|
|
68
|
+
if (Object.hasOwn(LIVE_FAILURE_STATUS, kind)) {
|
|
69
|
+
const status = LIVE_FAILURE_STATUS[kind];
|
|
70
|
+
const details = error.details.publicDetails;
|
|
71
|
+
if (kind === 'git_live_conflict' && !['invalid_phase', 'active_session', 'pending_publication',
|
|
72
|
+
'commit_indeterminate', 'stale_session', 'stale_fence', 'transition_busy'].includes(String(details?.reason)))
|
|
73
|
+
return internal();
|
|
74
|
+
if (status === 409)
|
|
75
|
+
return { ...conflict(kind), publicCode: kind, details };
|
|
76
|
+
if (status === 503)
|
|
77
|
+
return { kind: 'operational', category: 'provider_unavailable',
|
|
78
|
+
httpStatus: status, publicCode: kind, retriable: true, details };
|
|
79
|
+
return { kind: 'ordinary', category: kind, httpStatus: status,
|
|
80
|
+
publicCode: kind, retriable: false, details };
|
|
81
|
+
}
|
|
64
82
|
switch (kind) {
|
|
83
|
+
case 'git_head_moved': return { ...conflict(kind), publicCode: kind, details: error.details.publicDetails };
|
|
84
|
+
case 'git_path_refused':
|
|
85
|
+
case 'git_branch_missing':
|
|
86
|
+
case 'git_ref_not_allowed':
|
|
87
|
+
case 'git_repo_not_registered':
|
|
88
|
+
return { kind: 'ordinary', category: kind, httpStatus: DOMAIN_STATUS[kind], publicCode: kind,
|
|
89
|
+
retriable: false, details: error.details.publicDetails };
|
|
65
90
|
case 'provider_unavailable':
|
|
66
91
|
case 'admission_unavailable': return unavailable(kind);
|
|
92
|
+
case 'git_binding_conflict': return { ...conflict(kind), publicCode: kind, details: error.details.publicDetails };
|
|
67
93
|
case 'command_conflict':
|
|
68
94
|
case 'pointer_conflict': {
|
|
69
95
|
const base = conflict(kind);
|
|
@@ -94,6 +120,10 @@ function operationProjection(error) {
|
|
|
94
120
|
const base = { kind: 'ordinary', category: kind, httpStatus: DOMAIN_STATUS[kind], publicCode: kind, retriable: false };
|
|
95
121
|
if (kind === 'precondition_required')
|
|
96
122
|
return { ...base, details: error.details.publicDetails };
|
|
123
|
+
if (kind === 'limit_exceeded' && error.details.publicDetails?.quota === 'git_write' &&
|
|
124
|
+
['principal', 'artifact'].includes(String(error.details.publicDetails.scope)))
|
|
125
|
+
return { ...base,
|
|
126
|
+
details: { quota: 'git_write', scope: error.details.publicDetails.scope } };
|
|
97
127
|
if (kind === 'release_unavailable' && error.details.release !== undefined)
|
|
98
128
|
return { ...base, release: error.details.release };
|
|
99
129
|
return kind === 'edit_mismatch' && error.details.edit !== undefined ? { ...base, edit: error.details.edit } : base;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { GitBinding, GitBindingEvent, GitBindingStore, GitBindingWrite } from './git-contracts.js';
|
|
2
|
+
export type * from './git-contracts.js';
|
|
3
|
+
export declare function gitBindingReceiptKey(input: GitBindingWrite): string;
|
|
4
|
+
/** Shared invariants run inside the adapter transaction, after receipt replay and before mutation. */
|
|
5
|
+
export declare function validateGitBindingWrite(before: GitBinding | undefined, input: GitBindingWrite): void;
|
|
6
|
+
export declare function gitBindingEvent(input: GitBindingWrite): GitBindingEvent;
|
|
7
|
+
/** Atomic single-process fake tenant. Outputs never expose mutable internal records. */
|
|
8
|
+
export declare function createMemoryGitBindingStore(): GitBindingStore;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { ArtifactOperationError } from './failures.js';
|
|
3
|
+
import { createKeyMutex } from './ports.js';
|
|
4
|
+
import { isAllowedGitBranch } from './git-registry.js';
|
|
5
|
+
export function gitBindingReceiptKey(input) {
|
|
6
|
+
return 'git-binding:' + createHash('sha256').update(JSON.stringify([
|
|
7
|
+
input.binding.storageScopeId, input.actorId, input.idempotencyKey,
|
|
8
|
+
])).digest('hex');
|
|
9
|
+
}
|
|
10
|
+
/** Shared invariants run inside the adapter transaction, after receipt replay and before mutation. */
|
|
11
|
+
export function validateGitBindingWrite(before, input) {
|
|
12
|
+
const next = input.binding;
|
|
13
|
+
if (!isAllowedGitBranch(next.branch))
|
|
14
|
+
throw new ArtifactOperationError('git_ref_not_allowed');
|
|
15
|
+
if (!input.idempotencyKey.trim() || !input.commandHash || !input.actorId || !next.storageScopeId ||
|
|
16
|
+
!next.repoId || next.live !== undefined ||
|
|
17
|
+
!Number.isSafeInteger(next.version) || !Number.isSafeInteger(next.generation) ||
|
|
18
|
+
next.version < 1 || next.generation < 1 ||
|
|
19
|
+
(next.pendingJobId === null) !== (next.pendingCommitSha === null) ||
|
|
20
|
+
(next.channel === 'shared' ? next.personalPrincipalId !== null : !next.personalPrincipalId)) {
|
|
21
|
+
throw new ArtifactOperationError('invalid_request');
|
|
22
|
+
}
|
|
23
|
+
if (before && before.storageScopeId !== next.storageScopeId)
|
|
24
|
+
throw new ArtifactOperationError('not_found');
|
|
25
|
+
if (input.expectedVersion === null && before)
|
|
26
|
+
throw new ArtifactOperationError('git_binding_conflict', { publicDetails: { artifactId: next.artifactId } });
|
|
27
|
+
if (input.expectedVersion !== (before?.version ?? null))
|
|
28
|
+
throw new ArtifactOperationError('pointer_conflict');
|
|
29
|
+
if (next.version !== (before?.version ?? 0) + 1)
|
|
30
|
+
throw new ArtifactOperationError('invalid_request');
|
|
31
|
+
if (before && before.state !== 'detached' && (next.repoId !== before.repoId || next.branch !== before.branch ||
|
|
32
|
+
next.subPath !== before.subPath))
|
|
33
|
+
throw new ArtifactOperationError('git_binding_conflict', { publicDetails: { artifactId: next.artifactId } });
|
|
34
|
+
const action = input.event.action;
|
|
35
|
+
const active = before !== undefined && before.state !== 'detached';
|
|
36
|
+
const configuring = action === 'attach' && active;
|
|
37
|
+
const transition = action === 'attach' ? (configuring ? next.state === before.state : next.state === 'attached')
|
|
38
|
+
: action === 'pause' ? active && next.state === 'paused'
|
|
39
|
+
: action === 'resume' ? before?.state === 'paused' && next.state === 'attached' &&
|
|
40
|
+
!['git_branch_missing', 'git_diverged'].includes(before.stateDetail ?? '')
|
|
41
|
+
: action === 'detach' ? before !== undefined && next.state === 'detached'
|
|
42
|
+
: action === 'commit' ? before?.state === 'attached' && ['attached', 'build_pending', 'syncing'].includes(next.state)
|
|
43
|
+
: action === 'inbound' ? (before?.state === 'attached' && ['syncing', 'build_pending'].includes(next.state)) ||
|
|
44
|
+
(before?.state === 'syncing' && ['attached', 'build_pending', 'diverged'].includes(next.state)) ||
|
|
45
|
+
(before?.state === 'build_failed' && next.state === 'build_pending') ||
|
|
46
|
+
(active && next.stateDetail === 'git_branch_missing' && next.state === (before.state === 'paused' ? 'paused' : 'diverged'))
|
|
47
|
+
: action === 'publish' ? before !== undefined && (['attached', 'build_failed'].includes(before.state)
|
|
48
|
+
? next.state === 'build_pending' : before.state === 'build_pending' && ['attached', 'build_failed'].includes(next.state))
|
|
49
|
+
: action === 'conflict' ? active && ['diverged', 'paused'].includes(next.state)
|
|
50
|
+
: action === 'resolve' ? before?.state === 'diverged' && next.state === 'build_pending'
|
|
51
|
+
: action === 'error' && active && next.state === 'error';
|
|
52
|
+
if (!transition)
|
|
53
|
+
throw new ArtifactOperationError('invalid_request');
|
|
54
|
+
const advance = before && ((action === 'attach' && before.state === 'detached') ||
|
|
55
|
+
['detach', 'pause'].includes(action) || (action === 'inbound' && next.stateDetail === 'git_branch_missing'));
|
|
56
|
+
if (next.generation !== (before ? before.generation + (advance ? 1 : 0) : 1) ||
|
|
57
|
+
(advance && (next.pendingJobId !== null || next.pendingCommitSha !== null)) ||
|
|
58
|
+
(before && action !== 'publish' && next.releaseCommitSha !== before.releaseCommitSha)) {
|
|
59
|
+
throw new ArtifactOperationError('invalid_request');
|
|
60
|
+
}
|
|
61
|
+
const fenced = before && (before.state === 'diverged' || ['git_branch_missing', 'git_diverged'].includes(before.stateDetail ?? ''));
|
|
62
|
+
if ((action === 'pause' && fenced && next.stateDetail !== (before.stateDetail ?? 'git_diverged')) ||
|
|
63
|
+
(configuring && (next.headCommitSha !== before.headCommitSha || next.stateDetail !== before.stateDetail ||
|
|
64
|
+
next.baseCommitSha !== before.baseCommitSha || next.channel !== before.channel ||
|
|
65
|
+
next.personalPrincipalId !== before.personalPrincipalId || next.attachedBy !== before.attachedBy ||
|
|
66
|
+
next.attachedAt !== before.attachedAt))) {
|
|
67
|
+
throw new ArtifactOperationError('invalid_request');
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export function gitBindingEvent(input) {
|
|
71
|
+
return { id: randomUUID(), artifactId: input.binding.artifactId, storageScopeId: input.binding.storageScopeId,
|
|
72
|
+
actorId: input.actorId, action: input.event.action, beforeSha: input.event.beforeSha,
|
|
73
|
+
afterSha: input.event.afterSha, deliveryId: input.event.deliveryId,
|
|
74
|
+
invocationId: input.event.invocationId, createdAt: input.binding.updatedAt };
|
|
75
|
+
}
|
|
76
|
+
/** Atomic single-process fake tenant. Outputs never expose mutable internal records. */
|
|
77
|
+
export function createMemoryGitBindingStore() {
|
|
78
|
+
const rows = new Map();
|
|
79
|
+
const audit = [];
|
|
80
|
+
const receipts = new Map();
|
|
81
|
+
const mutex = createKeyMutex();
|
|
82
|
+
return {
|
|
83
|
+
async get(scope, id) {
|
|
84
|
+
const binding = rows.get(id);
|
|
85
|
+
return binding?.storageScopeId === scope ? structuredClone(binding) : undefined;
|
|
86
|
+
},
|
|
87
|
+
async events(scope, id) {
|
|
88
|
+
return structuredClone(audit.filter(event => event.storageScopeId === scope && event.artifactId === id));
|
|
89
|
+
},
|
|
90
|
+
async write(input) {
|
|
91
|
+
const snapshot = structuredClone(input);
|
|
92
|
+
return mutex.runSerialized('git-bindings', async () => {
|
|
93
|
+
const key = gitBindingReceiptKey(snapshot);
|
|
94
|
+
const receipt = receipts.get(key);
|
|
95
|
+
if (receipt) {
|
|
96
|
+
if (receipt.hash !== snapshot.commandHash)
|
|
97
|
+
throw new ArtifactOperationError('command_conflict');
|
|
98
|
+
return { binding: structuredClone(receipt.binding), replayed: true };
|
|
99
|
+
}
|
|
100
|
+
const binding = snapshot.binding;
|
|
101
|
+
validateGitBindingWrite(rows.get(binding.artifactId), snapshot);
|
|
102
|
+
if (binding.state !== 'detached' && [...rows.values()].some(row => row.artifactId !== binding.artifactId &&
|
|
103
|
+
row.state !== 'detached' && row.repoId === binding.repoId && row.branch === binding.branch)) {
|
|
104
|
+
throw new ArtifactOperationError('git_binding_conflict', { publicDetails: { artifactId: binding.artifactId } });
|
|
105
|
+
}
|
|
106
|
+
rows.set(binding.artifactId, binding);
|
|
107
|
+
audit.push(gitBindingEvent(snapshot));
|
|
108
|
+
receipts.set(key, { hash: snapshot.commandHash, binding: structuredClone(binding) });
|
|
109
|
+
return { binding: structuredClone(binding), replayed: false };
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/** G0 FROZEN v3 §2. Host records; never release profile or tool-definition state. */
|
|
2
|
+
export interface GitLiveSession {
|
|
3
|
+
liveSessionId: string;
|
|
4
|
+
bindingGeneration: number;
|
|
5
|
+
requestedBy: string;
|
|
6
|
+
state: 'starting' | 'running' | 'stopping' | 'stopped' | 'failed';
|
|
7
|
+
targetCommitSha: string;
|
|
8
|
+
appliedCommitSha: string | null;
|
|
9
|
+
previewPath: string | null;
|
|
10
|
+
startedAt: string;
|
|
11
|
+
lastActivityAt: string;
|
|
12
|
+
expiresAt: string;
|
|
13
|
+
stateDetail?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface GitBinding {
|
|
16
|
+
artifactId: string;
|
|
17
|
+
storageScopeId: string;
|
|
18
|
+
repoId: string;
|
|
19
|
+
branch: string;
|
|
20
|
+
channel: 'personal' | 'shared';
|
|
21
|
+
personalPrincipalId: string | null;
|
|
22
|
+
subPath: string;
|
|
23
|
+
baseCommitSha: string;
|
|
24
|
+
headCommitSha: string | null;
|
|
25
|
+
releaseCommitSha: string | null;
|
|
26
|
+
autoPublish: boolean;
|
|
27
|
+
executor: 'caller' | 'ci' | 'sandbox';
|
|
28
|
+
pendingJobId: string | null;
|
|
29
|
+
pendingCommitSha: string | null;
|
|
30
|
+
state: 'attached' | 'syncing' | 'build_pending' | 'build_failed' | 'diverged' | 'paused' | 'detached' | 'error';
|
|
31
|
+
stateDetail?: string;
|
|
32
|
+
generation: number;
|
|
33
|
+
version: number;
|
|
34
|
+
attachedBy: string;
|
|
35
|
+
attachedAt: string;
|
|
36
|
+
updatedAt: string;
|
|
37
|
+
live?: GitLiveSession | null;
|
|
38
|
+
}
|
|
39
|
+
export interface GitBindingEvent {
|
|
40
|
+
id: string;
|
|
41
|
+
artifactId: string;
|
|
42
|
+
storageScopeId: string;
|
|
43
|
+
actorId: string;
|
|
44
|
+
action: 'attach' | 'pause' | 'resume' | 'detach' | 'commit' | 'inbound' | 'publish' | 'conflict' | 'resolve' | 'error';
|
|
45
|
+
beforeSha?: string;
|
|
46
|
+
afterSha?: string;
|
|
47
|
+
deliveryId?: string;
|
|
48
|
+
invocationId?: string;
|
|
49
|
+
createdAt: string;
|
|
50
|
+
}
|
|
51
|
+
export interface GitReleaseIndex {
|
|
52
|
+
releaseId: string;
|
|
53
|
+
artifactId: string;
|
|
54
|
+
repoId: string;
|
|
55
|
+
branch: string;
|
|
56
|
+
commitSha: string;
|
|
57
|
+
treeSha?: string;
|
|
58
|
+
manifestDigest: string;
|
|
59
|
+
candidateDigest: string;
|
|
60
|
+
executor: 'caller' | 'ci' | 'sandbox';
|
|
61
|
+
verification: 'declared' | 'sha-verified' | 'builder-attested';
|
|
62
|
+
receiptId: string;
|
|
63
|
+
jobId: string;
|
|
64
|
+
buildRunId?: string;
|
|
65
|
+
attempt?: number;
|
|
66
|
+
createdAt: string;
|
|
67
|
+
}
|
|
68
|
+
/** Trusted storage input: SG3 derives commandHash and actor from the invocation, never tool arguments. */
|
|
69
|
+
export interface GitBindingWrite {
|
|
70
|
+
binding: GitBinding;
|
|
71
|
+
expectedVersion: number | null;
|
|
72
|
+
actorId: string;
|
|
73
|
+
idempotencyKey: string;
|
|
74
|
+
commandHash: string;
|
|
75
|
+
event: Pick<GitBindingEvent, 'action' | 'beforeSha' | 'afterSha' | 'deliveryId' | 'invocationId'>;
|
|
76
|
+
}
|
|
77
|
+
export interface GitBindingStore {
|
|
78
|
+
get(storageScopeId: string, artifactId: string): Promise<GitBinding | undefined>;
|
|
79
|
+
/** Binding, audit and principal-scoped replay receipt commit atomically. */
|
|
80
|
+
write(input: GitBindingWrite): Promise<{
|
|
81
|
+
binding: GitBinding;
|
|
82
|
+
replayed: boolean;
|
|
83
|
+
}>;
|
|
84
|
+
events(storageScopeId: string, artifactId: string): Promise<GitBindingEvent[]>;
|
|
85
|
+
}
|
|
86
|
+
/** SG4 writes the index using its release-transaction client; SG1 provides the record/read port. */
|
|
87
|
+
export interface GitReleaseIndexStore {
|
|
88
|
+
get(storageScopeId: string, releaseId: string): Promise<GitReleaseIndex | undefined>;
|
|
89
|
+
forCommit(storageScopeId: string, artifactId: string, commitSha: string): Promise<GitReleaseIndex[]>;
|
|
90
|
+
}
|
|
91
|
+
/** Repository code access is independent of artifact/page/source grants and shell roles. */
|
|
92
|
+
export interface GitCodeRightsPort {
|
|
93
|
+
canReadCode(input: {
|
|
94
|
+
storageScopeId: string;
|
|
95
|
+
repoId: string;
|
|
96
|
+
actorId: string;
|
|
97
|
+
}): Promise<boolean>;
|
|
98
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { GitPortRepository, GitPortTarget } from './git-port.js';
|
|
2
|
+
export declare const isGitSha: (sha: string) => boolean;
|
|
3
|
+
export declare function assertGitSha(sha: string): void;
|
|
4
|
+
export declare function assertGitBranch(branch: string): void;
|
|
5
|
+
export declare function assertGitReadRef(ref: string): void;
|
|
6
|
+
export declare function assertGitTarget(repo: GitPortRepository, target: GitPortTarget, write: boolean): void;
|
|
7
|
+
/** Small host-glob language: * stays inside one segment; ** spans complete subtrees. */
|
|
8
|
+
export declare function matchesGitGlob(path: string, glob: string): boolean;
|
|
9
|
+
export declare function gitPath(repo: GitPortRepository, target: GitPortTarget, path: string, write: boolean): string;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { ArtifactOperationError } from './failures.js';
|
|
2
|
+
export const isGitSha = (sha) => /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(sha);
|
|
3
|
+
export function assertGitSha(sha) {
|
|
4
|
+
if (!isGitSha(sha))
|
|
5
|
+
throw new ArtifactOperationError('invalid_request');
|
|
6
|
+
}
|
|
7
|
+
export function assertGitBranch(branch) {
|
|
8
|
+
if (!branch.startsWith('artifact/') || /[\x00-\x20\x7f~^:?*\[\\]/.test(branch) ||
|
|
9
|
+
branch.includes('..') || branch.includes('@{') || branch.endsWith('.') ||
|
|
10
|
+
branch.split('/').some(p => !p || p.startsWith('.') || p.endsWith('.lock'))) {
|
|
11
|
+
throw new ArtifactOperationError('git_ref_not_allowed');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export function assertGitReadRef(ref) {
|
|
15
|
+
if (!ref || ref.length > 1024 || /[\x00-\x20\x7f~^:?*\[\\]/.test(ref) || ref.includes('..') ||
|
|
16
|
+
ref.includes('@{') || ref.endsWith('.') || ref.split('/').some(p => !p || p.startsWith('.') || p.endsWith('.lock'))) {
|
|
17
|
+
throw new ArtifactOperationError('git_ref_not_allowed');
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export function assertGitTarget(repo, target, write) {
|
|
21
|
+
if (repo.storageScopeId !== target.storageScopeId || repo.repoId !== target.repoId) {
|
|
22
|
+
throw new ArtifactOperationError('git_repo_not_registered');
|
|
23
|
+
}
|
|
24
|
+
if (!repo.allowedSubPaths.includes(target.subPath) || (write && !repo.canWrite)) {
|
|
25
|
+
throw new ArtifactOperationError('git_ref_not_allowed');
|
|
26
|
+
}
|
|
27
|
+
if (write)
|
|
28
|
+
assertGitBranch(target.branch);
|
|
29
|
+
else
|
|
30
|
+
assertGitReadRef(target.branch);
|
|
31
|
+
if (target.subPath)
|
|
32
|
+
safePath(target.subPath);
|
|
33
|
+
safePath(target.outputDir);
|
|
34
|
+
}
|
|
35
|
+
function refused(path, reason) {
|
|
36
|
+
throw new ArtifactOperationError('git_path_refused', { publicDetails: { path, reason } });
|
|
37
|
+
}
|
|
38
|
+
function safePath(path) {
|
|
39
|
+
if (!path || Buffer.byteLength(path) > 1024 || Buffer.from(path).toString('utf8') !== path ||
|
|
40
|
+
/[\x00-\x1f\x7f\\%:?#]/.test(path) || path !== path.normalize('NFC') ||
|
|
41
|
+
path.split('/').some(p => !p || p === '.' || p === '..' || /[. ]$/.test(p)))
|
|
42
|
+
refused(path, 'noncanonical');
|
|
43
|
+
for (const part of path.split('/')) {
|
|
44
|
+
if (/^(?:\.git|\.github|\.ssh|\.npmrc)$/i.test(part) || /^\.yarnrc/i.test(part) || /^\.env/i.test(part) ||
|
|
45
|
+
/\.(?:pem|key|p12|pfx|p8|jks|keystore)$/i.test(part) ||
|
|
46
|
+
/^(?:id_(?:rsa|dsa|ecdsa|ed25519)|private[_-]?key)(?:\.|$)/i.test(part))
|
|
47
|
+
refused(path, 'reserved');
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** Small host-glob language: * stays inside one segment; ** spans complete subtrees. */
|
|
51
|
+
export function matchesGitGlob(path, glob) {
|
|
52
|
+
const segment = (part) => new RegExp(`^${part.split('*').map(s => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('[^/]*')}$`);
|
|
53
|
+
const parts = path.split('/');
|
|
54
|
+
const pattern = glob.split('/');
|
|
55
|
+
function match(i, j) {
|
|
56
|
+
if (j === pattern.length)
|
|
57
|
+
return i === parts.length;
|
|
58
|
+
if (pattern[j] === '**')
|
|
59
|
+
return match(i, j + 1) || (i < parts.length && match(i + 1, j));
|
|
60
|
+
return i < parts.length && segment(pattern[j]).test(parts[i]) && match(i + 1, j + 1);
|
|
61
|
+
}
|
|
62
|
+
// A basename-only denial (package*.json) applies at every depth.
|
|
63
|
+
return !glob.includes('/') ? segment(glob).test(parts.at(-1)) : match(0, 0);
|
|
64
|
+
}
|
|
65
|
+
export function gitPath(repo, target, path, write) {
|
|
66
|
+
safePath(path);
|
|
67
|
+
const full = target.subPath ? `${target.subPath}/${path}` : path;
|
|
68
|
+
safePath(full);
|
|
69
|
+
if (write) {
|
|
70
|
+
const output = target.outputDir;
|
|
71
|
+
if (`/${path}/`.includes(`/${output}/`) || repo.refusedWritePaths.some(g => matchesGitGlob(full, g)) ||
|
|
72
|
+
!repo.writablePaths.some(g => matchesGitGlob(full, g)))
|
|
73
|
+
refused(path, 'write_policy');
|
|
74
|
+
}
|
|
75
|
+
return full;
|
|
76
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { GitPort, GitPortRepository, GitPortTarget, GitSourcePage } from './git-port.js';
|
|
2
|
+
import type { GitPortOptions, GitPortSession } from './git-port-session.js';
|
|
3
|
+
export declare function gitSourceReader(options: GitPortOptions, open: (repo: GitPortRepository) => Promise<GitPortSession>): (repo: GitPortRepository, target: GitPortTarget, input: Parameters<GitPort['read']>[1]) => Promise<GitSourcePage>;
|