lex-cognitive-debt 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3393bad153f282e51a0542ee0272f7ccfd57699f0407003ad3b0fe94c57bbc04
4
+ data.tar.gz: f1c1c120c4a8ae30651b098a11954f5761975c9c5a9c55226f5c63699233a1eb
5
+ SHA512:
6
+ metadata.gz: edaa0467353d508aa9d0a5f265978684a9d033d44f401f1958ba83dc135ac17279b90a202524e32033f7647cd22bebfa3929cbff747008f2fbd61f53318dd792
7
+ data.tar.gz: 82c7d74195f7560227ca4409c814c9ddbd8702c7772eb1f2cf9db5a2685e350cb454318534acc3240e7378065cd7f04b6c41d1f2c325dc277ba6aa4988c68a8c
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rake'
8
+ gem 'rspec', '~> 3.13'
9
+ gem 'rspec_junit_formatter'
10
+ gem 'rubocop', '~> 1.75', require: false
11
+ gem 'rubocop-rspec', require: false
12
+ gem 'simplecov'
13
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Matthew Iverson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # lex-cognitive-debt
2
+
3
+ Cognitive debt modeling for LegionIO. Deferred processing that accrues interest over time.
4
+
5
+ ## What It Does
6
+
7
+ When the agent defers decisions, leaves inputs unprocessed, abandons partial analysis, or fails to integrate new information, cognitive debt accumulates. Like financial debt, it compounds with interest — the longer you defer, the more costly resolution becomes. This extension tracks that debt, applies interest each cycle, and supports structured repayment.
8
+
9
+ Five debt types are modeled: deferred decision, unprocessed input, incomplete analysis, pending integration, and unresolved conflict.
10
+
11
+ ## Usage
12
+
13
+ ```ruby
14
+ client = Legion::Extensions::CognitiveDebt::Client.new
15
+
16
+ debt = client.create_debt(
17
+ label: 'deferred analysis of conflicting trust signals',
18
+ debt_type: :incomplete_analysis,
19
+ principal: 0.5,
20
+ domain: 'trust'
21
+ )
22
+
23
+ client.accrue_interest
24
+ client.total_debt
25
+ # => { total_debt: 0.525 } # 5% interest applied
26
+
27
+ client.repay_debt(debt_id: debt[:id], amount: 0.2)
28
+ client.debt_report
29
+ client.prune_repaid
30
+ ```
31
+
32
+ ## Development
33
+
34
+ ```bash
35
+ bundle install
36
+ bundle exec rspec
37
+ bundle exec rubocop
38
+ ```
39
+
40
+ ## License
41
+
42
+ MIT
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/legion/extensions/cognitive_debt/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'lex-cognitive-debt'
7
+ spec.version = Legion::Extensions::CognitiveDebt::VERSION
8
+ spec.authors = ['Esity']
9
+ spec.email = ['matthewdiverson@gmail.com']
10
+
11
+ spec.summary = 'LEX Cognitive Debt'
12
+ spec.description = 'Cognitive debt modeling for brain-modeled agentic AI — deferred processing that accrues interest over time'
13
+ spec.homepage = 'https://github.com/LegionIO/lex-cognitive-debt'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 3.4'
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://github.com/LegionIO/lex-cognitive-debt'
19
+ spec.metadata['documentation_uri'] = 'https://github.com/LegionIO/lex-cognitive-debt'
20
+ spec.metadata['changelog_uri'] = 'https://github.com/LegionIO/lex-cognitive-debt'
21
+ spec.metadata['bug_tracker_uri'] = 'https://github.com/LegionIO/lex-cognitive-debt/issues'
22
+ spec.metadata['rubygems_mfa_required'] = 'true'
23
+
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ Dir.glob('{lib,spec}/**/*') + %w[lex-cognitive-debt.gemspec Gemfile LICENSE README.md]
26
+ end
27
+ spec.require_paths = ['lib']
28
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'legion/extensions/cognitive_debt/helpers/constants'
4
+ require 'legion/extensions/cognitive_debt/helpers/debt_item'
5
+ require 'legion/extensions/cognitive_debt/helpers/debt_engine'
6
+ require 'legion/extensions/cognitive_debt/runners/cognitive_debt'
7
+
8
+ module Legion
9
+ module Extensions
10
+ module CognitiveDebt
11
+ class Client
12
+ include Runners::CognitiveDebt
13
+
14
+ def initialize(engine: nil, **)
15
+ @default_engine = engine || Helpers::DebtEngine.new
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :default_engine
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Extensions
5
+ module CognitiveDebt
6
+ module Helpers
7
+ module Constants
8
+ MAX_DEBTS = 300
9
+ INTEREST_RATE = 0.05
10
+ DEFAULT_PRINCIPAL = 0.3
11
+ REPAYMENT_RATE = 0.1
12
+
13
+ SEVERITY_LABELS = {
14
+ (0.0...0.2) => :negligible,
15
+ (0.2...0.4) => :minor,
16
+ (0.4...0.6) => :moderate,
17
+ (0.6...0.8) => :severe,
18
+ (0.8..Float::INFINITY) => :critical
19
+ }.freeze
20
+
21
+ DEBT_TYPES = %i[
22
+ deferred_decision
23
+ unprocessed_input
24
+ incomplete_analysis
25
+ pending_integration
26
+ unresolved_conflict
27
+ ].freeze
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Extensions
5
+ module CognitiveDebt
6
+ module Helpers
7
+ class DebtEngine
8
+ def initialize
9
+ @debts = {}
10
+ end
11
+
12
+ def create_debt(label:, debt_type:, principal: Constants::DEFAULT_PRINCIPAL, domain: 'general')
13
+ return { created: false, reason: :debt_type_invalid } unless Constants::DEBT_TYPES.include?(debt_type.to_sym)
14
+ return { created: false, reason: :limit_reached } if @debts.size >= Constants::MAX_DEBTS
15
+
16
+ item = DebtItem.new(
17
+ label: label,
18
+ debt_type: debt_type,
19
+ principal: principal,
20
+ domain: domain
21
+ )
22
+ @debts[item.id] = item
23
+ { created: true, debt_id: item.id, debt: item.to_h }
24
+ end
25
+
26
+ def repay_debt(debt_id:, amount: Constants::REPAYMENT_RATE)
27
+ item = @debts[debt_id]
28
+ return { found: false } unless item
29
+ return { found: true, repaid: false, reason: :already_repaid } if item.repaid?
30
+
31
+ item.repay!(amount: amount)
32
+ { found: true, repaid: true, debt_id: debt_id, debt: item.to_h }
33
+ end
34
+
35
+ def accrue_all_interest
36
+ count = 0
37
+ @debts.each_value do |item|
38
+ next if item.repaid?
39
+
40
+ item.accrue!
41
+ count += 1
42
+ end
43
+ { accrued: count, total_debt: total_debt }
44
+ end
45
+
46
+ def total_debt
47
+ @debts.values.reject(&:repaid?).sum(&:total_cost).round(10)
48
+ end
49
+
50
+ def debt_by_type(debt_type:)
51
+ type = debt_type.to_sym
52
+ items = @debts.values.select { |d| d.debt_type == type && !d.repaid? }
53
+ { debt_type: type, count: items.size, items: items.map(&:to_h) }
54
+ end
55
+
56
+ def debt_by_domain(domain:)
57
+ items = @debts.values.select { |d| d.domain == domain && !d.repaid? }
58
+ { domain: domain, count: items.size, items: items.map(&:to_h) }
59
+ end
60
+
61
+ def most_costly(limit: 10)
62
+ items = @debts.values
63
+ .reject(&:repaid?)
64
+ .sort_by { |d| -d.total_cost }
65
+ .first(limit)
66
+ { count: items.size, items: items.map(&:to_h) }
67
+ end
68
+
69
+ def oldest_debts(limit: 10)
70
+ items = @debts.values
71
+ .reject(&:repaid?)
72
+ .sort_by(&:created_at)
73
+ .first(limit)
74
+ { count: items.size, items: items.map(&:to_h) }
75
+ end
76
+
77
+ def debt_report
78
+ active = @debts.values.reject(&:repaid?)
79
+ by_type = Constants::DEBT_TYPES.to_h do |type|
80
+ typed = active.select { |d| d.debt_type == type }
81
+ [type, { count: typed.size, total_cost: typed.sum(&:total_cost).round(10) }]
82
+ end
83
+
84
+ priority = active.sort_by { |d| -d.total_cost }.first(5)
85
+
86
+ {
87
+ total_debt: total_debt,
88
+ active_count: active.size,
89
+ repaid_count: @debts.values.count(&:repaid?),
90
+ by_type: by_type,
91
+ recommended_priority: priority.map(&:to_h)
92
+ }
93
+ end
94
+
95
+ def prune_repaid
96
+ before = @debts.size
97
+ @debts.delete_if { |_id, item| item.repaid? }
98
+ pruned = before - @debts.size
99
+ { pruned: pruned, remaining: @debts.size }
100
+ end
101
+
102
+ def to_h
103
+ {
104
+ total_debt: total_debt,
105
+ active_count: @debts.values.reject(&:repaid?).size,
106
+ total_count: @debts.size,
107
+ debts: @debts.values.map(&:to_h)
108
+ }
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+
5
+ module Legion
6
+ module Extensions
7
+ module CognitiveDebt
8
+ module Helpers
9
+ class DebtItem
10
+ attr_reader :id, :label, :debt_type, :principal, :accrued_interest,
11
+ :domain, :created_at, :repaid_at
12
+
13
+ def initialize(label:, debt_type:, principal:, domain:)
14
+ @id = SecureRandom.uuid
15
+ @label = label
16
+ @debt_type = debt_type.to_sym
17
+ @principal = principal.clamp(0.0, Float::INFINITY)
18
+ @accrued_interest = 0.0
19
+ @domain = domain
20
+ @created_at = Time.now.utc
21
+ @repaid_at = nil
22
+ end
23
+
24
+ def total_cost
25
+ (@principal + @accrued_interest).round(10)
26
+ end
27
+
28
+ def accrue!
29
+ return if repaid?
30
+
31
+ @accrued_interest = (@accrued_interest + (Constants::INTEREST_RATE * @principal)).round(10)
32
+ end
33
+
34
+ def repay!(amount: Constants::REPAYMENT_RATE)
35
+ return if repaid?
36
+
37
+ @accrued_interest = [0.0, @accrued_interest - amount].max.round(10)
38
+ @principal = [0.0, @principal - amount].max.round(10)
39
+ @repaid_at = Time.now.utc if @principal.zero? && @accrued_interest.zero?
40
+ end
41
+
42
+ def repaid?
43
+ !@repaid_at.nil?
44
+ end
45
+
46
+ def severity_label
47
+ cost = total_cost
48
+ Constants::SEVERITY_LABELS.each do |range, label|
49
+ return label if range.cover?(cost)
50
+ end
51
+ :critical
52
+ end
53
+
54
+ def age_seconds
55
+ reference = @repaid_at || Time.now.utc
56
+ (reference - @created_at).to_f
57
+ end
58
+
59
+ def to_h
60
+ {
61
+ id: @id,
62
+ label: @label,
63
+ debt_type: @debt_type,
64
+ principal: @principal,
65
+ accrued_interest: @accrued_interest,
66
+ total_cost: total_cost,
67
+ domain: @domain,
68
+ severity: severity_label,
69
+ age_seconds: age_seconds.round(2),
70
+ created_at: @created_at.iso8601,
71
+ repaid_at: @repaid_at&.iso8601,
72
+ repaid: repaid?
73
+ }
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Extensions
5
+ module CognitiveDebt
6
+ module Runners
7
+ module CognitiveDebt
8
+ def create_debt(label:, debt_type:, principal: Helpers::Constants::DEFAULT_PRINCIPAL,
9
+ domain: 'general', engine: nil, **)
10
+ eng = engine || default_engine
11
+ Legion::Logging.debug "[cognitive_debt] create debt label=#{label} type=#{debt_type} principal=#{principal}"
12
+ eng.create_debt(label: label, debt_type: debt_type, principal: principal, domain: domain)
13
+ end
14
+
15
+ def repay_debt(debt_id:, amount: Helpers::Constants::REPAYMENT_RATE, engine: nil, **)
16
+ eng = engine || default_engine
17
+ Legion::Logging.debug "[cognitive_debt] repay debt_id=#{debt_id[0..7]} amount=#{amount}"
18
+ eng.repay_debt(debt_id: debt_id, amount: amount)
19
+ end
20
+
21
+ def accrue_interest(engine: nil, **)
22
+ eng = engine || default_engine
23
+ result = eng.accrue_all_interest
24
+ Legion::Logging.debug "[cognitive_debt] accrued interest on #{result[:accrued]} debts total_debt=#{result[:total_debt]}"
25
+ result
26
+ end
27
+
28
+ def total_debt(engine: nil, **)
29
+ eng = engine || default_engine
30
+ cost = eng.total_debt
31
+ Legion::Logging.debug "[cognitive_debt] total_debt=#{cost}"
32
+ { total_debt: cost }
33
+ end
34
+
35
+ def debt_by_type(debt_type:, engine: nil, **)
36
+ eng = engine || default_engine
37
+ eng.debt_by_type(debt_type: debt_type)
38
+ end
39
+
40
+ def debt_by_domain(domain:, engine: nil, **)
41
+ eng = engine || default_engine
42
+ eng.debt_by_domain(domain: domain)
43
+ end
44
+
45
+ def most_costly(limit: 10, engine: nil, **)
46
+ eng = engine || default_engine
47
+ eng.most_costly(limit: limit)
48
+ end
49
+
50
+ def oldest_debts(limit: 10, engine: nil, **)
51
+ eng = engine || default_engine
52
+ eng.oldest_debts(limit: limit)
53
+ end
54
+
55
+ def debt_report(engine: nil, **)
56
+ eng = engine || default_engine
57
+ report = eng.debt_report
58
+ Legion::Logging.debug "[cognitive_debt] report total_debt=#{report[:total_debt]} active=#{report[:active_count]}"
59
+ report
60
+ end
61
+
62
+ def prune_repaid(engine: nil, **)
63
+ eng = engine || default_engine
64
+ result = eng.prune_repaid
65
+ Legion::Logging.debug "[cognitive_debt] pruned #{result[:pruned]} repaid debts remaining=#{result[:remaining]}"
66
+ result
67
+ end
68
+
69
+ include Legion::Extensions::Helpers::Lex if defined?(Legion::Extensions::Helpers::Lex)
70
+
71
+ private
72
+
73
+ def default_engine
74
+ @default_engine ||= Helpers::DebtEngine.new
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Extensions
5
+ module CognitiveDebt
6
+ VERSION = '0.1.0'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'legion/extensions/cognitive_debt/version'
4
+ require 'legion/extensions/cognitive_debt/helpers/constants'
5
+ require 'legion/extensions/cognitive_debt/helpers/debt_item'
6
+ require 'legion/extensions/cognitive_debt/helpers/debt_engine'
7
+ require 'legion/extensions/cognitive_debt/runners/cognitive_debt'
8
+
9
+ module Legion
10
+ module Extensions
11
+ module CognitiveDebt
12
+ extend Legion::Extensions::Core if Legion::Extensions.const_defined? :Core
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'legion/extensions/cognitive_debt/client'
4
+
5
+ RSpec.describe Legion::Extensions::CognitiveDebt::Client do
6
+ let(:client) { described_class.new }
7
+
8
+ it 'responds to create_debt' do
9
+ expect(client).to respond_to(:create_debt)
10
+ end
11
+
12
+ it 'responds to repay_debt' do
13
+ expect(client).to respond_to(:repay_debt)
14
+ end
15
+
16
+ it 'responds to accrue_interest' do
17
+ expect(client).to respond_to(:accrue_interest)
18
+ end
19
+
20
+ it 'responds to total_debt' do
21
+ expect(client).to respond_to(:total_debt)
22
+ end
23
+
24
+ it 'responds to debt_by_type' do
25
+ expect(client).to respond_to(:debt_by_type)
26
+ end
27
+
28
+ it 'responds to debt_by_domain' do
29
+ expect(client).to respond_to(:debt_by_domain)
30
+ end
31
+
32
+ it 'responds to most_costly' do
33
+ expect(client).to respond_to(:most_costly)
34
+ end
35
+
36
+ it 'responds to oldest_debts' do
37
+ expect(client).to respond_to(:oldest_debts)
38
+ end
39
+
40
+ it 'responds to debt_report' do
41
+ expect(client).to respond_to(:debt_report)
42
+ end
43
+
44
+ it 'responds to prune_repaid' do
45
+ expect(client).to respond_to(:prune_repaid)
46
+ end
47
+
48
+ it 'accepts an injected engine' do
49
+ engine = Legion::Extensions::CognitiveDebt::Helpers::DebtEngine.new
50
+ c = described_class.new(engine: engine)
51
+ c.create_debt(label: 'test', debt_type: :deferred_decision, principal: 0.3)
52
+ expect(engine.total_debt).to be_within(1e-10).of(0.3)
53
+ end
54
+
55
+ it 'round-trips a full lifecycle' do
56
+ created = client.create_debt(label: 'pick caching layer', debt_type: :deferred_decision, principal: 0.4)
57
+ expect(created[:created]).to be true
58
+ debt_id = created[:debt_id]
59
+
60
+ client.accrue_interest
61
+ report = client.debt_report
62
+ expect(report[:total_debt]).to be > 0.4
63
+
64
+ repaid = client.repay_debt(debt_id: debt_id, amount: 99.0)
65
+ expect(repaid[:repaid]).to be true
66
+
67
+ pruned = client.prune_repaid
68
+ expect(pruned[:pruned]).to eq(1)
69
+
70
+ final = client.total_debt
71
+ expect(final[:total_debt]).to eq(0.0)
72
+ end
73
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Legion::Extensions::CognitiveDebt::Helpers::Constants do
4
+ describe 'MAX_DEBTS' do
5
+ it 'is 300' do
6
+ expect(described_class::MAX_DEBTS).to eq(300)
7
+ end
8
+ end
9
+
10
+ describe 'INTEREST_RATE' do
11
+ it 'is 0.05' do
12
+ expect(described_class::INTEREST_RATE).to eq(0.05)
13
+ end
14
+ end
15
+
16
+ describe 'DEFAULT_PRINCIPAL' do
17
+ it 'is 0.3' do
18
+ expect(described_class::DEFAULT_PRINCIPAL).to eq(0.3)
19
+ end
20
+ end
21
+
22
+ describe 'REPAYMENT_RATE' do
23
+ it 'is 0.1' do
24
+ expect(described_class::REPAYMENT_RATE).to eq(0.1)
25
+ end
26
+ end
27
+
28
+ describe 'SEVERITY_LABELS' do
29
+ it 'is a frozen hash' do
30
+ expect(described_class::SEVERITY_LABELS).to be_frozen
31
+ end
32
+
33
+ it 'maps low cost to :negligible' do
34
+ label = described_class::SEVERITY_LABELS.find { |range, _| range.cover?(0.1) }&.last
35
+ expect(label).to eq(:negligible)
36
+ end
37
+
38
+ it 'maps 0.3 to :minor' do
39
+ label = described_class::SEVERITY_LABELS.find { |range, _| range.cover?(0.3) }&.last
40
+ expect(label).to eq(:minor)
41
+ end
42
+
43
+ it 'maps 0.5 to :moderate' do
44
+ label = described_class::SEVERITY_LABELS.find { |range, _| range.cover?(0.5) }&.last
45
+ expect(label).to eq(:moderate)
46
+ end
47
+
48
+ it 'maps 0.7 to :severe' do
49
+ label = described_class::SEVERITY_LABELS.find { |range, _| range.cover?(0.7) }&.last
50
+ expect(label).to eq(:severe)
51
+ end
52
+
53
+ it 'maps 0.9 to :critical' do
54
+ label = described_class::SEVERITY_LABELS.find { |range, _| range.cover?(0.9) }&.last
55
+ expect(label).to eq(:critical)
56
+ end
57
+ end
58
+
59
+ describe 'DEBT_TYPES' do
60
+ it 'includes all five expected types' do
61
+ expect(described_class::DEBT_TYPES).to include(
62
+ :deferred_decision,
63
+ :unprocessed_input,
64
+ :incomplete_analysis,
65
+ :pending_integration,
66
+ :unresolved_conflict
67
+ )
68
+ end
69
+
70
+ it 'has exactly five types' do
71
+ expect(described_class::DEBT_TYPES.size).to eq(5)
72
+ end
73
+
74
+ it 'contains only symbols' do
75
+ expect(described_class::DEBT_TYPES).to all(be_a(Symbol))
76
+ end
77
+ end
78
+ end