maka-agent 0.2.0-dev.32.20260914 → 0.2.0-dev.35.20260915
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/native/runtime-host-windows-task-launcher/prebuilds/win32-x64/maka-runtime-host-task-launcher.exe +0 -0
- package/node_modules/@maka/eval/dist/metering-checkpoint.js +4 -4
- package/node_modules/@maka/eval/harbor/relay_agent.py +1 -2
- package/node_modules/@maka/runtime/dist/ai-sdk-turn.js +2 -18
- package/node_modules/@maka/runtime/dist/openai-codex-history-compactor.js +1 -2
- package/node_modules/@maka/runtime/dist/provider-error-classification.js +29 -38
- package/node_modules/@maka/runtime-host/dist/client/connect-or-spawn.js +4 -1
- package/node_modules/@maka/runtime-host/dist/client/wait-for-ready.js +1 -1
- package/node_modules/@maka/runtime-host/dist/control/endpoint.js +7 -6
- package/node_modules/@maka/runtime-host/dist/server/execution-composition.js +2 -2
- package/node_modules/@maka/runtime-host/dist/server/workhub-message-attachments.js +1 -0
- package/node_modules/@maka/storage/dist/agent-run-store-contract.js +942 -0
- package/node_modules/@maka/storage/dist/agent-run-store.js +3 -923
- package/node_modules/@maka/storage/dist/artifact-store.js +63 -7
- package/node_modules/@maka/storage/dist/execution-persistence-provider.js +54 -0
- package/node_modules/@maka/storage/dist/execution-stores.js +181 -58
- package/node_modules/@maka/storage/dist/goal-authority.js +39 -8
- package/node_modules/@maka/storage/dist/graph-control-values.js +126 -0
- package/node_modules/@maka/storage/dist/interaction-store-contract.js +231 -0
- package/node_modules/@maka/storage/dist/interaction-store.js +62 -217
- package/node_modules/@maka/storage/dist/local-execution-persistence.js +80 -0
- package/node_modules/@maka/storage/dist/root-authority.js +4 -0
- package/node_modules/@maka/storage/dist/runtime-event-store-contract.js +19 -0
- package/node_modules/@maka/storage/dist/runtime-partial-values.js +114 -0
- package/node_modules/@maka/storage/dist/session-store-contract.js +61 -0
- package/node_modules/@maka/storage/dist/session-store-values.js +342 -0
- package/node_modules/@maka/storage/dist/session-store.js +7 -351
- package/node_modules/@maka/storage/dist/sqlite-runtime-store.js +4 -185
- package/node_modules/@maka/storage/dist/sqlite-session-metadata-store.js +9 -129
- package/node_modules/@maka/storage/dist/storage-writer-composition.js +2 -3
- package/node_modules/@maka/storage/dist/tool-commit-validation.js +111 -0
- package/node_modules/@maka/storage/package.json +1 -0
- package/package.json +1 -1
|
@@ -16,44 +16,19 @@
|
|
|
16
16
|
* specific language governing permissions and limitations
|
|
17
17
|
* under the License.
|
|
18
18
|
*/
|
|
19
|
+
import { assertCoordinationIdentityPairing, buildSessionHeader, toSummary, } from './session-store-values.js';
|
|
20
|
+
export { normalizeSessionHeader, decodePersistedSessionHeader, createUserMessage, } from './session-store-values.js';
|
|
21
|
+
import { SessionMetadataVersionConflictError, EXTERNAL_SESSION_IMPORT_LOOKUP_MAX_SOURCE_IDS, EXTERNAL_SESSION_IMPORT_LOOKUP_MAX_RECENT_SESSION_IDS, } from './session-store-contract.js';
|
|
22
|
+
export { isSafeSessionId, assertSafeSessionId, SessionNotFoundError, isSessionNotFoundError, EXTERNAL_SESSION_IMPORT_LOOKUP_MAX_SOURCE_IDS, EXTERNAL_SESSION_IMPORT_LOOKUP_MAX_RECENT_SESSION_IDS, } from './session-store-contract.js';
|
|
19
23
|
import { join } from 'node:path';
|
|
20
|
-
import { createHash
|
|
21
|
-
import { createSqliteSessionMetadataStore,
|
|
24
|
+
import { createHash } from 'node:crypto';
|
|
25
|
+
import { createSqliteSessionMetadataStore, } from './sqlite-session-metadata-store.js';
|
|
22
26
|
import { isDiscardableConversationCopy } from './session-conversation-copy.js';
|
|
23
27
|
import { acquireOperationalStateDatabase, OPERATIONAL_STATE_DATABASE_NAME, } from './operational-state-store.js';
|
|
24
28
|
import { DEFAULT_SESSION_NAME, normalizeUserSessionName } from '@maka/core/session-name';
|
|
25
|
-
import {
|
|
26
|
-
import { decodeCanonicalMessage, deriveTurnRecords, isSessionBlockedReason, isSessionConversationCopy, isSubagentSessionParent, isSubagentSessionRuntime, isSubagentSessionSpawn, isSessionStatus, isWorkHubCoordinationSessionId, subagentSessionRuntimeSummary, WORKHUB_COORDINATION_SESSION_ID, WORKHUB_COORDINATION_SESSION_ROLE, } from '@maka/core/session';
|
|
27
|
-
import { isCollaborationMode } from '@maka/core/collaboration';
|
|
28
|
-
import { isOrchestrationMode } from '@maka/core/orchestration';
|
|
29
|
-
import { decodePersistedPermissionMode, isPermissionMode } from '@maka/core/permission';
|
|
30
|
-
import { isSubagentWorkspaceBinding } from '@maka/core/subagent-workspace';
|
|
31
|
-
import { DEFAULT_TOOL_MODE, isToolMode } from '@maka/core/tool-mode';
|
|
32
|
-
import { isSessionToolProfile, } from '@maka/core/session';
|
|
29
|
+
import { decodeCanonicalMessage, deriveTurnRecords, WORKHUB_COORDINATION_SESSION_ID, } from '@maka/core/session';
|
|
33
30
|
import { projectSessionCatalogMessages } from './session-message-projection.js';
|
|
34
31
|
export { projectSessionCatalogMessages };
|
|
35
|
-
const SESSION_ID_PATTERN = /^[A-Za-z0-9_-]{1,128}$/;
|
|
36
|
-
export function isSafeSessionId(sessionId) {
|
|
37
|
-
return SESSION_ID_PATTERN.test(sessionId);
|
|
38
|
-
}
|
|
39
|
-
export function assertSafeSessionId(sessionId) {
|
|
40
|
-
if (!isSafeSessionId(sessionId))
|
|
41
|
-
throw new Error(`Invalid Session id: ${sessionId}`);
|
|
42
|
-
}
|
|
43
|
-
export class SessionNotFoundError extends Error {
|
|
44
|
-
sessionId;
|
|
45
|
-
name = 'SessionNotFoundError';
|
|
46
|
-
code = 'session_not_found';
|
|
47
|
-
constructor(sessionId) {
|
|
48
|
-
super(`Session metadata not found: ${sessionId}`);
|
|
49
|
-
this.sessionId = sessionId;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
export function isSessionNotFoundError(error) {
|
|
53
|
-
return error instanceof SessionNotFoundError;
|
|
54
|
-
}
|
|
55
|
-
export const EXTERNAL_SESSION_IMPORT_LOOKUP_MAX_SOURCE_IDS = 256;
|
|
56
|
-
export const EXTERNAL_SESSION_IMPORT_LOOKUP_MAX_RECENT_SESSION_IDS = 16;
|
|
57
32
|
export function createSessionStore(workspaceRoot) {
|
|
58
33
|
return new SqliteSessionStore(workspaceRoot);
|
|
59
34
|
}
|
|
@@ -560,265 +535,6 @@ class SqliteSessionStore {
|
|
|
560
535
|
function workHubIdentitySuffix(value) {
|
|
561
536
|
return createHash('sha256').update(value, 'utf8').digest('hex').slice(0, 48);
|
|
562
537
|
}
|
|
563
|
-
/**
|
|
564
|
-
* The reserved identity and the reserved role are one fact, and the invariant
|
|
565
|
-
* belongs to every creator that builds a header — subagents and Agent Graph
|
|
566
|
-
* operators included, whose inputs carry no role today.
|
|
567
|
-
*/
|
|
568
|
-
function assertCoordinationIdentityPairing(sessionId, role) {
|
|
569
|
-
if (isWorkHubCoordinationSessionId(sessionId) !== (role === WORKHUB_COORDINATION_SESSION_ROLE)) {
|
|
570
|
-
throw new Error('WorkHub Coordination Session identity and role must be claimed together');
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
function buildSessionHeader(workspaceRoot, input, sessionId = randomUUID(), conversationCopy) {
|
|
574
|
-
if (input.projectId !== undefined &&
|
|
575
|
-
input.projectId !== null &&
|
|
576
|
-
(typeof input.projectId !== 'string' || input.projectId.length === 0)) {
|
|
577
|
-
throw new Error('Invalid project id');
|
|
578
|
-
}
|
|
579
|
-
const now = Date.now();
|
|
580
|
-
assertSafeSessionId(sessionId);
|
|
581
|
-
assertCoordinationIdentityPairing(sessionId, input.role);
|
|
582
|
-
const name = input.name === undefined ? DEFAULT_SESSION_NAME : normalizeRequiredSessionName(input.name);
|
|
583
|
-
const header = {
|
|
584
|
-
id: sessionId,
|
|
585
|
-
...(input.role === undefined ? {} : { role: input.role }),
|
|
586
|
-
workspaceRoot,
|
|
587
|
-
cwd: input.cwd,
|
|
588
|
-
...(input.projectId !== undefined ? { projectId: input.projectId } : {}),
|
|
589
|
-
createdAt: now,
|
|
590
|
-
name,
|
|
591
|
-
titleIsManual: false,
|
|
592
|
-
isFlagged: false,
|
|
593
|
-
labels: input.labels ?? [],
|
|
594
|
-
isArchived: false,
|
|
595
|
-
status: input.status ?? 'active',
|
|
596
|
-
...(input.blockedReason ? { blockedReason: input.blockedReason } : {}),
|
|
597
|
-
statusUpdatedAt: now,
|
|
598
|
-
...(input.parentSessionId ? { parentSessionId: input.parentSessionId } : {}),
|
|
599
|
-
...(input.branchOfTurnId ? { branchOfTurnId: input.branchOfTurnId } : {}),
|
|
600
|
-
...(input.subagentParent ? { subagentParent: input.subagentParent } : {}),
|
|
601
|
-
...(input.subagentRuntime ? { subagentRuntime: input.subagentRuntime } : {}),
|
|
602
|
-
...(input.subagentSpawn ? { subagentSpawn: input.subagentSpawn } : {}),
|
|
603
|
-
...(input.subagentWorkspace ? { subagentWorkspace: input.subagentWorkspace } : {}),
|
|
604
|
-
...(conversationCopy ? { conversationCopy } : {}),
|
|
605
|
-
...(input.revisionRootSessionId ? { revisionRootSessionId: input.revisionRootSessionId } : {}),
|
|
606
|
-
...(input.revisionParentSessionId
|
|
607
|
-
? { revisionParentSessionId: input.revisionParentSessionId }
|
|
608
|
-
: {}),
|
|
609
|
-
...(input.revisionOfTurnId ? { revisionOfTurnId: input.revisionOfTurnId } : {}),
|
|
610
|
-
...(input.revisionIndex !== undefined ? { revisionIndex: input.revisionIndex } : {}),
|
|
611
|
-
...(input.revisionState ? { revisionState: input.revisionState } : {}),
|
|
612
|
-
hasUnread: false,
|
|
613
|
-
backend: input.executorId ? 'plugin-executor' : 'ai-sdk',
|
|
614
|
-
...(input.executorId ? { executorId: input.executorId } : {}),
|
|
615
|
-
...(input.llmConnectionId === undefined ? {} : { llmConnectionId: input.llmConnectionId }),
|
|
616
|
-
llmConnectionSlug: input.llmConnectionSlug,
|
|
617
|
-
// A subagent Session's route is chosen by the spawn that created it and is
|
|
618
|
-
// never re-targeted, so it is born frozen. Every other Session freezes on
|
|
619
|
-
// its first user Message.
|
|
620
|
-
connectionLocked: input.subagentParent !== undefined,
|
|
621
|
-
model: input.model ?? 'default',
|
|
622
|
-
...(input.toolProfile !== undefined ? { toolProfile: input.toolProfile } : {}),
|
|
623
|
-
toolMode: input.toolMode ?? DEFAULT_TOOL_MODE,
|
|
624
|
-
permissionMode: input.permissionMode,
|
|
625
|
-
collaborationMode: input.collaborationMode ?? 'agent',
|
|
626
|
-
orchestrationMode: input.orchestrationMode ?? 'default',
|
|
627
|
-
...(input.thinkingLevel !== undefined ? { thinkingLevel: input.thinkingLevel } : {}),
|
|
628
|
-
// Born on the ledger: a Session created here records its execution facts as
|
|
629
|
-
// RuntimeEvents from its first turn, so there is no transcript to convert.
|
|
630
|
-
// Only an imported transcript (staged at 0) and a Session written before
|
|
631
|
-
// this field existed have anything for the converter to do.
|
|
632
|
-
transcriptLedgerVersion: 1,
|
|
633
|
-
schemaVersion: 1,
|
|
634
|
-
};
|
|
635
|
-
assertValidSessionLineage(header);
|
|
636
|
-
return header;
|
|
637
|
-
}
|
|
638
|
-
function normalizeRequiredSessionName(name) {
|
|
639
|
-
const normalized = normalizeUserSessionName(name);
|
|
640
|
-
if (!normalized.ok)
|
|
641
|
-
throw new Error(normalized.error);
|
|
642
|
-
return normalized.value;
|
|
643
|
-
}
|
|
644
|
-
/** Validate and normalize a current SessionHeader before canonical persistence. */
|
|
645
|
-
export function normalizeSessionHeader(header, sessionId = header.id) {
|
|
646
|
-
const valid = header.id === sessionId &&
|
|
647
|
-
(header.role === undefined || header.role === WORKHUB_COORDINATION_SESSION_ROLE) &&
|
|
648
|
-
typeof header.workspaceRoot === 'string' &&
|
|
649
|
-
typeof header.cwd === 'string' &&
|
|
650
|
-
(header.projectId === undefined ||
|
|
651
|
-
header.projectId === null ||
|
|
652
|
-
(typeof header.projectId === 'string' && header.projectId.length > 0)) &&
|
|
653
|
-
isFiniteNumber(header.createdAt) &&
|
|
654
|
-
(header.lastMessageAt === undefined || isFiniteNumber(header.lastMessageAt)) &&
|
|
655
|
-
typeof header.name === 'string' &&
|
|
656
|
-
typeof header.titleIsManual === 'boolean' &&
|
|
657
|
-
typeof header.isFlagged === 'boolean' &&
|
|
658
|
-
Array.isArray(header.labels) &&
|
|
659
|
-
header.labels.every((label) => typeof label === 'string') &&
|
|
660
|
-
typeof header.isArchived === 'boolean' &&
|
|
661
|
-
!Object.prototype.hasOwnProperty.call(header, 'archivedAt') &&
|
|
662
|
-
isSessionStatus(header.status) &&
|
|
663
|
-
(header.blockedReason === undefined || isSessionBlockedReason(header.blockedReason)) &&
|
|
664
|
-
(header.statusUpdatedAt === undefined || isFiniteNumber(header.statusUpdatedAt)) &&
|
|
665
|
-
(header.parentSessionId === undefined || typeof header.parentSessionId === 'string') &&
|
|
666
|
-
(header.branchOfTurnId === undefined || typeof header.branchOfTurnId === 'string') &&
|
|
667
|
-
isValidConversationCopyLineage(header) &&
|
|
668
|
-
isValidRevisionLineage(header) &&
|
|
669
|
-
isValidSubagentSessionLineage(header) &&
|
|
670
|
-
isValidSessionExternalOrigin(header.externalOrigin) &&
|
|
671
|
-
(header.lastReadMessageId === undefined || typeof header.lastReadMessageId === 'string') &&
|
|
672
|
-
typeof header.hasUnread === 'boolean' &&
|
|
673
|
-
isPersistedBackendKind(header.backend) &&
|
|
674
|
-
isValidExecutorSelection(header) &&
|
|
675
|
-
(header.llmConnectionId === undefined ||
|
|
676
|
-
(typeof header.llmConnectionId === 'string' && header.llmConnectionId.length > 0)) &&
|
|
677
|
-
typeof header.llmConnectionSlug === 'string' &&
|
|
678
|
-
typeof header.connectionLocked === 'boolean' &&
|
|
679
|
-
typeof header.model === 'string' &&
|
|
680
|
-
(header.toolProfile === undefined || isSessionToolProfile(header.toolProfile)) &&
|
|
681
|
-
(header.toolMode === undefined || isToolMode(header.toolMode)) &&
|
|
682
|
-
isPermissionMode(header.permissionMode) &&
|
|
683
|
-
isCollaborationMode(header.collaborationMode) &&
|
|
684
|
-
isOrchestrationMode(header.orchestrationMode) &&
|
|
685
|
-
(header.transcriptLedgerVersion === undefined ||
|
|
686
|
-
header.transcriptLedgerVersion === 0 ||
|
|
687
|
-
header.transcriptLedgerVersion === 1) &&
|
|
688
|
-
header.schemaVersion === 1;
|
|
689
|
-
if (!valid) {
|
|
690
|
-
throw new Error(`Invalid session header for session ${sessionId}: malformed fields`);
|
|
691
|
-
}
|
|
692
|
-
const normalizedName = normalizeSessionName(header.name);
|
|
693
|
-
if (header.blockedReason === undefined) {
|
|
694
|
-
const { blockedReason: _blockedReason, ...withoutBlockedReason } = header;
|
|
695
|
-
return { ...withoutBlockedReason, name: normalizedName };
|
|
696
|
-
}
|
|
697
|
-
return { ...header, name: normalizedName };
|
|
698
|
-
}
|
|
699
|
-
export function decodePersistedSessionHeader(persisted, sessionId) {
|
|
700
|
-
const header = persisted;
|
|
701
|
-
const permissionMode = decodePersistedPermissionMode(header.permissionMode);
|
|
702
|
-
if (permissionMode === undefined) {
|
|
703
|
-
return normalizeSessionHeader(header, sessionId ?? header.id);
|
|
704
|
-
}
|
|
705
|
-
return normalizeSessionHeader(permissionMode === header.permissionMode ? header : { ...header, permissionMode }, sessionId ?? header.id);
|
|
706
|
-
}
|
|
707
|
-
function isValidSessionExternalOrigin(origin) {
|
|
708
|
-
if (origin === undefined)
|
|
709
|
-
return true;
|
|
710
|
-
return (typeof origin === 'object' &&
|
|
711
|
-
origin !== null &&
|
|
712
|
-
typeof origin.adapterId === 'string' &&
|
|
713
|
-
origin.adapterId.length > 0 &&
|
|
714
|
-
typeof origin.sourceSessionId === 'string' &&
|
|
715
|
-
origin.sourceSessionId.length > 0);
|
|
716
|
-
}
|
|
717
|
-
function isValidRevisionLineage(header) {
|
|
718
|
-
const values = [
|
|
719
|
-
header.revisionRootSessionId,
|
|
720
|
-
header.revisionParentSessionId,
|
|
721
|
-
header.revisionOfTurnId,
|
|
722
|
-
header.revisionIndex,
|
|
723
|
-
header.revisionState,
|
|
724
|
-
];
|
|
725
|
-
if (values.every((value) => value === undefined))
|
|
726
|
-
return true;
|
|
727
|
-
return (typeof header.revisionRootSessionId === 'string' &&
|
|
728
|
-
isSafeSessionId(header.revisionRootSessionId) &&
|
|
729
|
-
typeof header.revisionParentSessionId === 'string' &&
|
|
730
|
-
isSafeSessionId(header.revisionParentSessionId) &&
|
|
731
|
-
typeof header.revisionOfTurnId === 'string' &&
|
|
732
|
-
header.revisionOfTurnId.length > 0 &&
|
|
733
|
-
header.revisionOfTurnId.length <= 128 &&
|
|
734
|
-
Number.isSafeInteger(header.revisionIndex) &&
|
|
735
|
-
header.revisionIndex >= 2 &&
|
|
736
|
-
(header.revisionState === 'preparing' || header.revisionState === 'committed'));
|
|
737
|
-
}
|
|
738
|
-
function assertValidSessionLineage(header) {
|
|
739
|
-
if (!isValidConversationCopyLineage(header)) {
|
|
740
|
-
throw new Error('Invalid Session conversation-copy lineage');
|
|
741
|
-
}
|
|
742
|
-
if (!isValidRevisionLineage(header)) {
|
|
743
|
-
throw new Error('Invalid session revision lineage');
|
|
744
|
-
}
|
|
745
|
-
if (!isValidSubagentSessionLineage(header)) {
|
|
746
|
-
throw new Error('Invalid subagent session lineage');
|
|
747
|
-
}
|
|
748
|
-
}
|
|
749
|
-
function isValidConversationCopyLineage(header) {
|
|
750
|
-
const copy = header.conversationCopy;
|
|
751
|
-
if (copy === undefined)
|
|
752
|
-
return true;
|
|
753
|
-
if (!isSessionConversationCopy(copy) ||
|
|
754
|
-
!isSafeSessionId(copy.sourceSessionId) ||
|
|
755
|
-
copy.sourceSessionId === header.id ||
|
|
756
|
-
header.subagentParent !== undefined) {
|
|
757
|
-
return false;
|
|
758
|
-
}
|
|
759
|
-
if (copy.kind === 'branch') {
|
|
760
|
-
const revisionClear = header.revisionRootSessionId === undefined &&
|
|
761
|
-
header.revisionParentSessionId === undefined &&
|
|
762
|
-
header.revisionOfTurnId === undefined &&
|
|
763
|
-
header.revisionIndex === undefined &&
|
|
764
|
-
header.revisionState === undefined;
|
|
765
|
-
if (!revisionClear || header.parentSessionId !== copy.sourceSessionId) {
|
|
766
|
-
return false;
|
|
767
|
-
}
|
|
768
|
-
// An empty copy (absent `sourceTurnId`) records provenance
|
|
769
|
-
// (`parentSessionId`) but must not fabricate a `branchOfTurnId`, and is only
|
|
770
|
-
// valid for a side conversation; a through-turn copy must anchor to it.
|
|
771
|
-
return copy.sourceTurnId === undefined
|
|
772
|
-
? header.branchOfTurnId === undefined && copy.intent === 'side_conversation'
|
|
773
|
-
: header.branchOfTurnId === copy.sourceTurnId;
|
|
774
|
-
}
|
|
775
|
-
// Revision copies always carry a turn boundary (enforced at decode).
|
|
776
|
-
return (copy.sourceTurnId !== undefined &&
|
|
777
|
-
header.revisionParentSessionId === copy.sourceSessionId &&
|
|
778
|
-
header.revisionOfTurnId === copy.sourceTurnId);
|
|
779
|
-
}
|
|
780
|
-
function isValidSubagentSessionLineage(header) {
|
|
781
|
-
if (header.subagentParent === undefined) {
|
|
782
|
-
return (header.subagentRuntime === undefined &&
|
|
783
|
-
header.subagentSpawn === undefined &&
|
|
784
|
-
header.subagentWorkspace === undefined);
|
|
785
|
-
}
|
|
786
|
-
if (!isSubagentSessionParent(header.subagentParent) ||
|
|
787
|
-
!isSafeSessionId(header.subagentParent.parentSessionId) ||
|
|
788
|
-
header.parentSessionId !== undefined ||
|
|
789
|
-
header.branchOfTurnId !== undefined ||
|
|
790
|
-
header.revisionRootSessionId !== undefined ||
|
|
791
|
-
header.revisionParentSessionId !== undefined ||
|
|
792
|
-
header.revisionOfTurnId !== undefined ||
|
|
793
|
-
header.revisionIndex !== undefined ||
|
|
794
|
-
header.revisionState !== undefined) {
|
|
795
|
-
return false;
|
|
796
|
-
}
|
|
797
|
-
return ((header.subagentRuntime === undefined &&
|
|
798
|
-
header.subagentSpawn === undefined &&
|
|
799
|
-
header.subagentWorkspace === undefined) ||
|
|
800
|
-
(isSubagentSessionRuntime(header.subagentRuntime) &&
|
|
801
|
-
isSubagentSessionSpawn(header.subagentSpawn) &&
|
|
802
|
-
(header.subagentWorkspace === undefined ||
|
|
803
|
-
isSubagentWorkspaceBinding(header.subagentWorkspace))));
|
|
804
|
-
}
|
|
805
|
-
/**
|
|
806
|
-
* Decode guard for a durable session header. `'fake'` stays accepted:
|
|
807
|
-
* narrowing it here would make every session written by a build that shipped
|
|
808
|
-
* FakeBackend fail `normalizeSessionHeader` and read back as malformed (#3211).
|
|
809
|
-
*/
|
|
810
|
-
function isPersistedBackendKind(value) {
|
|
811
|
-
return value === 'ai-sdk' || value === 'plugin-executor' || value === 'fake';
|
|
812
|
-
}
|
|
813
|
-
function isValidExecutorSelection(header) {
|
|
814
|
-
if (header.backend === 'plugin-executor') {
|
|
815
|
-
return isExecutorId(header.executorId);
|
|
816
|
-
}
|
|
817
|
-
return header.executorId === undefined;
|
|
818
|
-
}
|
|
819
|
-
function isFiniteNumber(value) {
|
|
820
|
-
return typeof value === 'number' && Number.isFinite(value);
|
|
821
|
-
}
|
|
822
538
|
function assertNoConversationCopyMetadata(input) {
|
|
823
539
|
if (Object.prototype.hasOwnProperty.call(input, 'conversationCopy')) {
|
|
824
540
|
throw new Error('Conversation copy metadata requires createStableSession()');
|
|
@@ -846,69 +562,9 @@ function projectStableSessionCreateProbe(probe) {
|
|
|
846
562
|
? { kind: 'existing', record: projectHeaderSnapshot(probe.record) }
|
|
847
563
|
: probe;
|
|
848
564
|
}
|
|
849
|
-
function toSummary(header) {
|
|
850
|
-
const lastMessageAt = header.lastMessageAt;
|
|
851
|
-
return {
|
|
852
|
-
id: header.id,
|
|
853
|
-
cwd: header.cwd,
|
|
854
|
-
...(header.projectId !== undefined ? { projectId: header.projectId } : {}),
|
|
855
|
-
name: normalizeSessionName(header.name),
|
|
856
|
-
isFlagged: header.isFlagged,
|
|
857
|
-
isArchived: header.isArchived,
|
|
858
|
-
labels: header.labels,
|
|
859
|
-
hasUnread: header.hasUnread,
|
|
860
|
-
lastMessageAt,
|
|
861
|
-
status: header.status,
|
|
862
|
-
...(header.blockedReason ? { blockedReason: header.blockedReason } : {}),
|
|
863
|
-
...(header.statusUpdatedAt !== undefined ? { statusUpdatedAt: header.statusUpdatedAt } : {}),
|
|
864
|
-
...(header.parentSessionId ? { parentSessionId: header.parentSessionId } : {}),
|
|
865
|
-
...(header.branchOfTurnId ? { branchOfTurnId: header.branchOfTurnId } : {}),
|
|
866
|
-
...(header.subagentParent ? { subagentParent: header.subagentParent } : {}),
|
|
867
|
-
...(header.subagentRuntime
|
|
868
|
-
? {
|
|
869
|
-
subagentRuntime: subagentSessionRuntimeSummary(header.subagentRuntime),
|
|
870
|
-
}
|
|
871
|
-
: {}),
|
|
872
|
-
...(header.subagentWorkspace ? { subagentWorkspace: header.subagentWorkspace } : {}),
|
|
873
|
-
...(header.revisionRootSessionId
|
|
874
|
-
? { revisionRootSessionId: header.revisionRootSessionId }
|
|
875
|
-
: {}),
|
|
876
|
-
...(header.revisionParentSessionId
|
|
877
|
-
? { revisionParentSessionId: header.revisionParentSessionId }
|
|
878
|
-
: {}),
|
|
879
|
-
...(header.revisionOfTurnId ? { revisionOfTurnId: header.revisionOfTurnId } : {}),
|
|
880
|
-
...(header.revisionIndex !== undefined ? { revisionIndex: header.revisionIndex } : {}),
|
|
881
|
-
...(header.revisionState ? { revisionState: header.revisionState } : {}),
|
|
882
|
-
backend: header.backend,
|
|
883
|
-
...(header.executorId ? { executorId: header.executorId } : {}),
|
|
884
|
-
...(header.llmConnectionId === undefined ? {} : { llmConnectionId: header.llmConnectionId }),
|
|
885
|
-
llmConnectionSlug: header.llmConnectionSlug,
|
|
886
|
-
connectionLocked: header.connectionLocked,
|
|
887
|
-
model: header.model,
|
|
888
|
-
permissionMode: header.permissionMode,
|
|
889
|
-
collaborationMode: header.collaborationMode ?? 'agent',
|
|
890
|
-
orchestrationMode: header.orchestrationMode ?? 'default',
|
|
891
|
-
...(header.thinkingLevel !== undefined ? { thinkingLevel: header.thinkingLevel } : {}),
|
|
892
|
-
};
|
|
893
|
-
}
|
|
894
565
|
function toCatalogSummary(header, lastMessagePreview) {
|
|
895
566
|
return {
|
|
896
567
|
...toSummary(header),
|
|
897
568
|
...(lastMessagePreview === undefined ? {} : { lastMessagePreview }),
|
|
898
569
|
};
|
|
899
570
|
}
|
|
900
|
-
function normalizeSessionName(name) {
|
|
901
|
-
return name === 'New Session' ? DEFAULT_SESSION_NAME : name;
|
|
902
|
-
}
|
|
903
|
-
export function createUserMessage(input) {
|
|
904
|
-
return {
|
|
905
|
-
type: 'user',
|
|
906
|
-
id: randomUUID(),
|
|
907
|
-
turnId: input.turnId,
|
|
908
|
-
ts: Date.now(),
|
|
909
|
-
text: input.text,
|
|
910
|
-
...(input.displayText !== undefined ? { displayText: input.displayText } : {}),
|
|
911
|
-
attachments: input.attachments,
|
|
912
|
-
...(input.inlineReferences !== undefined ? { inlineReferences: input.inlineReferences } : {}),
|
|
913
|
-
};
|
|
914
|
-
}
|
|
@@ -16,7 +16,6 @@
|
|
|
16
16
|
* specific language governing permissions and limitations
|
|
17
17
|
* under the License.
|
|
18
18
|
*/
|
|
19
|
-
import { createHash } from 'node:crypto';
|
|
20
19
|
import { mkdirSync } from 'node:fs';
|
|
21
20
|
import { createRequire } from 'node:module';
|
|
22
21
|
import { dirname } from 'node:path';
|
|
@@ -25,9 +24,11 @@ import { assertHandoffClaimSource } from '@maka/core/runtime-handoff';
|
|
|
25
24
|
import { buildWorkspaceBaselineAuthorityEvents, buildWorkspaceSuccessorAuthorityEvent, scanWorkspaceBaselineAuthority, WORKSPACE_AUTHORITY_SESSION_ID, WORKSPACE_VERSION_AUTHORITY_CAPABILITY_V1, } from '@maka/core/workspace-version-authority';
|
|
26
25
|
import { decodeRuntimeEvent, decodeRuntimeInvocationOpened, isPartialRuntimeEvent, isTerminalRuntimeEvent, runtimeEventInvocationOpening, } from '@maka/core/runtime-event';
|
|
27
26
|
import { RunSealedError, RUNTIME_CONTINUATION_AUTHORITY_V1, TOOL_RECOVERY_BUNDLE_CAPABILITY_V1, } from '@maka/core/runtime-event-store';
|
|
28
|
-
import {
|
|
27
|
+
import { stableJsonStringify } from '@maka/core/tool-args-identity';
|
|
28
|
+
import { mergeRuntimePartialSnapshots, groupRuntimePartialSnapshots, compareRuntimePartialSnapshots, partialRuntimeStream, completedPartialRuntimeStreamKey, } from './runtime-partial-values.js';
|
|
29
|
+
import { assertPreparedInput, assertOutcomeInput, assertPreparedIdentity, assertOutcomeIdentity, } from './tool-commit-validation.js';
|
|
29
30
|
import { encodeCanonicalRuntimeEvent } from '@maka/core/canonical-runtime-event';
|
|
30
|
-
import { scanToolLedger, ToolLedgerCorruptionError, ToolLedgerRejectionError, validateGenericToolLedgerAppend,
|
|
31
|
+
import { scanToolLedger, ToolLedgerCorruptionError, ToolLedgerRejectionError, validateGenericToolLedgerAppend, validateToolLedgerTransition, } from '@maka/core/tool-ledger-scanner';
|
|
31
32
|
import { buildImmutableRuntimePrefix, buildImmutableRuntimePrefixProof, continuationStartEventMatchesClaim, decodeContinuationClaim, } from '@maka/core/runtime-boundary';
|
|
32
33
|
import { assertToolRecoveryEventBundle, interpretScannedToolRecovery, } from '@maka/core/tool-recovery-bundle';
|
|
33
34
|
import { configureSqliteRuntimeDatabase, migrateSqliteRuntimeDatabase, readUserVersion, RUNTIME_RECOVERY_AUTHORITY_CAPABILITY, RUNTIME_RECOVERY_AUTHORITY_CAPABILITY_VERSION, RUNTIME_CONTINUATION_AUTHORITY_CAPABILITY, RUNTIME_CONTINUATION_AUTHORITY_CAPABILITY_VERSION, RUNTIME_WORKSPACE_VERSION_AUTHORITY_CAPABILITY, RUNTIME_WORKSPACE_VERSION_AUTHORITY_CAPABILITY_VERSION, runtimeEventKind, SQLITE_RUNTIME_SCHEMA_VERSION, } from './sqlite-runtime-schema.js';
|
|
@@ -3143,93 +3144,6 @@ function toolJournalRecordFromRow(row) {
|
|
|
3143
3144
|
committedAt: row.committed_at,
|
|
3144
3145
|
};
|
|
3145
3146
|
}
|
|
3146
|
-
function assertPreparedInput(input) {
|
|
3147
|
-
if (input.journalEventId !== `${input.operationId}_prepared`) {
|
|
3148
|
-
throw new Error('T1 journal identity must be derived from the tool operation');
|
|
3149
|
-
}
|
|
3150
|
-
assertNoReservedRecoveryFact(input.runtimeEvent);
|
|
3151
|
-
assertNoReservedRecoveryFact(input.dispatchRuntimeEvent);
|
|
3152
|
-
const content = input.runtimeEvent.content;
|
|
3153
|
-
if (content?.kind !== 'function_call')
|
|
3154
|
-
throw new Error('T1 requires a function_call RuntimeEvent');
|
|
3155
|
-
if (content.id !== input.providerToolCallId || content.name !== input.toolName) {
|
|
3156
|
-
throw new Error('T1 RuntimeEvent identity does not match the tool operation');
|
|
3157
|
-
}
|
|
3158
|
-
let derivedArgsHash;
|
|
3159
|
-
try {
|
|
3160
|
-
derivedArgsHash = canonicalToolArgsHash(content.name, content.args);
|
|
3161
|
-
}
|
|
3162
|
-
catch {
|
|
3163
|
-
throw new Error('T1 argument hash does not match its canonical function call');
|
|
3164
|
-
}
|
|
3165
|
-
if (derivedArgsHash !== input.canonicalArgsHash ||
|
|
3166
|
-
validateToolLedgerEventLane(input.runtimeEvent).ok !== true) {
|
|
3167
|
-
throw new Error('T1 argument hash does not match its canonical function call');
|
|
3168
|
-
}
|
|
3169
|
-
const dispatch = input.dispatchRuntimeEvent.actions?.toolDispatch;
|
|
3170
|
-
if (!dispatch ||
|
|
3171
|
-
input.dispatchRuntimeEvent.content !== undefined ||
|
|
3172
|
-
input.dispatchRuntimeEvent.partial ||
|
|
3173
|
-
dispatch.operationId !== input.operationId ||
|
|
3174
|
-
dispatch.providerToolCallId !== input.providerToolCallId ||
|
|
3175
|
-
dispatch.toolName !== input.toolName ||
|
|
3176
|
-
dispatch.canonicalArgsHash !== input.canonicalArgsHash ||
|
|
3177
|
-
dispatch.recoveryMode !== input.recoveryMode ||
|
|
3178
|
-
validateToolLedgerEventLane(input.dispatchRuntimeEvent).ok !== true) {
|
|
3179
|
-
throw new Error('T1 requires a matching tool-dispatch RuntimeEvent');
|
|
3180
|
-
}
|
|
3181
|
-
assertSameRuntimeIdentity(input.runtimeEvent, input.dispatchRuntimeEvent, 'T1');
|
|
3182
|
-
}
|
|
3183
|
-
function assertOutcomeInput(input) {
|
|
3184
|
-
if (input.journalEventId !== `${input.operationId}_outcome`) {
|
|
3185
|
-
throw new Error('T2 journal identity must be derived from the tool operation');
|
|
3186
|
-
}
|
|
3187
|
-
assertNoReservedRecoveryFact(input.runtimeEvent);
|
|
3188
|
-
const content = input.runtimeEvent.content;
|
|
3189
|
-
if (content?.kind !== 'function_response') {
|
|
3190
|
-
throw new Error('T2 requires a function_response RuntimeEvent');
|
|
3191
|
-
}
|
|
3192
|
-
if (input.runtimeEvent.refs?.operationId !== input.operationId ||
|
|
3193
|
-
input.runtimeEvent.refs?.toolCallId !== content.id) {
|
|
3194
|
-
throw new Error('T2 requires operation and tool-call refs on the function_response RuntimeEvent');
|
|
3195
|
-
}
|
|
3196
|
-
if (validateToolLedgerEventLane(input.runtimeEvent).ok !== true) {
|
|
3197
|
-
throw new Error('T2 requires one canonical function-response semantic lane');
|
|
3198
|
-
}
|
|
3199
|
-
}
|
|
3200
|
-
function assertPreparedIdentity(operation, input) {
|
|
3201
|
-
const event = input.runtimeEvent;
|
|
3202
|
-
const matches = operation.invocationId === event.invocationId &&
|
|
3203
|
-
operation.runId === event.runId &&
|
|
3204
|
-
operation.turnId === event.turnId &&
|
|
3205
|
-
operation.providerToolCallId === input.providerToolCallId &&
|
|
3206
|
-
operation.toolName === input.toolName &&
|
|
3207
|
-
operation.canonicalArgsHash === input.canonicalArgsHash &&
|
|
3208
|
-
operation.recoveryMode === input.recoveryMode &&
|
|
3209
|
-
operation.callEventId === event.id &&
|
|
3210
|
-
operation.dispatchEventId === input.dispatchRuntimeEvent.id;
|
|
3211
|
-
if (!matches)
|
|
3212
|
-
throw new Error(`Tool operation identity conflict for ${input.operationId}`);
|
|
3213
|
-
}
|
|
3214
|
-
function assertSameRuntimeIdentity(first, second, boundary) {
|
|
3215
|
-
if (first.sessionId !== second.sessionId ||
|
|
3216
|
-
first.invocationId !== second.invocationId ||
|
|
3217
|
-
first.runId !== second.runId ||
|
|
3218
|
-
first.turnId !== second.turnId) {
|
|
3219
|
-
throw new Error(`${boundary} RuntimeEvents do not share one execution identity`);
|
|
3220
|
-
}
|
|
3221
|
-
}
|
|
3222
|
-
function assertOutcomeIdentity(operation, event) {
|
|
3223
|
-
const content = event.content;
|
|
3224
|
-
const matches = content?.kind === 'function_response' &&
|
|
3225
|
-
operation.invocationId === event.invocationId &&
|
|
3226
|
-
operation.runId === event.runId &&
|
|
3227
|
-
operation.turnId === event.turnId &&
|
|
3228
|
-
operation.providerToolCallId === content.id &&
|
|
3229
|
-
operation.toolName === content.name;
|
|
3230
|
-
if (!matches)
|
|
3231
|
-
throw new Error(`Tool operation outcome identity conflict for ${operation.operationId}`);
|
|
3232
|
-
}
|
|
3233
3147
|
function assertRuntimeEventIdentity(event) {
|
|
3234
3148
|
decodeRuntimeEvent(event);
|
|
3235
3149
|
for (const [field, value] of Object.entries({
|
|
@@ -3544,101 +3458,6 @@ function decodeRuntimePartialStorageRow(row) {
|
|
|
3544
3458
|
function decodeStoredRuntimeEvent(storedJson) {
|
|
3545
3459
|
return decodeRuntimeEvent(JSON.parse(storedJson));
|
|
3546
3460
|
}
|
|
3547
|
-
function mergeRuntimePartialSnapshots(immutableEvents, snapshots) {
|
|
3548
|
-
const { leading, afterEvent } = groupRuntimePartialSnapshots(snapshots);
|
|
3549
|
-
const merged = leading.sort(compareRuntimePartialSnapshots).map(({ event }) => event);
|
|
3550
|
-
for (const event of immutableEvents) {
|
|
3551
|
-
merged.push(event);
|
|
3552
|
-
const anchored = afterEvent.get(event.id);
|
|
3553
|
-
if (!anchored)
|
|
3554
|
-
continue;
|
|
3555
|
-
merged.push(...anchored.sort(compareRuntimePartialSnapshots).map((snapshot) => snapshot.event));
|
|
3556
|
-
afterEvent.delete(event.id);
|
|
3557
|
-
}
|
|
3558
|
-
for (const orphaned of afterEvent.values()) {
|
|
3559
|
-
merged.push(...orphaned.sort(compareRuntimePartialSnapshots).map((snapshot) => snapshot.event));
|
|
3560
|
-
}
|
|
3561
|
-
return merged;
|
|
3562
|
-
}
|
|
3563
|
-
function groupRuntimePartialSnapshots(snapshots) {
|
|
3564
|
-
const leading = [];
|
|
3565
|
-
const afterEvent = new Map();
|
|
3566
|
-
for (const snapshot of snapshots) {
|
|
3567
|
-
if (!snapshot.afterEventId) {
|
|
3568
|
-
leading.push(snapshot);
|
|
3569
|
-
continue;
|
|
3570
|
-
}
|
|
3571
|
-
const grouped = afterEvent.get(snapshot.afterEventId) ?? [];
|
|
3572
|
-
grouped.push(snapshot);
|
|
3573
|
-
afterEvent.set(snapshot.afterEventId, grouped);
|
|
3574
|
-
}
|
|
3575
|
-
return { leading, afterEvent };
|
|
3576
|
-
}
|
|
3577
|
-
function compareRuntimePartialSnapshots(left, right) {
|
|
3578
|
-
return left.event.ts - right.event.ts || left.event.id.localeCompare(right.event.id);
|
|
3579
|
-
}
|
|
3580
|
-
function partialRuntimeStream(event) {
|
|
3581
|
-
if (!event.partial || event.status !== undefined || event.actions)
|
|
3582
|
-
return undefined;
|
|
3583
|
-
const content = event.content;
|
|
3584
|
-
let identity;
|
|
3585
|
-
let text = '';
|
|
3586
|
-
if (content?.kind === 'text' &&
|
|
3587
|
-
content.attachments === undefined &&
|
|
3588
|
-
event.refs?.providerEventId &&
|
|
3589
|
-
hasOnlyKeys(event.refs, ['providerEventId'])) {
|
|
3590
|
-
identity = `${content.kind}:provider:${event.refs.providerEventId}`;
|
|
3591
|
-
text = content.text;
|
|
3592
|
-
}
|
|
3593
|
-
else if (content?.kind === 'thinking' &&
|
|
3594
|
-
content.signature === undefined &&
|
|
3595
|
-
event.refs?.providerEventId &&
|
|
3596
|
-
hasOnlyKeys(event.refs, ['providerEventId'])) {
|
|
3597
|
-
identity = `${content.kind}:provider:${event.refs.providerEventId}`;
|
|
3598
|
-
text = content.text;
|
|
3599
|
-
}
|
|
3600
|
-
else if (!content && event.refs?.toolCallId && hasOnlyKeys(event.refs, ['toolCallId'])) {
|
|
3601
|
-
identity = `tool:call:${event.refs.toolCallId}`;
|
|
3602
|
-
}
|
|
3603
|
-
if (!identity)
|
|
3604
|
-
return undefined;
|
|
3605
|
-
const key = runtimePartialStreamKey(identity, event);
|
|
3606
|
-
const snapshot = content?.kind === 'text' || content?.kind === 'thinking'
|
|
3607
|
-
? { ...event, content: { ...content, text: '' } }
|
|
3608
|
-
: event;
|
|
3609
|
-
return { key, snapshot, text };
|
|
3610
|
-
}
|
|
3611
|
-
function completedPartialRuntimeStreamKey(event) {
|
|
3612
|
-
if (event.partial)
|
|
3613
|
-
return undefined;
|
|
3614
|
-
const content = event.content;
|
|
3615
|
-
let identity;
|
|
3616
|
-
if ((content?.kind === 'text' || content?.kind === 'thinking') && event.refs?.providerEventId) {
|
|
3617
|
-
identity = `${content.kind}:provider:${event.refs.providerEventId}`;
|
|
3618
|
-
}
|
|
3619
|
-
else if (content?.kind === 'function_response' && event.refs?.toolCallId) {
|
|
3620
|
-
identity = `tool:call:${event.refs.toolCallId}`;
|
|
3621
|
-
}
|
|
3622
|
-
return identity ? runtimePartialStreamKey(identity, event) : undefined;
|
|
3623
|
-
}
|
|
3624
|
-
function runtimePartialStreamKey(identity, event) {
|
|
3625
|
-
return createHash('sha256')
|
|
3626
|
-
.update(JSON.stringify([
|
|
3627
|
-
identity,
|
|
3628
|
-
event.sessionId,
|
|
3629
|
-
event.invocationId,
|
|
3630
|
-
event.runId,
|
|
3631
|
-
event.turnId,
|
|
3632
|
-
event.branch ?? null,
|
|
3633
|
-
event.role,
|
|
3634
|
-
event.author,
|
|
3635
|
-
]))
|
|
3636
|
-
.digest('hex');
|
|
3637
|
-
}
|
|
3638
|
-
function hasOnlyKeys(value, allowed) {
|
|
3639
|
-
const allowedSet = new Set(allowed);
|
|
3640
|
-
return Object.keys(value).every((key) => allowedSet.has(key));
|
|
3641
|
-
}
|
|
3642
3461
|
function decodeContinuationClaimRow(row) {
|
|
3643
3462
|
if (row.protocol_version !== 1) {
|
|
3644
3463
|
throw new Error(`Unsupported continuation claim protocol ${row.protocol_version}`);
|