work-ally 0.2.0-alpha.1

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.
Files changed (172) hide show
  1. package/AGENTS.md +110 -0
  2. package/DASHBOARD.md +160 -0
  3. package/PRODUCT.md +113 -0
  4. package/README.md +403 -0
  5. package/ally.sh +171 -0
  6. package/bridge/src/approval-rules.ts +360 -0
  7. package/bridge/src/channel-delivery.ts +207 -0
  8. package/bridge/src/channel-types.ts +22 -0
  9. package/bridge/src/channels/fake/adapter.ts +31 -0
  10. package/bridge/src/channels/feishu/adapter.ts +411 -0
  11. package/bridge/src/channels/feishu/approvals.ts +6 -0
  12. package/bridge/src/channels/feishu/formatter.ts +276 -0
  13. package/bridge/src/channels/feishu/normalize.ts +368 -0
  14. package/bridge/src/codex-config.ts +52 -0
  15. package/bridge/src/config.ts +240 -0
  16. package/bridge/src/fake-runtime-client.ts +505 -0
  17. package/bridge/src/handoff-service.ts +494 -0
  18. package/bridge/src/logger.ts +194 -0
  19. package/bridge/src/memory-digest.ts +186 -0
  20. package/bridge/src/receiver-approval-autonomy.ts +158 -0
  21. package/bridge/src/receiver-control-core.ts +140 -0
  22. package/bridge/src/receiver-control-work-session.ts +218 -0
  23. package/bridge/src/receiver-control.ts +83 -0
  24. package/bridge/src/receiver-delivery.ts +136 -0
  25. package/bridge/src/receiver-helpers.ts +96 -0
  26. package/bridge/src/receiver-human-gate.ts +333 -0
  27. package/bridge/src/receiver-inbound-preflight.ts +162 -0
  28. package/bridge/src/receiver-recovery.ts +236 -0
  29. package/bridge/src/receiver-runtime-callbacks.ts +367 -0
  30. package/bridge/src/receiver-runtime-policy.ts +132 -0
  31. package/bridge/src/receiver-runtime-state.ts +124 -0
  32. package/bridge/src/receiver-support-actions.ts +189 -0
  33. package/bridge/src/receiver-thread-start.ts +57 -0
  34. package/bridge/src/receiver-turn-coordination.ts +94 -0
  35. package/bridge/src/receiver-turn-execution.ts +257 -0
  36. package/bridge/src/receiver-turn-failure.ts +143 -0
  37. package/bridge/src/receiver-turn-result.ts +185 -0
  38. package/bridge/src/receiver-turn-steer.ts +70 -0
  39. package/bridge/src/receiver-work-session.ts +76 -0
  40. package/bridge/src/receiver.ts +329 -0
  41. package/bridge/src/router.ts +62 -0
  42. package/bridge/src/runtime-client-agent-messages.ts +150 -0
  43. package/bridge/src/runtime-client-message-dispatch.ts +176 -0
  44. package/bridge/src/runtime-client-protocol.ts +411 -0
  45. package/bridge/src/runtime-client-request-ops.ts +56 -0
  46. package/bridge/src/runtime-client-run-turn.ts +158 -0
  47. package/bridge/src/runtime-client-thread-ops.ts +270 -0
  48. package/bridge/src/runtime-client-transport.ts +309 -0
  49. package/bridge/src/runtime-client-turn-poll.ts +224 -0
  50. package/bridge/src/runtime-client-turn-read.ts +185 -0
  51. package/bridge/src/runtime-client-turn-state.ts +105 -0
  52. package/bridge/src/runtime-client.ts +344 -0
  53. package/bridge/src/runtime-user-input.ts +403 -0
  54. package/bridge/src/scheduler.ts +239 -0
  55. package/bridge/src/server-handoff-command.ts +364 -0
  56. package/bridge/src/server-main.ts +80 -0
  57. package/bridge/src/server-routine-command.ts +60 -0
  58. package/bridge/src/server-routine-execution.ts +222 -0
  59. package/bridge/src/server-runtime-app-support.ts +107 -0
  60. package/bridge/src/server-runtime-app.ts +238 -0
  61. package/bridge/src/server-thread-sync-command.ts +63 -0
  62. package/bridge/src/server.ts +17 -0
  63. package/bridge/src/session-store-delivery.ts +220 -0
  64. package/bridge/src/session-store-human-gate.ts +380 -0
  65. package/bridge/src/session-store-inbound-acceptance.ts +66 -0
  66. package/bridge/src/session-store-meta.ts +134 -0
  67. package/bridge/src/session-store-turn-ledger.ts +272 -0
  68. package/bridge/src/session-store.ts +380 -0
  69. package/bridge/src/system-notify.ts +220 -0
  70. package/bridge/src/thread-sync.ts +200 -0
  71. package/bridge/src/translator.ts +494 -0
  72. package/bridge/src/types.ts +289 -0
  73. package/bridge/src/utils.ts +104 -0
  74. package/bridge/src/work-session-store.ts +471 -0
  75. package/docs/.gitkeep +0 -0
  76. package/docs/architecture/codex-feishu-bridge-proposal.md +2742 -0
  77. package/docs/completed/FEATURE-feishu-markdown-and-reply-support.md +327 -0
  78. package/docs/completed/README.md +21 -0
  79. package/docs/completed/SPEC-approval-autonomy-and-safe-defaults.md +205 -0
  80. package/docs/completed/SPEC-approval-batch-and-strict-reply-shortcuts.md +153 -0
  81. package/docs/completed/SPEC-conversation-noise-reduction-and-busy-input-gate.md +538 -0
  82. package/docs/completed/SPEC-engineering-sop-skillization.md +190 -0
  83. package/docs/completed/SPEC-faithful-bridge-core-thinning-v2.md +376 -0
  84. package/docs/completed/SPEC-faithful-bridge-core-thinning.md +1071 -0
  85. package/docs/completed/SPEC-group-chat-sender-identity.md +301 -0
  86. package/docs/completed/SPEC-middleware-exception-visibility.md +227 -0
  87. package/docs/completed/SPEC-nightly-memory-digest-visibility.md +121 -0
  88. package/docs/completed/SPEC-project-group-chat-human-centered-conversation-mapping.md +326 -0
  89. package/docs/completed/SPEC-remove-cli-persona-bootstrap.md +201 -0
  90. package/docs/developer-workflow.md +49 -0
  91. package/docs/implementation/SPEC-codex-same-machine-session-handoff-implementation.md +239 -0
  92. package/docs/implementation/test-coverage-map.md +363 -0
  93. package/docs/implementation/work-ally-implementation-guide.md +790 -0
  94. package/docs/issues/README.md +10 -0
  95. package/docs/issues/pending/ANALYSIS-ally-premature-recovery-notice-and-task-state-semantics-2026-03-18.md +295 -0
  96. package/docs/issues/resolved/ANALYSIS-approval-waiting-visible-but-approval-artifact-missing-2026-03-16.md +466 -0
  97. package/docs/issues/resolved/ANALYSIS-blocking-state-visible-without-user-actionable-artifact-2026-03-16.md +261 -0
  98. package/docs/issues/resolved/ANALYSIS-codex-app-server-transport-disconnect-semantics-2026-03-14.md +606 -0
  99. package/docs/issues/resolved/ANALYSIS-premature-terminalization-on-fresh-thread-poll-and-object-error-leak-2026-03-16.md +348 -0
  100. package/docs/issues/resolved/ANALYSIS-runtime-turn-delivery-and-recovery-2026-03-14.md +603 -0
  101. package/docs/issues/resolved/ANALYSIS-self-test-gap-approval-waiting-visible-but-approval-artifact-missing-2026-03-16.md +166 -0
  102. package/docs/issues/resolved/ANALYSIS-self-test-gap-blocking-state-visible-without-user-actionable-artifact-2026-03-16.md +186 -0
  103. package/docs/issues/resolved/ANALYSIS-self-test-gap-premature-terminalization-on-fresh-thread-poll-and-object-error-leak-2026-03-16.md +166 -0
  104. package/docs/issues/resolved/REPORT-ally-runtime-turn-delivery-3b42fb8-2026-03-15.md +373 -0
  105. package/docs/manual-acceptance.md +127 -0
  106. package/docs/ops-runbook.md +44 -0
  107. package/docs/planning/FEATURE-memory-system.md +748 -0
  108. package/docs/planning/SPEC-active-turn-steer-and-context-compaction-visibility.md +269 -0
  109. package/docs/planning/SPEC-approval-rules-inheritance-and-local-validation-lane.md +450 -0
  110. package/docs/planning/SPEC-assistant-persona-bootstrap.md +199 -0
  111. package/docs/planning/SPEC-assistant-rename.md +610 -0
  112. package/docs/planning/SPEC-bridge-app-server-protocol-alignment.md +667 -0
  113. package/docs/planning/SPEC-claude-runtime-host-for-work-ally.md +434 -0
  114. package/docs/planning/SPEC-cli-feishu-codex-session-unification.md +236 -0
  115. package/docs/planning/SPEC-codex-same-machine-session-handoff.md +873 -0
  116. package/docs/planning/SPEC-feishu-reaction-shortcuts.md +282 -0
  117. package/docs/planning/SPEC-local-stable-release-boundary.md +166 -0
  118. package/docs/planning/SPEC-managed-thread-entry-and-surface-mobility.md +862 -0
  119. package/docs/planning/SPEC-minimal-bridge-semantics-and-user-visible-surface.md +362 -0
  120. package/docs/planning/SPEC-npm-alpha-distribution-and-install-first-release.md +222 -0
  121. package/docs/planning/SPEC-remove-websocket-runtime-transport.md +364 -0
  122. package/docs/planning/SPEC-runtime-abstraction-phase-1.md +424 -0
  123. package/docs/planning/SPEC-runtime-connection-and-turn-recovery-semantics.md +274 -0
  124. package/docs/planning/SPEC-session-presence-and-state-visibility.md +397 -0
  125. package/docs/planning/SPEC-skill-first-capability-packaging.md +338 -0
  126. package/docs/planning/SPEC-stable-archive-contract.md +456 -0
  127. package/docs/planning/SPEC-supervised-start-boundary.md +127 -0
  128. package/docs/planning/SPEC-user-barrier-reduction-and-activation.md +832 -0
  129. package/docs/planning/ally-next.md +1278 -0
  130. package/docs/planning/assistant-workbench-spec.md +725 -0
  131. package/docs/planning/product-workbench.md +283 -0
  132. package/docs/product-onboarding.md +227 -0
  133. package/docs/product-spec-standard.md +528 -0
  134. package/docs/troubleshooting.md +45 -0
  135. package/docs/user-quickstart.md +46 -0
  136. package/internal/dispatch.sh +95 -0
  137. package/internal/lib/common.sh +1450 -0
  138. package/internal/modules/assistant/manage.sh +1312 -0
  139. package/internal/modules/bootstrap/setup.sh +144 -0
  140. package/internal/modules/config/init-env.sh +10 -0
  141. package/internal/modules/global/manage.sh +154 -0
  142. package/internal/modules/handoff/manage.sh +54 -0
  143. package/internal/modules/mcp/manage.sh +83 -0
  144. package/internal/modules/ops/logs.sh +76 -0
  145. package/internal/modules/routines/manage.sh +55 -0
  146. package/internal/modules/runtime/assistant-autosave.sh +26 -0
  147. package/internal/modules/runtime/restart.sh +6 -0
  148. package/internal/modules/runtime/start.sh +283 -0
  149. package/internal/modules/runtime/status.sh +194 -0
  150. package/internal/modules/runtime/stop.sh +55 -0
  151. package/internal/modules/runtime/supervisor.sh +216 -0
  152. package/internal/modules/runtime/update.sh +26 -0
  153. package/package.json +41 -0
  154. package/runtime/config/.gitkeep +0 -0
  155. package/runtime/host/.gitkeep +0 -0
  156. package/runtime/host/healthcheck-codex-app-server.ts +22 -0
  157. package/runtime/host/ping-pong-codex-app-server.ts +66 -0
  158. package/runtime/host/probe-codex-app-server.ts +115 -0
  159. package/skills/archive-reader/SKILL.md +9 -0
  160. package/skills/feishu-production-debug/SKILL.md +37 -0
  161. package/skills/feishu-production-debug/references/feishu-debug-order.md +49 -0
  162. package/skills/feishu-production-debug/references/platform-permission-baseline.md +23 -0
  163. package/skills/issue-to-spec-triage/SKILL.md +44 -0
  164. package/skills/issue-to-spec-triage/references/triage-rules.md +66 -0
  165. package/skills/memory-digest/SKILL.md +9 -0
  166. package/skills/post-implementation-closure/SKILL.md +39 -0
  167. package/skills/post-implementation-closure/references/closure-checklist.md +45 -0
  168. package/skills/post-implementation-closure/references/doc-drift-map.md +49 -0
  169. package/skills/product-spec/SKILL.md +244 -0
  170. package/templates/env.example +5 -0
  171. package/templates/routines/nightly-memory-digest.yaml +10 -0
  172. package/templates/workspace/AGENTS.md +26 -0
