@inneranimalmedia/agentsam-sdk 2.5.0 → 2.6.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.
Files changed (211) hide show
  1. package/AGENTSAM.md +55 -0
  2. package/README.md +12 -8
  3. package/bin/agentsam +2 -0
  4. package/docs/AGENTSAM_ASTRA_OPENAI_INTEGRATION.md +1363 -0
  5. package/docs/CLI_SHELL.md +163 -53
  6. package/docs/PLATFORM_RUNTIME_EVENTS.md +48 -0
  7. package/docs/RELEASES.md +16 -7
  8. package/docs/SOURCE_ARCHITECTURE.md +58 -0
  9. package/docs/TEST_TIERS.md +26 -0
  10. package/migrations/runtime/0001_cli_runtime.sql +298 -0
  11. package/package.json +45 -12
  12. package/packages/agentsam-repository/README.md +15 -0
  13. package/packages/agentsam-repository/package.json +25 -0
  14. package/packages/agentsam-repository/src/contracts.js +113 -0
  15. package/packages/agentsam-repository/src/index.js +3 -0
  16. package/{src/lib → packages/agentsam-repository/src}/merkle/cloudflare-persistence.js +14 -24
  17. package/{src/lib → packages/agentsam-repository/src}/merkle/index.js +1 -0
  18. package/{src/lib → packages/agentsam-repository/src}/merkle/persistence.js +6 -4
  19. package/{src/lib → packages/agentsam-repository/src}/merkle/policy.js +1 -0
  20. package/packages/agentsam-repository/test/contracts.test.mjs +40 -0
  21. package/packages/agentsam-repository/test/git-context.test.mjs +24 -0
  22. package/{test/merkle.test.mjs → packages/agentsam-repository/test/merkle-core.test.mjs} +2 -32
  23. package/{test → packages/agentsam-repository/test}/merkle-persistence.test.mjs +11 -6
  24. package/packages/connectors/cloudflare/package.json +10 -0
  25. package/packages/connectors/cloudflare/src/index.js +127 -0
  26. package/packages/connectors/cloudflare/src/owner.js +76 -0
  27. package/packages/connectors/cloudflare/src/routes.js +223 -0
  28. package/packages/connectors/cloudflare/src/vault.js +80 -0
  29. package/packages/connectors/cloudflare/tests/connector.test.mjs +44 -0
  30. package/packages/identity/package.json +2 -2
  31. package/packages/identity/src/contracts/auth-config.js +18 -7
  32. package/packages/identity/tests/auth-config.test.mjs +9 -5
  33. package/packages/identity/tests/oauth-credentials.test.mjs +4 -4
  34. package/protocol/COMPANY_REPOSITORY_GRAPH_V1.md +91 -0
  35. package/protocol/MERKLE_PERSISTENCE_V1.md +2 -0
  36. package/protocol/MERKLE_PERSISTENCE_V2.md +40 -0
  37. package/protocol/README.md +1 -0
  38. package/protocol/capabilities/cloudflare-cpu-audit-input.schema.json +19 -0
  39. package/protocol/capabilities/cloudflare-cpu-profile-input.schema.json +13 -0
  40. package/protocol/capabilities/cloudflare-wrangler-native-input.schema.json +19 -0
  41. package/protocol/capabilities/manifest.json +47 -0
  42. package/protocol/context/context-budget.schema.json +10 -15
  43. package/protocol/context/context-item.schema.json +4 -5
  44. package/protocol/context/resolved-context-pack.schema.json +19 -14
  45. package/protocol/models/README.md +373 -0
  46. package/protocol/models/model-inventory-v2.schema.json +212 -0
  47. package/protocol/repository/repository-contract.schema.json +24 -0
  48. package/protocol/repository/repository-dependency.schema.json +24 -0
  49. package/protocol/repository/repository-identity.schema.json +17 -0
  50. package/protocol/rpc/v1/common.proto +16 -0
  51. package/protocol/rpc/v1/errors.proto +35 -0
  52. package/protocol/rpc/v1/knowledge.proto +77 -0
  53. package/services/knowledge/package-lock.json +333 -0
  54. package/services/knowledge/package.json +5 -1
  55. package/skills/agentsam-cloudflare-workers/SKILL.md +53 -0
  56. package/skills/agentsam-cloudflare-workers/references/cpu-profiling.md +16 -0
  57. package/skills/agentsam-cloudflare-workers/references/errors-and-observability.md +29 -0
  58. package/skills/agentsam-cloudflare-workers/references/wrangler-native-map.md +28 -0
  59. package/skills/catalog.json +18 -0
  60. package/src/agent/capability-adapter.js +25 -13
  61. package/src/agent/index.js +1 -0
  62. package/src/agent/responses-runner.js +353 -0
  63. package/src/capabilities/repository-snapshot.js +3 -3
  64. package/src/cli.js +118 -31
  65. package/src/cloudflare/cpu-profile.js +115 -0
  66. package/src/cloudflare/index.js +14 -0
  67. package/src/cloudflare/wrangler.js +132 -0
  68. package/src/commands/account-auth.js +47 -0
  69. package/src/commands/cloudflare.js +58 -0
  70. package/src/commands/connections.js +93 -0
  71. package/src/commands/context-economics.js +129 -0
  72. package/src/commands/context.js +1 -1
  73. package/src/commands/db.js +20 -3
  74. package/src/commands/deploy.js +39 -3
  75. package/src/commands/env.js +90 -0
  76. package/src/commands/eval.js +63 -0
  77. package/src/commands/interactive.js +2 -5
  78. package/src/commands/knowledge.js +12 -4
  79. package/src/commands/merkle-persist.js +30 -11
  80. package/src/commands/merkle.js +1 -1
  81. package/src/commands/models.js +149 -46
  82. package/src/commands/ollama.js +26 -0
  83. package/src/commands/preferences.js +130 -61
  84. package/src/commands/resume.js +67 -0
  85. package/src/commands/security.js +5 -3
  86. package/src/commands/shell.js +568 -119
  87. package/src/commands/tunnel.js +2 -2
  88. package/src/commands/whoami.js +86 -0
  89. package/src/context/budget.js +68 -6
  90. package/src/context/index.js +3 -1
  91. package/src/context/rehydrate.js +35 -0
  92. package/src/context/resolve.js +44 -12
  93. package/src/errors/contract.js +236 -0
  94. package/src/errors/diagnostic.js +160 -0
  95. package/src/errors/index.js +23 -0
  96. package/src/eval/context.js +191 -0
  97. package/src/eval/index.js +1 -0
  98. package/src/index.js +68 -2
  99. package/src/knowledge/service/auth.js +13 -0
  100. package/src/knowledge/service/grpc-client.js +115 -0
  101. package/src/knowledge/service/grpc-codec.js +237 -0
  102. package/src/knowledge/service/grpc-server.js +83 -0
  103. package/src/knowledge/service/job-engine.js +248 -0
  104. package/src/knowledge/service/server.js +87 -135
  105. package/src/knowledge/source.js +1 -1
  106. package/src/lib/account-session.js +98 -0
  107. package/src/lib/agent-instructions.js +73 -0
  108. package/src/lib/auth.js +4 -0
  109. package/src/lib/cli-preferences.js +55 -24
  110. package/src/lib/deploy/git-guard.js +69 -0
  111. package/src/lib/deploy/health.js +57 -0
  112. package/src/lib/deploy/local-studio.js +283 -0
  113. package/src/lib/deploy/secret-scan.js +65 -0
  114. package/src/lib/deploy-receipt/index.js +2 -2
  115. package/src/lib/detect-context.js +2 -2
  116. package/src/lib/execution-approvals.js +59 -0
  117. package/src/lib/knowledge-docker.js +6 -3
  118. package/src/lib/local-sessions.js +148 -0
  119. package/src/lib/local-status.js +1 -1
  120. package/src/lib/project-config.js +1 -1
  121. package/src/lib/provider-credentials.js +183 -0
  122. package/src/lib/scaffold/templates/worker-api/index.js +101 -20
  123. package/src/lib/scaffold/wizards/worker-api.js +27 -11
  124. package/src/lib/slash-commands.js +23 -16
  125. package/src/local/migrations.js +93 -0
  126. package/src/local/runtime-store.js +141 -0
  127. package/src/local/sqlite.js +2 -0
  128. package/src/local-pty/server.js +113 -51
  129. package/src/models/catalog.js +135 -0
  130. package/src/models/discovery.js +292 -0
  131. package/src/models/index.js +7 -0
  132. package/src/providers/anthropic-messages.js +192 -0
  133. package/src/providers/cloudflare-chat.js +183 -0
  134. package/src/providers/factory.js +69 -0
  135. package/src/providers/gemini-generate-content.js +208 -0
  136. package/src/providers/index.js +10 -0
  137. package/src/providers/ollama-chat.js +148 -0
  138. package/src/providers/openai-responses.js +426 -0
  139. package/src/repository/index.js +14 -2
  140. package/src/rpc/generated/common_grpc_pb.js +1 -0
  141. package/src/rpc/generated/common_pb.js +536 -0
  142. package/src/rpc/generated/errors_grpc_pb.js +1 -0
  143. package/src/rpc/generated/errors_pb.js +482 -0
  144. package/src/rpc/generated/knowledge_grpc_pb.js +135 -0
  145. package/src/rpc/generated/knowledge_pb.js +2168 -0
  146. package/src/rpc/generated/package.json +3 -0
  147. package/src/security/process.js +35 -9
  148. package/src/security/trust-boundary.js +2 -2
  149. package/src/telemetry/contracts.js +203 -0
  150. package/src/telemetry/events.js +51 -0
  151. package/src/telemetry/index.js +8 -0
  152. package/src/tools/hydrate.js +35 -0
  153. package/src/tools/index.js +1 -0
  154. package/src/ui/boot.js +15 -17
  155. package/src/ui/cli/activity.js +76 -0
  156. package/src/ui/cli/compaction.js +15 -0
  157. package/src/ui/cli/footer.js +39 -0
  158. package/src/ui/cli/help.js +192 -0
  159. package/src/ui/cli/plan.js +20 -0
  160. package/src/ui/cli/runtime-events.js +110 -0
  161. package/src/ui/cli/waiting.js +16 -0
  162. package/src/ui/merkle/render.js +1 -1
  163. package/test/account-session.test.mjs +36 -0
  164. package/test/cli/preferences-runtime.test.mjs +11 -0
  165. package/test/cli/runtime-ui.test.mjs +74 -0
  166. package/test/cli-preferences.test.mjs +26 -5
  167. package/test/cloudflare-connector.test.mjs +96 -0
  168. package/test/cloudflare-runtime.test.mjs +75 -0
  169. package/test/context.test.mjs +61 -12
  170. package/test/deploy-health-scan.test.mjs +67 -0
  171. package/test/error-diagnostics.test.mjs +115 -0
  172. package/test/eval-context.test.mjs +37 -0
  173. package/test/execution-approvals.test.mjs +27 -0
  174. package/test/fixtures/knowledge-rpc-worker.mjs +16 -0
  175. package/test/integration/cli-help.test.mjs +37 -0
  176. package/test/integration/knowledge-rpc.test.mjs +112 -0
  177. package/test/integration/merkle-cli.test.mjs +61 -0
  178. package/test/integration/merkle-persistence-identity.test.mjs +48 -0
  179. package/test/integration/provider-env-cli.test.mjs +49 -0
  180. package/test/integration/provider-factory.test.mjs +197 -0
  181. package/test/integration/repository-company-graph.test.mjs +90 -0
  182. package/test/integration/runtime-migrations.test.mjs +82 -0
  183. package/test/knowledge-service.test.mjs +5 -0
  184. package/test/knowledge.test.mjs +16 -0
  185. package/test/live/terminal-transport.live.test.mjs +24 -0
  186. package/test/local-sessions.test.mjs +48 -0
  187. package/test/local-studio-deploy.test.mjs +83 -0
  188. package/test/model-catalog.test.mjs +43 -0
  189. package/test/models.test.mjs +127 -16
  190. package/test/npm10-lock.test.mjs +29 -0
  191. package/test/ollama.test.mjs +21 -0
  192. package/test/openai-responses.test.mjs +95 -0
  193. package/test/portable-context.test.mjs +1 -1
  194. package/test/provider-credentials.test.mjs +96 -0
  195. package/test/rehydrate.test.mjs +25 -0
  196. package/test/release-hygiene.test.mjs +13 -5
  197. package/test/responses-runner.test.mjs +150 -0
  198. package/test/shell.test.mjs +92 -23
  199. package/test/smoke.mjs +4 -1
  200. package/test/telemetry.test.mjs +79 -0
  201. package/test/terminal/local-pty.mock.test.mjs +151 -0
  202. package/test/tools-search.test.mjs +14 -1
  203. package/test/whoami-resume.test.mjs +56 -0
  204. /package/{src/lib → packages/agentsam-repository/src}/git-context.js +0 -0
  205. /package/{src/lib → packages/agentsam-repository/src}/merkle/diff.js +0 -0
  206. /package/{src/lib → packages/agentsam-repository/src}/merkle/filemeta.js +0 -0
  207. /package/{src/lib → packages/agentsam-repository/src}/merkle/git-ignore.js +0 -0
  208. /package/{src/lib → packages/agentsam-repository/src}/merkle/hash.js +0 -0
  209. /package/{src/lib → packages/agentsam-repository/src}/merkle/semantic.js +0 -0
  210. /package/{src/lib → packages/agentsam-repository/src}/merkle/snapshot.js +0 -0
  211. /package/{src/lib → packages/agentsam-repository/src}/merkle/tree.js +0 -0
