File size: 13,309 Bytes
ca4d83b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
"""Alignment entry points. Calls go through dispatch, so the pin and the fence stay.

Fixtures are the inputs in tests/stress/stress_test_inhouse_ai.py test_harmonic_math.
No formula was added. A missing or non-finite input is unavailable.
"""

from __future__ import annotations

import json
from typing import Any

from aureon_public.body import _unavailable, dispatch
from aureon_public.quiet import quiet
from aureon_public.tools import _finite

# Inputs copied from tests/stress/stress_test_inhouse_ai.py::test_harmonic_math.
FIXTURE_SOURCE = "tests/stress/stress_test_inhouse_ai.py::test_harmonic_math"
FIXTURE_NOTE = (
    "Fixed fixture from tests/stress/stress_test_inhouse_ai.py::test_harmonic_math. "
    "Not a live market reading and not a lighthouse reading."
)
SPLIT_SIGNALS = ["BUY", "BUY", "SELL", "SELL", "NEUTRAL", "NEUTRAL"]
AGREE_SIGNALS = ["BUY", "BUY", "BUY", "BUY", "BUY", "BUY"]
ALIGNED_PHASES = [0.0, 0.0, 0.0]
PILLAR_FREQUENCIES = {
    "Nexus": 432.0,
    "Omega": 432.0,
    "Infinite": 528.0,
    "Piano": 396.0,
    "QGITA": 528.0,
    "Auris": 741.0,
}
PERFECT_STACK = [
    {
        "pillar": f"P{index}",
        "signal": "BUY",
        "confidence": 0.95,
        "coherence": 0.96,
        "frequency_hz": 528.0,
    }
    for index in range(6)
]
GEOMETRIC_ONES = [1.0, 1.0, 1.0, 1.0]


def _loads(text: str, kind: type) -> Any | str:
    try:
        loaded = json.loads(text)
    except json.JSONDecodeError:
        return "arguments are not JSON"
    if not isinstance(loaded, kind):
        return "arguments have the wrong shape"
    return loaded


def _call(module: str, symbol: str, arguments: dict[str, Any]) -> str:
    return dispatch(module, symbol, arguments, server="public")


def label_result(text: str, source: str, note: str) -> str:
    """Attach input_source and input_note to a tool result."""

    try:
        body = json.loads(text)
    except json.JSONDecodeError:
        return text
    if not isinstance(body, dict):
        return text
    body["input_note"] = note
    body["input_source"] = source
    return json.dumps(body, ensure_ascii=False, allow_nan=False, sort_keys=True)


def _annotate(text: str, used_fixture: bool) -> str:
    """Mark a default-fixture result so scores are not read as a live reading."""

    if not used_fixture:
        return text
    return label_result(text, FIXTURE_SOURCE, FIXTURE_NOTE)


DEFAULTS_NOTE = "These fields are the dataclass defaults, not a reading."
DERIVE_NOTE = (
    "528 Hz is FUNDAMENTAL_HZ and BUY is the fixed example branch in derive_phase. "
    "They are fixed example arguments, not a reading."
)


@quiet
def alignment_signal_consensus(signals_json: str = "") -> str:
    """compute_signal_consensus. Provenance: cited.

    The default list is the three-way split in test_harmonic_math. That fixture is not a live market reading and not a lighthouse reading. No pillar agent is built.

    Args:
        signals_json: JSON list of signal names. Empty uses the split fixture. Label: cited.
    """

    raw = signals_json.strip() or json.dumps(SPLIT_SIGNALS)
    parsed = _loads(raw, list)
    if isinstance(parsed, str):
        return _unavailable(parsed)
    if not parsed or not all(isinstance(item, str) for item in parsed):
        return _unavailable("signals must be a non-empty list of strings")
    return _annotate(
        _call(
            "alignment/harmonic_resonance.py",
            "compute_signal_consensus",
            {"signals": parsed},
        ),
        parsed == SPLIT_SIGNALS,
    )


@quiet
def alignment_consonance_read(ratio: float = 1.0) -> str:
    """consonance_score. Provenance: cited.

    This is the inventory entry point, called through the pin check. The helper
    alignment_consonance is a separate tool and is not this call. The default
    ratio 1.0 is unison in test_harmonic_math. That fixture is not a live market
    reading and not a lighthouse reading.

    Args:
        ratio: Frequency ratio. The default 1.0 is unison in test_harmonic_math. Label: cited.
    """

    number = _finite(ratio)
    if number is None:
        return _unavailable("ratio is missing")
    return _annotate(
        _call("alignment/harmonic_resonance.py", "consonance_score", {"ratio": number}),
        number == 1.0,
    )


