opencode-swarm 6.14.11 → 6.14.12

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.
@@ -99,6 +99,7 @@ export declare const ContextBudgetConfigSchema: z.ZodObject<{
99
99
  critical_threshold: z.ZodDefault<z.ZodNumber>;
100
100
  model_limits: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodNumber>>;
101
101
  max_injection_tokens: z.ZodDefault<z.ZodNumber>;
102
+ tracked_agents: z.ZodDefault<z.ZodArray<z.ZodString>>;
102
103
  scoring: z.ZodOptional<z.ZodObject<{
103
104
  enabled: z.ZodDefault<z.ZodBoolean>;
104
105
  max_candidates: z.ZodDefault<z.ZodNumber>;
@@ -126,6 +127,12 @@ export declare const ContextBudgetConfigSchema: z.ZodObject<{
126
127
  json: z.ZodDefault<z.ZodNumber>;
127
128
  }, z.core.$strip>>;
128
129
  }, z.core.$strip>>;
130
+ enforce: z.ZodDefault<z.ZodBoolean>;
131
+ prune_target: z.ZodDefault<z.ZodNumber>;
132
+ preserve_last_n_turns: z.ZodDefault<z.ZodNumber>;
133
+ recent_window: z.ZodDefault<z.ZodNumber>;
134
+ enforce_on_agent_switch: z.ZodDefault<z.ZodBoolean>;
135
+ tool_output_mask_threshold: z.ZodDefault<z.ZodNumber>;
129
136
  }, z.core.$strip>;
130
137
  export type ContextBudgetConfig = z.infer<typeof ContextBudgetConfigSchema>;
