Skip to main content

agentmux_srv_core/backend/
bundle_import.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Armory Bundle Format (ABF) importer — Phase 2 of
5//! `docs/specs/SPEC_ABF_V0_1_SINGLE_FILE_AND_IMPORTER_2026_08_01.md`.
6//! Inverse of `bundle_export.rs`: takes a bundle's files (either a raw
7//! `[{path, content}]` list or an unpacked `.abf` zip archive) and
8//! validates + parses them into data ready for the `bundle.import` RPC
9//! handler to write to the Store.
10//!
11//! Pure functions only — no I/O, no Store access, no account resolution
12//! (account resolution needs `db_accounts`, which only the RPC handler has
13//! access to; see the spec's §4.5). Mirrors `bundle_export.rs`'s shape and
14//! quality bar deliberately: the same "warn, don't silently drop" and
15//! "reject the whole thing on structural failure, but never partially
16//! import on a per-entry problem" philosophy, since the two modules are
17//! meant to round-trip against each other.
18
19use std::collections::{HashMap, HashSet};
20
21use serde::{Deserialize, Serialize};
22use serde_json::Value;
23
24use super::bundle_export::sanitize_context_relative_path;
25
26/// Bounds how many warnings a parse call accumulates, and how long each
27/// individual warning string may be — Phase 3 spec
28/// (`SPEC_ABF_IMPORT_UI_PHASE3_2026_08_02.md`) §3.1, round 11: the cap must
29/// live at the point warnings are actually PRODUCED, not only at a later
30/// RPC-response-serialization boundary, or a hostile archive can still
31/// force the parser itself to build an unbounded `Vec<String>` in memory
32/// before any cap ever runs. `unbounded()` preserves today's existing
33/// `bundle.import` route's real, already-shipped behavior exactly — this is
34/// a capability the parser gains, not a behavior change forced onto Phase 2's
35/// existing caller.
36#[derive(Debug, Clone, Copy)]
37pub struct WarningBudget {
38    max_count: Option<usize>,
39    max_len: Option<usize>,
40}
41
42impl WarningBudget {
43    pub fn unbounded() -> Self {
44        Self { max_count: None, max_len: None }
45    }
46
47    pub fn bounded(max_count: usize, max_len: usize) -> Self {
48        Self { max_count: Some(max_count), max_len: Some(max_len) }
49    }
50}
51
52/// Accumulates warnings against a [`WarningBudget`]. Exposes `.push(String)`
53/// so every existing `warnings.push(format!(...))` call site in this module
54/// keeps working unchanged after its binding's type changes from
55/// `Vec<String>` to this — only construction (`WarningSink::new`) and
56/// extraction (`.into_vec()`) differ.
57#[derive(Debug, Clone)]
58struct WarningSink {
59    budget: WarningBudget,
60    warnings: Vec<String>,
61    dropped: usize,
62}
63
64impl WarningSink {
65    fn new(budget: WarningBudget) -> Self {
66        Self { budget, warnings: Vec::new(), dropped: 0 }
67    }
68
69    fn push(&mut self, message: String) {
70        if let Some(max_count) = self.budget.max_count {
71            if self.warnings.len() >= max_count {
72                self.dropped += 1;
73                return;
74            }
75        }
76        let message = match self.budget.max_len {
77            Some(max_len) => truncate_display(&message, max_len),
78            None => message,
79        };
80        self.warnings.push(message);
81    }
82
83    fn into_vec(mut self) -> Vec<String> {
84        if self.dropped > 0 {
85            self.warnings.push(format!("... {} more warning(s) not shown", self.dropped));
86        }
87        self.warnings
88    }
89}
90
91/// Fixed-character truncation shared by every bounded-display projection
92/// this module (and its RPC callers, Phase 3 spec §3.1/§3.2) define —
93/// stated once so `preview` and `commit` always apply IDENTICAL bounds to
94/// the equivalent field, rather than independent per-endpoint copies
95/// drifting apart (the exact class of gap codex found and re-found across
96/// rounds 9 and 12).
97pub fn truncate_display(s: &str, max_chars: usize) -> String {
98    if s.chars().count() <= max_chars {
99        s.to_string()
100    } else {
101        let mut truncated: String = s.chars().take(max_chars).collect();
102        truncated.push_str("...");
103        truncated
104    }
105}
106
107/// Cap on `instructions_preview` (Phase 3 spec §3.1, round 5) — generous for
108/// a glance-and-decide preview; bounds worst-case JSON-escaped response size
109/// to roughly 300 KB rather than the ~300 MB a maximally-adversarial,
110/// unbounded `instructions` string could otherwise force.
111pub const MAX_INSTRUCTIONS_PREVIEW_CHARS: usize = 50_000;
112
113/// Fixed-character display cap shared by every "meant to be short" bounded
114/// field this spec defines: skill `description`/`slug` (rounds 10/12),
115/// requirement `id`/`provider`/`env` (round 11), context-file
116/// `display_path` (round 13), and bundle `description` (round 12 self-audit).
117pub const MAX_DISPLAY_FIELD_CHARS: usize = 300;
118
119/// Fixed-character display cap for an MCP server's projected `name`/
120/// `command` (Phase 3 spec §3.1, round 7) — smaller than
121/// [`MAX_DISPLAY_FIELD_CHARS`] since these are meant to be short
122/// identifiers/executable names, not free-form text.
123pub const MAX_MCP_DISPLAY_FIELD_CHARS: usize = 200;
124
125/// Bounds `instructions_preview` for the RPC response — returns
126/// `(preview, truncated, total_chars)`. The full, untruncated `instructions`
127/// value is unaffected; this only bounds what's echoed back for display
128/// (Phase 3 spec §3.1, rounds 5 and 8).
129pub fn bounded_instructions_preview(instructions: &str) -> (String, bool, usize) {
130    let total_chars = instructions.chars().count();
131    if total_chars <= MAX_INSTRUCTIONS_PREVIEW_CHARS {
132        (instructions.to_string(), false, total_chars)
133    } else {
134        (instructions.chars().take(MAX_INSTRUCTIONS_PREVIEW_CHARS).collect(), true, total_chars)
135    }
136}
137
138/// Bounded `{name, command}` projection of an MCP server's full `config`
139/// (Phase 3 spec §3.1, round 7) — the full `config` is never returned in
140/// preview/commit responses, only this small, defensively-extracted
141/// projection. Falls back to `null` for either field when absent or not a
142/// string, since MCP JSON has no required shape (§3.0).
143pub fn mcp_server_display(config: &Value) -> Value {
144    let field = |key: &str| -> Value {
145        match config.get(key).and_then(|v| v.as_str()) {
146            Some(s) => Value::String(truncate_display(s, MAX_MCP_DISPLAY_FIELD_CHARS)),
147            None => Value::Null,
148        }
149    };
150    serde_json::json!({ "name": field("name"), "command": field("command") })
151}
152
153/// Every parsed skill slug that appears more than once across the WHOLE
154/// bundle — the `"duplicate_in_bundle"` collision pass (Phase 3 spec §3.1,
155/// codex P1 round 2), computed independently of any external global-catalog
156/// state so it's pure and directly testable.
157pub fn duplicate_in_bundle_slugs(skills: &[ParsedSkill]) -> HashSet<String> {
158    let mut seen: HashSet<&str> = HashSet::new();
159    let mut dupes: HashSet<String> = HashSet::new();
160    for skill in skills {
161        if !seen.insert(skill.slug.as_str()) {
162            dupes.insert(skill.slug.clone());
163        }
164    }
165    dupes
166}
167
168/// `"none"` / `"name_conflict"` / `"duplicate_in_bundle"` — Phase 3 spec
169/// §3.1's two-pass skill collision classification. `global_slugs` is
170/// whatever the caller already fetched from `skill.catalog.list`'s
171/// underlying `skill_list_global()` (Store access lives in the RPC
172/// handler, not here — this stays a pure function so it's testable with a
173/// seeded fake global-skill list, per the spec's own §6 testing notes).
174/// Pass 1 (global catalog) takes priority over pass 2 (intra-bundle
175/// duplicate) when both apply, matching §3.1's stated precedence.
176pub fn classify_skill_collision(slug: &str, global_slugs: &HashSet<String>, in_bundle_dupes: &HashSet<String>) -> &'static str {
177    if global_slugs.contains(slug) {
178        "name_conflict"
179    } else if in_bundle_dupes.contains(slug) {
180        "duplicate_in_bundle"
181    } else {
182        "none"
183    }
184}
185
186/// One file read from an import source (zip or raw list), path relative
187/// to the bundle root. Mirrors `bundle_export::BundleExportFile`.
188#[derive(Debug, Clone)]
189pub struct BundleImportFile {
190    pub path: String,
191    pub content: String,
192}
193
194/// A skill parsed out of a `skills/<slug>/SKILL.md` file — the inverse of
195/// `agent_config::render_skill_md`.
196#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
197pub struct ParsedSkill {
198    /// The exact `components.skills` directory reference that produced this
199    /// entry (Phase 3 spec §3.0) — a stable, always-unique selection key,
200    /// independent of `slug` (which two entries can share; see the
201    /// `"duplicate_in_bundle"` collision case). Never displayed truncated;
202    /// only used to match a preview row to a commit selection.
203    pub source_dir: String,
204    /// The slug from SKILL.md's own `name` field (Agent Skills spec:
205    /// must match the parent directory) — reused as both the imported
206    /// skill's display name and its `trigger`. Agent-skill-type skills
207    /// don't materialize a slash-command file from `trigger` (see
208    /// `agent_config.rs`'s `build_config_files`), so this is a safe,
209    /// deterministic default rather than a meaningful distinct value.
210    pub slug: String,
211    pub description: String,
212    pub content: String,
213}
214
215/// An MCP server parsed out of a `components.mcpServers` reference — Phase
216/// 3 spec §3.0. `source_path` is the stable, always-unique selection key
217/// (the manifest path reference, distinct from whatever `"name"` field
218/// happens to appear inside `config`'s arbitrary JSON, which has no
219/// uniqueness guarantee and isn't even required to be present).
220#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
221pub struct ParsedMcpServer {
222    pub source_path: String,
223    pub config: Value,
224}
225
226/// One `accounts/requirements.json` entry — mirrors the shape
227/// `bundle_export.rs` writes (research report §5.3).
228#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
229pub struct AccountRequirement {
230    pub id: String,
231    /// ABF v0.2 (SPEC_ABF_V0_2_PROVIDER_AWARE_COMPONENTS_AND_NATIVE_MEMORY_
232    /// 2026_08_10.md §2.1): the wire key is `credentialProvider`, renamed
233    /// from the ambiguous v0.1 `provider` (which collided with the
234    /// unrelated harness/model-vendor "provider" concept components.
235    /// instructions now uses — §2.2). `alias` keeps a v0.1-produced bundle
236    /// (still carrying the old key) importing unchanged; new exports only
237    /// ever write `credentialProvider`. The Rust field itself keeps its
238    /// name — every internal caller (account-requirement resolution,
239    /// `db_accounts.provider` matching) already reads it as "which
240    /// credential service", which was always accurate; only the wire
241    /// format needed disambiguating from the newer, unrelated sense.
242    #[serde(rename = "credentialProvider", alias = "provider")]
243    pub provider: String,
244    #[serde(default)]
245    pub kind: String,
246    #[serde(default)]
247    pub env: String,
248    #[serde(default)]
249    pub optional: bool,
250}
251
252/// One `instructions/context/*` file, in the `[{path, content}]` shape
253/// `db_bundles.context_files` stores.
254#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
255pub struct ImportedContextFile {
256    /// 0-based index within this parse's `context_files` list (Phase 3
257    /// spec §3.1, round 13) — the stable selection key `bundle.import.commit`'s
258    /// `include_context_files` uses. Deterministic and reusable across
259    /// `preview`/`commit` because `expected_content_digest` already
260    /// guarantees both calls parse identical content, so the same index
261    /// always means the same entry. Never used for display.
262    pub id: usize,
263    pub path: String,
264    pub content: String,
265}
266
267/// One `components.projectInstructions` entry, read back out of the bundle.
268///
269/// Phase 3 of `SPEC_INSTRUCTION_AND_MEMORY_PORTABILITY_2026_09_09.md`. This is
270/// a **record of what the SOURCE agent was reading**, not content to install:
271/// no import path writes these files, and `owner` is the field that says why
272/// (a `foreign` entry belongs to the source repository, and writing it into
273/// the importing one is exactly what
274/// `SPEC_CLAUDE_MD_OWNERSHIP_PROTECTION_2026_08_22.md` exists to prevent).
275///
276/// Carried through the parser rather than being reduced to a warning because
277/// "surfaced" has to mean readable to be worth anything: `agent.
278/// project_instructions` scans the IMPORTING agent's working directory, so it
279/// is structurally unable to show what the source machine had (Codex P1,
280/// PR #3163). Without this the import flow could tell you the component
281/// existed and nothing more.
282#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
283pub struct ImportedProjectInstruction {
284    /// Working-directory-relative path on the SOURCE machine, e.g. `CLAUDE.md`.
285    pub path: String,
286    /// Where the snapshot lives inside the bundle (`instructions/project/...`).
287    pub file: String,
288    /// SHA-256 the exporter recorded for the source bytes. Kept verbatim and
289    /// never recomputed here — a mismatch against `content` is itself
290    /// information about the bundle, not something to paper over.
291    pub content_hash: String,
292    /// `"agentmux"` or `"foreign"`, verbatim from the manifest. Not parsed
293    /// into an enum: an unrecognized value from a future/hand-written bundle
294    /// must survive to the UI as-is rather than being silently coerced to
295    /// `foreign`, which would understate AgentMux's own authorship.
296    pub owner: String,
297    /// The snapshot's content, resolved from the bundle's files.
298    pub content: String,
299}
300
301/// Parsed, validated bundle content ready for the RPC handler to resolve
302/// accounts against and write to the Store. `mcp_servers` still contains
303/// whatever `${VAR}`-style placeholders the exporter's redaction left in
304/// place — account resolution (§4.5, RPC-handler-side) substitutes real
305/// account bindings where exactly one match is found and leaves the rest
306/// untouched.
307#[derive(Debug, Clone, Serialize)]
308pub struct ParsedBundleImport {
309    pub name: String,
310    pub description: String,
311    /// ARCHITECTURE_MANDATORY_ABF_RETHINK_2026_08_14.md §7.4.3: harness +
312    /// vendor, carried through from the manifest's `suggestedFor` (v0.3) or
313    /// top-level `provider`/`model` (v0.1–v0.2). Empty when it has neither
314    /// (an older export predating this, or a bundle made with no hint). A
315    /// hint only: it never sets an agent's provider.
316    pub provider: String,
317    pub model: String,
318    pub instructions: String,
319    /// ABF v0.2 §2.2: `{provider_id: content}` — every non-"default"
320    /// variant found in a v0.2 `components.instructions` object, stored
321    /// verbatim with no merge decision made here. Empty for a v0.1 bundle
322    /// (flat array) or a v0.2 bundle with no provider-scoped variants.
323    pub instructions_by_provider: HashMap<String, String>,
324    pub context_files: Vec<ImportedContextFile>,
325    pub mcp_servers: Vec<ParsedMcpServer>,
326    pub skills: Vec<ParsedSkill>,
327    pub skipped_skills: Vec<String>,
328    pub requirements: Vec<AccountRequirement>,
329    /// What the SOURCE agent was reading as project instructions. Read-only —
330    /// see [`ImportedProjectInstruction`]. No caller writes these anywhere;
331    /// `PROJECT_INSTRUCTIONS_NOT_APPLIED_WARNING` rides alongside them.
332    pub project_instructions: Vec<ImportedProjectInstruction>,
333    pub warnings: Vec<String>,
334}
335
336/// Normalize every input file's path and reduce to a first-wins,
337/// deduped `(path -> content)` map — the exact effective representation
338/// `parse_bundle_import` builds internally before doing anything else.
339/// Extracted so the Phase 3 `files`-mode content digest (§3.0.5, round 6)
340/// can compute over the IDENTICAL order-resolved representation the parser
341/// itself uses, rather than a naive raw-input sort that could disagree
342/// with which entry the parser's own first-wins rule actually keeps.
343///
344/// Also enforces the accounts/ allowlist (§4.3.5): only
345/// `accounts/requirements.json` is ever readable from that directory,
346/// checked against every file actually present (not just what the
347/// manifest references, since a malformed bundle's `components` object
348/// isn't a trustworthy inventory of its own contents), and normalized the
349/// same way regardless of intake source (zip or raw `files` list) so
350/// neither can bypass the check with a non-canonical spelling
351/// (codex P1, PR #2379 rounds 1–2). A rejected file is excluded from the
352/// returned map entirely, not merely warned about — otherwise a
353/// `components.*` reference pointing at it (e.g.
354/// `accounts/secrets.json`) would still resolve and leak its content into
355/// the imported bundle.
356fn dedup_files_by_path<'a>(files: &'a [BundleImportFile], warnings: &mut WarningSink) -> HashMap<String, &'a str> {
357    let mut by_path: HashMap<String, &str> = HashMap::new();
358    for f in files {
359        let Some(safe_path) = sanitize_context_relative_path(&f.path) else {
360            warnings.push(format!("{}: not a safe path; skipped", f.path));
361            continue;
362        };
363        // reagent P1, PR #2379 round 4: case-insensitive on the DIRECTORY
364        // check -- sanitize_context_relative_path never case-folds, so
365        // `ACCOUNTS/secrets.json` (or any other-case variant) previously
366        // sailed past the literal lowercase `starts_with` check while a
367        // manifest reference using the identical casing would still
368        // resolve it. The exception itself stays exact-match against the
369        // canonical lowercase spelling the exporter always emits — an
370        // other-case "requirements.json" is still inside the rejected
371        // directory, just not recognized as the one allowed file.
372        if safe_path.to_ascii_lowercase().starts_with("accounts/") && safe_path != "accounts/requirements.json" {
373            warnings.push(format!(
374                "{safe_path}: rejected — only accounts/requirements.json is ever read from the accounts/ directory"
375            ));
376            continue;
377        }
378        // reagent P2, PR #2379 round 5: `unzip_bundle_import` explicitly
379        // detects and warns on a duplicate entry within a zip ("first
380        // occurrence kept"), but the raw `files` RPC list reaches this
381        // shared loop directly, bypassing that pass entirely — two input
382        // entries normalizing to the same safe_path silently resolved
383        // last-write-wins with no warning, so a caller inspecting the
384        // input couldn't tell which content actually got imported.
385        if by_path.contains_key(&safe_path) {
386            warnings.push(format!("{safe_path}: duplicate path in input; first occurrence kept"));
387            continue;
388        }
389        by_path.insert(safe_path, f.content.as_str());
390    }
391    by_path
392}
393
394/// Which of the three `bundle.import.preview`/`.commit` input fields
395/// produced a given payload — mixed into the content-digest hash domain
396/// itself (Phase 3 spec §3.0.5, round 7) so a `file_path` preview can never
397/// be satisfied by a `zip_base64` commit of the identical underlying bytes
398/// (both canonicalize to the same raw zip bytes and would otherwise hash
399/// identically), closing the gap a bare byte-digest comparison left open.
400#[derive(Debug, Clone, Copy, PartialEq, Eq)]
401pub enum ImportInputMode {
402    FilePath,
403    ZipBase64,
404    Files,
405}
406
407impl ImportInputMode {
408    fn mode_byte(self) -> u8 {
409        match self {
410            ImportInputMode::FilePath => 0x01,
411            ImportInputMode::ZipBase64 => 0x02,
412            ImportInputMode::Files => 0x03,
413        }
414    }
415}
416
417/// SHA-256 content digest for `file_path`/`zip_base64` input — both
418/// canonicalize to the same thing (raw zip bytes), differentiated only by
419/// the mode tag mixed into the hash domain (§3.0.5, round 7).
420pub fn content_digest_raw_bytes(mode: ImportInputMode, zip_bytes: &[u8]) -> String {
421    use sha2::{Digest, Sha256};
422    let mut hasher = Sha256::new();
423    hasher.update([mode.mode_byte()]);
424    hasher.update(zip_bytes);
425    hex::encode(hasher.finalize())
426}
427
428/// SHA-256 content digest for `files`-mode input (§3.0.5, round 6) — hashes
429/// `parse_bundle_import`'s own effective, order-resolved representation
430/// ([`dedup_files_by_path`]'s normalize-then-first-wins reduction, sorted
431/// by normalized key), not the raw input array. This makes the digest
432/// order-independent for genuinely equivalent inputs while remaining
433/// sensitive to any reordering that would actually change which entry the
434/// parser's first-wins rule keeps.
435pub fn content_digest_files(files: &[BundleImportFile]) -> String {
436    use sha2::{Digest, Sha256};
437    let mut discard = WarningSink::new(WarningBudget::unbounded());
438    let deduped = dedup_files_by_path(files, &mut discard);
439    let mut entries: Vec<(&String, &&str)> = deduped.iter().collect();
440    entries.sort_by(|a, b| a.0.cmp(b.0));
441    let mut hasher = Sha256::new();
442    hasher.update([ImportInputMode::Files.mode_byte()]);
443    for (path, content) in entries {
444        hasher.update((path.len() as u64).to_le_bytes());
445        hasher.update(path.as_bytes());
446        hasher.update((content.len() as u64).to_le_bytes());
447        hasher.update(content.as_bytes());
448    }
449    hex::encode(hasher.finalize())
450}
451
452/// Maximum number of distinct keys accepted in a v0.2
453/// `components.instructions` object (`"default"` plus per-provider
454/// variants) — ABF v0.2 §2.2. A real bundle needs at most one entry per
455/// known harness (nine, as of `providers.rs`) plus `"default"`; generous
456/// headroom for future providers without leaving this unbounded, mirroring
457/// this module's other component-count caps (e.g.
458/// [`MAX_ACCOUNT_REQUIREMENTS`]).
459const MAX_INSTRUCTION_PROVIDER_VARIANTS: usize = 32;
460
461/// The exact warning `parse_bundle_import_with_budget` pushes when
462/// `components.memory` is present — ABF v0.2 §2.3. A shared constant
463/// (rather than a literal duplicated at both the push site and
464/// `bundle_import_for_agent_impl`'s filter site, reagent P1 PR #2527)
465/// so the two can never drift apart: if this message ever changes, the
466/// filter in `bundle.rs` keeps working because it references the same
467/// constant, not a copy of the string.
468/// Warning for an ABF carrying `components.projectInstructions`.
469///
470/// Phase 3 of `SPEC_INSTRUCTION_AND_MEMORY_PORTABILITY_2026_09_09.md`. These
471/// entries record what the *source* agent was reading. They are surfaced and
472/// never installed — the files they describe belong to whatever repository the
473/// bundle is being imported into, and an import that wrote them would be doing
474/// what `SPEC_CLAUDE_MD_OWNERSHIP_PROTECTION_2026_08_22.md` exists to prevent.
475///
476/// Unlike [`MEMORY_COMPONENT_IGNORED_WARNING`] this has no filter site: memory
477/// warns only on the agent-less path because `bundle.import_for_agent` really
478/// does install it, whereas no import path installs these. The warning is
479/// therefore unconditional and permanent, not a "use the other RPC" pointer.
480pub(crate) const PROJECT_INSTRUCTIONS_NOT_APPLIED_WARNING: &str =
481    "components.projectInstructions: recorded for reference, never installed — these files belong to the importing repository, not to the bundle. Inspect them with agent.project_instructions and apply anything you want deliberately";
482
483/// Cap on how many `components.projectInstructions` entries are read back.
484///
485/// Matches `project_instructions::MAX_SCANNED_INSTRUCTION_FILES`, which is the
486/// most AgentMux's own exporter can ever produce — so this can only ever bite
487/// a hand-written or hostile bundle, where refusing to allocate unboundedly is
488/// the point. Overflow warns rather than truncating silently, same rule the
489/// rest of this parser follows.
490pub const MAX_IMPORTED_PROJECT_INSTRUCTIONS: usize = 200;
491
492pub const MEMORY_COMPONENT_IGNORED_WARNING: &str =
493    "components.memory: present but ignored — memory requires an agent-scoped import (bundle.import_for_agent), not bundle.import";
494
495/// Parse one `components.instructions`-shaped path array (either the v0.1
496/// flat array itself, or one key's array within the v0.2 keyed-object
497/// shape) into its joined instructions text. `label` is used only for
498/// warning messages (`"instructions"` for the flat/default case,
499/// `"instructions.<provider>"` for a variant) so every caller's warnings
500/// are traceable to the specific key that produced them.
501///
502/// `seen_instruction_paths`/`duplicate_instruction_refs` are threaded
503/// through BY THE CALLER, shared across every invocation within one
504/// `parse_bundle_import_with_budget` call (default + every provider
505/// variant) — reagent P1, PR #2523: a HashSet local to each call only
506/// dedupes within its own component array, so the same `instructions/
507/// context/*` path referenced from both `"default"` and a provider
508/// variant was pushed into `context_files` twice, and — worse — a
509/// manifest repeating one path across many provider keys (up to
510/// [`MAX_INSTRUCTION_PROVIDER_VARIANTS`]) re-opened the exact
511/// content-cloning amplification vector the original per-array dedup
512/// existed to close (Codex P1, PR #2379 round 2, cited below).
513///
514/// `divert_context_files`: only `true` for the flat/default case.
515/// reagent P1, PR #2523: diverting any `instructions/context/*`-prefixed
516/// path applies to the raw file path unconditionally, regardless of which
517/// provider variant is being parsed — so a provider literally named
518/// `"context"` (whose exported path is `instructions/context/AGENTS.md`,
519/// per `bundle_export.rs`'s `instructions/<provider>/AGENTS.md`
520/// convention) would have its own content silently misrouted into
521/// `context_files` on a later re-import, a reserved-word collision with
522/// no validation guarding it. Context files are shared, not
523/// provider-scoped, by design (§1's naming note) — a provider variant's
524/// array never needs the diversion at all, so disabling it there closes
525/// the collision structurally rather than special-casing the "context"
526/// name.
527fn parse_instruction_component_paths(
528    arr: &[Value],
529    label: &str,
530    by_path: &HashMap<String, &str>,
531    context_files: &mut Vec<ImportedContextFile>,
532    divert_context_files: bool,
533    seen_instruction_paths: &mut HashSet<String>,
534    duplicate_instruction_refs: &mut u32,
535    warnings: &mut WarningSink,
536) -> String {
537    let mut instructions_parts: Vec<String> = Vec::new();
538    let paths = capped_component_array(Some(arr), label, warnings);
539    for path_val in paths {
540        let Some(raw_path) = path_val.as_str() else {
541            warnings.push(format!("components.{label}: non-string entry skipped"));
542            continue;
543        };
544        // codex P2, PR #2379 round 4: normalize the manifest's OWN
545        // reference the same way `by_path`'s keys are normalized
546        // (round 4's earlier fix) — otherwise a valid non-canonical
547        // spelling here (e.g. `./instructions/AGENTS.md`) no longer
548        // matches the now-canonicalized lookup key and the component
549        // is reported missing even though the file is genuinely
550        // present under an equivalent spelling.
551        let Some(path) = sanitize_context_relative_path(raw_path) else {
552            warnings.push(format!("components.{label}: \"{raw_path}\" is not a safe path; skipped"));
553            continue;
554        };
555        // codex P1, PR #2379 round 6: accounts/requirements.json is
556        // intentionally IN by_path (the dedicated parser below needs
557        // to read it), but it must never be reachable through this
558        // generic lookup — a manifest listing it here would otherwise
559        // copy its raw JSON straight into `instructions`, later
560        // written unredacted to `instructions/AGENTS.md` on export.
561        if is_requirements_json(&path) {
562            warnings.push(format!(
563                "components.{label}: \"{path}\" is the accounts/ requirements file; not readable as instructions"
564            ));
565            continue;
566        }
567        // codex P1, PR #2379 round 6: dedup already avoids cloning
568        // CONTENT per duplicate reference (round 3), but pushing one
569        // warning STRING per duplicate is itself an amplification
570        // vector — a permitted 10 MB manifest repeating one short
571        // path hundreds of thousands of times could allocate hundreds
572        // of megabytes of warning text serialized into the RPC
573        // response. Count instead; a single summary warning is pushed
574        // after the whole components.instructions parse completes
575        // (ABF v0.2, §2.2: now after every array, not just this one —
576        // see this function's own doc comment).
577        if !seen_instruction_paths.insert(path.clone()) {
578            *duplicate_instruction_refs += 1;
579            continue;
580        }
581        let Some(content) = by_path.get(path.as_str()) else {
582            warnings.push(format!("components.{label}: \"{path}\" not found among the bundle's files; skipped"));
583            continue;
584        };
585        let diverted = divert_context_files && path.strip_prefix("instructions/context/").is_some();
586        if diverted {
587            let rel = path.strip_prefix("instructions/context/").unwrap();
588            match sanitize_context_relative_path(rel) {
589                Some(safe_rel) => context_files.push(ImportedContextFile {
590                    id: context_files.len(),
591                    path: safe_rel,
592                    content: content.to_string(),
593                }),
594                None => warnings.push(format!(
595                    "{path}: not a safe relative path under instructions/context/; skipped"
596                )),
597            }
598        } else {
599            instructions_parts.push(content.to_string());
600        }
601    }
602    instructions_parts.join("\n\n---\n\n")
603}
604
605/// The manifest an ABF v0.3 archive carries
606/// (SPEC_AGENT_BUNDLE_FORMAT_V0_3_2026_10_05.md §2.1).
607pub const MANIFEST_FILE: &str = "bundle.json";
608/// The manifest of ABF v0.1 and v0.2 archives, read forever.
609pub const LEGACY_MANIFEST_FILE: &str = "armory.json";
610/// The `$schema` an ABF v0.3 manifest names.
611pub const SCHEMA_V0_3: &str = "https://docs.agentmux.ai/schemas/agent-bundle/v0.3/bundle.schema.json";
612
613/// Which manifest file a set of archive paths carries: `bundle.json`, else
614/// the legacy `armory.json`.
615pub fn manifest_file_name(mut has: impl FnMut(&str) -> bool) -> Option<&'static str> {
616    [MANIFEST_FILE, LEGACY_MANIFEST_FILE].into_iter().find(|name| has(name))
617}
618
619/// The ABF format version a manifest declares through its `$schema`, or
620/// `None` when it names none this importer knows. The `version` field is
621/// the bundle's own content version, not this.
622pub fn abf_format_version(manifest: &Value) -> Option<&'static str> {
623    let schema = manifest.get("$schema")?.as_str()?;
624    if schema.contains("/agent-bundle/v0.3/") {
625        Some("0.3")
626    } else if schema.contains("/armory-bundle/v0.2/") {
627        Some("0.2")
628    } else if schema.contains("/armory-bundle/v0.1/") {
629        Some("0.1")
630    } else {
631        None
632    }
633}
634
635/// The `(major, minor)` of an ABF `$schema` URL of the form `.../<family>/v<major>[.<minor>[...]]...`, read leniently: a
636/// missing minor is 0, a patch part or a suffix (`-beta`) is ignored, and the segment may end the URL. `None` when no digits
637/// follow the `v`.
638fn schema_version_of(schema: &str, family: &str) -> Option<(u32, u32)> {
639    let segment = schema.split(&format!("/{family}/v")).nth(1)?.split('/').next()?;
640    let digits = |s: &str| -> (Option<u32>, usize) {
641        let n = s.chars().take_while(|c| c.is_ascii_digit()).count();
642        (s[..n].parse().ok(), n)
643    };
644    let (major, used) = digits(segment);
645    let minor = match segment[used..].strip_prefix('.') {
646        Some(rest) => digits(rest).0.unwrap_or(0),
647        None => 0,
648    };
649    Some((major?, minor))
650}
651
652/// The plain refusal for a manifest whose `$schema` names an ABF format newer than this build reads, or `None` (a format this
653/// build reads, or a `$schema` that names no ABF format: those keep the warning in `warn_on_schema`). It names the format
654/// and, when the bundle says which AgentMux made it (`metadata.agentmuxVersion`), that version. Such a bundle is refused whole:
655/// a newer format can change what a component means, and importing what this build can make sense of would be a half import.
656pub fn newer_abf_refusal(manifest: &Value, manifest_name: &str) -> Option<String> {
657    let schema = manifest.get("$schema")?.as_str()?;
658    let known = (0, 3);
659    let (major, minor) = schema_version_of(schema, "agent-bundle").or_else(|| schema_version_of(schema, "armory-bundle"))?;
660    if (major, minor) <= known {
661        return None;
662    }
663    // The version text is shown to the person: only what a version looks like.
664    let made_by = manifest
665        .pointer("/metadata/agentmuxVersion")
666        .and_then(|v| v.as_str())
667        .filter(|v| !v.is_empty() && v.len() <= 32 && v.chars().all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '-' || c == '+'));
668    Some(match made_by {
669        Some(v) => format!("{manifest_name}: this bundle is Agent Bundle Format v{major}.{minor}, made by AgentMux {v}, and this AgentMux reads up to v0.3: update to AgentMux {v} or newer to import it. Nothing was imported."),
670        None => format!("{manifest_name}: this bundle is Agent Bundle Format v{major}.{minor}, made by a newer AgentMux, and this AgentMux reads up to v0.3: update AgentMux to import it. Nothing was imported."),
671    })
672}
673
674/// Parse and validate a bundle's files into [`ParsedBundleImport`].
675/// Structural failures (no manifest, `bundle.json` or `armory.json`, or a
676/// malformed one) reject the whole import — `Err` — since there is nothing safe to partially write.
677/// Per-entry problems (a missing referenced file, an unsafe path, a
678/// malformed SKILL.md) degrade to a warning and that entry is skipped —
679/// matches `bundle_export.rs`'s own philosophy, and lets a lossy import
680/// still produce a usable bundle rather than an all-or-nothing failure on
681/// e.g. one corrupt skill among five good ones.
682pub fn parse_bundle_import(files: &[BundleImportFile]) -> Result<ParsedBundleImport, String> {
683    parse_bundle_import_with_budget(files, WarningBudget::unbounded())
684}
685
686/// Same as [`parse_bundle_import`], but with an explicit [`WarningBudget`]
687/// enforced at every warning-push site inside this function (Phase 3 spec
688/// §3.1, round 11) — the new `bundle.import.preview`/`.commit` RPC handlers
689/// call this directly with a tight budget; [`parse_bundle_import`] passes
690/// [`WarningBudget::unbounded`], preserving today's existing `bundle.import`
691/// route's behavior exactly.
692pub fn parse_bundle_import_with_budget(
693    files: &[BundleImportFile],
694    budget: WarningBudget,
695) -> Result<ParsedBundleImport, String> {
696    let mut warnings = WarningSink::new(budget);
697
698    // Path normalization, first-wins dedup, and accounts/ allowlist
699    // enforcement (§4.3.5) all live in dedup_files_by_path — see its own
700    // doc comment.
701    let by_path = dedup_files_by_path(files, &mut warnings);
702
703    let (manifest_name, manifest) = parse_steps::read_manifest(&by_path, &mut warnings)?;
704    if let Some(refusal) = newer_abf_refusal(&manifest, manifest_name) {
705        return Err(refusal);
706    }
707    let header = parse_steps::manifest_header(&manifest, manifest_name, &mut warnings);
708    parse_steps::warn_on_schema(&manifest, manifest_name, &mut warnings);
709    let components = manifest.get("components").and_then(|v| v.as_object());
710
711    let instructions = parse_steps::parse_instructions(components, &by_path, &mut warnings);
712    let (skills, skipped_skills) = parse_steps::parse_skills(components, &by_path, &mut warnings);
713    let mcp_servers = parse_steps::parse_mcp_servers(components, &by_path, &mut warnings);
714    let requirements = parse_steps::parse_account_requirements(components, &by_path, &mut warnings);
715    parse_steps::warn_memory_component(components, &mut warnings);
716    let project_instructions = parse_steps::parse_project_instructions(components, &by_path, &mut warnings);
717
718    Ok(ParsedBundleImport {
719        name: header.name,
720        description: header.description,
721        provider: header.provider,
722        model: header.model,
723        instructions: instructions.instructions,
724        instructions_by_provider: instructions.instructions_by_provider,
725        context_files: instructions.context_files,
726        mcp_servers,
727        skills,
728        skipped_skills,
729        requirements,
730        project_instructions,
731        warnings: warnings.into_vec(),
732    })
733}
734
735/// True if `path` is (case-insensitively) the one file ever read out of
736/// the accounts/ directory. Used both to allow it into `by_path` (the
737/// accounts/ allowlist above) and to REJECT it from every OTHER
738/// component category's generic lookup (codex P1, PR #2379 round 6) —
739/// components.instructions/mcpServers must never be able to pull its raw
740/// JSON content in as if it were ordinary bundle content (e.g. straight
741/// into `instructions/AGENTS.md` on a later export, or into an mcpServer
742/// entry), since only the dedicated accounts/requirements.json parser is
743/// meant to ever read it.
744fn is_requirements_json(path: &str) -> bool {
745    path.eq_ignore_ascii_case("accounts/requirements.json")
746}
747
748/// Bounds a manifest component array's length BEFORE any per-entry
749/// processing happens, so one cap protects every warning a per-entry
750/// loop can produce (non-string entries, unsafe paths, not-found
751/// lookups, malformed content, ...) at once — codex P1, PR #2379 round
752/// 7: round 6 bounded only the "duplicate reference" warning
753/// specifically; a manifest filled with non-string junk values hit an
754/// entirely different (still unbounded) warning path for the same
755/// amplification effect. Reuses [`MAX_ENTRY_COUNT`] — a manifest
756/// component array legitimately needs the same "more entries than any
757/// real bundle uses" ceiling a zip archive's entry count does.
758fn capped_component_array<'a>(arr: Option<&'a [Value]>, key: &str, warnings: &mut WarningSink) -> &'a [Value] {
759    let Some(arr) = arr else { return &[] };
760    let len = arr.len();
761    if len > MAX_ENTRY_COUNT {
762        warnings.push(format!(
763            "components.{key}: {len} entries exceeds the limit ({MAX_ENTRY_COUNT}); only the first {MAX_ENTRY_COUNT} are used"
764        ));
765        &arr[..MAX_ENTRY_COUNT]
766    } else {
767        arr
768    }
769}
770
771/// Parse a SKILL.md file (`render_skill_md`'s exact output shape, and
772/// nothing more general — this is not a YAML parser, just the inverse of
773/// the one writer this codebase has). Expects exactly:
774/// `---\nname: "<json-string>"\ndescription: "<json-string>"\n---\n\n<body>`.
775/// Returns `None` for anything that doesn't match — a real Agent Skills
776/// SKILL.md from elsewhere in the ecosystem may use additional frontmatter
777/// fields or different quoting; broadening this to a general YAML parser
778/// is a follow-up, not silently guessed at here (mirrors the exporter's
779/// own documented `server.json` scope decision).
780fn parse_skill_md(content: &str) -> Option<ParsedSkill> {
781    let rest = content.strip_prefix("---\n")?;
782    let (frontmatter, body) = rest.split_once("\n---\n\n")?;
783    let mut name: Option<String> = None;
784    let mut description: Option<String> = None;
785    for line in frontmatter.lines() {
786        let (key, value) = line.split_once(": ")?;
787        let parsed: String = serde_json::from_str(value).ok()?;
788        match key {
789            "name" => name = Some(parsed),
790            "description" => description = Some(parsed),
791            _ => {} // unrecognized frontmatter field — ignore, don't fail
792        }
793    }
794    Some(ParsedSkill {
795        // Filled in by the caller (parse_bundle_import), which alone knows
796        // the components.skills directory reference that led here.
797        source_dir: String::new(),
798        slug: name?,
799        // Required, not defaulted: render_skill_md always writes both
800        // fields (falling back to a placeholder string, never omitting
801        // the key), so a real exported SKILL.md always has both present.
802        // A file missing `description` entirely isn't this writer's
803        // output shape — reject it the same as a missing `name`.
804        description: description?,
805        content: body.to_string(),
806    })
807}
808
809/// Per-entry decompressed-size cap. Generous for any legitimate
810/// instructions/SKILL.md/context file (10 MB of text is already an
811/// absurdly large single bundle component) while bounding how much a
812/// single malicious entry can force into memory.
813const MAX_ENTRY_UNCOMPRESSED_BYTES: u64 = 10 * 1024 * 1024;
814
815/// Aggregate decompressed-size cap across the whole archive. Bounds a
816/// zip-bomb-style archive built from many medium-sized entries that would
817/// each individually pass [`MAX_ENTRY_UNCOMPRESSED_BYTES`].
818const MAX_TOTAL_UNCOMPRESSED_BYTES: u64 = 50 * 1024 * 1024;
819
820/// Maximum number of entries an archive may contain. A legitimate bundle
821/// (instructions, a handful of context files, a handful of skills, one
822/// manifest) never comes close to this; it exists purely to bound the CPU
823/// cost of iterating + path-sanitizing + hashing every entry, independent
824/// of the per-entry/aggregate byte caps below (reagent P2, PR #2379 round
825/// 2) — a crafted archive of many tiny/valid entries passes both size caps
826/// while still forcing unbounded iteration work.
827const MAX_ENTRY_COUNT: usize = 10_000;
828
829/// Maximum number of account requirements accepted from a single bundle's
830/// `accounts/requirements.json`. A real bundle needs a handful at most —
831/// one per distinct external account it depends on. Exists to bound the
832/// RPC handler's downstream per-provider account-lookup work (codex P1,
833/// PR #2379 round 4): an untrusted archive's requirements array,
834/// unbounded, could otherwise drive a synchronous store query for every
835/// row.
836const MAX_ACCOUNT_REQUIREMENTS: usize = 1_000;
837
838/// Maximum number of skills actually imported (i.e. written to the Store)
839/// from a single bundle. `MAX_ENTRY_COUNT`/`capped_component_array` bound
840/// how many `components.skills` entries are even LOOKED AT, but codex P1
841/// (PR #2379 round 7) points out that's still far too high a ceiling for
842/// the RPC handler's write side: importing up to that many skills means
843/// up to that many separate synchronous Store transactions, each creating
844/// a permanent, globally-visible skill row — a compact malicious archive
845/// (well within the size caps) could otherwise monopolize the handler and
846/// pollute the installation's skill catalog. A real bundle needs a
847/// handful to a few dozen skills at most.
848///
849/// Also reused by `bundle.import.commit` (Phase 3 spec §3.2, round 3) as
850/// the cap on `include_skills`'s length — the same reasoning applies to a
851/// client-supplied selection array as to the parser's own component list:
852/// neither may drive more Store-write attempts than a real bundle could
853/// ever need.
854pub const MAX_IMPORTED_SKILLS: usize = 200;
855
856/// Maximum character length of a bundle's manifest `name`, enforced at
857/// parse time (Phase 3 spec §3.1, round 13) rather than at a later display
858/// boundary — `name` is re-submitted verbatim as `bundle_name` when a user
859/// doesn't edit the preview's suggested value, so the canonical, bounded
860/// value must be what both `preview` and `commit` converge on from the
861/// moment parsing completes. Generous for any real bundle name.
862pub const MAX_BUNDLE_NAME_CHARS: usize = 200;
863
864/// Bounds a bundle name to [`MAX_BUNDLE_NAME_CHARS`] via plain truncation
865/// (no ellipsis, unlike [`truncate_display`]) — this IS the canonical
866/// value, not a display abbreviation of a longer "real" one. Shared by
867/// `parse_bundle_import`'s own manifest-`name` bounding and the Phase 3
868/// `bundle.import.commit` RPC's `bundle_name` override (round 3), so a
869/// client-supplied override can't bypass the same bound `parsed.name` is
870/// already held to.
871pub fn bound_bundle_name(name: &str) -> String {
872    if name.chars().count() > MAX_BUNDLE_NAME_CHARS {
873        name.chars().take(MAX_BUNDLE_NAME_CHARS).collect()
874    } else {
875        name.to_string()
876    }
877}
878
879/// Single choke point for the per-entry/aggregate size caps, shared by
880/// BOTH intake paths (zip decompression and the raw `files` RPC list) —
881/// reagent P1 / codex P1, PR #2379 round 5. Two separate gaps motivated
882/// unifying this instead of patching each path independently:
883///
884/// - The raw `files` RPC branch previously ran NO size/count checks at
885///   all, even though the spec explicitly treats it as an equally
886///   untrusted alternate ingestion path to the zip — a hostile bundle
887///   submitted via `files` instead of `zip_base64` bypassed every
888///   zip-bomb/DoS defense this module had built for the zip path alone.
889/// - Even on the zip path, an entry whose ACTUAL decompressed size
890///   exceeds the per-entry cap was decompressed (paying the full
891///   MAX_ENTRY_UNCOMPRESSED_BYTES-sized read) and then skipped WITHOUT
892///   ever counting that work toward the aggregate budget — so up to
893///   MAX_ENTRY_COUNT such entries could force on the order of
894///   MAX_ENTRY_COUNT * MAX_ENTRY_UNCOMPRESSED_BYTES of real decompression
895///   work while the aggregate cap never tripped.
896///
897/// This function accounts `content_len` toward the aggregate budget
898/// UNCONDITIONALLY, before deciding whether the entry itself is kept —
899/// closing the second gap structurally (every byte actually processed
900/// counts, whether or not the entry is ultimately retained) — and both
901/// intake paths call it, closing the first gap by construction (there is
902/// no path left that skips this check).
903///
904/// Returns `Ok(true)` to keep the entry, `Ok(false)` to skip it (a
905/// warning has already been pushed), or `Err` to reject the whole import
906/// (aggregate cap exceeded).
907fn check_entry_size(
908    safe_name: &str,
909    content_len: u64,
910    total_uncompressed: &mut u64,
911    warnings: &mut WarningSink,
912) -> Result<bool, String> {
913    *total_uncompressed = total_uncompressed.saturating_add(content_len);
914    if *total_uncompressed > MAX_TOTAL_UNCOMPRESSED_BYTES {
915        return Err(format!(
916            "aggregate decompressed size exceeds the total limit ({MAX_TOTAL_UNCOMPRESSED_BYTES} bytes) — rejecting the whole import"
917        ));
918    }
919    if content_len > MAX_ENTRY_UNCOMPRESSED_BYTES {
920        warnings.push(format!(
921            "{safe_name}: size ({content_len} bytes) exceeds the per-entry limit ({MAX_ENTRY_UNCOMPRESSED_BYTES} bytes); skipped"
922        ));
923        return Ok(false);
924    }
925    Ok(true)
926}
927
928/// Applies [`check_entry_size`]'s caps (plus [`MAX_ENTRY_COUNT`]) to the
929/// raw `files` RPC intake path — the path `unzip_bundle_import` never
930/// covers (reagent P1, PR #2379 round 5). Unlike zip entries, a raw
931/// file's `content` is already fully materialized (no separate
932/// declared-vs-actual distinction, no incremental decompression to
933/// bound), so this only needs the shared per-entry/aggregate check, no
934/// backstop-read machinery.
935pub fn enforce_raw_files_caps(files: Vec<BundleImportFile>) -> Result<(Vec<BundleImportFile>, Vec<String>), String> {
936    enforce_raw_files_caps_with_budget(files, WarningBudget::unbounded())
937}
938
939/// Same as [`enforce_raw_files_caps`], but with an explicit [`WarningBudget`]
940/// (Phase 3 spec §3.1, round 11).
941pub fn enforce_raw_files_caps_with_budget(
942    files: Vec<BundleImportFile>,
943    budget: WarningBudget,
944) -> Result<(Vec<BundleImportFile>, Vec<String>), String> {
945    if files.len() > MAX_ENTRY_COUNT {
946        return Err(format!(
947            "{} entries exceeds the limit ({MAX_ENTRY_COUNT}) — rejecting the whole import",
948            files.len()
949        ));
950    }
951    let mut warnings = WarningSink::new(budget);
952    let mut total_uncompressed: u64 = 0;
953    let mut out = Vec::new();
954    for f in files {
955        let keep = check_entry_size(&f.path, f.content.len() as u64, &mut total_uncompressed, &mut warnings)?;
956        if keep {
957            out.push(f);
958        }
959    }
960    Ok((out, warnings.into_vec()))
961}
962
963/// Unpack a `.abf` zip archive into a flat file list, applying the same
964/// path-safety check as everywhere else in this module to every entry
965/// name before it's trusted (§4.3.4) — a zip's own internal paths are
966/// exactly as untrusted as any other part of the archive's content.
967/// Directory entries are skipped; anything that fails the safety check is
968/// dropped with a warning rather than surfaced as a file.
969///
970/// Codex P1, PR #2379: also bounds decompressed size, per-entry and in
971/// aggregate — `read_to_string` alone has no size limit, so an untrusted
972/// `.abf` containing a highly compressed entry (a classic zip-bomb shape;
973/// DEFLATE alone permits ratios past 1000:1) could otherwise exhaust the
974/// server process's memory through `bundle.import`. An oversized single
975/// entry is skipped with a warning (matches this function's existing
976/// per-entry-problem philosophy); an oversized AGGREGATE fails the whole
977/// import — letting a partially-capped import through silently would be
978/// more confusing than useful, and hitting the aggregate cap at all
979/// already indicates a genuinely abusive archive rather than one bad file.
980pub fn unzip_bundle_import(zip_bytes: &[u8]) -> Result<(Vec<BundleImportFile>, Vec<String>), String> {
981    unzip_bundle_import_with_budget(zip_bytes, WarningBudget::unbounded())
982}
983
984/// Same as [`unzip_bundle_import`], but with an explicit [`WarningBudget`]
985/// (Phase 3 spec §3.1, round 11).
986pub fn unzip_bundle_import_with_budget(
987    zip_bytes: &[u8],
988    budget: WarningBudget,
989) -> Result<(Vec<BundleImportFile>, Vec<String>), String> {
990    use std::io::Read;
991    let cursor = std::io::Cursor::new(zip_bytes);
992    let mut archive = zip::ZipArchive::new(cursor).map_err(|e| format!("not a valid zip archive: {e}"))?;
993
994    if archive.len() > MAX_ENTRY_COUNT {
995        return Err(format!(
996            "zip archive: {} entries exceeds the limit ({MAX_ENTRY_COUNT}) — rejecting the whole import",
997            archive.len()
998        ));
999    }
1000
1001    // First pass: collect every non-dir entry's raw name, to DETECT
1002    // whether every single one shares one common wrapping directory
1003    // (`zip_bundle_export`'s convention: `<root_slug>/bundle.json`, etc.)
1004    // before deciding whether to strip a path component at all.
1005    //
1006    // reagent P1, PR #2379: the previous version unconditionally stripped
1007    // the first `/`-delimited segment of EVERY entry, on the assumption a
1008    // wrapper always exists. A hand-built `.abf` zipped directly from the
1009    // loose directory tree (no wrapping folder) — which this spec's §2
1010    // explicitly requires an importer to also accept — has entries like
1011    // `instructions/AGENTS.md` already bundle-relative; blindly stripping
1012    // silently mangled it to `AGENTS.md`, breaking every `components.*`
1013    // path lookup in `parse_bundle_import` (each degrades to a "not
1014    // found" warning and the content is dropped) with no error surfaced.
1015    // Only strip when EVERY entry agrees on the same first segment.
1016    let mut raw_names: Vec<String> = Vec::new();
1017    for i in 0..archive.len() {
1018        let entry = archive
1019            .by_index(i)
1020            .map_err(|e| format!("zip archive: failed to read entry {i}: {e}"))?;
1021        if entry.is_dir() {
1022            continue;
1023        }
1024        raw_names.push(entry.name().to_string());
1025    }
1026    let common_wrapper: Option<&str> = raw_names.first().and_then(|first| {
1027        let candidate = first.split_once('/').map(|(root, _)| root)?;
1028        raw_names
1029            .iter()
1030            .all(|n| n.split_once('/').map(|(root, _)| root == candidate).unwrap_or(false))
1031            .then_some(candidate)
1032    });
1033    let strip_len: Option<usize> = common_wrapper.map(|w| w.len() + 1); // +1 for the '/'
1034
1035    let mut out = Vec::new();
1036    let mut warnings = WarningSink::new(budget);
1037    let mut seen: HashSet<String> = HashSet::new();
1038    let mut total_uncompressed: u64 = 0;
1039    for i in 0..archive.len() {
1040        let mut entry = archive
1041            .by_index(i)
1042            .map_err(|e| format!("zip archive: failed to read entry {i}: {e}"))?;
1043        if entry.is_dir() {
1044            continue;
1045        }
1046        let raw_name = entry.name().to_string();
1047        let relative_name = match strip_len {
1048            Some(n) if raw_name.len() > n => &raw_name[n..],
1049            _ => raw_name.as_str(),
1050        };
1051        let Some(safe_name) = sanitize_context_relative_path(relative_name) else {
1052            warnings.push(format!("{raw_name}: not a safe path; skipped"));
1053            continue;
1054        };
1055        if !seen.insert(safe_name.clone()) {
1056            warnings.push(format!("{safe_name}: duplicate entry in archive; first occurrence kept"));
1057            continue;
1058        }
1059
1060        // Declared size check first — cheap, and avoids decompressing at
1061        // all for an entry that's already too large per its own metadata.
1062        // This is ONLY a cheap pre-filter: `entry.size()` is the zip's own
1063        // declared/attacker-controlled uncompressed size, not a verified
1064        // value, so it must never feed the aggregate cap below (reagent
1065        // P1, PR #2379 round 2) — an archive of many entries that each lie
1066        // with a tiny declared size while actually containing content up
1067        // to the per-entry cap would otherwise pass both checks here while
1068        // still exhausting memory once decompressed.
1069        let declared_size = entry.size();
1070        if declared_size > MAX_ENTRY_UNCOMPRESSED_BYTES {
1071            warnings.push(format!(
1072                "{safe_name}: declared uncompressed size ({declared_size} bytes) exceeds the per-entry limit ({MAX_ENTRY_UNCOMPRESSED_BYTES} bytes); skipped"
1073            ));
1074            continue;
1075        }
1076
1077        // Hard backstop on the actual read, independent of the declared
1078        // size above: read at most one byte past the cap so an entry
1079        // whose real decompressed content exceeds what it declared is
1080        // still caught (rather than trusting zip metadata alone).
1081        let mut limited = entry.by_ref().take(MAX_ENTRY_UNCOMPRESSED_BYTES + 1);
1082        let mut buf: Vec<u8> = Vec::new();
1083        let read_err = limited.read_to_end(&mut buf).err();
1084
1085        // codex P1, PR #2379 round 5/6: `check_entry_size` accounts these
1086        // bytes toward the aggregate budget BEFORE deciding whether to
1087        // keep the entry, unlike the previous inline checks here (which
1088        // accumulated only KEPT entries' bytes). Called unconditionally
1089        // here — even when the read above ultimately errored (e.g. a
1090        // forged CRC on an otherwise-decompressing entry) — since
1091        // `read_to_end` can leave real decompressed bytes in `buf` before
1092        // returning an error; that decompression work already happened
1093        // regardless of the read's outcome, and up to MAX_ENTRY_COUNT
1094        // such corrupt entries must not be able to force ~10MB of work
1095        // each while the aggregate counter stays untouched.
1096        let keep = check_entry_size(&safe_name, buf.len() as u64, &mut total_uncompressed, &mut warnings)?;
1097
1098        if let Some(e) = read_err {
1099            warnings.push(format!("{safe_name}: failed to read entry: {e}"));
1100            continue;
1101        }
1102        if !keep {
1103            continue;
1104        }
1105        let content = match String::from_utf8(buf) {
1106            Ok(s) => s,
1107            Err(e) => {
1108                warnings.push(format!("{safe_name}: not valid UTF-8 text; skipped ({e})"));
1109                continue;
1110            }
1111        };
1112        out.push(BundleImportFile { path: safe_name, content });
1113    }
1114    Ok((out, warnings.into_vec()))
1115}
1116
1117#[path = "bundle_import_parse.rs"]
1118pub mod parse_steps;
1119
1120#[cfg(test)]
1121#[path = "bundle_import_tests.rs"]
1122mod tests;