@@ -129,25 +129,15 @@ export function resolveWranglerMerklePersistence({
129
129
  };
130
130
  }
131
131
 
132
- function providerRepoId(git) {
133
- const host = clean(git?.remoteHost).toLowerCase();
134
- const fullName = clean(git?.repoFullName);
135
- if (!fullName) return null;
136
- if (host === 'github.com') return `github:${fullName}`;
137
- if (host === 'gitlab.com') return `gitlab:${fullName}`;
138
- if (host === 'bitbucket.org') return `bitbucket:${fullName}`;
139
- return null;
140
- }
141
-
142
132
  function safeId(value) {
143
133
  return clean(value).replace(/[^A-Za-z0-9._-]+/g, '').slice(0, 96);
144
134
  }
145
135
 
146
- function snapshotIdFor({ snapshot, repoId, captureKind, deploymentId }) {
136
+ function snapshotIdFor({ snapshot, repositoryId, captureKind, deploymentId }) {
147
137
  const deployment = safeId(deploymentId);
148
138
  if (captureKind === 'deploy' && deployment) return `mrs_dep_${deployment.toLowerCase()}`;
149
139
  const digest = createHash('sha256')
150
- .update([repoId, snapshot.rootHash, snapshot.semantic?.rootHash || '', captureKind].join('\0'))
140
+ .update([repositoryId, snapshot.rootHash, snapshot.semantic?.rootHash || '', captureKind].join('\0'))
151
141
  .digest('hex');
152
142
  return `mrs_${digest.slice(0, 24)}`;
153
143
  }
@@ -167,8 +157,8 @@ function sqlInt(value) {
167
157
  export function buildMerklePersistencePlan({
168
158
  snapshot,
169
159
  root = process.cwd(),
170
- ownerUserId,
171
- repoId,
160
+ accountId,
161
+ repositoryId,
172
162
  source = 'local',
173
163
  captureKind = 'manual',
174
164
  connectionId = null,
@@ -180,24 +170,24 @@ export function buildMerklePersistencePlan({
180
170
  wrangler,
181
171
  } = {}) {
182
172
  if (!snapshot?.rootHash || !Array.isArray(snapshot?.entries)) throw new Error('merkle_snapshot_required');
183
- const owner = clean(ownerUserId);
184
- if (!owner) throw new Error('owner_user_id_required');
173
+ const account = clean(accountId);
174
+ const repository = clean(repositoryId);
175
+ if (!account) throw new Error('account_id_required');
176
+ if (!repository) throw new Error('repository_id_required');
185
177
  if (!CAPTURE_KINDS.has(captureKind)) throw new Error(`capture_kind_invalid:${captureKind}`);
186
178
  if (!SOURCES.has(source)) throw new Error(`source_invalid:${source}`);
187
179
  if (captureKind !== 'deploy' && !clean(connectionId) && !clean(runtimeLeaseId)) throw new Error('execution_provenance_required');
188
180
  let git = null;
189
181
  try { git = resolveGitContext({ cwd: root }); } catch { git = null; }
190
- const resolvedRepoId = clean(repoId) || providerRepoId(git);
191
- if (!resolvedRepoId) throw new Error('repo_id_required');
192
- const snapshotId = snapshotIdFor({ snapshot, repoId: resolvedRepoId, captureKind, deploymentId });
182
+ const snapshotId = snapshotIdFor({ snapshot, repositoryId: repository, captureKind, deploymentId });
193
183
  const prefix = normalizeMerkleStoragePrefix(storagePrefix);
194
- const storageKey = merkleSnapshotStorageKey({ ownerUserId: owner, repoId: resolvedRepoId, snapshotId, prefix });
184
+ const storageKey = merkleSnapshotStorageKey({ accountId: account, repositoryId: repository, snapshotId, prefix });
195
185
  const createdAt = Math.floor(Date.now() / 1000);
196
186
  const classifier = snapshot.semantic?.classifier || null;
197
187
  const row = {
198
188
  snapshot_id: snapshotId,
199
- owner_user_id: owner,
200
- repo_id: resolvedRepoId,
189
+ account_id: account,
190
+ repository_id: repository,
201
191
  repository: git?.remoteUrl || null,
202
192
  source,
203
193
  manifest_format: snapshot.format || 'agentsam-merkle',
@@ -235,7 +225,7 @@ export function buildMerklePersistencePlan({
235
225
 
236
226
  export function merklePersistenceUpsertSql(row) {
237
227
  return `INSERT INTO ${MERKLE_SNAPSHOT_TABLE} (
238
- snapshot_id, owner_user_id, repo_id, repository, source,
228
+ snapshot_id, account_id, repository_id, repository, source,
239
229
  manifest_format, manifest_version, hash_algorithm, root_hash, policy_hash,
240
230
  resolved_commit_sha, resolved_tree_sha, git_branch, working_tree_dirty,
241
231
  connection_id, runtime_lease_id, storage_backend, storage_bucket, storage_key,
@@ -243,7 +233,7 @@ export function merklePersistenceUpsertSql(row) {
243
233
  capture_kind, deployment_id, worker_version_id, reference_label,
244
234
  created_at, persisted_at, metadata_root, classifier_format, classifier_version, classifier_source
245
235
  ) VALUES (
246
- ${sqlText(row.snapshot_id)}, ${sqlText(row.owner_user_id)}, ${sqlText(row.repo_id)}, ${sqlText(row.repository)}, ${sqlText(row.source)},
236
+ ${sqlText(row.snapshot_id)}, ${sqlText(row.account_id)}, ${sqlText(row.repository_id)}, ${sqlText(row.repository)}, ${sqlText(row.source)},
247
237
  ${sqlText(row.manifest_format)}, ${sqlInt(row.manifest_version)}, ${sqlText(row.hash_algorithm)}, ${sqlText(row.root_hash)}, ${sqlText(row.policy_hash)},
248
238
  ${sqlText(row.resolved_commit_sha)}, ${sqlText(row.resolved_tree_sha)}, ${sqlText(row.git_branch)}, ${sqlInt(row.working_tree_dirty)},
249
239
  ${sqlText(row.connection_id)}, ${sqlText(row.runtime_lease_id)}, ${sqlText(row.storage_backend)}, ${sqlText(row.storage_bucket)}, ${sqlText(row.storage_key)},
@@ -10,6 +10,7 @@ export async function buildSemanticMetadata(...args) {
10
10
  export { validateSemanticMetadata, metadataRoot, FILEMETA_FORMAT, FILEMETA_VERSION } from './filemeta.js';
11
11
 
12
12
  export {
13
+ MERKLE_PERSISTENCE_SCHEMA_VERSION,
13
14
  MERKLE_SNAPSHOT_SCHEMA_SQL,
14
15
  MERKLE_SNAPSHOT_STORAGE_PREFIX,
15
16
  MERKLE_SNAPSHOT_TABLE,
@@ -27,15 +27,17 @@ export function normalizeMerkleStoragePrefix(value = MERKLE_SNAPSHOT_STORAGE_PRE
27
27
  * Provider-neutral object key. The host chooses which physical bucket is bound
28
28
  * to the logical WEBSITE_ASSETS role; the SDK never owns cloud credentials.
29
29
  */
30
- export function merkleSnapshotStorageKey({ ownerUserId, repoId, snapshotId, prefix = MERKLE_SNAPSHOT_STORAGE_PREFIX } = {}) {
31
- return `${normalizeMerkleStoragePrefix(prefix)}/${safeSegment(ownerUserId, 'owner_user_id')}/${safeSegment(repoId, 'repo_id')}/${safeSegment(snapshotId, 'snapshot_id')}.json`;
30
+ export const MERKLE_PERSISTENCE_SCHEMA_VERSION = 2;
31
+
32
+ export function merkleSnapshotStorageKey({ accountId, repositoryId, snapshotId, prefix = MERKLE_SNAPSHOT_STORAGE_PREFIX } = {}) {
33
+ return `${normalizeMerkleStoragePrefix(prefix)}/${safeSegment(accountId, 'account_id')}/${safeSegment(repositoryId, 'repository_id')}/${safeSegment(snapshotId, 'snapshot_id')}.json`;
32
34
  }
33
35
 
34
36
  /** Portable D1/SQLite schema for hosts that opt into persisted Merkle snapshots. */
35
37
  export const MERKLE_SNAPSHOT_SCHEMA_SQL = `CREATE TABLE IF NOT EXISTS ${MERKLE_SNAPSHOT_TABLE} (
36
38
  snapshot_id TEXT PRIMARY KEY NOT NULL,
37
- owner_user_id TEXT NOT NULL,
38
- repo_id TEXT NOT NULL,
39
+ account_id TEXT NOT NULL,
40
+ repository_id TEXT NOT NULL,
39
41
  repository TEXT,
40
42
  source TEXT NOT NULL CHECK (source IN ('github','gitlab','bitbucket','local','upload')),
41
43
  manifest_format TEXT NOT NULL DEFAULT 'agentsam-merkle',
@@ -2,6 +2,7 @@ import { comparePaths } from './hash.js';
2
2
 
3
3
  export const DEFAULT_IGNORES = Object.freeze([
4
4
  '.git', 'node_modules', 'dist', '.DS_Store', '.agentsam/cache', '.agentsam/merkle', '.agentsam/merkle.json',
5
+ '.agentsam/backups', '.agentsam/intelligence/archive',
5
6
  ]);
6
7
  export function validPath(value, allowRoot = false) {
7
8
  return typeof value === 'string' && ((allowRoot && value === '') || (value.length > 0 &&
@@ -0,0 +1,40 @@
1
+ import assert from 'node:assert/strict';
2
+ import test from 'node:test';
3
+ import {
4
+ createRepositoryContract,
5
+ createRepositoryDependency,
6
+ createRepositoryIdentity,
7
+ } from '../src/contracts.js';
8
+
9
+ test('repository package owns account/repository graph normalization', () => {
10
+ const identity = createRepositoryIdentity({
11
+ repository_id: 'github:owner/repo',
12
+ full_name: 'owner/repo',
13
+ role: 'runtime',
14
+ });
15
+ assert.equal(identity.repository_id, 'github:owner/repo');
16
+ assert.equal(Object.hasOwn(identity, 'account_id'), false);
17
+
18
+ const contract = createRepositoryContract({
19
+ id: 'contract:runtime:v1',
20
+ account_id: 'au_test',
21
+ repository_id: identity.repository_id,
22
+ contract_key: 'runtime',
23
+ contract_version: '1',
24
+ contract_type: 'runtime',
25
+ contract_hash: `sha256:${'1'.repeat(64)}`,
26
+ });
27
+ assert.equal(contract.account_id, 'au_test');
28
+
29
+ const dependency = createRepositoryDependency({
30
+ id: 'dep:a:b',
31
+ account_id: 'au_test',
32
+ source_repository_id: 'github:owner/a',
33
+ target_repository_id: 'github:owner/b',
34
+ dependency_type: 'contract',
35
+ criticality: 'strict',
36
+ failure_policy: 'block_certification',
37
+ });
38
+ assert.equal(dependency.failure_policy, 'block_certification');
39
+ assert.throws(() => createRepositoryContract({ ...contract, workspace_id: 'ws_legacy' }), /legacy ownership field/);
40
+ });
@@ -0,0 +1,24 @@
1
+ import assert from 'node:assert/strict';
2
+ import fs from 'node:fs';
3
+ import os from 'node:os';
4
+ import path from 'node:path';
5
+ import { execFileSync } from 'node:child_process';
6
+ import test from 'node:test';
7
+ import { normalizeGitRemote, resolveGitContext } from '../src/git-context.js';
8
+
9
+ test('repository package derives resource identity from Git, not account environment', t => {
10
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agentsam-repository-git-'));
11
+ t.after(() => fs.rmSync(root, { recursive: true, force: true }));
12
+ execFileSync('git', ['init', '-q', root]);
13
+ execFileSync('git', ['-C', root, 'remote', 'add', 'origin', 'git@github.com:ExampleOrg/DemoRepo.git']);
14
+ const context = resolveGitContext({ cwd: root });
15
+ assert.equal(context.repoFullName, 'ExampleOrg/DemoRepo');
16
+ assert.equal(context.owner, 'ExampleOrg');
17
+ assert.equal(context.repo, 'DemoRepo');
18
+ assert.equal(context.revisionSha, null);
19
+ });
20
+
21
+ test('remote normalization supports HTTPS and SSH without ownership fields', () => {
22
+ assert.equal(normalizeGitRemote('https://github.com/Owner/Repo.git').repoFullName, 'Owner/Repo');
23
+ assert.equal(normalizeGitRemote('git@github.com:Owner/Repo.git').repoFullName, 'Owner/Repo');
24
+ });
@@ -3,11 +3,7 @@ import { test } from 'node:test';
3
3
  import fs from 'node:fs/promises';
4
4
  import os from 'node:os';
5
5
  import path from 'node:path';
6
- import { spawnSync } from 'node:child_process';
7
- import { fileURLToPath } from 'node:url';
8
- import { buildMerkleTree, saveSnapshot, readSnapshot, validateSnapshot, diffTrees, metadataRoot } from '../src/lib/merkle/index.js';
9
-
10
- const cli = fileURLToPath(new URL('../src/cli.js', import.meta.url));
6
+ import { buildMerkleTree, saveSnapshot, readSnapshot, validateSnapshot, diffTrees, metadataRoot } from '../src/merkle/index.js';
11
7
  async function fixture(t) {
12
8
  const root = await fs.mkdtemp(path.join(os.tmpdir(), 'agentsam-merkle-test-'));
13
9
  t.after(() => fs.rm(root, { recursive: true, force: true }));
@@ -17,8 +13,6 @@ async function write(root, name, content = name) {
17
13
  await fs.mkdir(path.dirname(path.join(root, name)), { recursive: true });
18
14
  await fs.writeFile(path.join(root, name), content);
19
15
  }
20
- function run(args, cwd) { return spawnSync(process.execPath, [cli, 'merkle', ...args], { cwd, encoding: 'utf8', timeout: 15000 }); }
21
-
22
16
  test('version 1 hashes match independent SHA-256 protocol vectors', async (t) => {
23
17
  const root = await fixture(t);
24
18
  assert.equal((await buildMerkleTree(root)).rootHash, 'sha256:6ce8ca443f3cf6c719c5cb9acc121403addf3f499eefa3db25edacf2c7fe0f94');
@@ -49,7 +43,7 @@ test('default ignores, explicit dist inclusion, literal exclusions, and empty-di
49
43
  const root = await fixture(t);
50
44
  await write(root, 'src/app.js');
51
45
  const first = await buildMerkleTree(root);
52
- for (const name of ['.git/config', 'node_modules/pkg/file', 'dist/app.js', '.DS_Store', '.agentsam/cache/data', '.agentsam/merkle/old.json']) await write(root, name);
46
+ for (const name of ['.git/config', 'node_modules/pkg/file', 'dist/app.js', '.DS_Store', '.agentsam/cache/data', '.agentsam/merkle/old.json', '.agentsam/backups/pre-cleanup.bak', '.agentsam/intelligence/archive/20260912/repo_intelligence.json']) await write(root, name);
53
47
  await fs.mkdir(path.join(root, 'empty'));
54
48
  assert.equal(first.rootHash, (await buildMerkleTree(root)).rootHash);
55
49
  const withDist = await buildMerkleTree(root, { include: ['dist'] });
@@ -172,30 +166,6 @@ test('scan fails when a file disappears and honors cancellation', async (t) => {
172
166
  await assert.rejects(buildMerkleTree(root, { signal: controller.signal }), { name: 'AbortError' });
173
167
  });
174
168
 
175
- test('CLI snapshot/verify/diff supports JSON, moved roots, and distinct mismatch/error exit codes', async (t) => {
176
- const root = await fixture(t), moved = await fixture(t);
177
- await write(root, 'a.txt', 'hello'); await write(moved, 'a.txt', 'hello');
178
- const saved = run(['snapshot', '.', '--json'], root);
179
- assert.equal(saved.status, 0, saved.stderr);
180
- const manifest = JSON.parse(saved.stdout);
181
- assert.ok(manifest.output.endsWith('merkle.json'));
182
- const matching = run(['verify', manifest.output, '--root', moved, '--json'], root);
183
- assert.equal(matching.status, 0, matching.stderr);
184
- assert.equal(JSON.parse(matching.stdout).equal, true);
185
- await write(moved, 'a.txt', 'changed'); await write(moved, 'b.txt', 'new');
186
- const changed = run(['verify', manifest.output, '--root', moved, '--json'], root);
187
- assert.equal(changed.status, 1, changed.stderr);
188
- assert.deepEqual(JSON.parse(changed.stdout).stats, { unchanged: 0, modified: 1, added: 1, removed: 0 });
189
- const diff = run(['diff', root, moved, '--json'], root);
190
- assert.equal(diff.status, 1, diff.stderr);
191
- const invalid = run(['root', '.', '--typo', '--json'], root);
192
- assert.equal(invalid.status, 2); assert.equal(invalid.stdout, '');
193
- assert.match(JSON.parse(invalid.stderr).error, /Unknown option/);
194
- const piped = run(['inspect', '.', '--tui'], root);
195
- assert.equal(piped.status, 0, piped.stderr);
196
- assert.ok(!piped.stdout.includes('\x1b'));
197
- });
198
-
199
169
  test('semantic index records execution domains, environment access, and resolved local imports as Merkle-bound evidence', async (t) => {
200
170
  const root = await fixture(t);
201
171
  await write(root, 'package.json', JSON.stringify({ name: 'boundary-fixture', version: '1.0.0' }));
@@ -1,6 +1,7 @@
1
1
  import test from 'node:test';
2
2
  import assert from 'node:assert/strict';
3
3
  import {
4
+ MERKLE_PERSISTENCE_SCHEMA_VERSION,
4
5
  MERKLE_SNAPSHOT_SCHEMA_SQL,
5
6
  MERKLE_SNAPSHOT_STORAGE_PREFIX,
6
7
  MERKLE_SNAPSHOT_TABLE,
@@ -9,15 +10,19 @@ import {
9
10
  merklePersistenceUpsertSql,
10
11
  merkleSnapshotStorageKey,
11
12
  resolveWranglerMerklePersistence,
12
- } from '../src/lib/merkle/index.js';
13
+ } from '../src/merkle/index.js';
13
14
  import fs from 'node:fs';
14
15
  import os from 'node:os';
15
16
  import path from 'node:path';
16
17
 
17
18
  test('portable Merkle persistence names one table, prefix, and logical asset role', () => {
19
+ assert.equal(MERKLE_PERSISTENCE_SCHEMA_VERSION, 2);
18
20
  assert.equal(MERKLE_SNAPSHOT_TABLE, 'agentsam_fs_merkle_snapshots');
19
21
  assert.equal(MERKLE_SNAPSHOT_STORAGE_PREFIX, 'agentsam_fs_merkle_snapshots');
20
22
  assert.equal(MERKLE_WEBSITE_ASSETS_BINDING, 'WEBSITE_ASSETS');
23
+ assert.match(MERKLE_SNAPSHOT_SCHEMA_SQL, /account_id TEXT NOT NULL/);
24
+ assert.match(MERKLE_SNAPSHOT_SCHEMA_SQL, /repository_id TEXT NOT NULL/);
25
+ assert.doesNotMatch(MERKLE_SNAPSHOT_SCHEMA_SQL, /owner_user_id|\brepo_id\b/);
21
26
  assert.match(MERKLE_SNAPSHOT_SCHEMA_SQL, /metadata_root TEXT/);
22
27
  assert.match(MERKLE_SNAPSHOT_SCHEMA_SQL, /classifier_format TEXT/);
23
28
  assert.match(MERKLE_SNAPSHOT_SCHEMA_SQL, /storage_bucket TEXT/);
@@ -25,12 +30,12 @@ test('portable Merkle persistence names one table, prefix, and logical asset rol
25
30
 
26
31
  test('snapshot storage keys stay beneath the canonical prefix and encode repo identity', () => {
27
32
  const key = merkleSnapshotStorageKey({
28
- ownerUserId: 'au_example',
29
- repoId: 'github:owner/repo',
33
+ accountId: 'au_example',
34
+ repositoryId: 'github:owner/repo',
30
35
  snapshotId: 'mrs_example',
31
36
  });
32
37
  assert.equal(key, 'agentsam_fs_merkle_snapshots/au_example/github%3Aowner%2Frepo/mrs_example.json');
33
- const escaped = merkleSnapshotStorageKey({ ownerUserId: '../oops', repoId: 'repo', snapshotId: 'snap' });
38
+ const escaped = merkleSnapshotStorageKey({ accountId: '../oops', repositoryId: 'repo', snapshotId: 'snap' });
34
39
  assert.ok(!escaped.includes('/../'));
35
40
  assert.ok(escaped.includes('..%2Foops'));
36
41
  });
@@ -63,7 +68,7 @@ test('persistence plan keeps content, policy, and metadata identities separate',
63
68
  },
64
69
  };
65
70
  const plan = buildMerklePersistencePlan({
66
- snapshot, root: process.cwd(), ownerUserId: 'au_test', repoId: 'github:owner/repo', source: 'github',
71
+ snapshot, root: process.cwd(), accountId: 'au_test', repositoryId: 'github:owner/repo', source: 'github',
67
72
  captureKind: 'agent', connectionId: 'conn_test',
68
73
  wrangler: { storage_bucket: 'customer-assets', r2_binding: 'WEBSITE_ASSETS', database_name: 'customer-db', d1_binding: 'DB' },
69
74
  });
@@ -85,7 +90,7 @@ test('non-deploy persistence requires execution provenance', () => {
85
90
  policyHash: `sha256:${'2'.repeat(64)}`, entries: [], stats: { files: 0, directories: 0, symlinks: 0, bytes: 0 },
86
91
  };
87
92
  assert.throws(() => buildMerklePersistencePlan({
88
- snapshot, root: process.cwd(), ownerUserId: 'au_test', repoId: 'github:owner/repo', captureKind: 'agent',
93
+ snapshot, root: process.cwd(), accountId: 'au_test', repositoryId: 'github:owner/repo', captureKind: 'agent',
89
94
  wrangler: { storage_bucket: 'customer-assets', r2_binding: 'WEBSITE_ASSETS', database_name: 'customer-db', d1_binding: 'DB' },
90
95
  }), /execution_provenance_required/);
91
96
  });
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "@inneranimalmedia/agentsam-connector-cloudflare",
3
+ "version": "0.1.0",
4
+ "private": true,
5
+ "type": "module",
6
+ "main": "./src/index.js",
7
+ "exports": {
8
+ ".": "./src/index.js"
9
+ }
10
+ }
@@ -0,0 +1,127 @@
1
+ /**
2
+ * Cloudflare account connector (not an identity provider).
3
+ * Answers: what Cloudflare account has this authenticated AgentSam user authorized?
4
+ */
5
+
6
+ export const CLOUDFLARE_OAUTH_AUTHORIZE_URL = 'https://dash.cloudflare.com/oauth2/auth';
7
+ export const CLOUDFLARE_OAUTH_TOKEN_URL = 'https://dash.cloudflare.com/oauth2/token';
8
+ export const CLOUDFLARE_CALLBACK_PATH = '/api/connections/cloudflare/callback';
9
+ export const CLOUDFLARE_FIXTURE_CLIENT_ID = 'sillynotreal';
10
+ export const CLOUDFLARE_FIXTURE_CLIENT_SECRET = 'sillynotreal-secret';
11
+
12
+ export const CLOUDFLARE_OAUTH_REVOKE_URL = 'https://dash.cloudflare.com/oauth2/revoke';
13
+
14
+ /** Smallest useful scopes mapped to Cloudflare API token permission names. */
15
+ export const CLOUDFLARE_CAPABILITY_SCOPES = Object.freeze({
16
+ workers_deploy: {
17
+ scopes: ['workers-scripts.write'],
18
+ why: 'Deploy Workers for the connected account (Workers Scripts Edit).',
19
+ },
20
+ d1_inspect: {
21
+ scopes: ['d1.read'],
22
+ why: 'Inspect D1 databases bound to the deployable.',
23
+ },
24
+ r2_inspect: {
25
+ scopes: ['workers-r2-storage.read'],
26
+ why: 'Inspect R2 buckets bound to the deployable.',
27
+ },
28
+ worker_logs: {
29
+ scopes: ['workers-scripts.read'],
30
+ why: 'Read Worker script metadata/logs for postdeploy health.',
31
+ },
32
+ });
33
+
34
+ export function requestedCloudflareScopes() {
35
+ const set = new Set();
36
+ for (const cap of Object.values(CLOUDFLARE_CAPABILITY_SCOPES)) {
37
+ for (const scope of cap.scopes) set.add(scope);
38
+ }
39
+ return [...set];
40
+ }
41
+
42
+ function clean(value) {
43
+ return value == null ? '' : String(value).trim();
44
+ }
45
+
46
+ export function isFixtureCloudflareCredential(value) {
47
+ const v = clean(value);
48
+ return v === CLOUDFLARE_FIXTURE_CLIENT_ID || v === CLOUDFLARE_FIXTURE_CLIENT_SECRET;
49
+ }
50
+
51
+ export function resolveCloudflareOAuthClient(env = {}) {
52
+ const clientId = clean(env.CLOUDFLARE_OAUTH_CLIENT_ID);
53
+ const clientSecret = clean(env.CLOUDFLARE_OAUTH_CLIENT_SECRET);
54
+ const present = Boolean(clientId && clientSecret);
55
+ const fixture = isFixtureCloudflareCredential(clientId) || isFixtureCloudflareCredential(clientSecret);
56
+ if (!present) {
57
+ return {
58
+ configured: false,
59
+ productionReady: false,
60
+ fixture: false,
61
+ status: 'not_configured',
62
+ clientIdConfigured: Boolean(clientId),
63
+ secretConfigured: Boolean(clientSecret),
64
+ };
65
+ }
66
+ return {
67
+ configured: true,
68
+ productionReady: !fixture,
69
+ fixture,
70
+ status: fixture ? 'fixture' : 'ready',
71
+ clientIdConfigured: true,
72
+ secretConfigured: true,
73
+ };
74
+ }
75
+
76
+ export function cloudflareConnectionSafeStatus(env = {}, connection = null, ownerId = '') {
77
+ const client = resolveCloudflareOAuthClient(env);
78
+ const record = connection && connection.ownerId === ownerId ? connection : null;
79
+ return {
80
+ provider: 'cloudflare',
81
+ status: record ? 'connected' : client.status === 'ready' ? 'ready' : client.status,
82
+ configured: client.configured && client.productionReady,
83
+ fixture: client.fixture,
84
+ clientId: client.clientIdConfigured ? 'configured' : 'missing',
85
+ secret: client.secretConfigured ? 'configured' : 'missing',
86
+ callbackPath: CLOUDFLARE_CALLBACK_PATH,
87
+ connection: record
88
+ ? {
89
+ connection_id: record.connectionId,
90
+ owner: record.ownerId,
91
+ cloudflare_account_id: record.cloudflareAccountId || null,
92
+ scopes: record.scopes || [],
93
+ status: record.status,
94
+ created_at: record.createdAt,
95
+ updated_at: record.updatedAt,
96
+ expires_at: record.expiresAt || null,
97
+ }
98
+ : null,
99
+ };
100
+ }
101
+
102
+ export function assertConnectionOwner(connection, ownerId) {
103
+ if (!ownerId) {
104
+ const err = new Error('unauthenticated');
105
+ err.code = 'unauthenticated';
106
+ throw err;
107
+ }
108
+ if (!connection || connection.ownerId !== ownerId) {
109
+ const err = new Error('cloudflare_connection_forbidden');
110
+ err.code = 'cloudflare_connection_forbidden';
111
+ throw err;
112
+ }
113
+ return connection;
114
+ }
115
+
116
+ export function buildAuthorizeUrl({ clientId, redirectUri, state, codeChallenge, scopes }) {
117
+ const params = new URLSearchParams({
118
+ response_type: 'code',
119
+ client_id: clientId,
120
+ redirect_uri: redirectUri,
121
+ state,
122
+ code_challenge: codeChallenge,
123
+ code_challenge_method: 'S256',
124
+ scope: (scopes || requestedCloudflareScopes()).join(' '),
125
+ });
126
+ return `${CLOUDFLARE_OAUTH_AUTHORIZE_URL}?${params}`;
127
+ }
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Cloudflare connection ownership is derived from authenticated AgentSam session.
3
+ * Browser-submitted account_id / user_id / owner_id / X-User-Id are never authority.
4
+ */
5
+
6
+ const UNTRUSTED = new Set(['account_id', 'user_id', 'owner_id', 'workspace_id']);
7
+
8
+ export function extractSessionToken(request) {
9
+ const auth = request.headers.get('authorization') || '';
10
+ if (/^bearer\s+/i.test(auth)) {
11
+ const token = auth.replace(/^bearer\s+/i, '').trim();
12
+ if (token && !token.startsWith('cf_') && token !== 'sillynotreal-secret') return token;
13
+ }
14
+ const cookie = request.headers.get('cookie') || '';
15
+ const match = cookie.match(/(?:^|;\s*)agentsam_session=([^;]+)/);
16
+ return match ? decodeURIComponent(match[1]).trim() : '';
17
+ }
18
+
19
+ export function rejectUntrustedOwnerHints(request, url, body = {}) {
20
+ const headerUser = (request.headers.get('x-user-id') || '').trim();
21
+ for (const key of UNTRUSTED) {
22
+ if (url.searchParams.get(key)) {
23
+ const err = new Error('untrusted_owner_hint');
24
+ err.code = 'untrusted_owner_hint';
25
+ throw err;
26
+ }
27
+ if (body && body[key]) {
28
+ const err = new Error('untrusted_owner_hint');
29
+ err.code = 'untrusted_owner_hint';
30
+ throw err;
31
+ }
32
+ }
33
+ // X-User-Id is vault v1 compatibility only and is never connector authority.
34
+ return { ignoredXUserId: Boolean(headerUser) };
35
+ }
36
+
37
+ export async function resolveAuthenticatedOwner(request, env, url, body) {
38
+ rejectUntrustedOwnerHints(request, url, body);
39
+ const sessionToken = extractSessionToken(request);
40
+ if (!sessionToken) {
41
+ const err = new Error('unauthenticated');
42
+ err.code = 'unauthenticated';
43
+ throw err;
44
+ }
45
+ if (env?.sessions instanceof Map) {
46
+ const owner = env.sessions.get(sessionToken);
47
+ if (!owner) {
48
+ const err = new Error('unauthenticated');
49
+ err.code = 'unauthenticated';
50
+ throw err;
51
+ }
52
+ return String(owner);
53
+ }
54
+ if (env?.DB?.prepare) {
55
+ const tables = [
56
+ ['agentsam_sessions', 'session_token', 'user_id'],
57
+ ['sessions', 'id', 'user_id'],
58
+ ['identity_sessions', 'session_token', 'user_id'],
59
+ ];
60
+ for (const [table, tokenCol, userCol] of tables) {
61
+ try {
62
+ const row = await env.DB.prepare(
63
+ `SELECT ${userCol} AS user_id FROM ${table} WHERE ${tokenCol} = ? LIMIT 1`,
64
+ )
65
+ .bind(sessionToken)
66
+ .first();
67
+ if (row?.user_id) return String(row.user_id);
68
+ } catch {
69
+ // table may not exist yet
70
+ }
71
+ }
72
+ }
73
+ const err = new Error('unauthenticated');
74
+ err.code = 'unauthenticated';
75
+ throw err;
76
+ }