@someonegg/codex-hub 0.1.1 → 0.1.3

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.
@@ -0,0 +1,174 @@
1
+ import { AssociationError, } from "./operation-state.js";
2
+ import { resolveResumeDirectory, } from "./session-manager.js";
3
+ /** Adapter launch and association checks; OperationService owns idempotency and occupancy. */
4
+ export class SessionLaunchExecutor {
5
+ sessions;
6
+ interaction;
7
+ managers;
8
+ bindings;
9
+ control;
10
+ now;
11
+ waitMs;
12
+ runtime;
13
+ constructor(sessions, interaction, managers, bindings, control, now, waitMs, runtime) {
14
+ this.sessions = sessions;
15
+ this.interaction = interaction;
16
+ this.managers = managers;
17
+ this.bindings = bindings;
18
+ this.control = control;
19
+ this.now = now;
20
+ this.waitMs = waitMs;
21
+ this.runtime = runtime;
22
+ }
23
+ async create(entry, input) {
24
+ try {
25
+ const manager = this.managers.get(entry.sourceInstanceId);
26
+ const resolved = await manager.resolveDirectory(input.directory);
27
+ if (!this.control.pending(entry))
28
+ return;
29
+ this.control.transition(entry, { kind: "create", phase: "launching" });
30
+ const target = await manager.launch({
31
+ operationId: entry.metadata.operationId,
32
+ directory: resolved.directory,
33
+ worktree: input.worktree,
34
+ yolo: input.yolo ?? false,
35
+ projectDirectory: resolved.directory,
36
+ });
37
+ if (!this.control.pending(entry))
38
+ return;
39
+ this.control.transition(entry, {
40
+ kind: "create",
41
+ phase: "observing",
42
+ project: resolved.directory,
43
+ target,
44
+ startedAt: this.now(),
45
+ }, this.now() + this.waitMs.create);
46
+ }
47
+ catch (error) {
48
+ this.control.error(entry, error);
49
+ }
50
+ }
51
+ async resume(entry, input) {
52
+ const sessionId = input.resumeSessionId;
53
+ try {
54
+ if (!this.runtime)
55
+ throw new Error("Resume is unavailable");
56
+ let snapshot = await this.sessions.managementSnapshot();
57
+ let session = snapshot.sessions.find((item) => item.id === sessionId);
58
+ if (!session)
59
+ throw new Error("Session not found");
60
+ let capability = await this.runtime.evaluate(session, snapshot.sessions);
61
+ if (!capability.resume.allowed)
62
+ throw new Error(capability.resume.reason);
63
+ entry.sourceInstanceId = session.origin.sourceInstanceId;
64
+ entry.resumeNativeId = session.sourceId ?? undefined;
65
+ const manager = this.managers.get(entry.sourceInstanceId);
66
+ const attempt = this.bindings.latest(session.id);
67
+ const replacement = this.bindings.replacement(session.id);
68
+ await resolveResumeDirectory(manager, session);
69
+ // Refresh the source after asynchronous checks, then revalidate immediately before launch.
70
+ snapshot = await this.sessions.managementSnapshot();
71
+ const latest = snapshot.sessions.find((item) => item.id === sessionId);
72
+ if (!latest ||
73
+ latest.sourceId !== session.sourceId ||
74
+ latest.origin.sourceInstanceId !== entry.sourceInstanceId ||
75
+ latest.workingDirectory !== session.workingDirectory ||
76
+ latest.projectDirectory !== session.projectDirectory ||
77
+ attempt !== this.bindings.latest(session.id) ||
78
+ replacement !== this.bindings.replacement(session.id))
79
+ throw new Error("Session identity or directory changed during validation");
80
+ session = latest;
81
+ capability = await this.runtime.evaluate(session, snapshot.sessions);
82
+ if (!capability.resume.allowed)
83
+ throw new Error(capability.resume.reason);
84
+ const resolved = await resolveResumeDirectory(manager, session);
85
+ const finalSnapshot = await this.sessions.managementSnapshot();
86
+ const finalSession = finalSnapshot.sessions.find((item) => item.id === sessionId);
87
+ if (!finalSession ||
88
+ finalSession.sourceId !== session.sourceId ||
89
+ finalSession.origin.sourceInstanceId !== session.origin.sourceInstanceId ||
90
+ finalSession.archived ||
91
+ finalSession.workingDirectory !== session.workingDirectory ||
92
+ finalSession.projectDirectory !== session.projectDirectory ||
93
+ finalSnapshot.sessions.filter((item) => item.sourceId === session.sourceId &&
94
+ item.origin.sourceInstanceId === session.origin.sourceInstanceId).length !== 1 ||
95
+ attempt !== this.bindings.latest(session.id) ||
96
+ replacement !== this.bindings.replacement(session.id))
97
+ throw new Error("Session identity, binding or directory changed during validation");
98
+ if (!this.control.pending(entry))
99
+ return;
100
+ this.control.transition(entry, { kind: "create", phase: "launching" });
101
+ const target = await manager.launch({
102
+ operationId: entry.metadata.operationId,
103
+ directory: resolved.directory,
104
+ worktree: false,
105
+ yolo: input.yolo ?? false,
106
+ resumeNativeId: session.sourceId,
107
+ projectDirectory: session.projectDirectory,
108
+ });
109
+ if (!this.control.pending(entry))
110
+ return;
111
+ this.control.transition(entry, {
112
+ kind: "create",
113
+ phase: "observing",
114
+ target,
115
+ project: session.projectDirectory ?? resolved.directory,
116
+ startedAt: this.now(),
117
+ }, this.now() + this.waitMs.create);
118
+ }
119
+ catch (error) {
120
+ this.control.error(entry, error);
121
+ }
122
+ }
123
+ async observe(entry) {
124
+ const snapshot = await this.sessions.managementSnapshot();
125
+ if (!this.control.pending(entry))
126
+ return;
127
+ const context = entry.stage;
128
+ if (context.kind !== "create" || context.phase !== "observing")
129
+ return;
130
+ const matches = snapshot.sessions.filter((session) => {
131
+ const attempt = this.bindings.latest(session.id);
132
+ return (session.origin.sourceInstanceId === entry.sourceInstanceId &&
133
+ attempt?.valid &&
134
+ attempt.operationId === entry.metadata.operationId);
135
+ });
136
+ if (matches.length > 1)
137
+ throw new AssociationError("Multiple sessions claimed this operation ID");
138
+ const match = matches[0];
139
+ if (!match)
140
+ return;
141
+ if (entry.metadata.mode === "resume" &&
142
+ (match.id !== entry.metadata.sessionId ||
143
+ match.archived ||
144
+ match.sourceId !== entry.resumeNativeId ||
145
+ !match.sourceId ||
146
+ this.#matches(snapshot, entry, match.sourceId).length !== 1))
147
+ throw new AssociationError("Resume did not bind the original unique session");
148
+ const attempt = this.bindings.latest(match.id);
149
+ if (!attempt?.valid ||
150
+ attempt.socketPath !== context.target.socketPath ||
151
+ attempt.paneId !== context.target.paneId ||
152
+ match.projectDirectory !== context.project) {
153
+ throw new AssociationError("Binding does not match the launched target/project");
154
+ }
155
+ const binding = await this.interaction.describe(match.id);
156
+ if (!this.control.pending(entry))
157
+ return;
158
+ if (binding?.supported && binding.state === "unknown")
159
+ return;
160
+ if (!binding?.supported || binding.state !== "connected")
161
+ throw new AssociationError("The new session binding failed validation");
162
+ const matchesTarget = await this.interaction.matchesTarget(match.id, context.target);
163
+ if (!this.control.pending(entry))
164
+ return;
165
+ if (!matchesTarget)
166
+ throw new AssociationError("Binding does not match the launched target");
167
+ entry.metadata.sessionId = match.id;
168
+ this.control.finish(entry, "succeeded", null);
169
+ }
170
+ #matches(snapshot, entry, nativeId) {
171
+ return snapshot.sessions.filter((session) => session.origin.sourceInstanceId === entry.sourceInstanceId && session.sourceId === nativeId);
172
+ }
173
+ }
174
+ //# sourceMappingURL=session-launch-executor.js.map
@@ -0,0 +1,151 @@
1
+ import { SessionExitService } from "./session-exit-service.js";
2
+ /** Exit, archive and delete execution, including confirmation of native effects. */
3
+ export class SessionLifecycleExecutor {
4
+ sessions;
5
+ interaction;
6
+ managers;
7
+ control;
8
+ now;
9
+ waitMs;
10
+ #exit;
11
+ constructor(sessions, interaction, managers, control, now, waitMs) {
12
+ this.sessions = sessions;
13
+ this.interaction = interaction;
14
+ this.managers = managers;
15
+ this.control = control;
16
+ this.now = now;
17
+ this.waitMs = waitMs;
18
+ this.#exit = new SessionExitService(interaction, now);
19
+ }
20
+ async manage(entry, type, sessionId) {
21
+ try {
22
+ const snapshot = await this.sessions.managementSnapshot();
23
+ if (!this.control.pending(entry))
24
+ return;
25
+ const session = snapshot.sessions.find((item) => item.id === sessionId);
26
+ if (!session) {
27
+ if (type === "delete") {
28
+ this.control.finish(entry, "succeeded", null);
29
+ return;
30
+ }
31
+ throw new Error("Session not found");
32
+ }
33
+ entry.sourceInstanceId = session.origin.sourceInstanceId;
34
+ const manager = this.managers.get(entry.sourceInstanceId);
35
+ if (type === "archive" && session.archived) {
36
+ this.control.finish(entry, "succeeded", null);
37
+ return;
38
+ }
39
+ if (type === "exit") {
40
+ await this.#startExit(entry, sessionId, null);
41
+ return;
42
+ }
43
+ const nativeId = session.sourceId;
44
+ if (!nativeId?.trim())
45
+ throw new Error("Session has no valid native identity");
46
+ if (this.#matches(snapshot, entry, nativeId).length !== 1)
47
+ throw new Error("Native session identity is ambiguous");
48
+ if (type === "delete") {
49
+ if (!session.archived)
50
+ throw new Error("Only archived sessions can be deleted");
51
+ this.control.transition(entry, { kind: "delete", phase: "deleting", nativeId });
52
+ await manager.delete(nativeId);
53
+ if (!this.control.pending(entry))
54
+ return;
55
+ this.control.transition(entry, { kind: "delete", phase: "confirming", nativeId });
56
+ }
57
+ else {
58
+ this.control.transition(entry, { kind: "archive", phase: "resolving_target", nativeId });
59
+ await this.#startExit(entry, sessionId, nativeId);
60
+ }
61
+ if (this.control.pending(entry))
62
+ await this.control.observe(entry);
63
+ }
64
+ catch (error) {
65
+ this.control.error(entry, error);
66
+ }
67
+ }
68
+ async observe(entry) {
69
+ if (!this.control.pending(entry))
70
+ return;
71
+ let context = entry.stage;
72
+ if (context.kind === "exiting") {
73
+ try {
74
+ // Refresh adapter bindings before observing or acting on the fixed target.
75
+ await this.sessions.managementSnapshot();
76
+ if (!this.control.pending(entry))
77
+ return;
78
+ const phase = context.task.phase;
79
+ const exited = await context.task.advance();
80
+ if (!this.control.pending(entry))
81
+ return;
82
+ if (phase !== context.task.phase)
83
+ entry.metadata.updatedAt = new Date(this.now()).toISOString();
84
+ if (!exited)
85
+ return;
86
+ if (context.nativeId === null) {
87
+ this.control.finish(entry, "succeeded", null);
88
+ return;
89
+ }
90
+ await this.#archive(entry, context.nativeId);
91
+ if (!this.control.pending(entry))
92
+ return;
93
+ }
94
+ catch (error) {
95
+ if (this.control.pending(entry))
96
+ this.control.finish(entry, "unknown", error instanceof Error ? error.message : String(error));
97
+ return;
98
+ }
99
+ }
100
+ const snapshot = await this.sessions.managementSnapshot();
101
+ if (!this.control.pending(entry))
102
+ return;
103
+ context = entry.stage;
104
+ if ((context.kind === "archive" || context.kind === "delete") &&
105
+ context.phase === "confirming") {
106
+ const matches = this.#matches(snapshot, entry, context.nativeId);
107
+ if ((context.kind === "archive" && matches.length === 1 && matches[0]?.archived) ||
108
+ (context.kind === "delete" && matches.length === 0))
109
+ this.control.finish(entry, "succeeded", null);
110
+ }
111
+ }
112
+ async #startExit(entry, sessionId, nativeId) {
113
+ const target = await this.interaction.resolveTarget(sessionId);
114
+ if (!this.control.pending(entry))
115
+ return;
116
+ if (!target) {
117
+ if (nativeId === null)
118
+ this.control.finish(entry, "succeeded", null);
119
+ else
120
+ await this.#archive(entry, nativeId);
121
+ return;
122
+ }
123
+ const task = this.#exit.start(sessionId, target, this.managers.get(entry.sourceInstanceId), () => {
124
+ this.control.timeout(entry);
125
+ if (!this.control.pending(entry))
126
+ throw new Error("Exit operation is no longer active");
127
+ });
128
+ this.control.transition(entry, { kind: "exiting", task, nativeId }, this.now() + (this.waitMs.exit ?? this.waitMs.archive));
129
+ await this.control.observe(entry);
130
+ }
131
+ async #archive(entry, nativeId) {
132
+ if (!this.control.pending(entry))
133
+ return;
134
+ this.control.transition(entry, { kind: "archive", phase: "archiving", nativeId }, this.now() + 25_000);
135
+ try {
136
+ await this.managers.get(entry.sourceInstanceId).archive(nativeId);
137
+ if (!this.control.pending(entry))
138
+ return;
139
+ this.control.transition(entry, { kind: "archive", phase: "confirming", nativeId }, this.now() + this.waitMs.archive);
140
+ }
141
+ catch (error) {
142
+ if (!this.control.pending(entry))
143
+ return;
144
+ this.control.error(entry, error);
145
+ }
146
+ }
147
+ #matches(snapshot, entry, nativeId) {
148
+ return snapshot.sessions.filter((session) => session.origin.sourceInstanceId === entry.sourceInstanceId && session.sourceId === nativeId);
149
+ }
150
+ }
151
+ //# sourceMappingURL=session-lifecycle-executor.js.map
@@ -1,5 +1,5 @@
1
- import { reconcileSessionState } from "../interaction/reconcile-session-state.js";
2
- import { createProcessUpdateRevisionFactory, withUpdateRevision, } from "./update-revision.js";
1
+ import { assembleSessionResponse } from "../application/session-response.js";
2
+ import { createProcessUpdateRevisionFactory, } from "./update-revision.js";
3
3
  export const UPDATES_PROBE_INTERVAL_MS = 1_500;
4
4
  export const UPDATES_WAIT_TIMEOUT_MS = 25_000;
5
5
  export const MAX_UPDATES_WAITERS = 100;
@@ -74,18 +74,7 @@ export class SessionUpdatesService {
74
74
  if (snapshot === null) {
75
75
  throw new SessionUpdatesError("session_not_found", "Session not found");
76
76
  }
77
- const interaction = await this.#describeInteraction(sessionId, snapshot.interactionSession);
78
- return withUpdateRevision(reconcileSessionState({
79
- session: snapshot.session,
80
- cursor: snapshot.cursor,
81
- hasMore: snapshot.hasMore,
82
- interaction,
83
- }), this.#createRevision);
84
- }
85
- #describeInteraction(sessionId, input) {
86
- if (this.options.interaction === undefined)
87
- return Promise.resolve({ supported: false, attachCommand: null });
88
- return this.options.interaction.describeSnapshot(sessionId, input);
77
+ return assembleSessionResponse(snapshot, this.options.interaction, this.#createRevision);
89
78
  }
90
79
  #acquire(sessionId, signal) {
91
80
  this.#throwIfAborted(signal);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@someonegg/codex-hub",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Local project and session hub for Codex",
5
5
  "type": "module",
6
6
  "engines": {