@@ -0,0 +1,494 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import type { WorkAllyConfig } from './config.ts';
4
+ import type { WorkSessionCliResumeRefType, WorkSessionMeta } from './types.ts';
5
+ import { nowIso } from './utils.ts';
6
+ import { WorkSessionStore } from './work-session-store.ts';
7
+
8
+ export type HandoffExportStatus = 'ready' | 'not_ready' | 'not_found';
9
+ export type HandoffExportReason = 'active_work_session_missing' | 'cli_resume_ref_missing';
10
+ export type AttachStatus = 'attached' | 'error';
11
+ export type AttachReason =
12
+ | 'assistant_codex_home_missing'
13
+ | 'attach_selector_required'
14
+ | 'attach_selector_conflict'
15
+ | 'candidate_not_found'
16
+ | 'candidate_ambiguous'
17
+ | 'workspace_mismatch';
18
+
19
+ export interface HandoffExportPayload {
20
+ status: HandoffExportStatus;
21
+ reason?: HandoffExportReason;
22
+ assistantName: string;
23
+ workspaceRoot: string;
24
+ assistantCodexHome: string | null;
25
+ workSessionId: string | null;
26
+ runtimeThreadId: string | null;
27
+ cliResumeRef: string | null;
28
+ cliResumeRefType: WorkSessionCliResumeRefType;
29
+ threadName: string | null;
30
+ activeSurface: WorkSessionMeta['activeSurface'] | null;
31
+ ownershipSource: WorkSessionMeta['ownershipSource'] | null;
32
+ resumeCapability: WorkSessionMeta['resumeCapability'] | 'not_ready';
33
+ command: string | null;
34
+ exportedAt: string;
35
+ }
36
+
37
+ interface AttachSelector {
38
+ last: boolean;
39
+ runtimeThreadId: string | null;
40
+ resumeRef: string | null;
41
+ }
42
+
43
+ interface AttachCandidate {
44
+ runtimeThreadId: string;
45
+ cliResumeRef: string;
46
+ cliResumeRefType: 'session_id';
47
+ threadName: string | null;
48
+ workspaceRoot: string;
49
+ updatedAt: string | null;
50
+ }
51
+
52
+ export interface AttachPayload {
53
+ status: AttachStatus;
54
+ reason?: AttachReason;
55
+ assistantName: string;
56
+ workspaceRoot: string;
57
+ assistantCodexHome: string | null;
58
+ selector: { mode: 'last' | 'thread' | 'resume_ref' | 'invalid'; value: string | null };
59
+ workSessionId: string | null;
60
+ threadHandle: string | null;
61
+ runtimeThreadId: string | null;
62
+ cliResumeRef: string | null;
63
+ cliResumeRefType: WorkSessionCliResumeRefType;
64
+ threadName: string | null;
65
+ activeSurface: WorkSessionMeta['activeSurface'] | null;
66
+ ownershipSource: WorkSessionMeta['ownershipSource'] | null;
67
+ candidateCount: number;
68
+ attachedAt: string;
69
+ }
70
+
71
+ function normalizeNullableText(value: string | null | undefined): string | null {
72
+ if (value === undefined || value === null) {
73
+ return null;
74
+ }
75
+ const trimmed = String(value).trim();
76
+ return trimmed || null;
77
+ }
78
+
79
+ function shellQuote(value: string): string {
80
+ if (value.length === 0) {
81
+ return "''";
82
+ }
83
+ return `'${value.replace(/'/g, `'\\''`)}'`;
84
+ }
85
+
86
+ function formatResumeCommand(codexHome: string | null, workspaceRoot: string, cliResumeRef: string): string {
87
+ const envPrefix = codexHome ? `CODEX_HOME=${shellQuote(codexHome)} ` : '';
88
+ return `${envPrefix}codex resume --cd ${shellQuote(workspaceRoot)} ${shellQuote(cliResumeRef)}`;
89
+ }
90
+
91
+ function dbTimestampToIso(value: unknown): string | null {
92
+ if (typeof value !== 'number' || !Number.isFinite(value)) {
93
+ return null;
94
+ }
95
+ const millis = value > 1e12 ? value : value * 1000;
96
+ return new Date(millis).toISOString();
97
+ }
98
+
99
+ function parseSessionIndex(filePath: string): Map<string, { threadName: string | null; updatedAt: string | null }> {
100
+ const result = new Map<string, { threadName: string | null; updatedAt: string | null }>();
101
+ if (!fs.existsSync(filePath)) {
102
+ return result;
103
+ }
104
+ const lines = fs.readFileSync(filePath, 'utf8').split(/\r?\n/);
105
+ for (const line of lines) {
106
+ const trimmed = line.trim();
107
+ if (!trimmed) {
108
+ continue;
109
+ }
110
+ try {
111
+ const parsed = JSON.parse(trimmed) as { id?: unknown; thread_name?: unknown; updated_at?: unknown };
112
+ if (typeof parsed.id !== 'string' || !parsed.id.trim()) {
113
+ continue;
114
+ }
115
+ result.set(parsed.id, {
116
+ threadName: typeof parsed.thread_name === 'string' ? parsed.thread_name : null,
117
+ updatedAt: typeof parsed.updated_at === 'string' ? parsed.updated_at : null,
118
+ });
119
+ } catch {
120
+ continue;
121
+ }
122
+ }
123
+ return result;
124
+ }
125
+
126
+ function attachSelectorFromArgs(args: string[]): AttachSelector {
127
+ let last = false;
128
+ let runtimeThreadId: string | null = null;
129
+ let resumeRef: string | null = null;
130
+
131
+ for (let index = 0; index < args.length; index += 1) {
132
+ const value = args[index];
133
+ if (value === '--last') {
134
+ last = true;
135
+ continue;
136
+ }
137
+ if (value === '--thread') {
138
+ runtimeThreadId = normalizeNullableText(args[index + 1]);
139
+ index += 1;
140
+ continue;
141
+ }
142
+ if (value.startsWith('--thread=')) {
143
+ runtimeThreadId = normalizeNullableText(value.slice('--thread='.length));
144
+ continue;
145
+ }
146
+ if (value === '--resume-ref') {
147
+ resumeRef = normalizeNullableText(args[index + 1]);
148
+ index += 1;
149
+ continue;
150
+ }
151
+ if (value.startsWith('--resume-ref=')) {
152
+ resumeRef = normalizeNullableText(value.slice('--resume-ref='.length));
153
+ }
154
+ }
155
+
156
+ return { last, runtimeThreadId, resumeRef };
157
+ }
158
+
159
+ async function openReadonlyDatabase(filePath: string) {
160
+ const sqlite = await import('node:sqlite');
161
+ return new sqlite.DatabaseSync(filePath, { open: true, readOnly: true });
162
+ }
163
+
164
+ export class HandoffService {
165
+ private readonly config: WorkAllyConfig;
166
+ private readonly workSessionStore: WorkSessionStore;
167
+
168
+ constructor(config: WorkAllyConfig, workSessionStore?: WorkSessionStore) {
169
+ this.config = config;
170
+ this.workSessionStore = workSessionStore || new WorkSessionStore(this.config.paths.workSessionsDir || path.join(this.config.paths.runtimeDir, 'work-sessions'));
171
+ }
172
+
173
+ private async hydrateCliResumeRef(current: WorkSessionMeta): Promise<WorkSessionMeta> {
174
+ if (current.cliResumeRef || current.resumeCapability === 'ready') {
175
+ return current;
176
+ }
177
+ const candidates = await this.loadAttachCandidates();
178
+ const candidate = candidates.find((item) => item.runtimeThreadId === current.runtimeThreadId) || null;
179
+ if (!candidate) {
180
+ return current;
181
+ }
182
+ return this.workSessionStore.updateWorkSession(current.workSessionId, {
183
+ cliResumeRef: candidate.cliResumeRef,
184
+ cliResumeRefType: candidate.cliResumeRefType,
185
+ threadName: candidate.threadName,
186
+ resumeCapability: 'ready',
187
+ lastRuntimeActivityAt: candidate.updatedAt,
188
+ });
189
+ }
190
+
191
+ async exportCurrentWorkSession(current: WorkSessionMeta | null): Promise<HandoffExportPayload> {
192
+ const exportedAt = nowIso();
193
+ if (!current) {
194
+ return {
195
+ status: 'not_found',
196
+ reason: 'active_work_session_missing',
197
+ assistantName: String(this.config.assistant?.name || '').trim() || 'native',
198
+ workspaceRoot: this.config.workspaceRoot,
199
+ assistantCodexHome: normalizeNullableText(this.config.assistant?.codexHome),
200
+ workSessionId: null,
201
+ threadHandle: null,
202
+ runtimeThreadId: null,
203
+ cliResumeRef: null,
204
+ cliResumeRefType: 'null',
205
+ threadName: null,
206
+ activeSurface: null,
207
+ ownershipSource: null,
208
+ resumeCapability: 'not_ready',
209
+ command: null,
210
+ exportedAt,
211
+ };
212
+ }
213
+
214
+ const hydrated = await this.hydrateCliResumeRef(current);
215
+
216
+ if (hydrated.resumeCapability !== 'ready' || !hydrated.cliResumeRef) {
217
+ return {
218
+ status: 'not_ready',
219
+ reason: 'cli_resume_ref_missing',
220
+ assistantName: hydrated.assistantName,
221
+ workspaceRoot: hydrated.workspaceRoot,
222
+ assistantCodexHome: hydrated.assistantCodexHome,
223
+ workSessionId: hydrated.workSessionId,
224
+ runtimeThreadId: hydrated.runtimeThreadId,
225
+ cliResumeRef: null,
226
+ cliResumeRefType: hydrated.cliResumeRefType,
227
+ threadName: hydrated.threadName,
228
+ activeSurface: hydrated.activeSurface,
229
+ ownershipSource: hydrated.ownershipSource,
230
+ resumeCapability: 'not_ready',
231
+ command: null,
232
+ exportedAt,
233
+ };
234
+ }
235
+
236
+ const updated = this.workSessionStore.updateWorkSession(hydrated.workSessionId, {
237
+ lastHandoffExportAt: exportedAt,
238
+ });
239
+ return {
240
+ status: 'ready',
241
+ assistantName: updated.assistantName,
242
+ workspaceRoot: updated.workspaceRoot,
243
+ assistantCodexHome: updated.assistantCodexHome,
244
+ workSessionId: updated.workSessionId,
245
+ runtimeThreadId: updated.runtimeThreadId,
246
+ cliResumeRef: updated.cliResumeRef,
247
+ cliResumeRefType: updated.cliResumeRefType,
248
+ threadName: updated.threadName,
249
+ activeSurface: updated.activeSurface,
250
+ ownershipSource: updated.ownershipSource,
251
+ resumeCapability: updated.resumeCapability,
252
+ command: formatResumeCommand(updated.assistantCodexHome, updated.workspaceRoot, updated.cliResumeRef),
253
+ exportedAt,
254
+ };
255
+ }
256
+
257
+ async exportActiveWorkSessionForAssistant(): Promise<HandoffExportPayload> {
258
+ const assistantName = String(this.config.assistant?.name || '').trim();
259
+ const current = assistantName
260
+ ? this.workSessionStore.getActiveWorkSessionForAssistant(assistantName)
261
+ : null;
262
+ return this.exportCurrentWorkSession(current);
263
+ }
264
+
265
+ markWorkSessionOfficialCli(workSessionId: string): WorkSessionMeta {
266
+ return this.workSessionStore.updateWorkSession(workSessionId, {
267
+ activeSurface: 'official_codex_cli',
268
+ ownershipSource: 'explicit_codex_launch',
269
+ lastSurfaceSwitchAt: nowIso(),
270
+ });
271
+ }
272
+
273
+ private existingManagedSessionFor(threadId: string): WorkSessionMeta | null {
274
+ const assistantName = String(this.config.assistant?.name || '').trim();
275
+ return this.workSessionStore.listWorkSessions().find((item) => (
276
+ item.assistantName === assistantName
277
+ && item.workspaceRoot === this.config.workspaceRoot
278
+ && item.runtimeThreadId === threadId
279
+ && !item.archivedAt
280
+ )) || null;
281
+ }
282
+
283
+ private async loadAttachCandidates(): Promise<AttachCandidate[]> {
284
+ const codexHome = normalizeNullableText(this.config.assistant?.codexHome);
285
+ if (!codexHome) {
286
+ return [];
287
+ }
288
+ const sqliteFile = path.join(codexHome, 'state_5.sqlite');
289
+ const sessionIndexFile = path.join(codexHome, 'session_index.jsonl');
290
+ if (!fs.existsSync(sqliteFile)) {
291
+ return [];
292
+ }
293
+
294
+ const sessionIndex = parseSessionIndex(sessionIndexFile);
295
+ const database = await openReadonlyDatabase(sqliteFile);
296
+ try {
297
+ const statement = database.prepare(`
298
+ SELECT id, cwd, title, updated_at
299
+ FROM threads
300
+ WHERE archived = 0 AND cwd = ?
301
+ ORDER BY updated_at DESC, id DESC
302
+ `);
303
+ const rows = statement.all(this.config.workspaceRoot) as Array<{ id: string; cwd: string; title: string; updated_at: number }>;
304
+ return rows.map((row) => {
305
+ const indexed = sessionIndex.get(row.id);
306
+ return {
307
+ runtimeThreadId: row.id,
308
+ cliResumeRef: row.id,
309
+ cliResumeRefType: 'session_id' as const,
310
+ threadName: indexed?.threadName || normalizeNullableText(row.title),
311
+ workspaceRoot: row.cwd,
312
+ updatedAt: indexed?.updatedAt || dbTimestampToIso(row.updated_at),
313
+ };
314
+ });
315
+ } finally {
316
+ database.close();
317
+ }
318
+ }
319
+
320
+ private async selectAttachCandidate(
321
+ selector: AttachSelector,
322
+ candidates: AttachCandidate[],
323
+ ): Promise<{ candidate: AttachCandidate | null; reason?: AttachReason; candidateCount: number; mode: 'last' | 'thread' | 'resume_ref' | 'invalid'; value: string | null }> {
324
+ const activeSelectors = Number(selector.last) + Number(Boolean(selector.runtimeThreadId)) + Number(Boolean(selector.resumeRef));
325
+ if (activeSelectors === 0) {
326
+ return { candidate: null, reason: 'attach_selector_required', candidateCount: candidates.length, mode: 'invalid', value: null };
327
+ }
328
+ if (activeSelectors > 1) {
329
+ return { candidate: null, reason: 'attach_selector_conflict', candidateCount: candidates.length, mode: 'invalid', value: null };
330
+ }
331
+
332
+ if (selector.last) {
333
+ if (candidates.length === 1) {
334
+ return { candidate: candidates[0], candidateCount: 1, mode: 'last', value: null };
335
+ }
336
+ return {
337
+ candidate: null,
338
+ reason: candidates.length === 0 ? 'candidate_not_found' : 'candidate_ambiguous',
339
+ candidateCount: candidates.length,
340
+ mode: 'last',
341
+ value: null,
342
+ };
343
+ }
344
+
345
+ if (selector.runtimeThreadId) {
346
+ const match = candidates.find((candidate) => candidate.runtimeThreadId === selector.runtimeThreadId) || null;
347
+ if (match) {
348
+ return { candidate: match, candidateCount: candidates.length, mode: 'thread', value: selector.runtimeThreadId };
349
+ }
350
+
351
+ const sqliteFile = path.join(String(this.config.assistant?.codexHome || ''), 'state_5.sqlite');
352
+ if (fs.existsSync(sqliteFile)) {
353
+ const database = await openReadonlyDatabase(sqliteFile);
354
+ try {
355
+ const row = database.prepare('SELECT cwd FROM threads WHERE id = ? LIMIT 1').get(selector.runtimeThreadId) as { cwd?: string } | undefined;
356
+ if (row?.cwd && row.cwd !== this.config.workspaceRoot) {
357
+ return { candidate: null, reason: 'workspace_mismatch', candidateCount: candidates.length, mode: 'thread', value: selector.runtimeThreadId };
358
+ }
359
+ } finally {
360
+ database.close();
361
+ }
362
+ }
363
+ return { candidate: null, reason: 'candidate_not_found', candidateCount: candidates.length, mode: 'thread', value: selector.runtimeThreadId };
364
+ }
365
+
366
+ const resumeRef = String(selector.resumeRef);
367
+ const exactId = candidates.find((candidate) => candidate.runtimeThreadId === resumeRef) || null;
368
+ if (exactId) {
369
+ return { candidate: exactId, candidateCount: candidates.length, mode: 'resume_ref', value: resumeRef };
370
+ }
371
+ const threadNameMatches = candidates.filter((candidate) => candidate.threadName === resumeRef);
372
+ if (threadNameMatches.length === 1) {
373
+ return { candidate: threadNameMatches[0], candidateCount: candidates.length, mode: 'resume_ref', value: resumeRef };
374
+ }
375
+ if (threadNameMatches.length > 1) {
376
+ return { candidate: null, reason: 'candidate_ambiguous', candidateCount: candidates.length, mode: 'resume_ref', value: resumeRef };
377
+ }
378
+ return { candidate: null, reason: 'candidate_not_found', candidateCount: candidates.length, mode: 'resume_ref', value: resumeRef };
379
+ }
380
+
381
+ async attachFromCliArgs(args: string[]): Promise<AttachPayload> {
382
+ const attachedAt = nowIso();
383
+ const assistantName = String(this.config.assistant?.name || '').trim() || 'native';
384
+ const assistantCodexHome = normalizeNullableText(this.config.assistant?.codexHome);
385
+ if (!assistantCodexHome) {
386
+ return {
387
+ status: 'error',
388
+ reason: 'assistant_codex_home_missing',
389
+ assistantName,
390
+ workspaceRoot: this.config.workspaceRoot,
391
+ assistantCodexHome: null,
392
+ selector: { mode: 'invalid', value: null },
393
+ workSessionId: null,
394
+ runtimeThreadId: null,
395
+ cliResumeRef: null,
396
+ cliResumeRefType: 'null',
397
+ threadName: null,
398
+ activeSurface: null,
399
+ ownershipSource: null,
400
+ candidateCount: 0,
401
+ attachedAt,
402
+ };
403
+ }
404
+
405
+ const candidates = await this.loadAttachCandidates();
406
+ const selected = await this.selectAttachCandidate(attachSelectorFromArgs(args), candidates);
407
+ if (!selected.candidate) {
408
+ return {
409
+ status: 'error',
410
+ reason: selected.reason,
411
+ assistantName,
412
+ workspaceRoot: this.config.workspaceRoot,
413
+ assistantCodexHome,
414
+ selector: { mode: selected.mode, value: selected.value },
415
+ workSessionId: null,
416
+ runtimeThreadId: null,
417
+ cliResumeRef: null,
418
+ cliResumeRefType: 'null',
419
+ threadName: null,
420
+ activeSurface: null,
421
+ ownershipSource: null,
422
+ candidateCount: selected.candidateCount,
423
+ attachedAt,
424
+ };
425
+ }
426
+
427
+ const existing = this.existingManagedSessionFor(selected.candidate.runtimeThreadId);
428
+ if (existing) {
429
+ this.workSessionStore.setAssistantActiveWorkSession(existing.assistantName, existing.workSessionId, attachedAt);
430
+ const updated = this.workSessionStore.updateWorkSession(existing.workSessionId, {
431
+ activeSurface: 'official_codex_cli',
432
+ ownershipSource: 'manual_attach',
433
+ lastAttachAt: attachedAt,
434
+ lastSurfaceSwitchAt: attachedAt,
435
+ cliResumeRef: selected.candidate.cliResumeRef,
436
+ cliResumeRefType: selected.candidate.cliResumeRefType,
437
+ threadName: selected.candidate.threadName,
438
+ resumeCapability: 'ready',
439
+ lastRuntimeActivityAt: selected.candidate.updatedAt,
440
+ });
441
+ return {
442
+ status: 'attached',
443
+ assistantName,
444
+ workspaceRoot: this.config.workspaceRoot,
445
+ assistantCodexHome,
446
+ selector: { mode: selected.mode, value: selected.value },
447
+ workSessionId: updated.workSessionId,
448
+ threadHandle: updated.threadHandle,
449
+ runtimeThreadId: updated.runtimeThreadId,
450
+ cliResumeRef: updated.cliResumeRef,
451
+ cliResumeRefType: updated.cliResumeRefType,
452
+ threadName: updated.threadName,
453
+ activeSurface: updated.activeSurface,
454
+ ownershipSource: updated.ownershipSource,
455
+ candidateCount: selected.candidateCount,
456
+ attachedAt,
457
+ };
458
+ }
459
+
460
+ const created = this.workSessionStore.createWorkSession({
461
+ assistantName,
462
+ assistantCodexHome,
463
+ workspaceRoot: this.config.workspaceRoot,
464
+ runtimeThreadId: selected.candidate.runtimeThreadId,
465
+ cliResumeRef: selected.candidate.cliResumeRef,
466
+ cliResumeRefType: selected.candidate.cliResumeRefType,
467
+ threadName: selected.candidate.threadName,
468
+ origin: 'codex_cli_attach',
469
+ activeSurface: 'official_codex_cli',
470
+ ownershipSource: 'manual_attach',
471
+ resumeCapability: 'ready',
472
+ lastAttachAt: attachedAt,
473
+ lastRuntimeActivityAt: selected.candidate.updatedAt,
474
+ });
475
+
476
+ return {
477
+ status: 'attached',
478
+ assistantName,
479
+ workspaceRoot: this.config.workspaceRoot,
480
+ assistantCodexHome,
481
+ selector: { mode: selected.mode, value: selected.value },
482
+ workSessionId: created.workSessionId,
483
+ threadHandle: created.threadHandle,
484
+ runtimeThreadId: created.runtimeThreadId,
485
+ cliResumeRef: created.cliResumeRef,
486
+ cliResumeRefType: created.cliResumeRefType,
487
+ threadName: created.threadName,
488
+ activeSurface: created.activeSurface,
489
+ ownershipSource: created.ownershipSource,
490
+ candidateCount: selected.candidateCount,
491
+ attachedAt,
492
+ };
493
+ }
494
+ }
@@ -0,0 +1,194 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import util from 'node:util';
4
+ import { ensureDir, nowIso, todayStamp } from './utils.ts';
5
+
6
+ export interface ProcessLoggerOptions {
7
+ logsDir: string;
8
+ component: string;
9
+ timezone: string;
10
+ retentionDays: number;
11
+ mirrorToStdout?: boolean;
12
+ captureConsole?: boolean;
13
+ }
14
+
15
+ type LogLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR';
16
+
17
+ const originalConsole = {
18
+ log: console.log.bind(console),
19
+ info: console.info.bind(console),
20
+ warn: console.warn.bind(console),
21
+ error: console.error.bind(console),
22
+ debug: console.debug.bind(console),
23
+ };
24
+
25
+ let processLogger: ProcessLogger | null = null;
26
+ let consolePatched = false;
27
+
28
+ function normalizeRetentionDays(value: number): number {
29
+ if (!Number.isFinite(value) || value < 1) {
30
+ return 3;
31
+ }
32
+ return Math.floor(value);
33
+ }
34
+
35
+ function serializeValue(value: unknown): string {
36
+ if (typeof value === 'string') {
37
+ return value;
38
+ }
39
+ return util.inspect(value, {
40
+ depth: 6,
41
+ breakLength: 120,
42
+ maxArrayLength: 50,
43
+ compact: true,
44
+ });
45
+ }
46
+
47
+ function formatMeta(meta?: Record<string, unknown>): string {
48
+ if (!meta || Object.keys(meta).length === 0) {
49
+ return '';
50
+ }
51
+ return ` ${JSON.stringify(meta)}`;
52
+ }
53
+
54
+ export class ProcessLogger {
55
+ private readonly logsDir: string;
56
+ private readonly component: string;
57
+ private readonly timezone: string;
58
+ private readonly retentionDays: number;
59
+ private readonly mirrorToStdout: boolean;
60
+ private lastPrunedStamp: string | null = null;
61
+
62
+ constructor(options: ProcessLoggerOptions) {
63
+ this.logsDir = ensureDir(options.logsDir);
64
+ this.component = options.component;
65
+ this.timezone = options.timezone;
66
+ this.retentionDays = normalizeRetentionDays(options.retentionDays);
67
+ this.mirrorToStdout = options.mirrorToStdout === true;
68
+ }
69
+
70
+ currentLogFile(date = new Date(), component = this.component): string {
71
+ return path.join(this.logsDir, `${component}-${todayStamp(date, this.timezone)}.log`);
72
+ }
73
+
74
+ private prune(now = new Date()): void {
75
+ const pruneStamp = todayStamp(now, this.timezone);
76
+ if (this.lastPrunedStamp === pruneStamp) {
77
+ return;
78
+ }
79
+ this.lastPrunedStamp = pruneStamp;
80
+
81
+ const entries = fs.readdirSync(this.logsDir, { withFileTypes: true });
82
+ for (const entry of entries) {
83
+ if (!entry.isFile()) {
84
+ continue;
85
+ }
86
+ const match = entry.name.match(/^(?<name>[a-z0-9-]+)-(?<date>\d{4}-\d{2}-\d{2})\.log$/i);
87
+ if (!match?.groups) {
88
+ continue;
89
+ }
90
+ const fileDate = new Date(`${match.groups.date}T00:00:00Z`);
91
+ if (Number.isNaN(fileDate.getTime())) {
92
+ continue;
93
+ }
94
+ const ageMs = now.getTime() - fileDate.getTime();
95
+ if (ageMs <= this.retentionDays * 24 * 60 * 60 * 1000) {
96
+ continue;
97
+ }
98
+ try {
99
+ fs.rmSync(path.join(this.logsDir, entry.name), { force: true });
100
+ } catch {
101
+ // noop
102
+ }
103
+ }
104
+ }
105
+
106
+ private append(line: string): void {
107
+ const now = new Date();
108
+ this.prune(now);
109
+ fs.appendFileSync(this.currentLogFile(now), line, 'utf8');
110
+ if (this.component !== 'timeline') {
111
+ fs.appendFileSync(this.currentLogFile(now, 'timeline'), line, 'utf8');
112
+ }
113
+ }
114
+
115
+ private emitStdout(level: LogLevel, line: string): void {
116
+ if (!this.mirrorToStdout) {
117
+ return;
118
+ }
119
+ if (level === 'WARN') {
120
+ originalConsole.warn(line.trimEnd());
121
+ return;
122
+ }
123
+ if (level === 'ERROR') {
124
+ originalConsole.error(line.trimEnd());
125
+ return;
126
+ }
127
+ originalConsole.log(line.trimEnd());
128
+ }
129
+
130
+ event(level: LogLevel, event: string, meta?: Record<string, unknown>): void {
131
+ const line = `${nowIso()} [${level}] [${this.component}] ${event}${formatMeta(meta)}\n`;
132
+ this.append(line);
133
+ this.emitStdout(level, line);
134
+ }
135
+
136
+ info(event: string, meta?: Record<string, unknown>): void {
137
+ this.event('INFO', event, meta);
138
+ }
139
+
140
+ warn(event: string, meta?: Record<string, unknown>): void {
141
+ this.event('WARN', event, meta);
142
+ }
143
+
144
+ error(event: string, meta?: Record<string, unknown>): void {
145
+ this.event('ERROR', event, meta);
146
+ }
147
+
148
+ debug(event: string, meta?: Record<string, unknown>): void {
149
+ this.event('DEBUG', event, meta);
150
+ }
151
+
152
+ console(level: LogLevel, args: unknown[]): void {
153
+ const rendered = args.map((item) => serializeValue(item)).join(' ');
154
+ const line = `${nowIso()} [${level}] [console] ${rendered}\n`;
155
+ this.append(line);
156
+ this.emitStdout(level, line);
157
+ }
158
+ }
159
+
160
+ export function initializeProcessLogger(options: ProcessLoggerOptions): ProcessLogger {
161
+ processLogger = new ProcessLogger(options);
162
+ if (options.captureConsole === false) {
163
+ return processLogger;
164
+ }
165
+ if (!consolePatched) {
166
+ console.log = (...args: unknown[]) => processLogger?.console('INFO', args);
167
+ console.info = (...args: unknown[]) => processLogger?.console('INFO', args);
168
+ console.warn = (...args: unknown[]) => processLogger?.console('WARN', args);
169
+ console.error = (...args: unknown[]) => processLogger?.console('ERROR', args);
170
+ console.debug = (...args: unknown[]) => processLogger?.console('DEBUG', args);
171
+ consolePatched = true;
172
+ }
173
+ return processLogger;
174
+ }
175
+
176
+ export function getProcessLogger(): ProcessLogger | null {
177
+ return processLogger;
178
+ }
179
+
180
+ export function logInfo(component: string, event: string, meta?: Record<string, unknown>): void {
181
+ processLogger?.info(`${component}.${event}`, meta);
182
+ }
183
+
184
+ export function logWarn(component: string, event: string, meta?: Record<string, unknown>): void {
185
+ processLogger?.warn(`${component}.${event}`, meta);
186
+ }
187
+
188
+ export function logError(component: string, event: string, meta?: Record<string, unknown>): void {
189
+ processLogger?.error(`${component}.${event}`, meta);
190
+ }
191
+
192
+ export function logDebug(component: string, event: string, meta?: Record<string, unknown>): void {
193
+ processLogger?.debug(`${component}.${event}`, meta);
194
+ }