
Abstract This paper presents a serverless architecture that overcomes the stateless...

This paper presents a serverless architecture that overcomes the stateless nature and 6-minute execution limit of Google Apps Script (GAS). By configuring a 1-second immediate timeout in UrlFetchApp loopback calls, an orchestrator dispatches background tasks and terminates immediately. This design frees up the caller's execution quota while the target Web App runs to completion in an isolated container. Combined with a transactional Google Sheets state machine, this design supports self-perpetuating parallel MapReduce runs and multi-turn, state-hydrated generative AI agent networks without external compute infrastructure.
Google Apps Script (GAS) is the premier low-code platform for integrating, automating, and extending Google Workspace Ref. While universally accessible and free, GAS is constrained by strict runtime limits—most notably a hard 6-minute execution cap per script instance and a synchronous, stateless container lifecycle Ref.
Although the compilation shift to the V8 engine significantly reduced processing costs, handling large-scale ETL pipelines, parallel computations, or multi-agent generative AI simulations remains highly challenging. Traditional workarounds, such as time-driven triggers, suffer from high initialization latency, trigger omissions, and strict platform quotas.
Earlier asynchronous attempts, such as the fetchAll-based RunAll framework, still remained bound by the parent container's 6-minute ceiling. However, the addition of the timeoutSeconds parameter to the UrlFetchApp utility Ref enables a new approach. Integrating aggressive timeouts, self-referential Web App loopbacks Ref, and a shared spreadsheet state ledger makes it possible to execute highly parallel, self-perpetuating, and infinite MapReduce operations that easily bypass the 6-minute wall.
The complete, refactored production source code, containing full implementations of the asynchronous engine, concurrency controllers, and state preservation layers, is hosted on GitHub:
https://github.com/tanaikech/gas-asynchronous-mapreduce-broker
To model this bypass mathematically, let N represent the total number of long-running computational or inferential tasks, and T_task represent the processing duration of a single task. In a traditional, single-threaded synchronous execution model, the total execution time T_sync is represented by:
T_sync = T_task,1 + T_task,2 + ... + T_task,N + T_overhead_sync
Under Google's runtime policies Ref, execution is forcefully terminated if:
T_sync >= 360 seconds
Even with synchronous parallelization utilizing UrlFetchApp.fetchAll Ref, the system remains bound by the slowest worker in the batch:
T_para = max(T_task,1, T_task,2, ..., T_task,N) + T_overhead_para
If the slowest task exceeds 360 seconds, or if the parent thread's aggregation overhead exceeds the limit, the parent container crashes, causing data loss.
Our paradigm bypasses this constraint by introducing a non-blocking asynchronous dispatch model. We configure UrlFetchApp.fetch with a parameter configuration of timeoutSeconds: 1 and muteHttpExceptions: true. When pointed at the script's own Web App URL Ref, the following sequence occurs:
Let T_hop represent the combined latency of the Web App invocation, authorization handshake, and spreadsheet state-saving. Because the orchestrator dispatches tasks asynchronously with a 1-second timeout, the orchestrator's active running time T_orch scales at O(1) relative to task execution:
T_orch = sum(T_timeoutSeconds for j from 1 to M) + T_overhead_orch ≈ M \* 1s + T_overhead_orch
where M is the number of active dispatches in the current batch. The actual heavy computational tasks run entirely in parallel background containers:
T_background = max(T_task,1, T_task,2, ..., T_task,C) + T_hop
where C is the concurrency limit. Once a worker finishes, it writes its state to the Shared Sheet and executes an autonomous loopback HTTP call to wake up the Dispatcher. The orchestrator container is thus recycled continuously, dodging the 360-second limit entirely.
Consequently, when this loopback mechanism is initiated via a Time-driven Trigger, the trigger execution context terminates cleanly within 1.0 seconds (consuming virtually zero quota), while the background Web App container obtains a full, independent execution window that bypasses the trigger's inherent time limits. This represents an important optimization for building highly resilient, long-running systems within Google Workspace.
This parallel execution framework relies on six core engineering innovations to manage state, concurrency, and error handling.
To prevent API rate limits (such as Google’s HTTP 429 exceptions or lock contention), tasks are not dispatched all at once. Instead, an independent background daemon (the Dispatcher) acts as a load balancer, continuously auditing the active queue and enforcing a strict concurrency cap (e.g., maximum of 5 concurrent workers). When a worker completes its task, it executes an autonomous loopback HTTP call to wake up the Dispatcher, which then launches the next pending task in the queue.
Since GAS Web App containers are stateless and initialize a clean memory space on every doPost invocation, in-memory state tracking is impossible. Our design repurposes Google Sheets as a persistent, transactional memory engine. Every task state (PENDING, RUNNING, COMPLETED, ERROR, CANCELLED) and historical conversation log is written directly to the sheet, providing a durable ledger that survives container restarts.
In the Reducer phase, when multiple workers finish almost simultaneously, they all attempt to wake up the Dispatcher to trigger the final consolidation step. This introduces a race condition where multiple threads might try to run the reduction logic concurrently. Our architecture resolves this by using the GAS sheet creation API as a mutex. The code attempts to programmatically insert a uniquely named results sheet (insertSheet). If a duplicate sheet creation is attempted, the underlying engine throws a catchable exception, which immediate aborts duplicate threads and ensures only one thread executes the final reduction.
In multi-agent environments, blocking execution while waiting for external API calls dramatically reduces throughput. Our framework completely bypasses global script locks during long-lived LLM network communications. Instead, workers execute their API queries in parallel, holding a localized DocumentLock only during the sub-second write-back operations to the shared spreadsheet, maximizing concurrency.
If a worker encounters an error (such as an API timeout, JSON parsing fault, or safety block), it writes the failure trace to the queue log. Rather than crashing the entire pipeline, the Dispatcher intercepts the failure and launches a specialized error-correction LLM loop. This sub-agent analyzes the failed prompt and the resulting error message, dynamically rewrites the prompt to bypass the issue, and queues the task for an automated retry.
Continuous logging can quickly exceed the physical limit of Google Sheets (10 million cells), degrading performance. To prevent this, the logging engine monitors raw log row counts in real-time. If the log sheet exceeds a safe operating threshold (e.g., 1000 rows), the engine automatically truncates and rotates older entries, maintaining fast, lightweight write operations indefinitely.
Our research validates this architecture across two distinct design patterns.
This topology establishes a decentralized broker that processes heavy synthetic tasks concurrently, using a Shared Google Sheet as an atomic transactional queue ledger Ref.
This configuration manages conversational states across multi-step execution. Using the GASADK library Ref, it orchestrates multiple specialized AI agents (Lead Engineer, Resource Director, Colony Governor) through progressive conversational turns, maintaining context and execution histories on a shared ledger.
The diagram below shows how conversational state ($\mathcal{H}$) accumulates across progressive execution turns, demonstrating how history is preserved and serialized across stateless containers.
Follow these steps to deploy and verify this architecture within a Google Workspace environment.
GASADK Project Key Ref: 1w2mwhWQd4_6rom-UBRPD8gayBoqGH_87awSBVqGI8DdaQI_pOeSuGYDuGASADK, and click Add.GEMINI_API_KEY and populate it with a valid API key obtained from Google AI Studio./exec endpoint anonymously)WEB_APP_URL variable.startTest4_1 (for Topology 1) or startUnifiedStatefulMapReduce (for Topology 2) and click Run. Navigate back to the spreadsheet. You will see the real-time creation of the log sheets and the concurrent execution of tasks as they transition from PENDING to RUNNING and COMPLETED.emergencyHaltNetwork function. Verify that all remaining PENDING queue entries immediately transition to CANCELLED and execution halts gracefully within one cycle.To modify the task definitions or agent roles, you do not need to rewrite the underlying execution engine. The engine is completely decoupled from the execution logic by parsing a declarative MISSION_BLUEPRINT configuration.
For instance, to re-engineer the system from the Martian colonization task to an AI Venture Strategy Startup Roadmap, you can update the MISSION_BLUEPRINT object as follows:
/**
* MISSION_BLUEPRINT
* Declaratively defines the agent structure, step counts, prompts, and reduction rules.
*/
const MISSION_BLUEPRINT = {
MAX_TURNS: 3,
MAX_RETRIES: 2,
AGENTS: [
{
id: "CHAIN-STARTUP-CEO",
initialPrompt:
"You are the CEO of a medical AI startup. Detail the core business model, value proposition, and customer acquisition strategy.",
},
{
id: "CHAIN-STARTUP-CTO",
initialPrompt:
"You are the CTO of a medical AI startup. Detail the software architecture, data privacy measures, and chosen model pipeline.",
},
{
id: "CHAIN-STARTUP-CMO",
initialPrompt:
"You are the CMO of a medical AI startup. Detail the product launch marketing strategy, branding, and conversion funnel.",
},
],
TURN_PROMPTS: {
2: "COMPETITION UPDATE: A tech giant has announced a similar free medical AI feature. Based on your previous plans, how do we pivot and build a defensive moat?",
3: "PITCH STAGE: The pivot is finalized. Write your department's definitive operational plan, synthesizing the baseline startup idea with the new defensive moat strategies.",
},
REDUCER: {
id: "CHAIN-VC-LEAD-INVESTOR",
prompt:
"You are the Lead VC Investor analyzing this startup. Synthesize the pitch reports from the CEO, CTO, and CMO into a single 'Start-up Investment Prospectus & 12-Month Execution Roadmap'. Highlight risk mitigations.\n\n",
},
};
Saving this configuration and running the initializer dynamically launches the CEO, CTO, and CMO agents in parallel, injects the competitive pivot threat on Turn 2, and aggregates the results into a cohesive VC prospectus in FinalResult_Plan3.
We evaluate both topologies using empirical runtime execution data.
We configured Topology 1 with 20 heavy synthetic tasks, each simulating a 120-second workload. Using our token-bucket design, the concurrency limit C was capped at 5.