@quiet
def alignment_frequency_harmony(frequencies_json: str = "") -> str:
    """analyse_frequency_harmony. Provenance: cited.

    fundamental_hz is omitted, so the function uses its own FUNDAMENTAL_HZ.
    The default map is the six pillar frequencies in test_harmonic_math. That
    fixture is not a live market reading and not a lighthouse reading.

    Args:
        frequencies_json: JSON object of name to Hz. Empty uses that map. Label: cited.
    """

    raw = frequencies_json.strip() or json.dumps(PILLAR_FREQUENCIES)
    parsed = _loads(raw, dict)
    if isinstance(parsed, str):
        return _unavailable(parsed)
    frequencies: dict[str, float] = {}
    for key, value in parsed.items():
        number = _finite(value)
        if number is None:
            return _unavailable(f"{key} is not a frequency")
        frequencies[str(key)] = number
    return _annotate(
        _call(
            "alignment/harmonic_resonance.py",
            "analyse_frequency_harmony",
            {"pillar_frequencies": frequencies},
        ),
        frequencies == PILLAR_FREQUENCIES,
    )


@quiet
def alignment_phase_coherence(phases_json: str = "") -> str:
    """compute_phase_coherence. Provenance: cited.

    The default phases are the aligned triple in test_harmonic_math. That fixture
    is not a live market reading and not a lighthouse reading.

    Args:
        phases_json: JSON list of radians. Empty uses that triple. Label: cited.
    """

    raw = phases_json.strip() or json.dumps(ALIGNED_PHASES)
    parsed = _loads(raw, list)
    if isinstance(parsed, str):
        return _unavailable(parsed)
    phases: list[float] = []
    for value in parsed:
        number = _finite(value)
        if number is None:
            return _unavailable("phases must be numbers")
        phases.append(number)
    return _annotate(
        _call(
            "alignment/harmonic_resonance.py",
            "compute_phase_coherence",
            {"phases_rad": phases},
        ),
        phases == ALIGNED_PHASES,
    )


@quiet
def alignment_derive_phase(frequency_hz: float = 528.0, t: float = 0.0, signal: str = "BUY") -> str:
    """derive_phase. Provenance: cited.

    528 Hz is FUNDAMENTAL_HZ in harmonic_resonance.py. t is 0 so the drift term is zero.
    BUY is the fixed example branch in derive_phase. Those defaults are not a reading.
    BUY, SELL, and any other string are the branches already in derive_phase.

    Args:
        frequency_hz: Pillar frequency in Hz. Label: cited, derive_phase.
        t: Time in seconds. Label: cited, derive_phase.
        signal: BUY, SELL, or another string. Label: cited, derive_phase.
    """

    frequency = _finite(frequency_hz)
    moment = _finite(t)
    if frequency is None or moment is None:
        return _unavailable("frequency_hz or t is missing")
    if not isinstance(signal, str) or not signal.strip():
        return _unavailable("signal is missing")
    text = _call(
        "alignment/harmonic_resonance.py",
        "derive_phase",
        {"frequency_hz": frequency, "t": moment, "signal": signal},
    )
    if frequency == 528.0 and moment == 0.0 and signal == "BUY":
        return label_result(text, "aureon.alignment.harmonic_resonance.derive_phase", DERIVE_NOTE)
    return text


@quiet
def alignment_geometric_mean(values_json: str = "") -> str:
    """geometric_mean. Provenance: cited.

    The default is the four ones in test_harmonic_math. That fixture is not a
    live market reading and not a lighthouse reading.

    Args:
        values_json: JSON list of numbers. Empty uses that list. Label: cited.
    """

    raw = values_json.strip() or json.dumps(GEOMETRIC_ONES)
    parsed = _loads(raw, list)
    if isinstance(parsed, str):
        return _unavailable(parsed)
    values: list[float] = []
    for value in parsed:
        number = _finite(value)
        if number is None:
            return _unavailable("values must be numbers")
        values.append(number)
    return _annotate(
        _call("alignment/harmonic_resonance.py", "geometric_mean", {"values": values}),
        values == GEOMETRIC_ONES,
    )


