Implement session state persistence for θ-cycle execution: - src/session/types.ts: SessionState, SessionMetadata interfaces - src/session/manager.ts: DynamoDB client with save/restore/query * saveState() - Persist session with TTL (default: 1 hour) * restoreState() - Recover session by ID * getPendingSessions() - Query incomplete sessions * updateStatus(), deleteSession() - State management - src/session/recovery.ts: Fly.io/Lambda restart recovery * recoverPendingSessions() - Auto-resume on restart * heartbeatSession() - Keep-alive TTL extension * completeSession(), failSession() - Lifecycle hooks - src/session/manager.test.ts: Unit tests (skeleton) Features: - DynamoDB TTL auto-cleanup - GSI for userId/guildId/status queries - Recovery on container restart - Multi-filter pending session queries Co-Authored-By: Claude <noreply@anthropic.com>
98 lines
2.1 KiB
TypeScript
98 lines
2.1 KiB
TypeScript
/**
|
|
* セッション永続化の型定義
|
|
*
|
|
* DynamoDBでθサイクルのセッション状態を永続化するための型
|
|
*/
|
|
|
|
import type { ThetaCycleState, ThetaPhase } from "../theta/types.js";
|
|
|
|
/**
|
|
* セッションの状態
|
|
*/
|
|
export enum SessionStatus {
|
|
/** 実行中 */
|
|
RUNNING = "running",
|
|
/** 一時停止中 */
|
|
PAUSED = "paused",
|
|
/** 完了 */
|
|
COMPLETED = "completed",
|
|
/** エラー停止 */
|
|
ERROR = "error",
|
|
}
|
|
|
|
/**
|
|
* セッションメタデータ
|
|
*/
|
|
export interface SessionMetadata {
|
|
/** セッションID */
|
|
sessionId: string;
|
|
/** ユーザーID */
|
|
userId?: string;
|
|
/** チャンネルID */
|
|
channelId?: string;
|
|
/** ギルドID (Discord) */
|
|
guildId?: string;
|
|
/** 開始時刻 */
|
|
startTime: number;
|
|
/** 最終更新時刻 */
|
|
lastUpdateTime: number;
|
|
/** ステータス */
|
|
status: SessionStatus;
|
|
/** TTL (有効期限) - Unix timestamp */
|
|
expiresAt: number;
|
|
}
|
|
|
|
/**
|
|
* 保存されるセッション状態
|
|
*/
|
|
export interface PersistedSessionState {
|
|
/** メタデータ */
|
|
metadata: SessionMetadata;
|
|
/** θサイクル状態 */
|
|
thetaState: ThetaCycleState;
|
|
/** 実行コンテキスト */
|
|
context: Record<string, unknown>;
|
|
/** エラー情報(ある場合) */
|
|
error?: {
|
|
message: string;
|
|
stack?: string;
|
|
code?: string;
|
|
};
|
|
}
|
|
|
|
/**
|
|
* セッション保存オプション
|
|
*/
|
|
export interface SaveStateOptions {
|
|
/** TTL (秒), デフォルト: 1時間 */
|
|
ttl?: number;
|
|
/** エラー情報を含めるか */
|
|
includeError?: boolean;
|
|
}
|
|
|
|
/**
|
|
* 未完了セッションフィルタ
|
|
*/
|
|
export interface PendingSessionsFilter {
|
|
/** ユーザーIDでフィルタ */
|
|
userId?: string;
|
|
/** チャンネルIDでフィルタ */
|
|
channelId?: string;
|
|
/** ギルドIDでフィルタ */
|
|
guildId?: string;
|
|
/** ステータスでフィルタ */
|
|
status?: SessionStatus;
|
|
}
|
|
|
|
/**
|
|
* セッション復元結果
|
|
*/
|
|
export interface RestoredSession {
|
|
/** セッション状態 */
|
|
state: PersistedSessionState;
|
|
/** 継続可能か */
|
|
resumable: boolean;
|
|
/** 復元時の経過時間(ms) */
|
|
elapsed: number;
|
|
}
|