-
Notifications
You must be signed in to change notification settings - Fork 452
feat: M16 content_migrated + swarm_* analytics events #730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cuongdo-byterover
merged 9 commits into
proj/analytics-system-tool-mode
from
feat/ENG-3012
May 28, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
68b528e
feat: [ENG-3012] M16.8 add content_migrated analytics event schema
cuongdo-byterover 4c32318
feat: [ENG-3015] M16.11 add swarm_onboarded analytics event schema
cuongdo-byterover cd33c75
feat: [ENG-3013] M16.9 add swarm_query_completed analytics event schema
cuongdo-byterover d2259b8
feat: [ENG-3014] M16.10 add swarm_store_completed analytics event schema
cuongdo-byterover 88ad6e6
feat: [ENG-3013/3014/3015] add SwarmHandler daemon transport for swar…
cuongdo-byterover 6e02664
Merge branch 'proj/analytics-system-tool-mode' into feat/ENG-3012
cuongdo-byterover ecff891
fix: [ENG-3013/3014/3015] address PR #730 review on SwarmHandler
cuongdo-byterover e24c780
Merge branch 'feat/ENG-3012' of github.com:campfirein/byterover-cli i…
cuongdo-byterover d2fd66f
Merge branch 'proj/analytics-system-tool-mode' into feat/ENG-3012
cuongdo-byterover File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /** | ||
| * Handler for `swarm:*` transport events. | ||
| * | ||
| * Thin emit surface for the federated-memory-provider operations | ||
| * (`brv swarm query`, `brv swarm curate`, `brv swarm onboard`). The | ||
| * coordinator itself still lives in the agent process at | ||
| * `src/agent/infra/swarm/swarm-coordinator.ts` — the daemon does NOT | ||
| * proxy the operations today. The CLI commands run swarm-coordinator | ||
| * client-side and dispatch one of these three transport events to | ||
| * the daemon when the operation terminates. The handler validates | ||
| * the payload against the per-event Zod schema and forwards to | ||
| * `analyticsClient.track()`. | ||
| * | ||
| * Mirrors the try/processLog pattern from `SettingsHandler` and | ||
| * `MigrateHandler` so analytics failures never affect command | ||
| * outcomes — the CLI gets `tracked: false` plus a reason; nothing | ||
| * throws. | ||
| * | ||
| * Forward direction (out of scope for this commit): if the swarm | ||
| * coordinator is moved into the daemon process, the SAME three event | ||
| * names extend to carry the operation request payloads. Only the | ||
| * handler internals change; CLI / LLM-tool callers stay unchanged. | ||
| */ | ||
|
|
||
| import type { | ||
| SwarmTrackOnboardedRequest, | ||
| SwarmTrackQueryCompletedRequest, | ||
| SwarmTrackResponse, | ||
| SwarmTrackStoreCompletedRequest, | ||
| } from '../../../../shared/transport/events/swarm-events.js' | ||
| import type {IAnalyticsClient} from '../../../core/interfaces/analytics/i-analytics-client.js' | ||
| import type {ITransportServer} from '../../../core/interfaces/transport/i-transport-server.js' | ||
|
|
||
| import {AnalyticsEventNames} from '../../../../shared/analytics/event-names.js' | ||
| import {SwarmOnboardedSchema} from '../../../../shared/analytics/events/swarm-onboarded.js' | ||
| import {SwarmQueryCompletedSchema} from '../../../../shared/analytics/events/swarm-query-completed.js' | ||
| import {SwarmStoreCompletedSchema} from '../../../../shared/analytics/events/swarm-store-completed.js' | ||
| import {SwarmEvents} from '../../../../shared/transport/events/swarm-events.js' | ||
| import {processLog} from '../../../utils/process-logger.js' | ||
|
|
||
| export interface SwarmHandlerDeps { | ||
| /** | ||
| * Optional — when undefined the handler still registers the transport | ||
| * events but returns `{tracked: false, reason: 'analytics-unavailable'}` | ||
| * for every call. Lets the wiring exist before analytics is plumbed | ||
| * in test harnesses. | ||
| */ | ||
| readonly analyticsClient?: IAnalyticsClient | ||
| transport: ITransportServer | ||
| } | ||
|
|
||
| export class SwarmHandler { | ||
| private readonly analyticsClient: IAnalyticsClient | undefined | ||
| private readonly transport: ITransportServer | ||
|
|
||
| constructor(deps: SwarmHandlerDeps) { | ||
| this.analyticsClient = deps.analyticsClient | ||
| this.transport = deps.transport | ||
| } | ||
|
|
||
| setup(): void { | ||
| this.transport.onRequest<SwarmTrackQueryCompletedRequest, SwarmTrackResponse>( | ||
| SwarmEvents.TRACK_QUERY_COMPLETED, | ||
| (data) => this.handleTrackQueryCompleted(data), | ||
| ) | ||
| this.transport.onRequest<SwarmTrackStoreCompletedRequest, SwarmTrackResponse>( | ||
| SwarmEvents.TRACK_STORE_COMPLETED, | ||
| (data) => this.handleTrackStoreCompleted(data), | ||
| ) | ||
| this.transport.onRequest<SwarmTrackOnboardedRequest, SwarmTrackResponse>( | ||
| SwarmEvents.TRACK_ONBOARDED, | ||
| (data) => this.handleTrackOnboarded(data), | ||
| ) | ||
| } | ||
|
|
||
| private handleTrackOnboarded(data: SwarmTrackOnboardedRequest): SwarmTrackResponse { | ||
| const parsed = SwarmOnboardedSchema.safeParse(data) | ||
| if (!parsed.success) return {reason: 'schema-rejection', tracked: false} | ||
| return this.runEmit(AnalyticsEventNames.SWARM_ONBOARDED, (client) => | ||
| client.track(AnalyticsEventNames.SWARM_ONBOARDED, parsed.data), | ||
| ) | ||
| } | ||
|
|
||
| private handleTrackQueryCompleted(data: SwarmTrackQueryCompletedRequest): SwarmTrackResponse { | ||
| // Validate at the transport boundary — the CLI is an external trust | ||
| // boundary even though we ship it ourselves. | ||
| const parsed = SwarmQueryCompletedSchema.safeParse(data) | ||
| if (!parsed.success) return {reason: 'schema-rejection', tracked: false} | ||
| return this.runEmit(AnalyticsEventNames.SWARM_QUERY_COMPLETED, (client) => | ||
| client.track(AnalyticsEventNames.SWARM_QUERY_COMPLETED, parsed.data), | ||
| ) | ||
| } | ||
|
|
||
| private handleTrackStoreCompleted(data: SwarmTrackStoreCompletedRequest): SwarmTrackResponse { | ||
| const parsed = SwarmStoreCompletedSchema.safeParse(data) | ||
| if (!parsed.success) return {reason: 'schema-rejection', tracked: false} | ||
| return this.runEmit(AnalyticsEventNames.SWARM_STORE_COMPLETED, (client) => | ||
| client.track(AnalyticsEventNames.SWARM_STORE_COMPLETED, parsed.data), | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Shared try/catch wrapper. The thunk does the literal-narrowed | ||
| * `track(NAME, props)` call so TS infers `PropsArg<E>` per event — no | ||
| * generic widening, no `as` cast. | ||
| */ | ||
| private runEmit(eventLabel: string, fn: (client: IAnalyticsClient) => void): SwarmTrackResponse { | ||
| const client = this.analyticsClient | ||
| if (!client) return {reason: 'analytics-unavailable', tracked: false} | ||
| try { | ||
| fn(client) | ||
| return {tracked: true} | ||
| } catch (error) { | ||
| processLog( | ||
| `[Swarm] analytics track ${eventLabel} failed: ${error instanceof Error ? error.message : String(error)}`, | ||
| ) | ||
| return {reason: 'analytics-throw', tracked: false} | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* eslint-disable camelcase */ | ||
| import {z} from 'zod' | ||
|
|
||
| /** | ||
| * Per-event schema for `content_migrated`. | ||
| * | ||
| * Admin-op content migration between scopes (e.g. moving curated knowledge | ||
| * between sources, spaces, or projects). Distinct from `migrate_run` | ||
| * (ENG-3008 / `migrate-run.ts`) which covers the `brv migrate` MD→HTML | ||
| * one-shot — that operation lives in `MigrateHandler` and its semantics | ||
| * are file-format-conversion, not scope-aware data movement. | ||
| * | ||
| * `source_kind` / `target_kind` are short enum strings naming the | ||
| * abstract scope on each side (e.g. `'local'`, `'space'`, `'shared'`). | ||
| * Kept as `z.string().min(1).max(64)` rather than a closed enum so future | ||
| * scopes can plug in without a schema migration; the producer is | ||
| * responsible for taxonomizing. | ||
| * | ||
| * Per the M15.1 outcome taxonomy: `outcome: 'success' | 'failure'`, | ||
| * `failure_kind` populated only on failure. Counts are optional so a | ||
| * failure path that surfaces before counts are known still emits a | ||
| * well-formed event. | ||
| * | ||
| * SCHEMA-ONLY REGISTRATION TODAY: no daemon-handler emit site exists in | ||
| * this codebase yet. The producer will land alongside the admin op when | ||
| * its handler is built. See ENG-2770 for the precedent. | ||
| */ | ||
| const failureKindSchema = z.string().min(1).max(64).optional() | ||
| const countSchema = z.number().int().nonnegative().optional() | ||
|
|
||
| export const ContentMigratedSchema = z | ||
| .object({ | ||
| /** True when the run was a no-write dry run. */ | ||
| dry_run: z.boolean().optional(), | ||
| /** Counts — optional because failure can surface before they're computed. */ | ||
| duration_ms: z.number().int().nonnegative().optional(), | ||
| failed: countSchema, | ||
| failure_kind: failureKindSchema, | ||
| migrated: countSchema, | ||
| outcome: z.enum(['success', 'failure']), | ||
| skipped: countSchema, | ||
| /** Abstract scope identifiers (e.g. 'local', 'space', 'shared'). Producer-taxonomized. */ | ||
| source_kind: z.string().min(1).max(64), | ||
| target_kind: z.string().min(1).max(64), | ||
| }) | ||
| .strict() | ||
|
|
||
| export type ContentMigratedProps = z.infer<typeof ContentMigratedSchema> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* eslint-disable camelcase */ | ||
| import {z} from 'zod' | ||
|
|
||
| /** | ||
| * Per-event schema for `swarm_onboarded`. | ||
| * | ||
| * Activation entry point for `brv swarm onboard` — fires when the wizard | ||
| * completes (success path) or aborts (failure path). Swarm counterpart | ||
| * to M15.2's brv-init / onboarding-completed activation events. | ||
| * | ||
| * `swarm_kind` is a short producer-taxonomized string (e.g. `'new'` when | ||
| * the user scaffolded a fresh config, `'joined'` when they pointed at an | ||
| * existing swarm). Kept as `z.string().min(1).max(64)` so future flows | ||
| * plug in without a schema migration. | ||
| * | ||
| * `member_count` captures the active-provider count from the resulting | ||
| * swarm config (e.g. `byterover`, `obsidian`, `gbrain`). Optional — | ||
| * failure paths may surface before the count is computed. | ||
| * | ||
| * SCHEMA-ONLY REGISTRATION TODAY: the swarm onboard surface lives in the | ||
| * agent process (`src/agent/infra/swarm/wizard/swarm-wizard.ts`), not in | ||
| * a daemon transport handler. The producer requires either a new daemon | ||
| * handler that the CLI command calls, or a synthetic-emit pattern (cf. | ||
| * M17). That wiring is deferred to a follow-up. See ENG-2770 for the | ||
| * schema-only precedent. | ||
| */ | ||
| const failureKindSchema = z.string().min(1).max(64).optional() | ||
|
|
||
| export const SwarmOnboardedSchema = z | ||
| .object({ | ||
| duration_ms: z.number().int().nonnegative().optional(), | ||
| failure_kind: failureKindSchema, | ||
| /** Number of active providers in the resulting swarm config. */ | ||
| member_count: z.number().int().nonnegative().optional(), | ||
| outcome: z.enum(['success', 'failure']), | ||
| /** Onboarding flow taxonomy (e.g. 'new', 'joined'). Producer-taxonomized. */ | ||
| swarm_kind: z.string().min(1).max(64).optional(), | ||
| }) | ||
| .strict() | ||
|
|
||
| export type SwarmOnboardedProps = z.infer<typeof SwarmOnboardedSchema> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* eslint-disable camelcase */ | ||
| import {z} from 'zod' | ||
|
|
||
| /** | ||
| * Per-event schema for `swarm_query_completed`. | ||
| * | ||
| * Swarm counterpart to `query_completed` (ENG-2770 / M12) — fires once | ||
| * per `brv swarm query` invocation OR per `swarm_query` LLM tool call, | ||
| * covering the read loop across federated memory providers (byterover, | ||
| * obsidian, gbrain, …) coordinated by `swarm-coordinator.ts`. | ||
| * | ||
| * `swarm_scope` is a short producer-taxonomized string describing which | ||
| * provider set the query spanned: `'local'` (current project only), | ||
| * `'remote'` (external providers only), or `'mixed'` (both). Kept as | ||
| * `z.string().min(1).max(64)` so future scope kinds plug in without a | ||
| * schema migration; the producer is responsible for the taxonomy. | ||
| * | ||
| * `tags` / `keywords` / `related` mirror the M12.3 frontmatter-harvest | ||
| * precedent — when the query fuses results from a Memory-Wiki adapter | ||
| * that carries those fields, surface them so the funnel stays comparable | ||
| * to the in-project `query_completed` events. | ||
| * | ||
| * Per the M15.1 outcome taxonomy: `outcome: 'success' | 'failure'`, | ||
| * `failure_kind` populated only on failure. `duration_ms` is required | ||
| * because the coordinator always knows it by terminal time. | ||
| * | ||
| * SCHEMA-ONLY REGISTRATION TODAY: the swarm query surface lives in the | ||
| * agent process (`src/agent/infra/swarm/swarm-coordinator.ts`), not in | ||
| * a daemon transport handler. Emit wiring deferred per plan flag #2. | ||
| */ | ||
| const failureKindSchema = z.string().min(1).max(64).optional() | ||
| const stringArraySchema = z.array(z.string().max(256)).max(50).optional() | ||
|
|
||
| export const SwarmQueryCompletedSchema = z | ||
| .object({ | ||
| duration_ms: z.number().int().nonnegative(), | ||
| failure_kind: failureKindSchema, | ||
| /** Optional frontmatter harvest (M12.3 parity) for the top-N fused results. */ | ||
| keywords: stringArraySchema, | ||
| outcome: z.enum(['success', 'failure']), | ||
| related: stringArraySchema, | ||
| /** Number of fused results returned to the caller. */ | ||
| result_count: z.number().int().nonnegative().optional(), | ||
| /** Provider-set kind ('local' | 'remote' | 'mixed' | …). Producer-taxonomized. */ | ||
| swarm_scope: z.string().min(1).max(64).optional(), | ||
| tags: stringArraySchema, | ||
| }) | ||
| .strict() | ||
|
|
||
| export type SwarmQueryCompletedProps = z.infer<typeof SwarmQueryCompletedSchema> | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.