@quiet
def alignment_full_analysis(pillar_results_json: str = "", t: float = 0.0) -> str:
    """full_harmonic_analysis. Provenance: cited.

    t is passed, so the function does not read the clock. The default stack is the
    perfect six-pillar stack in test_harmonic_math, including frequency_hz 528.
    lighthouse_cleared, dominant_signal, and alignment_score from that fixture
    are not a live market reading and not a lighthouse reading.

    Args:
        pillar_results_json: JSON list of pillar result objects. Empty uses that stack. Label: cited.
        t: Time passed to full_harmonic_analysis. Label: cited.
    """

    moment = _finite(t)
    if moment is None:
        return _unavailable("t is missing")
    raw = pillar_results_json.strip() or json.dumps(PERFECT_STACK)
    parsed = _loads(raw, list)
    if isinstance(parsed, str):
        return _unavailable(parsed)
    if not all(isinstance(item, dict) for item in parsed):
        return _unavailable("pillar_results must be a list of objects")
    return _annotate(
        _call(
            "alignment/harmonic_resonance.py",
            "full_harmonic_analysis",
            {"pillar_results": parsed, "t": moment},
        ),
        moment == 0.0 and parsed == PERFECT_STACK,
    )


@quiet
def alignment_harmonic_to_dict() -> str:
    """HarmonicAnalysis.to_dict on the dataclass defaults. Provenance: cited.

    The class has no constructor body. Defaults are the literals in the class.
    lighthouse_cleared false and dominant_signal NEUTRAL are those defaults, not a reading.
    No pillar agent is built.

    Args:
        none: This tool takes no inputs. Label: cited, HarmonicAnalysis.to_dict.
    """

    return label_result(
        _call("alignment/harmonic_resonance.py", "HarmonicAnalysis.to_dict", {}),
        "aureon.alignment.harmonic_resonance.HarmonicAnalysis",
        DEFAULTS_NOTE,
    )


@quiet
def alignment_cycle_to_dict(cycle_id: str = "fixed", timestamp: float = 0.0) -> str:
    """AlignmentCycleResult.to_dict with a fixed id and timestamp. Provenance: cited.

    The dataclass defaults would call uuid and the clock. Those arguments are passed
    so this call does not. Other fields stay the dataclass defaults and are not a reading.

    Args:
        cycle_id: Passed as the constructor cycle_id. Label: cited.
        timestamp: Passed as the constructor timestamp. Label: cited.
    """

    moment = _finite(timestamp)
    if moment is None or not isinstance(cycle_id, str) or not cycle_id:
        return _unavailable("cycle_id or timestamp is missing")
    text = _call(
        "alignment/pillar_alignment.py",
        "AlignmentCycleResult.to_dict",
        {"init": {"cycle_id": cycle_id, "timestamp": moment}},
    )
    if cycle_id == "fixed" and moment == 0.0:
        return label_result(text, "aureon.alignment.pillar_alignment.AlignmentCycleResult", DEFAULTS_NOTE)
    return text


@quiet
def alignment_directive_to_dict(directive_id: str = "fixed", timestamp: float = 0.0) -> str:
    """MasterDirective.to_dict with a fixed id and timestamp. Provenance: cited.

    The dataclass defaults would call uuid and the clock. Those arguments are passed
    so this call does not. Other fields stay the dataclass defaults, including
    dominant_frequency_hz = FUNDAMENTAL_HZ, and are not a reading.

    Args:
        directive_id: Passed as the constructor directive_id. Label: cited.
        timestamp: Passed as the constructor timestamp. Label: cited.
    """

    moment = _finite(timestamp)
    if moment is None or not isinstance(directive_id, str) or not directive_id:
        return _unavailable("directive_id or timestamp is missing")
    text = _call(
        "alignment/unified_directive.py",
        "MasterDirective.to_dict",
        {"init": {"directive_id": directive_id, "timestamp": moment}},
    )
    if directive_id == "fixed" and moment == 0.0:
        return label_result(text, "aureon.alignment.unified_directive.MasterDirective", DEFAULTS_NOTE)
    return text


@quiet
def alignment_pillar_factory() -> str:
    """get_pillar_alignment. Provenance: cited.

    The function's own default config sets auto_load_pillars false, so this call
    does not load pillar agents. The constructor still runs its thought-bus wire.
    If that call fails, the result is unavailable. No order is sent.

    Args:
        none: This tool takes no inputs. Label: cited, get_pillar_alignment.
    """

    return _call("alignment/pillar_alignment.py", "get_pillar_alignment", {})


TOOLS: list[Any] = [
    alignment_signal_consensus,
    alignment_consonance_read,
    alignment_frequency_harmony,
    alignment_phase_coherence,
    alignment_derive_phase,
    alignment_geometric_mean,
    alignment_full_analysis,
    alignment_harmonic_to_dict,
    alignment_cycle_to_dict,
    alignment_directive_to_dict,
    alignment_pillar_factory,
]