Analyzing the execution ledger QueueLog_4_1, we observe highly coordinated state progression:
06:06:45.401Z through 06:06:45.434Z. Each of these five worker threads executed in parallel.06:08:49.712Z.06:10:54.481Z.06:12:58.694Z and completed at 06:12:58.739Z.06:15:05.096Z.Total elapsed wall time was 500 seconds (8 minutes and 20 seconds). In a standard synchronous thread, this execution would have suffered a hard crash at exactly 360 seconds. Our asynchronous architecture sustained operations past the execution wall, completing all 20 tasks, sorting the resulting cryptographic tokens sequentially, and outputting a structured manifest in FinalResult_4_1.
We evaluated the multi-agent coordination of Topology 2 under dynamic environmental shifts using execution logs from Plan 3.3.
The entire state-space transitioned from initial manual trigger to finalized manifesto synthesis in 53.39 seconds (from the start log at 02:47:46.791Z to the system complete log at 02:48:40.181Z). Under the legacy synchronous paradigm (Plan 3.0), the identical workflow required 71.34 seconds, demonstrating an absolute speedup of 25.16%. By reducing the idle worker loopback delay (WORKER_START_DELAY_MS) from 3000ms to 1000ms in Plan 3.3, we eliminated 8.0 seconds of unnecessary container wait states, driving the system close to its mathematical minimum latency bound.
To confirm actual multi-threaded execution within Google’s infrastructure, we audited the start/end timestamps of Turn 1 (Step 1/3) agents:
CHAIN-MARS-ENGINEER: Executed from 02:47:52.709Z to 02:47:55.350Z (duration = 2.641 seconds)CHAIN-MARS-GOVERNOR: Executed from 02:47:52.789Z to 02:47:56.323Z (duration = 3.534 seconds)CHAIN-MARS-RESOURCES: Executed from 02:47:54.663Z to 02:47:57.695Z (duration = 3.032 seconds)Because all three workers started execution within an 1.8-second window and processed their tasks concurrently, the total step duration was bounded by the slowest task:
T_Step1 = max(2.641, 3.534, 3.032) = 3.534 seconds
In a sequential, single-threaded engine, this step would require the sum of all tasks:
T_Sequential = 2.641 + 3.534 + 3.032 = 9.207 seconds
Our parallel dispatcher achieved a 61.6% latency reduction on Turn 1 alone. This efficiency gain compounded across subsequent turns, demonstrating the scalability of our approach.

