@sentropic/artifacts 0.2.20 → 0.2.22
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 +36 -0
- package/dist/adapters/postgres-live.js +41 -0
- package/dist/git-tool-definitions.d.ts +168 -1
- package/dist/git-tool-definitions.js +11 -1
- package/dist/http.js +3 -2
- package/dist/mcp-core.d.ts +3 -3
- package/dist/mcp-core.js +5 -1
- package/dist/profile/contracts.d.ts +1 -1
- package/dist/profile/contracts.js +3 -0
- package/dist/profile/index.js +4 -2
- package/dist/profile/schemas.js +3 -0
- package/dist/profile/validation.js +2 -1
- package/dist/server/build-contract.d.ts +15 -1
- package/dist/server/build-contract.js +14 -5
- package/dist/server/build-dependencies.d.ts +12 -1
- package/dist/server/build-dependencies.js +41 -13
- package/dist/server/build-integrity.d.ts +48 -0
- package/dist/server/build-integrity.js +70 -0
- package/dist/server/build-output.d.ts +22 -0
- package/dist/server/build-output.js +40 -0
- package/dist/server/build-receipt.d.ts +51 -0
- package/dist/server/build-receipt.js +76 -0
- package/dist/server/failures.d.ts +2 -2
- package/dist/server/failures.js +1 -1
- package/dist/server/live-contracts.d.ts +37 -0
- package/dist/server/live-invitation-state.d.ts +9 -0
- package/dist/server/live-invitation-state.js +44 -0
- package/dist/server/live-invitations.d.ts +5 -0
- package/dist/server/live-invitations.js +178 -0
- package/dist/server/live-mutations.d.ts +1 -1
- package/dist/server/live-mutations.js +3 -2
- package/dist/server/live-read.d.ts +1 -1
- package/dist/server/live-read.js +2 -1
- package/dist/server/live-service.d.ts +5 -2
- package/dist/server/live-service.js +11 -1
- package/dist/server/live-store.d.ts +29 -1
- package/dist/server/live-store.js +36 -0
- package/migrations/0013_live_invitations.sql +101 -0
- package/package.json +1 -1
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { orderedDigest } from '../profile/json.js';
|
|
3
|
+
import { ArtifactOperationError } from './failures.js';
|
|
4
|
+
import { liveBusy } from './live-mutations.js';
|
|
5
|
+
import { UNKNOWN_USER_REF } from './ports.js';
|
|
6
|
+
import { currentLiveSession as currentSession, isOpenInvitation as open, sweepInvitations as sweep } from './live-invitation-state.js';
|
|
7
|
+
/** Generic live invitations (A-LIVE v2 §8.2, §9; contract 2026-11-07). Viewing only: no edit, publication or release grant. */
|
|
8
|
+
export const LIVE_INVITATION_OPERATIONS = ['live_invite', 'live_accept_invitation', 'live_revoke_invitation', 'live_invitations'];
|
|
9
|
+
export const LIVE_INVITATIONS_OPEN_MAX = 16;
|
|
10
|
+
const PAGE = 50;
|
|
11
|
+
const denied = () => new ArtifactOperationError('access_denied');
|
|
12
|
+
const iso = (ms) => new Date(ms).toISOString();
|
|
13
|
+
function admit(authority, invocation, artifactId) {
|
|
14
|
+
const { binding, target } = authority;
|
|
15
|
+
if (target.storageScopeId !== binding.storageScopeId || target.repoId !== binding.repoId || target.branch !== binding.branch ||
|
|
16
|
+
target.subPath !== binding.subPath || target.principalId !== invocation.actorId ||
|
|
17
|
+
binding.storageScopeId !== invocation.storageScopeId || binding.artifactId !== artifactId)
|
|
18
|
+
throw denied();
|
|
19
|
+
}
|
|
20
|
+
const terminal = (row) => {
|
|
21
|
+
if (row.status === 'ceased')
|
|
22
|
+
liveBusy('stale_session');
|
|
23
|
+
throw new ArtifactOperationError(row.status === 'expired' ? 'gone' : 'access_denied');
|
|
24
|
+
};
|
|
25
|
+
/** The onRevoked event of a revoked row; its epoch is frozen once the row leaves the open set. */
|
|
26
|
+
const revokedEvent = (row) => ({ storageScopeId: row.storageScopeId,
|
|
27
|
+
artifactId: row.artifactId, liveSessionId: row.liveSessionId, bindingGeneration: row.bindingGeneration,
|
|
28
|
+
invitationId: row.invitationId, epoch: row.epoch });
|
|
29
|
+
const cursorOf = (session, offset) => Buffer.from(JSON.stringify([session, offset])).toString('base64url');
|
|
30
|
+
function offsetOf(cursor, session) {
|
|
31
|
+
if (cursor === undefined)
|
|
32
|
+
return 0;
|
|
33
|
+
try {
|
|
34
|
+
const [owner, offset] = JSON.parse(Buffer.from(String(cursor), 'base64url').toString('utf8'));
|
|
35
|
+
if (owner === session && Number.isSafeInteger(offset) && offset > 0)
|
|
36
|
+
return offset;
|
|
37
|
+
}
|
|
38
|
+
catch { /* closed refusal below */ }
|
|
39
|
+
throw new ArtifactOperationError('invalid_request');
|
|
40
|
+
}
|
|
41
|
+
export async function executeLiveInvitation(deps, operation, args, invocation) {
|
|
42
|
+
const port = deps.invitations;
|
|
43
|
+
if (port?.qualified !== true)
|
|
44
|
+
throw new ArtifactOperationError('builder_unavailable');
|
|
45
|
+
const now = (deps.now ?? Date.now)();
|
|
46
|
+
const writer = operation !== 'live_invitations';
|
|
47
|
+
if (writer && (!args.idempotencyKey.trim() || args.idempotencyKey.length > (deps.idempotencyKeyMaxUnits ?? 256))) {
|
|
48
|
+
throw new ArtifactOperationError('invalid_request');
|
|
49
|
+
}
|
|
50
|
+
const authority = await deps.authorize(invocation, args.artifactId);
|
|
51
|
+
admit(authority, invocation, args.artifactId);
|
|
52
|
+
const owner = authority.ownerId === invocation.actorId;
|
|
53
|
+
const identity = { storageScopeId: invocation.storageScopeId, artifactId: authority.binding.artifactId,
|
|
54
|
+
bindingGeneration: authority.binding.generation };
|
|
55
|
+
let principalId;
|
|
56
|
+
if (operation === 'live_invite') {
|
|
57
|
+
if (!owner || !authority.canReadCode || !authority.canWrite || !authority.humanLinked)
|
|
58
|
+
throw denied();
|
|
59
|
+
if (!authority.qualified)
|
|
60
|
+
throw new ArtifactOperationError('builder_unavailable');
|
|
61
|
+
principalId = await port.resolvePrincipal(invocation.storageScopeId, args.userRef);
|
|
62
|
+
if (!principalId)
|
|
63
|
+
throw denied();
|
|
64
|
+
if (principalId === authority.ownerId)
|
|
65
|
+
throw new ArtifactOperationError('invalid_request');
|
|
66
|
+
const invitee = await deps.authorize({ ...invocation, actorId: principalId }, args.artifactId);
|
|
67
|
+
if (!invitee.canReadCode || !invitee.humanLinked || invitee.binding.generation !== identity.bindingGeneration)
|
|
68
|
+
throw denied();
|
|
69
|
+
}
|
|
70
|
+
if (operation === 'live_accept_invitation' && (!authority.canReadCode || !authority.humanLinked))
|
|
71
|
+
throw denied();
|
|
72
|
+
const command = writer ? { actorId: invocation.actorId, key: args.idempotencyKey,
|
|
73
|
+
hash: orderedDigest({ operation, args }), operation } : undefined;
|
|
74
|
+
let revoked;
|
|
75
|
+
const result = await deps.store.transaction(identity, async (tx) => {
|
|
76
|
+
const ledger = tx.invitations;
|
|
77
|
+
if (!ledger)
|
|
78
|
+
throw new ArtifactOperationError('capability_unselected');
|
|
79
|
+
if (command) {
|
|
80
|
+
const prior = await tx.receipt(command.actorId, command.key);
|
|
81
|
+
if (prior && prior.hash !== command.hash)
|
|
82
|
+
throw new ArtifactOperationError('command_conflict');
|
|
83
|
+
if (prior) {
|
|
84
|
+
// A replayed revoke re-invokes the idempotent onRevoked: the first push may have failed after the commit.
|
|
85
|
+
if (operation === 'live_revoke_invitation' && prior.result.status === 'revoked') {
|
|
86
|
+
const row = (await ledger.list()).find(entry => entry.invitationId === args.invitationId && entry.status === 'revoked');
|
|
87
|
+
if (row)
|
|
88
|
+
revoked = revokedEvent(row);
|
|
89
|
+
}
|
|
90
|
+
return { ...prior.result, replayed: true };
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const rows = await sweep(tx, identity, now, writer);
|
|
94
|
+
const session = currentSession(tx.record);
|
|
95
|
+
let out;
|
|
96
|
+
if (operation === 'live_invite') {
|
|
97
|
+
if (!session || args.liveSessionId !== session)
|
|
98
|
+
liveBusy('stale_session');
|
|
99
|
+
const opened = rows.filter(row => open(row) && row.liveSessionId === session);
|
|
100
|
+
if (opened.some(row => row.principalId === principalId))
|
|
101
|
+
liveBusy('active_session');
|
|
102
|
+
if (opened.length >= LIVE_INVITATIONS_OPEN_MAX)
|
|
103
|
+
throw new ArtifactOperationError('limit_exceeded');
|
|
104
|
+
let epoch = await ledger.epoch(identity.bindingGeneration, session);
|
|
105
|
+
if (epoch === 0) {
|
|
106
|
+
epoch = 1;
|
|
107
|
+
await ledger.setEpoch(identity.bindingGeneration, session, epoch);
|
|
108
|
+
}
|
|
109
|
+
const row = { ...identity, liveSessionId: session, invitationId: randomUUID(), principalId: principalId,
|
|
110
|
+
createdBy: invocation.actorId, status: 'pending', epoch, expiresAt: iso(now + args.expiresInSeconds * 1000),
|
|
111
|
+
acceptedAt: null, revokedAt: null, createdAt: iso(now), rowVersion: 1 };
|
|
112
|
+
await ledger.save(row);
|
|
113
|
+
out = { invitationId: row.invitationId, status: 'pending', liveSessionId: row.liveSessionId,
|
|
114
|
+
bindingGeneration: row.bindingGeneration, epoch, expiresAt: row.expiresAt };
|
|
115
|
+
}
|
|
116
|
+
else if (operation === 'live_invitations') {
|
|
117
|
+
const sessionId = args.liveSessionId ?? session;
|
|
118
|
+
const own = rows.filter(row => row.liveSessionId === sessionId);
|
|
119
|
+
const visible = owner ? own : own.filter(row => row.principalId === invocation.actorId);
|
|
120
|
+
if (!owner && !visible.length)
|
|
121
|
+
throw denied();
|
|
122
|
+
const generation = own[0]?.bindingGeneration ?? identity.bindingGeneration;
|
|
123
|
+
const offset = offsetOf(args.cursor, sessionId);
|
|
124
|
+
return { liveSessionId: sessionId, bindingGeneration: generation,
|
|
125
|
+
epoch: sessionId ? await ledger.epoch(generation, sessionId) : 0, rows: visible.slice(offset, offset + PAGE),
|
|
126
|
+
next: offset + PAGE < visible.length ? cursorOf(sessionId, offset + PAGE) : null };
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
const row = rows.find(entry => entry.invitationId === args.invitationId);
|
|
130
|
+
if (!row)
|
|
131
|
+
throw denied();
|
|
132
|
+
if (operation === 'live_accept_invitation') {
|
|
133
|
+
if (row.principalId !== invocation.actorId)
|
|
134
|
+
throw denied();
|
|
135
|
+
if (row.status === 'active')
|
|
136
|
+
liveBusy('active_session');
|
|
137
|
+
if (row.status !== 'pending')
|
|
138
|
+
terminal(row);
|
|
139
|
+
Object.assign(row, { status: 'active', acceptedAt: iso(now), rowVersion: row.rowVersion + 1 });
|
|
140
|
+
await ledger.save(row);
|
|
141
|
+
out = { invitationId: row.invitationId, status: 'active', liveSessionId: row.liveSessionId, epoch: row.epoch,
|
|
142
|
+
expiresAt: row.expiresAt };
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
if (!owner && row.principalId !== invocation.actorId)
|
|
146
|
+
throw denied();
|
|
147
|
+
if (row.status !== 'revoked') {
|
|
148
|
+
if (!open(row))
|
|
149
|
+
terminal(row);
|
|
150
|
+
const epoch = await ledger.epoch(row.bindingGeneration, row.liveSessionId) + 1;
|
|
151
|
+
await ledger.setEpoch(row.bindingGeneration, row.liveSessionId, epoch);
|
|
152
|
+
for (const other of rows.filter(entry => open(entry) && entry.liveSessionId === row.liveSessionId)) {
|
|
153
|
+
if (other === row)
|
|
154
|
+
Object.assign(other, { status: 'revoked', revokedAt: iso(now) });
|
|
155
|
+
Object.assign(other, { epoch, rowVersion: other.rowVersion + 1 });
|
|
156
|
+
await ledger.save(other);
|
|
157
|
+
}
|
|
158
|
+
revoked = revokedEvent(row);
|
|
159
|
+
}
|
|
160
|
+
out = { invitationId: row.invitationId, status: 'revoked', epoch: row.epoch };
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
out.replayed = false;
|
|
164
|
+
await tx.complete(command, out);
|
|
165
|
+
return out;
|
|
166
|
+
});
|
|
167
|
+
if (revoked)
|
|
168
|
+
await port.onRevoked(revoked);
|
|
169
|
+
if (operation !== 'live_invitations')
|
|
170
|
+
return result;
|
|
171
|
+
const { rows, ...page } = result;
|
|
172
|
+
return { ...page, invitations: await Promise.all(rows.map(async (row) => {
|
|
173
|
+
const person = await port.describePrincipal(invocation.storageScopeId, row.principalId);
|
|
174
|
+
const userRef = person?.userRef ?? UNKNOWN_USER_REF;
|
|
175
|
+
return { invitationId: row.invitationId, invitee: { userRef, displayName: person?.displayName ?? userRef },
|
|
176
|
+
status: row.status, expiresAt: row.expiresAt, acceptedAt: row.acceptedAt };
|
|
177
|
+
})) };
|
|
178
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type LiveBaseTree } from './live-tree.js';
|
|
2
2
|
import { type LiveAuthority, type LiveServiceDeps } from './live-contracts.js';
|
|
3
|
-
import type
|
|
3
|
+
import { type LiveOverlayEntry, type LiveTransaction } from './live-store.js';
|
|
4
4
|
export declare const liveBusy: (reason: import('./live-errors.js').LiveConflictReason) => never;
|
|
5
5
|
export declare function mutateLive(input: {
|
|
6
6
|
deps: LiveServiceDeps;
|
|
@@ -3,6 +3,7 @@ import { ArtifactOperationError } from './failures.js';
|
|
|
3
3
|
import { persistLiveOverlay } from './live-drafts.js';
|
|
4
4
|
import { applyLiveMutation, liveTreeSha } from './live-tree.js';
|
|
5
5
|
import { liveResponse } from './live-contracts.js';
|
|
6
|
+
import { LIVE_IDLE_THRESHOLD_MS } from './live-store.js';
|
|
6
7
|
export const liveBusy = (reason) => {
|
|
7
8
|
throw new ArtifactOperationError('git_live_conflict', { publicDetails: { reason } });
|
|
8
9
|
};
|
|
@@ -68,14 +69,14 @@ export async function mutateLive(input) {
|
|
|
68
69
|
origin: operation === 'live_control' ? 'explicit' : 'implicit', rowVersion: 1,
|
|
69
70
|
ownerId: authority.ownerId, podUid: null, sourceRevision: record.revision,
|
|
70
71
|
baseHeadSha: record.baseHeadSha, liveTreeSha: record.liveTreeSha,
|
|
71
|
-
idleDeadline: new Date(now +
|
|
72
|
+
idleDeadline: new Date(now + LIVE_IDLE_THRESHOLD_MS).toISOString(),
|
|
72
73
|
activeAttemptId: null, stateDetail: null };
|
|
73
74
|
Object.assign(record.runtime, { sourceRevision: record.revision, baseHeadSha: record.baseHeadSha,
|
|
74
75
|
liveTreeSha: record.liveTreeSha, rowVersion: record.runtime.rowVersion + 1 });
|
|
75
76
|
if (operation === 'live_control')
|
|
76
77
|
record.runtime.renewOnReady = true;
|
|
77
78
|
record.runtime.lastAccessAt = new Date(now).toISOString();
|
|
78
|
-
record.runtime.idleDeadline = new Date(now +
|
|
79
|
+
record.runtime.idleDeadline = new Date(now + LIVE_IDLE_THRESHOLD_MS).toISOString();
|
|
79
80
|
}
|
|
80
81
|
}
|
|
81
82
|
return { action, result: { ...liveResponse(record, currentHeadSha, now), state: record.runtime?.phase ?? 'DORMANT',
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { GitPort } from './git-port.js';
|
|
2
2
|
import type { LiveAuthority, LiveInvocation } from './live-contracts.js';
|
|
3
|
-
import type
|
|
3
|
+
import { type LiveDraftRecord, type LiveOverlayEntry } from './live-store.js';
|
|
4
4
|
import type { LiveBaseTree } from './live-tree.js';
|
|
5
5
|
export declare function readLivePage(input: {
|
|
6
6
|
git: GitPort;
|
package/dist/server/live-read.js
CHANGED
|
@@ -2,6 +2,7 @@ import { randomUUID } from 'node:crypto';
|
|
|
2
2
|
import { ArtifactOperationError } from './failures.js';
|
|
3
3
|
import { gitPath } from './git-port-policy.js';
|
|
4
4
|
import { liveDigest } from './live-crypto.js';
|
|
5
|
+
import { LIVE_IDLE_THRESHOLD_MS } from './live-store.js';
|
|
5
6
|
export async function readLivePage(input) {
|
|
6
7
|
const { git, authority, record, overlay, base, args, invocation, kind, now } = input;
|
|
7
8
|
let snapshot = { actorId: invocation.actorId, revision: record.revision,
|
|
@@ -89,7 +90,7 @@ export async function readLivePage(input) {
|
|
|
89
90
|
}
|
|
90
91
|
if (kind === 'read' && record.runtime) {
|
|
91
92
|
record.runtime.lastAccessAt = new Date(now).toISOString();
|
|
92
|
-
record.runtime.idleDeadline = new Date(now +
|
|
93
|
+
record.runtime.idleDeadline = new Date(now + LIVE_IDLE_THRESHOLD_MS).toISOString();
|
|
93
94
|
}
|
|
94
95
|
return kind === 'read' ? { files, nextCursor } : { diff, files: recipe, deletes, nextCursor };
|
|
95
96
|
}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type LiveServiceDeps, type LiveSessionService } from './live-contracts.js';
|
|
2
2
|
export type * from './live-contracts.js';
|
|
3
|
-
export
|
|
3
|
+
export type { LiveInvitationLedger, LiveInvitationRecord, LiveInvitationStatus } from './live-store.js';
|
|
4
|
+
export { LIVE_INVITATIONS_OPEN_MAX } from './live-invitations.js';
|
|
5
|
+
export { LIVE_INVITATION_TOOL_CLASSES } from '../git-tool-definitions.js';
|
|
6
|
+
export declare function createLiveService(deps: LiveServiceDeps): LiveSessionService;
|
|
@@ -11,10 +11,20 @@ import { liveBusy, mutateLive } from './live-mutations.js';
|
|
|
11
11
|
import { executeLiveCommit } from './live-commit.js';
|
|
12
12
|
import { settleLiveWorker } from './live-worker.js';
|
|
13
13
|
import { liveResponse } from './live-contracts.js';
|
|
14
|
+
import { executeLiveInvitation, LIVE_INVITATION_OPERATIONS } from './live-invitations.js';
|
|
15
|
+
import { liveInvitationViews } from './live-invitation-state.js';
|
|
16
|
+
export { LIVE_INVITATIONS_OPEN_MAX } from './live-invitations.js';
|
|
17
|
+
export { LIVE_INVITATION_TOOL_CLASSES } from '../git-tool-definitions.js';
|
|
14
18
|
export function createLiveService(deps) {
|
|
15
19
|
const now = deps.now ?? Date.now;
|
|
16
|
-
return { async execute(operation, raw, invocation) {
|
|
20
|
+
return { ...liveInvitationViews(deps), async execute(operation, raw, invocation) {
|
|
17
21
|
const args = structuredClone(raw);
|
|
22
|
+
if (LIVE_INVITATION_OPERATIONS.includes(operation)) {
|
|
23
|
+
if (!matchesSchema(args, GIT_TOOL_DEFINITIONS[`artifacts_${operation}`].inputSchema)) {
|
|
24
|
+
throw new ArtifactOperationError('invalid_request');
|
|
25
|
+
}
|
|
26
|
+
return executeLiveInvitation(deps, operation, args, invocation);
|
|
27
|
+
}
|
|
18
28
|
if (!['live_write', 'live_read', 'live_reset', 'live_status', 'live_control', 'git_commit'].includes(operation) ||
|
|
19
29
|
!matchesSchema(args, GIT_TOOL_DEFINITIONS[`artifacts_${operation}`].inputSchema)) {
|
|
20
30
|
throw new ArtifactOperationError('invalid_request');
|
|
@@ -3,6 +3,8 @@ export interface LiveIdentity {
|
|
|
3
3
|
artifactId: string;
|
|
4
4
|
bindingGeneration: number;
|
|
5
5
|
}
|
|
6
|
+
/** A-LIVE Amendment 1: idle strictly after 900 s since the last qualifying access; there is no absolute lifetime. */
|
|
7
|
+
export declare const LIVE_IDLE_THRESHOLD_MS = 900000;
|
|
6
8
|
export interface LiveOverlayEntry {
|
|
7
9
|
path: string;
|
|
8
10
|
text: string | null;
|
|
@@ -20,7 +22,6 @@ export interface LiveRuntimeState {
|
|
|
20
22
|
baseHeadSha: string;
|
|
21
23
|
liveTreeSha: string;
|
|
22
24
|
idleDeadline: string;
|
|
23
|
-
maximumLifetimeAt: string;
|
|
24
25
|
activeAttemptId: string | null;
|
|
25
26
|
stateDetail: string | null;
|
|
26
27
|
appliedRevision: string | null;
|
|
@@ -85,6 +86,31 @@ export interface LiveReceipt {
|
|
|
85
86
|
hash: string;
|
|
86
87
|
result: LiveResult;
|
|
87
88
|
}
|
|
89
|
+
export type LiveInvitationStatus = 'pending' | 'active' | 'revoked' | 'expired' | 'ceased';
|
|
90
|
+
/** Dedicated session-bound live authorization record (A-LIVE v2 §8.2, §9); never a release grant. */
|
|
91
|
+
export interface LiveInvitationRecord extends LiveIdentity {
|
|
92
|
+
liveSessionId: string;
|
|
93
|
+
invitationId: string;
|
|
94
|
+
principalId: string;
|
|
95
|
+
createdBy: string;
|
|
96
|
+
status: LiveInvitationStatus;
|
|
97
|
+
epoch: number;
|
|
98
|
+
expiresAt: string;
|
|
99
|
+
acceptedAt: string | null;
|
|
100
|
+
revokedAt: string | null;
|
|
101
|
+
createdAt: string;
|
|
102
|
+
rowVersion: number;
|
|
103
|
+
}
|
|
104
|
+
/** Invitations of the transaction's artifact, serialized by the same lock as its live record. */
|
|
105
|
+
export interface LiveInvitationLedger {
|
|
106
|
+
/** Every invitation of the artifact in this storage scope, across generations and sessions. */
|
|
107
|
+
list(): Promise<LiveInvitationRecord[]>;
|
|
108
|
+
/** Insert at rowVersion 1, otherwise compare-and-swap on rowVersion - 1. */
|
|
109
|
+
save(record: LiveInvitationRecord): Promise<void>;
|
|
110
|
+
/** The session invitation epoch; 0 before the first invitation. */
|
|
111
|
+
epoch(bindingGeneration: number, liveSessionId: string): Promise<number>;
|
|
112
|
+
setEpoch(bindingGeneration: number, liveSessionId: string, epoch: number): Promise<void>;
|
|
113
|
+
}
|
|
88
114
|
export interface LiveTransaction {
|
|
89
115
|
record: LiveDraftRecord | undefined;
|
|
90
116
|
getObject(ref: string): Promise<LiveEnvelope | undefined>;
|
|
@@ -101,6 +127,8 @@ export interface LiveTransaction {
|
|
|
101
127
|
actorId: string;
|
|
102
128
|
now: string;
|
|
103
129
|
}): Promise<void>;
|
|
130
|
+
/** Live invitations (A-LIVE v2 §9); absent when the adapter does not qualify them. */
|
|
131
|
+
invitations?: LiveInvitationLedger;
|
|
104
132
|
}
|
|
105
133
|
export interface LiveStore {
|
|
106
134
|
transaction<T>(identity: LiveIdentity, action: (tx: LiveTransaction) => Promise<T>): Promise<T>;
|
|
@@ -1,14 +1,47 @@
|
|
|
1
1
|
import { createKeyMutex } from './ports.js';
|
|
2
2
|
import { ArtifactOperationError } from './failures.js';
|
|
3
|
+
/** A-LIVE Amendment 1: idle strictly after 900 s since the last qualifying access; there is no absolute lifetime. */
|
|
4
|
+
export const LIVE_IDLE_THRESHOLD_MS = 900_000;
|
|
3
5
|
export const liveIdentityKey = (identity) => JSON.stringify([
|
|
4
6
|
identity.storageScopeId, identity.artifactId, identity.bindingGeneration
|
|
5
7
|
]);
|
|
8
|
+
const openInvitation = (row) => row.status === 'pending' || row.status === 'active';
|
|
9
|
+
function memoryInvitations(identity, rows, epochs) {
|
|
10
|
+
const pending = new Map();
|
|
11
|
+
const pendingEpochs = new Map();
|
|
12
|
+
const epochKey = (generation, session) => JSON.stringify([identity.storageScopeId, identity.artifactId, generation, session]);
|
|
13
|
+
const own = () => [...new Map([...rows, ...pending]).values()].filter(row => row.storageScopeId === identity.storageScopeId && row.artifactId === identity.artifactId);
|
|
14
|
+
const ledger = {
|
|
15
|
+
list: async () => structuredClone(own()),
|
|
16
|
+
async save(row) {
|
|
17
|
+
const prior = pending.get(row.invitationId) ?? rows.get(row.invitationId);
|
|
18
|
+
if (row.storageScopeId !== identity.storageScopeId || row.artifactId !== identity.artifactId || (prior &&
|
|
19
|
+
(prior.storageScopeId !== row.storageScopeId || prior.artifactId !== row.artifactId)))
|
|
20
|
+
throw new ArtifactOperationError('access_denied');
|
|
21
|
+
if ((prior?.rowVersion ?? 0) + 1 !== row.rowVersion)
|
|
22
|
+
throw new ArtifactOperationError('git_live_conflict', { publicDetails: { reason: 'stale_fence' } });
|
|
23
|
+
if (openInvitation(row) && own().some(other => other.invitationId !== row.invitationId && openInvitation(other) &&
|
|
24
|
+
other.bindingGeneration === row.bindingGeneration && other.liveSessionId === row.liveSessionId &&
|
|
25
|
+
other.principalId === row.principalId))
|
|
26
|
+
throw new ArtifactOperationError('git_live_conflict', { publicDetails: { reason: 'active_session' } });
|
|
27
|
+
pending.set(row.invitationId, structuredClone(row));
|
|
28
|
+
},
|
|
29
|
+
epoch: async (generation, session) => pendingEpochs.get(epochKey(generation, session)) ?? epochs.get(epochKey(generation, session)) ?? 0,
|
|
30
|
+
async setEpoch(generation, session, epoch) { pendingEpochs.set(epochKey(generation, session), epoch); },
|
|
31
|
+
};
|
|
32
|
+
const commit = () => { for (const [k, v] of pending)
|
|
33
|
+
rows.set(k, v); for (const [k, v] of pendingEpochs)
|
|
34
|
+
epochs.set(k, v); };
|
|
35
|
+
return { ledger, commit };
|
|
36
|
+
}
|
|
6
37
|
/** Atomic fake; the persisted representation contains encrypted objects and metadata only. */
|
|
7
38
|
export function createMemoryLiveStore() {
|
|
8
39
|
const records = new Map();
|
|
9
40
|
const objects = new Map();
|
|
10
41
|
const receipts = new Map();
|
|
11
42
|
const quota = [];
|
|
43
|
+
const invitations = new Map();
|
|
44
|
+
const epochs = new Map();
|
|
12
45
|
const mutex = createKeyMutex();
|
|
13
46
|
return {
|
|
14
47
|
async sessionIdentity(scope, artifactId, sessionId) {
|
|
@@ -21,9 +54,11 @@ export function createMemoryLiveStore() {
|
|
|
21
54
|
const pendingObjects = new Map();
|
|
22
55
|
const pendingReceipts = new Map();
|
|
23
56
|
const pendingQuota = [];
|
|
57
|
+
const invitationTx = memoryInvitations(identity, invitations, epochs);
|
|
24
58
|
const receiptKey = (actor, key) => JSON.stringify([identity.storageScopeId, actor, key]);
|
|
25
59
|
const tx = {
|
|
26
60
|
record: structuredClone(records.get(id)),
|
|
61
|
+
invitations: invitationTx.ledger,
|
|
27
62
|
getObject: async (ref) => structuredClone(pendingObjects.get(id + ref) ?? objects.get(id + ref)),
|
|
28
63
|
async putObject(ref, value) {
|
|
29
64
|
if (objects.has(id + ref) || pendingObjects.has(id + ref))
|
|
@@ -74,6 +109,7 @@ export function createMemoryLiveStore() {
|
|
|
74
109
|
for (const [key, receipt] of pendingReceipts)
|
|
75
110
|
receipts.set(key, receipt);
|
|
76
111
|
quota.push(...pendingQuota);
|
|
112
|
+
invitationTx.commit();
|
|
77
113
|
return structuredClone(result);
|
|
78
114
|
})
|
|
79
115
|
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
-- A-LIVE v2 §8.2/§9 generic live invitations (contract 2026-11-07): session-bound, viewing only, never a release grant.
|
|
2
|
+
-- Additive. The final guard refuses a pre-existing table of the same name with any other column or index, because
|
|
3
|
+
-- CREATE TABLE IF NOT EXISTS alone would silently keep a foreign schema.
|
|
4
|
+
CREATE TABLE IF NOT EXISTS artifacts.live_invitations (
|
|
5
|
+
storage_scope_id text NOT NULL,
|
|
6
|
+
artifact_id uuid NOT NULL,
|
|
7
|
+
binding_generation bigint NOT NULL CHECK (binding_generation > 0),
|
|
8
|
+
live_session_id uuid NOT NULL,
|
|
9
|
+
invitation_id uuid NOT NULL,
|
|
10
|
+
principal_id text NOT NULL,
|
|
11
|
+
created_by text NOT NULL,
|
|
12
|
+
status text NOT NULL CHECK (status IN ('pending', 'active', 'revoked', 'expired', 'ceased')),
|
|
13
|
+
epoch bigint NOT NULL CHECK (epoch > 0),
|
|
14
|
+
expires_at timestamptz NOT NULL,
|
|
15
|
+
accepted_at timestamptz,
|
|
16
|
+
revoked_at timestamptz,
|
|
17
|
+
created_at timestamptz NOT NULL,
|
|
18
|
+
row_version bigint NOT NULL CHECK (row_version > 0),
|
|
19
|
+
CONSTRAINT live_invitations_pkey PRIMARY KEY (invitation_id),
|
|
20
|
+
CONSTRAINT live_invitations_artifact_fk FOREIGN KEY (artifact_id, storage_scope_id)
|
|
21
|
+
REFERENCES artifacts.artifacts (id, storage_scope_id),
|
|
22
|
+
CONSTRAINT live_invitations_invitee_check CHECK (principal_id <> created_by),
|
|
23
|
+
CONSTRAINT live_invitations_accepted_check CHECK ((status <> 'pending' OR accepted_at IS NULL)
|
|
24
|
+
AND (status <> 'active' OR accepted_at IS NOT NULL)),
|
|
25
|
+
CONSTRAINT live_invitations_revoked_check CHECK ((status = 'revoked') = (revoked_at IS NOT NULL))
|
|
26
|
+
);
|
|
27
|
+
CREATE INDEX IF NOT EXISTS live_invitations_session_idx
|
|
28
|
+
ON artifacts.live_invitations (storage_scope_id, artifact_id, binding_generation, live_session_id);
|
|
29
|
+
CREATE UNIQUE INDEX IF NOT EXISTS live_invitations_open_principal_uq
|
|
30
|
+
ON artifacts.live_invitations (storage_scope_id, artifact_id, binding_generation, live_session_id, principal_id)
|
|
31
|
+
WHERE status IN ('pending', 'active');
|
|
32
|
+
|
|
33
|
+
-- One epoch row per live session: a revocation raises it and every open invitation of the session adopts it.
|
|
34
|
+
CREATE TABLE IF NOT EXISTS artifacts.live_invitation_epochs (
|
|
35
|
+
storage_scope_id text NOT NULL,
|
|
36
|
+
artifact_id uuid NOT NULL,
|
|
37
|
+
binding_generation bigint NOT NULL CHECK (binding_generation > 0),
|
|
38
|
+
live_session_id uuid NOT NULL,
|
|
39
|
+
epoch bigint NOT NULL CHECK (epoch > 0),
|
|
40
|
+
CONSTRAINT live_invitation_epochs_pkey PRIMARY KEY (storage_scope_id, artifact_id, binding_generation, live_session_id),
|
|
41
|
+
CONSTRAINT live_invitation_epochs_artifact_fk FOREIGN KEY (artifact_id, storage_scope_id)
|
|
42
|
+
REFERENCES artifacts.artifacts (id, storage_scope_id)
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
-- Mandatory guard: the exact columns (information_schema.columns) and indexes (pg_indexes) of both tables.
|
|
46
|
+
DO $$
|
|
47
|
+
DECLARE drift text;
|
|
48
|
+
BEGIN
|
|
49
|
+
WITH expected (table_name, column_name, data_type, is_nullable) AS (VALUES
|
|
50
|
+
('live_invitations', 'storage_scope_id', 'text', 'NO'), ('live_invitations', 'artifact_id', 'uuid', 'NO'),
|
|
51
|
+
('live_invitations', 'binding_generation', 'bigint', 'NO'), ('live_invitations', 'live_session_id', 'uuid', 'NO'),
|
|
52
|
+
('live_invitations', 'invitation_id', 'uuid', 'NO'), ('live_invitations', 'principal_id', 'text', 'NO'),
|
|
53
|
+
('live_invitations', 'created_by', 'text', 'NO'), ('live_invitations', 'status', 'text', 'NO'),
|
|
54
|
+
('live_invitations', 'epoch', 'bigint', 'NO'), ('live_invitations', 'expires_at', 'timestamp with time zone', 'NO'),
|
|
55
|
+
('live_invitations', 'accepted_at', 'timestamp with time zone', 'YES'),
|
|
56
|
+
('live_invitations', 'revoked_at', 'timestamp with time zone', 'YES'),
|
|
57
|
+
('live_invitations', 'created_at', 'timestamp with time zone', 'NO'), ('live_invitations', 'row_version', 'bigint', 'NO'),
|
|
58
|
+
('live_invitation_epochs', 'storage_scope_id', 'text', 'NO'), ('live_invitation_epochs', 'artifact_id', 'uuid', 'NO'),
|
|
59
|
+
('live_invitation_epochs', 'binding_generation', 'bigint', 'NO'),
|
|
60
|
+
('live_invitation_epochs', 'live_session_id', 'uuid', 'NO'), ('live_invitation_epochs', 'epoch', 'bigint', 'NO')
|
|
61
|
+
), actual AS (
|
|
62
|
+
SELECT c.table_name::text AS table_name, c.column_name::text AS column_name, c.data_type::text AS data_type,
|
|
63
|
+
c.is_nullable::text AS is_nullable
|
|
64
|
+
FROM information_schema.columns c
|
|
65
|
+
WHERE c.table_schema = 'artifacts' AND c.table_name IN ('live_invitations', 'live_invitation_epochs')
|
|
66
|
+
)
|
|
67
|
+
SELECT string_agg(coalesce(e.table_name, a.table_name) || '.' || coalesce(e.column_name, a.column_name), ', ') INTO drift
|
|
68
|
+
FROM expected e FULL JOIN actual a ON a.table_name = e.table_name AND a.column_name = e.column_name
|
|
69
|
+
AND a.data_type = e.data_type AND a.is_nullable = e.is_nullable
|
|
70
|
+
WHERE e.table_name IS NULL OR a.table_name IS NULL;
|
|
71
|
+
IF drift IS NOT NULL THEN
|
|
72
|
+
RAISE EXCEPTION 'live invitations schema mismatch (columns): %', drift;
|
|
73
|
+
END IF;
|
|
74
|
+
WITH expected (table_name, index_name, is_unique, is_partial, key_columns) AS (VALUES
|
|
75
|
+
('live_invitations', 'live_invitations_pkey', true, false, 'invitation_id'),
|
|
76
|
+
('live_invitations', 'live_invitations_session_idx', false, false,
|
|
77
|
+
'storage_scope_id,artifact_id,binding_generation,live_session_id'),
|
|
78
|
+
('live_invitations', 'live_invitations_open_principal_uq', true, true,
|
|
79
|
+
'storage_scope_id,artifact_id,binding_generation,live_session_id,principal_id'),
|
|
80
|
+
('live_invitation_epochs', 'live_invitation_epochs_pkey', true, false,
|
|
81
|
+
'storage_scope_id,artifact_id,binding_generation,live_session_id')
|
|
82
|
+
), actual AS (
|
|
83
|
+
SELECT p.tablename::text AS table_name, p.indexname::text AS index_name, x.indisunique AS is_unique,
|
|
84
|
+
x.indpred IS NOT NULL AS is_partial,
|
|
85
|
+
(SELECT string_agg(att.attname::text, ',' ORDER BY k.ord)
|
|
86
|
+
FROM unnest(x.indkey::int2[]) WITH ORDINALITY AS k(attnum, ord)
|
|
87
|
+
JOIN pg_attribute att ON att.attrelid = x.indrelid AND att.attnum = k.attnum) AS key_columns
|
|
88
|
+
FROM pg_indexes p
|
|
89
|
+
JOIN pg_class i ON i.relname = p.indexname AND i.relnamespace = 'artifacts'::regnamespace
|
|
90
|
+
JOIN pg_index x ON x.indexrelid = i.oid
|
|
91
|
+
WHERE p.schemaname = 'artifacts' AND p.tablename IN ('live_invitations', 'live_invitation_epochs')
|
|
92
|
+
)
|
|
93
|
+
SELECT string_agg(coalesce(e.table_name, a.table_name) || '.' || coalesce(e.index_name, a.index_name), ', ') INTO drift
|
|
94
|
+
FROM expected e FULL JOIN actual a ON a.table_name = e.table_name AND a.index_name = e.index_name
|
|
95
|
+
AND a.is_unique = e.is_unique AND a.is_partial = e.is_partial AND a.key_columns = e.key_columns
|
|
96
|
+
WHERE e.index_name IS NULL OR a.index_name IS NULL;
|
|
97
|
+
IF drift IS NOT NULL THEN
|
|
98
|
+
RAISE EXCEPTION 'live invitations schema mismatch (indexes): %', drift;
|
|
99
|
+
END IF;
|
|
100
|
+
END
|
|
101
|
+
$$;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sentropic/artifacts",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.22",
|
|
4
4
|
"description": "Generic configurable artifacts MCP and artifact service: StorageScope/Artifact/Binding/Release model, canonical manifests, object authorization, idempotent intents (spec-v8 §9).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|