@g9tm/app-diagnostic-lynx 0.4.0 → 0.5.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.
@@ -30,4 +30,4 @@ with the command that proved it and the date. Nothing here is claimed from a Nod
30
30
  | What | Why it matters | How it will be proven |
31
31
  | --- | --- | --- |
32
32
  | **N-AD-01b** The same on a phone (the gesture with real fingers, the microphone with a real voice) | An emulator injects touches into the decor view and records silence | A Didban session on a lab phone (P7.7) |
33
- | **N-AD-02** iOS (`ios/src`) | Not written yet in this package: the app's `AGMedia.mm` / `AGFiles.mm` move here with neutral names | A build on the Mac through a Didban session (the Mac is reserved for Didban) |
33
+ | **N-AD-02** iOS (`ios/src`) | Written and moved here on 2026-11-20 (0.5.0) from the first consumer's own iOS media and file code, with neutral names (`G9AppDiagnosticModule`, `G9AppDiagnosticHost`, `G9DiagnosticRecorders`); the same files serve the Capacitor and Tauri plugins. Not built or run yet | A build of the host and of `samples/tauri` on the Mac, then a tester report from an iPhone, through a Didban session (the Mac is reserved for Didban) |
package/README.md CHANGED
@@ -27,6 +27,27 @@ G9AppDiagnosticHost.isExcludedView = { it.javaClass.simpleName == "MapHost" }
27
27
 
28
28
  The host's activity needs no edit: the gesture wraps the activity window's callback itself.
29
29
 
30
+ ## iOS (0.5.0)
31
+
32
+ `pod 'G9AppDiagnosticLynx', :path => '../node_modules/@g9tm/app-diagnostic-lynx/ios'`, then register the module with the
33
+ Lynx config and set the hooks before the first call:
34
+
35
+ ```objc
36
+ #import "G9AppDiagnosticModule.h"
37
+ G9AppDiagnosticRegister(config); // the LynxConfig your host builds
38
+ G9AppDiagnosticHost.allowedRoots = ^{ return @[NSHomeDirectory()]; };
39
+ G9AppDiagnosticHost.isExcludedView = ^BOOL(UIView *v) { return [v isKindOfClass:MapHostView.class]; };
40
+ G9AppDiagnosticHost.urlSession = ^{ return mySession; }; // its trust rules for the range PUT
41
+ ```
42
+
43
+ The same methods as Android; iOS has no public way to inject a finger, so a remote `touch` is a logical tap (a control's
44
+ action or an accessibility activation).
45
+
46
+ ## A default panel
47
+
48
+ `import { TesterPanel } from '@g9tm/app-diagnostic-lynx/ui'` (`@lynx-js/react`, an optional peer): the pill, the report form
49
+ and the report list over the engine. An app with a look of its own keeps drawing its own over the engine's state.
50
+
30
51
  ## JS
31
52
 
