opencode-swarm 7.51.6 → 7.52.0

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.
@@ -0,0 +1,119 @@
1
+ /**
2
+ * Telemetry recording and persistence for the Context Capsule feature.
3
+ *
4
+ * Records capsule telemetry data (token estimates, cache hit/miss counts,
5
+ * stale summary counts) per delegation. Aggregates telemetry written to
6
+ * `.swarm/context-telemetry.jsonl` for user inspection.
7
+ *
8
+ * All functions are synchronous for simplicity and reliability. The module
9
+ * uses the `_internals` DI seam pattern so tests can override filesystem
10
+ * operations without `mock.module` (which leaks across files in Bun's
11
+ * shared test-runner process).
12
+ *
13
+ * State lives exclusively under `.swarm/` (Invariant 4). No `process.cwd()`
14
+ * usage — every function accepts an explicit `directory` parameter.
15
+ *
16
+ * No `bun:` imports — this module is Node-ESM-loadable (Invariant 2).
17
+ *
18
+ * See issue #1104, FR-007.
19
+ */
20
+ import * as fs from 'node:fs';
21
+ /**
22
+ * A single telemetry record for one capsule delegation.
23
+ * Written as one JSON line to `.swarm/context-telemetry.jsonl`.
24
+ */
25
+ export interface TelemetryEntry {
26
+ /** ISO 8601 timestamp of when the telemetry was recorded */
27
+ timestamp: string;
28
+ /** Task ID the capsule was generated for (e.g. "1.1", "2.3") */
29
+ task_id: string;
30
+ /** Agent role that received the capsule */
31
+ agent_role: string;
32
+ /** Why the capsule was generated */
33
+ delegation_reason: string;
34
+ /** Estimated token count of the capsule content */
35
+ token_estimate: number;
36
+ /** Number of entries reused from the context map cache */
37
+ cache_hits: number;
38
+ /** Number of entries that required fresh computation */
39
+ cache_misses: number;
40
+ /** Number of stale entries detected during generation */
41
+ stale_entries: number;
42
+ /** Number of files the agent should read directly */
43
+ recommended_reads: number;
44
+ /** Number of files whose summaries were sufficient */
45
+ skipped_reads: number;
46
+ /** Whether capsule generation succeeded */
47
+ success: boolean;
48
+ }
49
+ /**
50
+ * Aggregate statistics computed from all telemetry entries.
51
+ * Returned by {@link getTelemetrySummary}.
52
+ */
53
+ export interface TelemetrySummary {
54
+ /** Total number of capsule delegations recorded */
55
+ total_delegations: number;
56
+ /** Sum of all cache hits across entries */
57
+ total_cache_hits: number;
58
+ /** Sum of all cache misses across entries */
59
+ total_cache_misses: number;
60
+ /** Sum of all stale entries detected across entries */
61
+ total_stale_entries: number;
62
+ /** Average estimated token count across entries */
63
+ avg_token_estimate: number;
64
+ /** Sum of all recommended reads across entries */
65
+ total_recommended_reads: number;
66
+ /** Sum of all skipped reads across entries */
67
+ total_skipped_reads: number;
68
+ /** Percentage of successful capsule generations (0–100) */
69
+ success_rate: number;
70
+ }
71
+ /**
72
+ * Test-only dependency-injection seam. Production code calls through this
73
+ * object so tests can replace the underlying implementations without
74
+ * `mock.module` (which leaks across files in Bun's shared test-runner process).
75
+ * Mutating this local object is file-scoped and trivially restorable
76
+ * via `afterEach`.
77
+ */
78
+ export declare const _internals: {
79
+ readonly appendFileSync: typeof fs.appendFileSync;
80
+ readonly readFileSync: typeof fs.readFileSync;
81
+ readonly existsSync: typeof fs.existsSync;
82
+ readonly mkdirSync: typeof fs.mkdirSync;
83
+ };
84
+ /**
85
+ * Append a telemetry entry to `.swarm/context-telemetry.jsonl`.
86
+ *
87
+ * Creates the `.swarm/` directory if it does not exist. The entry is
88
+ * serialized as a single JSON line and appended to the file.
89
+ *
90
+ * Returns `true` on success, `false` on any error. Never throws.
91
+ *
92
+ * @param entry - The telemetry record to append
93
+ * @param directory - Project root directory (must contain `.swarm/`)
94
+ */
95
+ export declare function recordTelemetry(entry: TelemetryEntry, directory: string): boolean;
96
+ /**
97
+ * Read all telemetry entries from `.swarm/context-telemetry.jsonl`.
98
+ *
99
+ * Parses each line as JSON and returns the array of valid entries.
100
+ * Malformed lines are silently skipped. Returns an empty array if the
101
+ * file does not exist. Never throws.
102
+ *
103
+ * @param directory - Project root directory
104
+ * @returns Array of parsed telemetry entries (empty if none or on error)
105
+ */
106
+ export declare function readTelemetry(directory: string): TelemetryEntry[];
107
+ /**
108
+ * Compute aggregate statistics from all telemetry entries.
109
+ *
110
+ * Reads all entries via {@link readTelemetry}, then computes totals
111
+ * and averages across the dataset.
112
+ *
113
+ * Returns a summary with zeroed fields if no entries exist or on error.
114
+ * Never throws.
115
+ *
116
+ * @param directory - Project root directory
117
+ * @returns Aggregate telemetry summary
118
+ */
119
+ export declare function getTelemetrySummary(directory: string): TelemetrySummary;
@@ -21,6 +21,8 @@ export interface CouncilFinding {
21
21
  export interface CouncilMemberVerdict {
22
22
  agent: CouncilAgent;
23
23
  verdict: CouncilVerdict;
24
+ /** Optional source round for freshness checks on resubmission. */
25
+ verdictRound?: number;
24
26
  /** Confidence 0.0–1.0 */
25
27
  confidence: number;
26
28
  findings: CouncilFinding[];
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Context Capsule Injection Hook
3
+ *
4
+ * Intercepts architect-to-agent delegations and injects a role-specific
5
+ * Context Capsule into the delegated agent's system message. The capsule
6
+ * provides file summaries, read policies, and task context so the agent
7
+ * can begin work with focused, relevant context.
8
+ *
9
+ * This hook is part of the Context Map feature (issue #1104, FR-005).
10
+ * It is **opt-in** — disabled by default. Enable via config:
11
+ *
12
+ * context_map: { enabled: true }
13
+ *
14
+ * Uses the `_internals` DI seam pattern so tests can override external
15
+ * dependencies without `mock.module` (which leaks across files in Bun's
16
+ * shared test-runner process).
17
+ *
18
+ * All functions accept an explicit `directory` parameter (Invariant 4).
19
+ * Uses injected directory parameter instead of cwd. Never throws — wraps everything in try/catch.
20
+ * No `bun:` imports — Node-ESM-loadable (Invariant 2).
21
+ */
22
+ import type { PluginConfig } from '../config/index.js';
23
+ import { buildCapsule } from '../context-map/capsule-builder.js';
24
+ import { saveCapsule } from '../context-map/capsule-persistence.js';
25
+ import { recordTelemetry } from '../context-map/telemetry.js';
26
+ import type { AgentSessionState } from '../state.js';
27
+ import type { CapsuleDelegationReason } from '../types/context-capsule.js';
28
+ /**
29
+ * Map session state to a CapsuleDelegationReason.
30
+ *
31
+ * Infers the delegation reason from the agent role and task workflow state.
32
+ * Uses `taskWorkflowStates` to detect whether this is a re-delegation after
33
+ * a reviewer rejection or test failure. Falls back to 'new_task' for
34
+ * initial delegations where the task is still at 'idle' or 'coder_delegated'.
35
+ *
36
+ * Note: `lastDelegationReason` is not used because the delegation tracker
37
+ * overwrites it to 'normal_delegation' before this hook runs.
38
+ * `lastGateFailure` is not used because it tracks automated tool failures,
39
+ * not agent rejection — its tool name is always the automated tool (diff,
40
+ * lint, pre_check_batch), never 'reviewer' or 'test_engineer'.
41
+ */
42
+ declare function resolveCapsuleDelegationReason(session: AgentSessionState | undefined, agentRole: string, taskId: string): CapsuleDelegationReason;
43
+ /**
44
+ * Extract the task description (goal) from plan.json for a given task ID.
45
+ *
46
+ * Reads `.swarm/plan.json` synchronously and searches the phases[].tasks[]
47
+ * array for a matching task ID. Returns the task description, or '' if not
48
+ * found or on any error. Never throws.
49
+ */
50
+ declare function extractTaskGoal(taskId: string, directory: string): string;
51
+ /**
52
+ * Test-only dependency-injection seam. Production code calls through this
53
+ * object so tests can replace the underlying implementations without
54
+ * `mock.module`. Mutating this object is file-scoped and trivially
55
+ * restorable via `afterEach`.
56
+ */
57
+ export declare const _internals: {
58
+ buildCapsule: typeof buildCapsule;
59
+ recordTelemetry: typeof recordTelemetry;
60
+ saveCapsule: typeof saveCapsule;
61
+ getActiveAgent: (sessionID: string) => string | undefined;
62
+ getSession: (sessionID: string) => AgentSessionState | undefined;
63
+ getCurrentTaskId: (sessionID: string) => string | null;
64
+ readScopeFile: (taskId: string, dir: string) => string[];
65
+ resolveCapsuleDelegationReason: typeof resolveCapsuleDelegationReason;
66
+ extractTaskGoal: typeof extractTaskGoal;
67
+ };
68
+ /**
69
+ * Creates the context capsule injection hook.
70
+ *
71
+ * When `context_map.enabled === true`, this hook intercepts system message
72
+ * transforms for delegated agent sessions and injects a role-specific
73
+ * Context Capsule containing file summaries and read policies.
74
+ *
75
+ * When disabled (the default), returns an empty hook object — zero overhead.
76
+ */
77
+ export declare function createContextCapsuleInjectHook(config: PluginConfig, directory: string): Record<string, unknown>;
78
+ export {};
@@ -2,6 +2,7 @@ export { createAgentActivityHooks } from './agent-activity';
2
2
  export { createCcCommandInterceptHook } from './cc-command-intercept';
3
3
  export { createCompactionCustomizerHook } from './compaction-customizer';
4
4
  export { createContextBudgetHandler } from './context-budget';
5
+ export { createContextCapsuleInjectHook } from './context-capsule-inject';
5
6
  export { createCuratorLLMDelegate } from './curator-llm-factory';
6
7
  export { createDelegationGateHook } from './delegation-gate';
7
8
  export { createDelegationSanitizerHook } from './delegation-sanitizer';