@alfe.ai/openclaw-sync 0.1.0 → 0.1.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.
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.d.cts","names":[],"sources":["../src/plugin.ts"],"mappings":";;;AAgDsB;;;;;AAWA;;;;;;;;;;AAsCtB,UAjDU,YAAA,CAiDuB;EA8X3B,IAAA,CAAA,GAAA,EAqUL,MAAA,CAAA,EAAA,IAAA;EAAA,IAAA,CAAA,GAAA,EAAA,MAAA,CAAA,EAAA,IAAA;OA9Te,CAAA,GAAA,EAAA,MAAA,CAAA,EAAA,IAAA;OAmPQ,CAAA,GAAA,EAAA,MAAA,CAAA,EAAA,IAAA;;UAlqBd,oBAAA,CAwsBa;QAAmB,EAvsBhC,MAusBgC,CAAA,MAAA,EAAA,OAAA,CAAA;cAAgB,CAAA,EAAA,MAAA;EAAA,QAAA,EAAA,MAAA;UApsBhD;;UAGA,SAAA;UACA;;WAEC;wDAC6C;;;iBAGvC,gCAAgC;iBAChC,gCAAgC;;;UA2BhC,gBAAA;;;;;;;;;;;;;;cA8XX;;;;;gBAOU;kBAmPQ,YAAS;iBAsCV,mBAAmB,mBAAgB"}
1
+ {"version":3,"file":"plugin.d.cts","names":[],"sources":["../src/plugin.ts"],"mappings":";;;AAgDsB;;;;;AAWA;;;;;;;;;;AAsCtB,UAjDU,YAAA,CAiDuB;EA8X3B,IAAA,CAAA,GAAA,EA4UL,MAAA,CAAA,EAAA,IAAA;EAAA,IAAA,CAAA,GAAA,EAAA,MAAA,CAAA,EAAA,IAAA;OArUe,CAAA,GAAA,EAAA,MAAA,CAAA,EAAA,IAAA;OA0PQ,CAAA,GAAA,EAAA,MAAA,CAAA,EAAA,IAAA;;UAzqBd,oBAAA,CA+sBa;QAAmB,EA9sBhC,MA8sBgC,CAAA,MAAA,EAAA,OAAA,CAAA;cAAgB,CAAA,EAAA,MAAA;EAAA,QAAA,EAAA,MAAA;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CAAA;cAAgB,CAAA,EAAA,MAAA;EAAA,QAAA,EAAA,MAAA;UA3sBhD;;UAGA,SAAA;UACA;;WAEC;wDAC6C;;;iBAGvC,gCAAgC;iBAChC,gCAAgC;;;UA2BhC,gBAAA;;;;;;;;;;;;;;cA8XX;;;;;gBAOU;kBA0PQ,YAAS;iBAsCV,mBAAmB,mBAAgB"}
package/dist/plugin2.cjs CHANGED
@@ -574,7 +574,7 @@ const plugin = {
574
574
  "conversations",
575
575
  "memory"
576
576
  ];
577
- const syncSchedule = pluginConfig.syncSchedule ?? "daily";
577
+ const syncSchedule = pluginConfig.syncSchedule ?? "realtime";
578
578
  const socketPath = pluginConfig.socketPath ?? alfeConfig?.socketPath ?? _alfe_ai_config.DEFAULT_SOCKET_PATH;