32
53
  ```ts
@@ -6,10 +6,9 @@ import android.content.Context
6
6
  import android.content.ContextWrapper
7
7
  import android.os.Bundle
8
8
  import android.view.View
9
- import com.lynx.react.bridge.JavaOnlyArray
10
- import com.lynx.tasm.behavior.LynxContext
11
9
  import java.io.File
12
10
  import java.lang.ref.WeakReference
11
+ import java.util.concurrent.CopyOnWriteArrayList
13
12
  import kotlinx.coroutines.Dispatchers
14
13
  import kotlinx.coroutines.delay
15
14
  import kotlinx.coroutines.withContext
@@ -28,7 +27,8 @@ class G9DiagnosticException(val code: String, message: String, cause: Throwable?
28
27
  * - [isExcludedView]: a surface the gesture leaves native in the «except the map» mode (the host's map view). Default: none.
29
28
  *
30
29
  * The current activity is tracked here (the recorders draw it, the gesture watches its window); the module's events go to
31
- * every live Lynx context.
30
+ * every sink a bridge added — Lynx's contexts (LynxBridge.kt), a Capacitor plugin's listeners (0.5.0). This file and the
31
+ * recorders it serves know no bridge: @g9tm/app-diagnostic-capacitor compiles them as they are.
32
32
  */
33
33
  object G9AppDiagnosticHost {
34
34
  @Volatile var httpClient: () -> OkHttpClient = { defaultClient }
@@ -41,17 +41,18 @@ object G9AppDiagnosticHost {
41
41
 
42
42
  @Volatile private var application: Application? = null
43
43
  @Volatile private var current: WeakReference<Activity>? = null
44
- private val contexts = ArrayList<WeakReference<LynxContext>>()
44
+ private val sinks = CopyOnWriteArrayList<(String, String) -> Unit>()
45
45
 
46
- /** Called by every module instance: the application, its activity and its Lynx context. Idempotent. */
47
- internal fun attach(lynx: LynxContext) {
48
- synchronized(contexts) {
49
- contexts.removeAll { it.get() == null || it.get() === lynx }
50
- contexts += WeakReference(lynx)
51
- }
52
- activityOf(lynx)?.let { if (!it.isFinishing) current = WeakReference(it) }
46
+ /** Where the events go (a bridge's own way to the JS side). Returns how to take it away again. */
47
+ fun addSink(sink: (name: String, json: String) -> Unit): () -> Unit {
48
+ sinks += sink
49
+ return { sinks -= sink }
50
+ }
51
+
52
+ /** Called by every bridge's module: the application and the activity it lives in. Idempotent. */
53
+ fun attach(app: Application, activity: Activity?) {
54
+ activity?.let { if (!it.isFinishing) current = WeakReference(it) }
53
55
  if (application != null) return
54
- val app = lynx.applicationContext as? Application ?: return
55
56
  synchronized(this) {
56
57
  if (application != null) return
57
58
  application = app
@@ -102,26 +103,25 @@ object G9AppDiagnosticHost {
102
103
  return file
103
104
  }
104
105
 
105
- /** A global event to every live Lynx context (JSON text, the one argument). */
106
+ /** An event to every sink (JSON text): a sink's own failure never stops the next. */
106
107
  internal fun emit(name: String, json: String) {
107
- val targets = synchronized(contexts) { contexts.mapNotNull { it.get() } }
108
- for (lynx in targets) {
108
+ for (sink in sinks) {
109
109
  try {
110
- lynx.sendGlobalEvent(name, JavaOnlyArray().apply { pushString(json) })
110
+ sink(name, json)
111
111
  } catch (_: Throwable) {
112
- // A context being torn down: the next one still gets it.
112
+ // A bridge being torn down: the next one still gets it.
113
113
  }
114
114
  }
115
115
  }
116
116
 
117
117
  internal suspend fun <T> onMain(work: () -> T): T = withContext(Dispatchers.Main) { work() }
118
118
 
119
- private fun activityOf(context: Context?): Activity? {
119
+ /** The activity a context wraps (a LynxContext, a themed wrapper), or null. */
120
+ fun activityOf(context: Context?): Activity? {
120
121
  var c = context
121
122
  repeat(10) {
122
123
  when (c) {
123
124
  is Activity -> return c as Activity
124
- is LynxContext -> c = (c as LynxContext).baseContext
125
125
  is ContextWrapper -> c = (c as ContextWrapper).baseContext
126
126
  else -> return null
127
127
  }
@@ -43,7 +43,7 @@ class G9AppDiagnosticModule(private val lynx: LynxContext) : G9AppDiagnosticModu
43
43
  private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
44
44
 
45
45
  init {
46
- G9AppDiagnosticHost.attach(lynx)
46
+ LynxBridge.attach(lynx)
47
47
  }
48
48
 
49
49
  private fun respond(callback: Callback?, work: suspend () -> Any?) {
@@ -200,7 +200,7 @@ class G9AppDiagnosticModule(private val lynx: LynxContext) : G9AppDiagnosticModu
200
200
  }
201
201
 
202
202
  companion object {
203
- const val VERSION = "0.1.0"
203
+ const val VERSION = "0.5.0"
204
204
  private val nextKey = AtomicInteger()
205
205
 
206
206
  internal fun toMap(value: Map<*, *>): JavaOnlyMap {
@@ -0,0 +1,41 @@
1
+ package com.g9tm.appdiagnostic
2
+
3
+ import android.app.Application
4
+ import com.lynx.react.bridge.JavaOnlyArray
5
+ import com.lynx.tasm.behavior.LynxContext
6
+ import java.lang.ref.WeakReference
7
+
8
+ /**
9
+ * The Lynx side of G9AppDiagnosticHost (0.5.0 — split out so the recorders compile without Lynx): every module instance
10
+ * attaches its LynxContext here, and the host's events reach every live context as a global event (JSON text, the one
11
+ * argument).
12
+ */
13
+ internal object LynxBridge {
14
+ private val contexts = ArrayList<WeakReference<LynxContext>>()
15
+ @Volatile private var sinkAdded = false
16
+
17
+ fun attach(lynx: LynxContext) {
18
+ synchronized(contexts) {
19
+ contexts.removeAll { it.get() == null || it.get() === lynx }
20
+ contexts += WeakReference(lynx)
21
+ }
22
+ (lynx.applicationContext as? Application)?.let { G9AppDiagnosticHost.attach(it, G9AppDiagnosticHost.activityOf(lynx)) }
23
+ if (sinkAdded) return
24
+ synchronized(this) {
25
+ if (sinkAdded) return
26
+ sinkAdded = true
27
+ G9AppDiagnosticHost.addSink { name, json -> emit(name, json) }
28
+ }
29
+ }
30
+
31
+ private fun emit(name: String, json: String) {
32
+ val targets = synchronized(contexts) { contexts.mapNotNull { it.get() } }
33
+ for (lynx in targets) {
34
+ try {
35
+ lynx.sendGlobalEvent(name, JavaOnlyArray().apply { pushString(json) })
36
+ } catch (_: Throwable) {
37
+ // A context being torn down: the next one still gets it.
38
+ }
39
+ }
40
+ }
41
+ }
@@ -0,0 +1,23 @@
1
+ require 'json'
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, '..', 'package.json')))
4
+
5
+ # The iOS half of @g9tm/app-diagnostic-lynx (G9AppDiagnosticModule): the app's own window as an MP4, a spoken note, the
6
+ # window screenshot, a file range sent natively, the tester gesture and the remote touch. Register it with the app's
7
+ # LynxConfig: G9AppDiagnosticRegister(config). The host's hooks: G9AppDiagnosticHost (urlSession, allowedRoots,
8
+ # isExcludedView). Needs NSMicrophoneUsageDescription in the app's Info.plist for the voice note.
9
+ Pod::Spec.new do |s|
10
+ s.name = 'G9AppDiagnosticLynx'
11
+ s.version = package['version']
12
+ s.summary = 'The iOS half of @g9tm/app-diagnostic-lynx: tester-report recorders and the live channel for a Lynx app.'
13
+ s.homepage = 'https://www.npmjs.com/package/@g9tm/app-diagnostic-lynx'
14
+ s.license = { :type => 'MIT' }
15
+ s.author = 'G9TM'
16
+ s.platform = :ios, '14.0'
17
+ s.source = { :path => '.' }
18
+ s.source_files = 'src/**/*.{h,m}'
19
+ s.public_header_files = 'src/G9AppDiagnosticModule.h', 'src/G9AppDiagnosticHost.h'
20
+ s.frameworks = 'UIKit', 'AVFoundation', 'CoreVideo', 'CoreMedia'
21
+ s.requires_arc = true
22
+ s.dependency 'Lynx'
23
+ end
@@ -0,0 +1,32 @@
1
+ // The host's hooks for G9AppDiagnosticModule on iOS (TWIN: android/…/G9AppDiagnosticHost.kt). Set them before the first
2
+ // call, typically in application:didFinishLaunchingWithOptions:. Every hook has a default; none names a product.
3
+ //
4
+ // G9AppDiagnosticHost.urlSession = ^{ return myTrustedSession; }; // its trust rules for the range PUT
5
+ // G9AppDiagnosticHost.allowedRoots = ^{ return @[ supportDir, cachesDir ]; }; // the folders a path may be inside
6
+ // G9AppDiagnosticHost.isExcludedView = ^BOOL(UIView *v) { return [v isKindOfClass:MyMapHost.class]; }; // «except the map»
7
+ #import <Foundation/Foundation.h>
8
+ #import <UIKit/UIKit.h>
9
+
10
+ NS_ASSUME_NONNULL_BEGIN
11
+
12
+ @interface G9AppDiagnosticHost : NSObject
13
+ /** The session sendRange uses (the host's trust, proxy and certificates). Default: NSURLSession.sharedSession. */
14
+ @property(class, nonatomic, copy, nullable) NSURLSession *_Nonnull (^urlSession)(void);
15
+ /** The folders a recording, a screenshot or a sent file may live in. Default: Application Support, Caches, tmp. */
16
+ @property(class, nonatomic, copy, nullable) NSArray<NSString *> *_Nonnull (^allowedRoots)(void);
17
+ /** A view the tester gesture never holds a touch over (a map's own pinch and pan). Default: none. */
18
+ @property(class, nonatomic, copy, nullable) BOOL (^isExcludedView)(UIView *view);
19
+
20
+ /** The folders a path may be inside, resolved (symlinks, «..»). */
21
+ + (NSArray<NSString *> *)roots;
22
+ /** The path resolved, when strictly inside one of the roots (or a root itself, for a folder); else nil. */
23
+ + (nullable NSString *)inside:(nullable NSString *)path folder:(BOOL)folder;
24
+ /** The key window of the foreground scene. */
25
+ + (nullable UIWindow *)keyWindow;
26
+ /** The window as the user sees it (Metal and GL layers included), at a scale (1 = points). */
27
+ + (nullable UIImage *)captureWindowAtScale:(CGFloat)scale;
28
+ /** The window's size in points (zero when there is none). */
29
+ + (CGSize)windowSize;
30
+ @end
31
+
32
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,72 @@
1
+ #import "G9AppDiagnosticHost.h"
2
+
3
+ static NSURLSession * (^gSession)(void);
4
+ static NSArray<NSString *> * (^gRoots)(void);
5
+ static BOOL (^gExcluded)(UIView *);
6
+
7
+ @implementation G9AppDiagnosticHost
8
+
9
+ + (NSURLSession * (^)(void))urlSession { return gSession; }
10
+ + (void)setUrlSession:(NSURLSession * (^)(void))b { gSession = [b copy]; }
11
+ + (NSArray<NSString *> * (^)(void))allowedRoots { return gRoots; }
12
+ + (void)setAllowedRoots:(NSArray<NSString *> * (^)(void))b { gRoots = [b copy]; }
13
+ + (BOOL (^)(UIView *))isExcludedView { return gExcluded; }
14
+ + (void)setIsExcludedView:(BOOL (^)(UIView *))b { gExcluded = [b copy]; }
15
+
16
+ + (NSArray<NSString *> *)roots {
17
+ NSArray<NSString *> *raw = gRoots ? gRoots() : @[
18
+ NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES).firstObject ?: NSTemporaryDirectory(),
19
+ NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject ?: NSTemporaryDirectory(),
20
+ NSTemporaryDirectory(),
21
+ ];
22
+ NSMutableArray<NSString *> *out = [NSMutableArray array];
23
+ for (NSString *r in raw) {
24
+ NSString *p = r.stringByStandardizingPath.stringByResolvingSymlinksInPath;
25
+ while (p.length > 1 && [p hasSuffix:@"/"]) p = [p substringToIndex:p.length - 1];
26
+ if (p.length) [out addObject:p];
27
+ }
28
+ return out;
29
+ }
30
+
31
+ + (NSString *)inside:(NSString *)path folder:(BOOL)folder {
32
+ if (path.length == 0) return nil;
33
+ NSString *p = path.stringByStandardizingPath.stringByResolvingSymlinksInPath;
34
+ while (p.length > 1 && [p hasSuffix:@"/"]) p = [p substringToIndex:p.length - 1];
35
+ for (NSString *root in [self roots]) {
36
+ if ([p hasPrefix:[root stringByAppendingString:@"/"]]) return p; // strictly inside: never «/rootX» for «/root»
37
+ if (folder && [p isEqualToString:root]) return p;
38
+ }
39
+ return nil;
40
+ }
41
+
42
+ + (UIWindow *)keyWindow {
43
+ UIWindow *any = nil;
44
+ for (UIScene *scene in UIApplication.sharedApplication.connectedScenes) {
45
+ if (![scene isKindOfClass:UIWindowScene.class]) continue;
46
+ for (UIWindow *w in ((UIWindowScene *)scene).windows) {
47
+ if (w.isKeyWindow) return w;
48
+ if (!any && !w.hidden) any = w;
49
+ }
50
+ }
51
+ return any;
52
+ }
53
+
54
+ + (CGSize)windowSize {
55
+ UIWindow *w = [self keyWindow];
56
+ return w ? w.bounds.size : CGSizeZero;
57
+ }
58
+
59
+ + (UIImage *)captureWindowAtScale:(CGFloat)scale {
60
+ UIWindow *window = [self keyWindow];
61
+ if (window == nil || CGRectIsEmpty(window.bounds)) return nil;
62
+ UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat preferredFormat];
63
+ format.scale = scale;
64
+ format.opaque = YES;
65
+ // drawViewHierarchyInRect snapshots the composited screen, Metal layers included (a MapLibre map) — what PixelCopy
66
+ // and the SurfaceView composite give on Android.
67
+ return [[[UIGraphicsImageRenderer alloc] initWithSize:window.bounds.size format:format] imageWithActions:^(UIGraphicsImageRendererContext *ctx) {
68
+ [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:NO];
69
+ }];
70
+ }
71
+
72
+ @end
@@ -0,0 +1,23 @@
1
+ // G9AppDiagnosticModule on iOS (types/g9-app-diagnostic-module.d.ts; TWIN: android/…/G9AppDiagnosticModule.kt): the
2
+ // native half of the tester mode and the live channel — the app's own window as an MP4, a spoken note, the window
3
+ // screenshot, a file range sent natively, the tester gesture, and the operator's remote touch.
4
+ // Every reply is {ok: true, value} | {ok: false, error: {code, message}}; work runs off the JS thread.
5
+ //
6
+ // Register it once with the app's LynxConfig (before the first LynxView):
7
+ // #import <G9AppDiagnosticLynx/G9AppDiagnosticModule.h>
8
+ // G9AppDiagnosticRegister(config);
9
+ #import <Foundation/Foundation.h>
10
+ #import <Lynx/LynxContextModule.h>
11
+ #import <Lynx/LynxModule.h>
12
+
13
+ @class LynxConfig;
14
+
15
+ NS_ASSUME_NONNULL_BEGIN
16
+
17
+ @interface G9AppDiagnosticModule : NSObject <LynxContextModule>
18
+ @end
19
+
20
+ /** Registers G9AppDiagnosticModule with a LynxConfig. */
21
+ FOUNDATION_EXPORT void G9AppDiagnosticRegister(LynxConfig *config);
22
+
23
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,238 @@
1
+ #import "G9AppDiagnosticModule.h"
2
+
3
+ #import <AVFoundation/AVFoundation.h>
4
+ #import <Lynx/LynxConfig.h>
5
+ #import <Lynx/LynxContext.h>
6
+ #import <UIKit/UIKit.h>
7
+
8
+ #import "G9AppDiagnosticHost.h"
9
+ #import "G9DiagnosticRecorders.h"
10
+
11
+ static NSString *const kVersion = @"0.5.0";
12
+ static NSString *const kTouchEvent = @"g9:diagnostic-touch";
13
+
14
+ void G9AppDiagnosticRegister(LynxConfig *config) {
15
+ [config registerModule:G9AppDiagnosticModule.class];
16
+ }
17
+
18
+ /** The live LynxContexts, for the touch event (weak: a torn-down view simply drops out). */
19
+ static NSHashTable<LynxContext *> *G9Contexts(void) {
20
+ static NSHashTable *t;
21
+ static dispatch_once_t once;
22
+ dispatch_once(&once, ^{ t = [NSHashTable weakObjectsHashTable]; });
23
+ return t;
24
+ }
25
+
26
+ static void G9Emit(NSString *json) {
27
+ NSArray<LynxContext *> *live;
28
+ @synchronized(G9Contexts()) { live = G9Contexts().allObjects; }
29
+ for (LynxContext *c in live) {
30
+ @try { [c sendGlobalEvent:kTouchEvent withParams:@[ json ?: @"{}" ]]; } @catch (NSException *ex) { /* a context going away */ }
31
+ }
32
+ }
33
+
34
+ typedef id _Nullable (^G9Work)(NSError **error);
35
+
36
+ @implementation G9AppDiagnosticModule {
37
+ __weak LynxContext *_context;
38
+ }
39
+
40
+ + (NSString *)name { return @"G9AppDiagnosticModule"; }
41
+
42
+ + (NSDictionary<NSString *, NSString *> *)methodLookup {
43
+ return @{
44
+ @"capabilities" : NSStringFromSelector(@selector(capabilities:)),
45
+ @"state" : NSStringFromSelector(@selector(state:)),
46
+ @"startScreen" : NSStringFromSelector(@selector(startScreen:maxFps:callback:)),
47
+ @"stopScreen" : NSStringFromSelector(@selector(stopScreen:)),
48
+ @"startVoice" : NSStringFromSelector(@selector(startVoice:callback:)),
49
+ @"stopVoice" : NSStringFromSelector(@selector(stopVoice:)),
50
+ @"watchTouches" : NSStringFromSelector(@selector(watchTouches:touches:consume:excludeSurfaces:callback:)),
51
+ @"screenshot" : NSStringFromSelector(@selector(screenshot:maxEdge:quality:callback:)),
52
+ @"sendRange" : NSStringFromSelector(@selector(sendRange:callback:)),
53
+ @"touch" : NSStringFromSelector(@selector(touch:x:y:toX:toY:durationMs:callback:)),
54
+ };
55
+ }
56
+
57
+ - (instancetype)initWithLynxContext:(LynxContext *)context {
58
+ if ((self = [super init])) {
59
+ _context = context;
60
+ if (context) @synchronized(G9Contexts()) { [G9Contexts() addObject:context]; }
61
+ }
62
+ return self;
63
+ }
64
+
65
+ - (instancetype)init { return [super init]; }
66
+
67
+ - (void)destroy {
68
+ LynxContext *c = _context;
69
+ if (c) @synchronized(G9Contexts()) { [G9Contexts() removeObject:c]; }
70
+ }
71
+
72
+ /** The envelope: the same shape the Android module answers (a map; the JS face reads both a map and its JSON text). */
73
+ static NSDictionary *G9Reply(id value, NSError *error) {
74
+ if (error) {
75
+ NSString *code = [error isKindOfClass:G9DiagnosticError.class] ? ((G9DiagnosticError *)error).diagnosticCode : @"io";
76
+ return @{@"ok" : @NO, @"error" : @{@"code" : code ?: @"io", @"message" : error.localizedDescription ?: code ?: @"failed"}};
77
+ }
78
+ return @{@"ok" : @YES, @"value" : value ?: NSNull.null};
79
+ }
80
+
81
+ static void G9Run(dispatch_queue_t queue, LynxCallbackBlock callback, G9Work work) {
82
+ dispatch_async(queue, ^{
83
+ NSError *error = nil;
84
+ id value = nil;
85
+ @try { value = work(&error); }
86
+ @catch (NSException *ex) { error = [G9DiagnosticError code:@"io" message:[NSString stringWithFormat:@"%@: %@", ex.name, ex.reason]]; }
87
+ if (callback) callback(G9Reply(value, error));
88
+ });
89
+ }
90
+
91
+ static dispatch_queue_t G9Background(void) { return dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0); }
92
+
93
+ /** A folder inside the host's roots, made when missing. */
94
+ static NSString *G9Folder(NSString *dir, NSError **error) {
95
+ NSString *p = [G9AppDiagnosticHost inside:dir folder:YES];
96
+ if (!p) { if (error) *error = [G9DiagnosticError code:@"invalid" message:[@"Folder outside the app's folders: " stringByAppendingString:dir ?: @""]]; return nil; }
97
+ [NSFileManager.defaultManager createDirectoryAtPath:p withIntermediateDirectories:YES attributes:nil error:nil];
98
+ return p;
99
+ }
100
+
101
+ - (void)capabilities:(LynxCallbackBlock)callback {
102
+ G9Run(G9Background(), callback, ^id(NSError **error) {
103
+ return @{@"version" : kVersion, @"platform" : @"ios", @"screen" : @YES, @"voice" : @(AVAudioSession.sharedInstance.isInputAvailable), @"consume" : @YES};
104
+ });
105
+ }
106
+
107
+ - (void)state:(LynxCallbackBlock)callback {
108
+ G9Run(G9Background(), callback, ^id(NSError **error) {
109
+ return @{@"screen" : @(G9ScreenRecorder.shared.recording), @"voice" : @(G9VoiceRecorder.shared.recording)};
110
+ });
111
+ }
112
+
113
+ - (void)startScreen:(NSString *)dir maxFps:(double)maxFps callback:(LynxCallbackBlock)callback {
114
+ G9Run(G9Background(), callback, ^id(NSError **error) {
115
+ NSString *folder = G9Folder(dir, error);
116
+ if (folder) [G9ScreenRecorder.shared startIn:folder maxFps:(int)maxFps error:error];
117
+ return nil;
118
+ });
119
+ }
120
+
121
+ - (void)stopScreen:(LynxCallbackBlock)callback {
122
+ G9Run(G9Background(), callback, ^id(NSError **error) { return [G9ScreenRecorder.shared stop]; });
123
+ }
124
+
125
+ - (void)startVoice:(NSString *)dir callback:(LynxCallbackBlock)callback {
126
+ G9Run(G9Background(), callback, ^id(NSError **error) {
127
+ NSString *folder = G9Folder(dir, error);
128
+ if (folder) [G9VoiceRecorder.shared startIn:folder error:error];
129
+ return nil;
130
+ });
131
+ }
132
+
133
+ - (void)stopVoice:(LynxCallbackBlock)callback {
134
+ G9Run(G9Background(), callback, ^id(NSError **error) { return [G9VoiceRecorder.shared stop]; });
135
+ }
136
+
137
+ - (void)watchTouches:(BOOL)taps touches:(BOOL)touches consume:(BOOL)consume excludeSurfaces:(BOOL)exclude callback:(LynxCallbackBlock)callback {
138
+ G9Run(dispatch_get_main_queue(), callback, ^id(NSError **error) {
139
+ [G9TouchWatcher watchTaps:taps touches:touches consume:consume excludeSurfaces:exclude sink:^(NSString *json) { G9Emit(json); }];
140
+ return nil;
141
+ });
142
+ }
143
+
144
+ /** The window as the user sees it (Metal layers included), JPEG into `dir`: {path, width, height, bytes}. */
145
+ - (void)screenshot:(NSString *)dir maxEdge:(double)maxEdge quality:(double)quality callback:(LynxCallbackBlock)callback {
146
+ G9Run(dispatch_get_main_queue(), callback, ^id(NSError **error) {
147
+ NSString *folder = G9Folder(dir, error);
148
+ if (!folder) return nil;
149
+ UIWindow *window = G9AppDiagnosticHost.keyWindow;
150
+ if (window == nil || CGRectIsEmpty(window.bounds)) { if (error) *error = [G9DiagnosticError code:@"unavailable" message:@"No window to capture"]; return nil; }
151
+ CGSize size = window.bounds.size;
152
+ CGFloat edge = MIN(MAX(maxEdge, 64.0), 4096.0);
153
+ CGFloat scale = MIN(window.screen.scale, edge / MAX(size.width, size.height));
154
+ UIImage *image = [G9AppDiagnosticHost captureWindowAtScale:scale];
155
+ NSData *jpeg = image ? UIImageJPEGRepresentation(image, MIN(MAX(quality, 10.0), 100.0) / 100.0) : nil;
156
+ NSString *path = [folder stringByAppendingPathComponent:[NSString stringWithFormat:@"shot-%lld.jpg", (long long)(NSDate.date.timeIntervalSince1970 * 1000)]];
157
+ if (!jpeg || ![jpeg writeToFile:path atomically:YES]) { if (error) *error = [G9DiagnosticError code:@"io" message:@"The screenshot could not be written"]; return nil; }
158
+ return @{@"path" : path, @"width" : @((int)lround(size.width * scale)), @"height" : @((int)lround(size.height * scale)), @"bytes" : @(jpeg.length)};
159
+ });
160
+ }
161
+
162
+ /**
163
+ * One HTTP request whose body is a byte range of a file (the tester report's resumable PUT), read here so a chunk never
164
+ * crosses the bridge as base64; over the host's session (its trust). Any HTTP status resolves; only a transport failure
165
+ * rejects (network | timeout).
166
+ */
167
+ - (void)sendRange:(NSString *)requestJson callback:(LynxCallbackBlock)callback {
168
+ G9Run(G9Background(), callback, ^id(NSError **error) {
169
+ NSDictionary *r = requestJson.length ? [NSJSONSerialization JSONObjectWithData:[requestJson dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil] : nil;
170
+ if (![r isKindOfClass:NSDictionary.class] || ![r[@"url"] isKindOfClass:NSString.class] || ![r[@"path"] isKindOfClass:NSString.class]) {
171
+ if (error) *error = [G9DiagnosticError code:@"invalid" message:@"sendRange needs {url, path}"];
172
+ return nil;
173
+ }
174
+ NSString *p = [G9AppDiagnosticHost inside:r[@"path"] folder:NO];
175
+ if (!p) { if (error) *error = [G9DiagnosticError code:@"invalid" message:[@"Path outside the app's folders: " stringByAppendingString:r[@"path"]]]; return nil; }
176
+ NSFileHandle *h = [NSFileHandle fileHandleForReadingAtPath:p];
177
+ if (!h) { if (error) *error = [G9DiagnosticError code:@"not-found" message:[@"No such file: " stringByAppendingString:p]]; return nil; }
178
+ unsigned long long size = [[NSFileManager.defaultManager attributesOfItemAtPath:p error:nil] fileSize];
179
+ unsigned long long offset = MIN((unsigned long long)MAX([r[@"offset"] doubleValue], 0.0), size);
180
+ unsigned long long length = r[@"length"] ? (unsigned long long)MAX([r[@"length"] doubleValue], 0.0) : size - offset;
181
+ length = MIN(length, size - offset);
182
+ [h seekToFileOffset:offset];
183
+ NSData *body = [h readDataOfLength:(NSUInteger)length];
184
+ [h closeFile];
185
+ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:r[@"url"]]];
186
+ request.HTTPMethod = [r[@"method"] isKindOfClass:NSString.class] ? r[@"method"] : @"PUT";
187
+ double timeoutMs = [r[@"timeoutMs"] doubleValue] > 0 ? [r[@"timeoutMs"] doubleValue] : 180000.0;
188
+ request.timeoutInterval = MIN(MAX(timeoutMs, 1000.0), 900000.0) / 1000.0;
189
+ NSString *type = [r[@"contentType"] isKindOfClass:NSString.class] && [r[@"contentType"] length] ? r[@"contentType"] : @"application/octet-stream";
190
+ [request setValue:type forHTTPHeaderField:@"Content-Type"];
191
+ NSDictionary *headers = [r[@"headers"] isKindOfClass:NSDictionary.class] ? r[@"headers"] : @{};
192
+ [headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) { [request setValue:[value description] forHTTPHeaderField:key]; }];
193
+ NSURLSession *session = G9AppDiagnosticHost.urlSession ? G9AppDiagnosticHost.urlSession() : NSURLSession.sharedSession;
194
+ dispatch_semaphore_t done = dispatch_semaphore_create(0);
195
+ __block NSData *answer = nil;
196
+ __block NSHTTPURLResponse *response = nil;
197
+ __block NSError *failure = nil;
198
+ [[session uploadTaskWithRequest:request fromData:body completionHandler:^(NSData *data, NSURLResponse *res, NSError *err) {
199
+ answer = data;
200
+ response = [res isKindOfClass:NSHTTPURLResponse.class] ? (NSHTTPURLResponse *)res : nil;
201
+ failure = err;
202
+ dispatch_semaphore_signal(done);
203
+ }] resume];
204
+ dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER);
205
+ if (failure || !response) {
206
+ if (error) *error = [G9DiagnosticError code:failure.code == NSURLErrorTimedOut ? @"timeout" : @"network" message:failure.localizedDescription ?: @"No response"];
207
+ return nil;
208
+ }
209
+ NSMutableDictionary *out = [NSMutableDictionary dictionary];
210
+ [response.allHeaderFields enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) { out[[[key description] lowercaseString]] = [value description]; }];
211
+ return @{@"status" : @(response.statusCode), @"body" : answer ? ([[NSString alloc] initWithData:answer encoding:NSUTF8StringEncoding] ?: @"") : @"", @"headers" : out};
212
+ });
213
+ }
214
+
215
+ /**
216
+ * The operator's remote touch (E-13). iOS has no public way to inject a finger, so this is the store-safe LOGICAL touch
217
+ * (.agents/skills/g9-app-diagnostic/references/remote-control.md): the view under the point is hit-tested and, for a tap, a
218
+ * control's touch-up-inside action or the element's accessibility activation runs. A long press or a drag is refused
219
+ * (`handled: false`) — the console says so. Points from the window's top-left.
220
+ */
221
+ - (void)touch:(NSString *)kind x:(double)x y:(double)y toX:(double)toX toY:(double)toY durationMs:(double)durationMs callback:(LynxCallbackBlock)callback {
222
+ G9Run(dispatch_get_main_queue(), callback, ^id(NSError **error) {
223
+ UIWindow *window = G9AppDiagnosticHost.keyWindow;
224
+ if (!window) { if (error) *error = [G9DiagnosticError code:@"unavailable" message:@"No window"]; return nil; }
225
+ if (![kind isEqualToString:@"tap"]) return @{@"handled" : @NO, @"reason" : @"ios-logical-tap-only"};
226
+ UIView *hit = [window hitTest:CGPointMake(x, y) withEvent:nil];
227
+ for (UIView *v = hit; v; v = v.superview) {
228
+ if ([v isKindOfClass:UIControl.class] && ((UIControl *)v).enabled) {
229
+ [(UIControl *)v sendActionsForControlEvents:UIControlEventTouchUpInside];
230
+ return @{@"handled" : @YES};
231
+ }
232
+ if (v.isAccessibilityElement && [v accessibilityActivate]) return @{@"handled" : @YES};
233
+ }
234
+ return @{@"handled" : @NO};
235
+ });
236
+ }
237
+
238
+ @end
@@ -0,0 +1,36 @@
1
+ // The iOS recorders and the tester gesture of G9AppDiagnosticModule (TWIN: android/…/ScreenRecorder.kt, VoiceRecorder.kt,
2
+ // TouchWatcher.kt + core/TouchCore.kt). Moved here from the first Lynx app's own ios/Platform/AGMedia.mm (2026-11-20) so
3
+ // every Lynx app gets them; no product name, the excluded surface is the host's (G9AppDiagnosticHost.isExcludedView).
4
+ #import <Foundation/Foundation.h>
5
+ #import <UIKit/UIKit.h>
6
+
7
+ NS_ASSUME_NONNULL_BEGIN
8
+
9
+ /** A failure the JS side reads as DiagnosticNativeError(code, message) — the codes of types/g9-app-diagnostic-module.d.ts. */
10
+ @interface G9DiagnosticError : NSError
11
+ + (instancetype)code:(NSString *)code message:(NSString *)message;
12
+ @property(nonatomic, readonly) NSString *diagnosticCode;
13
+ @end
14
+
15
+ @interface G9ScreenRecorder : NSObject
16
+ @property(nonatomic, readonly) BOOL recording;
17
+ + (instancetype)shared;
18
+ - (BOOL)startIn:(NSString *)dir maxFps:(int)maxFps error:(NSError **)error;
19
+ /** Blocks until the file is finalised (call off the main thread). {path, bytes, durationMs, width, height, capped} or nil. */
20
+ - (nullable NSDictionary *)stop;
21
+ @end
22
+
23
+ @interface G9VoiceRecorder : NSObject
24
+ + (instancetype)shared;
25
+ @property(nonatomic, readonly) BOOL recording;
26
+ - (BOOL)startIn:(NSString *)dir error:(NSError **)error;
27
+ /** {path, bytes, durationMs} or nil. */
28
+ - (nullable NSDictionary *)stop;
29
+ @end
30
+
31
+ /** The tester gesture's watcher on the key window; events go to [sink] as JSON text («g9:diagnostic-touch»). */
32
+ @interface G9TouchWatcher : NSObject
33
+ + (void)watchTaps:(BOOL)taps touches:(BOOL)touches consume:(BOOL)consume excludeSurfaces:(BOOL)exclude sink:(void (^)(NSString *json))sink;
34
+ @end
35
+
36
+ NS_ASSUME_NONNULL_END