agentic-qe 2.8.1 → 2.8.2
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.
- package/CHANGELOG.md +43 -0
- package/README.md +1 -1
- package/dist/agents/BaseAgent.d.ts +201 -0
- package/dist/agents/BaseAgent.d.ts.map +1 -1
- package/dist/agents/BaseAgent.js +401 -0
- package/dist/agents/BaseAgent.js.map +1 -1
- package/dist/code-intelligence/embeddings/EmbeddingCacheFactory.d.ts +135 -0
- package/dist/code-intelligence/embeddings/EmbeddingCacheFactory.d.ts.map +1 -0
- package/dist/code-intelligence/embeddings/EmbeddingCacheFactory.js +301 -0
- package/dist/code-intelligence/embeddings/EmbeddingCacheFactory.js.map +1 -0
- package/dist/code-intelligence/embeddings/NomicEmbedder.d.ts +78 -6
- package/dist/code-intelligence/embeddings/NomicEmbedder.d.ts.map +1 -1
- package/dist/code-intelligence/embeddings/NomicEmbedder.js +162 -21
- package/dist/code-intelligence/embeddings/NomicEmbedder.js.map +1 -1
- package/dist/code-intelligence/embeddings/backends/MemoryBackend.d.ts +59 -0
- package/dist/code-intelligence/embeddings/backends/MemoryBackend.d.ts.map +1 -0
- package/dist/code-intelligence/embeddings/backends/MemoryBackend.js +173 -0
- package/dist/code-intelligence/embeddings/backends/MemoryBackend.js.map +1 -0
- package/dist/code-intelligence/embeddings/backends/RedisBackend.d.ts +50 -0
- package/dist/code-intelligence/embeddings/backends/RedisBackend.d.ts.map +1 -0
- package/dist/code-intelligence/embeddings/backends/RedisBackend.js +279 -0
- package/dist/code-intelligence/embeddings/backends/RedisBackend.js.map +1 -0
- package/dist/code-intelligence/embeddings/backends/SQLiteBackend.d.ts +64 -0
- package/dist/code-intelligence/embeddings/backends/SQLiteBackend.d.ts.map +1 -0
- package/dist/code-intelligence/embeddings/backends/SQLiteBackend.js +314 -0
- package/dist/code-intelligence/embeddings/backends/SQLiteBackend.js.map +1 -0
- package/dist/code-intelligence/embeddings/backends/index.d.ts +16 -0
- package/dist/code-intelligence/embeddings/backends/index.d.ts.map +1 -0
- package/dist/code-intelligence/embeddings/backends/index.js +28 -0
- package/dist/code-intelligence/embeddings/backends/index.js.map +1 -0
- package/dist/code-intelligence/embeddings/backends/types.d.ts +177 -0
- package/dist/code-intelligence/embeddings/backends/types.d.ts.map +1 -0
- package/dist/code-intelligence/embeddings/backends/types.js +30 -0
- package/dist/code-intelligence/embeddings/backends/types.js.map +1 -0
- package/dist/code-intelligence/embeddings/index.d.ts +7 -0
- package/dist/code-intelligence/embeddings/index.d.ts.map +1 -1
- package/dist/code-intelligence/embeddings/index.js +16 -1
- package/dist/code-intelligence/embeddings/index.js.map +1 -1
- package/dist/core/memory/HNSWVectorMemory.js +1 -1
- package/dist/infrastructure/index.d.ts +15 -0
- package/dist/infrastructure/index.d.ts.map +1 -0
- package/dist/infrastructure/index.js +44 -0
- package/dist/infrastructure/index.js.map +1 -0
- package/dist/infrastructure/network/AgentRateLimiter.d.ts +59 -0
- package/dist/infrastructure/network/AgentRateLimiter.d.ts.map +1 -0
- package/dist/infrastructure/network/AgentRateLimiter.js +186 -0
- package/dist/infrastructure/network/AgentRateLimiter.js.map +1 -0
- package/dist/infrastructure/network/AuditLogger.d.ts +102 -0
- package/dist/infrastructure/network/AuditLogger.d.ts.map +1 -0
- package/dist/infrastructure/network/AuditLogger.js +284 -0
- package/dist/infrastructure/network/AuditLogger.js.map +1 -0
- package/dist/infrastructure/network/DomainWhitelist.d.ts +111 -0
- package/dist/infrastructure/network/DomainWhitelist.d.ts.map +1 -0
- package/dist/infrastructure/network/DomainWhitelist.js +216 -0
- package/dist/infrastructure/network/DomainWhitelist.js.map +1 -0
- package/dist/infrastructure/network/NetworkPolicyManager.d.ts +97 -0
- package/dist/infrastructure/network/NetworkPolicyManager.d.ts.map +1 -0
- package/dist/infrastructure/network/NetworkPolicyManager.js +309 -0
- package/dist/infrastructure/network/NetworkPolicyManager.js.map +1 -0
- package/dist/infrastructure/network/index.d.ts +19 -0
- package/dist/infrastructure/network/index.d.ts.map +1 -0
- package/dist/infrastructure/network/index.js +46 -0
- package/dist/infrastructure/network/index.js.map +1 -0
- package/dist/infrastructure/network/policies/default-policies.d.ts +78 -0
- package/dist/infrastructure/network/policies/default-policies.d.ts.map +1 -0
- package/dist/infrastructure/network/policies/default-policies.js +312 -0
- package/dist/infrastructure/network/policies/default-policies.js.map +1 -0
- package/dist/infrastructure/network/types.d.ts +214 -0
- package/dist/infrastructure/network/types.d.ts.map +1 -0
- package/dist/infrastructure/network/types.js +25 -0
- package/dist/infrastructure/network/types.js.map +1 -0
- package/dist/infrastructure/sandbox/ResourceMonitor.d.ts +124 -0
- package/dist/infrastructure/sandbox/ResourceMonitor.d.ts.map +1 -0
- package/dist/infrastructure/sandbox/ResourceMonitor.js +305 -0
- package/dist/infrastructure/sandbox/ResourceMonitor.js.map +1 -0
- package/dist/infrastructure/sandbox/SandboxManager.d.ts +122 -0
- package/dist/infrastructure/sandbox/SandboxManager.d.ts.map +1 -0
- package/dist/infrastructure/sandbox/SandboxManager.js +527 -0
- package/dist/infrastructure/sandbox/SandboxManager.js.map +1 -0
- package/dist/infrastructure/sandbox/index.d.ts +18 -0
- package/dist/infrastructure/sandbox/index.d.ts.map +1 -0
- package/dist/infrastructure/sandbox/index.js +38 -0
- package/dist/infrastructure/sandbox/index.js.map +1 -0
- package/dist/infrastructure/sandbox/profiles/agent-profiles.d.ts +53 -0
- package/dist/infrastructure/sandbox/profiles/agent-profiles.d.ts.map +1 -0
- package/dist/infrastructure/sandbox/profiles/agent-profiles.js +433 -0
- package/dist/infrastructure/sandbox/profiles/agent-profiles.js.map +1 -0
- package/dist/infrastructure/sandbox/types.d.ts +227 -0
- package/dist/infrastructure/sandbox/types.d.ts.map +1 -0
- package/dist/infrastructure/sandbox/types.js +63 -0
- package/dist/infrastructure/sandbox/types.js.map +1 -0
- package/dist/mcp/server-instructions.d.ts +1 -1
- package/dist/mcp/server-instructions.js +1 -1
- package/dist/persistence/SupabasePersistenceProvider.d.ts.map +1 -1
- package/dist/persistence/SupabasePersistenceProvider.js +8 -4
- package/dist/persistence/SupabasePersistenceProvider.js.map +1 -1
- package/package.json +5 -1
package/dist/agents/BaseAgent.js
CHANGED
|
@@ -34,6 +34,10 @@ const KnowledgeGraphContextBuilder_js_1 = require("./context/KnowledgeGraphConte
|
|
|
34
34
|
const NervousSystemEnhancement_js_1 = require("../nervous-system/integration/NervousSystemEnhancement.js");
|
|
35
35
|
// Nervous System Persistence (Wave 7.1 - State Persistence)
|
|
36
36
|
const NervousSystemPersistenceManager_js_1 = require("../nervous-system/persistence/NervousSystemPersistenceManager.js");
|
|
37
|
+
const NetworkPolicyManager_js_1 = require("../infrastructure/network/NetworkPolicyManager.js");
|
|
38
|
+
const default_policies_js_1 = require("../infrastructure/network/policies/default-policies.js");
|
|
39
|
+
const SandboxManager_js_1 = require("../infrastructure/sandbox/SandboxManager.js");
|
|
40
|
+
const agent_profiles_js_1 = require("../infrastructure/sandbox/profiles/agent-profiles.js");
|
|
37
41
|
class BaseAgent extends events_1.EventEmitter {
|
|
38
42
|
constructor(config) {
|
|
39
43
|
super();
|
|
@@ -42,6 +46,10 @@ class BaseAgent extends events_1.EventEmitter {
|
|
|
42
46
|
this.federatedInitialized = false;
|
|
43
47
|
this.patternStoreInitialized = false;
|
|
44
48
|
this.nervousSystemEnhanced = false;
|
|
49
|
+
this.networkPolicyInitialized = false;
|
|
50
|
+
this.networkPolicyOwned = false; // true if we created the manager (not shared)
|
|
51
|
+
this.sandboxInitialized = false;
|
|
52
|
+
this.sandboxOwned = false; // true if we created the manager (not shared)
|
|
45
53
|
this.agentId = { id: config.id || (0, utils_1.generateAgentId)(config.type), type: config.type, created: new Date() };
|
|
46
54
|
this.capabilities = new Map((config.capabilities || []).map(cap => [cap.name, cap]));
|
|
47
55
|
this.context = config.context;
|
|
@@ -71,6 +79,12 @@ class BaseAgent extends events_1.EventEmitter {
|
|
|
71
79
|
this.codeIntelligenceConfig = config.codeIntelligence;
|
|
72
80
|
// Nervous System configuration (Wave 7 - Bio-Inspired Intelligence)
|
|
73
81
|
this.nervousSystemConfig = config.nervousSystem;
|
|
82
|
+
// Network Policy configuration (SP-3 - Issue #146)
|
|
83
|
+
// Default enabled for security, opt-out available
|
|
84
|
+
this.networkPolicyConfig = config.networkPolicy ?? { enabled: true };
|
|
85
|
+
// Sandbox configuration (SP-1 - Issue #146)
|
|
86
|
+
// Default disabled (opt-in) - requires Docker infrastructure
|
|
87
|
+
this.sandboxConfig = config.sandbox ?? { enabled: false };
|
|
74
88
|
// Early validation (Issue #137)
|
|
75
89
|
const validation = (0, utils_1.validateLearningConfig)(config);
|
|
76
90
|
if (!validation.valid && validation.warning) {
|
|
@@ -141,6 +155,10 @@ class BaseAgent extends events_1.EventEmitter {
|
|
|
141
155
|
await this.initializeCodeIntelligence();
|
|
142
156
|
// Initialize Nervous System (Wave 7 - Bio-Inspired Intelligence)
|
|
143
157
|
await this.initializeNervousSystem();
|
|
158
|
+
// Initialize Network Policy (SP-3 - Issue #146)
|
|
159
|
+
await this.initializeNetworkPolicy();
|
|
160
|
+
// Initialize Sandbox (SP-1 - Issue #146)
|
|
161
|
+
await this.initializeSandbox();
|
|
144
162
|
await this.initializeComponents();
|
|
145
163
|
await this.executeHook('post-initialization');
|
|
146
164
|
this.coordinator.emitEvent('agent.initialized', { agentId: this.agentId });
|
|
@@ -198,6 +216,8 @@ class BaseAgent extends events_1.EventEmitter {
|
|
|
198
216
|
await this.cleanupFederated(); // Phase 0 M0.5: Cleanup federated learning
|
|
199
217
|
await this.cleanupPatternStore(); // Phase 0.5: Cleanup pattern store
|
|
200
218
|
await this.cleanupNervousSystem(); // Wave 7: Cleanup nervous system
|
|
219
|
+
await this.cleanupNetworkPolicy(); // SP-3: Cleanup network policy
|
|
220
|
+
await this.cleanupSandbox(); // SP-1: Cleanup sandbox container
|
|
201
221
|
this.coordinator.clearAllHandlers();
|
|
202
222
|
},
|
|
203
223
|
onPostTermination: async () => {
|
|
@@ -1680,6 +1700,387 @@ class BaseAgent extends events_1.EventEmitter {
|
|
|
1680
1700
|
this.logger.warn(`[${this.agentId.id}] Nervous System cleanup error:`, error.message);
|
|
1681
1701
|
}
|
|
1682
1702
|
}
|
|
1703
|
+
// ============================================
|
|
1704
|
+
// Network Policy Methods (SP-3 - Issue #146)
|
|
1705
|
+
// ============================================
|
|
1706
|
+
/**
|
|
1707
|
+
* Initialize Network Policy Manager for domain whitelisting and rate limiting
|
|
1708
|
+
* Provides security hardening for agent network access
|
|
1709
|
+
*/
|
|
1710
|
+
async initializeNetworkPolicy() {
|
|
1711
|
+
if (this.networkPolicyConfig.enabled === false) {
|
|
1712
|
+
this.logger.info(`[${this.agentId.id}] Network Policy disabled by configuration`);
|
|
1713
|
+
return;
|
|
1714
|
+
}
|
|
1715
|
+
try {
|
|
1716
|
+
// Use shared manager if provided, otherwise create a new one
|
|
1717
|
+
if (this.networkPolicyConfig.sharedManager) {
|
|
1718
|
+
this.networkPolicyManager = this.networkPolicyConfig.sharedManager;
|
|
1719
|
+
this.networkPolicyOwned = false;
|
|
1720
|
+
}
|
|
1721
|
+
else {
|
|
1722
|
+
this.networkPolicyManager = (0, NetworkPolicyManager_js_1.createNetworkPolicyManager)({
|
|
1723
|
+
enableAuditLogging: this.networkPolicyConfig.enableAuditLogging ?? true,
|
|
1724
|
+
debug: this.networkPolicyConfig.debug ?? false,
|
|
1725
|
+
});
|
|
1726
|
+
await this.networkPolicyManager.initialize();
|
|
1727
|
+
this.networkPolicyOwned = true;
|
|
1728
|
+
}
|
|
1729
|
+
// Apply any policy overrides for this agent type
|
|
1730
|
+
if (this.networkPolicyConfig.policyOverrides) {
|
|
1731
|
+
this.networkPolicyManager.updatePolicy(this.agentId.type, this.networkPolicyConfig.policyOverrides);
|
|
1732
|
+
}
|
|
1733
|
+
this.networkPolicyInitialized = true;
|
|
1734
|
+
this.logger.info(`[${this.agentId.id}] Network Policy initialized (owned: ${this.networkPolicyOwned})`);
|
|
1735
|
+
}
|
|
1736
|
+
catch (error) {
|
|
1737
|
+
this.logger.warn(`[${this.agentId.id}] Network Policy initialization failed:`, error.message);
|
|
1738
|
+
// Don't throw - agent can work without network policy (graceful degradation)
|
|
1739
|
+
}
|
|
1740
|
+
}
|
|
1741
|
+
/**
|
|
1742
|
+
* Check if network policy enforcement is available
|
|
1743
|
+
*/
|
|
1744
|
+
hasNetworkPolicy() {
|
|
1745
|
+
return this.networkPolicyInitialized && this.networkPolicyManager !== undefined;
|
|
1746
|
+
}
|
|
1747
|
+
/**
|
|
1748
|
+
* Get the network policy for this agent type
|
|
1749
|
+
*/
|
|
1750
|
+
getNetworkPolicy() {
|
|
1751
|
+
if (!this.networkPolicyManager) {
|
|
1752
|
+
return (0, default_policies_js_1.getNetworkPolicy)(this.agentId.type);
|
|
1753
|
+
}
|
|
1754
|
+
return this.networkPolicyManager.getPolicy(this.agentId.type);
|
|
1755
|
+
}
|
|
1756
|
+
/**
|
|
1757
|
+
* Check if a network request to a domain is allowed
|
|
1758
|
+
* Does NOT consume rate limit tokens - use for pre-flight checks
|
|
1759
|
+
*
|
|
1760
|
+
* @param domain - The target domain (e.g., "api.anthropic.com")
|
|
1761
|
+
* @returns PolicyCheckResult with allowed status and details
|
|
1762
|
+
*/
|
|
1763
|
+
async checkNetworkRequest(domain) {
|
|
1764
|
+
if (!this.networkPolicyManager) {
|
|
1765
|
+
// No policy enforcement - allow all
|
|
1766
|
+
return {
|
|
1767
|
+
allowed: true,
|
|
1768
|
+
policy: (0, default_policies_js_1.getNetworkPolicy)(this.agentId.type),
|
|
1769
|
+
};
|
|
1770
|
+
}
|
|
1771
|
+
return this.networkPolicyManager.checkRequest(this.agentId.id, this.agentId.type, domain);
|
|
1772
|
+
}
|
|
1773
|
+
/**
|
|
1774
|
+
* Record a network request (consumes rate limit token)
|
|
1775
|
+
* Call this after making an actual network request
|
|
1776
|
+
*
|
|
1777
|
+
* @param domain - The target domain
|
|
1778
|
+
* @param allowed - Whether the request was allowed
|
|
1779
|
+
* @param responseTimeMs - Response time in milliseconds (optional)
|
|
1780
|
+
*/
|
|
1781
|
+
async recordNetworkRequest(domain, allowed, responseTimeMs) {
|
|
1782
|
+
if (!this.networkPolicyManager) {
|
|
1783
|
+
return;
|
|
1784
|
+
}
|
|
1785
|
+
await this.networkPolicyManager.recordRequest(this.agentId.id, this.agentId.type, domain, allowed, responseTimeMs);
|
|
1786
|
+
}
|
|
1787
|
+
/**
|
|
1788
|
+
* Make a policy-enforced network request
|
|
1789
|
+
* Checks domain whitelist and rate limits before allowing the request
|
|
1790
|
+
*
|
|
1791
|
+
* @param url - The full URL to request
|
|
1792
|
+
* @param requestFn - Function that performs the actual request
|
|
1793
|
+
* @returns The result of requestFn if allowed
|
|
1794
|
+
* @throws Error if request is blocked by policy
|
|
1795
|
+
*
|
|
1796
|
+
* @example
|
|
1797
|
+
* ```typescript
|
|
1798
|
+
* const response = await this.makeNetworkRequest(
|
|
1799
|
+
* 'https://api.anthropic.com/v1/messages',
|
|
1800
|
+
* async () => fetch('https://api.anthropic.com/v1/messages', { method: 'POST', ... })
|
|
1801
|
+
* );
|
|
1802
|
+
* ```
|
|
1803
|
+
*/
|
|
1804
|
+
async makeNetworkRequest(url, requestFn) {
|
|
1805
|
+
// Extract domain from URL
|
|
1806
|
+
let domain;
|
|
1807
|
+
try {
|
|
1808
|
+
const parsedUrl = new URL(url);
|
|
1809
|
+
domain = parsedUrl.hostname;
|
|
1810
|
+
}
|
|
1811
|
+
catch {
|
|
1812
|
+
throw new Error(`Invalid URL: ${url}`);
|
|
1813
|
+
}
|
|
1814
|
+
// Check if request is allowed
|
|
1815
|
+
const check = await this.checkNetworkRequest(domain);
|
|
1816
|
+
if (!check.allowed) {
|
|
1817
|
+
const error = new Error(`Network request blocked: ${check.reason} - ${check.details || domain}`);
|
|
1818
|
+
error.policyCheckResult = check;
|
|
1819
|
+
throw error;
|
|
1820
|
+
}
|
|
1821
|
+
// Make the actual request
|
|
1822
|
+
const startTime = Date.now();
|
|
1823
|
+
try {
|
|
1824
|
+
const result = await requestFn();
|
|
1825
|
+
const responseTime = Date.now() - startTime;
|
|
1826
|
+
await this.recordNetworkRequest(domain, true, responseTime);
|
|
1827
|
+
return result;
|
|
1828
|
+
}
|
|
1829
|
+
catch (error) {
|
|
1830
|
+
const responseTime = Date.now() - startTime;
|
|
1831
|
+
await this.recordNetworkRequest(domain, false, responseTime);
|
|
1832
|
+
throw error;
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
/**
|
|
1836
|
+
* Get rate limit status for this agent
|
|
1837
|
+
*/
|
|
1838
|
+
getNetworkRateLimitStatus() {
|
|
1839
|
+
if (!this.networkPolicyManager) {
|
|
1840
|
+
return null;
|
|
1841
|
+
}
|
|
1842
|
+
return this.networkPolicyManager.getRateLimitStatus(this.agentId.id, this.agentId.type);
|
|
1843
|
+
}
|
|
1844
|
+
/**
|
|
1845
|
+
* Get network audit statistics
|
|
1846
|
+
* @param since - Optional start date for stats
|
|
1847
|
+
*/
|
|
1848
|
+
async getNetworkAuditStats(since) {
|
|
1849
|
+
if (!this.networkPolicyManager) {
|
|
1850
|
+
return null;
|
|
1851
|
+
}
|
|
1852
|
+
return this.networkPolicyManager.getAuditStats(since);
|
|
1853
|
+
}
|
|
1854
|
+
/**
|
|
1855
|
+
* Get network policy statistics for this agent
|
|
1856
|
+
*/
|
|
1857
|
+
getNetworkPolicyStats() {
|
|
1858
|
+
if (!this.hasNetworkPolicy()) {
|
|
1859
|
+
return { enabled: false };
|
|
1860
|
+
}
|
|
1861
|
+
return {
|
|
1862
|
+
enabled: true,
|
|
1863
|
+
policy: this.getNetworkPolicy(),
|
|
1864
|
+
rateLimitStatus: this.getNetworkRateLimitStatus(),
|
|
1865
|
+
};
|
|
1866
|
+
}
|
|
1867
|
+
/**
|
|
1868
|
+
* Cleanup network policy resources on agent termination
|
|
1869
|
+
*/
|
|
1870
|
+
async cleanupNetworkPolicy() {
|
|
1871
|
+
if (!this.networkPolicyInitialized) {
|
|
1872
|
+
return;
|
|
1873
|
+
}
|
|
1874
|
+
try {
|
|
1875
|
+
// Only shutdown if we own the manager (not shared)
|
|
1876
|
+
if (this.networkPolicyOwned && this.networkPolicyManager) {
|
|
1877
|
+
await this.networkPolicyManager.shutdown();
|
|
1878
|
+
this.logger.info(`[${this.agentId.id}] Network Policy cleanup complete`);
|
|
1879
|
+
}
|
|
1880
|
+
}
|
|
1881
|
+
catch (error) {
|
|
1882
|
+
this.logger.warn(`[${this.agentId.id}] Network Policy cleanup error:`, error.message);
|
|
1883
|
+
}
|
|
1884
|
+
this.networkPolicyInitialized = false;
|
|
1885
|
+
this.networkPolicyManager = undefined;
|
|
1886
|
+
}
|
|
1887
|
+
// ============================================
|
|
1888
|
+
// Sandbox Infrastructure Methods (SP-1 - Issue #146)
|
|
1889
|
+
// ============================================
|
|
1890
|
+
/**
|
|
1891
|
+
* Initialize Sandbox Manager for Docker-based agent isolation
|
|
1892
|
+
* Provides secure, isolated execution environments with resource limits
|
|
1893
|
+
*/
|
|
1894
|
+
async initializeSandbox() {
|
|
1895
|
+
if (this.sandboxConfig.enabled === false) {
|
|
1896
|
+
this.logger.debug(`[${this.agentId.id}] Sandbox disabled by configuration`);
|
|
1897
|
+
return;
|
|
1898
|
+
}
|
|
1899
|
+
try {
|
|
1900
|
+
// Use shared manager if provided, otherwise create a new one
|
|
1901
|
+
if (this.sandboxConfig.sharedManager) {
|
|
1902
|
+
this.sandboxManager = this.sandboxConfig.sharedManager;
|
|
1903
|
+
this.sandboxOwned = false;
|
|
1904
|
+
}
|
|
1905
|
+
else {
|
|
1906
|
+
this.sandboxManager = (0, SandboxManager_js_1.createSandboxManager)({
|
|
1907
|
+
agentImage: process.env.AQE_SANDBOX_IMAGE || 'agentic-qe-agent',
|
|
1908
|
+
imageTag: process.env.AQE_SANDBOX_TAG || 'latest',
|
|
1909
|
+
cleanupOnShutdown: true,
|
|
1910
|
+
});
|
|
1911
|
+
await this.sandboxManager.initialize();
|
|
1912
|
+
this.sandboxOwned = true;
|
|
1913
|
+
}
|
|
1914
|
+
// Auto-create sandbox container if configured
|
|
1915
|
+
if (this.sandboxConfig.autoCreateSandbox !== false) {
|
|
1916
|
+
const result = await this.createSandboxContainer();
|
|
1917
|
+
if (result.success && result.container) {
|
|
1918
|
+
this.containerId = result.container.containerId;
|
|
1919
|
+
}
|
|
1920
|
+
else if (!result.success) {
|
|
1921
|
+
// Docker not available - graceful degradation
|
|
1922
|
+
this.logger.info(`[${this.agentId.id}] Sandbox container creation skipped: ${result.error}`);
|
|
1923
|
+
}
|
|
1924
|
+
}
|
|
1925
|
+
this.sandboxInitialized = true;
|
|
1926
|
+
this.logger.info(`[${this.agentId.id}] Sandbox initialized (owned: ${this.sandboxOwned}, container: ${this.containerId || 'none'})`);
|
|
1927
|
+
}
|
|
1928
|
+
catch (error) {
|
|
1929
|
+
this.logger.warn(`[${this.agentId.id}] Sandbox initialization failed:`, error.message);
|
|
1930
|
+
// Don't throw - agent can work without sandbox (graceful degradation)
|
|
1931
|
+
}
|
|
1932
|
+
}
|
|
1933
|
+
/**
|
|
1934
|
+
* Check if sandbox isolation is available
|
|
1935
|
+
*/
|
|
1936
|
+
hasSandbox() {
|
|
1937
|
+
return this.sandboxInitialized && this.sandboxManager !== undefined;
|
|
1938
|
+
}
|
|
1939
|
+
/**
|
|
1940
|
+
* Check if agent is running in a sandbox container
|
|
1941
|
+
*/
|
|
1942
|
+
isInSandbox() {
|
|
1943
|
+
return this.containerId !== undefined;
|
|
1944
|
+
}
|
|
1945
|
+
/**
|
|
1946
|
+
* Get the container ID if running in sandbox
|
|
1947
|
+
*/
|
|
1948
|
+
getContainerId() {
|
|
1949
|
+
return this.containerId;
|
|
1950
|
+
}
|
|
1951
|
+
/**
|
|
1952
|
+
* Get sandbox configuration for this agent type
|
|
1953
|
+
*/
|
|
1954
|
+
getSandboxConfig() {
|
|
1955
|
+
return (0, agent_profiles_js_1.getAgentSandboxConfig)(this.agentId.type);
|
|
1956
|
+
}
|
|
1957
|
+
/**
|
|
1958
|
+
* Create a sandbox container for this agent
|
|
1959
|
+
* @param customConfig - Optional custom sandbox configuration
|
|
1960
|
+
*/
|
|
1961
|
+
async createSandboxContainer(customConfig) {
|
|
1962
|
+
if (!this.sandboxManager) {
|
|
1963
|
+
return {
|
|
1964
|
+
success: false,
|
|
1965
|
+
error: 'Sandbox manager not initialized',
|
|
1966
|
+
};
|
|
1967
|
+
}
|
|
1968
|
+
// Check if Docker is available
|
|
1969
|
+
const dockerAvailable = await this.sandboxManager.isDockerAvailable();
|
|
1970
|
+
if (!dockerAvailable) {
|
|
1971
|
+
return {
|
|
1972
|
+
success: false,
|
|
1973
|
+
error: 'Docker not available',
|
|
1974
|
+
};
|
|
1975
|
+
}
|
|
1976
|
+
// Merge profile config with any overrides
|
|
1977
|
+
const mergedConfig = {
|
|
1978
|
+
...this.sandboxConfig.sandboxOverrides,
|
|
1979
|
+
...customConfig,
|
|
1980
|
+
};
|
|
1981
|
+
return this.sandboxManager.createSandbox(this.agentId.id, this.agentId.type, mergedConfig);
|
|
1982
|
+
}
|
|
1983
|
+
/**
|
|
1984
|
+
* Get resource usage for the sandbox container
|
|
1985
|
+
*/
|
|
1986
|
+
async getSandboxResourceUsage() {
|
|
1987
|
+
if (!this.sandboxManager || !this.containerId) {
|
|
1988
|
+
return null;
|
|
1989
|
+
}
|
|
1990
|
+
return this.sandboxManager.getResourceUsage(this.containerId);
|
|
1991
|
+
}
|
|
1992
|
+
/**
|
|
1993
|
+
* Check sandbox container health
|
|
1994
|
+
*/
|
|
1995
|
+
async checkSandboxHealth() {
|
|
1996
|
+
if (!this.sandboxManager || !this.containerId) {
|
|
1997
|
+
return null;
|
|
1998
|
+
}
|
|
1999
|
+
return this.sandboxManager.healthCheck(this.containerId);
|
|
2000
|
+
}
|
|
2001
|
+
/**
|
|
2002
|
+
* Execute a command in the sandbox container
|
|
2003
|
+
* @param command - Command to execute as array of strings
|
|
2004
|
+
*/
|
|
2005
|
+
async execInSandbox(command) {
|
|
2006
|
+
if (!this.sandboxManager || !this.containerId) {
|
|
2007
|
+
return null;
|
|
2008
|
+
}
|
|
2009
|
+
return this.sandboxManager.exec(this.containerId, command);
|
|
2010
|
+
}
|
|
2011
|
+
/**
|
|
2012
|
+
* Get sandbox container logs
|
|
2013
|
+
* @param options - Log retrieval options
|
|
2014
|
+
*/
|
|
2015
|
+
async getSandboxLogs(options) {
|
|
2016
|
+
if (!this.sandboxManager || !this.containerId) {
|
|
2017
|
+
return null;
|
|
2018
|
+
}
|
|
2019
|
+
return this.sandboxManager.getLogs(this.containerId, options);
|
|
2020
|
+
}
|
|
2021
|
+
/**
|
|
2022
|
+
* Subscribe to sandbox events
|
|
2023
|
+
* @param handler - Event handler function
|
|
2024
|
+
*/
|
|
2025
|
+
onSandboxEvent(handler) {
|
|
2026
|
+
if (this.sandboxManager) {
|
|
2027
|
+
this.sandboxManager.on(handler);
|
|
2028
|
+
}
|
|
2029
|
+
}
|
|
2030
|
+
/**
|
|
2031
|
+
* Unsubscribe from sandbox events
|
|
2032
|
+
* @param handler - Event handler to remove
|
|
2033
|
+
*/
|
|
2034
|
+
offSandboxEvent(handler) {
|
|
2035
|
+
if (this.sandboxManager) {
|
|
2036
|
+
this.sandboxManager.off(handler);
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
2039
|
+
/**
|
|
2040
|
+
* Get sandbox statistics for this agent
|
|
2041
|
+
*/
|
|
2042
|
+
getSandboxStats() {
|
|
2043
|
+
if (!this.hasSandbox()) {
|
|
2044
|
+
return { enabled: false, inSandbox: false };
|
|
2045
|
+
}
|
|
2046
|
+
return {
|
|
2047
|
+
enabled: true,
|
|
2048
|
+
inSandbox: this.isInSandbox(),
|
|
2049
|
+
containerId: this.containerId,
|
|
2050
|
+
sandboxConfig: this.getSandboxConfig(),
|
|
2051
|
+
};
|
|
2052
|
+
}
|
|
2053
|
+
/**
|
|
2054
|
+
* Cleanup sandbox resources on agent termination
|
|
2055
|
+
*/
|
|
2056
|
+
async cleanupSandbox() {
|
|
2057
|
+
if (!this.sandboxInitialized) {
|
|
2058
|
+
return;
|
|
2059
|
+
}
|
|
2060
|
+
try {
|
|
2061
|
+
// Destroy our container if we have one
|
|
2062
|
+
if (this.containerId && this.sandboxManager) {
|
|
2063
|
+
const result = await this.sandboxManager.destroySandbox(this.containerId, true);
|
|
2064
|
+
if (result.success) {
|
|
2065
|
+
this.logger.info(`[${this.agentId.id}] Sandbox container ${this.containerId} destroyed`);
|
|
2066
|
+
}
|
|
2067
|
+
else {
|
|
2068
|
+
this.logger.warn(`[${this.agentId.id}] Sandbox container destruction failed: ${result.error}`);
|
|
2069
|
+
}
|
|
2070
|
+
}
|
|
2071
|
+
// Only shutdown manager if we own it (not shared)
|
|
2072
|
+
if (this.sandboxOwned && this.sandboxManager) {
|
|
2073
|
+
await this.sandboxManager.shutdown();
|
|
2074
|
+
this.logger.info(`[${this.agentId.id}] Sandbox manager shutdown complete`);
|
|
2075
|
+
}
|
|
2076
|
+
}
|
|
2077
|
+
catch (error) {
|
|
2078
|
+
this.logger.warn(`[${this.agentId.id}] Sandbox cleanup error:`, error.message);
|
|
2079
|
+
}
|
|
2080
|
+
this.sandboxInitialized = false;
|
|
2081
|
+
this.sandboxManager = undefined;
|
|
2082
|
+
this.containerId = undefined;
|
|
2083
|
+
}
|
|
1683
2084
|
}
|
|
1684
2085
|
exports.BaseAgent = BaseAgent;
|
|
1685
2086
|
class BaseAgentFactory {
|