@danceiny/gotry 0.0.1-rc.11 → 0.0.1-rc.12

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.
Files changed (37) hide show
  1. package/README.md +19 -7
  2. package/cordis.gotry-patch.yml +14 -7
  3. package/dist/capabilities/flyai.js +135 -30
  4. package/dist/capabilities/hbcli.js +14 -2
  5. package/dist/capabilities/session/benchmark.js +252 -0
  6. package/dist/capabilities/session/transport.js +14 -9
  7. package/dist/capabilities/session-consent.js +82 -0
  8. package/dist/capabilities/session-login.js +103 -0
  9. package/dist/capabilities/session-search.js +5 -3
  10. package/dist/capabilities/weather.js +72 -17
  11. package/dist/scripts/agent-reach-wrapper-tests.js +4 -0
  12. package/dist/scripts/async-collect.js +33 -5
  13. package/dist/scripts/hbcli-tests.js +25 -4
  14. package/dist/scripts/ledger-tests.js +134 -4
  15. package/dist/scripts/memory-value-report.js +379 -0
  16. package/dist/scripts/product-metrics.js +569 -0
  17. package/dist/scripts/session-benchmark.js +261 -0
  18. package/dist/scripts/session-login.js +28 -0
  19. package/dist/scripts/session-tests.js +260 -23
  20. package/dist/scripts/smoke.js +112 -14
  21. package/dist/scripts/weather-tests.js +8 -1
  22. package/dist/src/index.js +124 -5
  23. package/dist/src/loop.js +65 -14
  24. package/dist/src/state-ledger.js +30 -4
  25. package/package.json +4 -2
  26. package/ts/capabilities/flyai.ts +177 -22
  27. package/ts/capabilities/hbcli.ts +16 -4
  28. package/ts/capabilities/session/benchmark.ts +273 -0
  29. package/ts/capabilities/session/transport.ts +19 -8
  30. package/ts/capabilities/session-consent.ts +127 -0
  31. package/ts/capabilities/session-login.ts +130 -0
  32. package/ts/capabilities/session-search.ts +11 -4
  33. package/ts/capabilities/weather.ts +84 -19
  34. package/ts/scripts/async-collect.ts +48 -7
  35. package/ts/src/index.ts +107 -11
  36. package/ts/src/loop.ts +85 -12
  37. package/ts/src/state-ledger.ts +48 -4
