@herdctl/core 0.0.1 → 0.0.2
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/dist/config/__tests__/agent.test.js +31 -13
- package/dist/config/__tests__/agent.test.js.map +1 -1
- package/dist/config/__tests__/merge.test.js +9 -2
- package/dist/config/__tests__/merge.test.js.map +1 -1
- package/dist/config/__tests__/schema.test.js +350 -1
- package/dist/config/__tests__/schema.test.js.map +1 -1
- package/dist/config/index.d.ts +1 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +3 -1
- package/dist/config/index.js.map +1 -1
- package/dist/config/schema.d.ts +828 -24
- package/dist/config/schema.d.ts.map +1 -1
- package/dist/config/schema.js +118 -6
- package/dist/config/schema.js.map +1 -1
- package/dist/fleet-manager/__tests__/coverage.test.js +11 -332
- package/dist/fleet-manager/__tests__/coverage.test.js.map +1 -1
- package/dist/fleet-manager/__tests__/errors.test.js +1 -49
- package/dist/fleet-manager/__tests__/errors.test.js.map +1 -1
- package/dist/fleet-manager/__tests__/integration.test.js +109 -0
- package/dist/fleet-manager/__tests__/integration.test.js.map +1 -1
- package/dist/fleet-manager/__tests__/reload.test.js +1 -1
- package/dist/fleet-manager/__tests__/reload.test.js.map +1 -1
- package/dist/fleet-manager/config-reload.d.ts +164 -0
- package/dist/fleet-manager/config-reload.d.ts.map +1 -0
- package/dist/fleet-manager/config-reload.js +445 -0
- package/dist/fleet-manager/config-reload.js.map +1 -0
- package/dist/fleet-manager/context.d.ts +76 -0
- package/dist/fleet-manager/context.d.ts.map +1 -0
- package/dist/fleet-manager/context.js +11 -0
- package/dist/fleet-manager/context.js.map +1 -0
- package/dist/fleet-manager/errors.d.ts +0 -25
- package/dist/fleet-manager/errors.d.ts.map +1 -1
- package/dist/fleet-manager/errors.js +0 -38
- package/dist/fleet-manager/errors.js.map +1 -1
- package/dist/fleet-manager/event-emitters.d.ts +123 -0
- package/dist/fleet-manager/event-emitters.d.ts.map +1 -0
- package/dist/fleet-manager/event-emitters.js +136 -0
- package/dist/fleet-manager/event-emitters.js.map +1 -0
- package/dist/fleet-manager/event-types.d.ts +0 -15
- package/dist/fleet-manager/event-types.d.ts.map +1 -1
- package/dist/fleet-manager/fleet-manager.d.ts +40 -653
- package/dist/fleet-manager/fleet-manager.d.ts.map +1 -1
- package/dist/fleet-manager/fleet-manager.js +95 -1720
- package/dist/fleet-manager/fleet-manager.js.map +1 -1
- package/dist/fleet-manager/index.d.ts +13 -2
- package/dist/fleet-manager/index.d.ts.map +1 -1
- package/dist/fleet-manager/index.js +19 -6
- package/dist/fleet-manager/index.js.map +1 -1
- package/dist/fleet-manager/job-control.d.ts +64 -0
- package/dist/fleet-manager/job-control.d.ts.map +1 -0
- package/dist/fleet-manager/job-control.js +296 -0
- package/dist/fleet-manager/job-control.js.map +1 -0
- package/dist/fleet-manager/log-streaming.d.ts +171 -0
- package/dist/fleet-manager/log-streaming.d.ts.map +1 -0
- package/dist/fleet-manager/log-streaming.js +503 -0
- package/dist/fleet-manager/log-streaming.js.map +1 -0
- package/dist/fleet-manager/schedule-executor.d.ts +63 -0
- package/dist/fleet-manager/schedule-executor.d.ts.map +1 -0
- package/dist/fleet-manager/schedule-executor.js +209 -0
- package/dist/fleet-manager/schedule-executor.js.map +1 -0
- package/dist/fleet-manager/schedule-management.d.ts +71 -0
- package/dist/fleet-manager/schedule-management.d.ts.map +1 -0
- package/dist/fleet-manager/schedule-management.js +171 -0
- package/dist/fleet-manager/schedule-management.js.map +1 -0
- package/dist/fleet-manager/status-queries.d.ts +105 -0
- package/dist/fleet-manager/status-queries.d.ts.map +1 -0
- package/dist/fleet-manager/status-queries.js +247 -0
- package/dist/fleet-manager/status-queries.js.map +1 -0
- package/dist/fleet-manager/types.d.ts +0 -39
- package/dist/fleet-manager/types.d.ts.map +1 -1
- package/dist/runner/__tests__/job-executor.test.js +206 -1
- package/dist/runner/__tests__/job-executor.test.js.map +1 -1
- package/dist/runner/job-executor.d.ts +9 -0
- package/dist/runner/job-executor.d.ts.map +1 -1
- package/dist/runner/job-executor.js +78 -4
- package/dist/runner/job-executor.js.map +1 -1
- package/dist/runner/types.d.ts +2 -0
- package/dist/runner/types.d.ts.map +1 -1
- package/dist/scheduler/__tests__/cron.test.d.ts +2 -0
- package/dist/scheduler/__tests__/cron.test.d.ts.map +1 -0
- package/dist/scheduler/__tests__/cron.test.js +867 -0
- package/dist/scheduler/__tests__/cron.test.js.map +1 -0
- package/dist/scheduler/__tests__/scheduler.test.js +164 -5
- package/dist/scheduler/__tests__/scheduler.test.js.map +1 -1
- package/dist/scheduler/cron.d.ts +126 -0
- package/dist/scheduler/cron.d.ts.map +1 -0
- package/dist/scheduler/cron.js +390 -0
- package/dist/scheduler/cron.js.map +1 -0
- package/dist/scheduler/errors.d.ts +81 -1
- package/dist/scheduler/errors.d.ts.map +1 -1
- package/dist/scheduler/errors.js +81 -6
- package/dist/scheduler/errors.js.map +1 -1
- package/dist/scheduler/index.d.ts +1 -0
- package/dist/scheduler/index.d.ts.map +1 -1
- package/dist/scheduler/index.js +2 -0
- package/dist/scheduler/index.js.map +1 -1
- package/dist/scheduler/schedule-runner.d.ts +2 -2
- package/dist/scheduler/schedule-runner.d.ts.map +1 -1
- package/dist/scheduler/schedule-runner.js +20 -8
- package/dist/scheduler/schedule-runner.js.map +1 -1
- package/dist/scheduler/scheduler.d.ts +4 -4
- package/dist/scheduler/scheduler.d.ts.map +1 -1
- package/dist/scheduler/scheduler.js +86 -20
- package/dist/scheduler/scheduler.js.map +1 -1
- package/dist/scheduler/types.d.ts +1 -1
- package/dist/scheduler/types.d.ts.map +1 -1
- package/dist/state/schemas/job-metadata.d.ts +2 -2
- package/package.json +33 -8
- package/.turbo/turbo-build.log +0 -4
- package/.turbo/turbo-test.log +0 -219
- package/.turbo/turbo-typecheck.log +0 -4
- package/coverage/base.css +0 -224
- package/coverage/block-navigation.js +0 -87
- package/coverage/coverage-final.json +0 -51
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +0 -251
- package/coverage/prettify.css +0 -1
- package/coverage/prettify.js +0 -2
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +0 -210
- package/coverage/src/config/index.html +0 -191
- package/coverage/src/config/index.ts.html +0 -442
- package/coverage/src/config/interpolate.ts.html +0 -652
- package/coverage/src/config/loader.ts.html +0 -1501
- package/coverage/src/config/merge.ts.html +0 -823
- package/coverage/src/config/parser.ts.html +0 -1213
- package/coverage/src/config/schema.ts.html +0 -1123
- package/coverage/src/fleet-manager/errors.ts.html +0 -2326
- package/coverage/src/fleet-manager/event-types.ts.html +0 -1219
- package/coverage/src/fleet-manager/fleet-manager.ts.html +0 -7030
- package/coverage/src/fleet-manager/index.html +0 -206
- package/coverage/src/fleet-manager/index.ts.html +0 -469
- package/coverage/src/fleet-manager/job-manager.ts.html +0 -2074
- package/coverage/src/fleet-manager/job-queue.ts.html +0 -2479
- package/coverage/src/fleet-manager/types.ts.html +0 -2602
- package/coverage/src/index.html +0 -116
- package/coverage/src/index.ts.html +0 -181
- package/coverage/src/runner/errors.ts.html +0 -1006
- package/coverage/src/runner/index.html +0 -191
- package/coverage/src/runner/index.ts.html +0 -256
- package/coverage/src/runner/job-executor.ts.html +0 -1429
- package/coverage/src/runner/message-processor.ts.html +0 -1150
- package/coverage/src/runner/sdk-adapter.ts.html +0 -658
- package/coverage/src/runner/types.ts.html +0 -559
- package/coverage/src/scheduler/errors.ts.html +0 -388
- package/coverage/src/scheduler/index.html +0 -206
- package/coverage/src/scheduler/index.ts.html +0 -244
- package/coverage/src/scheduler/interval.ts.html +0 -652
- package/coverage/src/scheduler/schedule-runner.ts.html +0 -1411
- package/coverage/src/scheduler/schedule-state.ts.html +0 -718
- package/coverage/src/scheduler/scheduler.ts.html +0 -1795
- package/coverage/src/scheduler/types.ts.html +0 -733
- package/coverage/src/state/directory.ts.html +0 -736
- package/coverage/src/state/errors.ts.html +0 -376
- package/coverage/src/state/fleet-state.ts.html +0 -937
- package/coverage/src/state/index.html +0 -221
- package/coverage/src/state/index.ts.html +0 -322
- package/coverage/src/state/job-metadata.ts.html +0 -1420
- package/coverage/src/state/job-output.ts.html +0 -1033
- package/coverage/src/state/schemas/fleet-state.ts.html +0 -445
- package/coverage/src/state/schemas/index.html +0 -176
- package/coverage/src/state/schemas/index.ts.html +0 -286
- package/coverage/src/state/schemas/job-metadata.ts.html +0 -628
- package/coverage/src/state/schemas/job-output.ts.html +0 -616
- package/coverage/src/state/schemas/session-info.ts.html +0 -361
- package/coverage/src/state/session.ts.html +0 -844
- package/coverage/src/state/types.ts.html +0 -262
- package/coverage/src/state/utils/atomic.ts.html +0 -748
- package/coverage/src/state/utils/index.html +0 -146
- package/coverage/src/state/utils/index.ts.html +0 -103
- package/coverage/src/state/utils/reads.ts.html +0 -1621
- package/coverage/src/work-sources/adapters/github.ts.html +0 -3583
- package/coverage/src/work-sources/adapters/index.html +0 -131
- package/coverage/src/work-sources/adapters/index.ts.html +0 -277
- package/coverage/src/work-sources/errors.ts.html +0 -298
- package/coverage/src/work-sources/index.html +0 -176
- package/coverage/src/work-sources/index.ts.html +0 -529
- package/coverage/src/work-sources/manager.ts.html +0 -1324
- package/coverage/src/work-sources/registry.ts.html +0 -619
- package/coverage/src/work-sources/types.ts.html +0 -568
- package/dist/fleet-manager/__tests__/event-helpers.test.d.ts +0 -7
- package/dist/fleet-manager/__tests__/event-helpers.test.d.ts.map +0 -1
- package/dist/fleet-manager/__tests__/event-helpers.test.js +0 -368
- package/dist/fleet-manager/__tests__/event-helpers.test.js.map +0 -1
- package/src/config/__tests__/agent.test.ts +0 -864
- package/src/config/__tests__/interpolate.test.ts +0 -644
- package/src/config/__tests__/loader.test.ts +0 -784
- package/src/config/__tests__/merge.test.ts +0 -751
- package/src/config/__tests__/parser.test.ts +0 -533
- package/src/config/__tests__/schema.test.ts +0 -873
- package/src/config/index.ts +0 -119
- package/src/config/interpolate.ts +0 -189
- package/src/config/loader.ts +0 -472
- package/src/config/merge.ts +0 -246
- package/src/config/parser.ts +0 -376
- package/src/config/schema.ts +0 -346
- package/src/fleet-manager/__tests__/coverage.test.ts +0 -2869
- package/src/fleet-manager/__tests__/errors.test.ts +0 -660
- package/src/fleet-manager/__tests__/event-helpers.test.ts +0 -448
- package/src/fleet-manager/__tests__/integration.test.ts +0 -1209
- package/src/fleet-manager/__tests__/job-control.test.ts +0 -283
- package/src/fleet-manager/__tests__/job-manager.test.ts +0 -869
- package/src/fleet-manager/__tests__/job-queue.test.ts +0 -401
- package/src/fleet-manager/__tests__/reload.test.ts +0 -751
- package/src/fleet-manager/__tests__/status-queries.test.ts +0 -595
- package/src/fleet-manager/__tests__/trigger.test.ts +0 -601
- package/src/fleet-manager/errors.ts +0 -747
- package/src/fleet-manager/event-types.ts +0 -378
- package/src/fleet-manager/fleet-manager.ts +0 -2315
- package/src/fleet-manager/index.ts +0 -128
- package/src/fleet-manager/job-manager.ts +0 -663
- package/src/fleet-manager/job-queue.ts +0 -798
- package/src/fleet-manager/types.ts +0 -839
- package/src/index.ts +0 -32
- package/src/runner/__tests__/errors.test.ts +0 -382
- package/src/runner/__tests__/job-executor.test.ts +0 -1708
- package/src/runner/__tests__/message-processor.test.ts +0 -960
- package/src/runner/__tests__/sdk-adapter.test.ts +0 -626
- package/src/runner/errors.ts +0 -307
- package/src/runner/index.ts +0 -57
- package/src/runner/job-executor.ts +0 -448
- package/src/runner/message-processor.ts +0 -355
- package/src/runner/sdk-adapter.ts +0 -191
- package/src/runner/types.ts +0 -158
- package/src/scheduler/__tests__/errors.test.ts +0 -159
- package/src/scheduler/__tests__/interval.test.ts +0 -515
- package/src/scheduler/__tests__/schedule-runner.test.ts +0 -798
- package/src/scheduler/__tests__/schedule-state.test.ts +0 -671
- package/src/scheduler/__tests__/scheduler.test.ts +0 -1280
- package/src/scheduler/errors.ts +0 -101
- package/src/scheduler/index.ts +0 -53
- package/src/scheduler/interval.ts +0 -189
- package/src/scheduler/schedule-runner.ts +0 -442
- package/src/scheduler/schedule-state.ts +0 -211
- package/src/scheduler/scheduler.ts +0 -570
- package/src/scheduler/types.ts +0 -216
- package/src/state/__tests__/directory.test.ts +0 -595
- package/src/state/__tests__/fleet-state.test.ts +0 -868
- package/src/state/__tests__/job-metadata-schema.test.ts +0 -414
- package/src/state/__tests__/job-metadata.test.ts +0 -831
- package/src/state/__tests__/job-output.test.ts +0 -856
- package/src/state/__tests__/session-schema.test.ts +0 -378
- package/src/state/__tests__/session.test.ts +0 -604
- package/src/state/directory.ts +0 -217
- package/src/state/errors.ts +0 -97
- package/src/state/fleet-state.ts +0 -284
- package/src/state/index.ts +0 -79
- package/src/state/job-metadata.ts +0 -445
- package/src/state/job-output.ts +0 -316
- package/src/state/schemas/__tests__/job-output.test.ts +0 -338
- package/src/state/schemas/fleet-state.ts +0 -120
- package/src/state/schemas/index.ts +0 -67
- package/src/state/schemas/job-metadata.ts +0 -181
- package/src/state/schemas/job-output.ts +0 -177
- package/src/state/schemas/session-info.ts +0 -92
- package/src/state/session.ts +0 -253
- package/src/state/types.ts +0 -59
- package/src/state/utils/__tests__/atomic.test.ts +0 -723
- package/src/state/utils/__tests__/reads.test.ts +0 -1071
- package/src/state/utils/atomic.ts +0 -221
- package/src/state/utils/index.ts +0 -6
- package/src/state/utils/reads.ts +0 -512
- package/src/work-sources/__tests__/github.test.ts +0 -1800
- package/src/work-sources/__tests__/manager.test.ts +0 -529
- package/src/work-sources/__tests__/registry.test.ts +0 -477
- package/src/work-sources/__tests__/types.test.ts +0 -479
- package/src/work-sources/adapters/github.ts +0 -1166
- package/src/work-sources/adapters/index.ts +0 -64
- package/src/work-sources/errors.ts +0 -71
- package/src/work-sources/index.ts +0 -148
- package/src/work-sources/manager.ts +0 -413
- package/src/work-sources/registry.ts +0 -178
- package/src/work-sources/types.ts +0 -161
- package/tsconfig.json +0 -9
- package/vitest.config.ts +0 -19
package/src/state/session.ts
DELETED
|
@@ -1,253 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Session state persistence operations
|
|
3
|
-
*
|
|
4
|
-
* Provides CRUD operations for session info files stored at
|
|
5
|
-
* .herdctl/sessions/<agent-name>.json
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { unlink } from "node:fs/promises";
|
|
9
|
-
import { join } from "node:path";
|
|
10
|
-
import { atomicWriteJson } from "./utils/atomic.js";
|
|
11
|
-
import { safeReadJson } from "./utils/reads.js";
|
|
12
|
-
import {
|
|
13
|
-
SessionInfoSchema,
|
|
14
|
-
createSessionInfo,
|
|
15
|
-
type SessionInfo,
|
|
16
|
-
type SessionMode,
|
|
17
|
-
type CreateSessionOptions,
|
|
18
|
-
} from "./schemas/session-info.js";
|
|
19
|
-
import { StateFileError } from "./errors.js";
|
|
20
|
-
|
|
21
|
-
// =============================================================================
|
|
22
|
-
// Types
|
|
23
|
-
// =============================================================================
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Options for session operations
|
|
27
|
-
*/
|
|
28
|
-
export interface SessionOptions {
|
|
29
|
-
/** Logger for warnings */
|
|
30
|
-
logger?: SessionLogger;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Logger interface for session operations
|
|
35
|
-
*/
|
|
36
|
-
export interface SessionLogger {
|
|
37
|
-
warn: (message: string) => void;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Partial updates for session info
|
|
42
|
-
*/
|
|
43
|
-
export type SessionInfoUpdates = Partial<
|
|
44
|
-
Omit<SessionInfo, "agent_name" | "created_at">
|
|
45
|
-
>;
|
|
46
|
-
|
|
47
|
-
// =============================================================================
|
|
48
|
-
// Helper Functions
|
|
49
|
-
// =============================================================================
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Get the file path for an agent's session
|
|
53
|
-
*/
|
|
54
|
-
function getSessionFilePath(sessionsDir: string, agentName: string): string {
|
|
55
|
-
return join(sessionsDir, `${agentName}.json`);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// =============================================================================
|
|
59
|
-
// CRUD Operations
|
|
60
|
-
// =============================================================================
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Get session info for an agent
|
|
64
|
-
*
|
|
65
|
-
* Returns null if no session exists (handles missing file gracefully).
|
|
66
|
-
*
|
|
67
|
-
* @param sessionsDir - Path to the sessions directory
|
|
68
|
-
* @param agentName - Name of the agent
|
|
69
|
-
* @param options - Optional operation options
|
|
70
|
-
* @returns The session info, or null if not found
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
* ```typescript
|
|
74
|
-
* const session = await getSessionInfo('/path/to/.herdctl/sessions', 'my-agent');
|
|
75
|
-
* if (session) {
|
|
76
|
-
* console.log(`Session ${session.session_id} has ${session.job_count} jobs`);
|
|
77
|
-
* } else {
|
|
78
|
-
* console.log('No existing session');
|
|
79
|
-
* }
|
|
80
|
-
* ```
|
|
81
|
-
*/
|
|
82
|
-
export async function getSessionInfo(
|
|
83
|
-
sessionsDir: string,
|
|
84
|
-
agentName: string,
|
|
85
|
-
options: SessionOptions = {}
|
|
86
|
-
): Promise<SessionInfo | null> {
|
|
87
|
-
const { logger = console } = options;
|
|
88
|
-
const filePath = getSessionFilePath(sessionsDir, agentName);
|
|
89
|
-
|
|
90
|
-
const result = await safeReadJson<unknown>(filePath);
|
|
91
|
-
|
|
92
|
-
if (!result.success) {
|
|
93
|
-
// File not found is not an error - return null
|
|
94
|
-
if (result.error.code === "ENOENT") {
|
|
95
|
-
return null;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// Log other errors but don't throw - treat as missing
|
|
99
|
-
logger.warn(
|
|
100
|
-
`Failed to read session file for ${agentName}: ${result.error.message}`
|
|
101
|
-
);
|
|
102
|
-
return null;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// Parse and validate
|
|
106
|
-
const parseResult = SessionInfoSchema.safeParse(result.data);
|
|
107
|
-
if (!parseResult.success) {
|
|
108
|
-
logger.warn(
|
|
109
|
-
`Corrupted session file for ${agentName}: ${parseResult.error.message}. Returning null.`
|
|
110
|
-
);
|
|
111
|
-
return null;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return parseResult.data;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Update session info for an agent
|
|
119
|
-
*
|
|
120
|
-
* If no session exists, creates a new one with the provided updates.
|
|
121
|
-
* Automatically updates last_used_at timestamp.
|
|
122
|
-
*
|
|
123
|
-
* @param sessionsDir - Path to the sessions directory
|
|
124
|
-
* @param agentName - Name of the agent
|
|
125
|
-
* @param updates - Updates to apply to the session
|
|
126
|
-
* @returns The updated session info
|
|
127
|
-
* @throws StateFileError if the file cannot be written
|
|
128
|
-
*
|
|
129
|
-
* @example
|
|
130
|
-
* ```typescript
|
|
131
|
-
* // Update existing session
|
|
132
|
-
* const session = await updateSessionInfo('/path/to/.herdctl/sessions', 'my-agent', {
|
|
133
|
-
* job_count: 5,
|
|
134
|
-
* mode: 'interactive',
|
|
135
|
-
* });
|
|
136
|
-
*
|
|
137
|
-
* // Create new session
|
|
138
|
-
* const newSession = await updateSessionInfo('/path/to/.herdctl/sessions', 'new-agent', {
|
|
139
|
-
* session_id: 'claude-session-abc123',
|
|
140
|
-
* mode: 'autonomous',
|
|
141
|
-
* });
|
|
142
|
-
* ```
|
|
143
|
-
*/
|
|
144
|
-
export async function updateSessionInfo(
|
|
145
|
-
sessionsDir: string,
|
|
146
|
-
agentName: string,
|
|
147
|
-
updates: SessionInfoUpdates
|
|
148
|
-
): Promise<SessionInfo> {
|
|
149
|
-
const filePath = getSessionFilePath(sessionsDir, agentName);
|
|
150
|
-
|
|
151
|
-
// Try to read existing session
|
|
152
|
-
const existingResult = await safeReadJson<unknown>(filePath);
|
|
153
|
-
let existingSession: SessionInfo | null = null;
|
|
154
|
-
|
|
155
|
-
if (existingResult.success) {
|
|
156
|
-
const parseResult = SessionInfoSchema.safeParse(existingResult.data);
|
|
157
|
-
if (parseResult.success) {
|
|
158
|
-
existingSession = parseResult.data;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const now = new Date().toISOString();
|
|
163
|
-
|
|
164
|
-
let updatedSession: SessionInfo;
|
|
165
|
-
|
|
166
|
-
if (existingSession) {
|
|
167
|
-
// Update existing session
|
|
168
|
-
updatedSession = {
|
|
169
|
-
...existingSession,
|
|
170
|
-
...updates,
|
|
171
|
-
last_used_at: now, // Always update last_used_at
|
|
172
|
-
};
|
|
173
|
-
} else {
|
|
174
|
-
// Create new session - session_id is required for new sessions
|
|
175
|
-
if (!updates.session_id) {
|
|
176
|
-
throw new StateFileError(
|
|
177
|
-
"session_id is required when creating a new session",
|
|
178
|
-
filePath,
|
|
179
|
-
"write"
|
|
180
|
-
);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
const newSession = createSessionInfo({
|
|
184
|
-
agent_name: agentName,
|
|
185
|
-
session_id: updates.session_id,
|
|
186
|
-
mode: updates.mode,
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
updatedSession = {
|
|
190
|
-
...newSession,
|
|
191
|
-
...updates,
|
|
192
|
-
agent_name: agentName, // Ensure agent_name is not overwritten
|
|
193
|
-
created_at: newSession.created_at, // Preserve created_at
|
|
194
|
-
last_used_at: now,
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// Validate the updated session
|
|
199
|
-
const validated = SessionInfoSchema.parse(updatedSession);
|
|
200
|
-
|
|
201
|
-
// Write atomically
|
|
202
|
-
try {
|
|
203
|
-
await atomicWriteJson(filePath, validated);
|
|
204
|
-
} catch (error) {
|
|
205
|
-
throw new StateFileError(
|
|
206
|
-
`Failed to update session file for ${agentName}: ${(error as Error).message}`,
|
|
207
|
-
filePath,
|
|
208
|
-
"write",
|
|
209
|
-
error as Error
|
|
210
|
-
);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
return validated;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Clear session info for an agent (delete the session file)
|
|
218
|
-
*
|
|
219
|
-
* @param sessionsDir - Path to the sessions directory
|
|
220
|
-
* @param agentName - Name of the agent
|
|
221
|
-
* @returns true if the session was deleted, false if it didn't exist
|
|
222
|
-
*
|
|
223
|
-
* @example
|
|
224
|
-
* ```typescript
|
|
225
|
-
* const cleared = await clearSession('/path/to/.herdctl/sessions', 'my-agent');
|
|
226
|
-
* if (cleared) {
|
|
227
|
-
* console.log('Session cleared');
|
|
228
|
-
* } else {
|
|
229
|
-
* console.log('No session to clear');
|
|
230
|
-
* }
|
|
231
|
-
* ```
|
|
232
|
-
*/
|
|
233
|
-
export async function clearSession(
|
|
234
|
-
sessionsDir: string,
|
|
235
|
-
agentName: string
|
|
236
|
-
): Promise<boolean> {
|
|
237
|
-
const filePath = getSessionFilePath(sessionsDir, agentName);
|
|
238
|
-
|
|
239
|
-
try {
|
|
240
|
-
await unlink(filePath);
|
|
241
|
-
return true;
|
|
242
|
-
} catch (error) {
|
|
243
|
-
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
|
|
244
|
-
return false;
|
|
245
|
-
}
|
|
246
|
-
throw new StateFileError(
|
|
247
|
-
`Failed to delete session file for ${agentName}: ${(error as Error).message}`,
|
|
248
|
-
filePath,
|
|
249
|
-
"write",
|
|
250
|
-
error as Error
|
|
251
|
-
);
|
|
252
|
-
}
|
|
253
|
-
}
|
package/src/state/types.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Type definitions for state management
|
|
3
|
-
*
|
|
4
|
-
* Defines types for state directory structure and configuration
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Paths to all state directory subdirectories and files
|
|
9
|
-
*/
|
|
10
|
-
export interface StateDirectory {
|
|
11
|
-
/** Root state directory path (e.g., .herdctl/) */
|
|
12
|
-
root: string;
|
|
13
|
-
/** Path to jobs subdirectory */
|
|
14
|
-
jobs: string;
|
|
15
|
-
/** Path to sessions subdirectory */
|
|
16
|
-
sessions: string;
|
|
17
|
-
/** Path to logs subdirectory */
|
|
18
|
-
logs: string;
|
|
19
|
-
/** Path to state.yaml file */
|
|
20
|
-
stateFile: string;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Options for initializing the state directory
|
|
25
|
-
*/
|
|
26
|
-
export interface InitStateDirectoryOptions {
|
|
27
|
-
/**
|
|
28
|
-
* Custom path for the state directory.
|
|
29
|
-
* Defaults to .herdctl/ in the current working directory.
|
|
30
|
-
*/
|
|
31
|
-
path?: string;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Result of state directory validation
|
|
36
|
-
*/
|
|
37
|
-
export interface StateDirectoryValidation {
|
|
38
|
-
/** Whether the directory structure is valid */
|
|
39
|
-
valid: boolean;
|
|
40
|
-
/** List of missing directories or files */
|
|
41
|
-
missing: string[];
|
|
42
|
-
/** List of validation errors */
|
|
43
|
-
errors: string[];
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Subdirectory names within the state directory
|
|
48
|
-
*/
|
|
49
|
-
export const STATE_SUBDIRECTORIES = ["jobs", "sessions", "logs"] as const;
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Default state directory name
|
|
53
|
-
*/
|
|
54
|
-
export const DEFAULT_STATE_DIR_NAME = ".herdctl";
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Default state file name
|
|
58
|
-
*/
|
|
59
|
-
export const STATE_FILE_NAME = "state.yaml";
|