@factiii/runner 0.24.0 → 0.43.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.
Files changed (37) hide show
  1. package/DISCLOSURE +174 -0
  2. package/README.md +87 -12
  3. package/dist/cli.js +83814 -40252
  4. package/dist/vault-client.js +876 -0
  5. package/dist/vault-db.js +858 -0
  6. package/embedder/app.py +331 -0
  7. package/embedder/clip_tokenizer.py +137 -0
  8. package/embedder/convert_clip.py +119 -0
  9. package/embedder/convert_onnx.py +129 -0
  10. package/embedder/convert_text.py +148 -0
  11. package/embedder/onnx_backend.py +48 -0
  12. package/embedder/overrides-onnx.txt +3 -0
  13. package/embedder/requirements-onnx.txt +22 -0
  14. package/embedder/requirements.txt +20 -0
  15. package/embedder/serve.py +241 -0
  16. package/embedder/text_tokenizer.py +34 -0
  17. package/native/simhid.m +7 -0
  18. package/native/simstream.m +7 -0
  19. package/package.json +28 -14
  20. package/plugin/factiii/bin/factiii-board-mcp +206 -0
  21. package/plugin/factiii/bin/factiii-secrets +182 -18
  22. package/plugin/factiii/bin/report-state.sh +19 -1
  23. package/plugin/factiii/bin/session-context.sh +76 -0
  24. package/plugin/factiii/hooks/hooks.json +22 -0
  25. package/plugin/factiii/skills/board-sessions/SKILL.md +74 -0
  26. package/plugin/factiii/skills/factiii-secrets/SKILL.md +21 -3
  27. package/plugin/factiii/skills/runner-onboarding/SKILL.md +67 -0
  28. package/plugin/factiii/skills/scratchpad/SKILL.md +1 -0
  29. package/plugin/factiii/skills/share-a-port/SKILL.md +11 -6
  30. package/index/image-package.json +0 -8
  31. package/index/index.js +0 -105341
  32. package/native/screenhost.m +0 -500
  33. package/plugin/factiii/skills/factiii-workspace/SKILL.md +0 -69
  34. package/vendor/frpc-darwin-amd64 +0 -0
  35. package/vendor/frpc-darwin-arm64 +0 -0
  36. package/vendor/frpc-linux-amd64 +0 -0
  37. package/vendor/frpc-linux-arm64 +0 -0
