Class: PromptObjects::Env::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/prompt_objects/environment/importer.rb

Overview

Imports an environment from a git bundle (.dump file — Declarative Unified Memory Package; legacy .poenv bundles work too). Handles security concerns around custom primitives.

Defined Under Namespace

Classes: InspectResult

Instance Method Summary collapse

Constructor Details

#initialize(bundle_path) ⇒ Importer

Returns a new instance of Importer.

Parameters:

  • bundle_path (String)

    Path to the .dump (or legacy .poenv) bundle file



25
26
27
# File 'lib/prompt_objects/environment/importer.rb', line 25

def initialize(bundle_path)
  @bundle_path = File.expand_path(bundle_path)
end

Instance Method Details

#has_primitives?Boolean

Check if a bundle contains custom primitives.

Returns:

  • (Boolean)


99
100
101
102
# File 'lib/prompt_objects/environment/importer.rb', line 99

def has_primitives?
  result = inspect_bundle
  result.valid && result.primitives.any?
end

#import(manager:, name:, trust_primitives: false) ⇒ Hash

Import the bundle as a new environment.

Parameters:

  • manager (Manager)

    Environment manager

  • name (String)

    Name for the new environment

  • trust_primitives (Boolean) (defaults to: false)

    Trust custom primitives (skip sandboxing)

Returns:

  • (Hash)

    Import result with :success, :path, :warnings



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/prompt_objects/environment/importer.rb', line 56

def import(manager:, name:, trust_primitives: false)
  inspect_result = inspect_bundle
  unless inspect_result.valid
    return { success: false, error: inspect_result.error }
  end

  # Check if environment already exists
  if manager.environment_exists?(name)
    return { success: false, error: "Environment '#{name}' already exists" }
  end

  # Clone bundle to environment location
  env_path = manager.environment_path(name)
  unless Git.clone_bundle(@bundle_path, env_path)
    return { success: false, error: "Failed to clone bundle" }
  end

  # Update manifest with new name if different
  update_manifest(env_path, name)

  # Build result with warnings about primitives
  warnings = []
  if inspect_result.primitives.any? && !trust_primitives
    warnings << "This environment contains #{inspect_result.primitives.count} custom primitive(s):"
    inspect_result.primitives.each do |prim|
      warnings << "  - #{prim}"
    end
    warnings << "Custom primitives will be sandboxed by default."
    warnings << "Review the code before trusting: #{File.join(env_path, 'primitives')}"
  end

  {
    success: true,
    path: env_path,
    name: name,
    objects: inspect_result.objects,
    primitives: inspect_result.primitives,
    warnings: warnings
  }
end

#inspect_bundleInspectResult

Inspect the bundle without importing it. Use this to show the user what's in the bundle before importing.

Returns:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/prompt_objects/environment/importer.rb', line 32

def inspect_bundle
  unless File.exist?(@bundle_path)
    return InspectResult.new(valid: false, error: "Bundle file not found: #{@bundle_path}")
  end

  unless valid_bundle?
    return InspectResult.new(valid: false, error: "Invalid git bundle file")
  end

  # Clone to temp dir to inspect contents
  Dir.mktmpdir("dump_inspect_") do |temp_dir|
    unless Git.clone_bundle(@bundle_path, temp_dir)
      return InspectResult.new(valid: false, error: "Failed to extract bundle")
    end

    gather_inspect_result(temp_dir)
  end
end