lex-synapse 0.4.33 → 0.4.40
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 +4 -4
- data/CHANGELOG.md +28 -0
- data/README.md +13 -5
- data/lib/legion/extensions/synapse/client.rb +10 -1
- data/lib/legion/extensions/synapse/helpers/knowledge.rb +44 -0
- data/lib/legion/extensions/synapse/helpers/target_schema.rb +44 -0
- data/lib/legion/extensions/synapse/integration.rb +11 -3
- data/lib/legion/extensions/synapse/runners/pain.rb +41 -18
- data/lib/legion/extensions/synapse/runners/promote.rb +45 -29
- data/lib/legion/extensions/synapse/runners/propose.rb +3 -7
- data/lib/legion/extensions/synapse/runners/retrieve.rb +30 -6
- data/lib/legion/extensions/synapse/runners/revert.rb +12 -10
- data/lib/legion/extensions/synapse/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0220a328e7f9c7ecf46cc08da91ddb0774a977508499917712b619d16e39700a
|
|
4
|
+
data.tar.gz: 5b561c8dc728de8a91a95042cc84366872b377e8ebe9e4a6b4aea461fd451af3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 70cd3d5c795d80aad0363ce96475f314c02136a244a8b0317be8b45002f81b9322f49def917b51a8184d2b06a9ccf1e9969224dacc38909d1546863944b11927
|
|
7
|
+
data.tar.gz: 3c852f16dac687544b1730831d8e5ad61b204bd7fef95df4e7e1d315bc518015057477375616380de40c858e344b45e323c12f8d3606c65e8c49ecf0b2dbf74e
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.4.40] - 2026-11-22
|
|
4
|
+
### Fixed
|
|
5
|
+
- Fingerprint shared pattern bodies so Apollo content normalization cannot collapse case-sensitive template changes into an older pattern (fixes #30).
|
|
6
|
+
|
|
7
|
+
## [0.4.39] - 2026-11-22
|
|
8
|
+
### Fixed
|
|
9
|
+
- Validate task/signal attachment before dispatch, reject missing or reassigned correlations, and preserve idempotent retries (references #18; tasker dispatcher integration remains required).
|
|
10
|
+
|
|
11
|
+
## [0.4.38] - 2026-11-22
|
|
12
|
+
### Fixed
|
|
13
|
+
- Resolve attached task identity for signal-only pain notifications and distinguish unknown or mismatched correlations from duplicate outcomes (fixes #21).
|
|
14
|
+
|
|
15
|
+
## [0.4.37] - 2026-11-22
|
|
16
|
+
### Fixed
|
|
17
|
+
- Publish stable synapse patterns through Apollo, distinguish candidates and asynchronous submissions from durable results, and retrieve versioned raw patterns before deduplicated seeding (fixes #30).
|
|
18
|
+
|
|
19
|
+
## [0.4.36] - 2026-11-22
|
|
20
|
+
### Fixed
|
|
21
|
+
- Resolve target proposal schemas from persisted function identity and the runtime tool registry, falling back to recorded keyword signatures instead of the nonexistent Discovery API (fixes #19).
|
|
22
|
+
|
|
23
|
+
## [0.4.35] - 2026-11-22
|
|
24
|
+
### Fixed
|
|
25
|
+
- Reject ambiguous legacy mutation versions and allow exact rollback by immutable mutation ID; verify monotonic history through repeated rollback branches (fixes #22).
|
|
26
|
+
|
|
27
|
+
## [0.4.34] - 2026-11-22
|
|
28
|
+
### Fixed
|
|
29
|
+
- Correlate task-only pain with its evaluation, prevent duplicate failure credit, and atomically persist outcome penalties; verify real interleaved evaluation/failure rollback (fixes #21).
|
|
30
|
+
|
|
3
31
|
## [0.4.33] - 2026-11-19
|
|
4
32
|
### Fixed
|
|
5
33
|
- Feed periodic crystallization from the latest 1000 completed parent-child task edges, retaining threshold and duplicate checks (fixes #29).
|
data/README.md
CHANGED
|
@@ -105,12 +105,12 @@ an AMQP producer publishing `synapse.evaluate` or `task.failed`.
|
|
|
105
105
|
### Pain
|
|
106
106
|
`handle_pain(synapse_id:, task_id: nil, signal_id: nil, **)`
|
|
107
107
|
|
|
108
|
-
Downstream failure handler.
|
|
108
|
+
Downstream failure handler. Task notifications must identify a correlated evaluation through `signal_id` or a unique `task_id`. Redelivery is neutral; uncorrelated task notifications return an error. Calls without either ID record a manual failure. Pending evaluations do not interrupt the completed-outcome failure streak. The outcome and confidence penalty commit together; three completed failures trigger reversion.
|
|
109
109
|
|
|
110
110
|
### Revert
|
|
111
|
-
`revert(synapse_id:, to_version: nil, trigger: 'pain')`
|
|
111
|
+
`revert(synapse_id:, to_version: nil, mutation_id: nil, trigger: 'pain', **)`
|
|
112
112
|
|
|
113
|
-
|
|
113
|
+
Restores a mutation’s `before_state`. `to_version` identifies the mutation being undone, rather than a target live-state version; omit it to undo the current live version. New history sequence numbers always exceed every prior history version, even after rollback lowers the live version. Legacy duplicate versions return an ambiguity error without changing state; use the immutable `mutation_id` to select exactly one historical row. Specify only one selector. Reversion appends a history entry and preserves existing identifiers.
|
|
114
114
|
|
|
115
115
|
### Crystallize
|
|
116
116
|
`crystallize(signal_pairs: [], threshold: 20)`
|
|
@@ -137,7 +137,13 @@ Aggregates stats: confidence, status, 24h signal count, success rate, last mutat
|
|
|
137
137
|
|
|
138
138
|
### GAIA, knowledge entries, and blast radius
|
|
139
139
|
|
|
140
|
-
`gaia_summary(**)` returns aggregate health; `gaia_reflection(**)` adds recent mutation information.
|
|
140
|
+
`gaia_summary(**)` returns aggregate health; `gaia_reflection(**)` adds recent mutation information.
|
|
141
|
+
|
|
142
|
+
`promote(synapse_id: nil, apollo_client: nil, publish: true, **)` publishes through the optional loaded `Legion::Apollo` client or an injected client implementing its `ingest`/`retrieve` API. Eligible synapses are active, have confidence at least 0.9, are at least 24 hours old, and have no mutations in the last 24 hours. `publish: false` previews `candidates`; candidates never count as published. `promoted` and `count` require a persisted entry ID or Apollo's persisted-duplicate result. Async responses appear only in `submitted`; they do not establish durable storage. Failures appear in `errors`, and unavailable Apollo returns an explicit error. Repeated identical content uses Apollo's content-hash deduplication; a changed snapshot is a new pattern.
|
|
143
|
+
|
|
144
|
+
`retrieve_and_seed(knowledge_entries: nil, apollo_client: nil, query: 'synapse routing patterns', limit: 100, **)` retrieves through Apollo before seeding. Supplying `knowledge_entries:` retains offline import support. Pending/failed queries create no synapses. Candidates require Apollo confidence of at least 0.7. New patterns carry the versioned `legion.synapse_pattern` marker inside preserved raw content, so Apollo's generic procedure/fact classification and summaries do not discard pattern identity. Unknown versions and invalid endpoint IDs are rejected; existing source/target pairs are skipped. Shared patterns require the same shared function catalog.
|
|
145
|
+
|
|
146
|
+
Standalone clients can receive `apollo_client:` at construction. Synapse always requests global scope; Apollo installation, startup, authorization, and durable backend availability remain operator responsibilities.
|
|
141
147
|
|
|
142
148
|
`compute(**)` recomputes blast-radius tiers from the active routing graph. `blast_tier(synapse_id:)` returns a stored tier. The private `analyze_routing` proposal stub is not a blast-radius API.
|
|
143
149
|
|
|
@@ -173,7 +179,7 @@ Five tables: `synapses` (core routing + confidence + status + blast_radius), `sy
|
|
|
173
179
|
|
|
174
180
|
`report(synapse_id:)` measures the last 24 hours by signal creation time. `success_rate` is successful downstream outcomes divided by completed outcomes (`success` plus `failed`), and is `nil` when none are complete. `completed_outcomes_24h`, `successful_outcomes_24h`, and `failed_outcomes_24h` expose that denominator. `unresolved_signals_24h` includes pending tasks and evaluations that never dispatch; these do not count as completed failures. `evaluation_successes_24h` counts successful evaluation/pass-through records and is not proof of downstream completion or transformation execution.
|
|
175
181
|
|
|
176
|
-
`Integration.attach_task` associates an evaluation signal with its downstream task and target runner/function. `Integration.record_outcome` and the installed host task event listeners finalize that same signal once; duplicate or conflicting completion notifications do not create another attempt. Live task-chain attachment
|
|
182
|
+
`Integration.attach_task` associates an evaluation signal with its downstream task and target runner/function. `Integration.record_outcome` and the installed host task event listeners finalize that same signal once; duplicate or conflicting completion notifications do not create another attempt. Live task-chain attachment requires the dispatcher caller in [lex-tasker PR #10](https://github.com/LegionIO/lex-tasker/pull/10), tracked in [#18](https://github.com/LegionIO/lex-synapse/issues/18). Attachment is idempotent for the same task and target; missing signals and attempts to reassign a signal raise before publication so the dispatcher can retry safely. Synapse alone cannot intercept tasker publication.
|
|
177
183
|
|
|
178
184
|
### Required proposal review
|
|
179
185
|
|
|
@@ -192,3 +198,5 @@ MIT
|
|
|
192
198
|
Configure `extensions.synapse.proposals` and `extensions.synapse.challenge` using symbol keys through `Legion::Settings` (JSON configuration keys are normalized by the loader). Defaults are exposed by `Legion::Extensions::Synapse.default_settings`. Standalone clients use the same settings and defaults. Migrate the former top-level `lex-synapse` section to `extensions.synapse`; that legacy path is no longer read.
|
|
193
199
|
|
|
194
200
|
Scheduled crystallization samples the latest 1000 completed child tasks from the shared `tasks` table and groups their parent/child function IDs. At least 20 sampled edges are required for an observing emergent synapse. Failed or pending children do not count. Existing function pairs are skipped on later cycles. Standalone callers may still supply `signal_pairs:` explicitly; an empty array disables collection for that call.
|
|
201
|
+
|
|
202
|
+
Transform proposals resolve numeric target function IDs through the shared function/runner/extension catalog to registered tool input schemas. If no rich schema is registered, recorded keyword signatures supply property names and required arguments without inventing types. Standalone databases without the host catalog use an empty target schema.
|
|
@@ -36,11 +36,20 @@ module Legion
|
|
|
36
36
|
|
|
37
37
|
attr_reader :conditioner_client, :transformer_client
|
|
38
38
|
|
|
39
|
-
def initialize(conditioner_client: nil, transformer_client: nil)
|
|
39
|
+
def initialize(conditioner_client: nil, transformer_client: nil, apollo_client: nil, **)
|
|
40
|
+
@apollo_client = apollo_client
|
|
40
41
|
@conditioner_client = conditioner_client
|
|
41
42
|
@transformer_client = transformer_client
|
|
42
43
|
end
|
|
43
44
|
|
|
45
|
+
def promote(apollo_client: @apollo_client, **)
|
|
46
|
+
super
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def retrieve_and_seed(apollo_client: @apollo_client, **)
|
|
50
|
+
super
|
|
51
|
+
end
|
|
52
|
+
|
|
44
53
|
def evaluate(synapse_id:, payload: {})
|
|
45
54
|
super(
|
|
46
55
|
synapse_id: synapse_id,
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/logging/helper'
|
|
4
|
+
|
|
5
|
+
module Legion
|
|
6
|
+
module Extensions
|
|
7
|
+
module Synapse
|
|
8
|
+
module Helpers
|
|
9
|
+
module Knowledge
|
|
10
|
+
include Legion::Logging::Helper
|
|
11
|
+
|
|
12
|
+
PATTERN_FORMAT = 'legion.synapse_pattern'
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
def knowledge_provider(client:)
|
|
17
|
+
# Apollo is an optional host plugin. Standalone callers can inject its public client API.
|
|
18
|
+
client || (Legion::Apollo if defined?(Legion::Apollo))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def publish_pattern(provider:, entry:)
|
|
22
|
+
provider.ingest(content: entry.fetch(:content), raw_content: entry.fetch(:content),
|
|
23
|
+
content_type: :procedure, tags: entry.fetch(:tags), source_agent: 'lex-synapse', scope: :global)
|
|
24
|
+
rescue StandardError => e
|
|
25
|
+
raise if e.is_a?(NoMethodError) || e.is_a?(ArgumentError) || e.is_a?(TypeError)
|
|
26
|
+
|
|
27
|
+
handle_exception(e, level: :warn, operation: :publish_synapse_pattern, synapse_id: entry[:synapse_id])
|
|
28
|
+
{ success: false, error: e.message }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def fetch_patterns(provider:, query:, limit:)
|
|
32
|
+
provider.retrieve(text: query, limit: limit, scope: :global, tags: ['synapse'],
|
|
33
|
+
min_confidence: 0.7, status: %i[confirmed candidate])
|
|
34
|
+
rescue StandardError => e
|
|
35
|
+
raise if e.is_a?(NoMethodError) || e.is_a?(ArgumentError) || e.is_a?(TypeError)
|
|
36
|
+
|
|
37
|
+
handle_exception(e, level: :warn, operation: :retrieve_synapse_patterns)
|
|
38
|
+
{ success: false, error: e.message }
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/settings/extensions'
|
|
4
|
+
|
|
5
|
+
module Legion
|
|
6
|
+
module Extensions
|
|
7
|
+
module Synapse
|
|
8
|
+
module Helpers
|
|
9
|
+
module TargetSchema
|
|
10
|
+
def self.resolve(function_id:, db:, **)
|
|
11
|
+
return {} unless %i[functions runners extensions].all? { |table| db.table_exists?(table) }
|
|
12
|
+
|
|
13
|
+
target = db[:functions].join(:runners, id: :runner_id)
|
|
14
|
+
.join(:extensions, id: Sequel[:runners][:extension_id])
|
|
15
|
+
.where(Sequel[:functions][:id] => function_id)
|
|
16
|
+
.select(Sequel[:functions][:name].as(:function), Sequel[:functions][:args],
|
|
17
|
+
Sequel[:runners][:name].as(:runner), Sequel[:extensions][:name].as(:extension)).first
|
|
18
|
+
return {} unless target
|
|
19
|
+
|
|
20
|
+
extension = target[:extension].delete_prefix('lex-')
|
|
21
|
+
tools = Legion::Settings::Extensions.tools.select do |tool|
|
|
22
|
+
tool[:extension].to_s.delete_prefix('lex-') == extension && tool[:runner] == target[:runner] &&
|
|
23
|
+
(tool[:function] == target[:function] || tool[:name] == "legion-#{extension}-#{target[:runner]}-#{target[:function]}")
|
|
24
|
+
end
|
|
25
|
+
schemas = tools.map { |tool| tool[:input_schema] }.reject(&:empty?).uniq
|
|
26
|
+
return schemas.first if schemas.one?
|
|
27
|
+
return {} if schemas.size > 1
|
|
28
|
+
|
|
29
|
+
signature_schema(args: target[:args])
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.signature_schema(args:, **)
|
|
33
|
+
parameters = args.is_a?(String) ? Legion::JSON.load(args) : args
|
|
34
|
+
return {} unless parameters.is_a?(Array) && parameters.all?(Array)
|
|
35
|
+
|
|
36
|
+
keywords = parameters.select { |kind, name| %w[key keyreq].include?(kind.to_s) && name }
|
|
37
|
+
{ type: 'object', properties: keywords.to_h { |_, name| [name.to_sym, {}] },
|
|
38
|
+
required: keywords.filter_map { |kind, name| name.to_s if kind.to_s == 'keyreq' } }
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -31,9 +31,17 @@ module Legion
|
|
|
31
31
|
def attach_task(signal_id:, task_id:, runner_class:, function:, **)
|
|
32
32
|
return unless signal_id
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
raise ArgumentError, 'task_id is required' if task_id.nil?
|
|
35
|
+
raise ArgumentError, 'target runner and function are required' if runner_class.to_s.empty? || function.to_s.empty?
|
|
36
|
+
|
|
37
|
+
Legion::Extensions::Synapse::Data::Model.define_synapse_signal_model
|
|
38
|
+
identity = { task_id: task_id, target_runner_class: runner_class.to_s, target_function: function.to_s }
|
|
39
|
+
updated = Legion::Extensions::Synapse::Data::Model::SynapseSignal.where(id: signal_id)
|
|
40
|
+
.where(Sequel.|({ task_id: nil, downstream_outcome: nil },
|
|
41
|
+
identity)).update(identity)
|
|
42
|
+
raise Sequel::Error, 'evaluation signal is missing or already correlated to another task' if updated.zero?
|
|
43
|
+
|
|
44
|
+
{ success: true, signal_id: signal_id, task_id: task_id }
|
|
37
45
|
end
|
|
38
46
|
|
|
39
47
|
def record_outcome(task_id:, outcome:, runner_class:, function:, **)
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
require 'legion/extensions/synapse/helpers/confidence'
|
|
4
|
+
require 'legion/extensions/synapse/data/models/synapse'
|
|
5
|
+
require 'legion/extensions/synapse/data/models/synapse_signal'
|
|
6
|
+
require 'legion/extensions/synapse/runners/revert'
|
|
7
7
|
|
|
8
8
|
module Legion
|
|
9
9
|
module Extensions
|
|
@@ -20,21 +20,15 @@ module Legion
|
|
|
20
20
|
synapse = Data::Model::Synapse[synapse_id]
|
|
21
21
|
return { success: false, error: 'synapse not found' } unless synapse
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
if
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return { success: true, duplicate: true } if changed.zero?
|
|
29
|
-
else
|
|
30
|
-
Legion::Extensions::Synapse::Data::Model::SynapseSignal.create(
|
|
31
|
-
synapse_id: synapse.id, task_id: task_id, passed_attention: true,
|
|
32
|
-
transform_success: true, downstream_outcome: 'failed'
|
|
33
|
-
)
|
|
34
|
-
end
|
|
23
|
+
correlation = resolve_failure_signal(synapse: synapse, task_id: task_id, signal_id: signal_id)
|
|
24
|
+
return correlation if correlation[:error]
|
|
25
|
+
|
|
26
|
+
task_id = correlation[:task_id]
|
|
27
|
+
signal_id = correlation[:signal_id]
|
|
35
28
|
|
|
36
|
-
#
|
|
37
|
-
new_confidence =
|
|
29
|
+
# Claim the outcome and its penalty together; revert runs after this transaction.
|
|
30
|
+
new_confidence = record_failure(synapse: synapse, task_id: task_id, signal_id: signal_id)
|
|
31
|
+
return { success: true, duplicate: true } unless new_confidence
|
|
38
32
|
|
|
39
33
|
# Check consecutive failures
|
|
40
34
|
consecutive = count_consecutive_failures(synapse)
|
|
@@ -68,6 +62,35 @@ module Legion
|
|
|
68
62
|
|
|
69
63
|
private
|
|
70
64
|
|
|
65
|
+
def resolve_failure_signal(synapse:, task_id:, signal_id:)
|
|
66
|
+
return {} unless task_id || signal_id
|
|
67
|
+
|
|
68
|
+
dataset = synapse.signals_dataset
|
|
69
|
+
signals = (signal_id ? dataset.where(id: signal_id) : dataset.where(task_id: task_id)).limit(2).all
|
|
70
|
+
return { success: false, error: 'task requires one correlated evaluation signal' } unless signals.one?
|
|
71
|
+
|
|
72
|
+
signal = signals.first
|
|
73
|
+
return { success: false, error: 'signal belongs to a different task' } if task_id && signal.task_id != task_id
|
|
74
|
+
|
|
75
|
+
{ task_id: signal.task_id, signal_id: signal.id }
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def record_failure(synapse:, task_id:, signal_id:)
|
|
79
|
+
synapse.db.transaction do
|
|
80
|
+
if signal_id
|
|
81
|
+
changed = Legion::Extensions::Synapse::Data::Model::SynapseSignal
|
|
82
|
+
.where(id: signal_id, synapse_id: synapse.id, task_id: task_id, downstream_outcome: nil)
|
|
83
|
+
.update(downstream_outcome: 'failed', success_credit_pending: false)
|
|
84
|
+
next if changed.zero?
|
|
85
|
+
else
|
|
86
|
+
Legion::Extensions::Synapse::Data::Model::SynapseSignal.create(
|
|
87
|
+
synapse_id: synapse.id, passed_attention: true, transform_success: true, downstream_outcome: 'failed'
|
|
88
|
+
)
|
|
89
|
+
end
|
|
90
|
+
Legion::Extensions::Synapse::Helpers::Confidence.adjust_persisted(synapse: synapse, event: :failure)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
71
94
|
def count_consecutive_failures(synapse)
|
|
72
95
|
recent = synapse.signals_dataset
|
|
73
96
|
.exclude(downstream_outcome: nil)
|
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
require 'digest'
|
|
4
|
+
require 'legion/extensions/synapse/data/models/synapse'
|
|
5
|
+
require 'legion/extensions/synapse/helpers/knowledge'
|
|
6
|
+
require 'legion/extensions/synapse/data/models/synapse_mutation'
|
|
5
7
|
|
|
6
8
|
module Legion
|
|
7
9
|
module Extensions
|
|
8
10
|
module Synapse
|
|
9
11
|
module Runners
|
|
10
12
|
module Promote
|
|
13
|
+
include Legion::Extensions::Synapse::Helpers::Knowledge
|
|
14
|
+
|
|
11
15
|
CONFIDENCE_THRESHOLD = 0.9
|
|
12
16
|
STABILITY_HOURS = 24
|
|
13
17
|
|
|
14
|
-
def promote(synapse_id: nil, **)
|
|
18
|
+
def promote(synapse_id: nil, apollo_client: nil, publish: true, **)
|
|
15
19
|
Data::Model.define_synapse_model
|
|
16
20
|
Data::Model.define_synapse_mutation_model
|
|
17
21
|
candidates = if synapse_id
|
|
@@ -21,19 +25,26 @@ module Legion
|
|
|
21
25
|
find_promotable
|
|
22
26
|
end
|
|
23
27
|
|
|
24
|
-
|
|
25
|
-
candidates.
|
|
26
|
-
|
|
28
|
+
entries = candidates.select { |synapse| promotable?(synapse) }.map { |synapse| build_knowledge_entry(synapse) }
|
|
29
|
+
result = { success: true, candidates: entries, candidate_count: entries.size, promoted: [], submitted: [], errors: [] }
|
|
30
|
+
if publish && !entries.empty?
|
|
31
|
+
provider = knowledge_provider(client: apollo_client)
|
|
32
|
+
return result.merge(success: false, count: 0, error: 'Apollo unavailable') unless provider
|
|
27
33
|
|
|
28
|
-
|
|
29
|
-
|
|
34
|
+
entries.each do |entry|
|
|
35
|
+
response = publish_pattern(provider: provider, entry: entry)
|
|
36
|
+
if response[:success] != true
|
|
37
|
+
result[:errors] << { synapse_id: entry[:synapse_id], error: response[:error] || 'publication not confirmed' }
|
|
38
|
+
elsif response[:mode].to_s == 'async'
|
|
39
|
+
result[:submitted] << entry
|
|
40
|
+
elsif response[:entry_id] || response[:id] || response[:mode].to_s == 'deduplicated'
|
|
41
|
+
result[:promoted] << entry
|
|
42
|
+
else
|
|
43
|
+
result[:errors] << { synapse_id: entry[:synapse_id], error: 'publication not confirmed' }
|
|
44
|
+
end
|
|
45
|
+
end
|
|
30
46
|
end
|
|
31
|
-
|
|
32
|
-
{
|
|
33
|
-
success: true,
|
|
34
|
-
promoted: promoted,
|
|
35
|
-
count: promoted.size
|
|
36
|
-
}
|
|
47
|
+
result.merge(success: result[:errors].empty?, count: result[:promoted].size)
|
|
37
48
|
end
|
|
38
49
|
|
|
39
50
|
private
|
|
@@ -49,26 +60,31 @@ module Legion
|
|
|
49
60
|
return false if synapse.confidence < CONFIDENCE_THRESHOLD
|
|
50
61
|
return false unless synapse.status == 'active'
|
|
51
62
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
recent_reverts.zero?
|
|
63
|
+
cutoff = Time.now - (STABILITY_HOURS * 3600)
|
|
64
|
+
return false if synapse.created_at > cutoff
|
|
65
|
+
|
|
66
|
+
synapse.mutations_dataset.where { created_at > cutoff }.none?
|
|
57
67
|
end
|
|
58
68
|
|
|
59
69
|
def build_knowledge_entry(synapse)
|
|
70
|
+
pattern = {
|
|
71
|
+
format: Legion::Extensions::Synapse::Helpers::Knowledge::PATTERN_FORMAT,
|
|
72
|
+
schema_version: 1,
|
|
73
|
+
source_function_id: synapse.source_function_id,
|
|
74
|
+
target_function_id: synapse.target_function_id,
|
|
75
|
+
attention: synapse.attention,
|
|
76
|
+
transform: synapse.transform,
|
|
77
|
+
routing_strategy: synapse.routing_strategy,
|
|
78
|
+
confidence: synapse.confidence,
|
|
79
|
+
origin: synapse.origin,
|
|
80
|
+
version: synapse.version
|
|
81
|
+
}
|
|
82
|
+
# Apollo normalizes content case for deduplication. Keep case-sensitive
|
|
83
|
+
# template changes distinct through a stable lowercase digest in the body.
|
|
84
|
+
pattern[:fingerprint] = Digest::SHA256.hexdigest(Legion::JSON.dump(pattern))
|
|
60
85
|
{
|
|
61
86
|
content_type: 'synapse_pattern',
|
|
62
|
-
content: Legion::JSON.dump(
|
|
63
|
-
source_function_id: synapse.source_function_id,
|
|
64
|
-
target_function_id: synapse.target_function_id,
|
|
65
|
-
attention: synapse.attention,
|
|
66
|
-
transform: synapse.transform,
|
|
67
|
-
routing_strategy: synapse.routing_strategy,
|
|
68
|
-
confidence: synapse.confidence,
|
|
69
|
-
origin: synapse.origin,
|
|
70
|
-
version: synapse.version
|
|
71
|
-
}),
|
|
87
|
+
content: Legion::JSON.dump(pattern),
|
|
72
88
|
tags: ['synapse', "origin:#{synapse.origin}", "route:#{synapse.routing_strategy}"],
|
|
73
89
|
source_agent: 'lex-synapse',
|
|
74
90
|
synapse_id: synapse.id
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'legion/logging/helper'
|
|
4
|
+
require 'legion/extensions/synapse/helpers/target_schema'
|
|
4
5
|
|
|
5
6
|
require 'legion/extensions/synapse/helpers/confidence'
|
|
6
7
|
require 'legion/extensions/synapse/helpers/proposals'
|
|
@@ -235,13 +236,8 @@ module Legion
|
|
|
235
236
|
end
|
|
236
237
|
|
|
237
238
|
def discovery_schema(function_id)
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
discovery.function_schema(function_id)
|
|
242
|
-
rescue StandardError => e
|
|
243
|
-
raise if e.is_a?(NoMethodError) || e.is_a?(ArgumentError) || e.is_a?(TypeError)
|
|
244
|
-
|
|
239
|
+
Legion::Extensions::Synapse::Helpers::TargetSchema.resolve(function_id: function_id, db: Sequel::Model.db)
|
|
240
|
+
rescue Sequel::DatabaseError, Legion::JSON::ParseError => e
|
|
245
241
|
handle_exception(e, level: :warn, operation: :lookup_target_schema, function_id: function_id)
|
|
246
242
|
nil
|
|
247
243
|
end
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'legion/logging/helper'
|
|
4
|
+
require 'legion/extensions/synapse/helpers/knowledge'
|
|
4
5
|
|
|
5
6
|
require 'legion/extensions/synapse/data/models/synapse'
|
|
6
7
|
require 'legion/extensions/synapse/helpers/confidence'
|
|
@@ -11,18 +12,27 @@ module Legion
|
|
|
11
12
|
module Runners
|
|
12
13
|
module Retrieve
|
|
13
14
|
include Legion::Logging::Helper
|
|
15
|
+
include Legion::Extensions::Synapse::Helpers::Knowledge
|
|
14
16
|
|
|
15
17
|
SEED_CONFIDENCE_THRESHOLD = 0.7
|
|
16
18
|
|
|
17
|
-
def retrieve_and_seed(knowledge_entries
|
|
19
|
+
def retrieve_and_seed(knowledge_entries: nil, apollo_client: nil, query: 'synapse routing patterns', limit: 100, **)
|
|
18
20
|
Data::Model.define_synapse_model
|
|
21
|
+
if knowledge_entries.nil?
|
|
22
|
+
provider = knowledge_provider(client: apollo_client)
|
|
23
|
+
return { success: false, error: 'Apollo unavailable', seeded: [], count: 0 } unless provider
|
|
24
|
+
|
|
25
|
+
response = fetch_patterns(provider: provider, query: query, limit: limit)
|
|
26
|
+
unless response[:success] == true && response[:entries].is_a?(Array)
|
|
27
|
+
return { success: false, error: response[:error] || 'retrieval not complete', seeded: [], count: 0 }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
knowledge_entries = response[:entries]
|
|
31
|
+
end
|
|
19
32
|
seeded = []
|
|
20
33
|
|
|
21
34
|
knowledge_entries.each do |entry|
|
|
22
|
-
|
|
23
|
-
next unless entry[:content_type] == 'synapse_pattern'
|
|
24
|
-
|
|
25
|
-
pattern = parse_pattern(entry)
|
|
35
|
+
pattern = eligible_pattern(entry: entry)
|
|
26
36
|
next unless pattern
|
|
27
37
|
next if synapse_exists?(pattern[:source_function_id], pattern[:target_function_id])
|
|
28
38
|
|
|
@@ -44,8 +54,22 @@ module Legion
|
|
|
44
54
|
|
|
45
55
|
private
|
|
46
56
|
|
|
57
|
+
def eligible_pattern(entry:)
|
|
58
|
+
return unless entry[:confidence] && entry[:confidence] >= SEED_CONFIDENCE_THRESHOLD
|
|
59
|
+
|
|
60
|
+
pattern = parse_pattern(entry)
|
|
61
|
+
return unless pattern.is_a?(Hash)
|
|
62
|
+
|
|
63
|
+
tagged = pattern[:format] == Legion::Extensions::Synapse::Helpers::Knowledge::PATTERN_FORMAT && pattern[:schema_version] == 1
|
|
64
|
+
return unless tagged || (entry[:content_type] == 'synapse_pattern' && !pattern.key?(:format))
|
|
65
|
+
return unless pattern[:source_function_id].is_a?(Integer) && pattern[:source_function_id].positive?
|
|
66
|
+
return unless pattern[:target_function_id].is_a?(Integer) && pattern[:target_function_id].positive?
|
|
67
|
+
|
|
68
|
+
pattern
|
|
69
|
+
end
|
|
70
|
+
|
|
47
71
|
def parse_pattern(entry)
|
|
48
|
-
content = entry[:content]
|
|
72
|
+
content = entry[:raw_content] || entry[:content]
|
|
49
73
|
return nil unless content
|
|
50
74
|
|
|
51
75
|
content.is_a?(String) ? Legion::JSON.load(content) : content
|
|
@@ -1,31 +1,33 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
require 'legion/extensions/synapse/data/models/synapse'
|
|
4
|
+
require 'legion/extensions/synapse/data/models/synapse_mutation'
|
|
5
5
|
|
|
6
6
|
module Legion
|
|
7
7
|
module Extensions
|
|
8
8
|
module Synapse
|
|
9
9
|
module Runners
|
|
10
10
|
module Revert
|
|
11
|
-
def revert(synapse_id:, to_version: nil, trigger: 'pain')
|
|
11
|
+
def revert(synapse_id:, to_version: nil, mutation_id: nil, trigger: 'pain', **)
|
|
12
12
|
Data::Model.define_synapse_model
|
|
13
13
|
Data::Model.define_synapse_mutation_model
|
|
14
14
|
synapse = Data::Model::Synapse[synapse_id]
|
|
15
15
|
return { success: false, error: 'synapse not found' } unless synapse
|
|
16
16
|
|
|
17
|
+
return { success: false, error: 'provide mutation_id or to_version, not both' } if mutation_id && to_version
|
|
18
|
+
|
|
17
19
|
synapse.db.transaction do
|
|
18
20
|
synapse.lock!
|
|
19
21
|
mutation_version = to_version || synapse.version
|
|
20
|
-
next { success: false, error: 'no previous version' } if mutation_version < 2
|
|
21
|
-
|
|
22
|
-
mutation = Data::Model::SynapseMutation.where(
|
|
23
|
-
synapse_id: synapse.id,
|
|
24
|
-
version: mutation_version
|
|
25
|
-
).first
|
|
22
|
+
next { success: false, error: 'no previous version' } if !mutation_id && mutation_version < 2
|
|
26
23
|
|
|
27
|
-
|
|
24
|
+
history = Data::Model::SynapseMutation.where(synapse_id: synapse.id)
|
|
25
|
+
candidates = (mutation_id ? history.where(id: mutation_id) : history.where(version: mutation_version)).limit(2).all
|
|
26
|
+
next { success: false, error: "mutation version or ID not found: #{mutation_id || mutation_version}" } if candidates.empty?
|
|
27
|
+
next { success: false, error: 'ambiguous mutation version; provide mutation_id' } if candidates.size > 1
|
|
28
28
|
|
|
29
|
+
mutation = candidates.first
|
|
30
|
+
mutation_version = mutation.version
|
|
29
31
|
before_state = Legion::JSON.load(mutation.before_state)
|
|
30
32
|
restored_version = before_state[:version] || (mutation_version - 1)
|
|
31
33
|
revert_version = [synapse.version, synapse.mutations_dataset.max(:version) || 1].max + 1
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: lex-synapse
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.4.
|
|
4
|
+
version: 0.4.40
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Esity
|
|
@@ -186,8 +186,10 @@ files:
|
|
|
186
186
|
- lib/legion/extensions/synapse/helpers/challenge.rb
|
|
187
187
|
- lib/legion/extensions/synapse/helpers/confidence.rb
|
|
188
188
|
- lib/legion/extensions/synapse/helpers/homeostasis.rb
|
|
189
|
+
- lib/legion/extensions/synapse/helpers/knowledge.rb
|
|
189
190
|
- lib/legion/extensions/synapse/helpers/proposals.rb
|
|
190
191
|
- lib/legion/extensions/synapse/helpers/relationship_wrapper.rb
|
|
192
|
+
- lib/legion/extensions/synapse/helpers/target_schema.rb
|
|
191
193
|
- lib/legion/extensions/synapse/integration.rb
|
|
192
194
|
- lib/legion/extensions/synapse/runners/blast_radius.rb
|
|
193
195
|
- lib/legion/extensions/synapse/runners/challenge.rb
|