131
138
  export declare const EvidenceConfigSchema: z.ZodObject<{
@@ -451,6 +458,7 @@ export declare const PluginConfigSchema: z.ZodObject<{
451
458
  critical_threshold: z.ZodDefault<z.ZodNumber>;
452
459
  model_limits: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodNumber>>;
453
460
  max_injection_tokens: z.ZodDefault<z.ZodNumber>;
461
+ tracked_agents: z.ZodDefault<z.ZodArray<z.ZodString>>;
454
462
  scoring: z.ZodOptional<z.ZodObject<{
455
463
  enabled: z.ZodDefault<z.ZodBoolean>;
456
464
  max_candidates: z.ZodDefault<z.ZodNumber>;
@@ -478,6 +486,12 @@ export declare const PluginConfigSchema: z.ZodObject<{
478
486
  json: z.ZodDefault<z.ZodNumber>;
479
487
  }, z.core.$strip>>;
480
488
  }, z.core.$strip>>;
489
+ enforce: z.ZodDefault<z.ZodBoolean>;
490
+ prune_target: z.ZodDefault<z.ZodNumber>;
491
+ preserve_last_n_turns: z.ZodDefault<z.ZodNumber>;
492
+ recent_window: z.ZodDefault<z.ZodNumber>;
493
+ enforce_on_agent_switch: z.ZodDefault<z.ZodBoolean>;
494
+ tool_output_mask_threshold: z.ZodDefault<z.ZodNumber>;
481
495
  }, z.core.$strip>>;
482
496
  guardrails: z.ZodOptional<z.ZodObject<{
483
497
  enabled: z.ZodDefault<z.ZodBoolean>;
@@ -10,6 +10,9 @@ interface MessageInfo {
10
10
  role: string;
11
11
  agent?: string;
12
12
  sessionID?: string;
13
+ modelID?: string;
14
+ providerID?: string;
15
+ [key: string]: unknown;
13
16
  }
14
17
  interface MessagePart {
15
18
  type: string;
@@ -5,7 +5,9 @@ export { createDelegationGateHook } from './delegation-gate';
5
5
  export { createDelegationTrackerHook } from './delegation-tracker';
6
6
  export { extractCurrentPhase, extractCurrentPhaseFromPlan, extractCurrentTask, extractCurrentTaskFromPlan, extractDecisions, extractIncompleteTasks, extractIncompleteTasksFromPlan, extractPatterns, } from './extractors';
7
7
  export { createGuardrailsHooks } from './guardrails';
8
+ export { classifyMessage, classifyMessages, containsPlanContent, isDuplicateToolRead, isStaleError, isToolResult, MessagePriority, type MessagePriorityType, type MessageWithParts, } from './message-priority';
8
9
  export { consolidateSystemMessages } from './messages-transform';
10
+ export { extractModelInfo, NATIVE_MODEL_LIMITS, PROVIDER_CAPS, resolveModelLimit, } from './model-limits';
9
11
  export { createPhaseMonitorHook } from './phase-monitor';
10
12
  export { createPipelineTrackerHook } from './pipeline-tracker';
11
13
  export { createSystemEnhancerHook } from './system-enhancer';
@@ -0,0 +1,105 @@
1
+ /**
2
+ * Message Priority Classifier Hook
3
+ *
4
+ * Provides zero-cost message priority classification to enable intelligent
5
+ * context pruning. Messages are tagged with priority tiers (0-4) so that
6
+ * low-priority messages are removed first during context budget pressure.
7
+ *
8
+ * Priority tiers:
9
+ * - CRITICAL (0): System prompt, plan state, active instructions
10
+ * - HIGH (1): User messages, current task context, tool definitions
11
+ * - MEDIUM (2): Recent assistant responses, recent tool results
12
+ * - LOW (3): Old assistant responses, old tool results, confirmations
13
+ * - DISPOSABLE (4): Duplicate reads, superseded writes, stale errors
14
+ */
15
+ /**
16
+ * Message priority tiers for context pruning decisions.
17
+ * Lower values = higher priority (kept longer during pruning).
18
+ */
19
+ export declare const MessagePriority: {
20
+ /** System prompt, plan state, active instructions - never prune */
21
+ readonly CRITICAL: 0;
22
+ /** User messages, current task context, tool definitions */
23
+ readonly HIGH: 1;
24
+ /** Recent assistant responses, recent tool results (within recentWindowSize) */
25
+ readonly MEDIUM: 2;
26
+ /** Old assistant responses, old tool results */
27
+ readonly LOW: 3;
28
+ /** Duplicate reads, superseded writes, stale errors - prune first */
29
+ readonly DISPOSABLE: 4;
30
+ };
31
+ export type MessagePriorityType = (typeof MessagePriority)[keyof typeof MessagePriority];
32
+ /** Message structure matching the format from context-budget.ts */
33
+ interface MessageInfo {
34
+ role?: string;
35
+ agent?: string;
36
+ sessionID?: string;
37
+ modelID?: string;
38
+ providerID?: string;
39
+ toolName?: string;
40
+ toolArgs?: unknown;
41
+ [key: string]: unknown;
42
+ }
43
+ interface MessagePart {
44
+ type?: string;
45
+ text?: string;
46
+ [key: string]: unknown;
47
+ }
48
+ export interface MessageWithParts {
49
+ info?: MessageInfo;
50
+ parts?: MessagePart[];
51
+ }
52
+ /**
53
+ * Checks if text contains .swarm/plan or .swarm/context references
54
+ * indicating swarm state that should be preserved.
55
+ *
56
+ * @param text - The text content to check
57
+ * @returns true if the text contains plan/context references
58
+ */
59
+ export declare function containsPlanContent(text: string): boolean;
60
+ /**
61
+ * Checks if a message is a tool result (assistant message with tool call).
62
+ *
63
+ * @param message - The message to check
64
+ * @returns true if the message appears to be a tool result
65
+ */
66
+ export declare function isToolResult(message: MessageWithParts): boolean;
67
+ /**
68
+ * Checks if two consecutive tool read calls are duplicates
69
+ * (same tool with same first argument).
70
+ *
71
+ * @param current - The current message
72
+ * @param previous - The previous message
73
+ * @returns true if this is a duplicate tool read
74
+ */
75
+ export declare function isDuplicateToolRead(current: MessageWithParts, previous: MessageWithParts): boolean;
76
+ /**
77
+ * Checks if a message contains an error pattern and is stale
78
+ * (more than the specified number of turns old).
79
+ *
80
+ * @param text - The message text to check
81
+ * @param turnsAgo - How many turns ago the message was sent
82
+ * @returns true if the message is a stale error
83
+ */
84
+ export declare function isStaleError(text: string, turnsAgo: number): boolean;
85
+ /**
86
+ * Classifies a message by priority tier for intelligent pruning.
87
+ *
88
+ * @param message - The message to classify
89
+ * @param index - Position in messages array (0-indexed)
90
+ * @param totalMessages - Total number of messages
91
+ * @param recentWindowSize - Number of recent messages to consider MEDIUM (default 10)
92
+ * @returns Priority tier (0=CRITICAL, 1=HIGH, 2=MEDIUM, 3=LOW, 4=DISPOSABLE)
93
+ */
94
+ export declare function classifyMessage(message: MessageWithParts, index: number, totalMessages: number, recentWindowSize?: number): MessagePriorityType;
95
+ /**
96
+ * Classifies a batch of messages with duplicate detection.
97
+ * This function should be called in order (oldest to newest) to properly
98
+ * detect consecutive duplicate tool reads.
99
+ *
100
+ * @param messages - Array of messages to classify
101
+ * @param recentWindowSize - Number of recent messages to consider MEDIUM (default 10)
102
+ * @returns Array of priority classifications matching message order
103
+ */
104
+ export declare function classifyMessages(messages: MessageWithParts[], recentWindowSize?: number): MessagePriorityType[];
105
+ export {};
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Provider-Aware Model Limit Resolution
3
+ *
4
+ * Resolves context window limits based on the model and provider platform.
5
+ * The same model has different context limits depending on the provider:
6
+ * - Claude Sonnet 4.6: 200k native, 128k on Copilot
7
+ * - GPT-5: 400k native, 128k on Copilot
8
+ * - Copilot caps ALL models at 128k prompt, regardless of native limit
9
+ */
10
+ /**
11
+ * Native model context limits (in tokens) when used on their native platform.
12
+ */
13
+ export declare const NATIVE_MODEL_LIMITS: Record<string, number>;
14
+ /**
15
+ * Provider-specific context caps that override native limits.
16
+ * These are typically lower than native limits (e.g., Copilot caps at 128k).
17
+ */
18
+ export declare const PROVIDER_CAPS: Record<string, number>;
19
+ /**
20
+ * Message structure from experimental.chat.messages.transform hook.
21
+ */
22
+ interface MessageInfo {
23
+ role: string;
24
+ agent?: string;
25
+ sessionID?: string;
26
+ modelID?: string;
27
+ providerID?: string;
28
+ [key: string]: unknown;
29
+ }
30
+ interface MessagePart {
31
+ type: string;
32
+ text?: string;
33
+ [key: string]: unknown;
34
+ }
35
+ interface MessageWithParts {
36
+ info: MessageInfo;
37
+ parts: MessagePart[];
38
+ }
39
+ /**
40
+ * Extracts modelID and providerID from the most recent assistant message.
41
+ *
42
+ * @param messages - Array of messages from experimental.chat.messages.transform hook
43
+ * @returns Object containing modelID and/or providerID if found
44
+ *
45
+ * @example
46
+ * const info = extractModelInfo(messages);
47
+ * // Returns: { modelID: 'claude-sonnet-4-6', providerID: 'anthropic' }
48
+ * // Or: {} if no assistant messages or fields not found
49
+ */
50
+ export declare function extractModelInfo(messages: MessageWithParts[]): {
51
+ modelID?: string;
52
+ providerID?: string;
53
+ };
54
+ /**
55
+ * Resolves the context limit for a given model/provider combination.
56
+ *
57
+ * Resolution order (first match wins):
58
+ * 1. Check configOverrides["provider/model"] (e.g., "copilot/claude-sonnet-4-6": 200000)
59
+ * 2. Check configOverrides[modelID] (e.g., "claude-sonnet-4-6": 200000)
60
+ * 3. Check PROVIDER_CAPS[providerID] (e.g., copilot → 128000)
61
+ * 4. Check NATIVE_MODEL_LIMITS with prefix matching (e.g., "claude-sonnet-4" matches "claude-sonnet-4-6-20260301")
62
+ * 5. Check configOverrides.default
63
+ * 6. Fall back to 128000
64
+ *
65
+ * @param modelID - The model identifier (e.g., "claude-sonnet-4-6", "gpt-5")
66
+ * @param providerID - The provider identifier (e.g., "copilot", "anthropic")
67
+ * @param configOverrides - User configuration overrides
68
+ * @returns The resolved context limit in tokens
69
+ *
70
+ * @example
71
+ * // Provider cap (copilot)
72
+ * resolveModelLimit("claude-sonnet-4-6", "copilot", {})
73
+ * // Returns: 128000
74
+ *
75
+ * @example
76
+ * // Native limit (anthropic)
77
+ * resolveModelLimit("claude-sonnet-4-6", "anthropic", {})
78
+ * // Returns: 200000
79
+ *
80
+ * @example
81
+ * // Override beats cap
82
+ * resolveModelLimit("gpt-5", "copilot", { "copilot/gpt-5": 200000 })
83
+ * // Returns: 200000
84
+ *
85
+ * @example
86
+ * // Prefix match for model variants
87
+ * resolveModelLimit("claude-sonnet-4-6-20260301", "anthropic", {})
88
+ * // Returns: 200000
89
+ *
90
+ * @example
91
+ * // Full fallback
92
+ * resolveModelLimit(undefined, undefined, {})
93
+ * // Returns: 128000
94
+ */
95
+ export declare function resolveModelLimit(modelID?: string, providerID?: string, configOverrides?: Record<string, number>): number;
96
+ export {};
package/dist/index.js CHANGED
@@ -14341,6 +14341,12 @@ function validateSwarmPath(directory, filename) {
14341
14341
  if (/\.\.[/\\]/.test(filename)) {
14342
14342
  throw new Error("Invalid filename: path traversal detected");
14343
14343
  }
14344
+ if (/^[A-Za-z]:[\\/]/.test(filename)) {
14345
+ throw new Error("Invalid filename: path escapes .swarm directory");
14346
+ }
14347
+ if (filename.startsWith("/")) {
14348
+ throw new Error("Invalid filename: path escapes .swarm directory");
14349
+ }
14344
14350
  const baseDir = path2.normalize(path2.resolve(directory, ".swarm"));
14345
14351
  const resolved = path2.normalize(path2.resolve(baseDir, filename));
14346
14352
  if (process.platform === "win32") {
@@ -31794,7 +31800,14 @@ var ContextBudgetConfigSchema = exports_external.object({
31794
31800
  critical_threshold: exports_external.number().min(0).max(1).default(0.9),
31795
31801
  model_limits: exports_external.record(exports_external.string(), exports_external.number().min(1000)).default({ default: 128000 }),
31796
31802
  max_injection_tokens: exports_external.number().min(100).max(50000).default(4000),
31797
- scoring: ScoringConfigSchema.optional()
31803
+ tracked_agents: exports_external.array(exports_external.string()).default(["architect"]),
31804
+ scoring: ScoringConfigSchema.optional(),
31805
+ enforce: exports_external.boolean().default(true),
31806
+ prune_target: exports_external.number().min(0).max(1).default(0.7),
31807
+ preserve_last_n_turns: exports_external.number().min(0).max(100).default(4),
31808
+ recent_window: exports_external.number().min(1).max(100).default(10),
31809
+ enforce_on_agent_switch: exports_external.boolean().default(true),
31810
+ tool_output_mask_threshold: exports_external.number().min(100).max(1e5).default(2000)
31798
31811
  });
31799
31812
  var EvidenceConfigSchema = exports_external.object({
31800
31813
  enabled: exports_external.boolean().default(true),
@@ -36698,8 +36711,232 @@ function createCompactionCustomizerHook(config3, directory) {
36698
36711
  })
36699
36712
  };
36700
36713
  }
36714
+ // src/hooks/context-budget.ts
36715
+ init_utils();
36716
+
36717
+ // src/hooks/message-priority.ts
36718
+ var MessagePriority = {
36719
+ CRITICAL: 0,
36720
+ HIGH: 1,
36721
+ MEDIUM: 2,
36722
+ LOW: 3,
36723
+ DISPOSABLE: 4
36724
+ };
36725
+ function containsPlanContent(text) {
36726
+ if (!text)
36727
+ return false;
36728
+ const lowerText = text.toLowerCase();
36729
+ return lowerText.includes(".swarm/plan") || lowerText.includes(".swarm/context") || lowerText.includes("swarm/plan.md") || lowerText.includes("swarm/context.md");
36730
+ }
36731
+ function isToolResult(message) {
36732
+ if (!message?.info)
36733
+ return false;
36734
+ const role = message.info.role;
36735
+ const toolName = message.info.toolName;
36736
+ return role === "assistant" && !!toolName;
36737
+ }
36738
+ function isDuplicateToolRead(current, previous) {
36739
+ if (!current?.info || !previous?.info)
36740
+ return false;
36741
+ const currentTool = current.info.toolName;
36742
+ const previousTool = previous.info.toolName;
36743
+ if (currentTool !== previousTool)
36744
+ return false;
36745
+ const isReadTool = currentTool?.toLowerCase().includes("read") && previousTool?.toLowerCase().includes("read");
36746
+ if (!isReadTool)
36747
+ return false;
36748
+ const currentArgs = current.info.toolArgs;
36749
+ const previousArgs = previous.info.toolArgs;
36750
+ if (!currentArgs || !previousArgs)
36751
+ return false;
36752
+ const currentKeys = Object.keys(currentArgs);
36753
+ const previousKeys = Object.keys(previousArgs);
36754
+ if (currentKeys.length === 0 || previousKeys.length === 0)
36755
+ return false;
36756
+ const firstKey = currentKeys[0];
36757
+ return currentArgs[firstKey] === previousArgs[firstKey];
36758
+ }
36759
+ function isStaleError(text, turnsAgo) {
36760
+ if (!text)
36761
+ return false;
36762
+ if (turnsAgo <= 6)
36763
+ return false;
36764
+ const lowerText = text.toLowerCase();
36765
+ const errorPatterns = [
36766
+ "error:",
36767
+ "failed to",
36768
+ "could not",
36769
+ "unable to",
36770
+ "exception",
36771
+ "errno",
36772
+ "cannot read",
36773
+ "not found",
36774
+ "access denied",
36775
+ "timeout"
36776
+ ];
36777
+ return errorPatterns.some((pattern) => lowerText.includes(pattern));
36778
+ }
36779
+ function extractMessageText(message) {
36780
+ if (!message?.parts || message.parts.length === 0)
36781
+ return "";
36782
+ return message.parts.map((part) => part?.text || "").join("");
36783
+ }
36784
+ function classifyMessage(message, index, totalMessages, recentWindowSize = 10) {
36785
+ const role = message?.info?.role;
36786
+ const text = extractMessageText(message);
36787
+ if (containsPlanContent(text)) {
36788
+ return MessagePriority.CRITICAL;
36789
+ }
36790
+ if (role === "system") {
36791
+ return MessagePriority.CRITICAL;
36792
+ }
36793
+ if (role === "user") {
36794
+ return MessagePriority.HIGH;
36795
+ }
36796
+ if (isToolResult(message)) {
36797
+ const positionFromEnd = totalMessages - 1 - index;
36798
+ if (positionFromEnd < recentWindowSize) {
36799
+ return MessagePriority.MEDIUM;
36800
+ }
36801
+ if (isStaleError(text, positionFromEnd)) {
36802
+ return MessagePriority.DISPOSABLE;
36803
+ }
36804
+ return MessagePriority.LOW;
36805
+ }
36806
+ if (role === "assistant") {
36807
+ const positionFromEnd = totalMessages - 1 - index;
36808
+ if (positionFromEnd < recentWindowSize) {
36809
+ return MessagePriority.MEDIUM;
36810
+ }
36811
+ if (isStaleError(text, positionFromEnd)) {
36812
+ return MessagePriority.DISPOSABLE;
36813
+ }
36814
+ return MessagePriority.LOW;
36815
+ }
36816
+ return MessagePriority.LOW;
36817
+ }
36818
+ function classifyMessages(messages, recentWindowSize = 10) {
36819
+ const results = [];
36820
+ const totalMessages = messages.length;
36821
+ for (let i2 = 0;i2 < messages.length; i2++) {
36822
+ const message = messages[i2];
36823
+ const priority = classifyMessage(message, i2, totalMessages, recentWindowSize);
36824
+ if (i2 > 0) {
36825
+ const current = messages[i2];
36826
+ const previous = messages[i2 - 1];
36827
+ if (isDuplicateToolRead(current, previous)) {
36828
+ if (results[i2 - 1] >= MessagePriority.MEDIUM) {
36829
+ results[i2 - 1] = MessagePriority.DISPOSABLE;
36830
+ }
36831
+ }
36832
+ }
36833
+ results.push(priority);
36834
+ }
36835
+ return results;
36836
+ }
36837
+
36838
+ // src/hooks/model-limits.ts
36839
+ init_utils();
36840
+ var NATIVE_MODEL_LIMITS = {
36841
+ "claude-sonnet-4": 200000,
36842
+ "claude-opus-4": 200000,
36843
+ "claude-haiku-4": 200000,
36844
+ "gpt-5": 400000,
36845
+ "gpt-5.1-codex": 400000,
36846
+ "gpt-5.1": 264000,
36847
+ "gpt-4.1": 1047576,
36848
+ "gemini-2.5-pro": 1048576,
36849
+ "gemini-2.5-flash": 1048576,
36850
+ o3: 200000,
36851
+ "o4-mini": 200000,
36852
+ "deepseek-r1": 163840,
36853
+ "deepseek-chat": 163840,
36854
+ "qwen3.5": 131072
36855
+ };
36856
+ var PROVIDER_CAPS = {
36857
+ copilot: 128000,
36858
+ "github-copilot": 128000
36859
+ };
36860
+ function extractModelInfo(messages) {
36861
+ if (!messages || messages.length === 0) {
36862
+ return {};
36863
+ }
36864
+ for (let i2 = messages.length - 1;i2 >= 0; i2--) {
36865
+ const message = messages[i2];
36866
+ if (!message?.info)
36867
+ continue;
36868
+ if (message.info.role === "assistant") {
36869
+ const modelID = message.info.modelID;
36870
+ const providerID = message.info.providerID;
36871
+ if (modelID || providerID) {
36872
+ return {
36873
+ ...modelID ? { modelID } : {},
36874
+ ...providerID ? { providerID } : {}
36875
+ };
36876
+ }
36877
+ }
36878
+ }
36879
+ return {};
36880
+ }
36881
+ var loggedFirstCalls = new Set;
36882
+ function resolveModelLimit(modelID, providerID, configOverrides = {}) {
36883
+ const normalizedModelID = modelID ?? "";
36884
+ const normalizedProviderID = providerID ?? "";
36885
+ if (normalizedProviderID && normalizedModelID) {
36886
+ const providerModelKey = `${normalizedProviderID}/${normalizedModelID}`;
36887
+ if (configOverrides[providerModelKey] !== undefined) {
36888
+ logFirstCall(normalizedModelID, normalizedProviderID, "override(provider/model)", configOverrides[providerModelKey]);
36889
+ return configOverrides[providerModelKey];
36890
+ }
36891
+ }
36892
+ if (normalizedModelID && configOverrides[normalizedModelID] !== undefined) {
36893
+ logFirstCall(normalizedModelID, normalizedProviderID, "override(model)", configOverrides[normalizedModelID]);
36894
+ return configOverrides[normalizedModelID];
36895
+ }
36896
+ if (normalizedProviderID && PROVIDER_CAPS[normalizedProviderID] !== undefined) {
36897
+ const cap = PROVIDER_CAPS[normalizedProviderID];
36898
+ logFirstCall(normalizedModelID, normalizedProviderID, "provider_cap", cap);
36899
+ return cap;
36900
+ }
36901
+ if (normalizedModelID) {
36902
+ const matchedLimit = findNativeLimit(normalizedModelID);
36903
+ if (matchedLimit !== undefined) {
36904
+ logFirstCall(normalizedModelID, normalizedProviderID, "native", matchedLimit);
36905
+ return matchedLimit;
36906
+ }
36907
+ }
36908
+ if (configOverrides.default !== undefined) {
36909
+ logFirstCall(normalizedModelID, normalizedProviderID, "default_override", configOverrides.default);
36910
+ return configOverrides.default;
36911
+ }
36912
+ logFirstCall(normalizedModelID, normalizedProviderID, "fallback", 128000);
36913
+ return 128000;
36914
+ }
36915
+ function findNativeLimit(modelID) {
36916
+ if (NATIVE_MODEL_LIMITS[modelID] !== undefined) {
36917
+ return NATIVE_MODEL_LIMITS[modelID];
36918
+ }
36919
+ let bestMatch;
36920
+ for (const key of Object.keys(NATIVE_MODEL_LIMITS)) {
36921
+ if (modelID.startsWith(key)) {
36922
+ if (!bestMatch || key.length > bestMatch.length) {
36923
+ bestMatch = key;
36924
+ }
36925
+ }
36926
+ }
36927
+ return bestMatch ? NATIVE_MODEL_LIMITS[bestMatch] : undefined;
36928
+ }
36929
+ function logFirstCall(modelID, providerID, source, limit) {
36930
+ const key = `${modelID || "unknown"}::${providerID || "unknown"}`;
36931
+ if (!loggedFirstCalls.has(key)) {
36932
+ loggedFirstCalls.add(key);
36933
+ warn(`[model-limits] Resolved limit for ${modelID || "(no model)"}@${providerID || "(no provider)"}: ${limit} (source: ${source})`);
36934
+ }
36935
+ }
36936
+
36701
36937
  // src/hooks/context-budget.ts
36702
36938
  init_utils2();
36939
+ var lastSeenAgent;
36703
36940
  function createContextBudgetHandler(config3) {
36704
36941
  const enabled = config3.context_budget?.enabled !== false;
36705
36942
  if (!enabled) {
@@ -36707,14 +36944,19 @@ function createContextBudgetHandler(config3) {
36707
36944
  }
36708
36945
  const warnThreshold = config3.context_budget?.warn_threshold ?? 0.7;
36709
36946
  const criticalThreshold = config3.context_budget?.critical_threshold ?? 0.9;
36710
- const modelLimits = config3.context_budget?.model_limits ?? {
36711
- default: 128000
36712
- };
36713
- const modelLimit = modelLimits.default ?? 128000;
36714
- return async (_input, output) => {
36947
+ const modelLimitsConfig = config3.context_budget?.model_limits ?? {};
36948
+ const loggedLimits = new Set;
36949
+ const handler = async (_input, output) => {
36715
36950
  const messages = output?.messages;
36716
36951
  if (!messages || messages.length === 0)
36717
36952
  return;
36953
+ const { modelID, providerID } = extractModelInfo(messages);
36954
+ const modelLimit = resolveModelLimit(modelID, providerID, modelLimitsConfig);
36955
+ const cacheKey = `${modelID || "unknown"}::${providerID || "unknown"}`;
36956
+ if (!loggedLimits.has(cacheKey)) {
36957
+ loggedLimits.add(cacheKey);
36958
+ warn(`[swarm] Context budget: model=${modelID || "unknown"} provider=${providerID || "unknown"} limit=${modelLimit}`);
36959
+ }
36718
36960
  let totalTokens = 0;
36719
36961
  for (const message of messages) {
36720
36962
  if (!message?.parts)
@@ -36726,6 +36968,79 @@ function createContextBudgetHandler(config3) {
36726
36968
  }
36727
36969
  }
36728
36970
  const usagePercent = totalTokens / modelLimit;
36971
+ let baseAgent;
36972
+ for (let i2 = messages.length - 1;i2 >= 0; i2--) {
36973
+ const msg = messages[i2];
36974
+ if (msg?.info?.role === "user" && msg?.info?.agent) {
36975
+ baseAgent = stripKnownSwarmPrefix(msg.info.agent);
36976
+ break;
36977
+ }
36978
+ }
36979
+ let ratio = usagePercent;
36980
+ if (lastSeenAgent !== undefined && baseAgent !== undefined && baseAgent !== lastSeenAgent) {
36981
+ const enforceOnSwitch = config3.context_budget?.enforce_on_agent_switch ?? true;
36982
+ if (enforceOnSwitch && usagePercent > (config3.context_budget?.warn_threshold ?? 0.7)) {
36983
+ warn(`[swarm] Agent switch detected: ${lastSeenAgent} \u2192 ${baseAgent}, enforcing context budget`, {
36984
+ from: lastSeenAgent,
36985
+ to: baseAgent
36986
+ });
36987
+ ratio = 1;
36988
+ }
36989
+ }
36990
+ lastSeenAgent = baseAgent;
36991
+ if (ratio >= criticalThreshold) {
36992
+ const enforce = config3.context_budget?.enforce ?? true;
36993
+ if (enforce) {
36994
+ const targetTokens = modelLimit * (config3.context_budget?.prune_target ?? 0.7);
36995
+ const recentWindow = config3.context_budget?.recent_window ?? 10;
36996
+ const priorities = classifyMessages(output.messages || [], recentWindow);
36997
+ const toolMaskThreshold = config3.context_budget?.tool_output_mask_threshold ?? 2000;
36998
+ let toolMaskFreedTokens = 0;
36999
+ const maskedIndices = new Set;
37000
+ for (let i2 = 0;i2 < (output.messages || []).length; i2++) {
37001
+ const msg = (output.messages || [])[i2];
37002
+ if (shouldMaskToolOutput(msg, i2, (output.messages || []).length, recentWindow, toolMaskThreshold)) {
37003
+ toolMaskFreedTokens += maskToolOutput(msg, toolMaskThreshold);
37004
+ maskedIndices.add(i2);
37005
+ }
37006
+ }
37007
+ if (toolMaskFreedTokens > 0) {
37008
+ totalTokens -= toolMaskFreedTokens;
37009
+ warn(`[swarm] Tool output masking: masked ${maskedIndices.size} tool results, freed ~${toolMaskFreedTokens} tokens`, {
37010
+ maskedCount: maskedIndices.size,
37011
+ freedTokens: toolMaskFreedTokens
37012
+ });
37013
+ }
37014
+ const preserveLastNTurns = config3.context_budget?.preserve_last_n_turns ?? 4;
37015
+ const removableMessages = identifyRemovableMessages(output.messages || [], priorities, preserveLastNTurns);
37016
+ let freedTokens = 0;
37017
+ const toRemove = new Set;
37018
+ for (const idx of removableMessages) {
37019
+ if (totalTokens - freedTokens <= targetTokens)
37020
+ break;
37021
+ toRemove.add(idx);
37022
+ freedTokens += estimateTokens(extractMessageText2(output.messages[idx]));
37023
+ }
37024
+ const beforeTokens = totalTokens;
37025
+ if (toRemove.size > 0) {
37026
+ const actualFreedTokens = applyObservationMasking(output.messages || [], toRemove);
37027
+ totalTokens -= actualFreedTokens;
37028
+ warn(`[swarm] Context enforcement: pruned ${toRemove.size} messages, freed ${actualFreedTokens} tokens (${beforeTokens}\u2192${totalTokens} of ${modelLimit})`, {
37029
+ pruned: toRemove.size,
37030
+ freedTokens: actualFreedTokens,
37031
+ before: beforeTokens,
37032
+ after: totalTokens,
37033
+ limit: modelLimit
37034
+ });
37035
+ } else if (removableMessages.length === 0 && totalTokens > targetTokens) {
37036
+ warn(`[swarm] Context enforcement: no removable messages found but still ${totalTokens} tokens (target: ${targetTokens})`, {
37037
+ currentTokens: totalTokens,
37038
+ targetTokens,
37039
+ limit: modelLimit
37040
+ });
37041
+ }
37042
+ }
37043
+ }
36729
37044
  let lastUserMessageIndex = -1;
36730
37045
  for (let i2 = messages.length - 1;i2 >= 0; i2--) {
36731
37046
  if (messages[i2]?.info?.role === "user") {
@@ -36738,8 +37053,10 @@ function createContextBudgetHandler(config3) {
36738
37053
  const lastUserMessage = messages[lastUserMessageIndex];
36739
37054
  if (!lastUserMessage?.parts)
36740
37055
  return;
36741
- const agent = lastUserMessage.info?.agent;
36742
- if (agent && agent !== "architect")
37056
+ const trackedAgents = config3.context_budget?.tracked_agents ?? [
37057
+ "architect"
37058
+ ];
37059
+ if (baseAgent && !trackedAgents.includes(baseAgent))
36743
37060
  return;
36744
37061
  const textPartIndex = lastUserMessage.parts.findIndex((p) => p?.type === "text" && p.text !== undefined);
36745
37062
  if (textPartIndex === -1)
@@ -36760,6 +37077,110 @@ function createContextBudgetHandler(config3) {
36760
37077
  lastUserMessage.parts[textPartIndex].text = `${warningText}${originalText}`;
36761
37078
  }
36762
37079
  };
37080
+ return handler;
37081
+ }
37082
+ function identifyRemovableMessages(messages, priorities, preserveLastNTurns) {
37083
+ let turnCount = 0;
37084
+ const protectedIndices = new Set;
37085
+ for (let i2 = messages.length - 1;i2 >= 0 && turnCount < preserveLastNTurns * 2; i2--) {
37086
+ const role = messages[i2]?.info?.role;
37087
+ if (role === "user" || role === "assistant") {
37088
+ protectedIndices.add(i2);
37089
+ if (role === "user")
37090
+ turnCount++;
37091
+ }
37092
+ }
37093
+ let lastUserIdx = -1;
37094
+ let lastAssistantIdx = -1;
37095
+ for (let i2 = messages.length - 1;i2 >= 0; i2--) {
37096
+ const role = messages[i2]?.info?.role;
37097
+ if (role === "user" && lastUserIdx === -1) {
37098
+ lastUserIdx = i2;
37099
+ }
37100
+ if (role === "assistant" && lastAssistantIdx === -1) {
37101
+ lastAssistantIdx = i2;
37102
+ }
37103
+ if (lastUserIdx !== -1 && lastAssistantIdx !== -1)
37104
+ break;
37105
+ }
37106
+ if (lastUserIdx !== -1)
37107
+ protectedIndices.add(lastUserIdx);
37108
+ if (lastAssistantIdx !== -1)
37109
+ protectedIndices.add(lastAssistantIdx);
37110
+ const HIGH = MessagePriority.HIGH;
37111
+ const MEDIUM = MessagePriority.MEDIUM;
37112
+ const LOW = MessagePriority.LOW;
37113
+ const DISPOSABLE = MessagePriority.DISPOSABLE;
37114
+ const byPriority = [[], [], [], [], []];
37115
+ for (let i2 = 0;i2 < priorities.length; i2++) {
37116
+ const priority = priorities[i2];
37117
+ if (!protectedIndices.has(i2) && priority > HIGH) {
37118
+ byPriority[priority].push(i2);
37119
+ }
37120
+ }
37121
+ return [...byPriority[DISPOSABLE], ...byPriority[LOW], ...byPriority[MEDIUM]];
37122
+ }
37123
+ function applyObservationMasking(messages, toRemove) {
37124
+ let actualFreedTokens = 0;
37125
+ for (const idx of toRemove) {
37126
+ const msg = messages[idx];
37127
+ if (msg?.parts) {
37128
+ for (const part of msg.parts) {
37129
+ if (part.type === "text" && part.text) {
37130
+ const originalTokens = estimateTokens(part.text);
37131
+ const placeholder = `[Context pruned \u2014 message from turn ${idx}, ~${originalTokens} tokens freed. Use retrieve_summary if needed.]`;
37132
+ const maskedTokens = estimateTokens(placeholder);
37133
+ part.text = placeholder;
37134
+ actualFreedTokens += originalTokens - maskedTokens;
37135
+ }
37136
+ }
37137
+ }
37138
+ }
37139
+ return actualFreedTokens;
37140
+ }
37141
+ function extractMessageText2(msg) {
37142
+ if (!msg?.parts)
37143
+ return "";
37144
+ return msg.parts.filter((p) => p.type === "text" && p.text).map((p) => p.text).join(`
37145
+ `);
37146
+ }
37147
+ function extractToolName(text) {
37148
+ const match = text.match(/^(read_file|write|edit|apply_patch|task|bun|npm|git|bash|glob|grep|mkdir|cp|mv|rm)\b/i);
37149
+ return match?.[1];
37150
+ }
37151
+ function shouldMaskToolOutput(msg, index, totalMessages, recentWindowSize, threshold) {
37152
+ if (!isToolResult(msg))
37153
+ return false;
37154
+ const text = extractMessageText2(msg);
37155
+ if (text.includes("[Tool output masked") || text.includes("[Context pruned")) {
37156
+ return false;
37157
+ }
37158
+ const toolName = extractToolName(text);
37159
+ if (toolName && ["retrieve_summary", "task"].includes(toolName.toLowerCase())) {
37160
+ return false;
37161
+ }
37162
+ const age = totalMessages - 1 - index;
37163
+ return age > recentWindowSize || text.length > threshold;
37164
+ }
37165
+ function maskToolOutput(msg, threshold) {
37166
+ if (!msg?.parts)
37167
+ return 0;
37168
+ let freedTokens = 0;
37169
+ for (const part of msg.parts) {
37170
+ if (part.type === "text" && part.text) {
37171
+ if (part.text.includes("[Tool output masked") || part.text.includes("[Context pruned")) {
37172
+ continue;
37173
+ }
37174
+ const originalTokens = estimateTokens(part.text);
37175
+ const toolName = extractToolName(part.text) || "unknown";
37176
+ const excerpt = part.text.substring(0, 200).replace(/\n/g, " ");
37177
+ const placeholder = `[Tool output masked \u2014 ${toolName} returned ~${originalTokens} tokens. First 200 chars: "${excerpt}..." Use retrieve_summary if needed.]`;
37178
+ const maskedTokens = estimateTokens(placeholder);
37179
+ part.text = placeholder;
37180
+ freedTokens += originalTokens - maskedTokens;
37181
+ }
37182
+ }
37183
+ return freedTokens;
36763
37184
  }
36764
37185
  // src/hooks/delegation-gate.ts
36765
37186
  function extractTaskLine(text) {
@@ -36988,6 +37409,12 @@ function isSourceCodePath(filePath) {
36988
37409
  ];
36989
37410
  return !nonSourcePatterns.some((pattern) => pattern.test(normalized));
36990
37411
  }
37412
+ function hasTraversalSegments(filePath) {
37413
+ if (!filePath)
37414
+ return false;
37415
+ const normalized = filePath.replace(/\\/g, "/");
37416
+ return normalized.startsWith("..") || normalized.includes("/../") || normalized.endsWith("/..");
37417
+ }
36991
37418
  function isGateTool(toolName) {
36992
37419
  const normalized = toolName.replace(/^[^:]+[:.]/, "");
36993
37420
  const gateTools = [
@@ -37030,10 +37457,43 @@ function createGuardrailsHooks(config3) {
37030
37457
  const inputArgsByCallID = new Map;
37031
37458
  return {
37032
37459
  toolBefore: async (input, output) => {
37033
- if (isArchitect(input.sessionID) && isWriteTool(input.tool)) {
37460
+ const currentSession = swarmState.agentSessions.get(input.sessionID);
37461
+ if (currentSession?.delegationActive) {} else if (isArchitect(input.sessionID) && isWriteTool(input.tool)) {
37034
37462
  const args2 = output.args;
37035
37463
  const targetPath = args2?.filePath ?? args2?.path ?? args2?.file ?? args2?.target;
37036
- if (typeof targetPath === "string" && isOutsideSwarmDir(targetPath) && isSourceCodePath(targetPath)) {
37464
+ if (!targetPath && (input.tool === "apply_patch" || input.tool === "patch")) {
37465
+ const patchText = args2?.input ?? args2?.patch ?? (Array.isArray(args2?.cmd) ? args2.cmd[1] : undefined);
37466
+ if (typeof patchText === "string") {
37467
+ const patchPathPattern = /\*\*\*\s+(?:Update|Add|Delete)\s+File:\s*(.+)/gi;
37468
+ const diffPathPattern = /\+\+\+\s+b\/(.+)/gm;
37469
+ const paths = new Set;
37470
+ let match;
37471
+ while ((match = patchPathPattern.exec(patchText)) !== null) {
37472
+ paths.add(match[1].trim());
37473
+ }
37474
+ while ((match = diffPathPattern.exec(patchText)) !== null) {
37475
+ const p = match[1].trim();
37476
+ if (p !== "/dev/null")
37477
+ paths.add(p);
37478
+ }
37479
+ for (const p of paths) {
37480
+ if (isOutsideSwarmDir(p) && (isSourceCodePath(p) || hasTraversalSegments(p))) {
37481
+ const session2 = swarmState.agentSessions.get(input.sessionID);
37482
+ if (session2) {
37483
+ session2.architectWriteCount++;
37484
+ warn("Architect direct code edit detected via apply_patch", {
37485
+ tool: input.tool,
37486
+ sessionID: input.sessionID,
37487
+ targetPath: p,
37488
+ writeCount: session2.architectWriteCount
37489
+ });
37490
+ }
37491
+ break;
37492
+ }
37493
+ }
37494
+ }
37495
+ }
37496
+ if (typeof targetPath === "string" && isOutsideSwarmDir(targetPath) && (isSourceCodePath(targetPath) || hasTraversalSegments(targetPath))) {
37037
37497
  const session2 = swarmState.agentSessions.get(input.sessionID);
37038
37498
  if (session2) {
37039
37499
  session2.architectWriteCount++;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-swarm",
3
- "version": "6.14.11",
3
+ "version": "6.14.12",
4
4
  "description": "Architect-centric agentic swarm plugin for OpenCode - hub-and-spoke orchestration with SME consultation, code generation, and QA review",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",