@@ -1,500 +0,0 @@
1
- // The Mac's own screen, for the runner owner's desktop view
2
- // (.specs/desktop-view.md). One program does both halves, because macOS grants
3
- // Screen Recording and Accessibility to a program, and one program is one pair
4
- // of switches for the owner to turn on:
5
- //
6
- // out: each H.264 access unit on stdout as [4-byte BE length][Annex-B
7
- // payload], the framing simstream.m uses, so the runner's video path
8
- // does not care which helper is feeding it;
9
- // in: one JSON command per line on stdin: pointer, scroll, key, text.
10
- //
11
- // It is built into an app bundle (FactiiiScreen.app) rather than run as a bare
12
- // binary. macOS charges a child process's privacy requests to whatever
13
- // launched it, which for a runner is `node` or the terminal it was started
14
- // from. The bundle has its own identifier, and this program gives up its
15
- // parent's responsibility on start (disclaimResponsibility below), so the
16
- // grant belongs to "Factiii Screen" and survives node and runner upgrades.
17
- //
18
- // Public APIs only (ScreenCaptureKit, VideoToolbox, CGEvent). Nothing here
19
- // moves between Xcode versions the way the simulator helpers' frameworks do.
20
- #import <AppKit/AppKit.h>
21
- #import <ApplicationServices/ApplicationServices.h>
22
- #import <CoreMedia/CoreMedia.h>
23
- #import <Foundation/Foundation.h>
24
- #import <ScreenCaptureKit/ScreenCaptureKit.h>
25
- #import <VideoToolbox/VideoToolbox.h>
26
- #import <dlfcn.h>
27
- #import <mach-o/dyld.h>
28
- #import <os/lock.h>
29
- #import <spawn.h>
30
-
31
- extern char **environ;
32
-
33
- static const char *kDisclaimed = "FACTIII_SCREEN_DISCLAIMED";
34
-
35
- // Re-exec as our own "responsible process". Without this the Screen Recording
36
- // and Accessibility prompts name the runner's parent, and a grant made to it
37
- // is a grant to everything that parent ever starts. The call is the one
38
- // browsers and IDEs use for the same reason; it is looked up at run time so a
39
- // macOS without it runs on, charged to the parent as before.
40
- static void disclaimResponsibility(char *const argv[]) {
41
- if (getenv(kDisclaimed)) return;
42
- int (*setdisclaim)(posix_spawnattr_t *, int) =
43
- dlsym(RTLD_DEFAULT, "responsibility_spawnattrs_setdisclaim");
44
- if (!setdisclaim) return;
45
- char path[4096];
46
- uint32_t size = sizeof(path);
47
- if (_NSGetExecutablePath(path, &size) != 0) return;
48
- posix_spawnattr_t attr;
49
- if (posix_spawnattr_init(&attr) != 0) return;
50
- // SETEXEC replaces this process, so stdin and stdout stay the runner's pipes.
51
- if (posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETEXEC) == 0 && setdisclaim(&attr, 1) == 0) {
52
- setenv(kDisclaimed, "1", 1);
53
- posix_spawn(NULL, path, NULL, &attr, argv, environ);
54
- // Only reached when the exec failed: carry on undisclaimed.
55
- unsetenv(kDisclaimed);
56
- }
57
- posix_spawnattr_destroy(&attr);
58
- }
59
-
60
- // ── What the runner is told ──────────────────────────────────────────────
61
- //
62
- // One JSON object per line on stderr, each with a "status". The store reads
63
- // these; anything else on stderr is a log line for a person.
64
- static void say(NSDictionary *status) {
65
- NSData *json = [NSJSONSerialization dataWithJSONObject:status options:0 error:nil];
66
- if (!json) return;
67
- fwrite(json.bytes, 1, json.length, stderr);
68
- fputc('\n', stderr);
69
- }
70
-
71
- static BOOL canCapture(void) { return CGPreflightScreenCaptureAccess(); }
72
- static BOOL canControl(void) { return AXIsProcessTrusted(); }
73
-
74
- static NSDictionary *permissions(void) {
75
- return @{ @"status": @"permissions", @"screen": @(canCapture()), @"control": @(canControl()) };
76
- }
77
-
78
- // ── Encode ───────────────────────────────────────────────────────────────
79
-
80
- static int gFPS = 30, gWidth = 0, gBitrate = 4000000;
81
- static VTCompressionSessionRef gSession = NULL;
82
- static size_t gSourceWidth = 0, gSourceHeight = 0;
83
- static dispatch_queue_t gWrite;
84
- static CVPixelBufferRef gLatest = NULL;
85
- static os_unfair_lock gLock = OS_UNFAIR_LOCK_INIT;
86
-
87
- static void emit(const void *bytes, size_t len) {
88
- dispatch_sync(gWrite, ^{
89
- uint8_t header[4] = { (uint8_t)(len >> 24), (uint8_t)(len >> 16),
90
- (uint8_t)(len >> 8), (uint8_t)len };
91
- fwrite(header, 1, 4, stdout);
92
- fwrite(bytes, 1, len, stdout);
93
- fflush(stdout);
94
- });
95
- }
96
-
97
- // VideoToolbox hands back AVCC (4-byte lengths) with the parameter sets apart;
98
- // the wire wants Annex-B, with them repeated before every keyframe so a viewer
99
- // that joins late can start decoding.
100
- static void appendAnnexB(NSMutableData *out, const uint8_t *nal, size_t len) {
101
- const uint8_t start[4] = { 0, 0, 0, 1 };
102
- [out appendBytes:start length:4];
103
- [out appendBytes:nal length:len];
104
- }
105
-
106
- static void onEncoded(void *ctx, void *src, OSStatus status, VTEncodeInfoFlags flags,
107
- CMSampleBufferRef sample) {
108
- if (status != noErr || !sample || !CMSampleBufferDataIsReady(sample)) return;
109
- NSMutableData *out = [NSMutableData data];
110
- CFArrayRef attachments = CMSampleBufferGetSampleAttachmentsArray(sample, false);
111
- BOOL keyframe = YES;
112
- if (attachments && CFArrayGetCount(attachments)) {
113
- CFDictionaryRef d = CFArrayGetValueAtIndex(attachments, 0);
114
- keyframe = !CFDictionaryContainsKey(d, kCMSampleAttachmentKey_NotSync);
115
- }
116
- if (keyframe) {
117
- CMFormatDescriptionRef fmt = CMSampleBufferGetFormatDescription(sample);
118
- size_t count = 0;
119
- CMVideoFormatDescriptionGetH264ParameterSetAtIndex(fmt, 0, NULL, NULL, &count, NULL);
120
- for (size_t i = 0; i < count; i++) {
121
- const uint8_t *ps = NULL; size_t psLen = 0;
122
- if (CMVideoFormatDescriptionGetH264ParameterSetAtIndex(fmt, i, &ps, &psLen, NULL, NULL) == noErr)
123
- appendAnnexB(out, ps, psLen);
124
- }
125
- }
126
- CMBlockBufferRef block = CMSampleBufferGetDataBuffer(sample);
127
- size_t total = 0; char *data = NULL;
128
- if (CMBlockBufferGetDataPointer(block, 0, NULL, &total, &data) != noErr) return;
129
- size_t offset = 0;
130
- while (offset + 4 <= total) {
131
- uint32_t len = 0;
132
- memcpy(&len, data + offset, 4);
133
- len = CFSwapInt32BigToHost(len);
134
- offset += 4;
135
- if (len == 0 || offset + len > total) break;
136
- appendAnnexB(out, (const uint8_t *)(data + offset), len);
137
- offset += len;
138
- }
139
- emit(out.bytes, out.length);
140
- }
141
-
142
- static void encode(CVPixelBufferRef pixels) {
143
- size_t w = CVPixelBufferGetWidth(pixels), h = CVPixelBufferGetHeight(pixels);
144
- // A session only takes the size it was made for, and a display can change
145
- // under us (a resolution switch, a monitor unplugged).
146
- if (gSession && (w != gSourceWidth || h != gSourceHeight)) {
147
- VTCompressionSessionCompleteFrames(gSession, kCMTimeInvalid);
148
- VTCompressionSessionInvalidate(gSession);
149
- CFRelease(gSession);
150
- gSession = NULL;
151
- }
152
- if (!gSession) {
153
- NSDictionary *props = @{ (id)kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder: @YES };
154
- if (VTCompressionSessionCreate(NULL, (int32_t)w, (int32_t)h, kCMVideoCodecType_H264,
155
- (__bridge CFDictionaryRef)props, NULL, NULL, onEncoded, NULL, &gSession) != noErr) {
156
- say(@{ @"status": @"error", @"message": @"The video encoder could not start." });
157
- return;
158
- }
159
- VTSessionSetProperty(gSession, kVTCompressionPropertyKey_RealTime, kCFBooleanTrue);
160
- // B-frames cost a frame of latency, the wrong trade for a remote control.
161
- VTSessionSetProperty(gSession, kVTCompressionPropertyKey_AllowFrameReordering, kCFBooleanFalse);
162
- VTSessionSetProperty(gSession, kVTCompressionPropertyKey_ProfileLevel, kVTProfileLevel_H264_High_AutoLevel);
163
- // A keyframe a second: nothing answers a viewer's request for one, so the
164
- // interval is a late or lossy viewer's only way back to a picture.
165
- VTSessionSetProperty(gSession, kVTCompressionPropertyKey_MaxKeyFrameInterval, (__bridge CFTypeRef)@(gFPS));
166
- VTSessionSetProperty(gSession, kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration, (__bridge CFTypeRef)@1.0);
167
- VTSessionSetProperty(gSession, kVTCompressionPropertyKey_ExpectedFrameRate, (__bridge CFTypeRef)@(gFPS));
168
- VTSessionSetProperty(gSession, kVTCompressionPropertyKey_AverageBitRate, (__bridge CFTypeRef)@(gBitrate));
169
- VTSessionSetProperty(gSession, kVTCompressionPropertyKey_DataRateLimits,
170
- (__bridge CFArrayRef)@[@(gBitrate / 8 * 1.5), @1.0]);
171
- gSourceWidth = w;
172
- gSourceHeight = h;
173
- say(@{ @"status": @"streaming", @"width": @(w), @"height": @(h) });
174
- }
175
- static int64_t n = 0;
176
- VTCompressionSessionEncodeFrame(gSession, pixels, CMTimeMake(n++, gFPS), kCMTimeInvalid, NULL, NULL, NULL);
177
- }
178
-
179
- // ── Capture ──────────────────────────────────────────────────────────────
180
-
181
- @interface Capture : NSObject <SCStreamOutput, SCStreamDelegate>
182
- @property(nonatomic, strong) SCStream *stream;
183
- @end
184
-
185
- @implementation Capture
186
- - (void)stream:(SCStream *)stream didOutputSampleBuffer:(CMSampleBufferRef)sample ofType:(SCStreamOutputType)type {
187
- if (type != SCStreamOutputTypeScreen) return;
188
- // A sample with no image is the stream saying "nothing changed".
189
- CVPixelBufferRef pixels = CMSampleBufferGetImageBuffer(sample);
190
- if (!pixels) return;
191
- CVPixelBufferRetain(pixels);
192
- os_unfair_lock_lock(&gLock);
193
- CVPixelBufferRef old = gLatest;
194
- gLatest = pixels;
195
- os_unfair_lock_unlock(&gLock);
196
- if (old) CVPixelBufferRelease(old);
197
- }
198
- - (void)stream:(SCStream *)stream didStopWithError:(NSError *)error {
199
- say(@{ @"status": @"error", @"message": error.localizedDescription ?: @"Screen capture stopped." });
200
- exit(1);
201
- }
202
- @end
203
-
204
- static Capture *gCapture;
205
- /** Held for the life of the process: a timer nothing references is released. */
206
- static dispatch_source_t gTick;
207
-
208
- static void startCapture(void) {
209
- [SCShareableContent getShareableContentExcludingDesktopWindows:NO onScreenWindowsOnly:YES
210
- completionHandler:^(SCShareableContent *content, NSError *error) {
211
- if (error || content.displays.count == 0) {
212
- // The one failure with a fix the owner can make: the switch is off.
213
- say(permissions());
214
- say(@{ @"status": @"error",
215
- @"message": error.localizedDescription ?: @"This Mac has no display to show." });
216
- exit(1);
217
- }
218
- SCDisplay *display = content.displays.firstObject;
219
- for (SCDisplay *d in content.displays)
220
- if (d.displayID == CGMainDisplayID()) display = d;
221
-
222
- size_t w = CGDisplayPixelsWide(display.displayID), h = CGDisplayPixelsHigh(display.displayID);
223
- CGDisplayModeRef mode = CGDisplayCopyDisplayMode(display.displayID);
224
- if (mode) {
225
- w = CGDisplayModeGetPixelWidth(mode);
226
- h = CGDisplayModeGetPixelHeight(mode);
227
- CGDisplayModeRelease(mode);
228
- }
229
- // Only ever scaled down, and by the capture itself rather than the encoder.
230
- if (gWidth > 0 && w > (size_t)gWidth) {
231
- h = h * gWidth / w;
232
- w = gWidth;
233
- }
234
- // H.264 chroma is subsampled, so both axes must be even.
235
- w -= w & 1;
236
- h -= h & 1;
237
-
238
- SCStreamConfiguration *config = [SCStreamConfiguration new];
239
- config.width = w;
240
- config.height = h;
241
- config.minimumFrameInterval = CMTimeMake(1, gFPS);
242
- config.pixelFormat = kCVPixelFormatType_32BGRA;
243
- config.showsCursor = YES;
244
- config.queueDepth = 3;
245
-
246
- gCapture = [Capture new];
247
- SCContentFilter *filter = [[SCContentFilter alloc] initWithDisplay:display excludingWindows:@[]];
248
- gCapture.stream = [[SCStream alloc] initWithFilter:filter configuration:config delegate:gCapture];
249
- NSError *addError = nil;
250
- [gCapture.stream addStreamOutput:gCapture type:SCStreamOutputTypeScreen
251
- sampleHandlerQueue:dispatch_queue_create("screen.capture", DISPATCH_QUEUE_SERIAL)
252
- error:&addError];
253
- [gCapture.stream startCaptureWithCompletionHandler:^(NSError *startError) {
254
- if (!startError) return;
255
- say(permissions());
256
- say(@{ @"status": @"error", @"message": startError.localizedDescription ?: @"Screen capture did not start." });
257
- exit(1);
258
- }];
259
- }];
260
-
261
- // The stream only delivers a frame when the screen changes, and a still
262
- // screen would then send no keyframe for a viewer who joins. So the newest
263
- // frame is encoded on our own clock, changed or not; an unchanged frame
264
- // costs a few hundred bytes.
265
- dispatch_source_t tick = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,
266
- dispatch_queue_create("screen.encode", DISPATCH_QUEUE_SERIAL));
267
- dispatch_source_set_timer(tick, dispatch_time(DISPATCH_TIME_NOW, 0),
268
- (uint64_t)(NSEC_PER_SEC / gFPS), NSEC_PER_SEC / 100);
269
- dispatch_source_set_event_handler(tick, ^{
270
- os_unfair_lock_lock(&gLock);
271
- CVPixelBufferRef pixels = gLatest ? CVPixelBufferRetain(gLatest) : NULL;
272
- os_unfair_lock_unlock(&gLock);
273
- if (!pixels) return;
274
- encode(pixels);
275
- CVPixelBufferRelease(pixels);
276
- });
277
- dispatch_resume(tick);
278
- gTick = tick;
279
- }
280
-
281
- // ── Input ────────────────────────────────────────────────────────────────
282
-
283
- // USB HID keyboard usage -> macOS virtual key code. The app sends usages (the
284
- // same ones the simulator panel does), because a browser's key codes name
285
- // physical keys and a usage is exactly that. 0xFF marks "no such key here".
286
- static const uint8_t kNoKey = 0xFF;
287
- static uint8_t keyCodeFor(int usage) {
288
- static const uint8_t letters[26] = { 0, 11, 8, 2, 14, 3, 5, 4, 34, 38, 40, 37, 46,
289
- 45, 31, 35, 12, 15, 1, 17, 32, 9, 13, 7, 16, 6 };
290
- static const uint8_t digits[10] = { 18, 19, 20, 21, 23, 22, 26, 28, 25, 29 };
291
- static const uint8_t fkeys[12] = { 122, 120, 99, 118, 96, 97, 98, 100, 101, 109, 103, 111 };
292
- if (usage >= 4 && usage <= 29) return letters[usage - 4];
293
- if (usage >= 30 && usage <= 39) return digits[usage - 30];
294
- if (usage >= 58 && usage <= 69) return fkeys[usage - 58];
295
- switch (usage) {
296
- case 40: return 36; // return
297
- case 41: return 53; // escape
298
- case 42: return 51; // delete (backspace)
299
- case 43: return 48; // tab
300
- case 44: return 49; // space
301
- case 45: return 27; // -
302
- case 46: return 24; // =
303
- case 47: return 33; // [
304
- case 48: return 30; // ]
305
- case 49: return 42; // backslash
306
- case 51: return 41; // ;
307
- case 52: return 39; // '
308
- case 53: return 50; // `
309
- case 54: return 43; // ,
310
- case 55: return 47; // .
311
- case 56: return 44; // /
312
- case 57: return 57; // caps lock
313
- case 74: return 115; // home
314
- case 75: return 116; // page up
315
- case 76: return 117; // forward delete
316
- case 77: return 119; // end
317
- case 78: return 121; // page down
318
- case 79: return 124; // right
319
- case 80: return 123; // left
320
- case 81: return 125; // down
321
- case 82: return 126; // up
322
- case 224: return 59; // left control
323
- case 225: return 56; // left shift
324
- case 226: return 58; // left option
325
- case 227: return 55; // left command
326
- case 228: return 62; // right control
327
- case 229: return 60; // right shift
328
- case 230: return 61; // right option
329
- case 231: return 54; // right command
330
- default: return kNoKey;
331
- }
332
- }
333
-
334
- static CGEventFlags flagFor(int usage) {
335
- switch (usage) {
336
- case 224: case 228: return kCGEventFlagMaskControl;
337
- case 225: case 229: return kCGEventFlagMaskShift;
338
- case 226: case 230: return kCGEventFlagMaskAlternate;
339
- case 227: case 231: return kCGEventFlagMaskCommand;
340
- default: return 0;
341
- }
342
- }
343
-
344
- /** Modifiers held right now, by usage, so each event carries the flags a real
345
- * keyboard's would and a chord is a chord on the far side. */
346
- static CGEventFlags gFlags = 0;
347
- static BOOL gLeftDown = NO, gRightDown = NO;
348
- static CFAbsoluteTime gLastDown = 0;
349
- static CGPoint gLastDownAt;
350
- static int64_t gClicks = 1;
351
-
352
- static void post(CGEventRef event) {
353
- if (!event) return;
354
- CGEventSetFlags(event, gFlags | CGEventGetFlags(event));
355
- CGEventPost(kCGHIDEventTap, event);
356
- CFRelease(event);
357
- }
358
-
359
- static double ratio(id value) {
360
- double n = [value isKindOfClass:NSNumber.class] ? [value doubleValue] : 0;
361
- if (!(n >= 0)) return 0;
362
- return n > 1 ? 1 : n;
363
- }
364
-
365
- static CGPoint pointFor(NSDictionary *cmd) {
366
- CGRect bounds = CGDisplayBounds(CGMainDisplayID());
367
- return CGPointMake(bounds.origin.x + ratio(cmd[@"x"]) * (bounds.size.width - 1),
368
- bounds.origin.y + ratio(cmd[@"y"]) * (bounds.size.height - 1));
369
- }
370
-
371
- static void handlePointer(NSDictionary *cmd) {
372
- NSString *phase = cmd[@"phase"];
373
- BOOL right = [cmd[@"button"] isEqual:@"right"];
374
- CGPoint at = pointFor(cmd);
375
- CGMouseButton button = right ? kCGMouseButtonRight : kCGMouseButtonLeft;
376
- CGEventType type;
377
- if ([phase isEqual:@"down"]) {
378
- type = right ? kCGEventRightMouseDown : kCGEventLeftMouseDown;
379
- // A second press soon after, in the same place, is a double click, and
380
- // macOS only treats it as one when the event says so.
381
- CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
382
- BOOL near = fabs(at.x - gLastDownAt.x) < 4 && fabs(at.y - gLastDownAt.y) < 4;
383
- gClicks = (!right && near && now - gLastDown < NSEvent.doubleClickInterval) ? gClicks + 1 : 1;
384
- gLastDown = now;
385
- gLastDownAt = at;
386
- if (right) gRightDown = YES; else gLeftDown = YES;
387
- } else if ([phase isEqual:@"up"]) {
388
- type = right ? kCGEventRightMouseUp : kCGEventLeftMouseUp;
389
- if (right) gRightDown = NO; else gLeftDown = NO;
390
- } else if ([phase isEqual:@"move"]) {
391
- if (gLeftDown) { type = kCGEventLeftMouseDragged; button = kCGMouseButtonLeft; }
392
- else if (gRightDown) { type = kCGEventRightMouseDragged; button = kCGMouseButtonRight; }
393
- else type = kCGEventMouseMoved;
394
- } else {
395
- return;
396
- }
397
- CGEventRef event = CGEventCreateMouseEvent(NULL, type, at, button);
398
- if (event && type != kCGEventMouseMoved) CGEventSetIntegerValueField(event, kCGMouseEventClickState, gClicks);
399
- post(event);
400
- }
401
-
402
- static void handleScroll(NSDictionary *cmd) {
403
- id dx = cmd[@"dx"], dy = cmd[@"dy"];
404
- int32_t x = [dx isKindOfClass:NSNumber.class] ? (int32_t)MAX(-2000, MIN(2000, [dx doubleValue])) : 0;
405
- int32_t y = [dy isKindOfClass:NSNumber.class] ? (int32_t)MAX(-2000, MIN(2000, [dy doubleValue])) : 0;
406
- // A browser's deltas grow downward and rightward; a scroll event's the other way.
407
- post(CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitPixel, 2, -y, -x));
408
- }
409
-
410
- static void handleKey(NSDictionary *cmd) {
411
- id code = cmd[@"code"], down = cmd[@"down"];
412
- if (![code isKindOfClass:NSNumber.class] || ![down isKindOfClass:NSNumber.class]) return;
413
- int usage = [code intValue];
414
- uint8_t key = keyCodeFor(usage);
415
- if (key == kNoKey) return;
416
- BOOL pressed = [down boolValue];
417
- CGEventFlags flag = flagFor(usage);
418
- if (flag) gFlags = pressed ? (gFlags | flag) : (gFlags & ~flag);
419
- post(CGEventCreateKeyboardEvent(NULL, key, pressed));
420
- }
421
-
422
- /** A paste: typed as the characters themselves, whatever the Mac's layout. */
423
- static void handleText(NSDictionary *cmd) {
424
- NSString *text = cmd[@"text"];
425
- if (![text isKindOfClass:NSString.class]) return;
426
- CGEventFlags held = gFlags;
427
- gFlags = 0;
428
- for (NSUInteger i = 0; i < text.length && i < 2000; i++) {
429
- unichar ch = [text characterAtIndex:i];
430
- for (int pressed = 1; pressed >= 0; pressed--) {
431
- CGEventRef event = CGEventCreateKeyboardEvent(NULL, 0, pressed);
432
- if (!event) continue;
433
- CGEventKeyboardSetUnicodeString(event, 1, &ch);
434
- CGEventPost(kCGHIDEventTap, event);
435
- CFRelease(event);
436
- }
437
- usleep(2000);
438
- }
439
- gFlags = held;
440
- }
441
-
442
- static void readCommands(void) {
443
- char *line = NULL; size_t cap = 0; ssize_t len;
444
- while ((len = getline(&line, &cap, stdin)) > 0) {
445
- @autoreleasepool {
446
- NSData *data = [NSData dataWithBytes:line length:(NSUInteger)len];
447
- NSDictionary *cmd = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
448
- if (![cmd isKindOfClass:NSDictionary.class]) continue;
449
- // Without Accessibility every event below is dropped by the system with
450
- // no error, so say so once rather than look broken.
451
- static BOOL warned = NO;
452
- if (!canControl()) {
453
- if (!warned) { warned = YES; say(permissions()); }
454
- continue;
455
- }
456
- NSString *type = cmd[@"type"];
457
- if ([type isEqual:@"pointer"]) handlePointer(cmd);
458
- else if ([type isEqual:@"scroll"]) handleScroll(cmd);
459
- else if ([type isEqual:@"key"]) handleKey(cmd);
460
- else if ([type isEqual:@"text"]) handleText(cmd);
461
- }
462
- }
463
- // The runner closed our stdin: it is gone, or done with us.
464
- exit(0);
465
- }
466
-
467
- int main(int argc, char *const argv[]) {
468
- disclaimResponsibility(argv);
469
- @autoreleasepool {
470
- setvbuf(stderr, NULL, _IONBF, 0);
471
- BOOL statusOnly = NO, request = NO;
472
- for (int i = 1; i < argc; i++) {
473
- if (!strcmp(argv[i], "--status")) statusOnly = YES;
474
- else if (!strcmp(argv[i], "--request")) request = YES;
475
- else if (!strcmp(argv[i], "--fps") && i + 1 < argc) gFPS = atoi(argv[++i]);
476
- else if (!strcmp(argv[i], "--width") && i + 1 < argc) gWidth = atoi(argv[++i]);
477
- else if (!strcmp(argv[i], "--bitrate") && i + 1 < argc) gBitrate = atoi(argv[++i]);
478
- }
479
- if (gFPS < 1) gFPS = 1;
480
- if (gFPS > 60) gFPS = 60;
481
-
482
- // Asking is what puts "Factiii Screen" in System Settings with a switch to
483
- // turn on. macOS shows each prompt once; after that the owner uses the
484
- // switch, and these calls return at once.
485
- if (request) {
486
- if (!canCapture()) CGRequestScreenCaptureAccess();
487
- if (!canControl())
488
- AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)@{ (__bridge id)kAXTrustedCheckOptionPrompt: @YES });
489
- }
490
- say(permissions());
491
- if (statusOnly) return 0;
492
- if (!canCapture()) return 3;
493
-
494
- gWrite = dispatch_queue_create("screen.write", DISPATCH_QUEUE_SERIAL);
495
- startCapture();
496
- dispatch_async(dispatch_queue_create("screen.input", DISPATCH_QUEUE_SERIAL), ^{ readCommands(); });
497
- [[NSRunLoop currentRunLoop] run];
498
- }
499
- return 0;
500
- }
@@ -1,69 +0,0 @@
1
- ---
2
- name: factiii-workspace
3
- description: You are on a factiii runner ($FACTIII_SPACE is set) - a person's real machine, driven from a board, not a sandbox. Work in the directory you started in; build output goes to $BUILDS_DIR, backups and dumps to $BACKUPS_DIR, files for the user to $SCRATCHPAD, servers on a port from $FACTIII_PORTS. TRIGGER before writing a build artifact, dump or export, before binding a port, before touching anything outside your working directory, and whenever you wonder where you are running.
4
- ---
5
-
6
- # factiii-workspace
7
-
8
- `echo $FACTIII_SPACE` prints a board's name. That means this session was
9
- started by a factiii runner for that board, and everything below applies.
10
-
11
- ## Where you are
12
-
13
- - **Someone's real machine.** Not a container, not CI, not a sandbox. Nothing
14
- resets when you finish, other people's work is on the same disk, and the
15
- person who started you may be watching from a phone.
16
- - **Driven from a board.** You were started for one card, or as a terminal
17
- someone named. Other sessions for the same board run beside you at the same
18
- time.
19
- - **In your own working directory.** The directory this session started in is
20
- yours: for a git board it is a worktree on its own branch, cut from a clone
21
- every session of the board shares. Do your work there.
22
-
23
- ## The directories the runner gives you
24
-
25
- Use the variables. Never type one of these paths out, and never work out a
26
- sibling directory from your own path: the layout is the runner's to change.
27
-
28
- | Variable | What goes there |
29
- |---|---|
30
- | (your starting directory) | The code you are changing. Nothing else. |
31
- | `$BUILDS_DIR` | Build output: bundles, binaries, images, archives. One directory per build, e.g. `$BUILDS_DIR/<commit>`. |
32
- | `$BACKUPS_DIR` | Anything that must outlive this session: database dumps, exports you may need again. |
33
- | `$SCRATCHPAD` | Anything the user should see or receive: a screenshot, a chart, a report, a file they asked for. See the `scratchpad` skill. |
34
- | `$WORKSPACE` | The deploy checkout of the default branch. Read it if you need to; do not work in it. A deploy resets it and deletes every untracked file. |
35
-
36
- Keep build output and dumps out of your working directory. They end up in a
37
- diff, in a commit, or deleted with the worktree when the session ends.
38
-
39
- ## Ports
40
-
41
- `$FACTIII_PORTS` is a comma-separated list of ports reserved for this board on
42
- this machine. Start a server on one of those. A port you pick yourself can
43
- already belong to another board's server on the same host, and the failure
44
- shows up in that board's session, not yours.
45
-
46
- To give a port a public URL, use the `share-a-port` skill.
47
-
48
- ## Secrets
49
-
50
- Nothing in your environment holds a deploy credential, and none is stored on
51
- disk for you to find. Do not look in `~/.aws`, `~/.ssh`, `.env` files or the
52
- keychain. A command that needs one runs through `factiii-secrets`: see the
53
- `factiii-secrets` skill.
54
-
55
- ## How work leaves
56
-
57
- Commit on the branch you were given. A person reviews it and merges it from
58
- the board. Do not push to the default branch and do not merge your own branch.
59
-
60
- ## Never touch
61
-
62
- - **The shared clone your worktree was cut from, or another session's
63
- worktree.** `git worktree` commands, a branch checkout or a `git clean` there
64
- breaks sessions that are running right now.
65
- - **The runner's state directory** (the `state` directory beside the
66
- worktrees). It holds the store every session on this machine reads its state
67
- from. Deleting or "cleaning" it destroys other people's sessions.
68
- - **Anything above your working directory that a variable did not point you
69
- to.** If a task seems to need it, stop and ask the user.
Binary file
Binary file
Binary file
Binary file