insika 0.2.0 → 0.3.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 +4 -4
- data/CHANGELOG.md +145 -17
- data/README.md +7 -1
- data/bin/insika +211 -8
- data/docs/AGENTS.md +377 -2
- data/docs/API.md +56 -0
- data/docs/CHANNELS.md +99 -9
- data/docs/CONTEXT.md +127 -16
- data/docs/DEPLOY.md +5 -2
- data/docs/EVALS.md +19 -0
- data/docs/FACTS.md +135 -0
- data/docs/HARVEST.md +117 -0
- data/docs/LOADTEST.md +14 -8
- data/docs/OBSERVABILITY.md +54 -0
- data/docs/REFINEMENT.md +3 -3
- data/docs/RELEASING.md +27 -0
- data/docs/RUNNING-LOCAL.md +3 -3
- data/docs/SECURITY.md +61 -0
- data/docs/SKILLS.md +2 -2
- data/docs/SOAK.md +127 -0
- data/docs/TOOLS.md +70 -2
- data/docs/domain.md +115 -0
- data/docs/index.md +1 -1
- data/lib/insika/agent_profile.rb +145 -10
- data/lib/insika/balloon_splitter.rb +102 -0
- data/lib/insika/budget_ledger.rb +34 -3
- data/lib/insika/cache_series_store.rb +49 -0
- data/lib/insika/channel_delivery.rb +118 -13
- data/lib/insika/channels/relay.rb +77 -3
- data/lib/insika/channels/web.rb +2 -2
- data/lib/insika/chat_builder.rb +84 -1
- data/lib/insika/checkpoint_store.rb +16 -0
- data/lib/insika/commands/agent_payload.rb +3 -3
- data/lib/insika/commands/cancel_followup.rb +49 -0
- data/lib/insika/commands/delete_tenant_data.rb +95 -0
- data/lib/insika/commands/export_customer_memory.rb +48 -0
- data/lib/insika/commands/forget_customer.rb +117 -0
- data/lib/insika/commands/freeze_funnel_baseline.rb +113 -0
- data/lib/insika/commands/gate_harvest.rb +138 -0
- data/lib/insika/commands/gate_refinement.rb +1 -1
- data/lib/insika/commands/judge_shadow_pairs.rb +124 -0
- data/lib/insika/commands/memory_forget_fact.rb +20 -4
- data/lib/insika/commands/memory_put_fact.rb +23 -4
- data/lib/insika/commands/promote_harvest.rb +130 -0
- data/lib/insika/commands/record_outcome.rb +46 -0
- data/lib/insika/commands/record_shadow_reply.rb +68 -0
- data/lib/insika/commands/reject_harvest.rb +38 -0
- data/lib/insika/commands/resolve_proposal.rb +108 -0
- data/lib/insika/commands/revoke_contact.rb +49 -0
- data/lib/insika/commands/rollback_harvest.rb +86 -0
- data/lib/insika/commands/run_distillation.rb +186 -0
- data/lib/insika/commands/run_harvest.rb +393 -0
- data/lib/insika/commands/send_message.rb +103 -6
- data/lib/insika/commands/session_purge.rb +67 -0
- data/lib/insika/contact_store.rb +183 -0
- data/lib/insika/context/builder.rb +21 -3
- data/lib/insika/context/fragment.rb +7 -3
- data/lib/insika/context/priority.rb +3 -0
- data/lib/insika/context/provider.rb +17 -3
- data/lib/insika/context/providers/briefing.rb +96 -0
- data/lib/insika/context/providers/memory.rb +16 -7
- data/lib/insika/context/providers/prompt.rb +30 -2
- data/lib/insika/context/providers/skill.rb +2 -0
- data/lib/insika/context/providers/tool_search.rb +2 -0
- data/lib/insika/context_trace_store.rb +38 -2
- data/lib/insika/distill.rb +224 -0
- data/lib/insika/distill_engine.rb +169 -0
- data/lib/insika/doctor.rb +716 -6
- data/lib/insika/dsl/runtime.rb +9 -3
- data/lib/insika/dsl/server_boot.rb +70 -1
- data/lib/insika/dsl.rb +83 -3
- data/lib/insika/edge_limiter.rb +28 -7
- data/lib/insika/env_schema.rb +9 -2
- data/lib/insika/errors.rb +11 -0
- data/lib/insika/evals/assertions.rb +3 -2
- data/lib/insika/evals/pairwise.rb +18 -6
- data/lib/insika/evidence.rb +183 -0
- data/lib/insika/executor.rb +687 -70
- data/lib/insika/followup_engine.rb +207 -0
- data/lib/insika/followup_policy.rb +221 -0
- data/lib/insika/followup_store.rb +306 -0
- data/lib/insika/funnel_declaration.rb +106 -0
- data/lib/insika/funnel_fold.rb +179 -0
- data/lib/insika/funnel_store.rb +163 -0
- data/lib/insika/golden_store.rb +1 -1
- data/lib/insika/grounding/matcher.rb +69 -0
- data/lib/insika/grounding.rb +44 -0
- data/lib/insika/harvest/conversion_gate.rb +159 -0
- data/lib/insika/harvest/criterion.rb +98 -0
- data/lib/insika/harvest/gate.rb +194 -0
- data/lib/insika/harvest/negative_list.rb +199 -0
- data/lib/insika/harvest.rb +241 -0
- data/lib/insika/harvest_engine.rb +193 -0
- data/lib/insika/harvest_store.rb +548 -0
- data/lib/insika/media.rb +298 -0
- data/lib/insika/memory_audit_store.rb +85 -0
- data/lib/insika/memory_store.rb +264 -23
- data/lib/insika/message_origin.rb +6 -1
- data/lib/insika/model_visible.rb +87 -0
- data/lib/insika/model_visible_trace_store.rb +66 -0
- data/lib/insika/onboarding.rb +6 -1
- data/lib/insika/outbox_store.rb +42 -4
- data/lib/insika/outcome_store.rb +147 -0
- data/lib/insika/packaging.rb +163 -0
- data/lib/insika/parity/criterion.rb +79 -0
- data/lib/insika/parity/verdict.rb +318 -0
- data/lib/insika/prefix_fingerprint.rb +58 -0
- data/lib/insika/profile_source.rb +22 -1
- data/lib/insika/proposal_store.rb +271 -0
- data/lib/insika/queue_policy.rb +4 -1
- data/lib/insika/refinement/proposer.rb +1 -1
- data/lib/insika/reliability.rb +32 -6
- data/lib/insika/retention.rb +281 -0
- data/lib/insika/routing.rb +101 -0
- data/lib/insika/safety/config.rb +44 -4
- data/lib/insika/safety/corpus.rb +255 -0
- data/lib/insika/safety/detectors.rb +32 -113
- data/lib/insika/safety/factory.rb +15 -2
- data/lib/insika/safety/grounding_enforcer.rb +59 -0
- data/lib/insika/safety/grounding_validator.rb +49 -0
- data/lib/insika/safety/input_guardrail.rb +1 -1
- data/lib/insika/safety/moderator.rb +1 -1
- data/lib/insika/safety/output_filter.rb +9 -5
- data/lib/insika/safety/output_validator.rb +11 -5
- data/lib/insika/schema_guard.rb +35 -0
- data/lib/insika/server/app.rb +144 -4
- data/lib/insika/server/rack_app.rb +16 -0
- data/lib/insika/server/responses.rb +40 -1
- data/lib/insika/session_actor.rb +8 -4
- data/lib/insika/session_store.rb +65 -2
- data/lib/insika/settings_store.rb +10 -0
- data/lib/insika/shadow_pair_store.rb +258 -0
- data/lib/insika/skill_catalog.rb +4 -0
- data/lib/insika/soak/envelope.rb +140 -0
- data/lib/insika/soak/report.rb +392 -0
- data/lib/insika/soak/runner.rb +554 -0
- data/lib/insika/store.rb +10 -1
- data/lib/insika/stores/memory.rb +6 -0
- data/lib/insika/stores/sqlite.rb +8 -0
- data/lib/insika/studio/app.rb +875 -14
- data/lib/insika/studio/assets/dist/application.css +1 -1
- data/lib/insika/studio/assets/dist/application.js +22 -22
- data/lib/insika/studio/assets/dist/favicon.svg +6 -0
- data/lib/insika/studio/forms.rb +262 -5
- data/lib/insika/studio/nav_icons.rb +6 -1
- data/lib/insika/studio/views/agent_detail.erb +627 -84
- data/lib/insika/studio/views/agents.erb +10 -6
- data/lib/insika/studio/views/approvals.erb +4 -1
- data/lib/insika/studio/views/chats.erb +4 -1
- data/lib/insika/studio/views/customer.erb +94 -0
- data/lib/insika/studio/views/customers.erb +32 -0
- data/lib/insika/studio/views/evals.erb +4 -1
- data/lib/insika/studio/views/facts.erb +133 -0
- data/lib/insika/studio/views/followups.erb +125 -0
- data/lib/insika/studio/views/funnel.erb +106 -0
- data/lib/insika/studio/views/harvest.erb +234 -0
- data/lib/insika/studio/views/home.erb +2 -1
- data/lib/insika/studio/views/layout.erb +1 -0
- data/lib/insika/studio/views/parity.erb +147 -0
- data/lib/insika/studio/views/playground.erb +7 -1
- data/lib/insika/studio/views/session.erb +56 -1
- data/lib/insika/studio/views/settings.erb +2 -0
- data/lib/insika/studio/views/skills.erb +0 -1
- data/lib/insika/studio/views/system_files.erb +1 -1
- data/lib/insika/studio/views/task.erb +13 -0
- data/lib/insika/studio/views/tasks.erb +4 -1
- data/lib/insika/studio/views/tools.erb +0 -1
- data/lib/insika/task_store.rb +21 -1
- data/lib/insika/testing/store_contract.rb +27 -6
- data/lib/insika/tick.rb +25 -1
- data/lib/insika/tool_definition.rb +17 -7
- data/lib/insika/tool_envelope.rb +69 -0
- data/lib/insika/tool_manifest.rb +5 -1
- data/lib/insika/tools/data_defined_tool.rb +10 -0
- data/lib/insika/tools/generate_image.rb +44 -0
- data/lib/insika/tools/schedule_followup.rb +164 -0
- data/lib/insika/tools/tts.rb +47 -0
- data/lib/insika/tools/update_briefing.rb +126 -0
- data/lib/insika/turn_state.rb +38 -1
- data/lib/insika/turn_timing.rb +22 -2
- data/lib/insika/version.rb +1 -1
- data/lib/insika/vitals.rb +84 -0
- data/lib/insika/wiring/graph.rb +300 -13
- data/lib/insika.rb +92 -3
- metadata +77 -1
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "ruby_llm"
|
|
4
|
+
require_relative "agent_enum"
|
|
5
|
+
|
|
6
|
+
module Insika
|
|
7
|
+
module Tools
|
|
8
|
+
# Shared write-edge plumbing for the two briefing tools: the
|
|
9
|
+
# session id always comes from the turn's task — never from the model's
|
|
10
|
+
# arguments — and the :briefing_updated event carries task/session
|
|
11
|
+
# correlation for the trace.
|
|
12
|
+
module BriefingWrite
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
# The turn's session, from the task (turn_state.rb:7) — never the model's args.
|
|
16
|
+
def session_id
|
|
17
|
+
@state.respond_to?(:task) ? @state.task&.session_id : nil
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def no_session_error = { error: "no session to brief" }
|
|
21
|
+
|
|
22
|
+
def emit(kind, field, value)
|
|
23
|
+
@event_stream&.emit(Insika::Event.new(
|
|
24
|
+
type: :briefing_updated,
|
|
25
|
+
data: { kind: kind, field: field, value: value },
|
|
26
|
+
meta: { task_id: @state.task&.id, session_id: @state.task&.session_id }
|
|
27
|
+
))
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Write edge of the session briefing: the agent records what it
|
|
32
|
+
# learned in THIS conversation — the working state it is accountable for
|
|
33
|
+
# (tool-written, never engine-inferred). System builtin (like remember):
|
|
34
|
+
# `require "ruby_llm"` stays in THIS file, loaded lazily by the Executor in
|
|
35
|
+
# create_chat. Wired only when the pack declared briefing_fields AND the
|
|
36
|
+
# builder has a session_store (double gate, like remember). Never enveloped:
|
|
37
|
+
# deterministic in-process writes.
|
|
38
|
+
class UpdateBriefing < RubyLLM::Tool
|
|
39
|
+
include BriefingWrite
|
|
40
|
+
|
|
41
|
+
# The class-level text carries no field list, so it can never render the
|
|
42
|
+
# empty "field must be one of: ." placeholder; the instance adds the
|
|
43
|
+
# DECLARED names (D3) to the description AND as the `field` enum (the
|
|
44
|
+
# model sees the valid set in the schema instead of inventing one —
|
|
45
|
+
# the set is instance-built. Per-instance, like the subagent tools' dynamic schemas.
|
|
46
|
+
BASE_DESCRIPTION = "Records a fact learned in this conversation into the session briefing. " \
|
|
47
|
+
"Call it as soon as the customer gives one of these, and when they correct one."
|
|
48
|
+
|
|
49
|
+
description BASE_DESCRIPTION
|
|
50
|
+
|
|
51
|
+
def self.description_for(fields)
|
|
52
|
+
names = Array(fields).map(&:to_s)
|
|
53
|
+
names.empty? ? BASE_DESCRIPTION : "#{BASE_DESCRIPTION} field must be one of: #{names.join(', ')}."
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
param :field, desc: "The briefing field name (one of the declared list)"
|
|
57
|
+
param :value, desc: "The value learned. Blank clears the field."
|
|
58
|
+
|
|
59
|
+
def name = "update_briefing"
|
|
60
|
+
|
|
61
|
+
def initialize(session_store:, fields:, event_stream:, state:)
|
|
62
|
+
@session_store = session_store
|
|
63
|
+
@fields = Array(fields).map(&:to_s)
|
|
64
|
+
@event_stream = event_stream
|
|
65
|
+
@state = state
|
|
66
|
+
super()
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def description
|
|
70
|
+
self.class.description_for(@fields)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# The declared names become the `field` enum in the schema the provider
|
|
74
|
+
# sees — per-turn data, like the subagent allowlist (Tools::AgentEnum).
|
|
75
|
+
def params_schema
|
|
76
|
+
@field_enum_schema ||= Insika::Tools::AgentEnum.inject(super, @fields, path: %i[field])
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# E2: an undeclared name returns an ENVELOPE error to the model and never
|
|
80
|
+
# persists. A missing session is the same shape (the model can retry later).
|
|
81
|
+
def execute(field:, value:)
|
|
82
|
+
sid = session_id
|
|
83
|
+
return no_session_error if sid.to_s.empty?
|
|
84
|
+
unless @fields.include?(field.to_s)
|
|
85
|
+
return { error: "unknown field '#{field}'; declared: #{@fields.join(', ')}" }
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
session = @session_store.update_briefing(sid, field: field.to_s, value: value.to_s)
|
|
89
|
+
emit("field", field.to_s, value.to_s)
|
|
90
|
+
{ updated: field.to_s, briefing: session.briefing }
|
|
91
|
+
rescue Insika::NotFoundError
|
|
92
|
+
{ error: "session not found" }
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# The agreed next step: same engine-owned briefing object,
|
|
96
|
+
# sibling writer. Blank clears to nil.
|
|
97
|
+
class SetNextStep < RubyLLM::Tool
|
|
98
|
+
include BriefingWrite
|
|
99
|
+
|
|
100
|
+
description "Records the next step agreed with the customer " \
|
|
101
|
+
"(e.g. 'send the payment link tomorrow at 10'). Blank clears it."
|
|
102
|
+
param :text, desc: "The agreed next step, in one sentence"
|
|
103
|
+
|
|
104
|
+
def name = "set_next_step"
|
|
105
|
+
|
|
106
|
+
def initialize(session_store:, event_stream:, state:)
|
|
107
|
+
@session_store = session_store
|
|
108
|
+
@event_stream = event_stream
|
|
109
|
+
@state = state
|
|
110
|
+
super()
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def execute(text:)
|
|
114
|
+
sid = session_id
|
|
115
|
+
return no_session_error if sid.to_s.empty?
|
|
116
|
+
|
|
117
|
+
session = @session_store.set_next_step(sid, text: text.to_s)
|
|
118
|
+
emit("next_step", nil, text.to_s)
|
|
119
|
+
{ next_step: session.briefing["next_step"] }
|
|
120
|
+
rescue Insika::NotFoundError
|
|
121
|
+
{ error: "session not found" }
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
data/lib/insika/turn_state.rb
CHANGED
|
@@ -25,11 +25,45 @@ module Insika
|
|
|
25
25
|
# (after_task); the Executor emits one :guardrail_flagged each.
|
|
26
26
|
:response_content, # the turn's final assistant text, set at stage 6/on halt so the
|
|
27
27
|
# after_task validator can inspect it.
|
|
28
|
+
:route, # WS4: the intent route the classifier chose (Symbol | nil).
|
|
29
|
+
# Set before the ask; rides the :route_classified event and the
|
|
30
|
+
# terminal event additively. nil = no routing on this turn.
|
|
31
|
+
:message_source, # WS9: how the turn's message entered (:voice when it came
|
|
32
|
+
# from a transcribed audio part; nil = typed). Rides the
|
|
33
|
+
# terminal event additively.
|
|
34
|
+
:media_attachments, # WS9: the ask's attachments (image parts); nil = none.
|
|
35
|
+
# The provider bills them; usage flows like any ask.
|
|
36
|
+
# The first image URL also lands on turn_context[:image_url]
|
|
37
|
+
# for data/HTTP tools (`{{ctx.image_url}}`).
|
|
38
|
+
:output_parts, # WS9 (saída): media the turn GENERATED, as additive
|
|
39
|
+
# parts ({ type:, mime_type:, base64:, model: }). Rides
|
|
40
|
+
# the terminal event (output_parts) and the /v1/responses
|
|
41
|
+
# envelope; the answer text stays text on purpose — the
|
|
42
|
+
# media is a separate sibling. nil = nothing generated.
|
|
43
|
+
:channel_capabilities, # WS9 (saída): the CHANNEL's declared OUTPUT media
|
|
44
|
+
# kinds (Media::OUTPUT_CAPABILITIES — image_output /
|
|
45
|
+
# audio_output). Half of the gate that decides whether
|
|
46
|
+
# generate_image/tts are wired at all (the other half is
|
|
47
|
+
# profile.outputs). [] = the channel declared nothing —
|
|
48
|
+
# no media can leak to it.
|
|
28
49
|
:output_filter, # per-turn Safety::OutputFilter (nil = off); redacts the stream.
|
|
29
|
-
:stuck_outcome
|
|
50
|
+
:stuck_outcome, # set by the signal_stuck system tool (WS5):
|
|
30
51
|
# { reason:, message: } when the agent declared it cannot
|
|
31
52
|
# proceed. The Executor tags the terminal event with
|
|
32
53
|
# outcome: "stuck" and emits :turn_stuck. nil = normal turn.
|
|
54
|
+
:evidence_ledger, # Insika::EvidenceLedger | nil — the
|
|
55
|
+
# session-scoped set of product ids that entered the
|
|
56
|
+
# context via an evidence-declared tool (the envelope
|
|
57
|
+
# appends; the validator/enforcer read it). Built per
|
|
58
|
+
# turn by the Executor; nil = no session/no evidence.
|
|
59
|
+
:evidence_attachments, # [ {type, url, caption} ] hoarded by the
|
|
60
|
+
# envelope this turn; read by the Executor at stage 8
|
|
61
|
+
# for the channel delivery. Reset per turn.
|
|
62
|
+
:context_trace_entry # the sanitized trace entry parked at
|
|
63
|
+
# prepare_turn (fingerprints + invalidation_reason);
|
|
64
|
+
# the stage-8 stamp merges the cache-hit fields into
|
|
65
|
+
# it and re-records the same (task_id, turn) key.
|
|
66
|
+
# nil = no store wired / no session.
|
|
33
67
|
|
|
34
68
|
# Internal (not part of the contract): per-CALL correlation between RubyLLM's
|
|
35
69
|
# tool callbacks and the tool decorators — `current_tool_call` keys the
|
|
@@ -151,6 +185,9 @@ module Insika
|
|
|
151
185
|
@turn = turn
|
|
152
186
|
@message = message
|
|
153
187
|
@capability_names = {}
|
|
188
|
+
@output_parts = []
|
|
189
|
+
@channel_capabilities = []
|
|
190
|
+
@evidence_attachments = []
|
|
154
191
|
# Fiber storage is INHERITED by fibers created later, so a turn spawned from
|
|
155
192
|
# inside a tool call (a subagent child) would start out carrying its
|
|
156
193
|
# parent's correlation. Clearing at turn start keeps a child from keying its
|
data/lib/insika/turn_timing.rb
CHANGED
|
@@ -26,22 +26,42 @@ module Insika
|
|
|
26
26
|
Insika::EnvSchema.truthy?(Insika::EnvSchema.read("INSIKA_TURN_TIMING", env))
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
# The marks that only mean something under INSIKA_TURN_TIMING. A `breakdown:
|
|
30
|
+
# false` clock (a channel turn with the flag off) ignores them and measures
|
|
31
|
+
# only first_balloon_ms.
|
|
32
|
+
BREAKDOWN_MARKS = %i[prep_start ask first_token done].freeze
|
|
33
|
+
private_constant :BREAKDOWN_MARKS
|
|
34
|
+
|
|
35
|
+
# breakdown: true (the default) is the flag-on clock: prep/ttft/gen/total.
|
|
36
|
+
# false is the channel clock: only :inbound -> :first_balloon,
|
|
37
|
+
# so a channel turn with the flag off still answers H-latência.
|
|
38
|
+
def initialize(breakdown: true)
|
|
30
39
|
@marks = {}
|
|
40
|
+
@breakdown = breakdown
|
|
31
41
|
end
|
|
32
42
|
|
|
33
43
|
def mark(name)
|
|
44
|
+
return unless @breakdown || !BREAKDOWN_MARKS.include?(name)
|
|
45
|
+
|
|
34
46
|
@marks[name] ||= Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
35
47
|
end
|
|
36
48
|
|
|
49
|
+
# Has this mark fired? Used by `SendMessage` to prove the channel clock was
|
|
50
|
+
# stamped at 202 acceptance (`:inbound` before the debounce window), and by
|
|
51
|
+
# the pipeline to know a threaded clock already started.
|
|
52
|
+
def marked?(name) = @marks.key?(name)
|
|
53
|
+
|
|
37
54
|
# -> Hash of phase deltas in ms (only the windows whose endpoints both fired;
|
|
38
55
|
# a workflow turn has no ask/first_token, so those are simply absent).
|
|
56
|
+
# first_balloon_ms is the inbound -> first outbox flush window.
|
|
57
|
+
# A missing number is never a zero — no mark, no key.
|
|
39
58
|
def to_h
|
|
40
59
|
{
|
|
41
60
|
prep_ms: delta(:prep_start, :ask),
|
|
42
61
|
ttft_ms: delta(:ask, :first_token),
|
|
43
62
|
gen_ms: delta(:first_token, :done),
|
|
44
|
-
total_ms: delta(:prep_start, :done)
|
|
63
|
+
total_ms: delta(:prep_start, :done),
|
|
64
|
+
first_balloon_ms: delta(:inbound, :first_balloon)
|
|
45
65
|
}.compact
|
|
46
66
|
end
|
|
47
67
|
|
data/lib/insika/version.rb
CHANGED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Insika
|
|
4
|
+
# Process vitals. One cheap read-only answer to "which process
|
|
5
|
+
# is this, how long has it been up, how much memory does it hold, and what is
|
|
6
|
+
# the Ruby heap doing?" It touches NO store, allocates nothing meaningful,
|
|
7
|
+
# and knows nothing about soaks — a general operator surface the soak
|
|
8
|
+
# happens to be the first consumer of. Exposed at GET /v1/vitals (operator
|
|
9
|
+
# only).
|
|
10
|
+
module Vitals
|
|
11
|
+
# Stamped when this file is first required: the process's own start, which
|
|
12
|
+
# is what "uptime" has to mean (Falcon's controller start is not this
|
|
13
|
+
# worker's). Per-process, not per-App, so a worker respawn under a live
|
|
14
|
+
# container keeps its real age and a new pid reads as a new clock.
|
|
15
|
+
STARTED_AT = Time.now.utc
|
|
16
|
+
|
|
17
|
+
# The subset of GC.stat that answers "is the Ruby heap growing, or is this
|
|
18
|
+
# the allocator?" — the difference between a leak and fragmentation, which
|
|
19
|
+
# is the first fork of any leak hunt .
|
|
20
|
+
GC_KEYS = %i[heap_live_slots heap_free_slots heap_allocated_pages
|
|
21
|
+
total_allocated_objects total_freed_objects
|
|
22
|
+
major_gc_count minor_gc_count
|
|
23
|
+
malloc_increase_bytes oldmalloc_increase_bytes].freeze
|
|
24
|
+
|
|
25
|
+
module_function
|
|
26
|
+
|
|
27
|
+
# -> Hash with STRING keys (it is a JSON body, not an internal value
|
|
28
|
+
# object). `executor:`/`db_path:` are optional — absent, the body simply
|
|
29
|
+
# omits those readings. `env:` is injected for specs.
|
|
30
|
+
def snapshot(executor: nil, db_path: nil, env: ENV)
|
|
31
|
+
{
|
|
32
|
+
"boot_id" => EnvSchema.read("INSIKA_BOOT_ID", env).to_s,
|
|
33
|
+
"pid" => Process.pid,
|
|
34
|
+
"started_at" => STARTED_AT.iso8601,
|
|
35
|
+
"uptime_s" => (Time.now.utc - STARTED_AT).round,
|
|
36
|
+
"version" => Insika::VERSION,
|
|
37
|
+
"ruby" => RUBY_DESCRIPTION,
|
|
38
|
+
"yjit" => (defined?(RubyVM::YJIT) && RubyVM::YJIT.enabled?),
|
|
39
|
+
"rss_bytes" => rss_bytes,
|
|
40
|
+
"gc" => gc_stat,
|
|
41
|
+
"threads" => Thread.list.size,
|
|
42
|
+
"in_flight" => executor&.in_flight&.size,
|
|
43
|
+
"db_bytes" => db_bytes(db_path),
|
|
44
|
+
"at" => Time.now.utc.iso8601
|
|
45
|
+
}
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Resident set size in bytes. Linux first (/proc/self/status VmRSS) —
|
|
49
|
+
# production is a Linux container; macOS/dev fall back to `ps`. Neither
|
|
50
|
+
# readable -> nil, NEVER a guess: a fabricated RSS would silently pass an
|
|
51
|
+
# envelope.
|
|
52
|
+
def rss_bytes
|
|
53
|
+
if File.file?("/proc/self/status")
|
|
54
|
+
match = File.read("/proc/self/status", encoding: "ASCII-8BIT")[/^VmRSS:\s+(\d+)\s*kB/, 1]
|
|
55
|
+
return Integer(match) * 1024 if match
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
out = `ps -o rss= -p #{Process.pid} 2>/dev/null`.strip
|
|
59
|
+
return Integer(out) * 1024 unless out.empty?
|
|
60
|
+
|
|
61
|
+
nil
|
|
62
|
+
rescue StandardError
|
|
63
|
+
nil
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def gc_stat
|
|
67
|
+
stat = GC.stat
|
|
68
|
+
GC_KEYS.each_with_object({}) { |key, acc| acc[key.to_s] = stat[key] }
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# SQLite file + WAL + shm bytes. -> { "db" =>, "wal" =>, "shm" => } | nil.
|
|
72
|
+
def db_bytes(path)
|
|
73
|
+
return nil if path.nil? || path.to_s.empty?
|
|
74
|
+
|
|
75
|
+
{
|
|
76
|
+
"db" => File.size(path.to_s),
|
|
77
|
+
"wal" => File.exist?("#{path}-wal") ? File.size("#{path}-wal") : 0,
|
|
78
|
+
"shm" => File.exist?("#{path}-shm") ? File.size("#{path}-shm") : 0
|
|
79
|
+
}
|
|
80
|
+
rescue Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR
|
|
81
|
+
nil
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|