579
579
  const startSyncService = async () => {
580
580
  if (globalThis.__alfeSyncPluginActivated === true) {
package/dist/plugin2.js CHANGED
@@ -574,7 +574,7 @@ const plugin = {
574
574
  "conversations",
575
575
  "memory"
576
576
  ];
577
- const syncSchedule = pluginConfig.syncSchedule ?? "daily";
577
+ const syncSchedule = pluginConfig.syncSchedule ?? "realtime";
578
578
  const socketPath = pluginConfig.socketPath ?? alfeConfig?.socketPath ?? DEFAULT_SOCKET_PATH;
579
579
  const startSyncService = async () => {
580
580
  if (globalThis.__alfeSyncPluginActivated === true) {
@@ -1 +1 @@
1
- {"version":3,"file":"plugin2.js","names":["resolveAlfeConfig","alfeConfigExists"],"sources":["../src/watcher.ts","../src/shared-sync.ts","../src/delete-brake.ts","../src/plugin.ts"],"sourcesContent":["/**\n * AlfeSync watcher — recursive file watcher with debounce and ignore support.\n *\n * Uses chokidar to watch the workspace root, debounces per-file changes\n * by 2 seconds, and emits batches of changed paths.\n */\n\nimport { watch } from \"chokidar\";\nimport { relative } from \"node:path\";\nimport { loadIgnorePatterns, shouldIgnore } from \"./ignore.js\";\n\nexport interface WatcherOptions {\n workspacePath: string;\n /** Debounce delay per file in milliseconds. Default: 2000 */\n debounceMs?: number;\n /** Callback invoked with batches of changed relative paths */\n onChanges: (paths: string[]) => void | Promise<void>;\n}\n\n/**\n * Start watching a workspace for file changes.\n *\n * Returns a cleanup function to stop watching.\n */\nexport async function startWatcher(\n options: WatcherOptions,\n): Promise<() => Promise<void>> {\n const { workspacePath, debounceMs = 2000, onChanges } = options;\n const ignorePatterns = await loadIgnorePatterns(workspacePath);\n\n // Pending changes map: relativePath → timeout handle\n const pending = new Map<string, ReturnType<typeof setTimeout>>();\n // Batch accumulator for flushing\n let batchPaths = new Set<string>();\n let flushTimer: ReturnType<typeof setTimeout> | null = null;\n\n function scheduleBatch() {\n if (flushTimer) return;\n flushTimer = setTimeout(() => {\n flushTimer = null;\n if (batchPaths.size === 0) return;\n\n const paths = [...batchPaths];\n batchPaths = new Set();\n void onChanges(paths);\n }, debounceMs);\n }\n\n function handleChange(absolutePath: string) {\n const relativePath = relative(workspacePath, absolutePath);\n\n // Skip ignored files\n if (shouldIgnore(relativePath, ignorePatterns)) return;\n\n // Clear existing timer for this file\n const existingTimer = pending.get(relativePath);\n if (existingTimer) clearTimeout(existingTimer);\n\n // Debounce: wait before adding to batch\n const timer = setTimeout(() => {\n pending.delete(relativePath);\n batchPaths.add(relativePath);\n scheduleBatch();\n }, debounceMs);\n\n pending.set(relativePath, timer);\n }\n\n const watcher = watch(workspacePath, {\n persistent: true,\n ignoreInitial: true,\n followSymlinks: false,\n depth: undefined, // unlimited depth\n ignored: [\n \"**/node_modules/**\",\n \"**/.alfesync/**\",\n \"**/.git/**\",\n \"**/.sst/**\",\n ],\n });\n\n watcher.on(\"add\", handleChange);\n watcher.on(\"change\", handleChange);\n watcher.on(\"unlink\", handleChange);\n\n // Return cleanup function\n return async () => {\n // Clear all pending timers\n for (const timer of pending.values()) {\n clearTimeout(timer);\n }\n pending.clear();\n\n if (flushTimer) {\n clearTimeout(flushTimer);\n flushTimer = null;\n }\n\n await watcher.close();\n };\n}\n","/**\n * Shared file sync — mirrors org/team/project files to a `shared/`\n * directory in the agent's workspace, organised by scope:\n *\n * shared/org/<files…>\n * shared/teams/<scopeId>/<files…>\n * shared/projects/<scopeId>/<files…>\n *\n * Backed by the agent self-service API (`AgentApiClient.sharedListFiles`,\n * `AgentApiClient.sharedDownloadUrl`).\n */\n\nimport { join, dirname, normalize, sep } from \"node:path\";\nimport { mkdir, writeFile, unlink, rm } from \"node:fs/promises\";\nimport type { AgentApiClient } from \"@alfe.ai/agent-api-client\";\n\nconst MAX_SHARED_FILE_SIZE = 100 * 1024 * 1024; // 100 MB\n\n/** Throw if `resolvedPath` would escape `baseDir`. */\nfunction assertContained(baseDir: string, resolvedPath: string): void {\n const normalizedBase = normalize(baseDir) + sep;\n const normalizedPath = normalize(resolvedPath);\n if (\n !normalizedPath.startsWith(normalizedBase) &&\n normalizedPath !== normalize(baseDir)\n ) {\n throw new Error(`Path traversal blocked: ${resolvedPath} escapes ${baseDir}`);\n }\n}\n\nexport interface SharedScope {\n scopeType: \"team\" | \"project\" | \"org\";\n scopeId: string;\n name: string;\n}\n\nexport interface SharedSyncConfig {\n workspacePath: string;\n client: AgentApiClient;\n}\n\ninterface PluginLogger {\n info(msg: string): void;\n warn(msg: string): void;\n error(msg: string): void;\n debug(msg: string): void;\n}\n\nexport interface SharedSyncEngine {\n initialize(scopes: SharedScope[]): Promise<void>;\n updateScopes(scopes: SharedScope[]): Promise<void>;\n handleNotification(filePath: string, eventType: \"created\" | \"deleted\"): Promise<void>;\n fullSync(): Promise<void>;\n getScopes(): SharedScope[];\n}\n\nexport function createSharedSyncEngine(\n config: SharedSyncConfig,\n log: PluginLogger,\n): SharedSyncEngine {\n let activeScopes: SharedScope[] = [];\n const sharedDir = join(config.workspacePath, \"shared\");\n\n function scopeDir(scope: SharedScope): string {\n if (scope.scopeType === \"org\") return join(sharedDir, \"org\");\n const plural = scope.scopeType === \"team\" ? \"teams\" : \"projects\";\n return join(sharedDir, plural, scope.scopeId);\n }\n\n async function downloadFile(\n scope: SharedScope,\n filePath: string,\n localPath: string,\n ): Promise<void> {\n assertContained(scopeDir(scope), localPath);\n\n const { downloadUrl } = await config.client.sharedDownloadUrl({\n scope: scope.scopeType,\n scopeId: scope.scopeId,\n filePath,\n });\n\n // Presigned URL → S3 directly (raw fetch is the right tool here).\n const response = await fetch(downloadUrl);\n if (!response.ok) {\n throw new Error(`Download failed: HTTP ${String(response.status)}`);\n }\n\n const contentLength = parseInt(\n response.headers.get(\"content-length\") ?? \"0\",\n 10,\n );\n if (contentLength > MAX_SHARED_FILE_SIZE) {\n throw new Error(\n `File too large: ${String(contentLength)} bytes exceeds ${String(MAX_SHARED_FILE_SIZE)} limit`,\n );\n }\n\n const buffer = Buffer.from(await response.arrayBuffer());\n if (buffer.length > MAX_SHARED_FILE_SIZE) {\n throw new Error(`Downloaded file exceeds size limit: ${String(buffer.length)} bytes`);\n }\n\n await mkdir(dirname(localPath), { recursive: true });\n await writeFile(localPath, buffer);\n }\n\n async function syncScope(scope: SharedScope): Promise<void> {\n const dir = scopeDir(scope);\n await mkdir(dir, { recursive: true });\n\n try {\n const { files } = await config.client.sharedListFiles({\n scope: scope.scopeType,\n scopeId: scope.scopeId,\n });\n\n for (const file of files) {\n const localPath = join(dir, file.filePath);\n try {\n await downloadFile(scope, file.filePath, localPath);\n log.debug(\n `Shared sync: downloaded ${scope.scopeType}/${scope.scopeId}/${file.filePath}`,\n );\n } catch (err: unknown) {\n log.error(\n `Shared sync: failed to download ${file.filePath}: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n }\n } catch (err: unknown) {\n log.error(\n `Shared sync: failed to list files for ${scope.scopeType}/${scope.scopeId}: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n }\n\n function parseScopedPath(\n filePath: string,\n ): { scope: SharedScope; relativePath: string } | null {\n if (filePath.includes(\"..\")) return null;\n\n const orgMatch = /^shared\\/org\\/(.+)$/.exec(filePath);\n if (orgMatch) {\n const scope = activeScopes.find((s) => s.scopeType === \"org\");\n if (scope) return { scope, relativePath: orgMatch[1] };\n }\n\n const teamMatch = /^shared\\/teams\\/([^/]+)\\/(.+)$/.exec(filePath);\n if (teamMatch) {\n const scope = activeScopes.find(\n (s) => s.scopeType === \"team\" && s.scopeId === teamMatch[1],\n );\n if (scope) return { scope, relativePath: teamMatch[2] };\n }\n\n const projectMatch = /^shared\\/projects\\/([^/]+)\\/(.+)$/.exec(filePath);\n if (projectMatch) {\n const scope = activeScopes.find(\n (s) => s.scopeType === \"project\" && s.scopeId === projectMatch[1],\n );\n if (scope) return { scope, relativePath: projectMatch[2] };\n }\n\n return null;\n }\n\n return {\n async initialize(scopes: SharedScope[]): Promise<void> {\n activeScopes = [...scopes];\n log.info(`Shared sync: initializing with ${String(scopes.length)} scope(s)`);\n await mkdir(sharedDir, { recursive: true });\n for (const scope of scopes) {\n await syncScope(scope);\n }\n log.info(\"Shared sync: initialization complete\");\n },\n\n async updateScopes(newScopes: SharedScope[]): Promise<void> {\n const oldIds = new Set(activeScopes.map((s) => `${s.scopeType}:${s.scopeId}`));\n const newIds = new Set(newScopes.map((s) => `${s.scopeType}:${s.scopeId}`));\n\n for (const scope of activeScopes) {\n const key = `${scope.scopeType}:${scope.scopeId}`;\n if (!newIds.has(key)) {\n const dir = scopeDir(scope);\n try {\n await rm(dir, { recursive: true, force: true });\n log.info(`Shared sync: removed scope directory ${dir}`);\n } catch (err: unknown) {\n log.warn(\n `Shared sync: failed to remove ${dir}: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n }\n }\n\n for (const scope of newScopes) {\n const key = `${scope.scopeType}:${scope.scopeId}`;\n if (!oldIds.has(key)) {\n log.info(\n `Shared sync: new scope ${scope.scopeType}/${scope.scopeId} — syncing files`,\n );\n await syncScope(scope);\n }\n }\n\n activeScopes = [...newScopes];\n },\n\n async handleNotification(\n filePath: string,\n eventType: \"created\" | \"deleted\",\n ): Promise<void> {\n const parsed = parseScopedPath(filePath);\n if (!parsed) {\n log.debug(`Shared sync: ignoring notification for unknown path: ${filePath}`);\n return;\n }\n\n const dir = scopeDir(parsed.scope);\n const localPath = join(dir, parsed.relativePath);\n\n try {\n assertContained(dir, localPath);\n } catch {\n log.warn(`Shared sync: path traversal blocked for ${filePath}`);\n return;\n }\n\n if (eventType === \"deleted\") {\n try {\n await unlink(localPath);\n log.debug(`Shared sync: deleted ${filePath}`);\n } catch {\n // idempotent — file may not exist locally on out-of-order events\n }\n return;\n }\n\n try {\n await downloadFile(parsed.scope, parsed.relativePath, localPath);\n log.debug(`Shared sync: pulled ${filePath}`);\n } catch (err: unknown) {\n log.error(\n `Shared sync: failed to pull ${filePath}: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n },\n\n async fullSync(): Promise<void> {\n log.info(`Shared sync: full sync across ${String(activeScopes.length)} scope(s)`);\n for (const scope of activeScopes) {\n await syncScope(scope);\n }\n },\n\n getScopes(): SharedScope[] {\n return [...activeScopes];\n },\n };\n}\n","// Rolling-window rate brake for cloud delete propagation.\n//\n// The watcher's onChanges callback emits batches every ~2 s. A per-batch\n// brake (`if batch / manifestSize > 30%`) is trivial to slip on a real\n// `rm -rf` because chokidar dribbles deletes out 5-10 at a time. Instead\n// we sum deletes across the last 60 s and trip when the cumulative count\n// exceeds 30% of the manifest. Legitimate single-file deletes via the\n// agent stay nowhere near the threshold; a workspace unmount or\n// catastrophic delete rapidly accumulates and is stopped.\n//\n// Manifest size of 0 means the agent has nothing in the cloud yet —\n// can't be a disaster, so always allow.\n//\n// In-memory only. A plugin restart resets the window, which is acceptable:\n// a restart already implies operator awareness.\n\nconst DEFAULT_WINDOW_MS = 60_000;\nconst DEFAULT_THRESHOLD_RATIO = 0.3;\n\nexport interface DeleteBrakeOptions {\n windowMs?: number;\n thresholdRatio?: number;\n now?: () => number;\n}\n\nexport interface DeleteBrake {\n // Returns true if the proposed delete count is safe to proceed (and\n // records it). Returns false if combining it with the existing rolling\n // window would exceed the threshold — caller must not perform the\n // delete in that case.\n check(deleteCount: number, manifestSize: number): boolean;\n // Current sum of deletes within the active window (after expiry).\n windowSum(): number;\n}\n\nexport function createDeleteBrake(opts: DeleteBrakeOptions = {}): DeleteBrake {\n const windowMs = opts.windowMs ?? DEFAULT_WINDOW_MS;\n const thresholdRatio = opts.thresholdRatio ?? DEFAULT_THRESHOLD_RATIO;\n const now = opts.now ?? (() => Date.now());\n\n const records: { ts: number; count: number }[] = [];\n\n function expire(): void {\n const cutoff = now() - windowMs;\n while (records.length > 0 && records[0].ts < cutoff) records.shift();\n }\n\n function sum(): number {\n return records.reduce((acc, r) => acc + r.count, 0);\n }\n\n return {\n check(deleteCount, manifestSize) {\n if (deleteCount <= 0) return true;\n expire();\n // Fresh agent (no manifest entries) — nothing to lose, always allow.\n if (manifestSize === 0) {\n records.push({ ts: now(), count: deleteCount });\n return true;\n }\n const projected = sum() + deleteCount;\n if (projected / manifestSize > thresholdRatio) return false;\n records.push({ ts: now(), count: deleteCount });\n return true;\n },\n windowSum() {\n expire();\n return sum();\n },\n };\n}\n","/**\n * @alfe.ai/openclaw-sync — OpenClaw Sync plugin.\n *\n * Wraps the sync engine as a lifecycle-managed integration. Same shape as\n * @alfe.ai/openclaw-memory-cloud / -secrets / -google: one AgentApiClient\n * is constructed in `activate()` and reused for every API call. The agent's\n * identity is resolved server-side from the API key — the plugin never\n * touches `/auth/validate`, never plumbs an `agentId` around, never writes\n * a `.alfesync/` directory.\n *\n * Lifecycle:\n * - activate(api): construct client, start sync engine + watcher\n * - deactivate(api): stop watcher, drop relay connections, clean up\n * - configure(api, config): swap schedule/scope at runtime\n *\n * Registers gateway RPC methods: `sync.now`, `sync.status`.\n */\n\nimport { createRequire } from 'node:module';\nimport {\n resolveConfig as resolveAlfeConfig,\n configExists as alfeConfigExists,\n DEFAULT_SOCKET_PATH,\n DEFAULT_WORKSPACE_PATH,\n type ResolvedConfig as AlfeResolvedConfig,\n} from '@alfe.ai/config';\nimport { AgentApiClient, type SyncAgentInfo } from '@alfe.ai/agent-api-client';\n\nimport { existsSync } from 'node:fs';\nimport { join } from 'node:path';\n\nimport { createSyncEngine, type SyncEngine, type SyncResult } from './sync-engine.js';\nimport { startWatcher } from './watcher.js';\nimport { createSharedSyncEngine, type SharedSyncEngine, type SharedScope } from './shared-sync.js';\nimport { createDeleteBrake, type DeleteBrake } from './delete-brake.js';\nimport { readManifest } from './manifest.js';\n\nconst require = createRequire(import.meta.url);\nconst pkg = require('../package.json') as { version: string };\n\n// ── Constants ───────────────────────────────────────────────\nconst SYNC_CAPABILITIES = ['sync.push', 'sync.pull', 'sync.fullSync'] as const;\nconst SYNC_RELAY_RECONNECT_BASE_MS = 1000;\nconst SYNC_RELAY_RECONNECT_MAX_MS = 30000;\nconst SYNC_RELAY_DEBOUNCE_MS = 500;\n\n// ── Types ───────────────────────────────────────────────────\n\ninterface PluginLogger {\n info(msg: string): void;\n warn(msg: string): void;\n error(msg: string): void;\n debug(msg: string): void;\n}\n\ninterface PluginServiceContext {\n config: Record<string, unknown>;\n workspaceDir?: string;\n stateDir: string;\n logger: PluginLogger;\n}\n\ninterface PluginApi {\n logger: PluginLogger;\n registrationMode?: 'full' | 'setup-only' | 'setup-runtime' | 'cli-metadata';\n config?: SyncPluginConfig;\n registerGatewayMethod?: (name: string, handler: () => Promise<unknown>) => void;\n registerService?(service: {\n id: string;\n start: (ctx: PluginServiceContext) => void | Promise<void>;\n stop?: (ctx: PluginServiceContext) => void | Promise<void>;\n }): void;\n}\n\ninterface IpcClient {\n on(event: string, handler: (...args: unknown[]) => void): void;\n request(method: string, params: unknown): Promise<{ ok: boolean; error?: { message: string } }>;\n start(): void;\n stop(): void;\n}\n\ninterface SyncRelaySocket {\n on(event: string, handler: (...args: never[]) => void): void;\n send(data: string): void;\n close(code?: number, reason?: string): void;\n}\n\ninterface RelayMessage {\n type: string;\n status?: string;\n agentId?: string;\n message?: string;\n filePath?: string;\n etag?: string;\n eventType?: string;\n}\n\nexport interface SyncPluginConfig {\n /** Workspace path to sync. Defaults to the resolved Alfe workspace. */\n workspacePath?: string;\n /** Sync scope — which data categories to sync. */\n syncScope?: ('config' | 'conversations' | 'memory')[];\n /** Sync schedule — how often to auto-sync. */\n syncSchedule?: 'realtime' | 'hourly' | 'daily' | 'weekly';\n /** IPC socket path override. */\n socketPath?: string;\n /** Sync relay URL override (default: derived from apiUrl). */\n syncRelayUrl?: string;\n /** Enable shared file sync for org/team/project scopes. Default: true. */\n sharedSync?: boolean;\n}\n\n// ── Plugin State ────────────────────────────────────────────\n\nlet client: AgentApiClient | null = null;\nlet agentId: string | null = null;\nlet syncEngine: SyncEngine | null = null;\nlet sharedSyncEngine: SharedSyncEngine | null = null;\nlet stopWatcher: (() => Promise<void>) | null = null;\nlet deleteBrake: DeleteBrake | null = null;\nlet daemonIpcClient: IpcClient | null = null;\nlet scheduledInterval: ReturnType<typeof setInterval> | null = null;\nlet currentConfig: SyncPluginConfig = {};\nlet lastSyncResult: SyncResult | null = null;\nlet syncRelayWs: SyncRelaySocket | null = null;\nlet syncRelayReconnectTimer: ReturnType<typeof setTimeout> | null = null;\nlet syncRelayReconnectAttempt = 0;\nlet syncRelayDebounceTimer: ReturnType<typeof setTimeout> | null = null;\nconst syncRelayPendingPaths = new Map<\n string,\n { etag?: string; eventType: 'created' | 'deleted' }\n>();\n\n// ── Schedule Helpers ────────────────────────────────────────\n\nconst SCHEDULE_INTERVALS_MS: Record<string, number> = {\n hourly: 60 * 60 * 1000,\n daily: 24 * 60 * 60 * 1000,\n weekly: 7 * 24 * 60 * 60 * 1000,\n};\n\nfunction clearSchedule() {\n if (scheduledInterval) {\n clearInterval(scheduledInterval);\n scheduledInterval = null;\n }\n}\n\nfunction setupSchedule(schedule: string, log: PluginLogger) {\n clearSchedule();\n if (schedule === 'realtime') {\n log.info('Sync schedule: realtime (file watcher active)');\n return;\n }\n\n const intervalMs = SCHEDULE_INTERVALS_MS[schedule];\n if (!intervalMs) {\n log.warn(`Unknown sync schedule: ${schedule}, defaulting to hourly`);\n setupSchedule('hourly', log);\n return;\n }\n\n log.info(`Sync schedule: ${schedule} (every ${String(intervalMs / 1000)}s)`);\n scheduledInterval = setInterval(() => {\n if (!syncEngine) return;\n const engine = syncEngine;\n void (async () => {\n try {\n log.info(`Scheduled sync (${schedule}) starting...`);\n lastSyncResult = await engine.fullSync({ quiet: true });\n log.info(\n `Scheduled sync complete: ${String(lastSyncResult.pushed)} pushed, ${String(lastSyncResult.pulled)} pulled`,\n );\n } catch (err: unknown) {\n log.error(`Scheduled sync failed: ${err instanceof Error ? err.message : String(err)}`);\n }\n })();\n }, intervalMs);\n}\n\n// ── Daemon IPC ──────────────────────────────────────────────\n\nasync function connectToDaemon(socketPath: string, log: PluginLogger): Promise<IpcClient | null> {\n try {\n const modPath = '@alfe.ai/openclaw';\n const mod = (await import(/* webpackIgnore: true */ modPath)) as {\n IPCClient: new (socketPath: string, log: PluginLogger) => IpcClient;\n };\n const ipc = new mod.IPCClient(socketPath, log);\n\n ipc.on('connected', () => {\n void (async () => {\n log.info('Connected to Alfe daemon — registering sync capabilities...');\n const response = await ipc.request('capability.register', {\n plugin: '@alfe.ai/openclaw-sync',\n capabilities: [...SYNC_CAPABILITIES],\n });\n if (response.ok) {\n log.info('Sync capabilities registered with daemon');\n } else {\n log.warn(`Failed to register sync capabilities: ${response.error?.message ?? 'unknown'}`);\n }\n })();\n });\n\n ipc.on('disconnected', (...args: unknown[]) => {\n const reason = typeof args[0] === 'string' ? args[0] : String(args[0]);\n log.warn(`Disconnected from Alfe daemon: ${reason}`);\n });\n\n ipc.on('message', (...args: unknown[]) => {\n const msg = args[0] as Record<string, unknown> | undefined;\n if (msg?.type === 'SYNC_NOW' || msg?.command === 'SYNC_NOW') {\n log.info('Received SYNC_NOW command — triggering immediate sync...');\n if (syncEngine) {\n const engine = syncEngine;\n void (async () => {\n try {\n lastSyncResult = await engine.fullSync({ quiet: true });\n log.info(\n `SYNC_NOW complete: ${String(lastSyncResult.pushed)} pushed, ${String(lastSyncResult.pulled)} pulled`,\n );\n } catch (err: unknown) {\n log.error(`SYNC_NOW failed: ${err instanceof Error ? err.message : String(err)}`);\n }\n })();\n }\n }\n\n if (msg?.type === 'SHARED_SCOPES') {\n const scopes = msg.scopes as SharedScope[] | undefined;\n const engine = sharedSyncEngine;\n if (scopes && engine) {\n log.info(`Received SHARED_SCOPES update: ${String(scopes.length)} scope(s)`);\n void (async () => {\n try {\n await engine.updateScopes(scopes);\n } catch (err: unknown) {\n log.error(\n `SHARED_SCOPES update failed: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n })();\n }\n }\n });\n\n ipc.on('error', (...args: unknown[]) => {\n const err = args[0];\n log.debug(`Daemon IPC error: ${err instanceof Error ? err.message : String(err)}`);\n });\n\n ipc.start();\n return ipc;\n } catch {\n log.info('Alfe daemon not available — Sync plugin running standalone');\n return null;\n }\n}\n\n// ── Sync Relay Connection ───────────────────────────────────\n\nfunction clearSyncRelayReconnect() {\n if (syncRelayReconnectTimer) {\n clearTimeout(syncRelayReconnectTimer);\n syncRelayReconnectTimer = null;\n }\n}\n\nfunction clearSyncRelayDebounce() {\n if (syncRelayDebounceTimer) {\n clearTimeout(syncRelayDebounceTimer);\n syncRelayDebounceTimer = null;\n }\n}\n\nasync function processPendingNotifications(log: PluginLogger) {\n if (syncRelayPendingPaths.size === 0) return;\n\n const entries = new Map(syncRelayPendingPaths);\n syncRelayPendingPaths.clear();\n\n const sharedEntries = new Map<string, { etag?: string; eventType: 'created' | 'deleted' }>();\n const privateEntries = new Map<string, { etag?: string; eventType: 'created' | 'deleted' }>();\n\n for (const [filePath, info] of entries) {\n if (filePath.startsWith('shared/')) {\n sharedEntries.set(filePath, info);\n } else {\n privateEntries.set(filePath, info);\n }\n }\n\n if (sharedEntries.size > 0 && sharedSyncEngine) {\n for (const [filePath, info] of sharedEntries) {\n try {\n await sharedSyncEngine.handleNotification(filePath, info.eventType);\n } catch (err: unknown) {\n log.error(\n `Shared sync relay: failed ${info.eventType} ${filePath}: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n }\n }\n\n if (privateEntries.size > 0 && syncEngine) {\n const engine = syncEngine;\n const toPull: string[] = [];\n const toDelete: string[] = [];\n\n for (const [filePath, info] of privateEntries) {\n if (info.eventType === 'deleted') {\n toDelete.push(filePath);\n } else {\n toPull.push(filePath);\n }\n }\n\n for (const filePath of toDelete) {\n try {\n await engine.removeLocalFile(filePath, { quiet: true });\n log.debug(`Sync relay: deleted ${filePath}`);\n } catch (err: unknown) {\n log.error(\n `Sync relay: failed to delete ${filePath}: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n }\n\n if (toPull.length > 0) {\n try {\n const result = await engine.pullFiles(toPull, { quiet: true });\n if (result.pulled > 0) log.info(`Sync relay: pulled ${String(result.pulled)} file(s)`);\n if (result.errors > 0) log.warn(`Sync relay: ${String(result.errors)} pull error(s)`);\n } catch (err: unknown) {\n log.error(`Sync relay: pull failed: ${err instanceof Error ? err.message : String(err)}`);\n }\n }\n }\n}\n\nasync function connectToSyncRelay(\n relayUrl: string,\n token: string,\n agentIdForSubscribe: string,\n log: PluginLogger,\n): Promise<SyncRelaySocket | null> {\n try {\n const { default: WebSocket } = await import('ws');\n\n const wsUrl = `${relayUrl}?token=${encodeURIComponent(token)}`;\n const ws = new WebSocket(wsUrl);\n\n ws.on('open', () => {\n log.info('Connected to Sync Relay');\n syncRelayReconnectAttempt = 0;\n ws.send(JSON.stringify({ type: 'SUBSCRIBE', agentId: agentIdForSubscribe }));\n });\n\n ws.on('message', (data: Buffer | string) => {\n let message: RelayMessage;\n try {\n message = JSON.parse(data.toString()) as RelayMessage;\n } catch {\n return;\n }\n\n switch (message.type) {\n case 'SUBSCRIBE_ACK':\n if (message.status === 'ok') {\n log.info(`Subscribed to sync notifications for agent ${message.agentId ?? agentIdForSubscribe}`);\n const sharedEngine = sharedSyncEngine;\n if (sharedEngine) {\n void (async () => {\n try {\n await sharedEngine.fullSync();\n log.info('Shared sync: reconnect full sync complete');\n } catch (err: unknown) {\n log.error(\n `Shared sync: reconnect full sync failed: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n })();\n }\n } else {\n log.warn(`Sync relay subscribe failed: ${message.message ?? 'unknown'}`);\n }\n break;\n\n case 'FILE_CHANGED':\n if (message.filePath) {\n syncRelayPendingPaths.set(message.filePath, {\n etag: message.etag,\n eventType: message.eventType === 'deleted' ? 'deleted' : 'created',\n });\n }\n clearSyncRelayDebounce();\n syncRelayDebounceTimer = setTimeout(\n () => void processPendingNotifications(log),\n SYNC_RELAY_DEBOUNCE_MS,\n );\n break;\n\n case 'PING':\n try {\n ws.send(JSON.stringify({ type: 'PONG' }));\n } catch {\n // ignore\n }\n break;\n }\n });\n\n ws.on('close', (code: number) => {\n log.info(`Sync Relay disconnected (code=${String(code)})`);\n syncRelayWs = null;\n scheduleSyncRelayReconnect(relayUrl, token, agentIdForSubscribe, log);\n });\n\n ws.on('error', (err: Error) => {\n log.debug(`Sync Relay error: ${err.message}`);\n });\n\n return ws as SyncRelaySocket;\n } catch (err: unknown) {\n log.debug(`Failed to connect to Sync Relay: ${err instanceof Error ? err.message : String(err)}`);\n scheduleSyncRelayReconnect(relayUrl, token, agentIdForSubscribe, log);\n return null;\n }\n}\n\nfunction scheduleSyncRelayReconnect(\n relayUrl: string,\n token: string,\n agentIdForSubscribe: string,\n log: PluginLogger,\n) {\n clearSyncRelayReconnect();\n const delay = Math.min(\n SYNC_RELAY_RECONNECT_BASE_MS * Math.pow(2, syncRelayReconnectAttempt),\n SYNC_RELAY_RECONNECT_MAX_MS,\n );\n syncRelayReconnectAttempt++;\n log.debug(\n `Reconnecting to Sync Relay in ${String(delay)}ms (attempt ${String(syncRelayReconnectAttempt)})`,\n );\n\n syncRelayReconnectTimer = setTimeout(() => {\n void (async () => {\n syncRelayWs = await connectToSyncRelay(relayUrl, token, agentIdForSubscribe, log);\n })();\n }, delay);\n}\n\nfunction disconnectSyncRelay() {\n clearSyncRelayReconnect();\n clearSyncRelayDebounce();\n syncRelayPendingPaths.clear();\n\n if (syncRelayWs) {\n try {\n syncRelayWs.send(JSON.stringify({ type: 'UNSUBSCRIBE' }));\n syncRelayWs.close(1000, 'Plugin deactivating');\n } catch {\n // ignore\n }\n syncRelayWs = null;\n }\n}\n\nfunction deriveRelayUrl(apiUrl: string): string {\n if (apiUrl.includes('dev.alfe.ai')) return 'wss://sync.dev.alfe.ai/ws';\n if (apiUrl.includes('demo.alfe.ai')) return 'wss://sync.demo.alfe.ai/ws';\n if (apiUrl.includes('test.alfe.ai')) return 'wss://sync.test.alfe.ai/ws';\n return 'wss://sync.alfe.ai/ws';\n}\n\n// ── Plugin Definition ───────────────────────────────────────\n\nconst plugin = {\n id: '@alfe.ai/openclaw-sync',\n name: 'Alfe Sync Plugin',\n description:\n 'Back up agent configuration, conversations, and memory to the cloud with scheduled or real-time sync.',\n version: pkg.version,\n\n activate(api: PluginApi) {\n const log = api.logger;\n\n const pluginConfig: SyncPluginConfig = api.config ?? {};\n currentConfig = pluginConfig;\n\n let alfeConfig: { workspacePath: string; socketPath: string } | null = null;\n try {\n alfeConfig = resolveAlfeConfig();\n } catch {\n // alfe login hasn't run; logged below\n }\n\n const workspacePath =\n pluginConfig.workspacePath ?? alfeConfig?.workspacePath ?? DEFAULT_WORKSPACE_PATH;\n\n const syncScope = pluginConfig.syncScope ?? ['config', 'conversations', 'memory'];\n const syncSchedule = pluginConfig.syncSchedule ?? 'daily';\n const socketPath =\n pluginConfig.socketPath ?? alfeConfig?.socketPath ?? DEFAULT_SOCKET_PATH;\n\n const startSyncService = async () => {\n if ((globalThis as Record<string, unknown>).__alfeSyncPluginActivated === true) {\n log.debug('Alfe Sync plugin already activated — skipping duplicate');\n return;\n }\n (globalThis as Record<string, unknown>).__alfeSyncPluginActivated = true;\n log.info('Alfe Sync plugin activating...');\n log.info(`Sync scope: ${syncScope.join(', ')}`);\n log.info(`Sync schedule: ${syncSchedule}`);\n log.info(`Workspace: ${workspacePath}`);\n\n // Sync needs `~/.alfe/config.toml` (the existing `alfe login` config).\n // Identity (agentId / tenantId) is resolved server-side from the API\n // key on every AgentApiClient call.\n if (!alfeConfigExists()) {\n log.info('Sync skipped — no Alfe config found. Run `alfe login` to enable.');\n return;\n }\n\n let syncCfg: AlfeResolvedConfig;\n try {\n syncCfg = resolveAlfeConfig();\n } catch (err: unknown) {\n log.warn(\n `Sync skipped — failed to resolve credentials from ~/.alfe/config.toml: ${err instanceof Error ? err.message : String(err)}`,\n );\n return;\n }\n\n client = new AgentApiClient({ apiKey: syncCfg.apiKey, apiUrl: syncCfg.apiUrl });\n syncEngine = createSyncEngine({ workspacePath, client });\n log.info('Sync engine initialized');\n\n // Realtime mode → initial push-only seed of pre-existing files\n // (chokidar ignoreInitial means the watcher only emits for\n // post-activation changes; without this the workspace tree never\n // reaches the cloud unless a file is touched). Push-only — not\n // fullSync — so we don't drag down stale cloud files into a fresh\n // local workspace or write .conflict-<ts> markers on first run.\n if (syncSchedule === 'realtime') {\n try {\n lastSyncResult = await syncEngine.push(undefined, { quiet: true });\n log.info('Initial workspace push complete');\n } catch (err: unknown) {\n log.warn(`Initial workspace push failed: ${err instanceof Error ? err.message : String(err)}`);\n }\n deleteBrake = createDeleteBrake();\n try {\n stopWatcher = await startWatcher({\n workspacePath,\n debounceMs: 2000,\n onChanges: async (paths) => {\n if (!syncEngine) return;\n const existing: string[] = [];\n const missing: string[] = [];\n for (const p of paths) {\n if (existsSync(join(workspacePath, p))) existing.push(p);\n else missing.push(p);\n }\n log.debug(\n `Realtime sync: ${String(existing.length)} upload(s), ${String(missing.length)} delete(s)`,\n );\n if (existing.length > 0) {\n try {\n lastSyncResult = await syncEngine.push(existing, { quiet: true });\n } catch (err: unknown) {\n log.error(`Realtime push failed: ${err instanceof Error ? err.message : String(err)}`);\n }\n }\n if (missing.length > 0) {\n // Rolling-window rate brake: refuse the batch if cumulative\n // deletes in the last 60 s would exceed 30% of the manifest.\n // See packages/openclaw-sync/src/delete-brake.ts.\n const manifest = await readManifest(workspacePath);\n const manifestSize = Object.keys(manifest.files).length;\n if (deleteBrake && !deleteBrake.check(missing.length, manifestSize)) {\n log.error(\n `Sync delete brake tripped: ${String(deleteBrake.windowSum() + missing.length)} ` +\n `deletes in last 60s vs manifest size ${String(manifestSize)} ` +\n `(threshold 30%). Refusing batch — investigate the workspace state.`,\n );\n } else {\n try {\n lastSyncResult = await syncEngine.pushDeletes(missing, { quiet: true });\n } catch (err: unknown) {\n log.error(`Realtime delete failed: ${err instanceof Error ? err.message : String(err)}`);\n }\n }\n }\n },\n });\n log.info('File watcher started for realtime sync');\n } catch (err: unknown) {\n log.warn(`Failed to start file watcher: ${err instanceof Error ? err.message : String(err)}`);\n }\n } else {\n setupSchedule(syncSchedule, log);\n }\n\n // Daemon IPC (for SYNC_NOW + SHARED_SCOPES messages)\n daemonIpcClient = await connectToDaemon(socketPath, log);\n\n // Register with the sync service to materialize the SyncAgent record\n // and learn our own agentId for relay subscription.\n let registered: SyncAgentInfo | null = null;\n try {\n const result = await client.syncRegister();\n registered = result.agent;\n agentId = registered.agentId;\n } catch (err: unknown) {\n log.warn(`Sync register failed: ${err instanceof Error ? err.message : String(err)}`);\n }\n\n // Sync relay (real-time file change notifications)\n if (registered) {\n try {\n const relayUrl = pluginConfig.syncRelayUrl ?? deriveRelayUrl(syncCfg.apiUrl);\n syncRelayWs = await connectToSyncRelay(\n relayUrl,\n syncCfg.apiKey,\n registered.agentId,\n log,\n );\n } catch (err: unknown) {\n log.debug(\n `Sync Relay connection skipped: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n // Shared sync (org/team/project scopes pushed by gateway)\n if (pluginConfig.sharedSync !== false) {\n try {\n sharedSyncEngine = createSharedSyncEngine({ workspacePath, client }, log);\n log.info('Shared sync engine created — waiting for SHARED_SCOPES from gateway');\n } catch (err: unknown) {\n log.debug(\n `Shared sync engine skipped: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n }\n }\n };\n\n const stopSyncService = async () => {\n (globalThis as Record<string, unknown>).__alfeSyncPluginActivated = false;\n clearSchedule();\n disconnectSyncRelay();\n\n if (stopWatcher) {\n try {\n await stopWatcher();\n log.info('File watcher stopped');\n } catch (err: unknown) {\n log.debug(`Error stopping watcher: ${err instanceof Error ? err.message : String(err)}`);\n }\n stopWatcher = null;\n }\n deleteBrake = null;\n\n if (daemonIpcClient) {\n try {\n daemonIpcClient.stop();\n log.info('Disconnected from Alfe daemon');\n } catch (err: unknown) {\n log.debug(`Error disconnecting from daemon: ${err instanceof Error ? err.message : String(err)}`);\n }\n daemonIpcClient = null;\n }\n\n client = null;\n agentId = null;\n syncEngine = null;\n sharedSyncEngine = null;\n lastSyncResult = null;\n currentConfig = {};\n\n log.info('Alfe Sync plugin deactivated');\n };\n\n if (typeof api.registerGatewayMethod === 'function') {\n api.registerGatewayMethod('sync.now', async () => {\n if (!syncEngine) {\n return { ok: false, error: 'Sync engine not initialized — run `alfe login`' };\n }\n try {\n lastSyncResult = await syncEngine.fullSync({ quiet: true });\n return { ok: true, result: lastSyncResult };\n } catch (err: unknown) {\n return { ok: false, error: err instanceof Error ? err.message : String(err) };\n }\n });\n log.info('Registered gateway RPC method: sync.now');\n\n api.registerGatewayMethod('sync.status', () =>\n Promise.resolve({\n ok: true,\n initialized: !!syncEngine,\n agentId,\n schedule: currentConfig.syncSchedule ?? 'daily',\n scope: currentConfig.syncScope ?? ['config', 'conversations', 'memory'],\n lastResult: lastSyncResult,\n watcherActive: !!stopWatcher,\n }),\n );\n log.info('Registered gateway RPC method: sync.status');\n }\n\n if (api.registerService) {\n api.registerService({\n id: 'alfe-sync-engine',\n start: () => startSyncService(),\n stop: () => stopSyncService(),\n });\n } else {\n void startSyncService().catch((err: unknown) => {\n log.error(`Sync plugin async init failed: ${err instanceof Error ? err.message : String(err)}`);\n });\n }\n\n log.info('Alfe Sync plugin activated');\n },\n\n async deactivate(api: PluginApi) {\n (globalThis as Record<string, unknown>).__alfeSyncPluginActivated = false;\n const log = api.logger;\n log.info('Alfe Sync plugin deactivating...');\n\n clearSchedule();\n disconnectSyncRelay();\n\n if (stopWatcher) {\n try {\n await stopWatcher();\n log.info('File watcher stopped');\n } catch (err: unknown) {\n log.debug(`Error stopping watcher: ${err instanceof Error ? err.message : String(err)}`);\n }\n stopWatcher = null;\n }\n\n if (daemonIpcClient) {\n try {\n daemonIpcClient.stop();\n log.info('Disconnected from Alfe daemon');\n } catch (err: unknown) {\n log.debug(`Error disconnecting from daemon: ${err instanceof Error ? err.message : String(err)}`);\n }\n daemonIpcClient = null;\n }\n\n client = null;\n agentId = null;\n syncEngine = null;\n sharedSyncEngine = null;\n lastSyncResult = null;\n currentConfig = {};\n\n log.info('Alfe Sync plugin deactivated');\n },\n\n async configure(api: PluginApi, config: SyncPluginConfig) {\n const log = api.logger;\n log.info('Reconfiguring Alfe Sync plugin...');\n currentConfig = { ...currentConfig, ...config };\n\n if (config.syncSchedule) {\n if (config.syncSchedule === 'realtime' && syncEngine && !stopWatcher) {\n const workspacePath = currentConfig.workspacePath ?? syncEngine.workspacePath;\n stopWatcher = await startWatcher({\n workspacePath,\n debounceMs: 2000,\n onChanges: async (paths) => {\n if (!syncEngine) return;\n try {\n lastSyncResult = await syncEngine.push(paths, { quiet: true });\n } catch (err: unknown) {\n log.error(`Realtime push failed: ${err instanceof Error ? err.message : String(err)}`);\n }\n },\n });\n clearSchedule();\n log.info('Switched to realtime sync');\n } else if (config.syncSchedule !== 'realtime') {\n if (stopWatcher) {\n await stopWatcher();\n stopWatcher = null;\n }\n setupSchedule(config.syncSchedule, log);\n }\n }\n\n if (config.syncScope) {\n log.info(`Updated sync scope: ${config.syncScope.join(', ')}`);\n }\n\n log.info('Alfe Sync plugin reconfigured');\n },\n};\n\nexport default plugin;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAwBA,eAAsB,aACpB,SAC8B;CAC9B,MAAM,EAAE,eAAe,aAAa,KAAM,cAAc;CACxD,MAAM,iBAAiB,MAAM,mBAAmB,cAAc;CAG9D,MAAM,0BAAU,IAAI,KAA4C;CAEhE,IAAI,6BAAa,IAAI,KAAa;CAClC,IAAI,aAAmD;CAEvD,SAAS,gBAAgB;AACvB,MAAI,WAAY;AAChB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I,iBAAiB;AACnB,OAAI;AACF,oBAAgB,MAAM;AACtB,QAAI,KAAK,gCAAgC;YAClC,KAAc;AACrB,QAAI,MAAM,oCAAoC,eAAe,QAAQ,IAAI,UAAU,OAAO,IAAI,GAAG;;AAEnG,qBAAkB;;AAGpB,WAAS;AACT,YAAU;AACV,eAAa;AACb,qBAAmB;AACnB,mBAAiB;AACjB,kBAAgB,EAAE;AAElB,MAAI,KAAK,+BAA+B;;CAG1C,MAAM,UAAU,KAAgB,QAA0B;EACxD,MAAM,MAAM,IAAI;AAChB,MAAI,KAAK,oCAAoC;AAC7C,kBAAgB;GAAE,GAAG;GAAe,GAAG;GAAQ;AAE/C,MAAI,OAAO;OACL,OAAO,iBAAiB,cAAc,cAAc,CAAC,aAAa;AAEpE,kBAAc,MAAM,aAAa;KAC/B,eAFoB,cAAc,iBAAiB,WAAW;KAG9D,YAAY;KACZ,WAAW,OAAO,UAAU;AAC1B,UAAI,CAAC,WAAY;AACjB,UAAI;AACF,wBAAiB,MAAM,WAAW,KAAK,OAAO,EAAE,OAAO,MAAM,CAAC;eACvD,KAAc;AACrB,WAAI,MAAM,yBAAyB,eAAe,QAAQ,IAAI,UAAU,OAAO,IAAI,GAAG;;;KAG3F,CAAC;AACF,mBAAe;AACf,QAAI,KAAK,4BAA4B;cAC5B,OAAO,iBAAiB,YAAY;AAC7C,QAAI,aAAa;AACf,WAAM,aAAa;AACnB,mBAAc;;AAEhB,kBAAc,OAAO,cAAc,IAAI;;;AAI3C,MAAI,OAAO,UACT,KAAI,KAAK,uBAAuB,OAAO,UAAU,KAAK,KAAK,GAAG;AAGhE,MAAI,KAAK,gCAAgC;;CAE5C"}
1
+ {"version":3,"file":"plugin2.js","names":["resolveAlfeConfig","alfeConfigExists"],"sources":["../src/watcher.ts","../src/shared-sync.ts","../src/delete-brake.ts","../src/plugin.ts"],"sourcesContent":["/**\n * AlfeSync watcher — recursive file watcher with debounce and ignore support.\n *\n * Uses chokidar to watch the workspace root, debounces per-file changes\n * by 2 seconds, and emits batches of changed paths.\n */\n\nimport { watch } from \"chokidar\";\nimport { relative } from \"node:path\";\nimport { loadIgnorePatterns, shouldIgnore } from \"./ignore.js\";\n\nexport interface WatcherOptions {\n workspacePath: string;\n /** Debounce delay per file in milliseconds. Default: 2000 */\n debounceMs?: number;\n /** Callback invoked with batches of changed relative paths */\n onChanges: (paths: string[]) => void | Promise<void>;\n}\n\n/**\n * Start watching a workspace for file changes.\n *\n * Returns a cleanup function to stop watching.\n */\nexport async function startWatcher(\n options: WatcherOptions,\n): Promise<() => Promise<void>> {\n const { workspacePath, debounceMs = 2000, onChanges } = options;\n const ignorePatterns = await loadIgnorePatterns(workspacePath);\n\n // Pending changes map: relativePath → timeout handle\n const pending = new Map<string, ReturnType<typeof setTimeout>>();\n // Batch accumulator for flushing\n let batchPaths = new Set<string>();\n let flushTimer: ReturnType<typeof setTimeout> | null = null;\n\n function scheduleBatch() {\n if (flushTimer) return;\n flushTimer = setTimeout(() => {\n flushTimer = null;\n if (batchPaths.size === 0) return;\n\n const paths = [...batchPaths];\n batchPaths = new Set();\n void onChanges(paths);\n }, debounceMs);\n }\n\n function handleChange(absolutePath: string) {\n const relativePath = relative(workspacePath, absolutePath);\n\n // Skip ignored files\n if (shouldIgnore(relativePath, ignorePatterns)) return;\n\n // Clear existing timer for this file\n const existingTimer = pending.get(relativePath);\n if (existingTimer) clearTimeout(existingTimer);\n\n // Debounce: wait before adding to batch\n const timer = setTimeout(() => {\n pending.delete(relativePath);\n batchPaths.add(relativePath);\n scheduleBatch();\n }, debounceMs);\n\n pending.set(relativePath, timer);\n }\n\n const watcher = watch(workspacePath, {\n persistent: true,\n ignoreInitial: true,\n followSymlinks: false,\n depth: undefined, // unlimited depth\n ignored: [\n \"**/node_modules/**\",\n \"**/.alfesync/**\",\n \"**/.git/**\",\n \"**/.sst/**\",\n ],\n });\n\n watcher.on(\"add\", handleChange);\n watcher.on(\"change\", handleChange);\n watcher.on(\"unlink\", handleChange);\n\n // Return cleanup function\n return async () => {\n // Clear all pending timers\n for (const timer of pending.values()) {\n clearTimeout(timer);\n }\n pending.clear();\n\n if (flushTimer) {\n clearTimeout(flushTimer);\n flushTimer = null;\n }\n\n await watcher.close();\n };\n}\n","/**\n * Shared file sync — mirrors org/team/project files to a `shared/`\n * directory in the agent's workspace, organised by scope:\n *\n * shared/org/<files…>\n * shared/teams/<scopeId>/<files…>\n * shared/projects/<scopeId>/<files…>\n *\n * Backed by the agent self-service API (`AgentApiClient.sharedListFiles`,\n * `AgentApiClient.sharedDownloadUrl`).\n */\n\nimport { join, dirname, normalize, sep } from \"node:path\";\nimport { mkdir, writeFile, unlink, rm } from \"node:fs/promises\";\nimport type { AgentApiClient } from \"@alfe.ai/agent-api-client\";\n\nconst MAX_SHARED_FILE_SIZE = 100 * 1024 * 1024; // 100 MB\n\n/** Throw if `resolvedPath` would escape `baseDir`. */\nfunction assertContained(baseDir: string, resolvedPath: string): void {\n const normalizedBase = normalize(baseDir) + sep;\n const normalizedPath = normalize(resolvedPath);\n if (\n !normalizedPath.startsWith(normalizedBase) &&\n normalizedPath !== normalize(baseDir)\n ) {\n throw new Error(`Path traversal blocked: ${resolvedPath} escapes ${baseDir}`);\n }\n}\n\nexport interface SharedScope {\n scopeType: \"team\" | \"project\" | \"org\";\n scopeId: string;\n name: string;\n}\n\nexport interface SharedSyncConfig {\n workspacePath: string;\n client: AgentApiClient;\n}\n\ninterface PluginLogger {\n info(msg: string): void;\n warn(msg: string): void;\n error(msg: string): void;\n debug(msg: string): void;\n}\n\nexport interface SharedSyncEngine {\n initialize(scopes: SharedScope[]): Promise<void>;\n updateScopes(scopes: SharedScope[]): Promise<void>;\n handleNotification(filePath: string, eventType: \"created\" | \"deleted\"): Promise<void>;\n fullSync(): Promise<void>;\n getScopes(): SharedScope[];\n}\n\nexport function createSharedSyncEngine(\n config: SharedSyncConfig,\n log: PluginLogger,\n): SharedSyncEngine {\n let activeScopes: SharedScope[] = [];\n const sharedDir = join(config.workspacePath, \"shared\");\n\n function scopeDir(scope: SharedScope): string {\n if (scope.scopeType === \"org\") return join(sharedDir, \"org\");\n const plural = scope.scopeType === \"team\" ? \"teams\" : \"projects\";\n return join(sharedDir, plural, scope.scopeId);\n }\n\n async function downloadFile(\n scope: SharedScope,\n filePath: string,\n localPath: string,\n ): Promise<void> {\n assertContained(scopeDir(scope), localPath);\n\n const { downloadUrl } = await config.client.sharedDownloadUrl({\n scope: scope.scopeType,\n scopeId: scope.scopeId,\n filePath,\n });\n\n // Presigned URL → S3 directly (raw fetch is the right tool here).\n const response = await fetch(downloadUrl);\n if (!response.ok) {\n throw new Error(`Download failed: HTTP ${String(response.status)}`);\n }\n\n const contentLength = parseInt(\n response.headers.get(\"content-length\") ?? \"0\",\n 10,\n );\n if (contentLength > MAX_SHARED_FILE_SIZE) {\n throw new Error(\n `File too large: ${String(contentLength)} bytes exceeds ${String(MAX_SHARED_FILE_SIZE)} limit`,\n );\n }\n\n const buffer = Buffer.from(await response.arrayBuffer());\n if (buffer.length > MAX_SHARED_FILE_SIZE) {\n throw new Error(`Downloaded file exceeds size limit: ${String(buffer.length)} bytes`);\n }\n\n await mkdir(dirname(localPath), { recursive: true });\n await writeFile(localPath, buffer);\n }\n\n async function syncScope(scope: SharedScope): Promise<void> {\n const dir = scopeDir(scope);\n await mkdir(dir, { recursive: true });\n\n try {\n const { files } = await config.client.sharedListFiles({\n scope: scope.scopeType,\n scopeId: scope.scopeId,\n });\n\n for (const file of files) {\n const localPath = join(dir, file.filePath);\n try {\n await downloadFile(scope, file.filePath, localPath);\n log.debug(\n `Shared sync: downloaded ${scope.scopeType}/${scope.scopeId}/${file.filePath}`,\n );\n } catch (err: unknown) {\n log.error(\n `Shared sync: failed to download ${file.filePath}: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n }\n } catch (err: unknown) {\n log.error(\n `Shared sync: failed to list files for ${scope.scopeType}/${scope.scopeId}: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n }\n\n function parseScopedPath(\n filePath: string,\n ): { scope: SharedScope; relativePath: string } | null {\n if (filePath.includes(\"..\")) return null;\n\n const orgMatch = /^shared\\/org\\/(.+)$/.exec(filePath);\n if (orgMatch) {\n const scope = activeScopes.find((s) => s.scopeType === \"org\");\n if (scope) return { scope, relativePath: orgMatch[1] };\n }\n\n const teamMatch = /^shared\\/teams\\/([^/]+)\\/(.+)$/.exec(filePath);\n if (teamMatch) {\n const scope = activeScopes.find(\n (s) => s.scopeType === \"team\" && s.scopeId === teamMatch[1],\n );\n if (scope) return { scope, relativePath: teamMatch[2] };\n }\n\n const projectMatch = /^shared\\/projects\\/([^/]+)\\/(.+)$/.exec(filePath);\n if (projectMatch) {\n const scope = activeScopes.find(\n (s) => s.scopeType === \"project\" && s.scopeId === projectMatch[1],\n );\n if (scope) return { scope, relativePath: projectMatch[2] };\n }\n\n return null;\n }\n\n return {\n async initialize(scopes: SharedScope[]): Promise<void> {\n activeScopes = [...scopes];\n log.info(`Shared sync: initializing with ${String(scopes.length)} scope(s)`);\n await mkdir(sharedDir, { recursive: true });\n for (const scope of scopes) {\n await syncScope(scope);\n }\n log.info(\"Shared sync: initialization complete\");\n },\n\n async updateScopes(newScopes: SharedScope[]): Promise<void> {\n const oldIds = new Set(activeScopes.map((s) => `${s.scopeType}:${s.scopeId}`));\n const newIds = new Set(newScopes.map((s) => `${s.scopeType}:${s.scopeId}`));\n\n for (const scope of activeScopes) {\n const key = `${scope.scopeType}:${scope.scopeId}`;\n if (!newIds.has(key)) {\n const dir = scopeDir(scope);\n try {\n await rm(dir, { recursive: true, force: true });\n log.info(`Shared sync: removed scope directory ${dir}`);\n } catch (err: unknown) {\n log.warn(\n `Shared sync: failed to remove ${dir}: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n }\n }\n\n for (const scope of newScopes) {\n const key = `${scope.scopeType}:${scope.scopeId}`;\n if (!oldIds.has(key)) {\n log.info(\n `Shared sync: new scope ${scope.scopeType}/${scope.scopeId} — syncing files`,\n );\n await syncScope(scope);\n }\n }\n\n activeScopes = [...newScopes];\n },\n\n async handleNotification(\n filePath: string,\n eventType: \"created\" | \"deleted\",\n ): Promise<void> {\n const parsed = parseScopedPath(filePath);\n if (!parsed) {\n log.debug(`Shared sync: ignoring notification for unknown path: ${filePath}`);\n return;\n }\n\n const dir = scopeDir(parsed.scope);\n const localPath = join(dir, parsed.relativePath);\n\n try {\n assertContained(dir, localPath);\n } catch {\n log.warn(`Shared sync: path traversal blocked for ${filePath}`);\n return;\n }\n\n if (eventType === \"deleted\") {\n try {\n await unlink(localPath);\n log.debug(`Shared sync: deleted ${filePath}`);\n } catch {\n // idempotent — file may not exist locally on out-of-order events\n }\n return;\n }\n\n try {\n await downloadFile(parsed.scope, parsed.relativePath, localPath);\n log.debug(`Shared sync: pulled ${filePath}`);\n } catch (err: unknown) {\n log.error(\n `Shared sync: failed to pull ${filePath}: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n },\n\n async fullSync(): Promise<void> {\n log.info(`Shared sync: full sync across ${String(activeScopes.length)} scope(s)`);\n for (const scope of activeScopes) {\n await syncScope(scope);\n }\n },\n\n getScopes(): SharedScope[] {\n return [...activeScopes];\n },\n };\n}\n","// Rolling-window rate brake for cloud delete propagation.\n//\n// The watcher's onChanges callback emits batches every ~2 s. A per-batch\n// brake (`if batch / manifestSize > 30%`) is trivial to slip on a real\n// `rm -rf` because chokidar dribbles deletes out 5-10 at a time. Instead\n// we sum deletes across the last 60 s and trip when the cumulative count\n// exceeds 30% of the manifest. Legitimate single-file deletes via the\n// agent stay nowhere near the threshold; a workspace unmount or\n// catastrophic delete rapidly accumulates and is stopped.\n//\n// Manifest size of 0 means the agent has nothing in the cloud yet —\n// can't be a disaster, so always allow.\n//\n// In-memory only. A plugin restart resets the window, which is acceptable:\n// a restart already implies operator awareness.\n\nconst DEFAULT_WINDOW_MS = 60_000;\nconst DEFAULT_THRESHOLD_RATIO = 0.3;\n\nexport interface DeleteBrakeOptions {\n windowMs?: number;\n thresholdRatio?: number;\n now?: () => number;\n}\n\nexport interface DeleteBrake {\n // Returns true if the proposed delete count is safe to proceed (and\n // records it). Returns false if combining it with the existing rolling\n // window would exceed the threshold — caller must not perform the\n // delete in that case.\n check(deleteCount: number, manifestSize: number): boolean;\n // Current sum of deletes within the active window (after expiry).\n windowSum(): number;\n}\n\nexport function createDeleteBrake(opts: DeleteBrakeOptions = {}): DeleteBrake {\n const windowMs = opts.windowMs ?? DEFAULT_WINDOW_MS;\n const thresholdRatio = opts.thresholdRatio ?? DEFAULT_THRESHOLD_RATIO;\n const now = opts.now ?? (() => Date.now());\n\n const records: { ts: number; count: number }[] = [];\n\n function expire(): void {\n const cutoff = now() - windowMs;\n while (records.length > 0 && records[0].ts < cutoff) records.shift();\n }\n\n function sum(): number {\n return records.reduce((acc, r) => acc + r.count, 0);\n }\n\n return {\n check(deleteCount, manifestSize) {\n if (deleteCount <= 0) return true;\n expire();\n // Fresh agent (no manifest entries) — nothing to lose, always allow.\n if (manifestSize === 0) {\n records.push({ ts: now(), count: deleteCount });\n return true;\n }\n const projected = sum() + deleteCount;\n if (projected / manifestSize > thresholdRatio) return false;\n records.push({ ts: now(), count: deleteCount });\n return true;\n },\n windowSum() {\n expire();\n return sum();\n },\n };\n}\n","/**\n * @alfe.ai/openclaw-sync — OpenClaw Sync plugin.\n *\n * Wraps the sync engine as a lifecycle-managed integration. Same shape as\n * @alfe.ai/openclaw-memory-cloud / -secrets / -google: one AgentApiClient\n * is constructed in `activate()` and reused for every API call. The agent's\n * identity is resolved server-side from the API key — the plugin never\n * touches `/auth/validate`, never plumbs an `agentId` around, never writes\n * a `.alfesync/` directory.\n *\n * Lifecycle:\n * - activate(api): construct client, start sync engine + watcher\n * - deactivate(api): stop watcher, drop relay connections, clean up\n * - configure(api, config): swap schedule/scope at runtime\n *\n * Registers gateway RPC methods: `sync.now`, `sync.status`.\n */\n\nimport { createRequire } from 'node:module';\nimport {\n resolveConfig as resolveAlfeConfig,\n configExists as alfeConfigExists,\n DEFAULT_SOCKET_PATH,\n DEFAULT_WORKSPACE_PATH,\n type ResolvedConfig as AlfeResolvedConfig,\n} from '@alfe.ai/config';\nimport { AgentApiClient, type SyncAgentInfo } from '@alfe.ai/agent-api-client';\n\nimport { existsSync } from 'node:fs';\nimport { join } from 'node:path';\n\nimport { createSyncEngine, type SyncEngine, type SyncResult } from './sync-engine.js';\nimport { startWatcher } from './watcher.js';\nimport { createSharedSyncEngine, type SharedSyncEngine, type SharedScope } from './shared-sync.js';\nimport { createDeleteBrake, type DeleteBrake } from './delete-brake.js';\nimport { readManifest } from './manifest.js';\n\nconst require = createRequire(import.meta.url);\nconst pkg = require('../package.json') as { version: string };\n\n// ── Constants ───────────────────────────────────────────────\nconst SYNC_CAPABILITIES = ['sync.push', 'sync.pull', 'sync.fullSync'] as const;\nconst SYNC_RELAY_RECONNECT_BASE_MS = 1000;\nconst SYNC_RELAY_RECONNECT_MAX_MS = 30000;\nconst SYNC_RELAY_DEBOUNCE_MS = 500;\n\n// ── Types ───────────────────────────────────────────────────\n\ninterface PluginLogger {\n info(msg: string): void;\n warn(msg: string): void;\n error(msg: string): void;\n debug(msg: string): void;\n}\n\ninterface PluginServiceContext {\n config: Record<string, unknown>;\n workspaceDir?: string;\n stateDir: string;\n logger: PluginLogger;\n}\n\ninterface PluginApi {\n logger: PluginLogger;\n registrationMode?: 'full' | 'setup-only' | 'setup-runtime' | 'cli-metadata';\n config?: SyncPluginConfig;\n registerGatewayMethod?: (name: string, handler: () => Promise<unknown>) => void;\n registerService?(service: {\n id: string;\n start: (ctx: PluginServiceContext) => void | Promise<void>;\n stop?: (ctx: PluginServiceContext) => void | Promise<void>;\n }): void;\n}\n\ninterface IpcClient {\n on(event: string, handler: (...args: unknown[]) => void): void;\n request(method: string, params: unknown): Promise<{ ok: boolean; error?: { message: string } }>;\n start(): void;\n stop(): void;\n}\n\ninterface SyncRelaySocket {\n on(event: string, handler: (...args: never[]) => void): void;\n send(data: string): void;\n close(code?: number, reason?: string): void;\n}\n\ninterface RelayMessage {\n type: string;\n status?: string;\n agentId?: string;\n message?: string;\n filePath?: string;\n etag?: string;\n eventType?: string;\n}\n\nexport interface SyncPluginConfig {\n /** Workspace path to sync. Defaults to the resolved Alfe workspace. */\n workspacePath?: string;\n /** Sync scope — which data categories to sync. */\n syncScope?: ('config' | 'conversations' | 'memory')[];\n /** Sync schedule — how often to auto-sync. */\n syncSchedule?: 'realtime' | 'hourly' | 'daily' | 'weekly';\n /** IPC socket path override. */\n socketPath?: string;\n /** Sync relay URL override (default: derived from apiUrl). */\n syncRelayUrl?: string;\n /** Enable shared file sync for org/team/project scopes. Default: true. */\n sharedSync?: boolean;\n}\n\n// ── Plugin State ────────────────────────────────────────────\n\nlet client: AgentApiClient | null = null;\nlet agentId: string | null = null;\nlet syncEngine: SyncEngine | null = null;\nlet sharedSyncEngine: SharedSyncEngine | null = null;\nlet stopWatcher: (() => Promise<void>) | null = null;\nlet deleteBrake: DeleteBrake | null = null;\nlet daemonIpcClient: IpcClient | null = null;\nlet scheduledInterval: ReturnType<typeof setInterval> | null = null;\nlet currentConfig: SyncPluginConfig = {};\nlet lastSyncResult: SyncResult | null = null;\nlet syncRelayWs: SyncRelaySocket | null = null;\nlet syncRelayReconnectTimer: ReturnType<typeof setTimeout> | null = null;\nlet syncRelayReconnectAttempt = 0;\nlet syncRelayDebounceTimer: ReturnType<typeof setTimeout> | null = null;\nconst syncRelayPendingPaths = new Map<\n string,\n { etag?: string; eventType: 'created' | 'deleted' }\n>();\n\n// ── Schedule Helpers ────────────────────────────────────────\n\nconst SCHEDULE_INTERVALS_MS: Record<string, number> = {\n hourly: 60 * 60 * 1000,\n daily: 24 * 60 * 60 * 1000,\n weekly: 7 * 24 * 60 * 60 * 1000,\n};\n\nfunction clearSchedule() {\n if (scheduledInterval) {\n clearInterval(scheduledInterval);\n scheduledInterval = null;\n }\n}\n\nfunction setupSchedule(schedule: string, log: PluginLogger) {\n clearSchedule();\n if (schedule === 'realtime') {\n log.info('Sync schedule: realtime (file watcher active)');\n return;\n }\n\n const intervalMs = SCHEDULE_INTERVALS_MS[schedule];\n if (!intervalMs) {\n log.warn(`Unknown sync schedule: ${schedule}, defaulting to hourly`);\n setupSchedule('hourly', log);\n return;\n }\n\n log.info(`Sync schedule: ${schedule} (every ${String(intervalMs / 1000)}s)`);\n scheduledInterval = setInterval(() => {\n if (!syncEngine) return;\n const engine = syncEngine;\n void (async () => {\n try {\n log.info(`Scheduled sync (${schedule}) starting...`);\n lastSyncResult = await engine.fullSync({ quiet: true });\n log.info(\n `Scheduled sync complete: ${String(lastSyncResult.pushed)} pushed, ${String(lastSyncResult.pulled)} pulled`,\n );\n } catch (err: unknown) {\n log.error(`Scheduled sync failed: ${err instanceof Error ? err.message : String(err)}`);\n }\n })();\n }, intervalMs);\n}\n\n// ── Daemon IPC ──────────────────────────────────────────────\n\nasync function connectToDaemon(socketPath: string, log: PluginLogger): Promise<IpcClient | null> {\n try {\n const modPath = '@alfe.ai/openclaw';\n const mod = (await import(/* webpackIgnore: true */ modPath)) as {\n IPCClient: new (socketPath: string, log: PluginLogger) => IpcClient;\n };\n const ipc = new mod.IPCClient(socketPath, log);\n\n ipc.on('connected', () => {\n void (async () => {\n log.info('Connected to Alfe daemon — registering sync capabilities...');\n const response = await ipc.request('capability.register', {\n plugin: '@alfe.ai/openclaw-sync',\n capabilities: [...SYNC_CAPABILITIES],\n });\n if (response.ok) {\n log.info('Sync capabilities registered with daemon');\n } else {\n log.warn(`Failed to register sync capabilities: ${response.error?.message ?? 'unknown'}`);\n }\n })();\n });\n\n ipc.on('disconnected', (...args: unknown[]) => {\n const reason = typeof args[0] === 'string' ? args[0] : String(args[0]);\n log.warn(`Disconnected from Alfe daemon: ${reason}`);\n });\n\n ipc.on('message', (...args: unknown[]) => {\n const msg = args[0] as Record<string, unknown> | undefined;\n if (msg?.type === 'SYNC_NOW' || msg?.command === 'SYNC_NOW') {\n log.info('Received SYNC_NOW command — triggering immediate sync...');\n if (syncEngine) {\n const engine = syncEngine;\n void (async () => {\n try {\n lastSyncResult = await engine.fullSync({ quiet: true });\n log.info(\n `SYNC_NOW complete: ${String(lastSyncResult.pushed)} pushed, ${String(lastSyncResult.pulled)} pulled`,\n );\n } catch (err: unknown) {\n log.error(`SYNC_NOW failed: ${err instanceof Error ? err.message : String(err)}`);\n }\n })();\n }\n }\n\n if (msg?.type === 'SHARED_SCOPES') {\n const scopes = msg.scopes as SharedScope[] | undefined;\n const engine = sharedSyncEngine;\n if (scopes && engine) {\n log.info(`Received SHARED_SCOPES update: ${String(scopes.length)} scope(s)`);\n void (async () => {\n try {\n await engine.updateScopes(scopes);\n } catch (err: unknown) {\n log.error(\n `SHARED_SCOPES update failed: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n })();\n }\n }\n });\n\n ipc.on('error', (...args: unknown[]) => {\n const err = args[0];\n log.debug(`Daemon IPC error: ${err instanceof Error ? err.message : String(err)}`);\n });\n\n ipc.start();\n return ipc;\n } catch {\n log.info('Alfe daemon not available — Sync plugin running standalone');\n return null;\n }\n}\n\n// ── Sync Relay Connection ───────────────────────────────────\n\nfunction clearSyncRelayReconnect() {\n if (syncRelayReconnectTimer) {\n clearTimeout(syncRelayReconnectTimer);\n syncRelayReconnectTimer = null;\n }\n}\n\nfunction clearSyncRelayDebounce() {\n if (syncRelayDebounceTimer) {\n clearTimeout(syncRelayDebounceTimer);\n syncRelayDebounceTimer = null;\n }\n}\n\nasync function processPendingNotifications(log: PluginLogger) {\n if (syncRelayPendingPaths.size === 0) return;\n\n const entries = new Map(syncRelayPendingPaths);\n syncRelayPendingPaths.clear();\n\n const sharedEntries = new Map<string, { etag?: string; eventType: 'created' | 'deleted' }>();\n const privateEntries = new Map<string, { etag?: string; eventType: 'created' | 'deleted' }>();\n\n for (const [filePath, info] of entries) {\n if (filePath.startsWith('shared/')) {\n sharedEntries.set(filePath, info);\n } else {\n privateEntries.set(filePath, info);\n }\n }\n\n if (sharedEntries.size > 0 && sharedSyncEngine) {\n for (const [filePath, info] of sharedEntries) {\n try {\n await sharedSyncEngine.handleNotification(filePath, info.eventType);\n } catch (err: unknown) {\n log.error(\n `Shared sync relay: failed ${info.eventType} ${filePath}: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n }\n }\n\n if (privateEntries.size > 0 && syncEngine) {\n const engine = syncEngine;\n const toPull: string[] = [];\n const toDelete: string[] = [];\n\n for (const [filePath, info] of privateEntries) {\n if (info.eventType === 'deleted') {\n toDelete.push(filePath);\n } else {\n toPull.push(filePath);\n }\n }\n\n for (const filePath of toDelete) {\n try {\n await engine.removeLocalFile(filePath, { quiet: true });\n log.debug(`Sync relay: deleted ${filePath}`);\n } catch (err: unknown) {\n log.error(\n `Sync relay: failed to delete ${filePath}: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n }\n\n if (toPull.length > 0) {\n try {\n const result = await engine.pullFiles(toPull, { quiet: true });\n if (result.pulled > 0) log.info(`Sync relay: pulled ${String(result.pulled)} file(s)`);\n if (result.errors > 0) log.warn(`Sync relay: ${String(result.errors)} pull error(s)`);\n } catch (err: unknown) {\n log.error(`Sync relay: pull failed: ${err instanceof Error ? err.message : String(err)}`);\n }\n }\n }\n}\n\nasync function connectToSyncRelay(\n relayUrl: string,\n token: string,\n agentIdForSubscribe: string,\n log: PluginLogger,\n): Promise<SyncRelaySocket | null> {\n try {\n const { default: WebSocket } = await import('ws');\n\n const wsUrl = `${relayUrl}?token=${encodeURIComponent(token)}`;\n const ws = new WebSocket(wsUrl);\n\n ws.on('open', () => {\n log.info('Connected to Sync Relay');\n syncRelayReconnectAttempt = 0;\n ws.send(JSON.stringify({ type: 'SUBSCRIBE', agentId: agentIdForSubscribe }));\n });\n\n ws.on('message', (data: Buffer | string) => {\n let message: RelayMessage;\n try {\n message = JSON.parse(data.toString()) as RelayMessage;\n } catch {\n return;\n }\n\n switch (message.type) {\n case 'SUBSCRIBE_ACK':\n if (message.status === 'ok') {\n log.info(`Subscribed to sync notifications for agent ${message.agentId ?? agentIdForSubscribe}`);\n const sharedEngine = sharedSyncEngine;\n if (sharedEngine) {\n void (async () => {\n try {\n await sharedEngine.fullSync();\n log.info('Shared sync: reconnect full sync complete');\n } catch (err: unknown) {\n log.error(\n `Shared sync: reconnect full sync failed: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n })();\n }\n } else {\n log.warn(`Sync relay subscribe failed: ${message.message ?? 'unknown'}`);\n }\n break;\n\n case 'FILE_CHANGED':\n if (message.filePath) {\n syncRelayPendingPaths.set(message.filePath, {\n etag: message.etag,\n eventType: message.eventType === 'deleted' ? 'deleted' : 'created',\n });\n }\n clearSyncRelayDebounce();\n syncRelayDebounceTimer = setTimeout(\n () => void processPendingNotifications(log),\n SYNC_RELAY_DEBOUNCE_MS,\n );\n break;\n\n case 'PING':\n try {\n ws.send(JSON.stringify({ type: 'PONG' }));\n } catch {\n // ignore\n }\n break;\n }\n });\n\n ws.on('close', (code: number) => {\n log.info(`Sync Relay disconnected (code=${String(code)})`);\n syncRelayWs = null;\n scheduleSyncRelayReconnect(relayUrl, token, agentIdForSubscribe, log);\n });\n\n ws.on('error', (err: Error) => {\n log.debug(`Sync Relay error: ${err.message}`);\n });\n\n return ws as SyncRelaySocket;\n } catch (err: unknown) {\n log.debug(`Failed to connect to Sync Relay: ${err instanceof Error ? err.message : String(err)}`);\n scheduleSyncRelayReconnect(relayUrl, token, agentIdForSubscribe, log);\n return null;\n }\n}\n\nfunction scheduleSyncRelayReconnect(\n relayUrl: string,\n token: string,\n agentIdForSubscribe: string,\n log: PluginLogger,\n) {\n clearSyncRelayReconnect();\n const delay = Math.min(\n SYNC_RELAY_RECONNECT_BASE_MS * Math.pow(2, syncRelayReconnectAttempt),\n SYNC_RELAY_RECONNECT_MAX_MS,\n );\n syncRelayReconnectAttempt++;\n log.debug(\n `Reconnecting to Sync Relay in ${String(delay)}ms (attempt ${String(syncRelayReconnectAttempt)})`,\n );\n\n syncRelayReconnectTimer = setTimeout(() => {\n void (async () => {\n syncRelayWs = await connectToSyncRelay(relayUrl, token, agentIdForSubscribe, log);\n })();\n }, delay);\n}\n\nfunction disconnectSyncRelay() {\n clearSyncRelayReconnect();\n clearSyncRelayDebounce();\n syncRelayPendingPaths.clear();\n\n if (syncRelayWs) {\n try {\n syncRelayWs.send(JSON.stringify({ type: 'UNSUBSCRIBE' }));\n syncRelayWs.close(1000, 'Plugin deactivating');\n } catch {\n // ignore\n }\n syncRelayWs = null;\n }\n}\n\nfunction deriveRelayUrl(apiUrl: string): string {\n if (apiUrl.includes('dev.alfe.ai')) return 'wss://sync.dev.alfe.ai/ws';\n if (apiUrl.includes('demo.alfe.ai')) return 'wss://sync.demo.alfe.ai/ws';\n if (apiUrl.includes('test.alfe.ai')) return 'wss://sync.test.alfe.ai/ws';\n return 'wss://sync.alfe.ai/ws';\n}\n\n// ── Plugin Definition ───────────────────────────────────────\n\nconst plugin = {\n id: '@alfe.ai/openclaw-sync',\n name: 'Alfe Sync Plugin',\n description:\n 'Back up agent configuration, conversations, and memory to the cloud with scheduled or real-time sync.',\n version: pkg.version,\n\n activate(api: PluginApi) {\n const log = api.logger;\n\n const pluginConfig: SyncPluginConfig = api.config ?? {};\n currentConfig = pluginConfig;\n\n let alfeConfig: { workspacePath: string; socketPath: string } | null = null;\n try {\n alfeConfig = resolveAlfeConfig();\n } catch {\n // alfe login hasn't run; logged below\n }\n\n const workspacePath =\n pluginConfig.workspacePath ?? alfeConfig?.workspacePath ?? DEFAULT_WORKSPACE_PATH;\n\n const syncScope = pluginConfig.syncScope ?? ['config', 'conversations', 'memory'];\n // Default to 'realtime' when no schedule is configured. The integration's\n // user-config schema marks sync_schedule as required, so an unset value\n // here means the framework didn't deliver the user's choice (manifest\n // template missing, applyConfig errored, etc.). 'daily' would leave a\n // managed agent silent for 24 hours after activation; 'realtime' fails\n // open into the safest mode — initial push runs immediately and the\n // watcher streams ongoing changes.\n const syncSchedule = pluginConfig.syncSchedule ?? 'realtime';\n const socketPath =\n pluginConfig.socketPath ?? alfeConfig?.socketPath ?? DEFAULT_SOCKET_PATH;\n\n const startSyncService = async () => {\n if ((globalThis as Record<string, unknown>).__alfeSyncPluginActivated === true) {\n log.debug('Alfe Sync plugin already activated — skipping duplicate');\n return;\n }\n (globalThis as Record<string, unknown>).__alfeSyncPluginActivated = true;\n log.info('Alfe Sync plugin activating...');\n log.info(`Sync scope: ${syncScope.join(', ')}`);\n log.info(`Sync schedule: ${syncSchedule}`);\n log.info(`Workspace: ${workspacePath}`);\n\n // Sync needs `~/.alfe/config.toml` (the existing `alfe login` config).\n // Identity (agentId / tenantId) is resolved server-side from the API\n // key on every AgentApiClient call.\n if (!alfeConfigExists()) {\n log.info('Sync skipped — no Alfe config found. Run `alfe login` to enable.');\n return;\n }\n\n let syncCfg: AlfeResolvedConfig;\n try {\n syncCfg = resolveAlfeConfig();\n } catch (err: unknown) {\n log.warn(\n `Sync skipped — failed to resolve credentials from ~/.alfe/config.toml: ${err instanceof Error ? err.message : String(err)}`,\n );\n return;\n }\n\n client = new AgentApiClient({ apiKey: syncCfg.apiKey, apiUrl: syncCfg.apiUrl });\n syncEngine = createSyncEngine({ workspacePath, client });\n log.info('Sync engine initialized');\n\n // Realtime mode → initial push-only seed of pre-existing files\n // (chokidar ignoreInitial means the watcher only emits for\n // post-activation changes; without this the workspace tree never\n // reaches the cloud unless a file is touched). Push-only — not\n // fullSync — so we don't drag down stale cloud files into a fresh\n // local workspace or write .conflict-<ts> markers on first run.\n if (syncSchedule === 'realtime') {\n try {\n lastSyncResult = await syncEngine.push(undefined, { quiet: true });\n log.info('Initial workspace push complete');\n } catch (err: unknown) {\n log.warn(`Initial workspace push failed: ${err instanceof Error ? err.message : String(err)}`);\n }\n deleteBrake = createDeleteBrake();\n try {\n stopWatcher = await startWatcher({\n workspacePath,\n debounceMs: 2000,\n onChanges: async (paths) => {\n if (!syncEngine) return;\n const existing: string[] = [];\n const missing: string[] = [];\n for (const p of paths) {\n if (existsSync(join(workspacePath, p))) existing.push(p);\n else missing.push(p);\n }\n log.debug(\n `Realtime sync: ${String(existing.length)} upload(s), ${String(missing.length)} delete(s)`,\n );\n if (existing.length > 0) {\n try {\n lastSyncResult = await syncEngine.push(existing, { quiet: true });\n } catch (err: unknown) {\n log.error(`Realtime push failed: ${err instanceof Error ? err.message : String(err)}`);\n }\n }\n if (missing.length > 0) {\n // Rolling-window rate brake: refuse the batch if cumulative\n // deletes in the last 60 s would exceed 30% of the manifest.\n // See packages/openclaw-sync/src/delete-brake.ts.\n const manifest = await readManifest(workspacePath);\n const manifestSize = Object.keys(manifest.files).length;\n if (deleteBrake && !deleteBrake.check(missing.length, manifestSize)) {\n log.error(\n `Sync delete brake tripped: ${String(deleteBrake.windowSum() + missing.length)} ` +\n `deletes in last 60s vs manifest size ${String(manifestSize)} ` +\n `(threshold 30%). Refusing batch — investigate the workspace state.`,\n );\n } else {\n try {\n lastSyncResult = await syncEngine.pushDeletes(missing, { quiet: true });\n } catch (err: unknown) {\n log.error(`Realtime delete failed: ${err instanceof Error ? err.message : String(err)}`);\n }\n }\n }\n },\n });\n log.info('File watcher started for realtime sync');\n } catch (err: unknown) {\n log.warn(`Failed to start file watcher: ${err instanceof Error ? err.message : String(err)}`);\n }\n } else {\n setupSchedule(syncSchedule, log);\n }\n\n // Daemon IPC (for SYNC_NOW + SHARED_SCOPES messages)\n daemonIpcClient = await connectToDaemon(socketPath, log);\n\n // Register with the sync service to materialize the SyncAgent record\n // and learn our own agentId for relay subscription.\n let registered: SyncAgentInfo | null = null;\n try {\n const result = await client.syncRegister();\n registered = result.agent;\n agentId = registered.agentId;\n } catch (err: unknown) {\n log.warn(`Sync register failed: ${err instanceof Error ? err.message : String(err)}`);\n }\n\n // Sync relay (real-time file change notifications)\n if (registered) {\n try {\n const relayUrl = pluginConfig.syncRelayUrl ?? deriveRelayUrl(syncCfg.apiUrl);\n syncRelayWs = await connectToSyncRelay(\n relayUrl,\n syncCfg.apiKey,\n registered.agentId,\n log,\n );\n } catch (err: unknown) {\n log.debug(\n `Sync Relay connection skipped: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n // Shared sync (org/team/project scopes pushed by gateway)\n if (pluginConfig.sharedSync !== false) {\n try {\n sharedSyncEngine = createSharedSyncEngine({ workspacePath, client }, log);\n log.info('Shared sync engine created — waiting for SHARED_SCOPES from gateway');\n } catch (err: unknown) {\n log.debug(\n `Shared sync engine skipped: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n }\n }\n };\n\n const stopSyncService = async () => {\n (globalThis as Record<string, unknown>).__alfeSyncPluginActivated = false;\n clearSchedule();\n disconnectSyncRelay();\n\n if (stopWatcher) {\n try {\n await stopWatcher();\n log.info('File watcher stopped');\n } catch (err: unknown) {\n log.debug(`Error stopping watcher: ${err instanceof Error ? err.message : String(err)}`);\n }\n stopWatcher = null;\n }\n deleteBrake = null;\n\n if (daemonIpcClient) {\n try {\n daemonIpcClient.stop();\n log.info('Disconnected from Alfe daemon');\n } catch (err: unknown) {\n log.debug(`Error disconnecting from daemon: ${err instanceof Error ? err.message : String(err)}`);\n }\n daemonIpcClient = null;\n }\n\n client = null;\n agentId = null;\n syncEngine = null;\n sharedSyncEngine = null;\n lastSyncResult = null;\n currentConfig = {};\n\n log.info('Alfe Sync plugin deactivated');\n };\n\n if (typeof api.registerGatewayMethod === 'function') {\n api.registerGatewayMethod('sync.now', async () => {\n if (!syncEngine) {\n return { ok: false, error: 'Sync engine not initialized — run `alfe login`' };\n }\n try {\n lastSyncResult = await syncEngine.fullSync({ quiet: true });\n return { ok: true, result: lastSyncResult };\n } catch (err: unknown) {\n return { ok: false, error: err instanceof Error ? err.message : String(err) };\n }\n });\n log.info('Registered gateway RPC method: sync.now');\n\n api.registerGatewayMethod('sync.status', () =>\n Promise.resolve({\n ok: true,\n initialized: !!syncEngine,\n agentId,\n schedule: currentConfig.syncSchedule ?? 'daily',\n scope: currentConfig.syncScope ?? ['config', 'conversations', 'memory'],\n lastResult: lastSyncResult,\n watcherActive: !!stopWatcher,\n }),\n );\n log.info('Registered gateway RPC method: sync.status');\n }\n\n if (api.registerService) {\n api.registerService({\n id: 'alfe-sync-engine',\n start: () => startSyncService(),\n stop: () => stopSyncService(),\n });\n } else {\n void startSyncService().catch((err: unknown) => {\n log.error(`Sync plugin async init failed: ${err instanceof Error ? err.message : String(err)}`);\n });\n }\n\n log.info('Alfe Sync plugin activated');\n },\n\n async deactivate(api: PluginApi) {\n (globalThis as Record<string, unknown>).__alfeSyncPluginActivated = false;\n const log = api.logger;\n log.info('Alfe Sync plugin deactivating...');\n\n clearSchedule();\n disconnectSyncRelay();\n\n if (stopWatcher) {\n try {\n await stopWatcher();\n log.info('File watcher stopped');\n } catch (err: unknown) {\n log.debug(`Error stopping watcher: ${err instanceof Error ? err.message : String(err)}`);\n }\n stopWatcher = null;\n }\n\n if (daemonIpcClient) {\n try {\n daemonIpcClient.stop();\n log.info('Disconnected from Alfe daemon');\n } catch (err: unknown) {\n log.debug(`Error disconnecting from daemon: ${err instanceof Error ? err.message : String(err)}`);\n }\n daemonIpcClient = null;\n }\n\n client = null;\n agentId = null;\n syncEngine = null;\n sharedSyncEngine = null;\n lastSyncResult = null;\n currentConfig = {};\n\n log.info('Alfe Sync plugin deactivated');\n },\n\n async configure(api: PluginApi, config: SyncPluginConfig) {\n const log = api.logger;\n log.info('Reconfiguring Alfe Sync plugin...');\n currentConfig = { ...currentConfig, ...config };\n\n if (config.syncSchedule) {\n if (config.syncSchedule === 'realtime' && syncEngine && !stopWatcher) {\n const workspacePath = currentConfig.workspacePath ?? syncEngine.workspacePath;\n stopWatcher = await startWatcher({\n workspacePath,\n debounceMs: 2000,\n onChanges: async (paths) => {\n if (!syncEngine) return;\n try {\n lastSyncResult = await syncEngine.push(paths, { quiet: true });\n } catch (err: unknown) {\n log.error(`Realtime push failed: ${err instanceof Error ? err.message : String(err)}`);\n }\n },\n });\n clearSchedule();\n log.info('Switched to realtime sync');\n } else if (config.syncSchedule !== 'realtime') {\n if (stopWatcher) {\n await stopWatcher();\n stopWatcher = null;\n }\n setupSchedule(config.syncSchedule, log);\n }\n }\n\n if (config.syncScope) {\n log.info(`Updated sync scope: ${config.syncScope.join(', ')}`);\n }\n\n log.info('Alfe Sync plugin reconfigured');\n },\n};\n\nexport default plugin;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAwBA,eAAsB,aACpB,SAC8B;CAC9B,MAAM,EAAE,eAAe,aAAa,KAAM,cAAc;CACxD,MAAM,iBAAiB,MAAM,mBAAmB,cAAc;CAG9D,MAAM,0BAAU,IAAI,KAA4C;CAEhE,IAAI,6BAAa,IAAI,KAAa;CAClC,IAAI,aAAmD;CAEvD,SAAS,gBAAgB;AACvB,MAAI,WAAY;AAChB,eAAa,iBAAiB;AAC5B,gBAAa;AACb,OAAI,WAAW,SAAS,EAAG;GAE3B,MAAM,QAAQ,CAAC,GAAG,WAAW;AAC7B,gCAAa,IAAI,KAAK;AACjB,aAAU,MAAM;KACpB,WAAW;;CAGhB,SAAS,aAAa,cAAsB;EAC1C,MAAM,eAAe,SAAS,eAAe,aAAa;AAG1D,MAAI,aAAa,cAAc,eAAe,CAAE;EAGhD,MAAM,gBAAgB,QAAQ,IAAI,aAAa;AAC/C,MAAI,cAAe,cAAa,cAAc;EAG9C,MAAM,QAAQ,iBAAiB;AAC7B,WAAQ,OAAO,aAAa;AAC5B,cAAW,IAAI,aAAa;AAC5B,kBAAe;KACd,WAAW;AAEd,UAAQ,IAAI,cAAc,MAAM;;CAGlC,MAAM,UAAU,MAAM,eAAe;EACnC,YAAY;EACZ,eAAe;EACf,gBAAgB;EAChB,OAAO,KAAA;EACP,SAAS;GACP;GACA;GACA;GACA;GACD;EACF,CAAC;AAEF,SAAQ,GAAG,OAAO,aAAa;AAC/B,SAAQ,GAAG,UAAU,aAAa;AAClC,SAAQ,GAAG,UAAU,aAAa;AAGlC,QAAO,YAAY;AAEjB,OAAK,MAAM,SAAS,QAAQ,QAAQ,CAClC,cAAa,MAAM;AAErB,UAAQ,OAAO;AAEf,MAAI,YAAY;AACd,gBAAa,WAAW;AACxB,gBAAa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C,QAAI,aAAa;AACf,WAAM,aAAa;AACnB,mBAAc;;AAEhB,kBAAc,OAAO,cAAc,IAAI;;;AAI3C,MAAI,OAAO,UACT,KAAI,KAAK,uBAAuB,OAAO,UAAU,KAAK,KAAK,GAAG;AAGhE,MAAI,KAAK,gCAAgC;;CAE5C"}
@@ -16,6 +16,36 @@
16
16
  "configSchema": {
17
17
  "type": "object",
18
18
  "additionalProperties": false,
19
- "properties": {}
19
+ "properties": {
20
+ "syncSchedule": {
21
+ "type": "string",
22
+ "enum": ["realtime", "hourly", "daily", "weekly"],
23
+ "default": "realtime",
24
+ "description": "How often the plugin syncs the workspace to the cloud. Realtime starts a chokidar watcher; hourly/daily/weekly run a full sync on a setInterval."
25
+ },
26
+ "syncScope": {
27
+ "type": "array",
28
+ "items": { "type": "string", "enum": ["config", "conversations", "memory"] },
29
+ "default": ["config", "conversations", "memory"],
30
+ "description": "Which categories of files to sync. Currently advisory — the engine pushes the whole workspace minus DEFAULT_IGNORES regardless."
31
+ },
32
+ "workspacePath": {
33
+ "type": "string",
34
+ "description": "Override the workspace root being watched. Defaults to the resolved Alfe workspace (~/.openclaw on managed agents)."
35
+ },
36
+ "socketPath": {
37
+ "type": "string",
38
+ "description": "Override the daemon IPC socket path. Defaults to the alfe daemon's gateway.sock."
39
+ },
40
+ "syncRelayUrl": {
41
+ "type": "string",
42
+ "description": "Override the sync relay WebSocket URL. Defaults are derived from the configured Alfe API endpoint."
43
+ },
44
+ "sharedSync": {
45
+ "type": "boolean",
46
+ "default": true,
47
+ "description": "Enable shared file sync for org/team/project scopes pushed by the gateway."
48
+ }
49
+ }
20
50
  }
21
51
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alfe.ai/openclaw-sync",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "AlfeSync — agent workspace backup and sync skill for OpenClaw",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",