The synthesis step was evaluated using the final strategic manifesto written to FinalResult_Plan3. The Reducer successfully hydrated and reconciled the outputs of the three agents:
The resulting "Mars Master Manifesto" synthesized these domain-specific strategic recommendations into a unified, chronological action plan (Phases I–III). This successful aggregation confirms the semantic consistency and preservation of conversational history throughout the parallel, stateless execution cycles of the network.
Evaluating this framework for production workloads requires analyzing its performance characteristics and operational trade-offs compared to traditional cloud environments.
Executing complex applications entirely within Google Apps Script introduces significant operational overhead compared to dedicated cloud compute engines such as AWS Lambda, GCP Cloud Run, or Cloud Functions. Spawning GAS Web Apps, validating OAuth tokens, initiating Spreadsheet or Property service I/O, calling LLM endpoints, and writing state back to the shared ledger introduces a latency penalty of approximately 3 to 5 seconds per execution hop. This latency is caused by container cold starts, authorization handshakes, and API serialization.
Consequently, this architecture is computationally inefficient and should not be used when native external cloud options are viable. Instead, it serves as "The Ultimate Last Resort" for compliance-restricted enterprise scenarios. When strict organizational regulations prohibit data egress to external, non-Workspace cloud environments, this framework allows developers to build secure, parallel, and self-healing systems directly within the compliant Workspace boundary.
Architectural planning must account for the platform's execution and storage limits:
LockService timeouts are set to 30 seconds. While sufficient for low-concurrency runs, lock contention scales non-linearly with task volume. We mitigate this by holding document-level locks only during sub-second write-back operations, bypassing script-level locks entirely during long API calls.UrlFetchApp quotas (20,000 requests for consumer accounts; 100,000 requests for Google Workspace accounts).We have presented an asynchronous, self-healing, and self-perpetuating MapReduce framework designed to run natively within Google Apps Script. By utilizing a 1-second timeout loopback protocol, our design bypasses the legacy 6-minute wall-time limit, allowing workflows to scale indefinitely.
This design opens up new development possibilities for secure, in-suite architectures. Future work will investigate integrating this loopback model with the Model Context Protocol (MCP) and Agent-to-Agent (A2A) networks. This will lay the groundwork for secure, federated agent networks capable of autonomous negotiation and execution entirely within enterprise office suites.
This paper presents an asynchronous serverless framework that overcomes the stateless nature and 6-minute execution limit of Google Apps Script (GAS) Ref. By configuring a 1-second timeout parameter in UrlFetchApp loopback calls Ref, the orchestrator can dispatch background tasks and terminate immediately. This frees up the caller's execution quota while the target Web App runs to completion in an isolated container. Combined with a transactional Google Sheets state machine Ref, this design supports self-perpetuating parallel MapReduce runs and multi-turn, state-hydrated generative AI agent networks without external compute infrastructure.
aiHave you ever tried to read a massive pile of reports and summarize them in under 50 words? It’s...
aieIn his keynote on Wednesday, Benoit Schillings, vice president of Technology at Google DeepMind and...
geminiThis article covers the MCP setup and configuration for using Google Omni Preview and underlying...
ai✨ Overview This article explores how Gemini tokenizes data and demonstrates how to count...
aieYesterday, we kicked off our physical newspaper, The Daily Context, at the AI Engineer World’s Fair...
aieWe (at DEV and MLH) are covering AI Engineer's World Fair by printing a physical newspaper called...
Workflows from the Neura Market marketplace related to this Gemini resource