@@ -0,0 +1,379 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { resolve } from 'node:path';
3
+ import { pathToFileURL } from 'node:url';
4
+ function isObject(value) {
5
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
6
+ }
7
+ function nonEmptyString(value) {
8
+ return typeof value === 'string' && value.trim().length > 0;
9
+ }
10
+ function finiteNumber(value) {
11
+ return typeof value === 'number' && Number.isFinite(value);
12
+ }
13
+ function round(value) {
14
+ return Math.round((value + Number.EPSILON) * 1_000_000) / 1_000_000;
15
+ }
16
+ function nearestRank(values, percentile) {
17
+ if (values.length === 0) return 0;
18
+ const sorted = [
19
+ ...values
20
+ ].sort((a, b)=>a - b);
21
+ const index = Math.max(0, Math.ceil(percentile * sorted.length) - 1);
22
+ return round(sorted[index]);
23
+ }
24
+ function parseTimestamp(value, path, errors) {
25
+ if (!nonEmptyString(value)) {
26
+ errors.push(`${path} must be a non-empty ISO timestamp`);
27
+ return null;
28
+ }
29
+ const parsed = Date.parse(value);
30
+ if (!Number.isFinite(parsed)) {
31
+ errors.push(`${path} is not a valid ISO timestamp`);
32
+ return null;
33
+ }
34
+ return parsed;
35
+ }
36
+ function scoreFlow(value, path, expectedEligibleIndex, allowedWaitCodes, errors) {
37
+ if (!isObject(value)) {
38
+ errors.push(`${path} must be an object`);
39
+ return null;
40
+ }
41
+ const flowId = value.flow_id;
42
+ if (!nonEmptyString(flowId)) errors.push(`${path}.flow_id must be non-empty`);
43
+ if (value.eligible !== true) errors.push(`${path}.eligible must be true`);
44
+ if (value.status !== 'completed') errors.push(`${path}.status must be completed`);
45
+ if (value.eligible_planning_index !== expectedEligibleIndex) {
46
+ errors.push(`${path}.eligible_planning_index must be ${expectedEligibleIndex}`);
47
+ }
48
+ const startedAtMs = parseTimestamp(value.started_at, `${path}.started_at`, errors);
49
+ const completedAtMs = parseTimestamp(value.completed_at, `${path}.completed_at`, errors);
50
+ if (startedAtMs === null || completedAtMs === null) return null;
51
+ if (completedAtMs <= startedAtMs) {
52
+ errors.push(`${path}.completed_at must be after started_at`);
53
+ return null;
54
+ }
55
+ const rawWaits = value.external_waits;
56
+ if (!Array.isArray(rawWaits)) {
57
+ errors.push(`${path}.external_waits must be an array`);
58
+ return null;
59
+ }
60
+ const waits = [];
61
+ for (const [index, rawWait] of rawWaits.entries()){
62
+ const waitPath = `${path}.external_waits[${index}]`;
63
+ if (!isObject(rawWait)) {
64
+ errors.push(`${waitPath} must be an object`);
65
+ continue;
66
+ }
67
+ const code = rawWait.code;
68
+ if (!nonEmptyString(code) || !allowedWaitCodes.has(code)) {
69
+ errors.push(`${waitPath}.code must be predeclared by measurement_policy`);
70
+ continue;
71
+ }
72
+ const waitStartedAtMs = parseTimestamp(rawWait.started_at, `${waitPath}.started_at`, errors);
73
+ const waitCompletedAtMs = parseTimestamp(rawWait.completed_at, `${waitPath}.completed_at`, errors);
74
+ if (waitStartedAtMs === null || waitCompletedAtMs === null) continue;
75
+ if (waitCompletedAtMs <= waitStartedAtMs) {
76
+ errors.push(`${waitPath}.completed_at must be after started_at`);
77
+ continue;
78
+ }
79
+ if (waitStartedAtMs < startedAtMs || waitCompletedAtMs > completedAtMs) {
80
+ errors.push(`${waitPath} must stay inside the planning flow`);
81
+ continue;
82
+ }
83
+ waits.push({
84
+ code,
85
+ startedAtMs: waitStartedAtMs,
86
+ completedAtMs: waitCompletedAtMs
87
+ });
88
+ }
89
+ waits.sort((a, b)=>a.startedAtMs - b.startedAtMs);
90
+ for(let index = 1; index < waits.length; index += 1){
91
+ if (waits[index].startedAtMs < waits[index - 1].completedAtMs) {
92
+ errors.push(`${path}.external_waits must not overlap`);
93
+ }
94
+ }
95
+ const externalWaitMs = waits.reduce((total, wait)=>total + wait.completedAtMs - wait.startedAtMs, 0);
96
+ const activeSeconds = (completedAtMs - startedAtMs - externalWaitMs) / 1_000;
97
+ if (activeSeconds <= 0) errors.push(`${path} must have positive active planning duration`);
98
+ return nonEmptyString(flowId) ? {
99
+ activeSeconds: round(activeSeconds),
100
+ completedAtMs,
101
+ flowId,
102
+ startedAtMs
103
+ } : null;
104
+ }
105
+ function invalidReport(errors, schema = '', evidenceKind = '') {
106
+ return {
107
+ schema: 'memory_value_report.v1',
108
+ contract_valid: false,
109
+ errors,
110
+ source: {
111
+ fixture_schema: schema,
112
+ evidence_kind: evidenceKind,
113
+ quantile_method: 'nearest_rank',
114
+ active_duration_rule: 'wall_clock_minus_non_overlapping_predeclared_external_waits'
115
+ },
116
+ cohort: {
117
+ eligible_pair_count: 0,
118
+ first_active_seconds: {
119
+ p50: 0,
120
+ p75: 0
121
+ },
122
+ returning_active_seconds: {
123
+ p50: 0,
124
+ p75: 0
125
+ },
126
+ paired_reduction_ratio: {
127
+ p50: 0,
128
+ p75: 0
129
+ },
130
+ target_median_reduction_ratio: 0,
131
+ target_met: false,
132
+ minimum_pair_count_for_exit: 0,
133
+ sample_size_met: false
134
+ },
135
+ experience_reflux: {
136
+ recalled_experience_count: 0,
137
+ verified_experience_count: 0,
138
+ baseline: null,
139
+ baseline_available: false,
140
+ real_evidence: false
141
+ },
142
+ preference_assertions: {
143
+ total_count: 0,
144
+ traceable_count: 0,
145
+ traceable_ratio: 0,
146
+ hard_filter_violation_count: 0,
147
+ contract_met: false
148
+ },
149
+ p4: {
150
+ state: 'unknown',
151
+ trigger_observed: false,
152
+ contract_met: false
153
+ },
154
+ exit_evidence_eligible: false,
155
+ exit_ready: false
156
+ };
157
+ }
158
+ export function scoreMemoryValue(input) {
159
+ const errors = [];
160
+ if (!isObject(input)) return invalidReport([
161
+ 'root must be an object'
162
+ ]);
163
+ const schema = nonEmptyString(input.schema) ? input.schema : '';
164
+ const evidenceKind = nonEmptyString(input.evidence_kind) ? input.evidence_kind : '';
165
+ if (schema !== 'memory_value_fixture.v1') errors.push('schema must be memory_value_fixture.v1');
166
+ if (![
167
+ 'synthetic_fixture',
168
+ 'observed_private'
169
+ ].includes(evidenceKind)) {
170
+ errors.push('evidence_kind must be synthetic_fixture or observed_private');
171
+ }
172
+ const policy = input.measurement_policy;
173
+ if (!isObject(policy)) return invalidReport([
174
+ ...errors,
175
+ 'measurement_policy must be an object'
176
+ ], schema, evidenceKind);
177
+ if (policy.quantile_method !== 'nearest_rank') errors.push('measurement_policy.quantile_method must be nearest_rank');
178
+ const rawWaitCodes = policy.predeclared_external_wait_codes;
179
+ const allowedWaitCodes = new Set();
180
+ if (!Array.isArray(rawWaitCodes) || rawWaitCodes.length === 0) {
181
+ errors.push('measurement_policy.predeclared_external_wait_codes must be a non-empty array');
182
+ } else {
183
+ for (const code of rawWaitCodes){
184
+ if (nonEmptyString(code)) allowedWaitCodes.add(code);
185
+ else errors.push('measurement_policy.predeclared_external_wait_codes must contain non-empty strings');
186
+ }
187
+ }
188
+ const minimumPairCount = policy.minimum_pair_count_for_exit;
189
+ const targetReduction = policy.target_median_reduction_ratio;
190
+ if (!Number.isInteger(minimumPairCount) || minimumPairCount <= 0) {
191
+ errors.push('measurement_policy.minimum_pair_count_for_exit must be a positive integer');
192
+ }
193
+ if (!finiteNumber(targetReduction) || targetReduction < 0 || targetReduction > 1) {
194
+ errors.push('measurement_policy.target_median_reduction_ratio must be between 0 and 1');
195
+ }
196
+ const rawPairs = input.pairs;
197
+ if (!Array.isArray(rawPairs) || rawPairs.length === 0) {
198
+ errors.push('pairs must be a non-empty array');
199
+ }
200
+ const firstDurations = [];
201
+ const returningDurations = [];
202
+ const reductions = [];
203
+ const pairIds = new Set();
204
+ const subjectRefs = new Set();
205
+ const flowIds = new Set();
206
+ for (const [index, rawPair] of (Array.isArray(rawPairs) ? rawPairs : []).entries()){
207
+ const pairPath = `pairs[${index}]`;
208
+ if (!isObject(rawPair)) {
209
+ errors.push(`${pairPath} must be an object`);
210
+ continue;
211
+ }
212
+ if (!nonEmptyString(rawPair.pair_id)) errors.push(`${pairPath}.pair_id must be non-empty`);
213
+ else if (pairIds.has(rawPair.pair_id)) errors.push(`${pairPath}.pair_id must be unique`);
214
+ else pairIds.add(rawPair.pair_id);
215
+ if (!nonEmptyString(rawPair.subject_ref)) {
216
+ errors.push(`${pairPath}.subject_ref must be a non-empty pseudonymous reference`);
217
+ } else if (subjectRefs.has(rawPair.subject_ref)) {
218
+ errors.push(`${pairPath}.subject_ref must be unique across the paired cohort`);
219
+ } else {
220
+ subjectRefs.add(rawPair.subject_ref);
221
+ }
222
+ const first = scoreFlow(rawPair.first, `${pairPath}.first`, 1, allowedWaitCodes, errors);
223
+ const returning = scoreFlow(rawPair.returning, `${pairPath}.returning`, 2, allowedWaitCodes, errors);
224
+ for (const flow of [
225
+ first,
226
+ returning
227
+ ]){
228
+ if (!flow) continue;
229
+ if (flowIds.has(flow.flowId)) errors.push(`${pairPath} flow_id values must be globally unique`);
230
+ flowIds.add(flow.flowId);
231
+ }
232
+ if (!first || !returning || first.activeSeconds <= 0 || returning.activeSeconds <= 0) continue;
233
+ if (returning.startedAtMs <= first.completedAtMs) {
234
+ errors.push(`${pairPath}.returning must start after the first completed flow`);
235
+ }
236
+ firstDurations.push(first.activeSeconds);
237
+ returningDurations.push(returning.activeSeconds);
238
+ reductions.push(round((first.activeSeconds - returning.activeSeconds) / first.activeSeconds));
239
+ }
240
+ const rawRefluxEvents = input.experience_reflux_events;
241
+ if (!Array.isArray(rawRefluxEvents)) errors.push('experience_reflux_events must be an array');
242
+ const recalled = new Set();
243
+ const verified = new Set();
244
+ for (const [index, rawEvent] of (Array.isArray(rawRefluxEvents) ? rawRefluxEvents : []).entries()){
245
+ const eventPath = `experience_reflux_events[${index}]`;
246
+ if (!isObject(rawEvent)) {
247
+ errors.push(`${eventPath} must be an object`);
248
+ continue;
249
+ }
250
+ if (!nonEmptyString(rawEvent.experience_id)) {
251
+ errors.push(`${eventPath}.experience_id must be non-empty`);
252
+ continue;
253
+ }
254
+ if (!nonEmptyString(rawEvent.evidence_ref)) errors.push(`${eventPath}.evidence_ref must be non-empty`);
255
+ if (rawEvent.kind === 'recalled') recalled.add(rawEvent.experience_id);
256
+ else if (rawEvent.kind === 'verified_outcome') verified.add(rawEvent.experience_id);
257
+ else errors.push(`${eventPath}.kind must be recalled or verified_outcome`);
258
+ }
259
+ for (const experienceId of verified){
260
+ if (!recalled.has(experienceId)) errors.push(`verified experience ${experienceId} must have a recalled event`);
261
+ }
262
+ const verifiedRecalledCount = [
263
+ ...verified
264
+ ].filter((id)=>recalled.has(id)).length;
265
+ const refluxBaseline = recalled.size > 0 ? round(verifiedRecalledCount / recalled.size) : null;
266
+ const rawAssertions = input.preference_assertions;
267
+ if (!Array.isArray(rawAssertions)) errors.push('preference_assertions must be an array');
268
+ let traceableCount = 0;
269
+ let hardFilterViolationCount = 0;
270
+ const assertions = Array.isArray(rawAssertions) ? rawAssertions : [];
271
+ for (const [index, rawAssertion] of assertions.entries()){
272
+ const assertionPath = `preference_assertions[${index}]`;
273
+ if (!isObject(rawAssertion)) {
274
+ errors.push(`${assertionPath} must be an object`);
275
+ continue;
276
+ }
277
+ if (!nonEmptyString(rawAssertion.assertion_id)) errors.push(`${assertionPath}.assertion_id must be non-empty`);
278
+ if (nonEmptyString(rawAssertion.evidence_ref)) traceableCount += 1;
279
+ if (rawAssertion.hard_filter === true || rawAssertion.consumer === 'hard_filter') hardFilterViolationCount += 1;
280
+ }
281
+ const traceableRatio = assertions.length > 0 ? round(traceableCount / assertions.length) : 0;
282
+ const assertionContractMet = assertions.length > 0 && traceableRatio === 1 && hardFilterViolationCount === 0;
283
+ const rawP4 = input.p4;
284
+ let p4State = 'unknown';
285
+ let p4TriggerObserved = false;
286
+ if (!isObject(rawP4) || !isObject(rawP4.triggers)) {
287
+ errors.push('p4.state and p4.triggers must be declared');
288
+ } else {
289
+ if (nonEmptyString(rawP4.state)) p4State = rawP4.state;
290
+ else errors.push('p4.state must be non-empty');
291
+ p4TriggerObserved = rawP4.triggers.real_usage === true || rawP4.triggers.multi_user === true;
292
+ }
293
+ const p4ContractMet = p4TriggerObserved || p4State === 'closed';
294
+ if (errors.length > 0) return invalidReport(errors, schema, evidenceKind);
295
+ const medianReduction = nearestRank(reductions, 0.5);
296
+ const numericMinimumPairCount = minimumPairCount;
297
+ const numericTargetReduction = targetReduction;
298
+ const sampleSizeMet = firstDurations.length >= numericMinimumPairCount;
299
+ const targetMet = medianReduction >= numericTargetReduction;
300
+ const exitEvidenceEligible = evidenceKind === 'observed_private';
301
+ const baselineAvailable = refluxBaseline !== null;
302
+ const exitReady = exitEvidenceEligible && sampleSizeMet && targetMet && baselineAvailable && assertionContractMet && p4ContractMet;
303
+ return {
304
+ schema: 'memory_value_report.v1',
305
+ contract_valid: true,
306
+ errors: [],
307
+ source: {
308
+ fixture_schema: schema,
309
+ evidence_kind: evidenceKind,
310
+ quantile_method: 'nearest_rank',
311
+ active_duration_rule: 'wall_clock_minus_non_overlapping_predeclared_external_waits'
312
+ },
313
+ cohort: {
314
+ eligible_pair_count: firstDurations.length,
315
+ first_active_seconds: {
316
+ p50: nearestRank(firstDurations, 0.5),
317
+ p75: nearestRank(firstDurations, 0.75)
318
+ },
319
+ returning_active_seconds: {
320
+ p50: nearestRank(returningDurations, 0.5),
321
+ p75: nearestRank(returningDurations, 0.75)
322
+ },
323
+ paired_reduction_ratio: {
324
+ p50: medianReduction,
325
+ p75: nearestRank(reductions, 0.75)
326
+ },
327
+ target_median_reduction_ratio: numericTargetReduction,
328
+ target_met: targetMet,
329
+ minimum_pair_count_for_exit: numericMinimumPairCount,
330
+ sample_size_met: sampleSizeMet
331
+ },
332
+ experience_reflux: {
333
+ recalled_experience_count: recalled.size,
334
+ verified_experience_count: verifiedRecalledCount,
335
+ baseline: refluxBaseline,
336
+ baseline_available: baselineAvailable,
337
+ real_evidence: exitEvidenceEligible
338
+ },
339
+ preference_assertions: {
340
+ total_count: assertions.length,
341
+ traceable_count: traceableCount,
342
+ traceable_ratio: traceableRatio,
343
+ hard_filter_violation_count: hardFilterViolationCount,
344
+ contract_met: assertionContractMet
345
+ },
346
+ p4: {
347
+ state: p4State,
348
+ trigger_observed: p4TriggerObserved,
349
+ contract_met: p4ContractMet
350
+ },
351
+ exit_evidence_eligible: exitEvidenceEligible,
352
+ exit_ready: exitReady
353
+ };
354
+ }
355
+ function main() {
356
+ const inputPath = process.argv[2];
357
+ if (!inputPath) {
358
+ console.error('usage: npx tsx scripts/memory-value-report.ts <fixture-or-private-manifest.json>');
359
+ process.exitCode = 2;
360
+ return;
361
+ }
362
+ let input;
363
+ try {
364
+ input = JSON.parse(readFileSync(resolve(inputPath), 'utf8'));
365
+ } catch (error) {
366
+ const message = error instanceof Error ? error.message : String(error);
367
+ console.error(`cannot read memory value input: ${message}`);
368
+ process.exitCode = 2;
369
+ return;
370
+ }
371
+ const report = scoreMemoryValue(input);
372
+ console.log(JSON.stringify(report, null, 2));
373
+ if (!report.contract_valid) process.exitCode = 2;
374
+ }
375
+ const invokedPath = process.argv[1] ? pathToFileURL(resolve(process.argv[1])).href : '';
376
+ if (import.meta.url === invokedPath) main();
377
+
378
+
379
+ //# sourceURL=/Users/bytedance/work/gotry/ts/scripts/memory-value-report.ts